Skip to content

Commit

Permalink
feat: better apply indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kamadorueda committed Mar 2, 2022
1 parent 1733660 commit 2842bdc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 59 deletions.
13 changes: 9 additions & 4 deletions src/alejandra_engine/src/rules/key_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub(crate) fn rule(

let mut children = crate::children::Children::new(build_ctx, node);

let vertical = children.has_comments()
|| children.has_newlines()
|| build_ctx.vertical;
let vertical = build_ctx.vertical
|| children.has_comments()
|| children.has_newlines();

// a
let child = children.get_next().unwrap();
Expand Down Expand Up @@ -103,7 +103,12 @@ pub(crate) fn rule(
) || (matches!(
child_expr.kind(),
rnix::SyntaxKind::NODE_APPLY
) && !newlines)
)
&& crate::utils::second_through_penultimate_line_are_indented(
build_ctx,
child_expr.clone(),
false,
))
{
steps.push_back(crate::builder::Step::Whitespace);
} else {
Expand Down
31 changes: 2 additions & 29 deletions src/alejandra_engine/src/rules/paren.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ pub(crate) fn rule(
| rnix::SyntaxKind::NODE_LAMBDA
| rnix::SyntaxKind::NODE_SELECT
| rnix::SyntaxKind::NODE_WITH
) && second_through_penultimate_line_are_not_indented(
) && !crate::utils::second_through_penultimate_line_are_indented(
build_ctx,
expression.element.clone(),
matches!(expression.element.kind(), rnix::SyntaxKind::NODE_LAMBDA),
);

// opener
Expand Down Expand Up @@ -96,31 +97,3 @@ pub(crate) fn rule(

steps
}

fn second_through_penultimate_line_are_not_indented(
build_ctx: &crate::builder::BuildCtx,
element: rnix::SyntaxElement,
) -> bool {
let mut build_ctx =
crate::builder::BuildCtx { force_wide: false, ..build_ctx.clone() };

let formatted =
crate::builder::build(&mut build_ctx, element).unwrap().to_string();

let formatted_lines: Vec<&str> = formatted.split('\n').collect();

if formatted_lines.len() <= 2 {
return false;
}

let whitespace = format!("{0:<1$} ", "", 2 * build_ctx.indentation);
let lambda = format!("{0:<1$}}}:", "", 2 * build_ctx.indentation);
let in_ = format!("{0:<1$}in", "", 2 * build_ctx.indentation);

formatted_lines.iter().skip(1).rev().skip(1).any(|line| {
!line.is_empty()
&& !(line.starts_with(&whitespace)
|| line.starts_with(&lambda)
|| line.starts_with(&in_))
})
}
29 changes: 29 additions & 0 deletions src/alejandra_engine/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,32 @@ pub(crate) fn has_newlines(string: &str) -> bool {
pub(crate) fn count_newlines(string: &str) -> usize {
string.chars().filter(|c| *c == '\n').count()
}

pub(crate) fn second_through_penultimate_line_are_indented(
build_ctx: &crate::builder::BuildCtx,
element: rnix::SyntaxElement,
if_leq_than_two_lines: bool,
) -> bool {
let mut build_ctx =
crate::builder::BuildCtx { force_wide: false, ..build_ctx.clone() };

let formatted =
crate::builder::build(&mut build_ctx, element).unwrap().to_string();

let formatted_lines: Vec<&str> = formatted.split('\n').collect();

if formatted_lines.len() <= 2 {
return if_leq_than_two_lines;
}

let whitespace = format!("{0:<1$} ", "", 2 * build_ctx.indentation);
let lambda = format!("{0:<1$}}}:", "", 2 * build_ctx.indentation);
let in_ = format!("{0:<1$}in", "", 2 * build_ctx.indentation);

formatted_lines.iter().skip(1).rev().skip(1).all(|line| {
line.is_empty()
|| line.starts_with(&lambda)
|| line.starts_with(&in_)
|| line.starts_with(&whitespace)
})
}
2 changes: 2 additions & 0 deletions src/alejandra_engine/tests/cases/apply/in
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[
(a
b)
(
(a b)
(a b)
Expand Down
59 changes: 33 additions & 26 deletions src/alejandra_engine/tests/cases/apply/out
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[
(a
b)
((a b)
(a b)
(a
Expand Down Expand Up @@ -57,17 +59,19 @@
asdf = 1;
};

name2 = function arg {
asdf = 1;
}
argument;
name2 =
function arg {
asdf = 1;
}
argument;

name3 = function arg {
asdf = 1;
} {
qwer = 12345;
}
argument;
name3 =
function arg {
asdf = 1;
} {
qwer = 12345;
}
argument;
}
{
name4 =
Expand All @@ -81,23 +85,26 @@
argument;
}
{
option1 = function arg {asdf = 1;} {
qwer = 12345;
qwer2 = 54321;
}
lastArg;
option1 =
function arg {asdf = 1;} {
qwer = 12345;
qwer2 = 54321;
}
lastArg;

option2 = function arg {asdf = 1;} {
qwer = 12345;
qwer2 = 54321;
}
lastArg;
option2 =
function arg {asdf = 1;} {
qwer = 12345;
qwer2 = 54321;
}
lastArg;

option3 = function arg {asdf = 1;}
{
qwer = 12345;
qwer2 = 54321;
}
lastArg;
option3 =
function arg {asdf = 1;}
{
qwer = 12345;
qwer2 = 54321;
}
lastArg;
}
]

0 comments on commit 2842bdc

Please # to comment.