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 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,28 +81,24 @@
#7 #7
} }
( (let
let # 1
# 1 #2
#2 a = 1; # 3
a = 1; # 3 b = 1;
b = 1; c = 1; # 4
c = 1; # 4 #5
#5
#6 #6
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
/**/ /**/
@ @
/**/ /**/
{}: {}:
_ _)
)
] ]

File diff suppressed because it is too large Load diff

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