Skip to content

Commit 99c80e9

Browse files
committed
perf: split children rewrittes
1 parent f04306e commit 99c80e9

File tree

3 files changed

+129
-120
lines changed

3 files changed

+129
-120
lines changed

flake.nix

+48-56
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,39 @@
1616
nixpkgsForHost = host:
1717
import inputs.nixpkgs {
1818
overlays = [
19-
(
20-
self: super: {
21-
alejandra = self.rustPlatform.buildRustPackage {
22-
pname = "alejandra";
23-
inherit version;
24-
src = self.stdenv.mkDerivation {
25-
name = "src";
26-
builder = builtins.toFile "builder.sh" ''
27-
source $stdenv/setup
28-
29-
mkdir $out
30-
cp -rT --no-preserve=mode,ownership $src $out/src/
31-
cp $cargoLock $out/Cargo.lock
32-
cp $cargoToml $out/Cargo.toml
33-
'';
34-
cargoLock = ./Cargo.lock;
35-
cargoToml = ./Cargo.toml;
36-
src = ./src;
37-
};
38-
cargoLock.lockFile = ./Cargo.lock;
39-
40-
passthru.tests = {
41-
version = self.testVersion {package = super.alejandra;};
42-
};
43-
44-
meta = {
45-
description = "The Uncompromising Nix Code Formatter.";
46-
homepage = "https://github.com/kamadorueda/alejandra";
47-
license = self.lib.licenses.unlicense;
48-
maintainers = [self.lib.maintainers.kamadorueda];
49-
platforms = self.lib.systems.doubles.all;
50-
};
19+
(self: super: {
20+
alejandra = self.rustPlatform.buildRustPackage {
21+
pname = "alejandra";
22+
inherit version;
23+
src = self.stdenv.mkDerivation {
24+
name = "src";
25+
builder = builtins.toFile "builder.sh" ''
26+
source $stdenv/setup
27+
28+
mkdir $out
29+
cp -rT --no-preserve=mode,ownership $src $out/src/
30+
cp $cargoLock $out/Cargo.lock
31+
cp $cargoToml $out/Cargo.toml
32+
'';
33+
cargoLock = ./Cargo.lock;
34+
cargoToml = ./Cargo.toml;
35+
src = ./src;
5136
};
52-
}
53-
)
37+
cargoLock.lockFile = ./Cargo.lock;
38+
39+
passthru.tests = {
40+
version = self.testVersion {package = super.alejandra;};
41+
};
42+
43+
meta = {
44+
description = "The Uncompromising Nix Code Formatter.";
45+
homepage = "https://github.com/kamadorueda/alejandra";
46+
license = self.lib.licenses.unlicense;
47+
maintainers = [self.lib.maintainers.kamadorueda];
48+
platforms = self.lib.systems.doubles.all;
49+
};
50+
};
51+
})
5452
];
5553
system = host;
5654
};
@@ -62,25 +60,21 @@
6260

6361
buildBinariesForHost = host: pkgs: let
6462
binaries = builtins.listToAttrs (
65-
builtins.map (
66-
pkg: {
67-
name = "alejandra-${pkg.stdenv.targetPlatform.config}";
68-
value = pkg;
69-
}
70-
)
63+
builtins.map (pkg: {
64+
name = "alejandra-${pkg.stdenv.targetPlatform.config}";
65+
value = pkg;
66+
})
7167
pkgs
7268
);
7369
in
7470
binaries
7571
// {
7672
"alejandra-binaries" = nixpkgs.${host}.linkFarm "alejandra-binaries" (
7773
nixpkgs.${host}.lib.mapAttrsToList
78-
(
79-
name: binary: {
80-
inherit name;
81-
path = "${binary}/bin/alejandra";
82-
}
83-
)
74+
(name: binary: {
75+
inherit name;
76+
path = "${binary}/bin/alejandra";
77+
})
8478
binaries
8579
);
8680
};
@@ -130,20 +124,18 @@
130124
alejandra
131125
];
132126
packages."x86_64-linux" = with nixpkgs."x86_64-linux";
133-
(
134-
buildBinariesForHost "x86_64-linux" [
135-
alejandra
136-
pkgsStatic.alejandra
127+
(buildBinariesForHost "x86_64-linux" [
128+
alejandra
129+
pkgsStatic.alejandra
137130

138-
pkgsCross.aarch64-multiplatform.pkgsStatic.alejandra
131+
pkgsCross.aarch64-multiplatform.pkgsStatic.alejandra
139132

140-
pkgsCross.armv7l-hf-multiplatform.pkgsStatic.alejandra
133+
pkgsCross.armv7l-hf-multiplatform.pkgsStatic.alejandra
141134

142-
pkgsCross.gnu32.pkgsStatic.alejandra
135+
pkgsCross.gnu32.pkgsStatic.alejandra
143136

144-
pkgsCross.raspberryPi.pkgsStatic.alejandra
145-
]
146-
)
137+
pkgsCross.raspberryPi.pkgsStatic.alejandra
138+
])
147139
// {
148140
"alejandra-vscode-vsix" = mkYarnPackage {
149141
name = "alejandra";

front/flake.nix

+5-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
devShell.${system} = nixpkgs.mkShell {
1616
name = "alejandra";
1717
packages = [
18-
(
19-
fenix.combine [
20-
fenix.latest.rustc
21-
fenix.latest.toolchain
22-
fenix.targets."wasm32-unknown-unknown".latest.rust-std
23-
]
24-
)
18+
(fenix.combine [
19+
fenix.latest.rustc
20+
fenix.latest.toolchain
21+
fenix.targets."wasm32-unknown-unknown".latest.rust-std
22+
])
2523
nixpkgs.binaryen
2624
nixpkgs.pkg-config
2725
nixpkgs.openssl

src/alejandra_engine/src/children.rs

+76-57
Original file line numberDiff line numberDiff line change
@@ -26,70 +26,89 @@ impl Children {
2626
};
2727

2828
for child in node.children_with_tokens() {
29-
let text: String = child.to_string();
29+
match child {
30+
rnix::SyntaxElement::Node(node) => {
31+
match node.kind() {
32+
rnix::SyntaxKind::NODE_PAREN => {
33+
let mut simplified = node.clone();
3034

31-
match child.kind() {
32-
rnix::SyntaxKind::NODE_PAREN => {
33-
let mut simplified = child.into_node().unwrap();
34-
35-
while matches!(
36-
simplified.kind(),
37-
rnix::SyntaxKind::NODE_PAREN
38-
) {
39-
let mut children =
40-
crate::children2::new(build_ctx, &simplified);
41-
42-
let opener = children.next().unwrap();
43-
let expression = children.next().unwrap();
44-
let closer = children.next().unwrap();
45-
46-
if !opener.has_inline_comment
47-
&& !opener.has_comments
48-
&& !expression.has_inline_comment
49-
&& !expression.has_comments
50-
&& !closer.has_inline_comment
51-
&& !closer.has_comments
52-
&& matches!(
53-
expression.element.kind(),
54-
rnix::SyntaxKind::NODE_ATTR_SET
55-
| rnix::SyntaxKind::NODE_IDENT
56-
| rnix::SyntaxKind::NODE_LIST
57-
| rnix::SyntaxKind::NODE_LITERAL
58-
| rnix::SyntaxKind::NODE_PAREN
59-
| rnix::SyntaxKind::NODE_PATH_WITH_INTERPOL
60-
| rnix::SyntaxKind::NODE_STRING
61-
)
62-
{
63-
simplified =
64-
expression.element.into_node().unwrap();
65-
} else {
66-
break;
35+
while matches!(
36+
simplified.kind(),
37+
rnix::SyntaxKind::NODE_PAREN
38+
) {
39+
let mut children = crate::children2::new(
40+
build_ctx,
41+
&simplified,
42+
);
43+
44+
let opener = children.next().unwrap();
45+
let expression = children.next().unwrap();
46+
let closer = children.next().unwrap();
47+
48+
if !opener.has_inline_comment
49+
&& !opener.has_comments
50+
&& !expression.has_inline_comment
51+
&& !expression.has_comments
52+
&& !closer.has_inline_comment
53+
&& !closer.has_comments
54+
&& matches!(
55+
expression.element.kind(),
56+
rnix::SyntaxKind::NODE_ATTR_SET
57+
| rnix::SyntaxKind::NODE_IDENT
58+
| rnix::SyntaxKind::NODE_LIST
59+
| rnix::SyntaxKind::NODE_LITERAL
60+
| rnix::SyntaxKind::NODE_PAREN
61+
| rnix::SyntaxKind::NODE_PATH_WITH_INTERPOL
62+
| rnix::SyntaxKind::NODE_STRING
63+
)
64+
{
65+
simplified =
66+
expression.element.into_node().unwrap();
67+
} else {
68+
break;
69+
}
70+
}
71+
72+
children.push(simplified.into());
73+
}
74+
_ => {
75+
children.push(node.clone().into());
6776
}
6877
}
6978

70-
children.push(simplified.into());
71-
}
72-
rnix::SyntaxKind::TOKEN_COMMENT => {
73-
children.push(
74-
crate::builder::make_isolated_token(
75-
rnix::SyntaxKind::TOKEN_COMMENT,
76-
&dedent_comment(pos.as_ref().unwrap(), &text),
77-
)
78-
.into(),
79-
);
80-
}
81-
rnix::SyntaxKind::TOKEN_WHITESPACE => {
82-
if crate::utils::count_newlines(&text) > 0 {
83-
children.push(child);
79+
if pos.is_some() {
80+
pos.as_mut().unwrap().update(&node.text().to_string());
8481
}
8582
}
86-
_ => {
87-
children.push(child);
88-
}
89-
}
9083

91-
if pos.is_some() {
92-
pos.as_mut().unwrap().update(&text);
84+
rnix::SyntaxElement::Token(token) => {
85+
match token.kind() {
86+
rnix::SyntaxKind::TOKEN_COMMENT => {
87+
children.push(
88+
crate::builder::make_isolated_token(
89+
rnix::SyntaxKind::TOKEN_COMMENT,
90+
&dedent_comment(
91+
pos.as_ref().unwrap(),
92+
token.text(),
93+
),
94+
)
95+
.into(),
96+
);
97+
}
98+
rnix::SyntaxKind::TOKEN_WHITESPACE => {
99+
if crate::utils::count_newlines(token.text()) > 0 {
100+
children.push(token.clone().into());
101+
}
102+
}
103+
_ => {
104+
children.push(token.clone().into());
105+
}
106+
}
107+
108+
if pos.is_some() {
109+
pos.as_mut().unwrap().update(token.text());
110+
}
111+
}
93112
}
94113
}
95114

0 commit comments

Comments
 (0)