1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-31 12:37:45 +00:00

feat: improve parentheses handling

This commit is contained in:
Kevin Amado 2022-03-02 13:51:22 -05:00
parent 3cc1fe8cbb
commit 4c7379772d
12 changed files with 1180 additions and 1424 deletions

View file

@ -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_))
})
}

View file

@ -1,12 +1,10 @@
(a b)
(a b)
(
a
(a
/*
b
*/
c
)
c)
(
/*
a

View file

@ -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)
]

View file

@ -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)
]

View file

@ -81,8 +81,7 @@
#7
}
(
let
(let
# 1
#2
a = 1; # 3
@ -95,14 +94,11 @@
d = 1;
#7
in
d
)
d)
(
{
({
a, # comment
b ? 2, # comment
}:
_
)
_)
]

View file

@ -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
)
null)
(a: b: c: {}: a: b: c:
a)
@ -63,23 +49,19 @@
# Stuff
})
(
{pkgs, ...}: let
({pkgs, ...}: let
in
pkgs
)
pkgs)
(a: {b, ...}: c: {
# Stuff
})
(
a: {
(a: {
b,
c,
...
}: d: {
# Stuff
}
)
})
]

View file

@ -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)
]

View file

@ -1,5 +1,4 @@
(
( # test
(( # test
a # test
)
c
@ -161,5 +160,4 @@
/*
e
*/
)
)
))

View file

@ -1,45 +1,33 @@
[
({} @ a: _)
(
{} @
({} @
/**/
a:
_
)
(
{}
_)
({}
/**/
@ a:
_
)
(
{}
_)
({}
/**/
@
/**/
a:
_
)
_)
(a @ {}: _)
(
a @
(a @
/**/
{}:
_
)
(
a
_)
(a
/**/
@ {}:
_
)
(
a
_)
(a
/**/
@
/**/
{}:
_
)
_)
]

View file

@ -9,24 +9,19 @@
foo,
bar, # Some comment
}: {})
(
a @ {
(a @ {
self,
gomod2nix,
mach-nix,
}:
_
)
(
{
_)
({
self,
gomod2nix,
mach-nix,
} @ inp:
_
)
(
{
_)
({
a ? [
1
2
@ -36,50 +31,38 @@
# ...
},
}:
_
)
_)
({}: _)
({a}: _)
(
{
({
/**/
}:
_
)
_)
({...}: _)
(
{
({
...
/**/
}:
_
)
(
{
_)
({
/**/
...
}:
_
)
(
{
_)
({
/**/
...
/**/
}:
_
)
_)
(
{
({
b,
e,
...
}:
_
)
(
{
_)
({
b,
e,
...
@ -87,10 +70,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
e,
/*
@ -98,10 +79,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
e,
/*
@ -112,10 +91,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
e,
/*
@ -123,10 +100,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
e,
/*
@ -137,10 +112,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
e,
/*
@ -151,10 +124,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
e,
/*
@ -168,10 +139,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
d
@ -179,10 +148,8 @@
e,
...
}:
_
)
(
{
_)
({
b,
/*
d
@ -193,10 +160,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
d
@ -207,10 +172,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
/*
d
@ -224,10 +187,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
d
@ -238,10 +199,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
/*
d
@ -255,10 +214,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
d
@ -272,10 +229,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
/*
d
@ -292,10 +247,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
c
@ -303,10 +256,8 @@
e,
...
}:
_
)
(
{
_)
({
b,
/*
c
@ -317,10 +268,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
c
@ -331,10 +280,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
/*
c
@ -348,10 +295,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
c
@ -362,10 +307,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
/*
c
@ -379,10 +322,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
c
@ -396,10 +337,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
/*
c
@ -416,10 +355,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
c
@ -430,10 +367,8 @@
e,
...
}:
_
)
(
{
_)
({
b,
/*
c
@ -447,10 +382,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
c
@ -464,10 +397,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
/*
c
@ -484,10 +415,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
c
@ -501,10 +430,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
/*
c
@ -521,10 +448,8 @@
h
*/
}:
_
)
(
{
_)
({
b,
/*
c
@ -541,10 +466,8 @@
*/
...
}:
_
)
(
{
_)
({
b,
/*
c
@ -564,10 +487,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -575,10 +496,8 @@
e,
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -589,10 +508,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -603,10 +520,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -620,10 +535,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -634,10 +547,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -651,10 +562,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -668,10 +577,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -688,10 +595,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -702,10 +607,8 @@
e,
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -719,10 +622,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -736,10 +637,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -756,10 +655,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -773,10 +670,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -793,10 +688,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -813,10 +706,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -836,10 +727,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -850,10 +739,8 @@
e,
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -867,10 +754,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -884,10 +769,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -904,10 +787,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -921,10 +802,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -941,10 +820,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -961,10 +838,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -984,10 +859,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -1001,10 +874,8 @@
e,
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -1021,10 +892,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -1041,10 +910,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -1064,10 +931,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -1084,10 +949,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -1107,10 +970,8 @@
h
*/
}:
_
)
(
{
_)
({
/*
a
*/
@ -1130,10 +991,8 @@
*/
...
}:
_
)
(
{
_)
({
/*
a
*/
@ -1156,12 +1015,10 @@
h
*/
}:
_
)
_)
({a ? null}: _)
(
{
({
/*
a
*/
@ -1200,11 +1057,9 @@
h
*/
}:
_
)
_)
(
{
({
/*
a
*/
@ -1262,6 +1117,5 @@
j
*/
#
_
)
_)
]

View file

@ -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)
]

View file

@ -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;
}