Skip to content

Commit

Permalink
Merge pull request #97 from kamadorueda/kamadorueda
Browse files Browse the repository at this point in the history
feat: avoid extra line on multiline patterns
  • Loading branch information
kamadorueda authored Feb 14, 2022
2 parents bc9abe2 + 7e8b768 commit 2c286e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
23 changes: 13 additions & 10 deletions src/rules/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,23 @@ pub fn rule(
let child = children.get_next().unwrap();
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() {
match child.element.kind() {
let kind = child.element.kind();
match kind {
// /**/
rnix::SyntaxKind::TOKEN_COMMENT => {
let prev_kind = children.peek_prev().unwrap().element.kind();
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::Indent);
}

if let rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::TOKEN_WHITESPACE
| rnix::SyntaxKind::NODE_PAT_ENTRY = prev_kind
| rnix::SyntaxKind::NODE_PAT_ENTRY = last_kind
{
steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine);
Expand All @@ -101,24 +102,24 @@ pub fn rule(
| rnix::SyntaxKind::TOKEN_COMMENT
| rnix::SyntaxKind::TOKEN_ELLIPSIS
| 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);
}

last_kind = kind;
}
// item
rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::NODE_PAT_ENTRY => {
let prev_kind = children.peek_prev().unwrap().element.kind();

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

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::Pad);
Expand All @@ -139,6 +140,7 @@ pub fn rule(
}
};
children.move_next();
last_kind = kind;
}
// ,
rnix::SyntaxKind::TOKEN_COMMA => {
Expand All @@ -151,6 +153,7 @@ pub fn rule(
};
steps.push_back(crate::builder::Step::Format(child.element));
children.move_next();
last_kind = kind;
}
// \n
rnix::SyntaxKind::TOKEN_WHITESPACE => {
Expand Down
5 changes: 5 additions & 0 deletions tests/cases/pattern/in
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
({
self,
gomod2nix,
mach-nix,
}@inp: _)
({}: _)
({ a }: _)
({ /**/ }: _)
Expand Down
18 changes: 12 additions & 6 deletions tests/cases/pattern/out
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
(
{ self
, gomod2nix
, mach-nix
,
}
@ inp:
_
)
({ }: _)
({ a }: _)
(
Expand Down Expand Up @@ -1096,8 +1105,7 @@
)

(
{
/*
{ /*
a
*/
#
Expand All @@ -1116,8 +1124,7 @@
c
*/
#
,
/*
, /*
d
*/
#
Expand All @@ -1136,8 +1143,7 @@
f
*/
#
,
/*
, /*
g
*/
#
Expand Down

0 comments on commit 2c286e3

Please sign in to comment.