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" {...@@ -84,6 +84,15 @@ test "zig fmt: empty file" {
84 );84 );
85}85}
8686
87test "zig fmt: file ends in comment" {
88 try testTransform(
89 \\ //foobar
90 ,
91 \\//foobar
92 \\
93 );
94}
95
87test "zig fmt: doc comments on test" {96test "zig fmt: doc comments on test" {
88 try testCanonical(97 try testCanonical(
89 \\/// hello98 \\/// hello
...@@ -3385,20 +3394,20 @@ test "zig fmt: file ends with struct field" {...@@ -3385,20 +3394,20 @@ test "zig fmt: file ends with struct field" {
3385 );3394 );
3386}3395}
33873396
3388//test "zig fmt: comment after empty comment" {3397test "zig fmt: comment after empty comment" {
3389// try testTransform(3398 try testTransform(
3390// \\const x = true; //3399 \\const x = true; //
3391// \\//3400 \\//
3392// \\//3401 \\//
3393// \\//a3402 \\//a
3394// \\3403 \\
3395// ,3404 ,
3396// \\const x = true;3405 \\const x = true;
3397// \\//a3406 \\//a
3398// \\3407 \\
3399// );3408 );
3400//}3409}
3401//3410
3402//test "zig fmt: line comment in array" {3411//test "zig fmt: line comment in array" {
3403// try testTransform(3412// try testTransform(
3404// \\test "a" {3413// \\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 {...@@ -26,7 +26,10 @@ pub fn renderTree(buffer: *std.ArrayList(u8), tree: ast.Tree) Error!void {
26 const ais = &auto_indenting_stream;26 const ais = &auto_indenting_stream;
2727
28 // Render all the line comments at the beginning of the file.28 // 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];
30 _ = try renderComments(ais, tree, 0, comment_end_loc);33 _ = try renderComments(ais, tree, 0, comment_end_loc);
3134
32 try renderMembers(ais, tree, tree.rootDecls());35 try renderMembers(ais, tree, tree.rootDecls());
...@@ -1995,11 +1998,20 @@ fn renderToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex, space: Sp...@@ -1995,11 +1998,20 @@ fn renderToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex, space: Sp
1995/// that end is the last byte before the next token.1998/// that end is the last byte before the next token.
1996fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {1999fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {
1997 var index: usize = start;2000 var index: usize = start;
2001 var rendered_empty_comments = false;
1998 while (mem.indexOf(u8, tree.source[index..end], "//")) |offset| {2002 while (mem.indexOf(u8, tree.source[index..end], "//")) |offset| {
1999 const comment_start = index + offset;2003 const comment_start = index + offset;
2000 const newline = comment_start +2004 const newline_index = mem.indexOfScalar(u8, tree.source[comment_start..end], '\n') orelse {
2001 mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;2005 // comment ends in EOF.
20022006 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;
2003 const untrimmed_comment = tree.source[comment_start..newline];2015 const untrimmed_comment = tree.source[comment_start..newline];
2004 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces);2016 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...@@ -2013,6 +2025,11 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
2013 // Respect the newline directly before the comment.2025 // Respect the newline directly before the comment.
2014 // Note: This allows an empty line between comments2026 // Note: This allows an empty line between comments
2015 try ais.insertNewline();2027 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 }
2016 } else if (index == start) {2033 } else if (index == start) {
2017 // Otherwise if the first comment is on the same line as2034 // Otherwise if the first comment is on the same line as
2018 // the token before it, prefix it with a single space.2035 // 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...@@ -2020,7 +2037,10 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
2020 }2037 }
2021 }2038 }
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 }
2024 index = newline + 1;2044 index = newline + 1;
20252045
2026 if (ais.disabled_offset) |disabled_offset| {2046 if (ais.disabled_offset) |disabled_offset| {
src/main.zig+1-2
...@@ -2866,8 +2866,7 @@ fn fmtPathFile(...@@ -2866,8 +2866,7 @@ fn fmtPathFile(
2866 try fmt.out_buffer.ensureCapacity(source_code.len);2866 try fmt.out_buffer.ensureCapacity(source_code.len);
28672867
2868 try tree.renderToArrayList(&fmt.out_buffer);2868 try tree.renderToArrayList(&fmt.out_buffer);
2869 const anything_changed = mem.eql(u8, fmt.out_buffer.items, source_code);2869 if (mem.eql(u8, fmt.out_buffer.items, source_code))
2870 if (!anything_changed)
2871 return;2870 return;
28722871
2873 if (check_mode) {2872 if (check_mode) {