fix comments getting removed after empty comments

This commit is contained in:
Vexu 2019-06-27 02:00:30 +03:00
parent 01ff0d4d62
commit de369de312
No known key found for this signature in database
GPG Key ID: 5AEABFCAFF5CD8D6
2 changed files with 32 additions and 9 deletions

View File

@ -2246,6 +2246,20 @@ test "zig fmt: if type expr" {
);
}
test "zig fmt: comment after empty comment" {
try testTransform(
\\const x = true; //
\\//
\\//
\\//a
\\
,
\\const x = true;
\\//a
\\
);
}
const std = @import("std");
const mem = std.mem;
const warn = std.debug.warn;

View File

@ -1941,15 +1941,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;
}
}