From 2a0c9fa66b56fe2121c8a363ffeddb6724500130 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Fri, 18 Feb 2022 18:03:38 -0500 Subject: [PATCH] feat: brace like after pattern entry --- CHANGELOG.md | 12 ++++++++++++ src/rules/pat_entry.rs | 32 +++++++++++++++++++++----------- tests/cases/pattern/in | 10 ++++++++++ tests/cases/pattern/out | 11 +++++++++++ 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb8ca1b..d5ef2b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -91,6 +91,18 @@ Types of changes + _ ``` +- Brace-like elements after a pattern entry now follow the exclamation mark: + + ```diff + - rootPoolProperties ? + - { + - autoexpand = "on"; + - }, + + rootPoolProperties ? { + + autoexpand = "on"; + + }, + ``` + ## [0.2.0] - 2022-02-17 ### Added diff --git a/src/rules/pat_entry.rs b/src/rules/pat_entry.rs index c83c93d..a036ca6 100644 --- a/src/rules/pat_entry.rs +++ b/src/rules/pat_entry.rs @@ -63,23 +63,33 @@ pub fn rule( // expr let child = children.get_next().unwrap(); - let single_line = crate::builder::fits_in_single_line( + let mut dedent = false; + + if comment { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } else if matches!( + child.element.kind(), + rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_IDENT + | rnix::SyntaxKind::NODE_PAREN + | rnix::SyntaxKind::NODE_LAMBDA + | rnix::SyntaxKind::NODE_LET_IN + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_LITERAL + | rnix::SyntaxKind::NODE_STRING, + ) || crate::builder::fits_in_single_line( build_ctx, child.element.clone(), - ); - - if single_line { - if comment { - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } else { - steps.push_back(crate::builder::Step::Whitespace); - } + ) { + steps.push_back(crate::builder::Step::Whitespace); } else { + dedent = true; steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); } + match layout { crate::config::Layout::Tall => { steps.push_back(crate::builder::Step::FormatWider( @@ -90,7 +100,7 @@ pub fn rule( steps.push_back(crate::builder::Step::Format(child.element)); } } - if !single_line { + if dedent { steps.push_back(crate::builder::Step::Dedent); } } diff --git a/tests/cases/pattern/in b/tests/cases/pattern/in index f4daa99..366a0a0 100644 --- a/tests/cases/pattern/in +++ b/tests/cases/pattern/in @@ -9,6 +9,16 @@ gomod2nix, mach-nix, }@inp: _) + ({ + a ? [ + 1 + 2 + 3 + ], + b ? { + # ... + } + }: _) ({}: _) ({ a }: _) ({ /**/ }: _) diff --git a/tests/cases/pattern/out b/tests/cases/pattern/out index 638e0d5..587e840 100644 --- a/tests/cases/pattern/out +++ b/tests/cases/pattern/out @@ -11,6 +11,17 @@ mach-nix, } @ inp: _) + ({ + a ? [ + 1 + 2 + 3 + ], + b ? { + # ... + }, + }: + _) ({}: _) ({a}: _) ({