Skip to content

Commit 6450948

Browse files
committed
perf: no need for pos_new
1 parent 3cd9911 commit 6450948

File tree

3 files changed

+19
-38
lines changed

3 files changed

+19
-38
lines changed

buildkite.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ steps:
8989
- cargo clippy
9090

9191
- label: flake check
92+
if: build.branch != "main"
9293
command:
9394
- echo +++
9495
- nix flake check

src/alejandra_engine/src/builder.rs

+17-37
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,26 @@ pub(crate) enum Step {
1313

1414
#[derive(Clone)]
1515
pub(crate) struct BuildCtx {
16-
pub force_wide: bool,
17-
pub indentation: usize,
18-
pub pos_new: crate::position::Position,
19-
pub pos_old: crate::position::Position,
20-
pub path: String,
21-
pub vertical: bool,
16+
pub force_wide: bool,
17+
pub force_wide_success: bool,
18+
pub indentation: usize,
19+
pub pos_old: crate::position::Position,
20+
pub path: String,
21+
pub vertical: bool,
2222
}
2323

2424
impl BuildCtx {
2525
pub fn new(
2626
force_wide: bool,
2727
path: String,
28-
pos_new: crate::position::Position,
2928
pos_old: crate::position::Position,
3029
vertical: bool,
3130
) -> BuildCtx {
3231
BuildCtx {
3332
force_wide,
33+
force_wide_success: true,
3434
indentation: 0,
3535
path,
36-
pos_new,
3736
pos_old,
3837
vertical,
3938
}
@@ -51,7 +50,6 @@ pub(crate) fn build(
5150
force_wide,
5251
path,
5352
crate::position::Position::default(),
54-
crate::position::Position::default(),
5553
vertical,
5654
);
5755

@@ -61,8 +59,8 @@ pub(crate) fn build(
6159
&crate::builder::Step::Format(element),
6260
);
6361

64-
if build_ctx.force_wide && build_ctx.pos_new.line > 1 {
65-
None
62+
if force_wide {
63+
if build_ctx.force_wide_success { Some(builder.finish()) } else { None }
6664
} else {
6765
Some(builder.finish())
6866
}
@@ -74,7 +72,7 @@ fn build_step(
7472

7573
step: &crate::builder::Step,
7674
) {
77-
if build_ctx.force_wide && build_ctx.pos_new.line > 1 {
75+
if build_ctx.force_wide && !build_ctx.force_wide_success {
7876
return;
7977
}
8078

@@ -102,7 +100,6 @@ fn build_step(
102100

103101
add_token(
104102
builder,
105-
build_ctx,
106103
rnix::SyntaxKind::TOKEN_COMMENT,
107104
&lines.join("\n"),
108105
);
@@ -120,45 +117,34 @@ fn build_step(
120117
build_ctx.indentation += 1;
121118
}
122119
crate::builder::Step::NewLine => {
123-
add_token(
124-
builder,
125-
build_ctx,
126-
rnix::SyntaxKind::TOKEN_WHITESPACE,
127-
"\n",
128-
);
120+
build_ctx.force_wide_success = false;
121+
122+
add_token(builder, rnix::SyntaxKind::TOKEN_WHITESPACE, "\n");
129123
}
130124
crate::builder::Step::Pad => {
131125
if build_ctx.indentation > 0 {
132126
add_token(
133127
builder,
134-
build_ctx,
135128
rnix::SyntaxKind::TOKEN_WHITESPACE,
136129
&format!("{0:<1$}", "", 2 * build_ctx.indentation),
137130
);
138131
}
139132
}
140133
crate::builder::Step::Token(kind, text) => {
141-
add_token(builder, build_ctx, *kind, text);
134+
add_token(builder, *kind, text);
142135
}
143136
crate::builder::Step::Whitespace => {
144-
add_token(
145-
builder,
146-
build_ctx,
147-
rnix::SyntaxKind::TOKEN_WHITESPACE,
148-
" ",
149-
);
137+
add_token(builder, rnix::SyntaxKind::TOKEN_WHITESPACE, " ");
150138
}
151139
}
152140
}
153141

154142
fn add_token(
155143
builder: &mut rowan::GreenNodeBuilder,
156-
build_ctx: &mut BuildCtx,
157144
kind: rnix::SyntaxKind,
158145
text: &str,
159146
) {
160147
builder.token(rowan::SyntaxKind(kind as u16), text);
161-
build_ctx.pos_new.update(text);
162148
}
163149

164150
fn format(
@@ -254,7 +240,7 @@ fn format(
254240
}
255241
rnix::SyntaxElement::Token(token) => {
256242
let text = token.text();
257-
add_token(builder, build_ctx, kind, text);
243+
add_token(builder, kind, text);
258244
build_ctx.pos_old.update(text);
259245
}
260246
}
@@ -283,13 +269,7 @@ pub(crate) fn fits_in_single_line(
283269
build_ctx: &crate::builder::BuildCtx,
284270
element: rnix::SyntaxElement,
285271
) -> bool {
286-
let line = build_ctx.pos_new.line;
287-
let maybe_green_node = build(element, true, build_ctx.path.clone(), false);
288-
289-
match maybe_green_node {
290-
Some(_) => build_ctx.pos_new.line == line,
291-
None => false,
292-
}
272+
build(element, true, build_ctx.path.clone(), false).is_some()
293273
}
294274

295275
pub(crate) fn make_isolated_token(

src/alejandra_engine/src/rules/lambda.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(crate) fn rule(
7575
| rnix::SyntaxKind::NODE_LET_IN
7676
| rnix::SyntaxKind::NODE_LIST
7777
| rnix::SyntaxKind::NODE_STRING
78-
) && build_ctx.pos_new.column > 1;
78+
) && build_ctx.indentation > 0;
7979

8080
if should_indent {
8181
steps.push_back(crate::builder::Step::Indent);

0 commit comments

Comments
 (0)