authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-28 12:01:08-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-08-28 12:01:08-04:00
logd5b3d97c232b71923bcc834261ce49bdb827c1d9
tree2740bbf8f47881317673a1e28ea9a42803f542bb
parentc98f792ff8ed7af0d5e427836600c2ed7b30965e
parent92a81252cbd8e2576930a0575e939ed4c26f3c29
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

Fix non-empty comments getting removed with empty comments

2 files changed, 32 insertions(+), 10 deletions(-)

std/zig/parser_test.zig+14-1
......@@ -2375,7 +2375,6 @@ test "zig fmt: if type expr" {
23752375 \\
23762376 );
23772377}
2378
23792378test "zig fmt: file ends with struct field" {
23802379 try testTransform(
23812380 \\a: bool
......@@ -2385,6 +2384,20 @@ test "zig fmt: file ends with struct field" {
23852384 );
23862385}
23872386
2387test "zig fmt: comment after empty comment" {
2388 try testTransform(
2389 \\const x = true; //
2390 \\//
2391 \\//
2392 \\//a
2393 \\
2394 ,
2395 \\const x = true;
2396 \\//a
2397 \\
2398 );
2399}
2400
23882401test "zig fmt: comments at several places in struct init" {
23892402 try testTransform(
23902403 \\var bar = Bar{
std/zig/render.zig+18-9
......@@ -1994,15 +1994,24 @@ fn renderTokenOffset(
19941994 }
19951995 }
19961996
1997 const comment_is_empty = mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ").len == 2;
1998 if (comment_is_empty) {
1999 switch (space) {
2000 Space.Newline => {
2001 try stream.writeByte('\n');
2002 start_col.* = 0;
2003 return;
2004 },
2005 else => {},
1997 while (true) {
1998 const comment_is_empty = mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ").len == 2;
1999 if (comment_is_empty) {
2000 switch (space) {
2001 Space.Newline => {
2002 offset += 1;
2003 token = next_token;
2004 next_token = tree.tokens.at(token_index + offset);
2005 if (next_token.id != .LineComment) {
2006 try stream.writeByte('\n');
2007 start_col.* = 0;
2008 return;
2009 }
2010 },
2011 else => break,
2012 }
2013 } else {
2014 break;
20062015 }
20072016 }
20082017