Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simplify parens #228

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/alejandra_engine/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn format(
rnix::SyntaxKind::NODE_INHERIT => crate::rules::inherit::rule,
// ( a )
rnix::SyntaxKind::NODE_INHERIT_FROM => {
crate::rules::paren::rule
crate::rules::inherit_from::rule
}
rnix::SyntaxKind::NODE_KEY => crate::rules::default,
// a = b;
Expand Down
6 changes: 6 additions & 0 deletions src/alejandra_engine/src/rules/inherit_from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub(crate) fn rule(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
) -> std::collections::LinkedList<crate::builder::Step> {
crate::rules::paren::rule_with_configuration(build_ctx, node, false)
}
1 change: 1 addition & 0 deletions src/alejandra_engine/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(crate) mod bin_op;
pub(crate) mod dynamic;
pub(crate) mod if_else;
pub(crate) mod inherit;
pub(crate) mod inherit_from;
pub(crate) mod key_value;
pub(crate) mod lambda;
pub(crate) mod let_in;
Expand Down
31 changes: 31 additions & 0 deletions src/alejandra_engine/src/rules/paren.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
pub(crate) fn rule(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
) -> std::collections::LinkedList<crate::builder::Step> {
rule_with_configuration(build_ctx, node, true)
}

pub(crate) fn rule_with_configuration(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
simplify: bool,
) -> std::collections::LinkedList<crate::builder::Step> {
let mut steps = std::collections::LinkedList::new();

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

// Simplify this expression
if simplify
&& !opener.has_inline_comment
&& !opener.has_comments
&& !expression.has_inline_comment
&& !expression.has_comments
&& !closer.has_inline_comment
&& !closer.has_comments
&& matches!(
expression.element.kind(),
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_IDENT
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_LITERAL
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_PATH_WITH_INTERPOL
| rnix::SyntaxKind::NODE_STRING
)
{
steps.push_back(crate::builder::Step::Format(expression.element));
return steps;
}

let vertical = opener.has_inline_comment
|| opener.has_trivialities
|| expression.has_inline_comment
Expand Down
20 changes: 10 additions & 10 deletions src/alejandra_engine/tests/cases/paren/out
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
( # test
a # test
)
((c))
c
(
(c)
c
/*
e
*/
)
((
(
c
/*
d
*/
))
)
(
(
c
Expand All @@ -26,12 +26,12 @@
e
*/
)
((
(
/*
b
*/
c
))
)
(
(
/*
Expand All @@ -43,15 +43,15 @@
e
*/
)
((
(
/*
b
*/
c
/*
d
*/
))
)
(
(
/*
Expand All @@ -70,13 +70,13 @@
/*
a
*/
(c)
c
)
(
/*
a
*/
(c)
c
/*
e
*/
Expand Down
10 changes: 4 additions & 6 deletions src/alejandra_engine/tests/cases/with/out
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@
}
{
binPath = with pkgs;
makeBinPath (
[
rsync
util-linux
]
);
makeBinPath [
rsync
util-linux
];
}
]