1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-31 12:37:45 +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:
Kevin Amado 2022-02-12 15:20:40 -05:00
parent e16dd2c208
commit cfba9388d1
No known key found for this signature in database
GPG key ID: FFF341057F503148
6 changed files with 36 additions and 113 deletions

View file

@ -169,7 +169,9 @@ fn format(
// a b
rnix::SyntaxKind::NODE_APPLY => crate::rules::apply::rule,
// 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,
// a $op b
@ -246,7 +248,9 @@ fn format(
// !a
rnix::SyntaxKind::NODE_UNARY_OP => crate::rules::default,
// with a; b
rnix::SyntaxKind::NODE_WITH => crate::rules::with::rule,
rnix::SyntaxKind::NODE_WITH => {
crate::rules::assert_and_with::rule
}
kind => {
panic!(
"Missing rule for {:?} at: {}",

View file

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

View file

@ -69,7 +69,10 @@ pub fn rule(
match layout {
crate::config::Layout::Tall => {
if comment
|| matches!(child.element.kind(), rnix::SyntaxKind::NODE_WITH)
|| matches!(
child.element.kind(),
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
)
|| !matches!(
child.element.kind(),
rnix::SyntaxKind::NODE_ATTR_SET

View file

@ -91,27 +91,29 @@ pub fn rule(
.kind(),
rnix::SyntaxKind::NODE_PATTERN
))
|| (matches!(next_kind, rnix::SyntaxKind::NODE_WITH)
&& matches!(
next.element
.clone()
.into_node()
.unwrap()
.children()
.collect::<Vec<rnix::SyntaxNode>>()
.iter()
.rev()
.next()
.unwrap()
.kind(),
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_IDENT
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_LITERAL
| rnix::SyntaxKind::NODE_STRING
))
|| (matches!(
next_kind,
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
) && matches!(
next.element
.clone()
.into_node()
.unwrap()
.children()
.collect::<Vec<rnix::SyntaxNode>>()
.iter()
.rev()
.next()
.unwrap()
.kind(),
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_IDENT
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_LITERAL
| rnix::SyntaxKind::NODE_STRING
))
{
steps.push_back(crate::builder::Step::Whitespace);
} else {

View file

@ -1,5 +1,5 @@
pub mod apply;
pub mod assert;
pub mod assert_and_with;
pub mod attr_set;
pub mod bin_op;
pub mod dynamic;
@ -18,7 +18,6 @@ pub mod root;
pub mod select;
pub mod string;
pub mod string_interpol;
pub mod with;
pub fn default(
build_ctx: &crate::builder::BuildCtx,

View file

@ -12,8 +12,7 @@
/*
a
*/
b;
c
b; c
)
(
assert
@ -27,8 +26,5 @@
c
)
(assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
(
assert b;
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
)
(assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
]