Skip to content

Commit 421b1e9

Browse files
committed
refactor: split assert or with
1 parent 3071c38 commit 421b1e9

File tree

6 files changed

+173
-102
lines changed

6 files changed

+173
-102
lines changed

src/alejandra_engine/src/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ fn format(
177177
rnix::SyntaxKind::NODE_APPLY => crate::rules::apply::rule,
178178
// assert a; b
179179
rnix::SyntaxKind::NODE_ASSERT => {
180-
crate::rules::assert_and_with::rule
180+
crate::rules::assert_or_with::rule
181181
}
182182
// { }
183183
rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule,
@@ -244,7 +244,7 @@ fn format(
244244
rnix::SyntaxKind::NODE_UNARY_OP => crate::rules::default,
245245
// with a; b
246246
rnix::SyntaxKind::NODE_WITH => {
247-
crate::rules::assert_and_with::rule
247+
crate::rules::assert_or_with::rule
248248
}
249249
kind => {
250250
panic!(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use std::collections::LinkedList;
2+
3+
#[derive(Debug)]
4+
pub(crate) struct AssertOrWith {
5+
pub assert_or_with: rnix::SyntaxElement,
6+
pub comments_after_assert_or_with: LinkedList<String>,
7+
pub has_newlines_after_assert_or_with: bool,
8+
pub first_expression: rnix::SyntaxElement,
9+
pub semicolon: rnix::SyntaxElement,
10+
pub comments_after_semicolon: LinkedList<String>,
11+
pub has_newlines_after_semicolon: bool,
12+
pub second_expression: rnix::SyntaxElement,
13+
}
14+
15+
impl AssertOrWith {
16+
pub(crate) fn new(
17+
build_ctx: &crate::builder::BuildCtx,
18+
node: &rnix::SyntaxNode,
19+
) -> AssertOrWith {
20+
let mut children = crate::children::Children::new(build_ctx, node);
21+
22+
// assert_or_with
23+
let assert_or_with = children.get_next().unwrap();
24+
25+
// comments_after_assert_or_with
26+
// has_newlines_after_assert_or_with
27+
let mut comments_after_assert_or_with = LinkedList::new();
28+
let mut has_newlines_after_assert_or_with = false;
29+
children.drain_trivia(|element| match element {
30+
crate::children::Trivia::Comment(text) => {
31+
comments_after_assert_or_with.push_back(text);
32+
}
33+
crate::children::Trivia::Whitespace(text) => {
34+
has_newlines_after_assert_or_with =
35+
has_newlines_after_assert_or_with
36+
|| crate::utils::count_newlines(&text) > 0;
37+
}
38+
});
39+
40+
// first_expression
41+
let first_expression = children.get_next().unwrap();
42+
43+
// semicolon
44+
let semicolon = children.get_next().unwrap();
45+
46+
// comments_after_semicolon
47+
// has_newlines_after_semicolon
48+
let mut comments_after_semicolon = LinkedList::new();
49+
let mut has_newlines_after_semicolon = false;
50+
children.drain_trivia(|element| match element {
51+
crate::children::Trivia::Comment(text) => {
52+
comments_after_semicolon.push_back(text);
53+
}
54+
crate::children::Trivia::Whitespace(text) => {
55+
has_newlines_after_semicolon = has_newlines_after_semicolon
56+
|| crate::utils::count_newlines(&text) > 0;
57+
}
58+
});
59+
60+
// second_expression
61+
let second_expression = children.get_next().unwrap();
62+
63+
AssertOrWith {
64+
assert_or_with,
65+
comments_after_assert_or_with,
66+
has_newlines_after_assert_or_with,
67+
first_expression,
68+
semicolon,
69+
comments_after_semicolon,
70+
has_newlines_after_semicolon,
71+
second_expression,
72+
}
73+
}
74+
}
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub(crate) mod apply;
2+
pub(crate) mod assert_or_with;
23
pub(crate) mod if_else;
34
pub(crate) mod pattern;

src/alejandra_engine/src/rules/assert_and_with.rs

-99
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 parsed =
8+
crate::parsers::assert_or_with::AssertOrWith::new(build_ctx, node);
9+
10+
let vertical = build_ctx.vertical
11+
|| !parsed.comments_after_assert_or_with.is_empty()
12+
|| parsed.has_newlines_after_assert_or_with
13+
|| !parsed.comments_after_semicolon.is_empty()
14+
|| parsed.has_newlines_after_semicolon;
15+
16+
// assert_or_with
17+
steps.push_back(crate::builder::Step::Format(parsed.assert_or_with));
18+
19+
// comments_after_assert_or_with
20+
if parsed.comments_after_assert_or_with.is_empty() {
21+
steps.push_back(crate::builder::Step::Whitespace);
22+
} else {
23+
steps.push_back(crate::builder::Step::NewLine);
24+
steps.push_back(crate::builder::Step::Pad);
25+
for text in parsed.comments_after_assert_or_with {
26+
steps.push_back(crate::builder::Step::Comment(text));
27+
steps.push_back(crate::builder::Step::NewLine);
28+
steps.push_back(crate::builder::Step::Pad);
29+
}
30+
}
31+
32+
// first_expression
33+
if vertical {
34+
steps.push_back(crate::builder::Step::FormatWider(
35+
parsed.first_expression,
36+
));
37+
} else {
38+
steps.push_back(crate::builder::Step::Format(parsed.first_expression));
39+
}
40+
41+
// semicolon
42+
steps.push_back(crate::builder::Step::Format(parsed.semicolon));
43+
44+
// comments_after_semicolon
45+
let has_comments_after_semicolon =
46+
!parsed.comments_after_semicolon.is_empty();
47+
for text in parsed.comments_after_semicolon {
48+
steps.push_back(crate::builder::Step::NewLine);
49+
steps.push_back(crate::builder::Step::Pad);
50+
steps.push_back(crate::builder::Step::Comment(text));
51+
}
52+
53+
// second_expression
54+
if vertical {
55+
if matches!(
56+
parsed.second_expression.kind(),
57+
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
58+
) {
59+
steps.push_back(crate::builder::Step::NewLine);
60+
steps.push_back(crate::builder::Step::Pad);
61+
steps.push_back(crate::builder::Step::FormatWider(
62+
parsed.second_expression,
63+
));
64+
} else if has_comments_after_semicolon
65+
|| !matches!(
66+
parsed.second_expression.kind(),
67+
rnix::SyntaxKind::NODE_ATTR_SET
68+
| rnix::SyntaxKind::NODE_IDENT
69+
| rnix::SyntaxKind::NODE_PAREN
70+
| rnix::SyntaxKind::NODE_LET_IN
71+
| rnix::SyntaxKind::NODE_LIST
72+
| rnix::SyntaxKind::NODE_LITERAL
73+
| rnix::SyntaxKind::NODE_STRING
74+
)
75+
{
76+
steps.push_back(crate::builder::Step::Indent);
77+
steps.push_back(crate::builder::Step::NewLine);
78+
steps.push_back(crate::builder::Step::Pad);
79+
steps.push_back(crate::builder::Step::FormatWider(
80+
parsed.second_expression,
81+
));
82+
steps.push_back(crate::builder::Step::Dedent);
83+
} else {
84+
steps.push_back(crate::builder::Step::Whitespace);
85+
steps.push_back(crate::builder::Step::FormatWider(
86+
parsed.second_expression,
87+
));
88+
}
89+
} else {
90+
steps.push_back(crate::builder::Step::Whitespace);
91+
steps.push_back(crate::builder::Step::Format(parsed.second_expression));
92+
}
93+
94+
steps
95+
}

src/alejandra_engine/src/rules/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub(crate) mod apply;
2-
pub(crate) mod assert_and_with;
2+
pub(crate) mod assert_or_with;
33
pub(crate) mod attr_set;
44
pub(crate) mod bin_op_and_or_default;
55
pub(crate) mod dynamic;

0 commit comments

Comments
 (0)