From 750ca52debb5107e8970f0c4090a08c2c51a66b8 Mon Sep 17 00:00:00 2001 From: Sean Hagstrom Date: Mon, 16 May 2022 19:18:41 +0100 Subject: [PATCH] improvement(formatter): format empty record patterns without spaces --- compiler/fmt/src/pattern.rs | 20 +++++++------ compiler/fmt/tests/test_fmt.rs | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/compiler/fmt/src/pattern.rs b/compiler/fmt/src/pattern.rs index e4f53fc47c..4df623dd5c 100644 --- a/compiler/fmt/src/pattern.rs +++ b/compiler/fmt/src/pattern.rs @@ -85,20 +85,22 @@ impl<'a> Formattable for Pattern<'a> { RecordDestructure(loc_patterns) => { buf.indent(indent); buf.push_str("{"); - buf.spaces(1); - let mut it = loc_patterns.iter().peekable(); + 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); - while let Some(loc_pattern) = it.next() { - loc_pattern.format(buf, indent); - - if it.peek().is_some() { - buf.push_str(","); - buf.spaces(1); + if it.peek().is_some() { + buf.push_str(","); + buf.spaces(1); + } } + buf.spaces(1); } - buf.push_str(" }"); + buf.push_str("}"); } RequiredField(name, loc_pattern) => { diff --git a/compiler/fmt/tests/test_fmt.rs b/compiler/fmt/tests/test_fmt.rs index 985d4cbc28..3f3c25033c 100644 --- a/compiler/fmt/tests/test_fmt.rs +++ b/compiler/fmt/tests/test_fmt.rs @@ -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]