1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 21:17:45 +00:00

feat: try parentheses heuristics

This commit is contained in:
Kevin Amado 2022-03-01 21:54:20 -05:00
parent 64509489fb
commit f04306e80e
17 changed files with 1944 additions and 1581 deletions

View file

@ -21,45 +21,15 @@ pub(crate) struct BuildCtx {
pub vertical: bool,
}
impl BuildCtx {
pub fn new(
force_wide: bool,
path: String,
pos_old: crate::position::Position,
vertical: bool,
) -> BuildCtx {
BuildCtx {
force_wide,
force_wide_success: true,
indentation: 0,
path,
pos_old,
vertical,
}
}
}
pub(crate) fn build(
build_ctx: &mut BuildCtx,
element: rnix::SyntaxElement,
force_wide: bool,
path: String,
vertical: bool,
) -> Option<rowan::GreenNode> {
let mut builder = rowan::GreenNodeBuilder::new();
let mut build_ctx = BuildCtx::new(
force_wide,
path,
crate::position::Position::default(),
vertical,
);
build_step(
&mut builder,
&mut build_ctx,
&crate::builder::Step::Format(element),
);
build_step(&mut builder, build_ctx, &crate::builder::Step::Format(element));
if force_wide {
if build_ctx.force_wide {
if build_ctx.force_wide_success { Some(builder.finish()) } else { None }
} else {
Some(builder.finish())
@ -266,10 +236,17 @@ fn format_wider(
}
pub(crate) fn fits_in_single_line(
build_ctx: &crate::builder::BuildCtx,
build_ctx_old: &crate::builder::BuildCtx,
element: rnix::SyntaxElement,
) -> bool {
build(element, true, build_ctx.path.clone(), false).is_some()
let mut build_ctx = crate::builder::BuildCtx {
force_wide: true,
force_wide_success: true,
vertical: false,
..build_ctx_old.clone()
};
build(&mut build_ctx, element).is_some()
}
pub(crate) fn make_isolated_token(

View file

@ -1,12 +1,10 @@
use std::collections::LinkedList;
#[derive(Debug)]
pub(crate) enum Trivia {
Comment(String),
Newlines(usize),
}
#[derive(Debug)]
pub(crate) struct Child {
pub element: rnix::SyntaxElement,

View file

@ -19,7 +19,16 @@ pub fn in_memory(path: String, before: String) -> (Status, String) {
return (Status::Error(errors[0].to_string()), before);
}
let after = crate::builder::build(ast.node().into(), false, path, true)
let mut build_ctx = crate::builder::BuildCtx {
force_wide: false,
force_wide_success: true,
indentation: 0,
path,
pos_old: crate::position::Position::default(),
vertical: true,
};
let after = crate::builder::build(&mut build_ctx, ast.node().into())
.unwrap()
.to_string();

View file

@ -1,13 +1,13 @@
use std::collections::LinkedList;
#[derive(Debug, Default)]
#[derive(Default)]
pub(crate) struct Argument {
pub comments_before: LinkedList<String>,
pub item: Option<rnix::SyntaxElement>,
pub comment_after: Option<String>,
}
#[derive(Debug, Default)]
#[derive(Default)]
pub(crate) struct Pattern {
pub initial_at: Option<rnix::SyntaxElement>,
pub comments_after_initial_at: LinkedList<String>,

View file

@ -1,4 +1,4 @@
#[derive(Clone, Debug)]
#[derive(Clone)]
pub(crate) struct Position {
pub column: usize,
pub line: usize,

View file

@ -11,11 +11,28 @@ pub(crate) fn rule(
let closer = children.next().unwrap();
let vertical = opener.has_inline_comment
|| opener.has_trivialities
|| opener.has_comments
|| expression.has_inline_comment
|| expression.has_trivialities
|| expression.has_comments
|| closer.has_inline_comment
|| closer.has_trivialities;
|| closer.has_comments
|| 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
| rnix::SyntaxKind::NODE_BIN_OP
| rnix::SyntaxKind::NODE_OR_DEFAULT
| rnix::SyntaxKind::NODE_LAMBDA
| rnix::SyntaxKind::NODE_SELECT
| rnix::SyntaxKind::NODE_WITH
) && second_through_penultimate_line_are_not_indented(
build_ctx,
expression.element.clone(),
));
// opener
steps.push_back(crate::builder::Step::Format(opener.element));
@ -77,3 +94,29 @@ pub(crate) fn rule(
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 + 1));
formatted_lines
.iter()
.skip(1)
.rev()
.skip(1)
.any(|line| !line.is_empty() && !line.starts_with(&whitespace))
}

View file

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

View file

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

View file

@ -1,32 +1,45 @@
[
(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
@ -45,5 +58,6 @@
+ 1
+ 1
+ 1
+ 1)
+ 1
)
]

View file

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

View file

@ -1,22 +1,29 @@
[
(if true
(
if true
then {
version = "1.2.3";
}
else {
version = "3.2.1";
})
(if true
}
)
(
if true
then ''
some text
''
else ''
other text
'')
(if ./a
''
)
(
if ./a
then b
else c)
(if
else c
)
(
if
/**/
a
/**/
@ -26,14 +33,18 @@
/**/
else
/**/
c)
(if # test
c
)
(
if # test
a # test
then # test
b # test
else # test
c)
(if # test
c
)
(
if # test
/**/
a # test
/**/
@ -42,8 +53,10 @@
/**/
else # test
/**/
c)
(if
c
)
(
if
if a
then b
else c
@ -52,8 +65,10 @@
then b
else if a
then b
else c)
(if
else c
)
(
if
if a
then b
else c
@ -66,125 +81,206 @@
*/
if a
then b
else c)
(if
(if
(if
(if a
else c
)
(
if
(
if
(
if
(
if a
then b
else c)
else c
)
then
(if a
(
if a
then b
else c)
else c
)
else
(if a
(
if a
then b
else c))
else c
)
)
then
(if
(if a
(
if
(
if a
then b
else c)
else c
)
then
(if a
(
if a
then b
else c)
else c
)
else
(if a
(
if a
then b
else c))
else c
)
)
else
(if
(if a
(
if
(
if a
then b
else c)
else c
)
then
(if a
(
if a
then b
else c)
else c
)
else
(if a
(
if a
then b
else c)))
else c
)
)
)
then
(if
(if
(if a
(
if
(
if
(
if a
then b
else c)
else c
)
then
(if a
(
if a
then b
else c)
else c
)
else
(if a
(
if a
then b
else c))
else c
)
)
then
(if
(if a
(
if
(
if a
then b
else c)
else c
)
then
(if a
(
if a
then b
else c)
else c
)
else
(if a
(
if a
then b
else c))
else c
)
)
else
(if
(if a
(
if
(
if a
then b
else c)
else c
)
then
(if a
(
if a
then b
else c)
else c
)
else
(if a
(
if a
then b
else c)))
else c
)
)
)
else
(if
(if
(if a
(
if
(
if
(
if a
then b
else c)
else c
)
then
(if a
(
if a
then b
else c)
else c
)
else
(if a
(
if a
then b
else c))
else c
)
)
then
(if
(if a
(
if
(
if a
then b
else c)
else c
)
then
(if a
(
if a
then b
else c)
else c
)
else
(if a
(
if a
then b
else c))
else c
)
)
else
(if
(if a
(
if
(
if a
then b
else c)
else c
)
then
(if a
(
if a
then b
else c)
else c
)
else
(if a
(
if a
then b
else c))))
else c
)
)
)
)
]

View file

@ -1,32 +1,43 @@
[
(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
*/
@ -34,18 +45,17 @@
/*
c
*/
d)
(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
d
)
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
)
({
{
pkgs ? import ./.. {},
locationsXml,
}:
null)
null
)
(a: b: c: {}: a: b: c:
a)
@ -53,19 +63,23 @@
# Stuff
})
({pkgs, ...}: let
(
{pkgs, ...}: let
in
pkgs)
pkgs
)
(a: {b, ...}: c: {
# Stuff
})
(a: {
(
a: {
b,
c,
...
}: d: {
# Stuff
})
}
)
]

View file

@ -1,48 +1,68 @@
[
(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,33 +1,45 @@
[
({} @ a: _)
({} @
(
{} @
/**/
a:
_)
({}
_
)
(
{}
/**/
@ a:
_)
({}
_
)
(
{}
/**/
@
/**/
a:
_)
_
)
(a @ {}: _)
(a @
(
a @
/**/
{}:
_)
(a
_
)
(
a
/**/
@ {}:
_)
(a
_
)
(
a
/**/
@
/**/
{}:
_)
_
)
]

View file

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

View file

@ -1,19 +1,26 @@
[
(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
@ -51,5 +58,6 @@
.a
.a
.a
.a)
.a
)
]

View file

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