diff --git a/flake.nix b/flake.nix index 7be714a..4470941 100644 --- a/flake.nix +++ b/flake.nix @@ -105,6 +105,7 @@ cargo-tarpaulin clippy jq + linuxPackages_latest.perf nodejs nodePackages.prettier nodePackages.prettier-plugin-toml diff --git a/front/Cargo.lock b/front/Cargo.lock index 12da102..ac5b58b 100644 --- a/front/Cargo.lock +++ b/front/Cargo.lock @@ -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" diff --git a/src/alejandra_engine/src/children.rs b/src/alejandra_engine/src/children.rs index bd366a5..2b0f8b7 100644 --- a/src/alejandra_engine/src/children.rs +++ b/src/alejandra_engine/src/children.rs @@ -96,10 +96,6 @@ impl Children { self.get(self.current_index) } - pub fn peek_prev(&mut self) -> Option { - self.get(self.current_index - 1) - } - pub fn move_next(&mut self) { self.current_index += 1 } diff --git a/src/alejandra_engine/src/parsers/apply.rs b/src/alejandra_engine/src/parsers/apply.rs new file mode 100644 index 0000000..726333e --- /dev/null +++ b/src/alejandra_engine/src/parsers/apply.rs @@ -0,0 +1,38 @@ +use std::collections::LinkedList; + +#[derive(Debug, Default)] +pub(crate) struct Apply { + pub left: Option, + pub comments: LinkedList, + pub newline: bool, + pub right: Option, +} + +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 +} diff --git a/src/alejandra_engine/src/parsers/mod.rs b/src/alejandra_engine/src/parsers/mod.rs index 4b25b4a..0c0ea00 100644 --- a/src/alejandra_engine/src/parsers/mod.rs +++ b/src/alejandra_engine/src/parsers/mod.rs @@ -1,2 +1,3 @@ +pub(crate) mod apply; pub(crate) mod if_else; pub(crate) mod pattern; diff --git a/src/alejandra_engine/src/rules/apply.rs b/src/alejandra_engine/src/rules/apply.rs index ba1a950..6ed33f5 100644 --- a/src/alejandra_engine/src/rules/apply.rs +++ b/src/alejandra_engine/src/rules/apply.rs @@ -4,55 +4,51 @@ pub(crate) fn rule( ) -> std::collections::LinkedList { 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