From 7e8b768398545217ab365df23902dba4756401c3 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Mon, 14 Feb 2022 17:29:01 -0500 Subject: [PATCH] feat: avoid extra line on multiline patterns --- src/rules/pattern.rs | 23 +++++++++++++---------- tests/cases/pattern/in | 5 +++++ tests/cases/pattern/out | 18 ++++++++++++------ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/rules/pattern.rs b/src/rules/pattern.rs index dd17f35b..16b8a698 100644 --- a/src/rules/pattern.rs +++ b/src/rules/pattern.rs @@ -70,13 +70,15 @@ 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); @@ -84,8 +86,7 @@ pub fn rule( 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); @@ -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); @@ -139,6 +140,7 @@ pub fn rule( } }; children.move_next(); + last_kind = kind; } // , rnix::SyntaxKind::TOKEN_COMMA => { @@ -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 => { diff --git a/tests/cases/pattern/in b/tests/cases/pattern/in index 1c6b7a0e..0fd7c763 100644 --- a/tests/cases/pattern/in +++ b/tests/cases/pattern/in @@ -1,4 +1,9 @@ [ + ({ + self, + gomod2nix, + mach-nix, + }@inp: _) ({}: _) ({ a }: _) ({ /**/ }: _) diff --git a/tests/cases/pattern/out b/tests/cases/pattern/out index dbfb8a9a..5a58a6f3 100644 --- a/tests/cases/pattern/out +++ b/tests/cases/pattern/out @@ -1,4 +1,13 @@ [ + ( + { self + , gomod2nix + , mach-nix + , + } + @ inp: + _ + ) ({ }: _) ({ a }: _) ( @@ -1096,8 +1105,7 @@ ) ( - { - /* + { /* a */ # @@ -1116,8 +1124,7 @@ c */ # - , - /* + , /* d */ # @@ -1136,8 +1143,7 @@ f */ # - , - /* + , /* g */ #