Skip to content

Commit 530d175

Browse files
committed
zig fmt: fix spacing when moving doc comment on var decls
1 parent 0d1b473 commit 530d175

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

std/zig/parser_test.zig

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
test "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+
126
test "zig fmt: if-else with comment before else" {
227
try testCanonical(
328
\\comptime {
@@ -557,20 +582,6 @@ test "zig fmt: add comma on last switch prong" {
557582
);
558583
}
559584

560-
test "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-
574585
test "zig fmt: same-line comment after a statement" {
575586
try testCanonical(
576587
\\test "" {

std/zig/render.zig

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(
3939
}
4040

4141
fn renderExtraNewline(tree: &ast.Tree, stream: var, node: &ast.Node) !void {
42-
var first_token = node.firstToken();
43-
while (tree.tokens.at(first_token - 1).id == Token.Id.DocComment) {
44-
first_token -= 1;
42+
const first_token = node.firstToken();
43+
var prev_token = first_token;
44+
while (tree.tokens.at(prev_token - 1).id == Token.Id.DocComment) {
45+
prev_token -= 1;
4546
}
46-
const prev_token_end = tree.tokens.at(first_token - 1).end;
47+
const prev_token_end = tree.tokens.at(prev_token - 1).end;
4748
const loc = tree.tokenLocation(prev_token_end, first_token);
4849
if (loc.line >= 2) {
4950
try stream.writeByte('\n');
@@ -1715,7 +1716,17 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent
17151716
else => {},
17161717
}
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: {
17191730
switch (space) {
17201731
Space.None, Space.NoNewline => return,
17211732
Space.Newline => {
@@ -1739,7 +1750,6 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent
17391750
}
17401751

17411752
var loc = tree.tokenLocationPtr(token.end, next_token);
1742-
var offset: usize = 1;
17431753
if (loc.line == 0) {
17441754
try stream.print(" {}", mem.trimRight(u8, tree.tokenSlicePtr(next_token), " "));
17451755
offset = 2;
@@ -1820,8 +1830,15 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent
18201830
fn renderDocComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@typeOf(stream).Child.Error || Error)!void {
18211831
const comment = node.doc_comments ?? return;
18221832
var it = comment.lines.iterator(0);
1833+
const first_token = node.firstToken();
18231834
while (it.next()) |line_token_index| {
1824-
try renderToken(tree, stream, line_token_index.*, indent, Space.Newline);
1825-
try stream.writeByteNTimes(' ', indent);
1835+
if (line_token_index.* < first_token) {
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+
}
18261843
}
18271844
}

0 commit comments

Comments
 (0)