From ada28f06755504e7af0009d3ca5e11423d6c3ec8 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Sat, 19 Oct 2024 17:04:24 -0300 Subject: [PATCH] Make sure to always attach semicolons to parent groups --- tooling/nargo_fmt/src/formatter/expression.rs | 4 +++- tooling/nargo_fmt/src/formatter/function.rs | 12 +++++++----- tooling/nargo_fmt/src/formatter/use_tree.rs | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tooling/nargo_fmt/src/formatter/expression.rs b/tooling/nargo_fmt/src/formatter/expression.rs index b870cb9783..93642f8458 100644 --- a/tooling/nargo_fmt/src/formatter/expression.rs +++ b/tooling/nargo_fmt/src/formatter/expression.rs @@ -156,8 +156,10 @@ impl<'a> Formatter<'a> { group.line(); self.format_expression(*repeated_element, &mut group); - group.text(self.chunk(|formatter| { + group.text_attached_to_last_group(self.chunk(|formatter| { formatter.write_semicolon(); + })); + group.text(self.chunk(|formatter| { formatter.write_space(); })); self.format_expression(*length, &mut group); diff --git a/tooling/nargo_fmt/src/formatter/function.rs b/tooling/nargo_fmt/src/formatter/function.rs index d88dacbaf1..e13d902b82 100644 --- a/tooling/nargo_fmt/src/formatter/function.rs +++ b/tooling/nargo_fmt/src/formatter/function.rs @@ -224,14 +224,16 @@ impl<'a> Formatter<'a> { } if !has_where_clause && !wrote_comment { - group.text(self.chunk(|formatter| { - if semicolon { + if semicolon { + group.text_attached_to_last_group(self.chunk(|formatter| { formatter.write_semicolon(); - } else { + })); + } else { + group.text(self.chunk(|formatter| { formatter.write_space(); formatter.write_left_brace(); - } - })); + })); + } } if has_parameters { diff --git a/tooling/nargo_fmt/src/formatter/use_tree.rs b/tooling/nargo_fmt/src/formatter/use_tree.rs index bd5ab3b7c5..1e6c492702 100644 --- a/tooling/nargo_fmt/src/formatter/use_tree.rs +++ b/tooling/nargo_fmt/src/formatter/use_tree.rs @@ -20,8 +20,8 @@ impl<'a> Formatter<'a> { chunks.group(self.format_use_tree(use_tree)); - chunks.text(self.chunk(|formatter| { - formatter.write_token(Token::Semicolon); + chunks.text_attached_to_last_group(self.chunk(|formatter| { + formatter.write_semicolon(); })); self.write_indentation(); @@ -183,4 +183,14 @@ mod tests { let expected = "use crate::hash::{Hash, Hasher};\n"; assert_format(src, expected); } + + #[test] + fn attaches_semicolon_to_last_group() { + let src = " use crate::hash::{Hash, Hasher}; "; + let expected = "use crate::hash::{ + Hash, Hasher, +}; +"; + assert_format_with_max_width(src, expected, "use crate::hash::{Hash, Hasher}".len()); + } }