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_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 {

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]
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(