Skip to content

Commit 0b480bc

Browse files
committed
feat: simplify parens
1 parent 6d2b242 commit 0b480bc

File tree

6 files changed

+53
-17
lines changed

6 files changed

+53
-17
lines changed

src/alejandra_engine/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn format(
191191
rnix::SyntaxKind::NODE_INHERIT => crate::rules::inherit::rule,
192192
// ( a )
193193
rnix::SyntaxKind::NODE_INHERIT_FROM => {
194-
crate::rules::paren::rule
194+
crate::rules::inherit_from::rule
195195
}
196196
rnix::SyntaxKind::NODE_KEY => crate::rules::default,
197197
// a = b;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub(crate) fn rule(
2+
build_ctx: &crate::builder::BuildCtx,
3+
node: &rnix::SyntaxNode,
4+
) -> std::collections::LinkedList<crate::builder::Step> {
5+
crate::rules::paren::rule_with_configuration(build_ctx, node, false)
6+
}

src/alejandra_engine/src/rules/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub(crate) mod bin_op;
44
pub(crate) mod dynamic;
55
pub(crate) mod if_else;
66
pub(crate) mod inherit;
7+
pub(crate) mod inherit_from;
78
pub(crate) mod key_value;
89
pub(crate) mod lambda;
910
pub(crate) mod let_in;

src/alejandra_engine/src/rules/paren.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
pub(crate) fn rule(
22
build_ctx: &crate::builder::BuildCtx,
33
node: &rnix::SyntaxNode,
4+
) -> std::collections::LinkedList<crate::builder::Step> {
5+
rule_with_configuration(build_ctx, node, true)
6+
}
7+
8+
pub(crate) fn rule_with_configuration(
9+
build_ctx: &crate::builder::BuildCtx,
10+
node: &rnix::SyntaxNode,
11+
simplify: bool,
412
) -> std::collections::LinkedList<crate::builder::Step> {
513
let mut steps = std::collections::LinkedList::new();
614

@@ -10,6 +18,29 @@ pub(crate) fn rule(
1018
let expression = children.next().unwrap();
1119
let closer = children.next().unwrap();
1220

21+
// Simplify this expression
22+
if simplify
23+
&& !opener.has_inline_comment
24+
&& !opener.has_comments
25+
&& !expression.has_inline_comment
26+
&& !expression.has_comments
27+
&& !closer.has_inline_comment
28+
&& !closer.has_comments
29+
&& matches!(
30+
expression.element.kind(),
31+
rnix::SyntaxKind::NODE_ATTR_SET
32+
| rnix::SyntaxKind::NODE_IDENT
33+
| rnix::SyntaxKind::NODE_LIST
34+
| rnix::SyntaxKind::NODE_LITERAL
35+
| rnix::SyntaxKind::NODE_PAREN
36+
| rnix::SyntaxKind::NODE_PATH_WITH_INTERPOL
37+
| rnix::SyntaxKind::NODE_STRING
38+
)
39+
{
40+
steps.push_back(crate::builder::Step::Format(expression.element));
41+
return steps;
42+
}
43+
1344
let vertical = opener.has_inline_comment
1445
|| opener.has_trivialities
1546
|| expression.has_inline_comment

src/alejandra_engine/tests/cases/paren/out

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
( # test
33
a # test
44
)
5-
((c))
5+
c
66
(
7-
(c)
7+
c
88
/*
99
e
1010
*/
1111
)
12-
((
12+
(
1313
c
1414
/*
1515
d
1616
*/
17-
))
17+
)
1818
(
1919
(
2020
c
@@ -26,12 +26,12 @@
2626
e
2727
*/
2828
)
29-
((
29+
(
3030
/*
3131
b
3232
*/
3333
c
34-
))
34+
)
3535
(
3636
(
3737
/*
@@ -43,15 +43,15 @@
4343
e
4444
*/
4545
)
46-
((
46+
(
4747
/*
4848
b
4949
*/
5050
c
5151
/*
5252
d
5353
*/
54-
))
54+
)
5555
(
5656
(
5757
/*
@@ -70,13 +70,13 @@
7070
/*
7171
a
7272
*/
73-
(c)
73+
c
7474
)
7575
(
7676
/*
7777
a
7878
*/
79-
(c)
79+
c
8080
/*
8181
e
8282
*/

src/alejandra_engine/tests/cases/with/out

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,9 @@
6161
}
6262
{
6363
binPath = with pkgs;
64-
makeBinPath (
65-
[
66-
rsync
67-
util-linux
68-
]
69-
);
64+
makeBinPath [
65+
rsync
66+
util-linux
67+
];
7068
}
7169
]

0 commit comments

Comments
 (0)