mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-31 04:27:45 +00:00
refactor: split assert or with
This commit is contained in:
parent
3071c38e10
commit
421b1e911f
6 changed files with 173 additions and 102 deletions
|
@ -177,7 +177,7 @@ fn format(
|
||||||
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 => {
|
rnix::SyntaxKind::NODE_ASSERT => {
|
||||||
crate::rules::assert_and_with::rule
|
crate::rules::assert_or_with::rule
|
||||||
}
|
}
|
||||||
// { }
|
// { }
|
||||||
rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule,
|
rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule,
|
||||||
|
@ -244,7 +244,7 @@ fn format(
|
||||||
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 => {
|
rnix::SyntaxKind::NODE_WITH => {
|
||||||
crate::rules::assert_and_with::rule
|
crate::rules::assert_or_with::rule
|
||||||
}
|
}
|
||||||
kind => {
|
kind => {
|
||||||
panic!(
|
panic!(
|
||||||
|
|
74
src/alejandra_engine/src/parsers/assert_or_with.rs
Normal file
74
src/alejandra_engine/src/parsers/assert_or_with.rs
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
use std::collections::LinkedList;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct AssertOrWith {
|
||||||
|
pub assert_or_with: rnix::SyntaxElement,
|
||||||
|
pub comments_after_assert_or_with: LinkedList<String>,
|
||||||
|
pub has_newlines_after_assert_or_with: bool,
|
||||||
|
pub first_expression: rnix::SyntaxElement,
|
||||||
|
pub semicolon: rnix::SyntaxElement,
|
||||||
|
pub comments_after_semicolon: LinkedList<String>,
|
||||||
|
pub has_newlines_after_semicolon: bool,
|
||||||
|
pub second_expression: rnix::SyntaxElement,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AssertOrWith {
|
||||||
|
pub(crate) fn new(
|
||||||
|
build_ctx: &crate::builder::BuildCtx,
|
||||||
|
node: &rnix::SyntaxNode,
|
||||||
|
) -> AssertOrWith {
|
||||||
|
let mut children = crate::children::Children::new(build_ctx, node);
|
||||||
|
|
||||||
|
// assert_or_with
|
||||||
|
let assert_or_with = children.get_next().unwrap();
|
||||||
|
|
||||||
|
// comments_after_assert_or_with
|
||||||
|
// has_newlines_after_assert_or_with
|
||||||
|
let mut comments_after_assert_or_with = LinkedList::new();
|
||||||
|
let mut has_newlines_after_assert_or_with = false;
|
||||||
|
children.drain_trivia(|element| match element {
|
||||||
|
crate::children::Trivia::Comment(text) => {
|
||||||
|
comments_after_assert_or_with.push_back(text);
|
||||||
|
}
|
||||||
|
crate::children::Trivia::Whitespace(text) => {
|
||||||
|
has_newlines_after_assert_or_with =
|
||||||
|
has_newlines_after_assert_or_with
|
||||||
|
|| crate::utils::count_newlines(&text) > 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// first_expression
|
||||||
|
let first_expression = children.get_next().unwrap();
|
||||||
|
|
||||||
|
// semicolon
|
||||||
|
let semicolon = children.get_next().unwrap();
|
||||||
|
|
||||||
|
// comments_after_semicolon
|
||||||
|
// has_newlines_after_semicolon
|
||||||
|
let mut comments_after_semicolon = LinkedList::new();
|
||||||
|
let mut has_newlines_after_semicolon = false;
|
||||||
|
children.drain_trivia(|element| match element {
|
||||||
|
crate::children::Trivia::Comment(text) => {
|
||||||
|
comments_after_semicolon.push_back(text);
|
||||||
|
}
|
||||||
|
crate::children::Trivia::Whitespace(text) => {
|
||||||
|
has_newlines_after_semicolon = has_newlines_after_semicolon
|
||||||
|
|| crate::utils::count_newlines(&text) > 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// second_expression
|
||||||
|
let second_expression = children.get_next().unwrap();
|
||||||
|
|
||||||
|
AssertOrWith {
|
||||||
|
assert_or_with,
|
||||||
|
comments_after_assert_or_with,
|
||||||
|
has_newlines_after_assert_or_with,
|
||||||
|
first_expression,
|
||||||
|
semicolon,
|
||||||
|
comments_after_semicolon,
|
||||||
|
has_newlines_after_semicolon,
|
||||||
|
second_expression,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
pub(crate) mod apply;
|
pub(crate) mod apply;
|
||||||
|
pub(crate) mod assert_or_with;
|
||||||
pub(crate) mod if_else;
|
pub(crate) mod if_else;
|
||||||
pub(crate) mod pattern;
|
pub(crate) mod pattern;
|
||||||
|
|
|
@ -1,99 +0,0 @@
|
||||||
pub(crate) 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(build_ctx, node);
|
|
||||||
|
|
||||||
let vertical = children.has_comments()
|
|
||||||
|| children.has_newlines()
|
|
||||||
|| build_ctx.vertical;
|
|
||||||
|
|
||||||
// with
|
|
||||||
let child = children.get_next().unwrap();
|
|
||||||
steps.push_back(crate::builder::Step::Format(child));
|
|
||||||
|
|
||||||
// /**/
|
|
||||||
let mut comment = false;
|
|
||||||
children.drain_trivia(|element| match element {
|
|
||||||
crate::children::Trivia::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::Trivia::Whitespace(_) => {}
|
|
||||||
});
|
|
||||||
|
|
||||||
if comment {
|
|
||||||
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();
|
|
||||||
if vertical {
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child));
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ;
|
|
||||||
let child = children.get_next().unwrap();
|
|
||||||
steps.push_back(crate::builder::Step::Format(child));
|
|
||||||
|
|
||||||
// /**/
|
|
||||||
let mut comment: bool = false;
|
|
||||||
children.drain_trivia(|element| match element {
|
|
||||||
crate::children::Trivia::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::Trivia::Whitespace(_) => {}
|
|
||||||
});
|
|
||||||
|
|
||||||
// expr
|
|
||||||
let child = children.get_next().unwrap();
|
|
||||||
if vertical {
|
|
||||||
if {
|
|
||||||
matches!(
|
|
||||||
child.kind(),
|
|
||||||
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
|
|
||||||
)
|
|
||||||
} {
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child));
|
|
||||||
} else if comment
|
|
||||||
|| !matches!(
|
|
||||||
child.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::Indent);
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child));
|
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
steps.push_back(crate::builder::Step::Format(child));
|
|
||||||
}
|
|
||||||
|
|
||||||
steps
|
|
||||||
}
|
|
95
src/alejandra_engine/src/rules/assert_or_with.rs
Normal file
95
src/alejandra_engine/src/rules/assert_or_with.rs
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
pub(crate) fn rule(
|
||||||
|
build_ctx: &crate::builder::BuildCtx,
|
||||||
|
node: &rnix::SyntaxNode,
|
||||||
|
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||||
|
let mut steps = std::collections::LinkedList::new();
|
||||||
|
|
||||||
|
let parsed =
|
||||||
|
crate::parsers::assert_or_with::AssertOrWith::new(build_ctx, node);
|
||||||
|
|
||||||
|
let vertical = build_ctx.vertical
|
||||||
|
|| !parsed.comments_after_assert_or_with.is_empty()
|
||||||
|
|| parsed.has_newlines_after_assert_or_with
|
||||||
|
|| !parsed.comments_after_semicolon.is_empty()
|
||||||
|
|| parsed.has_newlines_after_semicolon;
|
||||||
|
|
||||||
|
// assert_or_with
|
||||||
|
steps.push_back(crate::builder::Step::Format(parsed.assert_or_with));
|
||||||
|
|
||||||
|
// comments_after_assert_or_with
|
||||||
|
if parsed.comments_after_assert_or_with.is_empty() {
|
||||||
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
|
for text in parsed.comments_after_assert_or_with {
|
||||||
|
steps.push_back(crate::builder::Step::Comment(text));
|
||||||
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// first_expression
|
||||||
|
if vertical {
|
||||||
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
|
parsed.first_expression,
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Format(parsed.first_expression));
|
||||||
|
}
|
||||||
|
|
||||||
|
// semicolon
|
||||||
|
steps.push_back(crate::builder::Step::Format(parsed.semicolon));
|
||||||
|
|
||||||
|
// comments_after_semicolon
|
||||||
|
let has_comments_after_semicolon =
|
||||||
|
!parsed.comments_after_semicolon.is_empty();
|
||||||
|
for text in parsed.comments_after_semicolon {
|
||||||
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
|
steps.push_back(crate::builder::Step::Comment(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
// second_expression
|
||||||
|
if vertical {
|
||||||
|
if matches!(
|
||||||
|
parsed.second_expression.kind(),
|
||||||
|
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
|
||||||
|
) {
|
||||||
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
|
parsed.second_expression,
|
||||||
|
));
|
||||||
|
} else if has_comments_after_semicolon
|
||||||
|
|| !matches!(
|
||||||
|
parsed.second_expression.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::Indent);
|
||||||
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
|
parsed.second_expression,
|
||||||
|
));
|
||||||
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
|
parsed.second_expression,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
|
steps.push_back(crate::builder::Step::Format(parsed.second_expression));
|
||||||
|
}
|
||||||
|
|
||||||
|
steps
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
pub(crate) mod apply;
|
pub(crate) mod apply;
|
||||||
pub(crate) mod assert_and_with;
|
pub(crate) mod assert_or_with;
|
||||||
pub(crate) mod attr_set;
|
pub(crate) mod attr_set;
|
||||||
pub(crate) mod bin_op_and_or_default;
|
pub(crate) mod bin_op_and_or_default;
|
||||||
pub(crate) mod dynamic;
|
pub(crate) mod dynamic;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue