diff --git a/src/rules/bin_op_and_or_default.rs b/src/rules/bin_op_and_or_default.rs index 8cb1b05..efa2aaa 100644 --- a/src/rules/bin_op_and_or_default.rs +++ b/src/rules/bin_op_and_or_default.rs @@ -1,6 +1,14 @@ pub fn rule( build_ctx: &crate::builder::BuildCtx, node: &rnix::SyntaxNode, +) -> std::collections::LinkedList { + rule_with_configuration(build_ctx, node, "bin_op_and_or_default") +} + +pub fn rule_with_configuration( + build_ctx: &crate::builder::BuildCtx, + node: &rnix::SyntaxNode, + parent_kind: &str, ) -> std::collections::LinkedList { let mut steps = std::collections::LinkedList::new(); @@ -18,17 +26,24 @@ pub fn rule( let child = children.get_next().unwrap(); match layout { crate::config::Layout::Tall => { - match child.element.kind() { - rnix::SyntaxKind::NODE_BIN_OP - | rnix::SyntaxKind::NODE_OR_DEFAULT => { - steps - .push_back(crate::builder::Step::Format(child.element)); - } - _ => { - steps.push_back(crate::builder::Step::FormatWider( - child.element, - )); - } + let kind = child.element.kind(); + + if parent_kind == "bin_op_and_or_default" + && matches!( + kind, + rnix::SyntaxKind::NODE_BIN_OP + | rnix::SyntaxKind::NODE_OR_DEFAULT + ) + { + steps.push_back(crate::builder::Step::Format(child.element)); + } else if parent_kind == "select" + && matches!(kind, rnix::SyntaxKind::NODE_SELECT) + { + steps.push_back(crate::builder::Step::Format(child.element)); + } else { + steps.push_back(crate::builder::Step::FormatWider( + child.element, + )); } steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); @@ -53,7 +68,9 @@ pub fn rule( match layout { crate::config::Layout::Tall => {} crate::config::Layout::Wide => { - steps.push_back(crate::builder::Step::Whitespace); + if parent_kind == "bin_op_and_or_default" { + steps.push_back(crate::builder::Step::Whitespace); + } } } steps.push_back(crate::builder::Step::Format(child.element)); @@ -73,7 +90,7 @@ pub fn rule( if comment { steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); - } else { + } else if parent_kind == "bin_op_and_or_default" { steps.push_back(crate::builder::Step::Whitespace); } diff --git a/src/rules/select.rs b/src/rules/select.rs index b4e925c..d706d67 100644 --- a/src/rules/select.rs +++ b/src/rules/select.rs @@ -2,82 +2,7 @@ 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() - }; - - // expr - let child = children.get_next().unwrap(); - match layout { - crate::config::Layout::Tall => match child.element.kind() { - rnix::SyntaxKind::NODE_SELECT => { - steps.push_back(crate::builder::Step::Format(child.element)); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } - _ => { - 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)); - } - } - - // /**/ - children.drain_comments_and_newlines(|element| match element { - crate::children::DrainCommentOrNewline::Comment(text) => { - steps.push_back(crate::builder::Step::Comment(text)); - steps.push_back(crate::builder::Step::NewLine); - steps.push_back(crate::builder::Step::Pad); - } - crate::children::DrainCommentOrNewline::Newline(_) => {} - }); - - // . - 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); - } - - // 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)); - } - } - - steps + crate::rules::bin_op_and_or_default::rule_with_configuration( + build_ctx, node, "select", + ) }