1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-30 12:07:46 +00:00

feat: simplify parens

This commit is contained in:
Kevin Amado 2022-02-28 17:12:13 -05:00
parent 6d2b242989
commit 0b480bc36d
6 changed files with 53 additions and 17 deletions

View file

@ -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;

View file

@ -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)
}

View file

@ -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;

View file

@ -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();
@ -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

View file

@ -2,19 +2,19 @@
( # test
a # test
)
((c))
c
(
(c)
c
/*
e
*/
)
((
(
c
/*
d
*/
))
)
(
(
c
@ -26,12 +26,12 @@
e
*/
)
((
(
/*
b
*/
c
))
)
(
(
/*
@ -43,7 +43,7 @@
e
*/
)
((
(
/*
b
*/
@ -51,7 +51,7 @@
/*
d
*/
))
)
(
(
/*
@ -70,13 +70,13 @@
/*
a
*/
(c)
c
)
(
/*
a
*/
(c)
c
/*
e
*/

View file

@ -61,11 +61,9 @@
}
{
binPath = with pkgs;
makeBinPath (
[
rsync
util-linux
]
);
makeBinPath [
rsync
util-linux
];
}
]