Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: slightly better formatting of empty blocks with comments #6367

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion tooling/nargo_fmt/src/chunks.rs
Original file line number Diff line number Diff line change
@@ -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)
@@ -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;
}
}
}

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
@@ -643,7 +643,10 @@ mod foo;
/* hello */
}
";
let expected = "struct Foo { /* hello */ }\n";
let expected = "struct Foo {
/* hello */
}
";
assert_format(src, expected);
}

15 changes: 8 additions & 7 deletions tooling/nargo_fmt/src/formatter/expression.rs
Original file line number Diff line number Diff line change
@@ -1142,6 +1142,9 @@
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();
});
@@ -1151,15 +1154,13 @@
// 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)
}
}
@@ -1750,9 +1751,9 @@

#[test]
fn format_method_call_chain_3() {
let src = "fn foo() { assert(p4_affine.eq(Gaffine::new(6890855772600357754907169075114257697580319025794532037257385534741338397365, 4338620300185947561074059802482547481416142213883829469920100239455078257889))); }";

Check warning on line 1754 in tooling/nargo_fmt/src/formatter/expression.rs

GitHub Actions / Code

Unknown word (Gaffine)
let expected = "fn foo() {
assert(p4_affine.eq(Gaffine::new(

Check warning on line 1756 in tooling/nargo_fmt/src/formatter/expression.rs

GitHub Actions / Code

Unknown word (Gaffine)
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889,
)));
@@ -1788,7 +1789,7 @@
fn format_nested_method_call_with_maximum_width_2() {
let src = "fn foo() {
assert(
p4_affine.eq(Gaffine::new(

Check warning on line 1792 in tooling/nargo_fmt/src/formatter/expression.rs

GitHub Actions / Code

Unknown word (Gaffine)
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889,
)),
@@ -1796,7 +1797,7 @@
}
";
let expected = "fn foo() {
assert(p4_affine.eq(Gaffine::new(

Check warning on line 1800 in tooling/nargo_fmt/src/formatter/expression.rs

GitHub Actions / Code

Unknown word (Gaffine)
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889,
)));
28 changes: 15 additions & 13 deletions tooling/nargo_fmt/src/formatter/module.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}

@@ -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 { }";