mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 13:07:47 +00:00
perf: no need for pos_new
This commit is contained in:
parent
3cd9911014
commit
64509489fb
3 changed files with 19 additions and 38 deletions
|
@ -89,6 +89,7 @@ steps:
|
||||||
- cargo clippy
|
- cargo clippy
|
||||||
|
|
||||||
- label: flake check
|
- label: flake check
|
||||||
|
if: build.branch != "main"
|
||||||
command:
|
command:
|
||||||
- echo +++
|
- echo +++
|
||||||
- nix flake check
|
- nix flake check
|
||||||
|
|
|
@ -13,27 +13,26 @@ pub(crate) enum Step {
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct BuildCtx {
|
pub(crate) struct BuildCtx {
|
||||||
pub force_wide: bool,
|
pub force_wide: bool,
|
||||||
pub indentation: usize,
|
pub force_wide_success: bool,
|
||||||
pub pos_new: crate::position::Position,
|
pub indentation: usize,
|
||||||
pub pos_old: crate::position::Position,
|
pub pos_old: crate::position::Position,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
pub vertical: bool,
|
pub vertical: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BuildCtx {
|
impl BuildCtx {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
force_wide: bool,
|
force_wide: bool,
|
||||||
path: String,
|
path: String,
|
||||||
pos_new: crate::position::Position,
|
|
||||||
pos_old: crate::position::Position,
|
pos_old: crate::position::Position,
|
||||||
vertical: bool,
|
vertical: bool,
|
||||||
) -> BuildCtx {
|
) -> BuildCtx {
|
||||||
BuildCtx {
|
BuildCtx {
|
||||||
force_wide,
|
force_wide,
|
||||||
|
force_wide_success: true,
|
||||||
indentation: 0,
|
indentation: 0,
|
||||||
path,
|
path,
|
||||||
pos_new,
|
|
||||||
pos_old,
|
pos_old,
|
||||||
vertical,
|
vertical,
|
||||||
}
|
}
|
||||||
|
@ -51,7 +50,6 @@ pub(crate) fn build(
|
||||||
force_wide,
|
force_wide,
|
||||||
path,
|
path,
|
||||||
crate::position::Position::default(),
|
crate::position::Position::default(),
|
||||||
crate::position::Position::default(),
|
|
||||||
vertical,
|
vertical,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -61,8 +59,8 @@ pub(crate) fn build(
|
||||||
&crate::builder::Step::Format(element),
|
&crate::builder::Step::Format(element),
|
||||||
);
|
);
|
||||||
|
|
||||||
if build_ctx.force_wide && build_ctx.pos_new.line > 1 {
|
if force_wide {
|
||||||
None
|
if build_ctx.force_wide_success { Some(builder.finish()) } else { None }
|
||||||
} else {
|
} else {
|
||||||
Some(builder.finish())
|
Some(builder.finish())
|
||||||
}
|
}
|
||||||
|
@ -74,7 +72,7 @@ fn build_step(
|
||||||
|
|
||||||
step: &crate::builder::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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +100,6 @@ fn build_step(
|
||||||
|
|
||||||
add_token(
|
add_token(
|
||||||
builder,
|
builder,
|
||||||
build_ctx,
|
|
||||||
rnix::SyntaxKind::TOKEN_COMMENT,
|
rnix::SyntaxKind::TOKEN_COMMENT,
|
||||||
&lines.join("\n"),
|
&lines.join("\n"),
|
||||||
);
|
);
|
||||||
|
@ -120,45 +117,34 @@ fn build_step(
|
||||||
build_ctx.indentation += 1;
|
build_ctx.indentation += 1;
|
||||||
}
|
}
|
||||||
crate::builder::Step::NewLine => {
|
crate::builder::Step::NewLine => {
|
||||||
add_token(
|
build_ctx.force_wide_success = false;
|
||||||
builder,
|
|
||||||
build_ctx,
|
add_token(builder, rnix::SyntaxKind::TOKEN_WHITESPACE, "\n");
|
||||||
rnix::SyntaxKind::TOKEN_WHITESPACE,
|
|
||||||
"\n",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
crate::builder::Step::Pad => {
|
crate::builder::Step::Pad => {
|
||||||
if build_ctx.indentation > 0 {
|
if build_ctx.indentation > 0 {
|
||||||
add_token(
|
add_token(
|
||||||
builder,
|
builder,
|
||||||
build_ctx,
|
|
||||||
rnix::SyntaxKind::TOKEN_WHITESPACE,
|
rnix::SyntaxKind::TOKEN_WHITESPACE,
|
||||||
&format!("{0:<1$}", "", 2 * build_ctx.indentation),
|
&format!("{0:<1$}", "", 2 * build_ctx.indentation),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
crate::builder::Step::Token(kind, text) => {
|
crate::builder::Step::Token(kind, text) => {
|
||||||
add_token(builder, build_ctx, *kind, text);
|
add_token(builder, *kind, text);
|
||||||
}
|
}
|
||||||
crate::builder::Step::Whitespace => {
|
crate::builder::Step::Whitespace => {
|
||||||
add_token(
|
add_token(builder, rnix::SyntaxKind::TOKEN_WHITESPACE, " ");
|
||||||
builder,
|
|
||||||
build_ctx,
|
|
||||||
rnix::SyntaxKind::TOKEN_WHITESPACE,
|
|
||||||
" ",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_token(
|
fn add_token(
|
||||||
builder: &mut rowan::GreenNodeBuilder,
|
builder: &mut rowan::GreenNodeBuilder,
|
||||||
build_ctx: &mut BuildCtx,
|
|
||||||
kind: rnix::SyntaxKind,
|
kind: rnix::SyntaxKind,
|
||||||
text: &str,
|
text: &str,
|
||||||
) {
|
) {
|
||||||
builder.token(rowan::SyntaxKind(kind as u16), text);
|
builder.token(rowan::SyntaxKind(kind as u16), text);
|
||||||
build_ctx.pos_new.update(text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format(
|
fn format(
|
||||||
|
@ -254,7 +240,7 @@ fn format(
|
||||||
}
|
}
|
||||||
rnix::SyntaxElement::Token(token) => {
|
rnix::SyntaxElement::Token(token) => {
|
||||||
let text = token.text();
|
let text = token.text();
|
||||||
add_token(builder, build_ctx, kind, text);
|
add_token(builder, kind, text);
|
||||||
build_ctx.pos_old.update(text);
|
build_ctx.pos_old.update(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -283,13 +269,7 @@ pub(crate) fn fits_in_single_line(
|
||||||
build_ctx: &crate::builder::BuildCtx,
|
build_ctx: &crate::builder::BuildCtx,
|
||||||
element: rnix::SyntaxElement,
|
element: rnix::SyntaxElement,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let line = build_ctx.pos_new.line;
|
build(element, true, build_ctx.path.clone(), false).is_some()
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn make_isolated_token(
|
pub(crate) fn make_isolated_token(
|
||||||
|
|
|
@ -75,7 +75,7 @@ pub(crate) fn rule(
|
||||||
| rnix::SyntaxKind::NODE_LET_IN
|
| rnix::SyntaxKind::NODE_LET_IN
|
||||||
| rnix::SyntaxKind::NODE_LIST
|
| rnix::SyntaxKind::NODE_LIST
|
||||||
| rnix::SyntaxKind::NODE_STRING
|
| rnix::SyntaxKind::NODE_STRING
|
||||||
) && build_ctx.pos_new.column > 1;
|
) && build_ctx.indentation > 0;
|
||||||
|
|
||||||
if should_indent {
|
if should_indent {
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue