From cfba9388d100dc5eaaacda4a91c73cdd7d0f34be Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Sat, 12 Feb 2022 15:20:40 -0500 Subject: [PATCH] refactor: reuse code - Assert and With are handled the same, and instead of copy-pasting, lets reuse what we now do very well for the With --- src/builder.rs | 8 ++- src/rules/assert.rs | 81 ----------------------- src/rules/{with.rs => assert_and_with.rs} | 5 +- src/rules/key_value.rs | 44 ++++++------ src/rules/mod.rs | 3 +- tests/cases/assert/out | 8 +-- 6 files changed, 36 insertions(+), 113 deletions(-) delete mode 100644 src/rules/assert.rs rename src/rules/{with.rs => assert_and_with.rs} (95%) diff --git a/src/builder.rs b/src/builder.rs index 639a341..316f4dd 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -169,7 +169,9 @@ fn format( // a b rnix::SyntaxKind::NODE_APPLY => crate::rules::apply::rule, // assert a; b - rnix::SyntaxKind::NODE_ASSERT => crate::rules::assert::rule, + rnix::SyntaxKind::NODE_ASSERT => { + crate::rules::assert_and_with::rule + } // { } rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule, // a $op b @@ -246,7 +248,9 @@ fn format( // !a rnix::SyntaxKind::NODE_UNARY_OP => crate::rules::default, // with a; b - rnix::SyntaxKind::NODE_WITH => crate::rules::with::rule, + rnix::SyntaxKind::NODE_WITH => { + crate::rules::assert_and_with::rule + } kind => { panic!( "Missing rule for {:?} at: {}", diff --git a/src/rules/assert.rs b/src/rules/assert.rs deleted file mode 100644 index 73f13ca..0000000 --- a/src/rules/assert.rs +++ /dev/null @@ -1,81 +0,0 @@ -pub fn rule( - build_ctx: &crate::builder::BuildCtx, - node: &rnix::SyntaxNode, -) -> std::collections::LinkedList { - let mut steps = std::collections::LinkedList::new(); - - let mut children = crate::children::Children::new_with_configuration( - build_ctx, node, true, - ); - - let layout = if children.has_comments() || children.has_newlines() { - &crate::config::Layout::Tall - } else { - build_ctx.config.layout() - }; - - // assert - let child = children.get_next().unwrap(); - steps.push_back(crate::builder::Step::Format(child.element)); - - // /**/ - 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)); - } - crate::children::DrainCommentOrNewline::Newline(_) => {} - }); - - if let rnix::SyntaxKind::TOKEN_COMMENT - | rnix::SyntaxKind::TOKEN_WHITESPACE = - children.peek_prev().unwrap().element.kind() - { - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } else { - steps.push_back(crate::builder::Step::Whitespace); - } - - // expr - let child = children.get_next().unwrap(); - 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)); - } - } - - // ; - let child = children.get_next().unwrap(); - steps.push_back(crate::builder::Step::Format(child.element)); - - // /**/ - 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)); - } - crate::children::DrainCommentOrNewline::Newline(_) => {} - }); - - // expr - let child = children.get_next().unwrap(); - 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)); - } - } - - steps -} diff --git a/src/rules/with.rs b/src/rules/assert_and_with.rs similarity index 95% rename from src/rules/with.rs rename to src/rules/assert_and_with.rs index 16675c0..cc37654 100644 --- a/src/rules/with.rs +++ b/src/rules/assert_and_with.rs @@ -69,7 +69,10 @@ pub fn rule( match layout { crate::config::Layout::Tall => { if comment - || matches!(child.element.kind(), rnix::SyntaxKind::NODE_WITH) + || matches!( + child.element.kind(), + rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH + ) || !matches!( child.element.kind(), rnix::SyntaxKind::NODE_ATTR_SET diff --git a/src/rules/key_value.rs b/src/rules/key_value.rs index a4e8d29..1ed9b19 100644 --- a/src/rules/key_value.rs +++ b/src/rules/key_value.rs @@ -91,27 +91,29 @@ pub fn rule( .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 - )) + || (matches!( + next_kind, + rnix::SyntaxKind::NODE_ASSERT | 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/mod.rs b/src/rules/mod.rs index 15cd897..f124429 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -1,5 +1,5 @@ pub mod apply; -pub mod assert; +pub mod assert_and_with; pub mod attr_set; pub mod bin_op; pub mod dynamic; @@ -18,7 +18,6 @@ pub mod root; pub mod select; pub mod string; pub mod string_interpol; -pub mod with; pub fn default( build_ctx: &crate::builder::BuildCtx, diff --git a/tests/cases/assert/out b/tests/cases/assert/out index 63ac9dc..2366343 100644 --- a/tests/cases/assert/out +++ b/tests/cases/assert/out @@ -12,8 +12,7 @@ /* a */ - b; - c + b; c ) ( assert @@ -27,8 +26,5 @@ c ) (assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) - ( - assert b; - cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc - ) + (assert b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) ]