From 70042d5bbd7082915708849caffc591dd05e2d42 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Sat, 12 Feb 2022 13:56:06 -0500 Subject: [PATCH] feat: improve with - There were some interactions where the user would feel that indentation levels were missing/excessive --- src/rules/key_value.rs | 62 ++++++++++++++++++++++++++++-------------- src/rules/with.rs | 40 +++++++++++++++++++-------- tests/cases/with/in | 8 ++++++ tests/cases/with/out | 19 +++++++++---- 4 files changed, 92 insertions(+), 37 deletions(-) diff --git a/src/rules/key_value.rs b/src/rules/key_value.rs index 7bff1ed..a4e8d29 100644 --- a/src/rules/key_value.rs +++ b/src/rules/key_value.rs @@ -52,26 +52,15 @@ pub fn rule( let next = children.peek_next().unwrap(); let next_kind = next.element.kind(); - if matches!( - next_kind, - rnix::SyntaxKind::NODE_ATTR_SET - | rnix::SyntaxKind::NODE_PAREN - | rnix::SyntaxKind::NODE_WITH - | rnix::SyntaxKind::NODE_LET_IN - | rnix::SyntaxKind::NODE_LIST - | rnix::SyntaxKind::NODE_STRING - ) || (matches!(next_kind, rnix::SyntaxKind::NODE_LAMBDA) - && !matches!( - next.element - .clone() - .into_node() - .unwrap() - .children() - .next() - .unwrap() - .kind(), - rnix::SyntaxKind::NODE_PATTERN - )) + if false + || matches!( + next_kind, + rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_PAREN + | rnix::SyntaxKind::NODE_LET_IN + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_STRING + ) || (matches!(next_kind, rnix::SyntaxKind::NODE_APPLY) && matches!( next.element @@ -90,6 +79,39 @@ pub fn rule( | rnix::SyntaxKind::NODE_LIST | rnix::SyntaxKind::NODE_STRING )) + || (matches!(next_kind, rnix::SyntaxKind::NODE_LAMBDA) + && !matches!( + next.element + .clone() + .into_node() + .unwrap() + .children() + .next() + .unwrap() + .kind(), + rnix::SyntaxKind::NODE_PATTERN + )) + || (matches!(next_kind, rnix::SyntaxKind::NODE_WITH) + && matches!( + next.element + .clone() + .into_node() + .unwrap() + .children() + .collect::>() + .iter() + .rev() + .next() + .unwrap() + .kind(), + rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_IDENT + | rnix::SyntaxKind::NODE_PAREN + | rnix::SyntaxKind::NODE_LET_IN + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_LITERAL + | rnix::SyntaxKind::NODE_STRING + )) { steps.push_back(crate::builder::Step::Whitespace); } else { diff --git a/src/rules/with.rs b/src/rules/with.rs index e5baab2..16675c0 100644 --- a/src/rules/with.rs +++ b/src/rules/with.rs @@ -19,19 +19,18 @@ pub fn rule( steps.push_back(crate::builder::Step::Format(child.element)); // /**/ + let mut comment = false; children.drain_comments_and_newlines(|element| match element { crate::children::DrainCommentOrNewline::Comment(text) => { steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Comment(text)); + comment = true; } crate::children::DrainCommentOrNewline::Newline(_) => {} }); - if let rnix::SyntaxKind::TOKEN_COMMENT - | rnix::SyntaxKind::TOKEN_WHITESPACE = - children.peek_prev().unwrap().element.kind() - { + if comment { steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); } else { @@ -40,7 +39,6 @@ pub fn rule( // expr let child = children.get_next().unwrap(); - match layout { crate::config::Layout::Tall => { steps.push_back(crate::builder::Step::FormatWider(child.element)); @@ -68,17 +66,35 @@ pub fn rule( // expr let child = children.get_next().unwrap(); - if comment || matches!(child.element.kind(), rnix::SyntaxKind::NODE_WITH) { - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } else { - steps.push_back(crate::builder::Step::Whitespace); - } match layout { crate::config::Layout::Tall => { - steps.push_back(crate::builder::Step::FormatWider(child.element)); + if comment + || matches!(child.element.kind(), rnix::SyntaxKind::NODE_WITH) + || !matches!( + child.element.kind(), + rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_IDENT + | rnix::SyntaxKind::NODE_PAREN + | rnix::SyntaxKind::NODE_LET_IN + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_LITERAL + | rnix::SyntaxKind::NODE_STRING + ) + { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + steps.push_back(crate::builder::Step::FormatWider( + child.element, + )); + } else { + steps.push_back(crate::builder::Step::Whitespace); + steps.push_back(crate::builder::Step::FormatWider( + child.element, + )); + } } crate::config::Layout::Wide => { + steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Format(child.element)); } } diff --git a/tests/cases/with/in b/tests/cases/with/in index 5fbfb10..279754d 100644 --- a/tests/cases/with/in +++ b/tests/cases/with/in @@ -21,4 +21,12 @@ (with a; with b; with c; {a=1;}) (with a; with b; with c; {a=1;b=2;}) (with a; /* comment */ with b; with c; {a=1;b=2;}) + { a = with b;with b;with b; + 1; + } + {binPath = with pkgs; + makeBinPath ( + [ + rsync + util-linux]);} ] diff --git a/tests/cases/with/out b/tests/cases/with/out index 8164148..bd5a137 100644 --- a/tests/cases/with/out +++ b/tests/cases/with/out @@ -46,11 +46,7 @@ a = with b; 1; # comment } - ( - with a; - with b; - with c; { a = 1; } - ) + (with a; with b; with c; { a = 1; }) ( with a; with b; @@ -70,4 +66,17 @@ b = 2; } ) + { + a = with b; with b; with b; 1; + } + { + binPath = + with pkgs; + makeBinPath ( + [ + rsync + util-linux + ] + ); + } ]