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" {...@@ -93,6 +93,17 @@ test "zig fmt: file ends in comment" {
93 );93 );
94}94}
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
96test "zig fmt: doc comments on test" {107test "zig fmt: doc comments on test" {
97 try testCanonical(108 try testCanonical(
98 \\/// hello109 \\/// hello
lib/std/zig/render.zig+12-18
...@@ -26,10 +26,7 @@ pub fn renderTree(buffer: *std.ArrayList(u8), tree: ast.Tree) Error!void {...@@ -26,10 +26,7 @@ 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 = if (tree.tokens.items(.tag)[0] == .eof)29 const comment_end_loc = tree.tokens.items(.start)[0];
30 tree.source.len
31 else
32 tree.tokens.items(.start)[0];
33 _ = try renderComments(ais, tree, 0, comment_end_loc);30 _ = try renderComments(ais, tree, 0, comment_end_loc);
3431
35 try renderMembers(ais, tree, tree.rootDecls());32 try renderMembers(ais, tree, tree.rootDecls());
...@@ -1609,15 +1606,18 @@ fn renderArrayInit(...@@ -1609,15 +1606,18 @@ fn renderArrayInit(
1609 } else {1606 } else {
1610 try renderExpression(ais, tree, array_init.ast.type_expr, .none); // T1607 try renderExpression(ais, tree, array_init.ast.type_expr, .none); // T
1611 }1608 }
1609
1612 if (array_init.ast.elements.len == 0) {1610 if (array_init.ast.elements.len == 0) {
1613 ais.pushIndentNextLine();1611 ais.pushIndentNextLine();
1614 try renderToken(ais, tree, array_init.ast.lbrace, .none); // lbrace1612 try renderToken(ais, tree, array_init.ast.lbrace, .none); // lbrace
1615 ais.popIndent();1613 ais.popIndent();
1616 return renderToken(ais, tree, array_init.ast.lbrace + 1, space); // rbrace1614 return renderToken(ais, tree, array_init.ast.lbrace + 1, space); // rbrace
1617 }1615 }
1616
1618 const last_elem = array_init.ast.elements[array_init.ast.elements.len - 1];1617 const last_elem = array_init.ast.elements[array_init.ast.elements.len - 1];
1619 const last_elem_token = tree.lastToken(last_elem);1618 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) {
1621 // Render one element per line.1621 // Render one element per line.
1622 ais.pushIndentNextLine();1622 ais.pushIndentNextLine();
1623 try renderToken(ais, tree, array_init.ast.lbrace, .newline);1623 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...@@ -2001,18 +2001,12 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
2001 var rendered_empty_comments = false;2001 var rendered_empty_comments = false;
2002 while (mem.indexOf(u8, tree.source[index..end], "//")) |offset| {2002 while (mem.indexOf(u8, tree.source[index..end], "//")) |offset| {
2003 const comment_start = index + offset;2003 const comment_start = index + offset;
2004 const newline_index = mem.indexOfScalar(u8, tree.source[comment_start..end], '\n') orelse {2004
2005 // comment ends in EOF.2005 // If there is no newline, the comment ends with EOF
2006 const untrimmed_comment = tree.source[comment_start..];2006 const newline_index = mem.indexOfScalar(u8, tree.source[comment_start..end], '\n');
2007 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces);2007 const newline = if (newline_index) |i| comment_start + i else null;
2008 if (trimmed_comment.len != 2) {2008
2009 try ais.writer().print("{s}\n", .{trimmed_comment});2009 const untrimmed_comment = tree.source[comment_start .. newline orelse tree.source.len];
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];
2016 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces);2010 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces);
20172011
2018 // Don't leave any whitespace at the start of the file2012 // 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...@@ -2041,7 +2035,7 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
2041 try ais.writer().print("{s}\n", .{trimmed_comment});2035 try ais.writer().print("{s}\n", .{trimmed_comment});
2042 rendered_empty_comments = false;2036 rendered_empty_comments = false;
2043 }2037 }
2044 index = newline + 1;2038 index = 1 + (newline orelse return true);
20452039
2046 if (ais.disabled_offset) |disabled_offset| {2040 if (ais.disabled_offset) |disabled_offset| {
2047 if (mem.eql(u8, trimmed_comment, "// zig fmt: on")) {2041 if (mem.eql(u8, trimmed_comment, "// zig fmt: on")) {
lib/std/zig/tokenizer.zig+2
...@@ -1444,6 +1444,7 @@ pub const Tokenizer = struct {...@@ -1444,6 +1444,7 @@ pub const Tokenizer = struct {
1444 self.pending_invalid_token = null;1444 self.pending_invalid_token = null;
1445 return token;1445 return token;
1446 }1446 }
1447 result.loc.start = self.index;
1447 }1448 }
14481449
1449 result.loc.end = self.index;1450 result.loc.end = self.index;
...@@ -2055,4 +2056,5 @@ fn testTokenize(source: []const u8, expected_tokens: []const Token.Tag) void {...@@ -2055,4 +2056,5 @@ fn testTokenize(source: []const u8, expected_tokens: []const Token.Tag) void {
2055 }2056 }
2056 const last_token = tokenizer.next();2057 const last_token = tokenizer.next();
2057 std.testing.expect(last_token.tag == .eof);2058 std.testing.expect(last_token.tag == .eof);
2059 std.testing.expect(last_token.loc.start == source.len);
2058}2060}