Merge pull request #2760 from Vexu/fmt-comment-fix

Fix non-empty comments getting removed with empty comments
This commit is contained in:
Andrew Kelley 2019-08-28 12:01:08 -04:00 committed by GitHub
commit d5b3d97c23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 10 deletions

View File

@ -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{

View File

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