authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-25 01:03:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-25 01:03:15-04:00
logdfc3e11748615f10cf6958c61a934ef852b5aa94
treefe9791d6534372eb0972f7a2d522ef007b7c05cf
parentca49b6f6b4ffa3ff4324a45501eef98848a2d141

zig fmt: fix handling of comments at top of file


3 files changed, 42 insertions(+), 1 deletions(-)

std/zig/parse.zig+7
...@@ -41,6 +41,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -41,6 +41,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
41 }41 }
42 var tok_it = tree.tokens.iterator(0);42 var tok_it = tree.tokens.iterator(0);
4343
44 // skip over line comments at the top of the file
45 while (true) {
46 const next_tok = tok_it.peek() ?? break;
47 if (next_tok.id != Token.Id.LineComment) break;
48 _ = tok_it.next();
49 }
50
44 try stack.append(State.TopLevel);51 try stack.append(State.TopLevel);
4552
46 while (true) {53 while (true) {
std/zig/parser_test.zig+20
...@@ -1,3 +1,23 @@...@@ -1,3 +1,23 @@
1test "zig fmt: first thing in file is line comment" {
2 try testCanonical(
3 \\// Introspection and determination of system libraries needed by zig.
4 \\
5 \\// Introspection and determination of system libraries needed by zig.
6 \\
7 \\const std = @import("std");
8 \\
9 );
10}
11
12test "zig fmt: line comment after doc comment" {
13 try testCanonical(
14 \\/// doc comment
15 \\// line comment
16 \\fn foo() void {}
17 \\
18 );
19}
20
1test "zig fmt: float literal with exponent" {21test "zig fmt: float literal with exponent" {
2 try testCanonical(22 try testCanonical(
3 \\test "bit field alignment" {23 \\test "bit field alignment" {
std/zig/render.zig+15-1
...@@ -15,6 +15,20 @@ pub const Error = error{...@@ -15,6 +15,20 @@ pub const Error = error{
15pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(stream).Child.Error || Error)!void {15pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(stream).Child.Error || Error)!void {
16 comptime assert(@typeId(@typeOf(stream)) == builtin.TypeId.Pointer);16 comptime assert(@typeId(@typeOf(stream)) == builtin.TypeId.Pointer);
1717
18 // render all the line comments at the beginning of the file
19 var tok_it = tree.tokens.iterator(0);
20 while (tok_it.next()) |token| {
21 if (token.id != Token.Id.LineComment) break;
22 try stream.print("{}\n", tree.tokenSlicePtr(token));
23 if (tok_it.peek()) |next_token| {
24 const loc = tree.tokenLocationPtr(token.end, next_token);
25 if (loc.line >= 2) {
26 try stream.writeByte('\n');
27 }
28 }
29 }
30
31
18 var it = tree.root_node.decls.iterator(0);32 var it = tree.root_node.decls.iterator(0);
19 while (it.next()) |decl| {33 while (it.next()) |decl| {
20 try renderTopLevelDecl(allocator, stream, tree, 0, decl.*);34 try renderTopLevelDecl(allocator, stream, tree, 0, decl.*);
...@@ -1455,7 +1469,7 @@ fn renderDocComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@t...@@ -1455,7 +1469,7 @@ fn renderDocComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@t
1455 const comment = node.doc_comments ?? return;1469 const comment = node.doc_comments ?? return;
1456 var it = comment.lines.iterator(0);1470 var it = comment.lines.iterator(0);
1457 while (it.next()) |line_token_index| {1471 while (it.next()) |line_token_index| {
1458 try stream.print("{}\n", tree.tokenSlice(line_token_index.*));1472 try renderToken(tree, stream, line_token_index.*, indent, Space.Newline);
1459 try stream.writeByteNTimes(' ', indent);1473 try stream.writeByteNTimes(' ', indent);
1460 }1474 }
1461}1475}