1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-30 12:07:46 +00:00

refactor: apply

This commit is contained in:
Kevin Amado 2022-02-27 16:38:00 -05:00
parent 421b1e911f
commit 712f3988f9
2 changed files with 58 additions and 50 deletions

View file

@ -1,38 +1,45 @@
use std::collections::LinkedList;
#[derive(Debug, Default)]
#[derive(Debug)]
pub(crate) struct Apply {
pub left: Option<rnix::SyntaxElement>,
pub comments: LinkedList<String>,
pub newline: bool,
pub right: Option<rnix::SyntaxElement>,
pub left_expression: rnix::SyntaxElement,
pub comments_after_left: LinkedList<String>,
pub has_newlines_after_left: bool,
pub right_expression: rnix::SyntaxElement,
}
pub(crate) fn parse(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
) -> Apply {
let mut apply = Apply::default();
impl Apply {
pub(crate) fn parse(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
) -> 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
apply.left = Some(children.get_next().unwrap());
// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
apply.comments.push_back(text);
}
crate::children::Trivia::Whitespace(text) => {
if !apply.newline {
apply.newline = crate::utils::count_newlines(&text) > 0;
// comments_after_left
// has_newlines_after_left
let mut comments_after_left = LinkedList::new();
let mut has_newlines_after_left = false;
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
comments_after_left.push_back(text);
}
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
}
}

View file

@ -4,36 +4,35 @@ pub(crate) fn rule(
) -> std::collections::LinkedList<crate::builder::Step> {
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 =
build_ctx.vertical || apply.newline || !apply.comments.is_empty();
let vertical = build_ctx.vertical
|| !parsed.comments_after_left.is_empty()
|| parsed.has_newlines_after_left;
// left
let element = apply.left.unwrap();
// left_expression
if vertical {
steps.push_back(crate::builder::Step::FormatWider(element));
steps.push_back(crate::builder::Step::FormatWider(
parsed.left_expression,
));
} else {
steps.push_back(crate::builder::Step::Format(element));
steps.push_back(crate::builder::Step::Format(parsed.left_expression));
}
// /**/
let comments = !apply.comments.is_empty();
if comments {
for text in apply.comments {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
}
// comments_after_left
let has_comments_after_left = !parsed.comments_after_left.is_empty();
for text in parsed.comments_after_left {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
}
// right
let element = apply.right.unwrap();
// right_expression
if vertical {
if !apply.newline
&& !comments
if !has_comments_after_left
&& !parsed.has_newlines_after_left
&& matches!(
element.kind(),
parsed.right_expression.kind(),
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_LIST
| 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::Pad);
};
steps.push_back(crate::builder::Step::FormatWider(element));
steps.push_back(crate::builder::Step::FormatWider(
parsed.right_expression,
));
} else {
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