mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 04:57:44 +00:00
feat: merge similar rules
This commit is contained in:
parent
942dc8fd92
commit
c4ca35b570
6 changed files with 56 additions and 146 deletions
|
@ -175,7 +175,9 @@ fn format(
|
||||||
// { }
|
// { }
|
||||||
rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule,
|
rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule,
|
||||||
// a $op b
|
// a $op b
|
||||||
rnix::SyntaxKind::NODE_BIN_OP => crate::rules::bin_op::rule,
|
rnix::SyntaxKind::NODE_BIN_OP => {
|
||||||
|
crate::rules::bin_op_and_or_default::rule
|
||||||
|
}
|
||||||
// ${a} (interpolation but for NODE_SELECT)
|
// ${a} (interpolation but for NODE_SELECT)
|
||||||
rnix::SyntaxKind::NODE_DYNAMIC => crate::rules::dynamic::rule,
|
rnix::SyntaxKind::NODE_DYNAMIC => crate::rules::dynamic::rule,
|
||||||
// implementation detail of rnix-parser
|
// implementation detail of rnix-parser
|
||||||
|
@ -219,7 +221,7 @@ fn format(
|
||||||
}
|
}
|
||||||
// a or b
|
// a or b
|
||||||
rnix::SyntaxKind::NODE_OR_DEFAULT => {
|
rnix::SyntaxKind::NODE_OR_DEFAULT => {
|
||||||
crate::rules::or_default::rule
|
crate::rules::bin_op_and_or_default::rule
|
||||||
}
|
}
|
||||||
// ( a )
|
// ( a )
|
||||||
rnix::SyntaxKind::NODE_PAREN => crate::rules::paren::rule,
|
rnix::SyntaxKind::NODE_PAREN => crate::rules::paren::rule,
|
||||||
|
|
|
@ -19,7 +19,8 @@ pub fn rule(
|
||||||
match layout {
|
match layout {
|
||||||
crate::config::Layout::Tall => {
|
crate::config::Layout::Tall => {
|
||||||
match child.element.kind() {
|
match child.element.kind() {
|
||||||
rnix::SyntaxKind::NODE_BIN_OP => {
|
rnix::SyntaxKind::NODE_BIN_OP
|
||||||
|
| rnix::SyntaxKind::NODE_OR_DEFAULT => {
|
||||||
steps
|
steps
|
||||||
.push_back(crate::builder::Step::Format(child.element));
|
.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
|
@ -58,19 +59,18 @@ pub fn rule(
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
let mut comment = false;
|
||||||
children.drain_comments_and_newlines(|element| match element {
|
children.drain_comments_and_newlines(|element| match element {
|
||||||
crate::children::DrainCommentOrNewline::Comment(text) => {
|
crate::children::DrainCommentOrNewline::Comment(text) => {
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Comment(text));
|
steps.push_back(crate::builder::Step::Comment(text));
|
||||||
|
comment = true;
|
||||||
}
|
}
|
||||||
crate::children::DrainCommentOrNewline::Newline(_) => {}
|
crate::children::DrainCommentOrNewline::Newline(_) => {}
|
||||||
});
|
});
|
||||||
|
|
||||||
if let rnix::SyntaxKind::TOKEN_COMMENT
|
if comment {
|
||||||
| rnix::SyntaxKind::TOKEN_WHITESPACE =
|
|
||||||
children.peek_prev().unwrap().element.kind()
|
|
||||||
{
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
} else {
|
} else {
|
|
@ -1,7 +1,7 @@
|
||||||
pub mod apply;
|
pub mod apply;
|
||||||
pub mod assert_and_with;
|
pub mod assert_and_with;
|
||||||
pub mod attr_set;
|
pub mod attr_set;
|
||||||
pub mod bin_op;
|
pub mod bin_op_and_or_default;
|
||||||
pub mod dynamic;
|
pub mod dynamic;
|
||||||
pub mod if_else;
|
pub mod if_else;
|
||||||
pub mod inherit;
|
pub mod inherit;
|
||||||
|
@ -9,7 +9,6 @@ pub mod key_value;
|
||||||
pub mod lambda;
|
pub mod lambda;
|
||||||
pub mod let_in;
|
pub mod let_in;
|
||||||
pub mod list;
|
pub mod list;
|
||||||
pub mod or_default;
|
|
||||||
pub mod paren;
|
pub mod paren;
|
||||||
pub mod pat_bind;
|
pub mod pat_bind;
|
||||||
pub mod pat_entry;
|
pub mod pat_entry;
|
||||||
|
|
|
@ -1,96 +0,0 @@
|
||||||
pub fn rule(
|
|
||||||
build_ctx: &crate::builder::BuildCtx,
|
|
||||||
node: &rnix::SyntaxNode,
|
|
||||||
) -> std::collections::LinkedList<crate::builder::Step> {
|
|
||||||
let mut steps = std::collections::LinkedList::new();
|
|
||||||
|
|
||||||
let mut children = crate::children::Children::new_with_configuration(
|
|
||||||
build_ctx, node, true,
|
|
||||||
);
|
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
|
||||||
&crate::config::Layout::Tall
|
|
||||||
} else {
|
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// expr
|
|
||||||
let child = children.get_next().unwrap();
|
|
||||||
match layout {
|
|
||||||
crate::config::Layout::Tall => {
|
|
||||||
match child.element.kind() {
|
|
||||||
rnix::SyntaxKind::NODE_OR_DEFAULT => {
|
|
||||||
steps
|
|
||||||
.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
|
||||||
child.element,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// /**/
|
|
||||||
children.drain_comments_and_newlines(|element| match element {
|
|
||||||
crate::children::DrainCommentOrNewline::Comment(text) => {
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
steps.push_back(crate::builder::Step::Comment(text));
|
|
||||||
}
|
|
||||||
crate::children::DrainCommentOrNewline::Newline(_) => {}
|
|
||||||
});
|
|
||||||
|
|
||||||
if let rnix::SyntaxKind::TOKEN_COMMENT
|
|
||||||
| rnix::SyntaxKind::TOKEN_WHITESPACE =
|
|
||||||
children.peek_prev().unwrap().element.kind()
|
|
||||||
{
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
}
|
|
||||||
|
|
||||||
// operator
|
|
||||||
let child = children.get_next().unwrap();
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
|
|
||||||
// /**/
|
|
||||||
children.drain_comments_and_newlines(|element| match element {
|
|
||||||
crate::children::DrainCommentOrNewline::Comment(text) => {
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
steps.push_back(crate::builder::Step::Comment(text));
|
|
||||||
}
|
|
||||||
crate::children::DrainCommentOrNewline::Newline(_) => {}
|
|
||||||
});
|
|
||||||
|
|
||||||
if let rnix::SyntaxKind::TOKEN_COMMENT
|
|
||||||
| rnix::SyntaxKind::TOKEN_WHITESPACE =
|
|
||||||
children.peek_prev().unwrap().element.kind()
|
|
||||||
{
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
}
|
|
||||||
|
|
||||||
// expr
|
|
||||||
let child = children.get_next().unwrap();
|
|
||||||
match layout {
|
|
||||||
crate::config::Layout::Tall => {
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
steps
|
|
||||||
}
|
|
|
@ -8,5 +8,7 @@
|
||||||
( a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a )
|
( a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a )
|
||||||
( a or a
|
( a or a
|
||||||
or a
|
or a
|
||||||
or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a)
|
or
|
||||||
|
a or
|
||||||
|
a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a)
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,64 +1,67 @@
|
||||||
[
|
[
|
||||||
(a or a)
|
(a or a)
|
||||||
(
|
(
|
||||||
a or
|
a
|
||||||
/**/
|
or
|
||||||
|
/**/
|
||||||
|
a
|
||||||
|
)
|
||||||
|
(
|
||||||
|
a
|
||||||
|
/**/
|
||||||
|
or a
|
||||||
|
)
|
||||||
|
(
|
||||||
|
a
|
||||||
|
/**/
|
||||||
|
or
|
||||||
|
/**/
|
||||||
|
a
|
||||||
|
)
|
||||||
|
(
|
||||||
|
a
|
||||||
|
/**/
|
||||||
|
or
|
||||||
|
/**/
|
||||||
|
(
|
||||||
a
|
a
|
||||||
)
|
|
||||||
(
|
|
||||||
a
|
|
||||||
/**/
|
|
||||||
or a
|
|
||||||
)
|
|
||||||
(
|
|
||||||
a
|
|
||||||
/**/
|
|
||||||
or
|
|
||||||
/**/
|
|
||||||
a
|
|
||||||
)
|
|
||||||
(
|
|
||||||
a
|
|
||||||
/**/
|
/**/
|
||||||
or
|
or
|
||||||
/**/
|
/**/
|
||||||
(
|
(
|
||||||
a
|
a
|
||||||
/**/
|
/**/
|
||||||
or
|
or
|
||||||
/**/
|
/**/
|
||||||
(
|
a
|
||||||
a
|
|
||||||
/**/
|
|
||||||
or
|
|
||||||
/**/
|
|
||||||
a
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
(
|
(
|
||||||
a
|
a
|
||||||
|
/**/
|
||||||
|
or
|
||||||
|
/**/
|
||||||
|
(
|
||||||
|
a
|
||||||
/**/
|
/**/
|
||||||
or
|
or
|
||||||
/**/
|
/**/
|
||||||
(
|
(
|
||||||
a
|
a
|
||||||
/**/
|
/**/
|
||||||
or
|
or
|
||||||
/**/
|
/**/
|
||||||
(
|
a
|
||||||
a
|
|
||||||
/**/
|
|
||||||
or
|
|
||||||
/**/
|
|
||||||
a
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
(a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a)
|
(a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a)
|
||||||
(
|
(
|
||||||
a or a
|
a
|
||||||
or a
|
or a
|
||||||
or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a
|
or a
|
||||||
|
or a
|
||||||
|
or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue