Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: no need for pos_new #233

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions buildkite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ steps:
- cargo clippy

- label: flake check
if: build.branch != "main"
command:
- echo +++
- nix flake check
54 changes: 17 additions & 37 deletions src/alejandra_engine/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,26 @@ pub(crate) enum Step {

#[derive(Clone)]
pub(crate) struct BuildCtx {
pub force_wide: bool,
pub indentation: usize,
pub pos_new: crate::position::Position,
pub pos_old: crate::position::Position,
pub path: String,
pub vertical: bool,
pub force_wide: bool,
pub force_wide_success: bool,
pub indentation: usize,
pub pos_old: crate::position::Position,
pub path: String,
pub vertical: bool,
}

impl BuildCtx {
pub fn new(
force_wide: bool,
path: String,
pos_new: crate::position::Position,
pos_old: crate::position::Position,
vertical: bool,
) -> BuildCtx {
BuildCtx {
force_wide,
force_wide_success: true,
indentation: 0,
path,
pos_new,
pos_old,
vertical,
}
Expand All @@ -51,7 +50,6 @@ pub(crate) fn build(
force_wide,
path,
crate::position::Position::default(),
crate::position::Position::default(),
vertical,
);

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

if build_ctx.force_wide && build_ctx.pos_new.line > 1 {
None
if force_wide {
if build_ctx.force_wide_success { Some(builder.finish()) } else { None }
} else {
Some(builder.finish())
}
Expand All @@ -74,7 +72,7 @@ fn build_step(

step: &crate::builder::Step,
) {
if build_ctx.force_wide && build_ctx.pos_new.line > 1 {
if build_ctx.force_wide && !build_ctx.force_wide_success {
return;
}

Expand Down Expand Up @@ -102,7 +100,6 @@ fn build_step(

add_token(
builder,
build_ctx,
rnix::SyntaxKind::TOKEN_COMMENT,
&lines.join("\n"),
);
Expand All @@ -120,45 +117,34 @@ fn build_step(
build_ctx.indentation += 1;
}
crate::builder::Step::NewLine => {
add_token(
builder,
build_ctx,
rnix::SyntaxKind::TOKEN_WHITESPACE,
"\n",
);
build_ctx.force_wide_success = false;

add_token(builder, rnix::SyntaxKind::TOKEN_WHITESPACE, "\n");
}
crate::builder::Step::Pad => {
if build_ctx.indentation > 0 {
add_token(
builder,
build_ctx,
rnix::SyntaxKind::TOKEN_WHITESPACE,
&format!("{0:<1$}", "", 2 * build_ctx.indentation),
);
}
}
crate::builder::Step::Token(kind, text) => {
add_token(builder, build_ctx, *kind, text);
add_token(builder, *kind, text);
}
crate::builder::Step::Whitespace => {
add_token(
builder,
build_ctx,
rnix::SyntaxKind::TOKEN_WHITESPACE,
" ",
);
add_token(builder, rnix::SyntaxKind::TOKEN_WHITESPACE, " ");
}
}
}

fn add_token(
builder: &mut rowan::GreenNodeBuilder,
build_ctx: &mut BuildCtx,
kind: rnix::SyntaxKind,
text: &str,
) {
builder.token(rowan::SyntaxKind(kind as u16), text);
build_ctx.pos_new.update(text);
}

fn format(
Expand Down Expand Up @@ -254,7 +240,7 @@ fn format(
}
rnix::SyntaxElement::Token(token) => {
let text = token.text();
add_token(builder, build_ctx, kind, text);
add_token(builder, kind, text);
build_ctx.pos_old.update(text);
}
}
Expand Down Expand Up @@ -283,13 +269,7 @@ pub(crate) fn fits_in_single_line(
build_ctx: &crate::builder::BuildCtx,
element: rnix::SyntaxElement,
) -> bool {
let line = build_ctx.pos_new.line;
let maybe_green_node = build(element, true, build_ctx.path.clone(), false);

match maybe_green_node {
Some(_) => build_ctx.pos_new.line == line,
None => false,
}
build(element, true, build_ctx.path.clone(), false).is_some()
}

pub(crate) fn make_isolated_token(
Expand Down
2 changes: 1 addition & 1 deletion src/alejandra_engine/src/rules/lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub(crate) fn rule(
| rnix::SyntaxKind::NODE_LET_IN
| rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_STRING
) && build_ctx.pos_new.column > 1;
) && build_ctx.indentation > 0;

if should_indent {
steps.push_back(crate::builder::Step::Indent);
Expand Down