From 99c80e9e51b05a8e7af5e34c2ef51cdd26b15646 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Tue, 1 Mar 2022 23:17:26 -0500 Subject: [PATCH] perf: split children rewrittes --- flake.nix | 102 ++++++++++----------- front/flake.nix | 12 ++- src/alejandra_engine/src/children.rs | 127 +++++++++++++++------------ 3 files changed, 125 insertions(+), 116 deletions(-) diff --git a/flake.nix b/flake.nix index ece2d61..091447a 100644 --- a/flake.nix +++ b/flake.nix @@ -16,41 +16,39 @@ nixpkgsForHost = host: import inputs.nixpkgs { overlays = [ - ( - self: super: { - alejandra = self.rustPlatform.buildRustPackage { - pname = "alejandra"; - inherit version; - src = self.stdenv.mkDerivation { - name = "src"; - builder = builtins.toFile "builder.sh" '' - source $stdenv/setup + (self: super: { + alejandra = self.rustPlatform.buildRustPackage { + pname = "alejandra"; + inherit version; + src = self.stdenv.mkDerivation { + name = "src"; + builder = builtins.toFile "builder.sh" '' + source $stdenv/setup - mkdir $out - cp -rT --no-preserve=mode,ownership $src $out/src/ - cp $cargoLock $out/Cargo.lock - cp $cargoToml $out/Cargo.toml - ''; - cargoLock = ./Cargo.lock; - cargoToml = ./Cargo.toml; - src = ./src; - }; - cargoLock.lockFile = ./Cargo.lock; - - passthru.tests = { - version = self.testVersion {package = super.alejandra;}; - }; - - meta = { - description = "The Uncompromising Nix Code Formatter."; - homepage = "https://github.com/kamadorueda/alejandra"; - license = self.lib.licenses.unlicense; - maintainers = [self.lib.maintainers.kamadorueda]; - platforms = self.lib.systems.doubles.all; - }; + mkdir $out + cp -rT --no-preserve=mode,ownership $src $out/src/ + cp $cargoLock $out/Cargo.lock + cp $cargoToml $out/Cargo.toml + ''; + cargoLock = ./Cargo.lock; + cargoToml = ./Cargo.toml; + src = ./src; }; - } - ) + cargoLock.lockFile = ./Cargo.lock; + + passthru.tests = { + version = self.testVersion {package = super.alejandra;}; + }; + + meta = { + description = "The Uncompromising Nix Code Formatter."; + homepage = "https://github.com/kamadorueda/alejandra"; + license = self.lib.licenses.unlicense; + maintainers = [self.lib.maintainers.kamadorueda]; + platforms = self.lib.systems.doubles.all; + }; + }; + }) ]; system = host; }; @@ -62,12 +60,10 @@ buildBinariesForHost = host: pkgs: let binaries = builtins.listToAttrs ( - builtins.map ( - pkg: { - name = "alejandra-${pkg.stdenv.targetPlatform.config}"; - value = pkg; - } - ) + builtins.map (pkg: { + name = "alejandra-${pkg.stdenv.targetPlatform.config}"; + value = pkg; + }) pkgs ); in @@ -75,12 +71,10 @@ // { "alejandra-binaries" = nixpkgs.${host}.linkFarm "alejandra-binaries" ( nixpkgs.${host}.lib.mapAttrsToList - ( - name: binary: { - inherit name; - path = "${binary}/bin/alejandra"; - } - ) + (name: binary: { + inherit name; + path = "${binary}/bin/alejandra"; + }) binaries ); }; @@ -130,20 +124,18 @@ alejandra ]; packages."x86_64-linux" = with nixpkgs."x86_64-linux"; - ( - buildBinariesForHost "x86_64-linux" [ - alejandra - pkgsStatic.alejandra + (buildBinariesForHost "x86_64-linux" [ + alejandra + pkgsStatic.alejandra - pkgsCross.aarch64-multiplatform.pkgsStatic.alejandra + pkgsCross.aarch64-multiplatform.pkgsStatic.alejandra - pkgsCross.armv7l-hf-multiplatform.pkgsStatic.alejandra + pkgsCross.armv7l-hf-multiplatform.pkgsStatic.alejandra - pkgsCross.gnu32.pkgsStatic.alejandra + pkgsCross.gnu32.pkgsStatic.alejandra - pkgsCross.raspberryPi.pkgsStatic.alejandra - ] - ) + pkgsCross.raspberryPi.pkgsStatic.alejandra + ]) // { "alejandra-vscode-vsix" = mkYarnPackage { name = "alejandra"; diff --git a/front/flake.nix b/front/flake.nix index 158d85e..6fc9dbf 100644 --- a/front/flake.nix +++ b/front/flake.nix @@ -15,13 +15,11 @@ devShell.${system} = nixpkgs.mkShell { name = "alejandra"; packages = [ - ( - fenix.combine [ - fenix.latest.rustc - fenix.latest.toolchain - fenix.targets."wasm32-unknown-unknown".latest.rust-std - ] - ) + (fenix.combine [ + fenix.latest.rustc + fenix.latest.toolchain + fenix.targets."wasm32-unknown-unknown".latest.rust-std + ]) nixpkgs.binaryen nixpkgs.pkg-config nixpkgs.openssl diff --git a/src/alejandra_engine/src/children.rs b/src/alejandra_engine/src/children.rs index 413f016..fcf26ff 100644 --- a/src/alejandra_engine/src/children.rs +++ b/src/alejandra_engine/src/children.rs @@ -26,70 +26,89 @@ impl Children { }; for child in node.children_with_tokens() { - let text: String = child.to_string(); + match child { + rnix::SyntaxElement::Node(node) => { + match node.kind() { + rnix::SyntaxKind::NODE_PAREN => { + let mut simplified = node.clone(); - match child.kind() { - rnix::SyntaxKind::NODE_PAREN => { - let mut simplified = child.into_node().unwrap(); + while matches!( + simplified.kind(), + rnix::SyntaxKind::NODE_PAREN + ) { + let mut children = crate::children2::new( + build_ctx, + &simplified, + ); - while matches!( - simplified.kind(), - rnix::SyntaxKind::NODE_PAREN - ) { - let mut children = - crate::children2::new(build_ctx, &simplified); + let opener = children.next().unwrap(); + let expression = children.next().unwrap(); + let closer = children.next().unwrap(); - let opener = children.next().unwrap(); - let expression = children.next().unwrap(); - let closer = children.next().unwrap(); + if !opener.has_inline_comment + && !opener.has_comments + && !expression.has_inline_comment + && !expression.has_comments + && !closer.has_inline_comment + && !closer.has_comments + && matches!( + expression.element.kind(), + rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_IDENT + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_LITERAL + | rnix::SyntaxKind::NODE_PAREN + | rnix::SyntaxKind::NODE_PATH_WITH_INTERPOL + | rnix::SyntaxKind::NODE_STRING + ) + { + simplified = + expression.element.into_node().unwrap(); + } else { + break; + } + } - if !opener.has_inline_comment - && !opener.has_comments - && !expression.has_inline_comment - && !expression.has_comments - && !closer.has_inline_comment - && !closer.has_comments - && matches!( - expression.element.kind(), - rnix::SyntaxKind::NODE_ATTR_SET - | rnix::SyntaxKind::NODE_IDENT - | rnix::SyntaxKind::NODE_LIST - | rnix::SyntaxKind::NODE_LITERAL - | rnix::SyntaxKind::NODE_PAREN - | rnix::SyntaxKind::NODE_PATH_WITH_INTERPOL - | rnix::SyntaxKind::NODE_STRING - ) - { - simplified = - expression.element.into_node().unwrap(); - } else { - break; + children.push(simplified.into()); + } + _ => { + children.push(node.clone().into()); } } - children.push(simplified.into()); - } - rnix::SyntaxKind::TOKEN_COMMENT => { - children.push( - crate::builder::make_isolated_token( - rnix::SyntaxKind::TOKEN_COMMENT, - &dedent_comment(pos.as_ref().unwrap(), &text), - ) - .into(), - ); - } - rnix::SyntaxKind::TOKEN_WHITESPACE => { - if crate::utils::count_newlines(&text) > 0 { - children.push(child); + if pos.is_some() { + pos.as_mut().unwrap().update(&node.text().to_string()); } } - _ => { - children.push(child); - } - } - if pos.is_some() { - pos.as_mut().unwrap().update(&text); + rnix::SyntaxElement::Token(token) => { + match token.kind() { + rnix::SyntaxKind::TOKEN_COMMENT => { + children.push( + crate::builder::make_isolated_token( + rnix::SyntaxKind::TOKEN_COMMENT, + &dedent_comment( + pos.as_ref().unwrap(), + token.text(), + ), + ) + .into(), + ); + } + rnix::SyntaxKind::TOKEN_WHITESPACE => { + if crate::utils::count_newlines(token.text()) > 0 { + children.push(token.clone().into()); + } + } + _ => { + children.push(token.clone().into()); + } + } + + if pos.is_some() { + pos.as_mut().unwrap().update(token.text()); + } + } } }