mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 16:21:11 +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_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 {
|
||||||
|
|
|
@ -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,47 +690,47 @@ 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
|
));
|
||||||
//
|
|
||||||
//
|
expr_formats_to(
|
||||||
// willBoil home water
|
indoc!(
|
||||||
//
|
r#"
|
||||||
//
|
if
|
||||||
// then
|
|
||||||
// \_ -> leave
|
|
||||||
//
|
willBoil home water
|
||||||
// else
|
|
||||||
// identity
|
|
||||||
// "#
|
then
|
||||||
// ),
|
\_ -> leave
|
||||||
// indoc!(
|
|
||||||
// r#"
|
else
|
||||||
// if
|
identity
|
||||||
// willBoil home water
|
"#
|
||||||
// then
|
),
|
||||||
// \_ -> leave
|
indoc!(
|
||||||
//
|
r#"
|
||||||
// else
|
if
|
||||||
// identity
|
willBoil home water
|
||||||
// "#
|
then
|
||||||
// ),
|
\_ -> leave
|
||||||
// );
|
|
||||||
// }
|
else
|
||||||
|
identity
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn if_removes_newlines() {
|
// fn if_removes_newlines() {
|
||||||
|
@ -721,24 +773,30 @@ mod test_format {
|
||||||
// if
|
// if
|
||||||
// # You never know!
|
// # You never know!
|
||||||
// isPrime 8
|
// isPrime 8
|
||||||
|
|
||||||
// # Top Comment
|
// # Top Comment
|
||||||
|
|
||||||
// # Bottom Comment
|
// # Bottom Comment
|
||||||
// then
|
// then
|
||||||
// # A
|
// # A
|
||||||
|
|
||||||
// # B
|
// # B
|
||||||
|
|
||||||
// nothing
|
// nothing
|
||||||
|
|
||||||
// # B again
|
// # B again
|
||||||
//
|
//
|
||||||
// else
|
// else
|
||||||
// # C
|
// # C
|
||||||
// # D
|
// # D
|
||||||
|
|
||||||
// # E
|
// # E
|
||||||
// # F
|
// # F
|
||||||
// just (div 1 8)
|
// just (div 1 8)
|
||||||
// "#
|
// "#
|
||||||
// ),
|
// ),
|
||||||
// );
|
// );
|
||||||
}
|
// }
|
||||||
#[test]
|
#[test]
|
||||||
fn multi_line_if() {
|
fn multi_line_if() {
|
||||||
expr_formats_to(
|
expr_formats_to(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue