mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-30 12:07:46 +00:00
feat: improve parentheses handling
This commit is contained in:
parent
3cc1fe8cbb
commit
4c7379772d
12 changed files with 1180 additions and 1424 deletions
|
@ -10,17 +10,16 @@ pub(crate) fn rule(
|
|||
let expression = children.next().unwrap();
|
||||
let closer = children.next().unwrap();
|
||||
|
||||
let vertical = opener.has_inline_comment
|
||||
let loose = opener.has_inline_comment
|
||||
|| opener.has_comments
|
||||
|| expression.has_inline_comment
|
||||
|| expression.has_comments
|
||||
|| closer.has_inline_comment
|
||||
|| closer.has_comments
|
||||
|| matches!(expression.element.kind(), rnix::SyntaxKind::NODE_IF_ELSE);
|
||||
|
||||
let should_indent = loose
|
||||
|| matches!(
|
||||
expression.element.kind(),
|
||||
rnix::SyntaxKind::NODE_IF_ELSE | rnix::SyntaxKind::NODE_LET_IN
|
||||
)
|
||||
|| (matches!(
|
||||
expression.element.kind(),
|
||||
rnix::SyntaxKind::NODE_APPLY
|
||||
| rnix::SyntaxKind::NODE_ASSERT
|
||||
|
@ -32,11 +31,11 @@ pub(crate) fn rule(
|
|||
) && second_through_penultimate_line_are_not_indented(
|
||||
build_ctx,
|
||||
expression.element.clone(),
|
||||
));
|
||||
);
|
||||
|
||||
// opener
|
||||
steps.push_back(crate::builder::Step::Format(opener.element));
|
||||
if vertical {
|
||||
if should_indent {
|
||||
steps.push_back(crate::builder::Step::Indent);
|
||||
}
|
||||
|
||||
|
@ -45,7 +44,7 @@ pub(crate) fn rule(
|
|||
steps.push_back(crate::builder::Step::Comment(text));
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
} else if vertical {
|
||||
} else if loose {
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
|
@ -62,7 +61,7 @@ pub(crate) fn rule(
|
|||
}
|
||||
|
||||
// expression
|
||||
if vertical {
|
||||
if loose {
|
||||
steps.push_back(crate::builder::Step::FormatWider(expression.element));
|
||||
} else {
|
||||
steps.push_back(crate::builder::Step::Format(expression.element));
|
||||
|
@ -85,8 +84,11 @@ pub(crate) fn rule(
|
|||
}
|
||||
|
||||
// closer
|
||||
if vertical {
|
||||
if should_indent {
|
||||
steps.push_back(crate::builder::Step::Dedent);
|
||||
}
|
||||
|
||||
if loose {
|
||||
steps.push_back(crate::builder::Step::NewLine);
|
||||
steps.push_back(crate::builder::Step::Pad);
|
||||
}
|
||||
|
@ -111,12 +113,14 @@ fn second_through_penultimate_line_are_not_indented(
|
|||
return false;
|
||||
}
|
||||
|
||||
let whitespace = format!("{0:<1$}", "", 2 * (build_ctx.indentation + 1));
|
||||
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))
|
||||
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_))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
(a b)
|
||||
(a b)
|
||||
(
|
||||
a
|
||||
(a
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c
|
||||
)
|
||||
c)
|
||||
(
|
||||
/*
|
||||
a
|
||||
|
|
|
@ -1,29 +1,22 @@
|
|||
[
|
||||
(assert b; e)
|
||||
(
|
||||
assert b;
|
||||
(assert b;
|
||||
/*
|
||||
d
|
||||
*/
|
||||
e
|
||||
)
|
||||
e)
|
||||
(assert b; e)
|
||||
(
|
||||
assert b;
|
||||
(assert b;
|
||||
/*
|
||||
d
|
||||
*/
|
||||
e
|
||||
)
|
||||
(
|
||||
assert
|
||||
e)
|
||||
(assert
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b; e
|
||||
)
|
||||
(
|
||||
assert
|
||||
b; e)
|
||||
(assert
|
||||
/*
|
||||
a
|
||||
*/
|
||||
|
@ -31,17 +24,13 @@
|
|||
/*
|
||||
d
|
||||
*/
|
||||
e
|
||||
)
|
||||
(
|
||||
assert
|
||||
e)
|
||||
(assert
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b; e
|
||||
)
|
||||
(
|
||||
assert
|
||||
b; e)
|
||||
(assert
|
||||
/*
|
||||
a
|
||||
*/
|
||||
|
@ -49,8 +38,7 @@
|
|||
/*
|
||||
d
|
||||
*/
|
||||
e
|
||||
)
|
||||
e)
|
||||
(assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
|
||||
(assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
|
||||
]
|
||||
|
|
|
@ -1,45 +1,32 @@
|
|||
[
|
||||
(1 + 1)
|
||||
(
|
||||
1
|
||||
(1
|
||||
+
|
||||
/**/
|
||||
1
|
||||
)
|
||||
(
|
||||
1
|
||||
1)
|
||||
(1
|
||||
/**/
|
||||
+ 1
|
||||
)
|
||||
(
|
||||
1
|
||||
+ 1)
|
||||
(1
|
||||
/**/
|
||||
+
|
||||
/**/
|
||||
1
|
||||
)
|
||||
(
|
||||
1
|
||||
1)
|
||||
(1
|
||||
/**/
|
||||
+
|
||||
/**/
|
||||
(
|
||||
1
|
||||
(1
|
||||
/**/
|
||||
+
|
||||
/**/
|
||||
(
|
||||
1
|
||||
(1
|
||||
/**/
|
||||
+
|
||||
/**/
|
||||
1
|
||||
)
|
||||
)
|
||||
)
|
||||
1)))
|
||||
(1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1)
|
||||
(
|
||||
1
|
||||
(1
|
||||
+ 1
|
||||
+ 1
|
||||
+ 1
|
||||
|
@ -58,6 +45,5 @@
|
|||
+ 1
|
||||
+ 1
|
||||
+ 1
|
||||
+ 1
|
||||
)
|
||||
+ 1)
|
||||
]
|
||||
|
|
|
@ -81,28 +81,24 @@
|
|||
#7
|
||||
}
|
||||
|
||||
(
|
||||
let
|
||||
# 1
|
||||
#2
|
||||
a = 1; # 3
|
||||
b = 1;
|
||||
c = 1; # 4
|
||||
#5
|
||||
(let
|
||||
# 1
|
||||
#2
|
||||
a = 1; # 3
|
||||
b = 1;
|
||||
c = 1; # 4
|
||||
#5
|
||||
|
||||
#6
|
||||
#6
|
||||
|
||||
d = 1;
|
||||
#7
|
||||
in
|
||||
d
|
||||
)
|
||||
d = 1;
|
||||
#7
|
||||
in
|
||||
d)
|
||||
|
||||
(
|
||||
{
|
||||
a, # comment
|
||||
b ? 2, # comment
|
||||
}:
|
||||
_
|
||||
)
|
||||
({
|
||||
a, # comment
|
||||
b ? 2, # comment
|
||||
}:
|
||||
_)
|
||||
]
|
||||
|
|
|
@ -1,43 +1,32 @@
|
|||
[
|
||||
(
|
||||
a: b:
|
||||
(a: b:
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
)
|
||||
(
|
||||
{}: b:
|
||||
d)
|
||||
({}: b:
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
)
|
||||
(
|
||||
a: {}:
|
||||
d)
|
||||
(a: {}:
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
)
|
||||
d)
|
||||
(a: d)
|
||||
(
|
||||
a:
|
||||
(a:
|
||||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
)
|
||||
(
|
||||
a
|
||||
d)
|
||||
(a
|
||||
/*
|
||||
b
|
||||
*/
|
||||
:
|
||||
d
|
||||
)
|
||||
(
|
||||
a
|
||||
d)
|
||||
(a
|
||||
/*
|
||||
b
|
||||
*/
|
||||
|
@ -45,17 +34,14 @@
|
|||
/*
|
||||
c
|
||||
*/
|
||||
d
|
||||
)
|
||||
d)
|
||||
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
|
||||
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
|
||||
(
|
||||
{
|
||||
pkgs ? import ./.. {},
|
||||
locationsXml,
|
||||
}:
|
||||
null
|
||||
)
|
||||
({
|
||||
pkgs ? import ./.. {},
|
||||
locationsXml,
|
||||
}:
|
||||
null)
|
||||
(a: b: c: {}: a: b: c:
|
||||
a)
|
||||
|
||||
|
@ -63,23 +49,19 @@
|
|||
# Stuff
|
||||
})
|
||||
|
||||
(
|
||||
{pkgs, ...}: let
|
||||
in
|
||||
pkgs
|
||||
)
|
||||
({pkgs, ...}: let
|
||||
in
|
||||
pkgs)
|
||||
|
||||
(a: {b, ...}: c: {
|
||||
# Stuff
|
||||
})
|
||||
|
||||
(
|
||||
a: {
|
||||
b,
|
||||
c,
|
||||
...
|
||||
}: d: {
|
||||
# Stuff
|
||||
}
|
||||
)
|
||||
(a: {
|
||||
b,
|
||||
c,
|
||||
...
|
||||
}: d: {
|
||||
# Stuff
|
||||
})
|
||||
]
|
||||
|
|
|
@ -1,68 +1,48 @@
|
|||
[
|
||||
(a.b or c)
|
||||
(
|
||||
a.b
|
||||
(a.b
|
||||
or
|
||||
/**/
|
||||
c
|
||||
)
|
||||
(
|
||||
a.b
|
||||
c)
|
||||
(a.b
|
||||
/**/
|
||||
or c
|
||||
)
|
||||
(
|
||||
a.b
|
||||
or c)
|
||||
(a.b
|
||||
/**/
|
||||
or
|
||||
/**/
|
||||
c
|
||||
)
|
||||
(
|
||||
a.b
|
||||
c)
|
||||
(a.b
|
||||
/**/
|
||||
or
|
||||
/**/
|
||||
(
|
||||
a.b
|
||||
(a.b
|
||||
/**/
|
||||
or
|
||||
/**/
|
||||
(
|
||||
a.b
|
||||
(a.b
|
||||
/**/
|
||||
or
|
||||
/**/
|
||||
c
|
||||
)
|
||||
)
|
||||
)
|
||||
(
|
||||
a.b
|
||||
c)))
|
||||
(a.b
|
||||
/**/
|
||||
or
|
||||
/**/
|
||||
(
|
||||
a.b
|
||||
(a.b
|
||||
/**/
|
||||
or
|
||||
/**/
|
||||
(
|
||||
a.b
|
||||
(a.b
|
||||
/**/
|
||||
or
|
||||
/**/
|
||||
c
|
||||
)
|
||||
)
|
||||
)
|
||||
c)))
|
||||
(a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a)
|
||||
(
|
||||
a.a
|
||||
(a.a
|
||||
or a.a # test
|
||||
or a.a # test
|
||||
or # test
|
||||
a.a
|
||||
or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a
|
||||
)
|
||||
or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a or a.a)
|
||||
]
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
(
|
||||
( # test
|
||||
(( # test
|
||||
a # test
|
||||
)
|
||||
c
|
||||
|
@ -161,5 +160,4 @@
|
|||
/*
|
||||
e
|
||||
*/
|
||||
)
|
||||
)
|
||||
))
|
||||
|
|
|
@ -1,45 +1,33 @@
|
|||
[
|
||||
({} @ a: _)
|
||||
(
|
||||
{} @
|
||||
({} @
|
||||
/**/
|
||||
a:
|
||||
_
|
||||
)
|
||||
(
|
||||
{}
|
||||
_)
|
||||
({}
|
||||
/**/
|
||||
@ a:
|
||||
_
|
||||
)
|
||||
(
|
||||
{}
|
||||
_)
|
||||
({}
|
||||
/**/
|
||||
@
|
||||
/**/
|
||||
a:
|
||||
_
|
||||
)
|
||||
_)
|
||||
|
||||
(a @ {}: _)
|
||||
(
|
||||
a @
|
||||
(a @
|
||||
/**/
|
||||
{}:
|
||||
_
|
||||
)
|
||||
(
|
||||
a
|
||||
_)
|
||||
(a
|
||||
/**/
|
||||
@ {}:
|
||||
_
|
||||
)
|
||||
(
|
||||
a
|
||||
_)
|
||||
(a
|
||||
/**/
|
||||
@
|
||||
/**/
|
||||
{}:
|
||||
_
|
||||
)
|
||||
_)
|
||||
]
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,26 +1,19 @@
|
|||
[
|
||||
(a.a)
|
||||
(
|
||||
a
|
||||
(a
|
||||
.
|
||||
/**/
|
||||
a
|
||||
)
|
||||
(
|
||||
a
|
||||
a)
|
||||
(a
|
||||
/**/
|
||||
.a
|
||||
)
|
||||
(
|
||||
a
|
||||
.a)
|
||||
(a
|
||||
/**/
|
||||
.
|
||||
/**/
|
||||
a
|
||||
)
|
||||
a)
|
||||
(a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a)
|
||||
(
|
||||
a
|
||||
(a
|
||||
.a
|
||||
.a
|
||||
.a
|
||||
|
@ -58,6 +51,5 @@
|
|||
.a
|
||||
.a
|
||||
.a
|
||||
.a
|
||||
)
|
||||
.a)
|
||||
]
|
||||
|
|
|
@ -1,21 +1,16 @@
|
|||
[
|
||||
(with b; c)
|
||||
(
|
||||
with b;
|
||||
(with b;
|
||||
/*
|
||||
b
|
||||
*/
|
||||
c
|
||||
)
|
||||
(
|
||||
with
|
||||
c)
|
||||
(with
|
||||
/*
|
||||
a
|
||||
*/
|
||||
b; c
|
||||
)
|
||||
(
|
||||
with
|
||||
b; c)
|
||||
(with
|
||||
/*
|
||||
a
|
||||
*/
|
||||
|
@ -23,8 +18,7 @@
|
|||
/*
|
||||
b
|
||||
*/
|
||||
c
|
||||
)
|
||||
c)
|
||||
(with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
|
||||
(with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
|
||||
{a = with b; 1;}
|
||||
|
@ -47,16 +41,13 @@
|
|||
# comment
|
||||
}
|
||||
(with a; with b; with c; {a = 1;})
|
||||
(
|
||||
with a;
|
||||
(with a;
|
||||
with b;
|
||||
with c; {
|
||||
a = 1;
|
||||
b = 2;
|
||||
}
|
||||
)
|
||||
(
|
||||
with a;
|
||||
})
|
||||
(with a;
|
||||
/*
|
||||
comment
|
||||
*/
|
||||
|
@ -64,8 +55,7 @@
|
|||
with c; {
|
||||
a = 1;
|
||||
b = 2;
|
||||
}
|
||||
)
|
||||
})
|
||||
{
|
||||
a = with b; with b; with b; 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue