mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 04:57:44 +00:00
refactor: reuse code
- Assert and With are handled the same, and instead of copy-pasting, lets reuse what we now do very well for the With
This commit is contained in:
parent
e16dd2c208
commit
cfba9388d1
6 changed files with 36 additions and 113 deletions
|
@ -169,7 +169,9 @@ fn format(
|
||||||
// a b
|
// a b
|
||||||
rnix::SyntaxKind::NODE_APPLY => crate::rules::apply::rule,
|
rnix::SyntaxKind::NODE_APPLY => crate::rules::apply::rule,
|
||||||
// assert a; b
|
// assert a; b
|
||||||
rnix::SyntaxKind::NODE_ASSERT => crate::rules::assert::rule,
|
rnix::SyntaxKind::NODE_ASSERT => {
|
||||||
|
crate::rules::assert_and_with::rule
|
||||||
|
}
|
||||||
// { }
|
// { }
|
||||||
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
|
||||||
|
@ -246,7 +248,9 @@ fn format(
|
||||||
// !a
|
// !a
|
||||||
rnix::SyntaxKind::NODE_UNARY_OP => crate::rules::default,
|
rnix::SyntaxKind::NODE_UNARY_OP => crate::rules::default,
|
||||||
// with a; b
|
// with a; b
|
||||||
rnix::SyntaxKind::NODE_WITH => crate::rules::with::rule,
|
rnix::SyntaxKind::NODE_WITH => {
|
||||||
|
crate::rules::assert_and_with::rule
|
||||||
|
}
|
||||||
kind => {
|
kind => {
|
||||||
panic!(
|
panic!(
|
||||||
"Missing rule for {:?} at: {}",
|
"Missing rule for {:?} at: {}",
|
||||||
|
|
|
@ -1,81 +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()
|
|
||||||
};
|
|
||||||
|
|
||||||
// assert
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ;
|
|
||||||
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(_) => {}
|
|
||||||
});
|
|
||||||
|
|
||||||
// expr
|
|
||||||
let child = children.get_next().unwrap();
|
|
||||||
match layout {
|
|
||||||
crate::config::Layout::Tall => {
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
steps
|
|
||||||
}
|
|
|
@ -69,7 +69,10 @@ pub fn rule(
|
||||||
match layout {
|
match layout {
|
||||||
crate::config::Layout::Tall => {
|
crate::config::Layout::Tall => {
|
||||||
if comment
|
if comment
|
||||||
|| matches!(child.element.kind(), rnix::SyntaxKind::NODE_WITH)
|
|| matches!(
|
||||||
|
child.element.kind(),
|
||||||
|
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
|
||||||
|
)
|
||||||
|| !matches!(
|
|| !matches!(
|
||||||
child.element.kind(),
|
child.element.kind(),
|
||||||
rnix::SyntaxKind::NODE_ATTR_SET
|
rnix::SyntaxKind::NODE_ATTR_SET
|
|
@ -91,27 +91,29 @@ pub fn rule(
|
||||||
.kind(),
|
.kind(),
|
||||||
rnix::SyntaxKind::NODE_PATTERN
|
rnix::SyntaxKind::NODE_PATTERN
|
||||||
))
|
))
|
||||||
|| (matches!(next_kind, rnix::SyntaxKind::NODE_WITH)
|
|| (matches!(
|
||||||
&& matches!(
|
next_kind,
|
||||||
next.element
|
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
|
||||||
.clone()
|
) && matches!(
|
||||||
.into_node()
|
next.element
|
||||||
.unwrap()
|
.clone()
|
||||||
.children()
|
.into_node()
|
||||||
.collect::<Vec<rnix::SyntaxNode>>()
|
.unwrap()
|
||||||
.iter()
|
.children()
|
||||||
.rev()
|
.collect::<Vec<rnix::SyntaxNode>>()
|
||||||
.next()
|
.iter()
|
||||||
.unwrap()
|
.rev()
|
||||||
.kind(),
|
.next()
|
||||||
rnix::SyntaxKind::NODE_ATTR_SET
|
.unwrap()
|
||||||
| rnix::SyntaxKind::NODE_IDENT
|
.kind(),
|
||||||
| rnix::SyntaxKind::NODE_PAREN
|
rnix::SyntaxKind::NODE_ATTR_SET
|
||||||
| rnix::SyntaxKind::NODE_LET_IN
|
| rnix::SyntaxKind::NODE_IDENT
|
||||||
| rnix::SyntaxKind::NODE_LIST
|
| rnix::SyntaxKind::NODE_PAREN
|
||||||
| rnix::SyntaxKind::NODE_LITERAL
|
| rnix::SyntaxKind::NODE_LET_IN
|
||||||
| rnix::SyntaxKind::NODE_STRING
|
| rnix::SyntaxKind::NODE_LIST
|
||||||
))
|
| rnix::SyntaxKind::NODE_LITERAL
|
||||||
|
| rnix::SyntaxKind::NODE_STRING
|
||||||
|
))
|
||||||
{
|
{
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
pub mod apply;
|
pub mod apply;
|
||||||
pub mod assert;
|
pub mod assert_and_with;
|
||||||
pub mod attr_set;
|
pub mod attr_set;
|
||||||
pub mod bin_op;
|
pub mod bin_op;
|
||||||
pub mod dynamic;
|
pub mod dynamic;
|
||||||
|
@ -18,7 +18,6 @@ pub mod root;
|
||||||
pub mod select;
|
pub mod select;
|
||||||
pub mod string;
|
pub mod string;
|
||||||
pub mod string_interpol;
|
pub mod string_interpol;
|
||||||
pub mod with;
|
|
||||||
|
|
||||||
pub fn default(
|
pub fn default(
|
||||||
build_ctx: &crate::builder::BuildCtx,
|
build_ctx: &crate::builder::BuildCtx,
|
||||||
|
|
|
@ -12,8 +12,7 @@
|
||||||
/*
|
/*
|
||||||
a
|
a
|
||||||
*/
|
*/
|
||||||
b;
|
b; c
|
||||||
c
|
|
||||||
)
|
)
|
||||||
(
|
(
|
||||||
assert
|
assert
|
||||||
|
@ -27,8 +26,5 @@
|
||||||
c
|
c
|
||||||
)
|
)
|
||||||
(assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
|
(assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
|
||||||
(
|
(assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
|
||||||
assert b;
|
|
||||||
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
|
|
||||||
)
|
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue