mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-31 12:37:45 +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 mut children = crate::children::Children::new(build_ctx, node);
|
||||||
|
|
||||||
let vertical = children.has_comments()
|
let vertical = build_ctx.vertical
|
||||||
|| children.has_newlines()
|
|| children.has_comments()
|
||||||
|| build_ctx.vertical;
|
|| children.has_newlines();
|
||||||
|
|
||||||
// a
|
// a
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
|
@ -103,7 +103,12 @@ pub(crate) fn rule(
|
||||||
) || (matches!(
|
) || (matches!(
|
||||||
child_expr.kind(),
|
child_expr.kind(),
|
||||||
rnix::SyntaxKind::NODE_APPLY
|
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);
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -28,9 +28,10 @@ pub(crate) fn rule(
|
||||||
| rnix::SyntaxKind::NODE_LAMBDA
|
| rnix::SyntaxKind::NODE_LAMBDA
|
||||||
| rnix::SyntaxKind::NODE_SELECT
|
| rnix::SyntaxKind::NODE_SELECT
|
||||||
| rnix::SyntaxKind::NODE_WITH
|
| rnix::SyntaxKind::NODE_WITH
|
||||||
) && second_through_penultimate_line_are_not_indented(
|
) && !crate::utils::second_through_penultimate_line_are_indented(
|
||||||
build_ctx,
|
build_ctx,
|
||||||
expression.element.clone(),
|
expression.element.clone(),
|
||||||
|
matches!(expression.element.kind(), rnix::SyntaxKind::NODE_LAMBDA),
|
||||||
);
|
);
|
||||||
|
|
||||||
// opener
|
// opener
|
||||||
|
@ -96,31 +97,3 @@ pub(crate) fn rule(
|
||||||
|
|
||||||
steps
|
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 {
|
pub(crate) fn count_newlines(string: &str) -> usize {
|
||||||
string.chars().filter(|c| *c == '\n').count()
|
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)
|
||||||
(a b)
|
(a b)
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
[
|
[
|
||||||
|
(a
|
||||||
|
b)
|
||||||
((a b)
|
((a b)
|
||||||
(a b)
|
(a b)
|
||||||
(a
|
(a
|
||||||
|
@ -57,17 +59,19 @@
|
||||||
asdf = 1;
|
asdf = 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
name2 = function arg {
|
name2 =
|
||||||
asdf = 1;
|
function arg {
|
||||||
}
|
asdf = 1;
|
||||||
argument;
|
}
|
||||||
|
argument;
|
||||||
|
|
||||||
name3 = function arg {
|
name3 =
|
||||||
asdf = 1;
|
function arg {
|
||||||
} {
|
asdf = 1;
|
||||||
qwer = 12345;
|
} {
|
||||||
}
|
qwer = 12345;
|
||||||
argument;
|
}
|
||||||
|
argument;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
name4 =
|
name4 =
|
||||||
|
@ -81,23 +85,26 @@
|
||||||
argument;
|
argument;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
option1 = function arg {asdf = 1;} {
|
option1 =
|
||||||
qwer = 12345;
|
function arg {asdf = 1;} {
|
||||||
qwer2 = 54321;
|
qwer = 12345;
|
||||||
}
|
qwer2 = 54321;
|
||||||
lastArg;
|
}
|
||||||
|
lastArg;
|
||||||
|
|
||||||
option2 = function arg {asdf = 1;} {
|
option2 =
|
||||||
qwer = 12345;
|
function arg {asdf = 1;} {
|
||||||
qwer2 = 54321;
|
qwer = 12345;
|
||||||
}
|
qwer2 = 54321;
|
||||||
lastArg;
|
}
|
||||||
|
lastArg;
|
||||||
|
|
||||||
option3 = function arg {asdf = 1;}
|
option3 =
|
||||||
{
|
function arg {asdf = 1;}
|
||||||
qwer = 12345;
|
{
|
||||||
qwer2 = 54321;
|
qwer = 12345;
|
||||||
}
|
qwer2 = 54321;
|
||||||
lastArg;
|
}
|
||||||
|
lastArg;
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue