diff --git a/src/builder.rs b/src/builder.rs index 316f4dd..aa325bc 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -175,7 +175,9 @@ fn format( // { } rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule, // a $op b - rnix::SyntaxKind::NODE_BIN_OP => crate::rules::bin_op::rule, + rnix::SyntaxKind::NODE_BIN_OP => { + crate::rules::bin_op_and_or_default::rule + } // ${a} (interpolation but for NODE_SELECT) rnix::SyntaxKind::NODE_DYNAMIC => crate::rules::dynamic::rule, // implementation detail of rnix-parser @@ -219,7 +221,7 @@ fn format( } // a or b rnix::SyntaxKind::NODE_OR_DEFAULT => { - crate::rules::or_default::rule + crate::rules::bin_op_and_or_default::rule } // ( a ) rnix::SyntaxKind::NODE_PAREN => crate::rules::paren::rule, diff --git a/src/rules/bin_op.rs b/src/rules/bin_op_and_or_default.rs similarity index 93% rename from src/rules/bin_op.rs rename to src/rules/bin_op_and_or_default.rs index 880b2cc..8cb1b05 100644 --- a/src/rules/bin_op.rs +++ b/src/rules/bin_op_and_or_default.rs @@ -19,7 +19,8 @@ pub fn rule( match layout { crate::config::Layout::Tall => { match child.element.kind() { - rnix::SyntaxKind::NODE_BIN_OP => { + rnix::SyntaxKind::NODE_BIN_OP + | rnix::SyntaxKind::NODE_OR_DEFAULT => { steps .push_back(crate::builder::Step::Format(child.element)); } @@ -58,19 +59,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 { diff --git a/src/rules/mod.rs b/src/rules/mod.rs index f124429..8e182aa 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -1,7 +1,7 @@ pub mod apply; pub mod assert_and_with; pub mod attr_set; -pub mod bin_op; +pub mod bin_op_and_or_default; pub mod dynamic; pub mod if_else; pub mod inherit; @@ -9,7 +9,6 @@ pub mod key_value; pub mod lambda; pub mod let_in; pub mod list; -pub mod or_default; pub mod paren; pub mod pat_bind; pub mod pat_entry; diff --git a/src/rules/or_default.rs b/src/rules/or_default.rs deleted file mode 100644 index 84bf7dc..0000000 --- a/src/rules/or_default.rs +++ /dev/null @@ -1,96 +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() - }; - - // expr - let child = children.get_next().unwrap(); - match layout { - crate::config::Layout::Tall => { - match child.element.kind() { - rnix::SyntaxKind::NODE_OR_DEFAULT => { - steps - .push_back(crate::builder::Step::Format(child.element)); - } - _ => { - steps.push_back(crate::builder::Step::FormatWider( - child.element, - )); - } - } - steps.push_back(crate::builder::Step::Indent); - } - 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::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); - } - - // operator - 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)); - steps.push_back(crate::builder::Step::Dedent); - } - crate::config::Layout::Wide => { - steps.push_back(crate::builder::Step::Format(child.element)); - } - } - - steps -} diff --git a/tests/cases/or_default/in b/tests/cases/or_default/in index 2e46e24..bf62a2e 100644 --- a/tests/cases/or_default/in +++ b/tests/cases/or_default/in @@ -8,5 +8,7 @@ ( a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a ) ( a or a or a - or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a) + or + a or + a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a) ] diff --git a/tests/cases/or_default/out b/tests/cases/or_default/out index 518869f..f762f18 100644 --- a/tests/cases/or_default/out +++ b/tests/cases/or_default/out @@ -1,64 +1,67 @@ [ (a or a) ( - a or - /**/ + a + or + /**/ + a + ) + ( + a + /**/ + or a + ) + ( + a + /**/ + or + /**/ + a + ) + ( + a + /**/ + or + /**/ + ( a - ) - ( - a - /**/ - or a - ) - ( - a - /**/ - or - /**/ - a - ) - ( - a /**/ or /**/ ( a - /**/ - or - /**/ - ( - a - /**/ - or - /**/ - a - ) + /**/ + or + /**/ + a ) + ) ) ( a + /**/ + or + /**/ + ( + a /**/ or /**/ ( a - /**/ - or - /**/ - ( - a - /**/ - or - /**/ - a - ) + /**/ + or + /**/ + a ) + ) ) (a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a) ( - a or a - or a - or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a + a + or a + or a + or a + or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a or a ) ]