authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-22 18:30:51+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-22 18:32:37+01:00
logf3ee10b4547bf5f03a4728b0d48b22bad6c9a5b1
tree9c69673212de2f57f5dddee0a15abe4c140c2ea1
parent011bc1b84fc0ea1147cc96ccd30962bc38b65e02
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: fix comments ending with EOF after decls

Achieve this by reducing the amount of special casing to handle EOF so that the already correct logic for normal comments does not need to be duplicated.

3 files changed, 25 insertions(+), 18 deletions(-)

lib/std/zig/parser_test.zig+11
......@@ -93,6 +93,17 @@ test "zig fmt: file ends in comment" {
9393 );
9494}
9595
96test "zig fmt: file ends in comment after var decl" {
97 try testTransform(
98 \\const x = 42;
99 \\ //foobar
100 ,
101 \\const x = 42;
102 \\//foobar
103 \\
104 );
105}
106
96107test "zig fmt: doc comments on test" {
97108 try testCanonical(
98109 \\/// hello
lib/std/zig/render.zig+12-18
......@@ -26,10 +26,7 @@ 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 = if (tree.tokens.items(.tag)[0] == .eof)
30 tree.source.len
31 else
32 tree.tokens.items(.start)[0];
29 const comment_end_loc = tree.tokens.items(.start)[0];
3330 _ = try renderComments(ais, tree, 0, comment_end_loc);
3431
3532 try renderMembers(ais, tree, tree.rootDecls());
......@@ -1609,15 +1606,18 @@ fn renderArrayInit(
16091606 } else {
16101607 try renderExpression(ais, tree, array_init.ast.type_expr, .none); // T
16111608 }
1609
16121610 if (array_init.ast.elements.len == 0) {
16131611 ais.pushIndentNextLine();
16141612 try renderToken(ais, tree, array_init.ast.lbrace, .none); // lbrace
16151613 ais.popIndent();
16161614 return renderToken(ais, tree, array_init.ast.lbrace + 1, space); // rbrace
16171615 }
1616
16181617 const last_elem = array_init.ast.elements[array_init.ast.elements.len - 1];
16191618 const last_elem_token = tree.lastToken(last_elem);
1620 if (token_tags[last_elem_token + 1] == .comma) {
1619 const trailing_comma = token_tags[last_elem_token + 1] == .comma;
1620 if (trailing_comma) {
16211621 // Render one element per line.
16221622 ais.pushIndentNextLine();
16231623 try renderToken(ais, tree, array_init.ast.lbrace, .newline);
......@@ -2001,18 +2001,12 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
20012001 var rendered_empty_comments = false;
20022002 while (mem.indexOf(u8, tree.source[index..end], "//")) |offset| {
20032003 const comment_start = index + offset;
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;
2015 const untrimmed_comment = tree.source[comment_start..newline];
2004
2005 // If there is no newline, the comment ends with EOF
2006 const newline_index = mem.indexOfScalar(u8, tree.source[comment_start..end], '\n');
2007 const newline = if (newline_index) |i| comment_start + i else null;
2008
2009 const untrimmed_comment = tree.source[comment_start .. newline orelse tree.source.len];
20162010 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces);
20172011
20182012 // Don't leave any whitespace at the start of the file
......@@ -2041,7 +2035,7 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
20412035 try ais.writer().print("{s}\n", .{trimmed_comment});
20422036 rendered_empty_comments = false;
20432037 }
2044 index = newline + 1;
2038 index = 1 + (newline orelse return true);
20452039
20462040 if (ais.disabled_offset) |disabled_offset| {
20472041 if (mem.eql(u8, trimmed_comment, "// zig fmt: on")) {
lib/std/zig/tokenizer.zig+2
......@@ -1444,6 +1444,7 @@ pub const Tokenizer = struct {
14441444 self.pending_invalid_token = null;
14451445 return token;
14461446 }
1447 result.loc.start = self.index;
14471448 }
14481449
14491450 result.loc.end = self.index;
......@@ -2055,4 +2056,5 @@ fn testTokenize(source: []const u8, expected_tokens: []const Token.Tag) void {
20552056 }
20562057 const last_token = tokenizer.next();
20572058 std.testing.expect(last_token.tag == .eof);
2059 std.testing.expect(last_token.loc.start == source.len);
20582060}