From 9d6a9fc90b90747743bfd44a3036d9f7922c2c14 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Mon, 31 Jan 2022 20:05:01 -0500 Subject: [PATCH] feat: collapse nested lambdas --- src/rules/lambda.rs | 43 ++++++++++++++++++++++++++++++++++++----- src/rules/pattern.rs | 43 ++++++++++++++++------------------------- tests/cases/lambda/in | 4 ++++ tests/cases/lambda/out | 29 +++++++++++++++++++++++++++ tests/cases/pattern/out | 8 +++++++- 5 files changed, 95 insertions(+), 32 deletions(-) diff --git a/src/rules/lambda.rs b/src/rules/lambda.rs index 0f782ff..593cd71 100644 --- a/src/rules/lambda.rs +++ b/src/rules/lambda.rs @@ -14,7 +14,16 @@ pub fn rule( // a let child = children.get_next().unwrap(); - steps.push_back(crate::builder::Step::Format(child.element)); + let is_pattern_type = + child.element.kind() == rnix::SyntaxKind::NODE_PATTERN; + match layout { + crate::config::Layout::Tall => { + steps.push_back(crate::builder::Step::FormatWider(child.element)); + } + crate::config::Layout::Wide => { + steps.push_back(crate::builder::Step::Format(child.element)); + } + } if let rnix::SyntaxKind::TOKEN_COMMENT = children.peek_next().unwrap().element.kind() @@ -35,29 +44,53 @@ pub fn rule( match layout { crate::config::Layout::Tall => { steps.push_back(crate::builder::Step::FormatWider(child.element)); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); } crate::config::Layout::Wide => { steps.push_back(crate::builder::Step::Format(child.element)); - steps.push_back(crate::builder::Step::Whitespace); } } // /**/ children.drain_comments(|text| { - steps.push_back(crate::builder::Step::Comment(text)); steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); + steps.push_back(crate::builder::Step::Comment(text)); }); // c + let child_prev = children.peek_prev().unwrap(); let child = children.get_next().unwrap(); match layout { crate::config::Layout::Tall => { + if is_pattern_type { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } else if let rnix::SyntaxKind::TOKEN_COMMENT = + child_prev.element.kind() + { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } else if child.element.kind() == rnix::SyntaxKind::NODE_LAMBDA + && child + .element + .clone() + .into_node() + .unwrap() + .children_with_tokens() + .next() + .unwrap() + .kind() + == rnix::SyntaxKind::NODE_IDENT + { + steps.push_back(crate::builder::Step::Whitespace); + } else { + 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/src/rules/pattern.rs b/src/rules/pattern.rs index e2876a5..debf03b 100644 --- a/src/rules/pattern.rs +++ b/src/rules/pattern.rs @@ -40,18 +40,17 @@ pub fn rule( steps.push_back(crate::builder::Step::FormatWider( child.element, )); - if !has_comments && items_count <= 1 { - steps.push_back(crate::builder::Step::Whitespace); - } else { - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } } crate::config::Layout::Wide => { steps.push_back(crate::builder::Step::Format(child.element)); - steps.push_back(crate::builder::Step::Whitespace); } } + if !has_comments && items_count <= 1 { + steps.push_back(crate::builder::Step::Whitespace); + } else { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } children.move_next(); } @@ -152,18 +151,11 @@ pub fn rule( // } let child = children.get_next().unwrap(); - match layout { - crate::config::Layout::Tall => { - if !has_comments_between_curly_b && items_count <= 1 { - steps.push_back(crate::builder::Step::Whitespace); - } else { - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } - } - crate::config::Layout::Wide => { - steps.push_back(crate::builder::Step::Whitespace); - } + if !has_comments_between_curly_b && items_count <= 1 { + steps.push_back(crate::builder::Step::Whitespace); + } else { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); } steps.push_back(crate::builder::Step::Format(child.element)); @@ -177,20 +169,19 @@ pub fn rule( // @ x if let Some(child) = children.peek_next() { if let rnix::SyntaxKind::NODE_PAT_BIND = child.element.kind() { + if !has_comments && items_count <= 1 { + steps.push_back(crate::builder::Step::Whitespace); + } else { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + } match layout { crate::config::Layout::Tall => { - if !has_comments && items_count <= 1 { - steps.push_back(crate::builder::Step::Whitespace); - } else { - 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/lambda/in b/tests/cases/lambda/in index b78cf36..c467519 100644 --- a/tests/cases/lambda/in +++ b/tests/cases/lambda/in @@ -1,4 +1,7 @@ [ + (a: b: /*c*/ d) + ({}: b: /*c*/ d) + (a: {}: /*c*/ d) (a : d) (a : /*c*/ d) (a /*b*/ : d) @@ -7,4 +10,5 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ) ( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ) + ({ pkgs ? import ./.. { }, locationsXml }: null) ] diff --git a/tests/cases/lambda/out b/tests/cases/lambda/out index 275f06e..b9aaac5 100644 --- a/tests/cases/lambda/out +++ b/tests/cases/lambda/out @@ -1,4 +1,27 @@ [ + ( + a: b: + /* + c + */ + d + ) + ( + { }: + b: + /* + c + */ + d + ) + ( + a: + { }: + /* + c + */ + d + ) (a: d) ( a: @@ -31,4 +54,10 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ) + ( + { pkgs ? import ./.. { } + , locationsXml + }: + null + ) ] diff --git a/tests/cases/pattern/out b/tests/cases/pattern/out index 2f081af..a3da858 100644 --- a/tests/cases/pattern/out +++ b/tests/cases/pattern/out @@ -26,7 +26,13 @@ }: _ ) - ({ b, e, ... }: _) + ( + { b + , e + , ... + }: + _ + ) ( { b , e