Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: apply #216

Merged
merged 1 commit into from Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 34 additions & 27 deletions src/alejandra_engine/src/parsers/apply.rs
@@ -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
apply.right = Some(children.get_next().unwrap());
// right_expression
let right_expression = children.get_next().unwrap();

apply
Apply {
left_expression,
comments_after_left,
has_newlines_after_left,
right_expression,
}
}
}
45 changes: 23 additions & 22 deletions src/alejandra_engine/src/rules/apply.rs
Expand Up @@ -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
Expand All @@ -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
Expand Down