Merge pull request #92 from rtfeldman/ct/multi-line-if-condition-1

Multi line if conditions
This commit is contained in:
Richard Feldman 2020-01-01 04:36:29 -05:00 committed by GitHub
commit 3b008b69a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 170 additions and 103 deletions

View file

@ -397,11 +397,8 @@ fn fmt_if<'a>(
) { ) {
let is_multiline_then = is_multiline_expr(&loc_then.value); let is_multiline_then = is_multiline_expr(&loc_then.value);
let is_multiline_else = is_multiline_expr(&loc_else.value); let is_multiline_else = is_multiline_expr(&loc_else.value);
let is_multiline = is_multiline_then || is_multiline_else; let is_multiline_condition = is_multiline_expr(&loc_condition.value);
let is_multiline = is_multiline_then || is_multiline_else || is_multiline_condition;
buf.push_str("if ");
fmt_expr(buf, &loc_condition.value, indent, false, true);
buf.push_str(" then");
let return_indent = if is_multiline { let return_indent = if is_multiline {
indent + INDENT indent + INDENT
@ -409,6 +406,18 @@ fn fmt_if<'a>(
indent 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 { if is_multiline {
newline(buf, return_indent); newline(buf, return_indent);
} else { } else {

View file

@ -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] #[test]
fn doesnt_detect_comment_in_comment() { fn doesnt_detect_comment_in_comment() {
expr_formats_same(indoc!( expr_formats_same(indoc!(
@ -638,107 +690,113 @@ mod test_format {
#[test] #[test]
fn multi_line_if_condition() { fn multi_line_if_condition() {
// expr_formats_same(indoc!( expr_formats_same(indoc!(
// r#" r#"
// if if
// waterWillBoil pressure temperature waterWillBoil pressure temperature
// then then
// turnOnAc turnOnAc
//
// else
// identity
// "#
// ));
// expr_formats_to( else
// indoc!( identity
// r#" "#
// if ));
//
//
// willBoil home water
//
//
// then
// \_ -> leave
//
// else
// identity
// "#
// ),
// indoc!(
// r#"
// if
// willBoil home water
// then
// \_ -> leave
//
// else
// identity
// "#
// ),
// );
// }
// #[test] expr_formats_to(
// fn if_removes_newlines() { indoc!(
// expr_formats_to( r#"
// indoc!( if
// r#"
// if
// willBoil home water
// # You never know!
// isPrime 8
// then
// # Top Comment \_ -> leave
//
// # Bottom Comment else
// identity
// "#
// then ),
// indoc!(
// # A r#"
// if
// # B willBoil home water
// then
// nothing \_ -> leave
//
// # B again else
// identity
// 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 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] #[test]
fn multi_line_if() { fn multi_line_if() {
expr_formats_to( expr_formats_to(