fix(formatter): ensure multi-line function type annotations for annotated bodies are indented properly

This commit is contained in:
Sean Hagstrom 2022-05-16 18:46:02 +01:00
parent 301a192f9d
commit 98a174c984
No known key found for this signature in database
GPG Key ID: AA9814E95B301A5C
2 changed files with 92 additions and 2 deletions

View File

@ -134,15 +134,42 @@ impl<'a> Formattable for Def<'a> {
body_pattern,
body_expr,
} => {
use roc_parse::ast::TypeAnnotation;
let is_type_multiline = ann_type.is_multiline();
let is_type_function = matches!(
ann_type.value,
TypeAnnotation::Function(..)
| TypeAnnotation::SpaceBefore(TypeAnnotation::Function(..), ..)
| TypeAnnotation::SpaceAfter(TypeAnnotation::Function(..), ..)
);
let next_indent = if is_type_multiline {
indent + INDENT
} else {
indent
};
ann_pattern.format(buf, indent);
buf.push_str(" :");
buf.spaces(1);
ann_type.format(buf, indent);
if is_type_multiline && is_type_function {
ann_type.format_with_options(
buf,
Parens::NotNeeded,
Newlines::Yes,
next_indent,
);
} else {
buf.spaces(1);
ann_type.format(buf, indent);
}
if let Some(comment_str) = comment {
buf.push_str(" #");
buf.spaces(1);
buf.push_str(comment_str.trim());
}
buf.newline();
fmt_body(buf, &body_pattern.value, &body_expr.value, indent);
}

View File

@ -4487,6 +4487,19 @@ mod test_fmt {
"#
));
expr_formats_same(indoc!(
r#"
foo :
(Str -> Bool),
Str
-> Bool
foo = \bar, baz ->
42
42
"#
));
expr_formats_to(
indoc!(
r#"
@ -4506,6 +4519,56 @@ mod test_fmt {
"#
),
);
expr_formats_to(
indoc!(
r#"
foo :
(Str -> Bool), Str -> Bool
foo = \bar, baz ->
42
42
"#
),
indoc!(
r#"
foo :
(Str -> Bool),
Str
-> Bool
foo = \bar, baz ->
42
42
"#
),
);
expr_formats_to(
indoc!(
r#"
foo :
(Str -> Bool), Str -> Bool # comment
foo = \bar, baz ->
42
42
"#
),
indoc!(
r#"
foo :
(Str -> Bool),
Str
-> Bool # comment
foo = \bar, baz ->
42
42
"#
),
);
}
#[test]