mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 04:57:44 +00:00
feat: collapse nested lambdas
This commit is contained in:
parent
a5757d2056
commit
9d6a9fc90b
5 changed files with 95 additions and 32 deletions
|
@ -14,7 +14,16 @@ pub fn rule(
|
||||||
|
|
||||||
// a
|
// a
|
||||||
let child = children.get_next().unwrap();
|
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 =
|
if let rnix::SyntaxKind::TOKEN_COMMENT =
|
||||||
children.peek_next().unwrap().element.kind()
|
children.peek_next().unwrap().element.kind()
|
||||||
|
@ -35,29 +44,53 @@ pub fn rule(
|
||||||
match layout {
|
match layout {
|
||||||
crate::config::Layout::Tall => {
|
crate::config::Layout::Tall => {
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
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 => {
|
crate::config::Layout::Wide => {
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
children.drain_comments(|text| {
|
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::NewLine);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
|
steps.push_back(crate::builder::Step::Comment(text));
|
||||||
});
|
});
|
||||||
|
|
||||||
// c
|
// c
|
||||||
|
let child_prev = children.peek_prev().unwrap();
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
match layout {
|
||||||
crate::config::Layout::Tall => {
|
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));
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
}
|
}
|
||||||
crate::config::Layout::Wide => {
|
crate::config::Layout::Wide => {
|
||||||
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,18 +40,17 @@ pub fn rule(
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
child.element,
|
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 => {
|
crate::config::Layout::Wide => {
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
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();
|
children.move_next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,18 +151,11 @@ pub fn rule(
|
||||||
|
|
||||||
// }
|
// }
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if !has_comments_between_curly_b && items_count <= 1 {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
if !has_comments_between_curly_b && items_count <= 1 {
|
} else {
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
} else {
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
|
||||||
|
@ -177,20 +169,19 @@ pub fn rule(
|
||||||
// @ x
|
// @ x
|
||||||
if let Some(child) = children.peek_next() {
|
if let Some(child) = children.peek_next() {
|
||||||
if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() {
|
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 {
|
match layout {
|
||||||
crate::config::Layout::Tall => {
|
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(
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
child.element,
|
child.element,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
crate::config::Layout::Wide => {
|
crate::config::Layout::Wide => {
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
steps
|
steps
|
||||||
.push_back(crate::builder::Step::Format(child.element));
|
.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
[
|
[
|
||||||
|
(a: b: /*c*/ d)
|
||||||
|
({}: b: /*c*/ d)
|
||||||
|
(a: {}: /*c*/ d)
|
||||||
(a : d)
|
(a : d)
|
||||||
(a : /*c*/ d)
|
(a : /*c*/ d)
|
||||||
(a /*b*/ : d)
|
(a /*b*/ : d)
|
||||||
|
@ -7,4 +10,5 @@
|
||||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||||
)
|
)
|
||||||
( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa )
|
( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa )
|
||||||
|
({ pkgs ? import ./.. { }, locationsXml }: null)
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,4 +1,27 @@
|
||||||
[
|
[
|
||||||
|
(
|
||||||
|
a: b:
|
||||||
|
/*
|
||||||
|
c
|
||||||
|
*/
|
||||||
|
d
|
||||||
|
)
|
||||||
|
(
|
||||||
|
{ }:
|
||||||
|
b:
|
||||||
|
/*
|
||||||
|
c
|
||||||
|
*/
|
||||||
|
d
|
||||||
|
)
|
||||||
|
(
|
||||||
|
a:
|
||||||
|
{ }:
|
||||||
|
/*
|
||||||
|
c
|
||||||
|
*/
|
||||||
|
d
|
||||||
|
)
|
||||||
(a: d)
|
(a: d)
|
||||||
(
|
(
|
||||||
a:
|
a:
|
||||||
|
@ -31,4 +54,10 @@
|
||||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
|
||||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||||
)
|
)
|
||||||
|
(
|
||||||
|
{ pkgs ? import ./.. { }
|
||||||
|
, locationsXml
|
||||||
|
}:
|
||||||
|
null
|
||||||
|
)
|
||||||
]
|
]
|
||||||
|
|
|
@ -26,7 +26,13 @@
|
||||||
}:
|
}:
|
||||||
_
|
_
|
||||||
)
|
)
|
||||||
({ b, e, ... }: _)
|
(
|
||||||
|
{ b
|
||||||
|
, e
|
||||||
|
, ...
|
||||||
|
}:
|
||||||
|
_
|
||||||
|
)
|
||||||
(
|
(
|
||||||
{ b
|
{ b
|
||||||
, e
|
, e
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue