Skip to content

Commit 635ba7a

Browse files
authored
Merge pull request #117 from kamadorueda/kamadorueda
feat: start attr set in same line
2 parents 94ed5ec + e92172f commit 635ba7a

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

src/rules/let_in.rs

+46-20
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ pub fn rule(
8080
}
8181
}
8282

83-
// in
84-
let child = children.get_next().unwrap();
85-
let indent = build_ctx.pos_new.column > 1;
8683
match layout {
8784
crate::config::Layout::Tall => {
8885
steps.push_back(crate::builder::Step::Dedent);
@@ -93,40 +90,69 @@ pub fn rule(
9390
steps.push_back(crate::builder::Step::Whitespace);
9491
}
9592
}
96-
steps.push_back(crate::builder::Step::Format(child.element));
93+
94+
// in
95+
let child_in = children.get_next().unwrap();
96+
let top_level = build_ctx.pos_new.column <= 1;
97+
98+
// /**/
99+
let mut child_comments = std::collections::LinkedList::new();
100+
children.drain_comments_and_newlines(|element| match element {
101+
crate::children::DrainCommentOrNewline::Comment(text) => {
102+
child_comments.push_back(crate::builder::Step::Comment(text))
103+
}
104+
crate::children::DrainCommentOrNewline::Newline(_) => {}
105+
});
106+
107+
// expr
108+
let child_expr = children.get_next().unwrap();
109+
110+
// in
111+
steps.push_back(crate::builder::Step::Format(child_in.element));
97112
match layout {
98113
crate::config::Layout::Tall => {
99-
if indent {
100-
steps.push_back(crate::builder::Step::Indent);
114+
if child_comments.is_empty()
115+
&& matches!(
116+
child_expr.element.kind(),
117+
rnix::SyntaxKind::NODE_ATTR_SET
118+
| rnix::SyntaxKind::NODE_LET_IN
119+
| rnix::SyntaxKind::NODE_LIST
120+
| rnix::SyntaxKind::NODE_PAREN
121+
| rnix::SyntaxKind::NODE_STRING
122+
)
123+
{
124+
steps.push_back(crate::builder::Step::Whitespace);
125+
} else {
126+
if !top_level {
127+
steps.push_back(crate::builder::Step::Indent);
128+
}
129+
steps.push_back(crate::builder::Step::NewLine);
130+
steps.push_back(crate::builder::Step::Pad);
101131
}
102132
}
103133
crate::config::Layout::Wide => {}
104134
}
105135

106136
// /**/
107-
children.drain_comments_and_newlines(|element| match element {
108-
crate::children::DrainCommentOrNewline::Comment(text) => {
109-
steps.push_back(crate::builder::Step::NewLine);
110-
steps.push_back(crate::builder::Step::Pad);
111-
steps.push_back(crate::builder::Step::Comment(text));
112-
}
113-
crate::children::DrainCommentOrNewline::Newline(_) => {}
114-
});
137+
for comment in child_comments {
138+
steps.push_back(comment);
139+
steps.push_back(crate::builder::Step::NewLine);
140+
steps.push_back(crate::builder::Step::Pad);
141+
}
115142

116143
// expr
117-
let child = children.get_next().unwrap();
118144
match layout {
119145
crate::config::Layout::Tall => {
120-
steps.push_back(crate::builder::Step::NewLine);
121-
steps.push_back(crate::builder::Step::Pad);
122-
steps.push_back(crate::builder::Step::FormatWider(child.element));
123-
if indent {
146+
steps.push_back(crate::builder::Step::FormatWider(
147+
child_expr.element,
148+
));
149+
if !top_level {
124150
steps.push_back(crate::builder::Step::Dedent);
125151
}
126152
}
127153
crate::config::Layout::Wide => {
128154
steps.push_back(crate::builder::Step::Whitespace);
129-
steps.push_back(crate::builder::Step::Format(child.element));
155+
steps.push_back(crate::builder::Step::Format(child_expr.element));
130156
}
131157
}
132158

tests/cases/let_in/in

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ let
2525
a = let /*b*/ c=1; /*d*/ in /*e*/ f;
2626
/**/
2727

28+
a = let
29+
in [
30+
1
31+
2
32+
];
2833

2934
in
3035

tests/cases/let_in/out

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ let
8181
*/
8282
f;
8383
/**/
84+
85+
a = let
86+
in [
87+
1
88+
2
89+
];
8490
in
8591
/**/
8692
a

0 commit comments

Comments
 (0)