authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-28 23:41:09-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-28 23:41:09-04:00
log530d17542207151f768a14a0cc8311181599b9b9
tree533ac0d915596239c8f7b0df0e9d573b0a276b30
parent0d1b47362c7623ba923a6831dab0989133caf9ab

zig fmt: fix spacing when moving doc comment on var decls


2 files changed, 50 insertions(+), 22 deletions(-)

std/zig/parser_test.zig+25-14
...@@ -1,3 +1,28 @@...@@ -1,3 +1,28 @@
1test "zig fmt: same-line doc comment on variable declaration" {
2 try testTransform(
3 \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
4 \\pub const MAP_FILE = 0x0000; /// map from file (default)
5 \\
6 \\pub const EMEDIUMTYPE = 124; /// Wrong medium type
7 \\
8 \\// nameserver query return codes
9 \\pub const ENSROK = 0; /// DNS server returned answer with no data
10 ,
11 \\/// allocated from memory, swap space
12 \\pub const MAP_ANONYMOUS = 0x1000;
13 \\/// map from file (default)
14 \\pub const MAP_FILE = 0x0000;
15 \\
16 \\/// Wrong medium type
17 \\pub const EMEDIUMTYPE = 124;
18 \\
19 \\// nameserver query return codes
20 \\/// DNS server returned answer with no data
21 \\pub const ENSROK = 0;
22 \\
23 );
24}
25
1test "zig fmt: if-else with comment before else" {26test "zig fmt: if-else with comment before else" {
2 try testCanonical(27 try testCanonical(
3 \\comptime {28 \\comptime {
...@@ -557,20 +582,6 @@ test "zig fmt: add comma on last switch prong" {...@@ -557,20 +582,6 @@ test "zig fmt: add comma on last switch prong" {
557 );582 );
558}583}
559584
560test "zig fmt: same-line doc comment on variable declaration" {
561 try testTransform(
562 \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
563 \\pub const MAP_FILE = 0x0000; /// map from file (default)
564 \\
565 ,
566 \\/// allocated from memory, swap space
567 \\pub const MAP_ANONYMOUS = 0x1000;
568 \\/// map from file (default)
569 \\pub const MAP_FILE = 0x0000;
570 \\
571 );
572}
573
574test "zig fmt: same-line comment after a statement" {585test "zig fmt: same-line comment after a statement" {
575 try testCanonical(586 try testCanonical(
576 \\test "" {587 \\test "" {
std/zig/render.zig+25-8
...@@ -39,11 +39,12 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(...@@ -39,11 +39,12 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(
39}39}
4040
41fn renderExtraNewline(tree: &ast.Tree, stream: var, node: &ast.Node) !void {41fn renderExtraNewline(tree: &ast.Tree, stream: var, node: &ast.Node) !void {
42 var first_token = node.firstToken();42 const first_token = node.firstToken();
43 while (tree.tokens.at(first_token - 1).id == Token.Id.DocComment) {43 var prev_token = first_token;
44 first_token -= 1;44 while (tree.tokens.at(prev_token - 1).id == Token.Id.DocComment) {
45 prev_token -= 1;
45 }46 }
46 const prev_token_end = tree.tokens.at(first_token - 1).end;47 const prev_token_end = tree.tokens.at(prev_token - 1).end;
47 const loc = tree.tokenLocation(prev_token_end, first_token);48 const loc = tree.tokenLocation(prev_token_end, first_token);
48 if (loc.line >= 2) {49 if (loc.line >= 2) {
49 try stream.writeByte('\n');50 try stream.writeByte('\n');
...@@ -1715,7 +1716,17 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent...@@ -1715,7 +1716,17 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent
1715 else => {},1716 else => {},
1716 }1717 }
17171718
1718 if (next_token.id != Token.Id.LineComment) {1719 // Skip over same line doc comments
1720 var offset: usize = 1;
1721 if (next_token.id == Token.Id.DocComment) {
1722 const loc = tree.tokenLocationPtr(token.end, next_token);
1723 if (loc.line == 0) {
1724 offset += 1;
1725 next_token = tree.tokens.at(token_index + offset);
1726 }
1727 }
1728
1729 if (next_token.id != Token.Id.LineComment) blk: {
1719 switch (space) {1730 switch (space) {
1720 Space.None, Space.NoNewline => return,1731 Space.None, Space.NoNewline => return,
1721 Space.Newline => {1732 Space.Newline => {
...@@ -1739,7 +1750,6 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent...@@ -1739,7 +1750,6 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent
1739 }1750 }
17401751
1741 var loc = tree.tokenLocationPtr(token.end, next_token);1752 var loc = tree.tokenLocationPtr(token.end, next_token);
1742 var offset: usize = 1;
1743 if (loc.line == 0) {1753 if (loc.line == 0) {
1744 try stream.print(" {}", mem.trimRight(u8, tree.tokenSlicePtr(next_token), " "));1754 try stream.print(" {}", mem.trimRight(u8, tree.tokenSlicePtr(next_token), " "));
1745 offset = 2;1755 offset = 2;
...@@ -1820,8 +1830,15 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent...@@ -1820,8 +1830,15 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent
1820fn renderDocComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@typeOf(stream).Child.Error || Error)!void {1830fn renderDocComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@typeOf(stream).Child.Error || Error)!void {
1821 const comment = node.doc_comments ?? return;1831 const comment = node.doc_comments ?? return;
1822 var it = comment.lines.iterator(0);1832 var it = comment.lines.iterator(0);
1833 const first_token = node.firstToken();
1823 while (it.next()) |line_token_index| {1834 while (it.next()) |line_token_index| {
1824 try renderToken(tree, stream, line_token_index.*, indent, Space.Newline);1835 if (line_token_index.* < first_token) {
1825 try stream.writeByteNTimes(' ', indent);1836 try renderToken(tree, stream, line_token_index.*, indent, Space.Newline);
1837 try stream.writeByteNTimes(' ', indent);
1838 } else {
1839 try renderToken(tree, stream, line_token_index.*, indent, Space.NoComment);
1840 try stream.write("\n");
1841 try stream.writeByteNTimes(' ', indent);
1842 }
1826 }1843 }
1827}1844}