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

Merge pull request #109 from kamadorueda/kamadorueda

feat: merge similar rules
This commit is contained in:
Kevin Amado 2022-02-16 18:25:34 -05:00 committed by GitHub
commit 72ab2888b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 146 deletions

View file

@ -175,7 +175,9 @@ fn format(
// { }
rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule,
// 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)
rnix::SyntaxKind::NODE_DYNAMIC => crate::rules::dynamic::rule,
// implementation detail of rnix-parser
@ -219,7 +221,7 @@ fn format(
}
// a or b
rnix::SyntaxKind::NODE_OR_DEFAULT => {
crate::rules::or_default::rule
crate::rules::bin_op_and_or_default::rule
}
// ( a )
rnix::SyntaxKind::NODE_PAREN => crate::rules::paren::rule,

View file

@ -19,7 +19,8 @@ pub fn rule(
match layout {
crate::config::Layout::Tall => {
match child.element.kind() {
rnix::SyntaxKind::NODE_BIN_OP => {
rnix::SyntaxKind::NODE_BIN_OP
| rnix::SyntaxKind::NODE_OR_DEFAULT => {
steps
.push_back(crate::builder::Step::Format(child.element));
}
@ -58,19 +59,18 @@ pub fn rule(
steps.push_back(crate::builder::Step::Format(child.element));
// /**/
let mut comment = false;
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));
comment = true;
}
crate::children::DrainCommentOrNewline::Newline(_) => {}
});
if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE =
children.peek_prev().unwrap().element.kind()
{
if comment {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else {

View file

@ -1,7 +1,7 @@
pub mod apply;
pub mod assert_and_with;
pub mod attr_set;
pub mod bin_op;
pub mod bin_op_and_or_default;
pub mod dynamic;
pub mod if_else;
pub mod inherit;
@ -9,7 +9,6 @@ pub mod key_value;
pub mod lambda;
pub mod let_in;
pub mod list;
pub mod or_default;
pub mod paren;
pub mod pat_bind;
pub mod pat_entry;

View file

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

View file

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

View file

@ -1,64 +1,67 @@
[
(a or a)
(
a or
/**/
a
or
/**/
a
)
(
a
/**/
or a
)
(
a
/**/
or
/**/
a
)
(
a
/**/
or
/**/
(
a
)
(
a
/**/
or a
)
(
a
/**/
or
/**/
a
)
(
a
/**/
or
/**/
(
a
/**/
or
/**/
(
a
/**/
or
/**/
a
)
/**/
or
/**/
a
)
)
)
(
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
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 or a
)
]