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

feat: collapse nested lambdas

This commit is contained in:
Kevin Amado 2022-01-31 20:05:01 -05:00
parent a5757d2056
commit 9d6a9fc90b
No known key found for this signature in database
GPG key ID: FFF341057F503148
5 changed files with 95 additions and 32 deletions

View file

@ -14,7 +14,16 @@ pub fn rule(
// a
let child = children.get_next().unwrap();
steps.push_back(crate::builder::Step::Format(child.element));
let is_pattern_type =
child.element.kind() == rnix::SyntaxKind::NODE_PATTERN;
match layout {
crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::FormatWider(child.element));
}
crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Format(child.element));
}
}
if let rnix::SyntaxKind::TOKEN_COMMENT =
children.peek_next().unwrap().element.kind()
@ -35,29 +44,53 @@ pub fn rule(
match layout {
crate::config::Layout::Tall => {
steps.push_back(crate::builder::Step::FormatWider(child.element));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Format(child.element));
steps.push_back(crate::builder::Step::Whitespace);
}
}
// /**/
children.drain_comments(|text| {
steps.push_back(crate::builder::Step::Comment(text));
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
steps.push_back(crate::builder::Step::Comment(text));
});
// c
let child_prev = children.peek_prev().unwrap();
let child = children.get_next().unwrap();
match layout {
crate::config::Layout::Tall => {
if is_pattern_type {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else if let rnix::SyntaxKind::TOKEN_COMMENT =
child_prev.element.kind()
{
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
} else if child.element.kind() == rnix::SyntaxKind::NODE_LAMBDA
&& child
.element
.clone()
.into_node()
.unwrap()
.children_with_tokens()
.next()
.unwrap()
.kind()
== rnix::SyntaxKind::NODE_IDENT
{
steps.push_back(crate::builder::Step::Whitespace);
} else {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
steps.push_back(crate::builder::Step::FormatWider(child.element));
}
crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Format(child.element));
}
}

View file

@ -40,18 +40,17 @@ pub fn rule(
steps.push_back(crate::builder::Step::FormatWider(
child.element,
));
if !has_comments && items_count <= 1 {
steps.push_back(crate::builder::Step::Whitespace);
} else {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
}
crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Format(child.element));
steps.push_back(crate::builder::Step::Whitespace);
}
}
if !has_comments && items_count <= 1 {
steps.push_back(crate::builder::Step::Whitespace);
} else {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
children.move_next();
}
@ -152,18 +151,11 @@ pub fn rule(
// }
let child = children.get_next().unwrap();
match layout {
crate::config::Layout::Tall => {
if !has_comments_between_curly_b && items_count <= 1 {
steps.push_back(crate::builder::Step::Whitespace);
} else {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
}
crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Whitespace);
}
if !has_comments_between_curly_b && items_count <= 1 {
steps.push_back(crate::builder::Step::Whitespace);
} else {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
steps.push_back(crate::builder::Step::Format(child.element));
@ -177,20 +169,19 @@ pub fn rule(
// @ x
if let Some(child) = children.peek_next() {
if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() {
if !has_comments && items_count <= 1 {
steps.push_back(crate::builder::Step::Whitespace);
} else {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
match layout {
crate::config::Layout::Tall => {
if !has_comments && items_count <= 1 {
steps.push_back(crate::builder::Step::Whitespace);
} else {
steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad);
}
steps.push_back(crate::builder::Step::FormatWider(
child.element,
));
}
crate::config::Layout::Wide => {
steps.push_back(crate::builder::Step::Whitespace);
steps
.push_back(crate::builder::Step::Format(child.element));
}

View file

@ -1,4 +1,7 @@
[
(a: b: /*c*/ d)
({}: b: /*c*/ d)
(a: {}: /*c*/ d)
(a : d)
(a : /*c*/ d)
(a /*b*/ : d)
@ -7,4 +10,5 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
)
( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa )
({ pkgs ? import ./.. { }, locationsXml }: null)
]

View file

@ -1,4 +1,27 @@
[
(
a: b:
/*
c
*/
d
)
(
{ }:
b:
/*
c
*/
d
)
(
a:
{ }:
/*
c
*/
d
)
(a: d)
(
a:
@ -31,4 +54,10 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
)
(
{ pkgs ? import ./.. { }
, locationsXml
}:
null
)
]

View file

@ -26,7 +26,13 @@
}:
_
)
({ b, e, ... }: _)
(
{ b
, e
, ...
}:
_
)
(
{ b
, e