authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-22 16:59:44+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-22 16:59:44+02:00
log69d5a106da3fd339c7d1ed177b706aa30d8cb1a9
treed069a1de68b596884319db71d0fedc521b680bed
parent67dac2936cb1fc2ce1d3ea00a5c8e574921aded3
signaturelock-open Commit is signed but in an unrecognized format.

render: handle comments ending in EOF


3 files changed, 49 insertions(+), 21 deletions(-)

lib/std/zig/parser_test.zig+23-14
......@@ -84,6 +84,15 @@ test "zig fmt: empty file" {
8484 );
8585}
8686
87test "zig fmt: file ends in comment" {
88 try testTransform(
89 \\ //foobar
90 ,
91 \\//foobar
92 \\
93 );
94}
95
8796test "zig fmt: doc comments on test" {
8897 try testCanonical(
8998 \\/// hello
......@@ -3385,20 +3394,20 @@ test "zig fmt: file ends with struct field" {
33853394 );
33863395}
33873396
3388//test "zig fmt: comment after empty comment" {
3389// try testTransform(
3390// \\const x = true; //
3391// \\//
3392// \\//
3393// \\//a
3394// \\
3395// ,
3396// \\const x = true;
3397// \\//a
3398// \\
3399// );
3400//}
3401//
3397test "zig fmt: comment after empty comment" {
3398 try testTransform(
3399 \\const x = true; //
3400 \\//
3401 \\//
3402 \\//a
3403 \\
3404 ,
3405 \\const x = true;
3406 \\//a
3407 \\
3408 );
3409}
3410
34023411//test "zig fmt: line comment in array" {
34033412// try testTransform(
34043413// \\test "a" {
lib/std/zig/render.zig+25-5
......@@ -26,7 +26,10 @@ pub fn renderTree(buffer: *std.ArrayList(u8), tree: ast.Tree) Error!void {
2626 const ais = &auto_indenting_stream;
2727
2828 // Render all the line comments at the beginning of the file.
29 const comment_end_loc: usize = tree.tokens.items(.start)[0];
29 const comment_end_loc = if (tree.tokens.items(.tag)[0] == .eof)
30 tree.source.len
31 else
32 tree.tokens.items(.start)[0];
3033 _ = try renderComments(ais, tree, 0, comment_end_loc);
3134
3235 try renderMembers(ais, tree, tree.rootDecls());
......@@ -1995,11 +1998,20 @@ fn renderToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex, space: Sp
19951998/// that end is the last byte before the next token.
19961999fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {
19972000 var index: usize = start;
2001 var rendered_empty_comments = false;
19982002 while (mem.indexOf(u8, tree.source[index..end], "//")) |offset| {
19992003 const comment_start = index + offset;
2000 const newline = comment_start +
2001 mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;
2002
2004 const newline_index = mem.indexOfScalar(u8, tree.source[comment_start..end], '\n') orelse {
2005 // comment ends in EOF.
2006 const untrimmed_comment = tree.source[comment_start..];
2007 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces);
2008 if (trimmed_comment.len != 2) {
2009 try ais.writer().print("{s}\n", .{trimmed_comment});
2010 index = end;
2011 }
2012 return index != start;
2013 };
2014 const newline = comment_start + newline_index;
20032015 const untrimmed_comment = tree.source[comment_start..newline];
20042016 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces);
20052017
......@@ -2013,6 +2025,11 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
20132025 // Respect the newline directly before the comment.
20142026 // Note: This allows an empty line between comments
20152027 try ais.insertNewline();
2028 } else if (trimmed_comment.len == 2) {
2029 if (!rendered_empty_comments) {
2030 try ais.writer().writeByte('\n');
2031 rendered_empty_comments = true;
2032 }
20162033 } else if (index == start) {
20172034 // Otherwise if the first comment is on the same line as
20182035 // the token before it, prefix it with a single space.
......@@ -2020,7 +2037,10 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
20202037 }
20212038 }
20222039
2023 try ais.writer().print("{s}\n", .{trimmed_comment});
2040 if (trimmed_comment.len != 2) {
2041 try ais.writer().print("{s}\n", .{trimmed_comment});
2042 rendered_empty_comments = false;
2043 }
20242044 index = newline + 1;
20252045
20262046 if (ais.disabled_offset) |disabled_offset| {
src/main.zig+1-2
......@@ -2866,8 +2866,7 @@ fn fmtPathFile(
28662866 try fmt.out_buffer.ensureCapacity(source_code.len);
28672867
28682868 try tree.renderToArrayList(&fmt.out_buffer);
2869 const anything_changed = mem.eql(u8, fmt.out_buffer.items, source_code);
2870 if (!anything_changed)
2869 if (mem.eql(u8, fmt.out_buffer.items, source_code))
28712870 return;
28722871
28732872 if (check_mode) {