Skip to content

Commit de6088b

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

File tree

14 files changed

+273
-267
lines changed

14 files changed

+273
-267
lines changed

src/alejandra_engine/src/builder.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,7 @@ fn format(
182182
// { }
183183
rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule,
184184
// a $op b
185-
rnix::SyntaxKind::NODE_BIN_OP => {
186-
crate::rules::bin_op_and_or_default::rule
187-
}
185+
rnix::SyntaxKind::NODE_BIN_OP => crate::rules::bin_op::rule,
188186
// ${a} (interpolation but for NODE_SELECT)
189187
rnix::SyntaxKind::NODE_DYNAMIC => crate::rules::dynamic::rule,
190188
// $identifier
@@ -213,9 +211,7 @@ fn format(
213211
// let { }
214212
rnix::SyntaxKind::NODE_LEGACY_LET => crate::rules::default,
215213
// a or b
216-
rnix::SyntaxKind::NODE_OR_DEFAULT => {
217-
crate::rules::bin_op_and_or_default::rule
218-
}
214+
rnix::SyntaxKind::NODE_OR_DEFAULT => crate::rules::bin_op::rule,
219215
// ( a )
220216
rnix::SyntaxKind::NODE_PAREN => crate::rules::paren::rule,
221217
// a | a ? b

src/alejandra_engine/src/children2.rs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use std::collections::LinkedList;
2+
3+
pub(crate) enum Trivia {
4+
Comment(String),
5+
Newlines(usize),
6+
}
7+
8+
pub(crate) struct Child {
9+
pub element: rnix::SyntaxElement,
10+
11+
pub inline_comment: Option<String>,
12+
pub has_inline_comment: bool,
13+
14+
pub trivialities: LinkedList<Trivia>,
15+
pub has_comments: bool,
16+
pub has_trivialities: bool,
17+
}
18+
19+
pub(crate) fn new(
20+
build_ctx: &crate::builder::BuildCtx,
21+
node: &rnix::SyntaxNode,
22+
) -> std::collections::linked_list::IntoIter<Child> {
23+
let mut children = crate::children::Children::new(build_ctx, node);
24+
25+
let mut elements = LinkedList::new();
26+
27+
while let Some(element) = children.get_next() {
28+
let mut inline_comment = None;
29+
let mut trivialities = LinkedList::new();
30+
31+
children.drain_trivia(|element| match element {
32+
crate::children::Trivia::Comment(text) => {
33+
if trivialities.is_empty() && text.starts_with('#') {
34+
inline_comment = Some(text);
35+
} else {
36+
trivialities.push_back(Trivia::Comment(text));
37+
}
38+
}
39+
crate::children::Trivia::Whitespace(text) => {
40+
let newlines = crate::utils::count_newlines(&text);
41+
42+
if newlines > 0 {
43+
trivialities.push_back(Trivia::Newlines(newlines))
44+
}
45+
}
46+
});
47+
48+
let has_inline_comment = inline_comment.is_some();
49+
let has_comments = trivialities
50+
.iter()
51+
.any(|trivia| matches!(trivia, Trivia::Comment(_)));
52+
let has_trivialities = !trivialities.is_empty();
53+
54+
elements.push_back(Child {
55+
element,
56+
57+
inline_comment,
58+
has_inline_comment,
59+
60+
trivialities,
61+
has_comments,
62+
has_trivialities,
63+
})
64+
}
65+
66+
elements.into_iter()
67+
}

src/alejandra_engine/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
2929

3030
pub(crate) mod builder;
3131
pub(crate) mod children;
32+
pub(crate) mod children2;
3233
pub mod format;
3334
pub(crate) mod parsers;
3435
pub(crate) mod position;

src/alejandra_engine/src/parsers/apply.rs

-45
This file was deleted.

src/alejandra_engine/src/parsers/bin_op.rs

-66
This file was deleted.
-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
pub(crate) mod apply;
21
pub(crate) mod assert_or_with;
3-
pub(crate) mod bin_op;
42
pub(crate) mod if_else;
53
pub(crate) mod pattern;

src/alejandra_engine/src/rules/apply.rs

+31-20
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,48 @@ pub(crate) fn rule(
44
) -> std::collections::LinkedList<crate::builder::Step> {
55
let mut steps = std::collections::LinkedList::new();
66

7-
let parsed = crate::parsers::apply::Apply::parse(build_ctx, node);
7+
let mut children = crate::children2::new(build_ctx, node);
8+
9+
let first = children.next().unwrap();
10+
let second = children.next().unwrap();
811

912
let vertical = build_ctx.vertical
10-
|| !parsed.comments_after_left.is_empty()
11-
|| parsed.has_newlines_after_left;
13+
|| first.has_inline_comment
14+
|| first.has_trivialities
15+
|| second.has_inline_comment
16+
|| second.has_trivialities;
1217

13-
// left_expression
18+
// first
1419
if vertical {
15-
steps.push_back(crate::builder::Step::FormatWider(
16-
parsed.left_expression,
17-
));
20+
steps.push_back(crate::builder::Step::FormatWider(first.element));
1821
} else {
19-
steps.push_back(crate::builder::Step::Format(parsed.left_expression));
22+
steps.push_back(crate::builder::Step::Format(first.element));
2023
}
2124

22-
// comments_after_left
23-
let has_comments_after_left = !parsed.comments_after_left.is_empty();
24-
for text in parsed.comments_after_left {
25+
if let Some(text) = first.inline_comment {
26+
steps.push_back(crate::builder::Step::Whitespace);
27+
steps.push_back(crate::builder::Step::Comment(text));
2528
steps.push_back(crate::builder::Step::NewLine);
2629
steps.push_back(crate::builder::Step::Pad);
27-
steps.push_back(crate::builder::Step::Comment(text));
2830
}
2931

30-
// right_expression
32+
for trivia in first.trivialities {
33+
match trivia {
34+
crate::children2::Trivia::Comment(text) => {
35+
steps.push_back(crate::builder::Step::NewLine);
36+
steps.push_back(crate::builder::Step::Pad);
37+
steps.push_back(crate::builder::Step::Comment(text));
38+
}
39+
crate::children2::Trivia::Newlines(_) => {}
40+
}
41+
}
42+
43+
// second
3144
if vertical {
32-
if !has_comments_after_left
33-
&& !parsed.has_newlines_after_left
45+
if !first.has_inline_comment
46+
&& !first.has_trivialities
3447
&& matches!(
35-
parsed.right_expression.kind(),
48+
second.element.kind(),
3649
rnix::SyntaxKind::NODE_ATTR_SET
3750
| rnix::SyntaxKind::NODE_LIST
3851
| rnix::SyntaxKind::NODE_PAREN
@@ -44,12 +57,10 @@ pub(crate) fn rule(
4457
steps.push_back(crate::builder::Step::NewLine);
4558
steps.push_back(crate::builder::Step::Pad);
4659
};
47-
steps.push_back(crate::builder::Step::FormatWider(
48-
parsed.right_expression,
49-
));
60+
steps.push_back(crate::builder::Step::FormatWider(second.element));
5061
} else {
5162
steps.push_back(crate::builder::Step::Whitespace);
52-
steps.push_back(crate::builder::Step::Format(parsed.right_expression));
63+
steps.push_back(crate::builder::Step::Format(second.element));
5364
}
5465

5566
steps

0 commit comments

Comments
 (0)