commit d5b3d97c232b71923bcc834261ce49bdb827c1d9 (tree)
parent c98f792ff8ed7af0d5e427836600c2ed7b30965e
Author: Andrew Kelley <andrew@ziglang.org>
Date: Wed, 28 Aug 2019 12:01:08 -0400
Merge pull request #2760 from Vexu/fmt-comment-fix
Fix non-empty comments getting removed with empty comments
Diffstat:
2 files changed, 32 insertions(+), 10 deletions(-)
diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig
@@ -2375,7 +2375,6 @@ test "zig fmt: if type expr" {
\\
);
}
-
test "zig fmt: file ends with struct field" {
try testTransform(
\\a: bool
@@ -2385,6 +2384,20 @@ test "zig fmt: file ends with struct field" {
);
}
+test "zig fmt: comment after empty comment" {
+ try testTransform(
+ \\const x = true; //
+ \\//
+ \\//
+ \\//a
+ \\
+ ,
+ \\const x = true;
+ \\//a
+ \\
+ );
+}
+
test "zig fmt: comments at several places in struct init" {
try testTransform(
\\var bar = Bar{
diff --git a/std/zig/render.zig b/std/zig/render.zig
@@ -1994,15 +1994,24 @@ fn renderTokenOffset(
}
}
- const comment_is_empty = mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ").len == 2;
- if (comment_is_empty) {
- switch (space) {
- Space.Newline => {
- try stream.writeByte('\n');
- start_col.* = 0;
- return;
- },
- else => {},
+ while (true) {
+ const comment_is_empty = mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ").len == 2;
+ if (comment_is_empty) {
+ switch (space) {
+ Space.Newline => {
+ offset += 1;
+ token = next_token;
+ next_token = tree.tokens.at(token_index + offset);
+ if (next_token.id != .LineComment) {
+ try stream.writeByte('\n');
+ start_col.* = 0;
+ return;
+ }
+ },
+ else => break,
+ }
+ } else {
+ break;
}
}