mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-30 12:07:46 +00:00
refactor: split apply
This commit is contained in:
parent
790660b072
commit
3071c38e10
6 changed files with 87 additions and 30 deletions
|
@ -105,6 +105,7 @@
|
|||
cargo-tarpaulin
|
||||
clippy
|
||||
jq
|
||||
linuxPackages_latest.perf
|
||||
nodejs
|
||||
nodePackages.prettier
|
||||
nodePackages.prettier-plugin-toml
|
||||
|
|
25
front/Cargo.lock
generated
25
front/Cargo.lock
generated
|
@ -6,6 +6,7 @@ version = 3
|
|||
name = "alejandra_engine"
|
||||
version = "0.6.0"
|
||||
dependencies = [
|
||||
"mimalloc",
|
||||
"rnix",
|
||||
"rowan",
|
||||
]
|
||||
|
@ -41,6 +42,12 @@ dependencies = [
|
|||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.73"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
|
@ -87,6 +94,15 @@ version = "0.2.119"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4"
|
||||
|
||||
[[package]]
|
||||
name = "libmimalloc-sys"
|
||||
version = "0.1.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7705fc40f6ed493f73584abbb324e74f96b358ff60dfe5659a0f8fc12c590a69"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.14"
|
||||
|
@ -111,6 +127,15 @@ version = "0.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
|
||||
|
||||
[[package]]
|
||||
name = "mimalloc"
|
||||
version = "0.1.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0dfa131390c2f6bdb3242f65ff271fcdaca5ff7b6c08f28398be7f2280e3926"
|
||||
dependencies = [
|
||||
"libmimalloc-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.14"
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
38
src/alejandra_engine/src/parsers/apply.rs
Normal file
38
src/alejandra_engine/src/parsers/apply.rs
Normal file
|
@ -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,2 +1,3 @@
|
|||
pub(crate) mod apply;
|
||||
pub(crate) mod if_else;
|
||||
pub(crate) mod pattern;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue