mirror of
https://github.com/ziglang/zig.git
synced 2026-01-06 05:25:10 +00:00
Merge pull request #2760 from Vexu/fmt-comment-fix
Fix non-empty comments getting removed with empty comments
This commit is contained in:
commit
d5b3d97c23
@ -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{
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user