mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 13:07:47 +00:00
refactor: apply
This commit is contained in:
parent
421b1e911f
commit
712f3988f9
2 changed files with 58 additions and 50 deletions
|
@ -1,38 +1,45 @@
|
||||||
use std::collections::LinkedList;
|
use std::collections::LinkedList;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct Apply {
|
pub(crate) struct Apply {
|
||||||
pub left: Option<rnix::SyntaxElement>,
|
pub left_expression: rnix::SyntaxElement,
|
||||||
pub comments: LinkedList<String>,
|
pub comments_after_left: LinkedList<String>,
|
||||||
pub newline: bool,
|
pub has_newlines_after_left: bool,
|
||||||
pub right: Option<rnix::SyntaxElement>,
|
pub right_expression: rnix::SyntaxElement,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn parse(
|
impl Apply {
|
||||||
build_ctx: &crate::builder::BuildCtx,
|
pub(crate) fn parse(
|
||||||
node: &rnix::SyntaxNode,
|
build_ctx: &crate::builder::BuildCtx,
|
||||||
) -> Apply {
|
node: &rnix::SyntaxNode,
|
||||||
let mut apply = Apply::default();
|
) -> Apply {
|
||||||
|
let mut children = crate::children::Children::new(build_ctx, node);
|
||||||
|
|
||||||
let mut children = crate::children::Children::new(build_ctx, node);
|
// left_expression
|
||||||
|
let left_expression = children.get_next().unwrap();
|
||||||
|
|
||||||
// left
|
// comments_after_left
|
||||||
apply.left = Some(children.get_next().unwrap());
|
// has_newlines_after_left
|
||||||
|
let mut comments_after_left = LinkedList::new();
|
||||||
// /**/
|
let mut has_newlines_after_left = false;
|
||||||
children.drain_trivia(|element| match element {
|
children.drain_trivia(|element| match element {
|
||||||
crate::children::Trivia::Comment(text) => {
|
crate::children::Trivia::Comment(text) => {
|
||||||
apply.comments.push_back(text);
|
comments_after_left.push_back(text);
|
||||||
}
|
|
||||||
crate::children::Trivia::Whitespace(text) => {
|
|
||||||
if !apply.newline {
|
|
||||||
apply.newline = crate::utils::count_newlines(&text) > 0;
|
|
||||||
}
|
}
|
||||||
|
crate::children::Trivia::Whitespace(text) => {
|
||||||
|
has_newlines_after_left = has_newlines_after_left
|
||||||
|
|| crate::utils::count_newlines(&text) > 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// right_expression
|
||||||
|
let right_expression = children.get_next().unwrap();
|
||||||
|
|
||||||
|
Apply {
|
||||||
|
left_expression,
|
||||||
|
comments_after_left,
|
||||||
|
has_newlines_after_left,
|
||||||
|
right_expression,
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
// right
|
|
||||||
apply.right = Some(children.get_next().unwrap());
|
|
||||||
|
|
||||||
apply
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,36 +4,35 @@ pub(crate) fn rule(
|
||||||
) -> std::collections::LinkedList<crate::builder::Step> {
|
) -> std::collections::LinkedList<crate::builder::Step> {
|
||||||
let mut steps = std::collections::LinkedList::new();
|
let mut steps = std::collections::LinkedList::new();
|
||||||
|
|
||||||
let apply = crate::parsers::apply::parse(build_ctx, node);
|
let parsed = crate::parsers::apply::Apply::parse(build_ctx, node);
|
||||||
|
|
||||||
let vertical =
|
let vertical = build_ctx.vertical
|
||||||
build_ctx.vertical || apply.newline || !apply.comments.is_empty();
|
|| !parsed.comments_after_left.is_empty()
|
||||||
|
|| parsed.has_newlines_after_left;
|
||||||
|
|
||||||
// left
|
// left_expression
|
||||||
let element = apply.left.unwrap();
|
|
||||||
if vertical {
|
if vertical {
|
||||||
steps.push_back(crate::builder::Step::FormatWider(element));
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
|
parsed.left_expression,
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
steps.push_back(crate::builder::Step::Format(element));
|
steps.push_back(crate::builder::Step::Format(parsed.left_expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// comments_after_left
|
||||||
let comments = !apply.comments.is_empty();
|
let has_comments_after_left = !parsed.comments_after_left.is_empty();
|
||||||
if comments {
|
for text in parsed.comments_after_left {
|
||||||
for text in apply.comments {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::Comment(text));
|
||||||
steps.push_back(crate::builder::Step::Comment(text));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// right
|
// right_expression
|
||||||
let element = apply.right.unwrap();
|
|
||||||
if vertical {
|
if vertical {
|
||||||
if !apply.newline
|
if !has_comments_after_left
|
||||||
&& !comments
|
&& !parsed.has_newlines_after_left
|
||||||
&& matches!(
|
&& matches!(
|
||||||
element.kind(),
|
parsed.right_expression.kind(),
|
||||||
rnix::SyntaxKind::NODE_ATTR_SET
|
rnix::SyntaxKind::NODE_ATTR_SET
|
||||||
| rnix::SyntaxKind::NODE_LIST
|
| rnix::SyntaxKind::NODE_LIST
|
||||||
| rnix::SyntaxKind::NODE_PAREN
|
| rnix::SyntaxKind::NODE_PAREN
|
||||||
|
@ -45,10 +44,12 @@ pub(crate) fn rule(
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
};
|
};
|
||||||
steps.push_back(crate::builder::Step::FormatWider(element));
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
|
parsed.right_expression,
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
steps.push_back(crate::builder::Step::Format(element));
|
steps.push_back(crate::builder::Step::Format(parsed.right_expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
steps
|
steps
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue