1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 04:57:44 +00:00

feat: avoid extra line on multiline patterns

This commit is contained in:
Kevin Amado 2022-02-14 17:29:01 -05:00
parent bc9abe2826
commit 7e8b768398
No known key found for this signature in database
GPG key ID: FFF341057F503148
3 changed files with 30 additions and 16 deletions

View file

@ -70,13 +70,15 @@ pub fn rule(
let child = children.get_next().unwrap(); let child = children.get_next().unwrap();
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
let mut last_kind = rnix::SyntaxKind::TOKEN_CURLY_B_OPEN;
while let Some(child) = children.peek_next() { while let Some(child) = children.peek_next() {
match child.element.kind() { let kind = child.element.kind();
match kind {
// /**/ // /**/
rnix::SyntaxKind::TOKEN_COMMENT => { rnix::SyntaxKind::TOKEN_COMMENT => {
let prev_kind = children.peek_prev().unwrap().element.kind();
if let rnix::SyntaxKind::TOKEN_COMMA if let rnix::SyntaxKind::TOKEN_COMMA
| rnix::SyntaxKind::TOKEN_CURLY_B_OPEN = prev_kind | rnix::SyntaxKind::TOKEN_CURLY_B_OPEN = last_kind
{ {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
@ -84,8 +86,7 @@ pub fn rule(
if let rnix::SyntaxKind::TOKEN_COMMENT if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_ELLIPSIS | rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::TOKEN_WHITESPACE | rnix::SyntaxKind::NODE_PAT_ENTRY = last_kind
| rnix::SyntaxKind::NODE_PAT_ENTRY = prev_kind
{ {
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
@ -101,24 +102,24 @@ pub fn rule(
| rnix::SyntaxKind::TOKEN_COMMENT | rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_ELLIPSIS | rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::TOKEN_WHITESPACE | rnix::SyntaxKind::TOKEN_WHITESPACE
| rnix::SyntaxKind::NODE_PAT_ENTRY = prev_kind | rnix::SyntaxKind::NODE_PAT_ENTRY = last_kind
{ {
steps.push_back(crate::builder::Step::Dedent); steps.push_back(crate::builder::Step::Dedent);
} }
last_kind = kind;
} }
// item // item
rnix::SyntaxKind::TOKEN_ELLIPSIS rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::NODE_PAT_ENTRY => { | rnix::SyntaxKind::NODE_PAT_ENTRY => {
let prev_kind = children.peek_prev().unwrap().element.kind();
if let rnix::SyntaxKind::TOKEN_COMMA if let rnix::SyntaxKind::TOKEN_COMMA
| rnix::SyntaxKind::TOKEN_CURLY_B_OPEN = prev_kind | rnix::SyntaxKind::TOKEN_CURLY_B_OPEN = last_kind
{ {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
} }
if let rnix::SyntaxKind::TOKEN_COMMENT if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_WHITESPACE = prev_kind | rnix::SyntaxKind::TOKEN_WHITESPACE = last_kind
{ {
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);
@ -139,6 +140,7 @@ pub fn rule(
} }
}; };
children.move_next(); children.move_next();
last_kind = kind;
} }
// , // ,
rnix::SyntaxKind::TOKEN_COMMA => { rnix::SyntaxKind::TOKEN_COMMA => {
@ -151,6 +153,7 @@ pub fn rule(
}; };
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
children.move_next(); children.move_next();
last_kind = kind;
} }
// \n // \n
rnix::SyntaxKind::TOKEN_WHITESPACE => { rnix::SyntaxKind::TOKEN_WHITESPACE => {

View file

@ -1,4 +1,9 @@
[ [
({
self,
gomod2nix,
mach-nix,
}@inp: _)
({}: _) ({}: _)
({ a }: _) ({ a }: _)
({ /**/ }: _) ({ /**/ }: _)

View file

@ -1,4 +1,13 @@
[ [
(
{ self
, gomod2nix
, mach-nix
,
}
@ inp:
_
)
({ }: _) ({ }: _)
({ a }: _) ({ a }: _)
( (
@ -1096,8 +1105,7 @@
) )
( (
{ { /*
/*
a a
*/ */
# #
@ -1116,8 +1124,7 @@
c c
*/ */
# #
, , /*
/*
d d
*/ */
# #
@ -1136,8 +1143,7 @@
f f
*/ */
# #
, , /*
/*
g g
*/ */
# #