diff --git a/src/rules/let_in.rs b/src/rules/let_in.rs index a9a5e07..ae9a9e4 100644 --- a/src/rules/let_in.rs +++ b/src/rules/let_in.rs @@ -80,9 +80,6 @@ pub fn rule( } } - // in - let child = children.get_next().unwrap(); - let indent = build_ctx.pos_new.column > 1; match layout { crate::config::Layout::Tall => { steps.push_back(crate::builder::Step::Dedent); @@ -93,40 +90,69 @@ pub fn rule( steps.push_back(crate::builder::Step::Whitespace); } } - steps.push_back(crate::builder::Step::Format(child.element)); + + // in + let child_in = children.get_next().unwrap(); + let top_level = build_ctx.pos_new.column <= 1; + + // /**/ + let mut child_comments = std::collections::LinkedList::new(); + children.drain_comments_and_newlines(|element| match element { + crate::children::DrainCommentOrNewline::Comment(text) => { + child_comments.push_back(crate::builder::Step::Comment(text)) + } + crate::children::DrainCommentOrNewline::Newline(_) => {} + }); + + // expr + let child_expr = children.get_next().unwrap(); + + // in + steps.push_back(crate::builder::Step::Format(child_in.element)); match layout { crate::config::Layout::Tall => { - if indent { - steps.push_back(crate::builder::Step::Indent); + if child_comments.is_empty() + && matches!( + child_expr.element.kind(), + rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_LET_IN + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_PAREN + | rnix::SyntaxKind::NODE_STRING + ) + { + steps.push_back(crate::builder::Step::Whitespace); + } else { + if !top_level { + steps.push_back(crate::builder::Step::Indent); + } + steps.push_back(crate::builder::Step::NewLine); + steps.push_back(crate::builder::Step::Pad); } } crate::config::Layout::Wide => {} } // /**/ - 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(_) => {} - }); + for comment in child_comments { + steps.push_back(comment); + 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::NewLine); - steps.push_back(crate::builder::Step::Pad); - steps.push_back(crate::builder::Step::FormatWider(child.element)); - if indent { + steps.push_back(crate::builder::Step::FormatWider( + child_expr.element, + )); + if !top_level { steps.push_back(crate::builder::Step::Dedent); } } crate::config::Layout::Wide => { steps.push_back(crate::builder::Step::Whitespace); - steps.push_back(crate::builder::Step::Format(child.element)); + steps.push_back(crate::builder::Step::Format(child_expr.element)); } } diff --git a/tests/cases/let_in/in b/tests/cases/let_in/in index 9051301..6f80d6f 100644 --- a/tests/cases/let_in/in +++ b/tests/cases/let_in/in @@ -25,6 +25,11 @@ let a = let /*b*/ c=1; /*d*/ in /*e*/ f; /**/ + a = let + in [ + 1 + 2 + ]; in diff --git a/tests/cases/let_in/out b/tests/cases/let_in/out index 30fff92..589efa7 100644 --- a/tests/cases/let_in/out +++ b/tests/cases/let_in/out @@ -81,6 +81,12 @@ let */ f; /**/ + + a = let + in [ + 1 + 2 + ]; in /**/ a