1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-02 05:27:45 +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, rnix::SyntaxKind::NODE_INHERIT => crate::rules::inherit::rule,
// ( a ) // ( a )
rnix::SyntaxKind::NODE_INHERIT_FROM => { rnix::SyntaxKind::NODE_INHERIT_FROM => {
crate::rules::paren::rule crate::rules::inherit_from::rule
} }
rnix::SyntaxKind::NODE_KEY => crate::rules::default, rnix::SyntaxKind::NODE_KEY => crate::rules::default,
// a = b; // 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 dynamic;
pub(crate) mod if_else; pub(crate) mod if_else;
pub(crate) mod inherit; pub(crate) mod inherit;
pub(crate) mod inherit_from;
pub(crate) mod key_value; pub(crate) mod key_value;
pub(crate) mod lambda; pub(crate) mod lambda;
pub(crate) mod let_in; pub(crate) mod let_in;

View file

@ -1,6 +1,14 @@
pub(crate) fn rule( pub(crate) fn rule(
build_ctx: &crate::builder::BuildCtx, build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode, 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> { ) -> std::collections::LinkedList<crate::builder::Step> {
let mut steps = std::collections::LinkedList::new(); let mut steps = std::collections::LinkedList::new();
@ -10,6 +18,29 @@ pub(crate) fn rule(
let expression = children.next().unwrap(); let expression = children.next().unwrap();
let closer = 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 let vertical = opener.has_inline_comment
|| opener.has_trivialities || opener.has_trivialities
|| expression.has_inline_comment || expression.has_inline_comment

View file

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

View file

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