Skip to content

Commit

Permalink
fix: slightly better formatting of empty blocks with comments (#6367)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Oct 28, 2024
1 parent 2f37610 commit da72979
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
15 changes: 14 additions & 1 deletion tooling/nargo_fmt/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,12 @@ impl<'a> Formatter<'a> {

/// Appends the string to the current buffer line by line, with some pre-checks.
fn write_chunk_lines(&mut self, string: &str) {
for (index, line) in string.lines().enumerate() {
let lines: Vec<_> = string.lines().collect();

let mut index = 0;
while index < lines.len() {
let line = &lines[index];

// Don't indent the first line (it should already be indented).
// Also don't indent if the current line already has a space as the last char
// (it means it's already indented)
Expand All @@ -1015,6 +1020,14 @@ impl<'a> Formatter<'a> {
} else {
self.write(line);
}

index += 1;

// If a newline comes next too, write multiple lines to preserve original formatting
while index < lines.len() && lines[index].is_empty() {
self.write_multiple_lines_without_skipping_whitespace_and_comments();
index += 1;
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion tooling/nargo_fmt/src/formatter/comments_and_whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,10 @@ mod foo;
/* hello */
}
";
let expected = "struct Foo { /* hello */ }\n";
let expected = "struct Foo {
/* hello */
}
";
assert_format(src, expected);
}

Expand Down
15 changes: 8 additions & 7 deletions tooling/nargo_fmt/src/formatter/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,9 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> {
pub(super) fn empty_block_contents_chunk(&mut self) -> Option<ChunkGroup> {
let mut group = ChunkGroup::new();
group.increase_indentation();

let newlines_count = self.following_newlines_count();

let mut chunk = self.chunk(|formatter| {
formatter.skip_comments_and_whitespace_writing_multiple_lines_if_found();
});
Expand All @@ -1151,15 +1154,13 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> {
// so there's nothing to write.
None
} else {
if chunk.string.trim_start().starts_with("//") {
group.text(chunk);
group.decrease_indentation();
group.line();
} else {
// If we have a trailing comment, preserve it in the same line
if newlines_count == 0 && !chunk.string.trim_start().starts_with("//") {
chunk.string = format!(" {} ", chunk.string.trim());
group.text(chunk);
group.decrease_indentation();
}
group.text(chunk);
group.decrease_indentation();
group.line();
Some(group)
}
}
Expand Down
28 changes: 15 additions & 13 deletions tooling/nargo_fmt/src/formatter/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,17 @@ impl<'a> Formatter<'a> {
self.write_space();
self.write_identifier(submodule.name);
self.write_space();
self.write_left_brace();
if parsed_module_is_empty(&submodule.contents) {
self.write_left_brace();
self.increase_indentation();

let comments_count_before = self.written_comments_count;
self.skip_comments_and_whitespace_writing_multiple_lines_if_found();
self.decrease_indentation();
if self.written_comments_count > comments_count_before {
self.write_line();
self.write_indentation();
}
self.write_right_brace();
self.format_empty_block_contents();
} else {
self.write_left_brace();
self.increase_indentation();
self.write_line();
self.format_parsed_module(submodule.contents, self.ignore_next);
self.decrease_indentation();
self.write_indentation();
self.write_right_brace();
}
self.write_right_brace();
}
}

Expand Down Expand Up @@ -102,6 +92,18 @@ mod foo;\n";
assert_format(src, expected);
}

#[test]
fn format_empty_submodule_2() {
let src = "mod foo { mod bar {
} }";
let expected = "mod foo {
mod bar {}
}
";
assert_format(src, expected);
}

#[test]
fn format_empty_subcontract() {
let src = "contract foo { }";
Expand Down

0 comments on commit da72979

Please sign in to comment.