diff --git a/src/alejandra_engine/src/rules/key_value.rs b/src/alejandra_engine/src/rules/key_value.rs index b8494f9..79dc0fd 100644 --- a/src/alejandra_engine/src/rules/key_value.rs +++ b/src/alejandra_engine/src/rules/key_value.rs @@ -6,9 +6,9 @@ pub(crate) fn rule( let mut children = crate::children::Children::new(build_ctx, node); - let vertical = children.has_comments() - || children.has_newlines() - || build_ctx.vertical; + let vertical = build_ctx.vertical + || children.has_comments() + || children.has_newlines(); // a let child = children.get_next().unwrap(); @@ -103,7 +103,12 @@ pub(crate) fn rule( ) || (matches!( child_expr.kind(), rnix::SyntaxKind::NODE_APPLY - ) && !newlines) + ) + && crate::utils::second_through_penultimate_line_are_indented( + build_ctx, + child_expr.clone(), + false, + )) { steps.push_back(crate::builder::Step::Whitespace); } else { diff --git a/src/alejandra_engine/src/rules/paren.rs b/src/alejandra_engine/src/rules/paren.rs index 14d3273..d7dc46e 100644 --- a/src/alejandra_engine/src/rules/paren.rs +++ b/src/alejandra_engine/src/rules/paren.rs @@ -28,9 +28,10 @@ pub(crate) fn rule( | rnix::SyntaxKind::NODE_LAMBDA | rnix::SyntaxKind::NODE_SELECT | rnix::SyntaxKind::NODE_WITH - ) && second_through_penultimate_line_are_not_indented( + ) && !crate::utils::second_through_penultimate_line_are_indented( build_ctx, expression.element.clone(), + matches!(expression.element.kind(), rnix::SyntaxKind::NODE_LAMBDA), ); // opener @@ -96,31 +97,3 @@ pub(crate) fn rule( steps } - -fn second_through_penultimate_line_are_not_indented( - build_ctx: &crate::builder::BuildCtx, - element: rnix::SyntaxElement, -) -> bool { - let mut build_ctx = - crate::builder::BuildCtx { force_wide: false, ..build_ctx.clone() }; - - let formatted = - crate::builder::build(&mut build_ctx, element).unwrap().to_string(); - - let formatted_lines: Vec<&str> = formatted.split('\n').collect(); - - if formatted_lines.len() <= 2 { - return false; - } - - let whitespace = format!("{0:<1$} ", "", 2 * build_ctx.indentation); - let lambda = format!("{0:<1$}}}:", "", 2 * build_ctx.indentation); - let in_ = format!("{0:<1$}in", "", 2 * build_ctx.indentation); - - formatted_lines.iter().skip(1).rev().skip(1).any(|line| { - !line.is_empty() - && !(line.starts_with(&whitespace) - || line.starts_with(&lambda) - || line.starts_with(&in_)) - }) -} diff --git a/src/alejandra_engine/src/utils.rs b/src/alejandra_engine/src/utils.rs index ecd2208..c4b8799 100644 --- a/src/alejandra_engine/src/utils.rs +++ b/src/alejandra_engine/src/utils.rs @@ -5,3 +5,32 @@ pub(crate) fn has_newlines(string: &str) -> bool { pub(crate) fn count_newlines(string: &str) -> usize { string.chars().filter(|c| *c == '\n').count() } + +pub(crate) fn second_through_penultimate_line_are_indented( + build_ctx: &crate::builder::BuildCtx, + element: rnix::SyntaxElement, + if_leq_than_two_lines: bool, +) -> bool { + let mut build_ctx = + crate::builder::BuildCtx { force_wide: false, ..build_ctx.clone() }; + + let formatted = + crate::builder::build(&mut build_ctx, element).unwrap().to_string(); + + let formatted_lines: Vec<&str> = formatted.split('\n').collect(); + + if formatted_lines.len() <= 2 { + return if_leq_than_two_lines; + } + + let whitespace = format!("{0:<1$} ", "", 2 * build_ctx.indentation); + let lambda = format!("{0:<1$}}}:", "", 2 * build_ctx.indentation); + let in_ = format!("{0:<1$}in", "", 2 * build_ctx.indentation); + + formatted_lines.iter().skip(1).rev().skip(1).all(|line| { + line.is_empty() + || line.starts_with(&lambda) + || line.starts_with(&in_) + || line.starts_with(&whitespace) + }) +} diff --git a/src/alejandra_engine/tests/cases/apply/in b/src/alejandra_engine/tests/cases/apply/in index 7b9d6b3..a02e377 100644 --- a/src/alejandra_engine/tests/cases/apply/in +++ b/src/alejandra_engine/tests/cases/apply/in @@ -1,4 +1,6 @@ [ + (a + b) ( (a b) (a b) diff --git a/src/alejandra_engine/tests/cases/apply/out b/src/alejandra_engine/tests/cases/apply/out index 5bc2dbd..72e7317 100644 --- a/src/alejandra_engine/tests/cases/apply/out +++ b/src/alejandra_engine/tests/cases/apply/out @@ -1,4 +1,6 @@ [ + (a + b) ((a b) (a b) (a @@ -57,17 +59,19 @@ asdf = 1; }; - name2 = function arg { - asdf = 1; - } - argument; + name2 = + function arg { + asdf = 1; + } + argument; - name3 = function arg { - asdf = 1; - } { - qwer = 12345; - } - argument; + name3 = + function arg { + asdf = 1; + } { + qwer = 12345; + } + argument; } { name4 = @@ -81,23 +85,26 @@ argument; } { - option1 = function arg {asdf = 1;} { - qwer = 12345; - qwer2 = 54321; - } - lastArg; + option1 = + function arg {asdf = 1;} { + qwer = 12345; + qwer2 = 54321; + } + lastArg; - option2 = function arg {asdf = 1;} { - qwer = 12345; - qwer2 = 54321; - } - lastArg; + option2 = + function arg {asdf = 1;} { + qwer = 12345; + qwer2 = 54321; + } + lastArg; - option3 = function arg {asdf = 1;} - { - qwer = 12345; - qwer2 = 54321; - } - lastArg; + option3 = + function arg {asdf = 1;} + { + qwer = 12345; + qwer2 = 54321; + } + lastArg; } ]