Skip to content

Commit 8ed23bf

Browse files
committed
feat: support more inline comments
1 parent de6088b commit 8ed23bf

File tree

9 files changed

+161
-187
lines changed

9 files changed

+161
-187
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ Types of changes
1717
- Security in case of vulnerabilities.
1818
-->
1919

20+
### Added
21+
22+
- Binary operators now support inline comments:
23+
```diff
24+
- ++
25+
- # subsections go last
26+
+ ++ # subsections go last
27+
```
28+
2029
### Changed
2130

2231
- Linux binaries now use [mimalloc](https://github.com/microsoft/mimalloc)

src/alejandra_engine/src/builder.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ fn format(
176176
// a b
177177
rnix::SyntaxKind::NODE_APPLY => crate::rules::apply::rule,
178178
// assert a; b
179-
rnix::SyntaxKind::NODE_ASSERT => {
180-
crate::rules::assert_or_with::rule
181-
}
179+
rnix::SyntaxKind::NODE_ASSERT => crate::rules::scoped::rule,
182180
// { }
183181
rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule,
184182
// a $op b
@@ -239,9 +237,7 @@ fn format(
239237
// !a
240238
rnix::SyntaxKind::NODE_UNARY_OP => crate::rules::default,
241239
// with a; b
242-
rnix::SyntaxKind::NODE_WITH => {
243-
crate::rules::assert_or_with::rule
244-
}
240+
rnix::SyntaxKind::NODE_WITH => crate::rules::scoped::rule,
245241
kind => {
246242
panic!(
247243
"Missing rule for {:?} at: {}",

src/alejandra_engine/src/parsers/assert_or_with.rs

-74
This file was deleted.
-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
pub(crate) mod assert_or_with;
21
pub(crate) mod if_else;
32
pub(crate) mod pattern;

src/alejandra_engine/src/rules/assert_or_with.rs

-95
This file was deleted.

src/alejandra_engine/src/rules/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pub(crate) mod apply;
2-
pub(crate) mod assert_or_with;
32
pub(crate) mod attr_set;
43
pub(crate) mod bin_op;
54
pub(crate) mod dynamic;
@@ -14,6 +13,7 @@ pub(crate) mod pat_bind;
1413
pub(crate) mod pat_entry;
1514
pub(crate) mod pattern;
1615
pub(crate) mod root;
16+
pub(crate) mod scoped;
1717
pub(crate) mod select;
1818
pub(crate) mod string;
1919
pub(crate) mod string_interpol;
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
pub(crate) fn rule(
2+
build_ctx: &crate::builder::BuildCtx,
3+
node: &rnix::SyntaxNode,
4+
) -> std::collections::LinkedList<crate::builder::Step> {
5+
let mut steps = std::collections::LinkedList::new();
6+
7+
let mut children = crate::children2::new(build_ctx, node);
8+
9+
let first = children.next().unwrap();
10+
let second = children.next().unwrap();
11+
let third = children.next().unwrap();
12+
let fourth = children.next().unwrap();
13+
14+
let vertical = build_ctx.vertical
15+
|| first.has_inline_comment
16+
|| first.has_trivialities
17+
|| second.has_inline_comment
18+
|| second.has_trivialities
19+
|| third.has_inline_comment
20+
|| third.has_trivialities
21+
|| fourth.has_inline_comment
22+
|| fourth.has_trivialities;
23+
24+
// first
25+
steps.push_back(crate::builder::Step::Format(first.element));
26+
27+
if let Some(text) = first.inline_comment {
28+
steps.push_back(crate::builder::Step::Whitespace);
29+
steps.push_back(crate::builder::Step::Comment(text));
30+
steps.push_back(crate::builder::Step::NewLine);
31+
steps.push_back(crate::builder::Step::Pad);
32+
} else if first.has_comments {
33+
steps.push_back(crate::builder::Step::NewLine);
34+
steps.push_back(crate::builder::Step::Pad);
35+
} else {
36+
steps.push_back(crate::builder::Step::Whitespace);
37+
}
38+
39+
for trivia in first.trivialities {
40+
match trivia {
41+
crate::children2::Trivia::Comment(text) => {
42+
steps.push_back(crate::builder::Step::Comment(text));
43+
steps.push_back(crate::builder::Step::NewLine);
44+
steps.push_back(crate::builder::Step::Pad);
45+
}
46+
crate::children2::Trivia::Newlines(_) => {}
47+
}
48+
}
49+
50+
// second
51+
if vertical {
52+
steps.push_back(crate::builder::Step::FormatWider(second.element));
53+
} else {
54+
steps.push_back(crate::builder::Step::Format(second.element));
55+
}
56+
57+
// third
58+
steps.push_back(crate::builder::Step::Format(third.element));
59+
60+
if let Some(text) = third.inline_comment {
61+
steps.push_back(crate::builder::Step::Whitespace);
62+
steps.push_back(crate::builder::Step::Comment(text));
63+
steps.push_back(crate::builder::Step::NewLine);
64+
steps.push_back(crate::builder::Step::Pad);
65+
}
66+
67+
for trivia in third.trivialities {
68+
match trivia {
69+
crate::children2::Trivia::Comment(text) => {
70+
steps.push_back(crate::builder::Step::NewLine);
71+
steps.push_back(crate::builder::Step::Pad);
72+
steps.push_back(crate::builder::Step::Comment(text));
73+
}
74+
crate::children2::Trivia::Newlines(_) => {}
75+
}
76+
}
77+
78+
// fourth
79+
if vertical {
80+
if matches!(
81+
fourth.element.kind(),
82+
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
83+
) {
84+
steps.push_back(crate::builder::Step::NewLine);
85+
steps.push_back(crate::builder::Step::Pad);
86+
steps.push_back(crate::builder::Step::FormatWider(fourth.element));
87+
} else if third.has_inline_comment
88+
|| third.has_comments
89+
|| !matches!(
90+
fourth.element.kind(),
91+
rnix::SyntaxKind::NODE_ATTR_SET
92+
| rnix::SyntaxKind::NODE_IDENT
93+
| rnix::SyntaxKind::NODE_PAREN
94+
| rnix::SyntaxKind::NODE_LET_IN
95+
| rnix::SyntaxKind::NODE_LIST
96+
| rnix::SyntaxKind::NODE_LITERAL
97+
| rnix::SyntaxKind::NODE_STRING
98+
)
99+
{
100+
steps.push_back(crate::builder::Step::Indent);
101+
steps.push_back(crate::builder::Step::NewLine);
102+
steps.push_back(crate::builder::Step::Pad);
103+
steps.push_back(crate::builder::Step::FormatWider(fourth.element));
104+
steps.push_back(crate::builder::Step::Dedent);
105+
} else {
106+
steps.push_back(crate::builder::Step::Whitespace);
107+
steps.push_back(crate::builder::Step::FormatWider(fourth.element));
108+
}
109+
} else {
110+
steps.push_back(crate::builder::Step::Whitespace);
111+
steps.push_back(crate::builder::Step::Format(fourth.element));
112+
}
113+
114+
steps
115+
}

src/alejandra_engine/tests/cases/assert/in

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
[
2-
(assert b; c)
3-
(assert b; /*b*/ c)
4-
(assert /*a*/ b; c)
5-
(assert /*a*/ b; /*b*/ c)
2+
(assert b ; e)
3+
(assert b ; /*d*/ e)
4+
(assert b /*c*/; e)
5+
(assert b /*c*/; /*d*/ e)
6+
(assert /*a*/ b ; e)
7+
(assert /*a*/ b ; /*d*/ e)
8+
(assert /*a*/ b /*c*/; e)
9+
(assert /*a*/ b /*c*/; /*d*/ e)
610
( assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc )
711
( assert b;
812
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc )

0 commit comments

Comments
 (0)