Skip to content

Commit

Permalink
Make sure to always attach semicolons to parent groups
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 19, 2024
1 parent 451d174 commit ada28f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 3 additions & 1 deletion tooling/nargo_fmt/src/formatter/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions tooling/nargo_fmt/src/formatter/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions tooling/nargo_fmt/src/formatter/use_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
}

0 comments on commit ada28f0

Please sign in to comment.