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: split apply #214

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
cargo-tarpaulin
clippy
jq
linuxPackages_latest.perf
nodejs
nodePackages.prettier
nodePackages.prettier-plugin-toml
Expand Down
25 changes: 25 additions & 0 deletions front/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/alejandra_engine/src/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ impl Children {
self.get(self.current_index)
}

pub fn peek_prev(&mut self) -> Option<rnix::SyntaxElement> {
self.get(self.current_index - 1)
}

pub fn move_next(&mut self) {
self.current_index += 1
}
Expand Down
38 changes: 38 additions & 0 deletions src/alejandra_engine/src/parsers/apply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::collections::LinkedList;

#[derive(Debug, Default)]
pub(crate) struct Apply {
pub left: Option<rnix::SyntaxElement>,
pub comments: LinkedList<String>,
pub newline: bool,
pub right: Option<rnix::SyntaxElement>,
}

pub(crate) fn parse(
build_ctx: &crate::builder::BuildCtx,
node: &rnix::SyntaxNode,
) -> Apply {
let mut apply = Apply::default();

let mut children = crate::children::Children::new(build_ctx, node);

// 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;
}
}
});

// right
apply.right = Some(children.get_next().unwrap());

apply
}
1 change: 1 addition & 0 deletions src/alejandra_engine/src/parsers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod apply;
pub(crate) mod if_else;
pub(crate) mod pattern;
48 changes: 22 additions & 26 deletions src/alejandra_engine/src/rules/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,51 @@ pub(crate) fn rule(
) -> std::collections::LinkedList<crate::builder::Step> {
let mut steps = std::collections::LinkedList::new();

let mut children = crate::children::Children::new(build_ctx, node);
let apply = crate::parsers::apply::parse(build_ctx, node);

let vertical = children.has_comments()
|| children.has_newlines()
|| build_ctx.vertical;
let vertical =
build_ctx.vertical || apply.newline || !apply.comments.is_empty();

// left
let child = children.get_next().unwrap();

let element = apply.left.unwrap();
if vertical {
steps.push_back(crate::builder::Step::FormatWider(child));
steps.push_back(crate::builder::Step::FormatWider(element));
} else {
steps.push_back(crate::builder::Step::Format(child));
steps.push_back(crate::builder::Step::Format(element));
}

// /**/
children.drain_trivia(|element| match element {
crate::children::Trivia::Comment(text) => {
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));
}
crate::children::Trivia::Whitespace(_) => {}
});

let child_prev = children.peek_prev().unwrap();
}

// right
let child = children.get_next().unwrap();
let element = apply.right.unwrap();
if vertical {
if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE = child_prev.kind()
{
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else if let rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_STRING = child.kind()
if !apply.newline
&& !comments
&& matches!(
element.kind(),
rnix::SyntaxKind::NODE_ATTR_SET
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_PAREN
| rnix::SyntaxKind::NODE_STRING
)
{
steps.push_back(crate::builder::Step::Whitespace);
} else {
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::FormatWider(element));
} else {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Format(child));
steps.push_back(crate::builder::Step::Format(element));
}

steps
Expand Down