mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-31 12:37:45 +00:00
Merge pull request #112 from kamadorueda/kamadorueda
refactor: reuse code
This commit is contained in:
commit
1f01ac6ea9
2 changed files with 33 additions and 91 deletions
|
@ -1,6 +1,14 @@
|
|||
pub fn rule(
|
||||
build_ctx: &crate::builder::BuildCtx,
|
||||
node: &rnix::SyntaxNode,
|
||||
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||
rule_with_configuration(build_ctx, node, "bin_op_and_or_default")
|
||||
}
|
||||
|
||||
pub fn rule_with_configuration(
|
||||
build_ctx: &crate::builder::BuildCtx,
|
||||
node: &rnix::SyntaxNode,
|
||||
parent_kind: &str,
|
||||
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||
let mut steps = std::collections::LinkedList::new();
|
||||
|
||||
|
@ -18,17 +26,24 @@ pub fn rule(
|
|||
let child = children.get_next().unwrap();
|
||||
match layout {
|
||||
crate::config::Layout::Tall => {
|
||||
match child.element.kind() {
|
||||
rnix::SyntaxKind::NODE_BIN_OP
|
||||
| rnix::SyntaxKind::NODE_OR_DEFAULT => {
|
||||
steps
|
||||
.push_back(crate::builder::Step::Format(child.element));
|
||||
}
|
||||
_ => {
|
||||
steps.push_back(crate::builder::Step::FormatWider(
|
||||
child.element,
|
||||
));
|
||||
}
|
||||
let kind = child.element.kind();
|
||||
|
||||
if parent_kind == "bin_op_and_or_default"
|
||||
&& matches!(
|
||||
kind,
|
||||
rnix::SyntaxKind::NODE_BIN_OP
|
||||
| rnix::SyntaxKind::NODE_OR_DEFAULT
|
||||
)
|
||||
{
|
||||
steps.push_back(crate::builder::Step::Format(child.element));
|
||||
} else if parent_kind == "select"
|
||||
&& matches!(kind, rnix::SyntaxKind::NODE_SELECT)
|
||||
{
|
||||
steps.push_back(crate::builder::Step::Format(child.element));
|
||||
} else {
|
||||
steps.push_back(crate::builder::Step::FormatWider(
|
||||
child.element,
|
||||
));
|
||||
}
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
|
@ -53,7 +68,9 @@ pub fn rule(
|
|||
match layout {
|
||||
crate::config::Layout::Tall => {}
|
||||
crate::config::Layout::Wide => {
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
if parent_kind == "bin_op_and_or_default" {
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
}
|
||||
}
|
||||
}
|
||||
steps.push_back(crate::builder::Step::Format(child.element));
|
||||
|
@ -73,7 +90,7 @@ pub fn rule(
|
|||
if comment {
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
} else {
|
||||
} else if parent_kind == "bin_op_and_or_default" {
|
||||
steps.push_back(crate::builder::Step::Whitespace);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,82 +2,7 @@ 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_SELECT => {
|
||||
steps.push_back(crate::builder::Step::Format(child.element));
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
_ => {
|
||||
steps.push_back(crate::builder::Step::FormatWider(
|
||||
child.element,
|
||||
));
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
},
|
||||
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::Comment(text));
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
crate::children::DrainCommentOrNewline::Newline(_) => {}
|
||||
});
|
||||
|
||||
// .
|
||||
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);
|
||||
}
|
||||
|
||||
// expr
|
||||
let child = children.get_next().unwrap();
|
||||
match layout {
|
||||
crate::config::Layout::Tall => {
|
||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||
}
|
||||
crate::config::Layout::Wide => {
|
||||
steps.push_back(crate::builder::Step::Format(child.element));
|
||||
}
|
||||
}
|
||||
|
||||
steps
|
||||
crate::rules::bin_op_and_or_default::rule_with_configuration(
|
||||
build_ctx, node, "select",
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue