diff --git a/src/fmt/expr.rs b/src/fmt/expr.rs index 0ee15c89ed..8b463962df 100644 --- a/src/fmt/expr.rs +++ b/src/fmt/expr.rs @@ -397,11 +397,8 @@ fn fmt_if<'a>( ) { let is_multiline_then = is_multiline_expr(&loc_then.value); let is_multiline_else = is_multiline_expr(&loc_else.value); - let is_multiline = is_multiline_then || is_multiline_else; - - buf.push_str("if "); - fmt_expr(buf, &loc_condition.value, indent, false, true); - buf.push_str(" then"); + let is_multiline_condition = is_multiline_expr(&loc_condition.value); + let is_multiline = is_multiline_then || is_multiline_else || is_multiline_condition; let return_indent = if is_multiline { indent + INDENT @@ -409,6 +406,18 @@ fn fmt_if<'a>( indent }; + if is_multiline_condition { + buf.push_str("if"); + newline(buf, return_indent); + fmt_expr(buf, &loc_condition.value, return_indent, false, false); + newline(buf, indent); + buf.push_str("then"); + } else { + buf.push_str("if "); + fmt_expr(buf, &loc_condition.value, indent, false, true); + buf.push_str(" then"); + } + if is_multiline { newline(buf, return_indent); } else { diff --git a/tests/test_format.rs b/tests/test_format.rs index d95a20cb61..6e3405066e 100644 --- a/tests/test_format.rs +++ b/tests/test_format.rs @@ -417,6 +417,58 @@ mod test_format { )); } + #[test] + fn reduce_space_between_comments() { + expr_formats_to( + indoc!( + r#" + # First + + + + + # Second + x + "# + ), + indoc!( + r#" + # First + + # Second + x + "# + ), + ); + + // expr_formats_to( + // indoc!( + // r#" + // f = \x -> + // # 1st + // + // + // + // + // # 2nd + // x + // + // f 4 + // "# + // ), + // indoc!( + // r#" + // f = \x -> + // # 1st + // + // # 2nd + // x + // + // f 4 + // "# + // ), + // ); + } #[test] fn doesnt_detect_comment_in_comment() { expr_formats_same(indoc!( @@ -638,107 +690,113 @@ mod test_format { #[test] fn multi_line_if_condition() { - // expr_formats_same(indoc!( - // r#" - // if - // waterWillBoil pressure temperature - // then - // turnOnAc - // - // else - // identity - // "# - // )); + expr_formats_same(indoc!( + r#" + if + waterWillBoil pressure temperature + then + turnOnAc - // expr_formats_to( - // indoc!( - // r#" - // if - // - // - // willBoil home water - // - // - // then - // \_ -> leave - // - // else - // identity - // "# - // ), - // indoc!( - // r#" - // if - // willBoil home water - // then - // \_ -> leave - // - // else - // identity - // "# - // ), - // ); - // } + else + identity + "# + )); - // #[test] - // fn if_removes_newlines() { - // expr_formats_to( - // indoc!( - // r#" - // if - // - // # You never know! - // isPrime 8 - // - // # Top Comment - // - // # Bottom Comment - // - // - // then - // - // # A - // - // # B - // - // nothing - // - // # B again - // - // else - // - // # C - // # D - // - // # E - // # F - // - // just (div 1 8) - // "# - // ), - // indoc!( - // r#" - // if - // # You never know! - // isPrime 8 - // # Top Comment - // # Bottom Comment - // then - // # A - // # B - // nothing - // # B again - // - // else - // # C - // # D - // # E - // # F - // just (div 1 8) - // "# - // ), - // ); + expr_formats_to( + indoc!( + r#" + if + + + willBoil home water + + + then + \_ -> leave + + else + identity + "# + ), + indoc!( + r#" + if + willBoil home water + then + \_ -> leave + + else + identity + "# + ), + ); } + + // #[test] + // fn if_removes_newlines() { + // expr_formats_to( + // indoc!( + // r#" + // if + // + // # You never know! + // isPrime 8 + // + // # Top Comment + // + // # Bottom Comment + // + // + // then + // + // # A + // + // # B + // + // nothing + // + // # B again + // + // else + // + // # C + // # D + // + // # E + // # F + // + // just (div 1 8) + // "# + // ), + // indoc!( + // r#" + // if + // # You never know! + // isPrime 8 + + // # Top Comment + + // # Bottom Comment + // then + // # A + + // # B + + // nothing + + // # B again + // + // else + // # C + // # D + + // # E + // # F + // just (div 1 8) + // "# + // ), + // ); + // } #[test] fn multi_line_if() { expr_formats_to(