mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 08:11:12 +00:00
Merge pull request #92 from rtfeldman/ct/multi-line-if-condition-1
Multi line if conditions
This commit is contained in:
commit
3b008b69a8
2 changed files with 170 additions and 103 deletions
|
@ -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 {
|
||||
|
|
|
@ -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,47 +690,47 @@ 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
|
||||
"#
|
||||
));
|
||||
|
||||
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() {
|
||||
|
@ -721,24 +773,30 @@ mod test_format {
|
|||
// 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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue