| author | |
| committer | |
| log | d5b3d97c232b71923bcc834261ce49bdb827c1d9 |
| tree | 2740bbf8f47881317673a1e28ea9a42803f542bb |
| parent | c98f792ff8ed7af0d5e427836600c2ed7b30965e |
| parent | 92a81252cbd8e2576930a0575e939ed4c26f3c29 |
| signature |
Fix non-empty comments getting removed with empty comments2 files changed, 32 insertions(+), 10 deletions(-)
std/zig/parser_test.zig+14-1| ... | ... | @@ -2375,7 +2375,6 @@ test "zig fmt: if type expr" { |
| 2375 | 2375 | \\ |
| 2376 | 2376 | ); |
| 2377 | 2377 | } |
| 2378 | ||
| 2379 | 2378 | test "zig fmt: file ends with struct field" { |
| 2380 | 2379 | try testTransform( |
| 2381 | 2380 | \\a: bool |
| ... | ... | @@ -2385,6 +2384,20 @@ test "zig fmt: file ends with struct field" { |
| 2385 | 2384 | ); |
| 2386 | 2385 | } |
| 2387 | 2386 | |
| 2387 | test "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 | ||
| 2388 | 2401 | test "zig fmt: comments at several places in struct init" { |
| 2389 | 2402 | try testTransform( |
| 2390 | 2403 | \\var bar = Bar{ |
std/zig/render.zig+18-9| ... | ... | @@ -1994,15 +1994,24 @@ fn renderTokenOffset( |
| 1994 | 1994 | } |
| 1995 | 1995 | } |
| 1996 | 1996 | |
| 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; | |
| 2006 | 2015 | } |
| 2007 | 2016 | } |
| 2008 | 2017 |