diff --git a/src/builder.rs b/src/builder.rs index aab364f..04ebb09 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -275,7 +275,8 @@ fn format_wider( ) { match element { rnix::SyntaxElement::Node(node) => { - let layout = if fits_in_single_line(build_ctx, node) { + let layout = if fits_in_single_line(build_ctx, node.clone().into()) + { crate::config::Layout::Wide } else { crate::config::Layout::Tall @@ -293,11 +294,11 @@ fn format_wider( pub fn fits_in_single_line( build_ctx: &crate::builder::BuildCtx, - node: &rnix::SyntaxNode, + node: rnix::SyntaxElement, ) -> bool { let maybe_green_node = build( &build_ctx.config.with_layout(crate::config::Layout::Wide), - node.clone().into(), + node, true, build_ctx.path.clone(), ); diff --git a/src/rules/if_else.rs b/src/rules/if_else.rs index 1f56220..1dd15d7 100644 --- a/src/rules/if_else.rs +++ b/src/rules/if_else.rs @@ -10,21 +10,47 @@ pub fn rule( // if/then/else let child = children.get_next().unwrap(); steps.push_back(crate::builder::Step::Format(child.element)); - steps.push_back(crate::builder::Step::Indent); - // /**/ - children.drain_comments(|text| { + if let rnix::SyntaxKind::TOKEN_COMMENT = + children.peek_next().unwrap().element.kind() + { + steps.push_back(crate::builder::Step::Indent); + + // /**/ + children.drain_comments(|text| { + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); + steps.push_back(crate::builder::Step::Comment(text)); + }); + + // expr + let child = children.get_next().unwrap(); steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); - steps.push_back(crate::builder::Step::Comment(text)); - }); + steps.push_back(crate::builder::Step::FormatWider(child.element)); + steps.push_back(crate::builder::Step::Dedent); + } else { + let child = children.get_next().unwrap(); - // expr - let child = children.get_next().unwrap(); - 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::Dedent); + // expr + if crate::builder::fits_in_single_line( + build_ctx, + child.element.clone(), + ) { + steps.push_back(crate::builder::Step::Whitespace); + steps.push_back(crate::builder::Step::FormatWider( + child.element, + )); + } else { + steps.push_back(crate::builder::Step::Indent); + 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::Dedent); + } + } if branch != "else" { steps.push_back(crate::builder::Step::NewLine); diff --git a/tests/cases/if_else/in b/tests/cases/if_else/in index 79123b8..aa1bd21 100644 --- a/tests/cases/if_else/in +++ b/tests/cases/if_else/in @@ -1,5 +1,5 @@ [ - (if a then b else c) + (if ./a then b else c) (if /**/ a /**/ then /**/ b /**/ else /**/ c) (if (if diff --git a/tests/cases/if_else/out b/tests/cases/if_else/out index ba2c44d..0408ec0 100644 --- a/tests/cases/if_else/out +++ b/tests/cases/if_else/out @@ -1,11 +1,8 @@ [ ( - if - a - then - b - else - c + if ./a + then b + else c ) ( if @@ -27,90 +24,63 @@ ( if ( - if - a - then - b - else - c + if a + then b + else c ) then ( - if - a - then - b - else - c + if a + then b + else c ) else ( - if - a - then - b - else - c + if a + then b + else c ) ) then ( if ( - if - a - then - b - else - c + if a + then b + else c ) then ( - if - a - then - b - else - c + if a + then b + else c ) else ( - if - a - then - b - else - c + if a + then b + else c ) ) else ( if ( - if - a - then - b - else - c + if a + then b + else c ) then ( - if - a - then - b - else - c + if a + then b + else c ) else ( - if - a - then - b - else - c + if a + then b + else c ) ) ) @@ -120,90 +90,63 @@ ( if ( - if - a - then - b - else - c + if a + then b + else c ) then ( - if - a - then - b - else - c + if a + then b + else c ) else ( - if - a - then - b - else - c + if a + then b + else c ) ) then ( if ( - if - a - then - b - else - c + if a + then b + else c ) then ( - if - a - then - b - else - c + if a + then b + else c ) else ( - if - a - then - b - else - c + if a + then b + else c ) ) else ( if ( - if - a - then - b - else - c + if a + then b + else c ) then ( - if - a - then - b - else - c + if a + then b + else c ) else ( - if - a - then - b - else - c + if a + then b + else c ) ) ) @@ -213,90 +156,63 @@ ( if ( - if - a - then - b - else - c + if a + then b + else c ) then ( - if - a - then - b - else - c + if a + then b + else c ) else ( - if - a - then - b - else - c + if a + then b + else c ) ) then ( if ( - if - a - then - b - else - c + if a + then b + else c ) then ( - if - a - then - b - else - c + if a + then b + else c ) else ( - if - a - then - b - else - c + if a + then b + else c ) ) else ( if ( - if - a - then - b - else - c + if a + then b + else c ) then ( - if - a - then - b - else - c + if a + then b + else c ) else ( - if - a - then - b - else - c + if a + then b + else c ) ) )