mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-30 12:07:46 +00:00
feat: better apply indentation
This commit is contained in:
parent
17336602d6
commit
2842bdc239
5 changed files with 75 additions and 59 deletions
|
@ -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();
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
@ -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_))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
[
|
||||
(a
|
||||
b)
|
||||
(
|
||||
(a b)
|
||||
(a b)
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
[
|
||||
(a
|
||||
b)
|
||||
((a b)
|
||||
(a b)
|
||||
(a
|
||||
|
@ -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 =
|
||||
|
@ -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;
|
||||
}
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue