From 77db3703b1d53cef390bcd5187a6e1c4a4bffea7 Mon Sep 17 00:00:00 2001 From: Tom Bereknyei Date: Thu, 10 Feb 2022 20:16:08 -0500 Subject: [PATCH] with: inline follow on expressions --- src/rules/key_value.rs | 2 ++ src/rules/with.rs | 24 +++++++++++++++++++--- tests/cases/idioms/in | 3 +++ tests/cases/idioms/out | 31 +++++++++++++++++------------ tests/cases/with/in | 15 ++++++++++++++ tests/cases/with/out | 45 +++++++++++++++++++++++++++++++++++++++--- 6 files changed, 101 insertions(+), 19 deletions(-) diff --git a/src/rules/key_value.rs b/src/rules/key_value.rs index c76e38e..c36561e 100644 --- a/src/rules/key_value.rs +++ b/src/rules/key_value.rs @@ -56,6 +56,7 @@ pub fn rule( 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 @@ -87,6 +88,7 @@ pub fn rule( rnix::SyntaxKind::NODE_ATTR_SET | rnix::SyntaxKind::NODE_PAREN | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_WITH | rnix::SyntaxKind::NODE_STRING )) { diff --git a/src/rules/with.rs b/src/rules/with.rs index 6a13f60..014b0b8 100644 --- a/src/rules/with.rs +++ b/src/rules/with.rs @@ -40,6 +40,19 @@ pub fn rule( // expr let child = children.get_next().unwrap(); + if let rnix::SyntaxKind::TOKEN_COMMENT = + children.peek_prev().unwrap().element.kind() + { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } else { + if let rnix::SyntaxKind::NODE_WITH = child.element.kind() { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } else { + } + } + match layout { crate::config::Layout::Tall => { steps.push_back(crate::builder::Step::FormatWider(child.element)); @@ -54,25 +67,30 @@ pub fn rule( steps.push_back(crate::builder::Step::Format(child.element)); // /**/ + let mut comment : bool = 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(_) => {} }); // 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::NewLine); - steps.push_back(crate::builder::Step::Pad); 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/idioms/in b/tests/cases/idioms/in index 61e7956..f8158f6 100644 --- a/tests/cases/idioms/in +++ b/tests/cases/idioms/in @@ -12,4 +12,7 @@ description = "something"; }; } + { meta = with lib; + # comment + { a=1; b=2; c=3; };} ] diff --git a/tests/cases/idioms/out b/tests/cases/idioms/out index b793e13..187a55c 100644 --- a/tests/cases/idioms/out +++ b/tests/cases/idioms/out @@ -1,12 +1,10 @@ [ { - meta = - with lib; - { - a = 1; - b = 2; - c = 3; - }; + meta = with lib; { + a = 1; + b = 2; + c = 3; + }; } { stdenv , lib @@ -19,11 +17,18 @@ src = fetchFrom { url = "example/${version}"; }; - meta = - with lib; - { - maintainers = with maintainers; [ someone ]; - description = "something"; - }; + meta = with lib; { + maintainers = with maintainers; [ someone ]; + description = "something"; + }; + } + { + meta = with lib; + # comment + { + a = 1; + b = 2; + c = 3; + }; } ] diff --git a/tests/cases/with/in b/tests/cases/with/in index 922aa33..5fbfb10 100644 --- a/tests/cases/with/in +++ b/tests/cases/with/in @@ -6,4 +6,19 @@ ( with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc ) ( with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc ) + { a = with b; 1;} + { a = with b; 1 + 1;} + { a = with b; {c=1;};} + { a = with b; {c=1; d=2; e=3;};} + { a = with b; + # comment + 1; + } + { a = with b; + 1; + # comment + } + (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;}) ] diff --git a/tests/cases/with/out b/tests/cases/with/out index 6c3a2b2..8164148 100644 --- a/tests/cases/with/out +++ b/tests/cases/with/out @@ -12,8 +12,7 @@ /* a */ - b; - c + b; c ) ( with @@ -27,8 +26,48 @@ c ) (with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) + (with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) + { a = with b; 1; } + { a = with b; 1 + 1; } + { a = with b; { c = 1; }; } + { + a = with b; { + c = 1; + d = 2; + e = 3; + }; + } + { + a = with b; + # comment + 1; + } + { + a = with b; 1; + # comment + } ( + with a; with b; - cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc + 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; + } ) ]