1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 04:57:44 +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 expression = children.next().unwrap();
let closer = children.next().unwrap(); let closer = children.next().unwrap();
let vertical = opener.has_inline_comment let loose = opener.has_inline_comment
|| opener.has_comments || opener.has_comments
|| expression.has_inline_comment || expression.has_inline_comment
|| expression.has_comments || expression.has_comments
|| closer.has_inline_comment || closer.has_inline_comment
|| closer.has_comments || closer.has_comments
|| matches!(expression.element.kind(), rnix::SyntaxKind::NODE_IF_ELSE);
let should_indent = loose
|| matches!( || matches!(
expression.element.kind(),
rnix::SyntaxKind::NODE_IF_ELSE | rnix::SyntaxKind::NODE_LET_IN
)
|| (matches!(
expression.element.kind(), expression.element.kind(),
rnix::SyntaxKind::NODE_APPLY rnix::SyntaxKind::NODE_APPLY
| rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_ASSERT
@ -32,11 +31,11 @@ pub(crate) fn rule(
) && second_through_penultimate_line_are_not_indented( ) && second_through_penultimate_line_are_not_indented(
build_ctx, build_ctx,
expression.element.clone(), expression.element.clone(),
)); );
// opener // opener
steps.push_back(crate::builder::Step::Format(opener.element)); steps.push_back(crate::builder::Step::Format(opener.element));
if vertical { if should_indent {
steps.push_back(crate::builder::Step::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::Comment(text));
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad); 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::NewLine);
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Pad);
} }
@ -62,7 +61,7 @@ pub(crate) fn rule(
} }
// expression // expression
if vertical { if loose {
steps.push_back(crate::builder::Step::FormatWider(expression.element)); steps.push_back(crate::builder::Step::FormatWider(expression.element));
} else { } else {
steps.push_back(crate::builder::Step::Format(expression.element)); steps.push_back(crate::builder::Step::Format(expression.element));
@ -85,8 +84,11 @@ pub(crate) fn rule(
} }
// closer // closer
if vertical { if should_indent {
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
}
if loose {
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Pad);
} }
@ -111,12 +113,14 @@ fn second_through_penultimate_line_are_not_indented(
return false; 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 formatted_lines.iter().skip(1).rev().skip(1).any(|line| {
.iter() !line.is_empty()
.skip(1) && !(line.starts_with(&whitespace)
.rev() || line.starts_with(&lambda)
.skip(1) || line.starts_with(&in_))
.any(|line| !line.is_empty() && !line.starts_with(&whitespace)) })
} }

View file

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

View file

@ -1,29 +1,22 @@
[ [
(assert b; e) (assert b; e)
( (assert b;
assert b;
/* /*
d d
*/ */
e e)
)
(assert b; e) (assert b; e)
( (assert b;
assert b;
/* /*
d d
*/ */
e e)
) (assert
(
assert
/* /*
a a
*/ */
b; e b; e)
) (assert
(
assert
/* /*
a a
*/ */
@ -31,17 +24,13 @@
/* /*
d d
*/ */
e e)
) (assert
(
assert
/* /*
a a
*/ */
b; e b; e)
) (assert
(
assert
/* /*
a a
*/ */
@ -49,8 +38,7 @@
/* /*
d d
*/ */
e e)
)
(assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) (assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
(assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) (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 + 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 + 1
+ 1 + 1)
)
] ]

View file

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

View file

@ -1,43 +1,32 @@
[ [
( (a: b:
a: b:
/* /*
c c
*/ */
d d)
) ({}: b:
(
{}: b:
/* /*
c c
*/ */
d d)
) (a: {}:
(
a: {}:
/* /*
c c
*/ */
d d)
)
(a: d) (a: d)
( (a:
a:
/* /*
c c
*/ */
d d)
) (a
(
a
/* /*
b b
*/ */
: :
d d)
) (a
(
a
/* /*
b b
*/ */
@ -45,17 +34,14 @@
/* /*
c c
*/ */
d d)
)
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
( ({
{
pkgs ? import ./.. {}, pkgs ? import ./.. {},
locationsXml, locationsXml,
}: }:
null null)
)
(a: b: c: {}: a: b: c: (a: b: c: {}: a: b: c:
a) a)
@ -63,23 +49,19 @@
# Stuff # Stuff
}) })
( ({pkgs, ...}: let
{pkgs, ...}: let
in in
pkgs pkgs)
)
(a: {b, ...}: c: { (a: {b, ...}: c: {
# Stuff # Stuff
}) })
( (a: {
a: {
b, b,
c, c,
... ...
}: d: { }: d: {
# Stuff # Stuff
} })
)
] ]

View file

@ -1,68 +1,48 @@
[ [
(a.b or c) (a.b or c)
( (a.b
a.b
or or
/**/ /**/
c c)
) (a.b
(
a.b
/**/ /**/
or c or c)
) (a.b
(
a.b
/**/ /**/
or or
/**/ /**/
c c)
) (a.b
(
a.b
/**/ /**/
or or
/**/ /**/
( (a.b
a.b
/**/ /**/
or or
/**/ /**/
( (a.b
a.b
/**/ /**/
or or
/**/ /**/
c c)))
) (a.b
)
)
(
a.b
/**/ /**/
or or
/**/ /**/
( (a.b
a.b
/**/ /**/
or or
/**/ /**/
( (a.b
a.b
/**/ /**/
or 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 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 a.a # test or a.a # test
or # test or # test
a.a 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 a # test
) )
c c
@ -161,5 +160,4 @@
/* /*
e e
*/ */
) ))
)

View file

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

View file

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

View file

@ -1,21 +1,16 @@
[ [
(with b; c) (with b; c)
( (with b;
with b;
/* /*
b b
*/ */
c c)
) (with
(
with
/* /*
a a
*/ */
b; c b; c)
) (with
(
with
/* /*
a a
*/ */
@ -23,8 +18,7 @@
/* /*
b b
*/ */
c c)
)
(with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) (with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
(with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) (with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc)
{a = with b; 1;} {a = with b; 1;}
@ -47,16 +41,13 @@
# comment # comment
} }
(with a; with b; with c; {a = 1;}) (with a; with b; with c; {a = 1;})
( (with a;
with a;
with b; with b;
with c; { with c; {
a = 1; a = 1;
b = 2; b = 2;
} })
) (with a;
(
with a;
/* /*
comment comment
*/ */
@ -64,8 +55,7 @@
with c; { with c; {
a = 1; a = 1;
b = 2; b = 2;
} })
)
{ {
a = with b; with b; with b; 1; a = with b; with b; with b; 1;
} }