authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 17:37:02-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 17:37:15-04:00
logc03b9010db55e52dfa227b17e35203b93b5ee1df
tree06be415170adf4ea9f500026f115603386f729a5
parent2387292f204c59259fc64d7c960d201e808af5a9

zig fmt: preserve same-line comment after statement


2 files changed, 68 insertions(+), 14 deletions(-)

std/zig/ast.zig+2-1
......@@ -6,7 +6,8 @@ const mem = std.mem;
66
77pub const Node = struct {
88 id: Id,
9 comments: ?&LineComment,
9 before_comments: ?&LineComment,
10 same_line_comment: ?&Token,
1011
1112 pub const Id = enum {
1213 // Top level
std/zig/parser.zig+66-13
......@@ -229,6 +229,7 @@ pub const Parser = struct {
229229 ComptimeStatement: ComptimeStatementCtx,
230230 Semicolon: &&ast.Node,
231231 AddComments: AddCommentsCtx,
232 LookForSameLineComment: &&ast.Node,
232233
233234 AsmOutputItems: &ArrayList(&ast.Node.AsmOutput),
234235 AsmOutputReturnOrType: &ast.Node.AsmOutput,
......@@ -356,7 +357,8 @@ pub const Parser = struct {
356357 const block = try arena.construct(ast.Node.Block {
357358 .base = ast.Node {
358359 .id = ast.Node.Id.Block,
359 .comments = null,
360 .before_comments = null,
361 .same_line_comment = null,
360362 },
361363 .label = null,
362364 .lbrace = undefined,
......@@ -366,7 +368,8 @@ pub const Parser = struct {
366368 const test_node = try arena.construct(ast.Node.TestDecl {
367369 .base = ast.Node {
368370 .id = ast.Node.Id.TestDecl,
369 .comments = comments,
371 .before_comments = comments,
372 .same_line_comment = null,
370373 },
371374 .test_token = token,
372375 .name = undefined,
......@@ -547,7 +550,8 @@ pub const Parser = struct {
547550 const fn_proto = try arena.construct(ast.Node.FnProto {
548551 .base = ast.Node {
549552 .id = ast.Node.Id.FnProto,
550 .comments = ctx.comments,
553 .before_comments = ctx.comments,
554 .same_line_comment = null,
551555 },
552556 .visib_token = ctx.visib_token,
553557 .name_token = null,
......@@ -812,7 +816,8 @@ pub const Parser = struct {
812816 const var_decl = try arena.construct(ast.Node.VarDecl {
813817 .base = ast.Node {
814818 .id = ast.Node.Id.VarDecl,
815 .comments = ctx.comments,
819 .before_comments = ctx.comments,
820 .same_line_comment = null,
816821 },
817822 .visib_token = ctx.visib_token,
818823 .mut_token = ctx.mut_token,
......@@ -1242,7 +1247,8 @@ pub const Parser = struct {
12421247 const node = try arena.construct(ast.Node.Defer {
12431248 .base = ast.Node {
12441249 .id = ast.Node.Id.Defer,
1245 .comments = comments,
1250 .before_comments = comments,
1251 .same_line_comment = null,
12461252 },
12471253 .defer_token = token,
12481254 .kind = switch (token.id) {
......@@ -1275,7 +1281,8 @@ pub const Parser = struct {
12751281 else => {
12761282 self.putBackToken(token);
12771283 const statement = try block.statements.addOne();
1278 stack.append(State { .Semicolon = statement }) catch unreachable;
1284 stack.append(State { .LookForSameLineComment = statement }) catch unreachable;
1285 try stack.append(State { .Semicolon = statement });
12791286 try stack.append(State { .AddComments = AddCommentsCtx {
12801287 .node_ptr = statement,
12811288 .comments = comments,
......@@ -1324,7 +1331,28 @@ pub const Parser = struct {
13241331
13251332 State.AddComments => |add_comments_ctx| {
13261333 const node = *add_comments_ctx.node_ptr;
1327 node.comments = add_comments_ctx.comments;
1334 node.before_comments = add_comments_ctx.comments;
1335 continue;
1336 },
1337
1338 State.LookForSameLineComment => |node_ptr| {
1339 const node = *node_ptr;
1340 const node_last_token = node.lastToken();
1341
1342 const line_comment_token = self.getNextToken();
1343 if (line_comment_token.id != Token.Id.LineComment) {
1344 self.putBackToken(line_comment_token);
1345 continue;
1346 }
1347
1348 const offset_loc = self.tokenizer.getTokenLocation(node_last_token.end, line_comment_token);
1349 const different_line = offset_loc.line != 0;
1350 if (different_line) {
1351 self.putBackToken(line_comment_token);
1352 continue;
1353 }
1354
1355 node.same_line_comment = try arena.construct(line_comment_token);
13281356 continue;
13291357 },
13301358
......@@ -1614,7 +1642,8 @@ pub const Parser = struct {
16141642 const fn_proto = try arena.construct(ast.Node.FnProto {
16151643 .base = ast.Node {
16161644 .id = ast.Node.Id.FnProto,
1617 .comments = ctx.comments,
1645 .before_comments = ctx.comments,
1646 .same_line_comment = null,
16181647 },
16191648 .visib_token = null,
16201649 .name_token = null,
......@@ -2585,7 +2614,8 @@ pub const Parser = struct {
25852614 const fn_proto = try arena.construct(ast.Node.FnProto {
25862615 .base = ast.Node {
25872616 .id = ast.Node.Id.FnProto,
2588 .comments = null,
2617 .before_comments = null,
2618 .same_line_comment = null,
25892619 },
25902620 .visib_token = null,
25912621 .name_token = null,
......@@ -2608,7 +2638,8 @@ pub const Parser = struct {
26082638 const fn_proto = try arena.construct(ast.Node.FnProto {
26092639 .base = ast.Node {
26102640 .id = ast.Node.Id.FnProto,
2611 .comments = null,
2641 .before_comments = null,
2642 .same_line_comment = null,
26122643 },
26132644 .visib_token = null,
26142645 .name_token = null,
......@@ -2788,7 +2819,8 @@ pub const Parser = struct {
27882819 const comment_node = try arena.construct(ast.Node.LineComment {
27892820 .base = ast.Node {
27902821 .id = ast.Node.Id.LineComment,
2791 .comments = null,
2822 .before_comments = null,
2823 .same_line_comment = null,
27922824 },
27932825 .lines = ArrayList(Token).init(arena),
27942826 });
......@@ -3134,7 +3166,11 @@ pub const Parser = struct {
31343166 *node = *init_to;
31353167 node.base = blk: {
31363168 const id = ast.Node.typeToId(T);
3137 break :blk ast.Node {.id = id, .comments = null};
3169 break :blk ast.Node {
3170 .id = id,
3171 .before_comments = null,
3172 .same_line_comment = null,
3173 };
31383174 };
31393175
31403176 return node;
......@@ -3270,6 +3306,7 @@ pub const Parser = struct {
32703306 FieldInitializer: &ast.Node.FieldInitializer,
32713307 PrintIndent,
32723308 Indent: usize,
3309 PrintSameLineComment: ?&Token,
32733310 };
32743311
32753312 pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void {
......@@ -4370,6 +4407,7 @@ pub const Parser = struct {
43704407 },
43714408 RenderState.Statement => |base| {
43724409 try self.renderComments(stream, base, indent);
4410 try stack.append(RenderState { .PrintSameLineComment = base.same_line_comment } );
43734411 switch (base.id) {
43744412 ast.Node.Id.VarDecl => {
43754413 const var_decl = @fieldParentPtr(ast.Node.VarDecl, "base", base);
......@@ -4385,12 +4423,16 @@ pub const Parser = struct {
43854423 },
43864424 RenderState.Indent => |new_indent| indent = new_indent,
43874425 RenderState.PrintIndent => try stream.writeByteNTimes(' ', indent),
4426 RenderState.PrintSameLineComment => |maybe_comment| blk: {
4427 const comment_token = maybe_comment ?? break :blk;
4428 try stream.print(" {}", self.tokenizer.getTokenSlice(comment_token));
4429 },
43884430 }
43894431 }
43904432 }
43914433
43924434 fn renderComments(self: &Parser, stream: var, node: &ast.Node, indent: usize) !void {
4393 const comment = node.comments ?? return;
4435 const comment = node.before_comments ?? return;
43944436 for (comment.lines.toSliceConst()) |line_token| {
43954437 try stream.print("{}\n", self.tokenizer.getTokenSlice(line_token));
43964438 try stream.writeByteNTimes(' ', indent);
......@@ -4471,6 +4513,17 @@ fn testCanonical(source: []const u8) !void {
44714513 }
44724514}
44734515
4516test "zig fmt: preserve same-line comment after a statement" {
4517 try testCanonical(
4518 \\test "" {
4519 \\ a = b;
4520 \\ debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption
4521 \\ a = b;
4522 \\}
4523 \\
4524 );
4525}
4526
44744527test "zig fmt: preserve comments before global variables" {
44754528 try testCanonical(
44764529 \\/// Foo copies keys and values before they go into the map, and