Merge pull request #3079 from rtfeldman/formatter-empty-record-patterns

Formatter empty record patterns
This commit is contained in:
Richard Feldman 2022-05-16 19:12:45 -04:00 committed by GitHub
commit 3bbfb397d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 77 additions and 20 deletions

View file

@ -85,10 +85,10 @@ impl<'a> Formattable for Pattern<'a> {
RecordDestructure(loc_patterns) => {
buf.indent(indent);
buf.push_str("{");
if !loc_patterns.is_empty() {
buf.spaces(1);
let mut it = loc_patterns.iter().peekable();
while let Some(loc_pattern) = it.next() {
loc_pattern.format(buf, indent);
@ -97,6 +97,8 @@ impl<'a> Formattable for Pattern<'a> {
buf.spaces(1);
}
}
buf.spaces(1);
}
buf.push_str("}");
}

View file

@ -2727,6 +2727,61 @@ mod test_fmt {
#[test]
fn empty_record() {
expr_formats_same("{}");
expr_formats_to("{ }", "{}");
}
#[test]
fn empty_record_patterns() {
expr_formats_to(
indoc!(
r#"
f = \{ } -> "Hello World"
f
"#
),
indoc!(
r#"
f = \{} -> "Hello World"
f
"#
),
);
expr_formats_to(
indoc!(
r#"
f = \a, b -> { }
f
"#
),
indoc!(
r#"
f = \a, b -> {}
f
"#
),
);
expr_formats_to(
indoc!(
r#"
{ } <- f a b
{}
"#
),
indoc!(
r#"
{} <- f a b
{}
"#
),
);
}
#[test]