authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-23 18:23:49-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-23 18:23:49-07:00
log988f1c6a6f075e63d0ab2dc8f6e802a67b4a0902
tree556d86698b53d5be7708ded08c817ba4f28d318c
parent08107a555eb1bc8d6e9aa8d80507896d08b2b18d

zig fmt: fn proto end with anytype and comma

also zig fmt: space after top level doc comment

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

lib/std/zig/parse.zig+6-1
...@@ -3706,7 +3706,12 @@ const Parser = struct {...@@ -3706,7 +3706,12 @@ const Parser = struct {
3706 const param = try p.expectParamDecl();3706 const param = try p.expectParamDecl();
3707 if (param != 0) break param;3707 if (param != 0) break param;
3708 switch (p.token_tags[p.nextToken()]) {3708 switch (p.token_tags[p.nextToken()]) {
3709 .comma => continue,3709 .comma => {
3710 if (p.eatToken(.r_paren)) |_| {
3711 return SmallSpan{ .zero_or_one = 0 };
3712 }
3713 continue;
3714 },
3710 .r_paren => return SmallSpan{ .zero_or_one = 0 },3715 .r_paren => return SmallSpan{ .zero_or_one = 0 },
3711 else => {3716 else => {
3712 // This is likely just a missing comma;3717 // This is likely just a missing comma;
lib/std/zig/parser_test.zig+18
...@@ -4134,6 +4134,24 @@ test "zig fmt: function params should align nicely" {...@@ -4134,6 +4134,24 @@ test "zig fmt: function params should align nicely" {
4134 );4134 );
4135}4135}
41364136
4137test "zig fmt: fn proto end with anytype and comma" {
4138 try testCanonical(
4139 \\pub fn format(
4140 \\ out_stream: anytype,
4141 \\) !void {}
4142 \\
4143 );
4144}
4145
4146test "zig fmt: space after top level doc comment" {
4147 try testCanonical(
4148 \\//! top level doc comment
4149 \\
4150 \\field: i32,
4151 \\
4152 );
4153}
4154
4137test "zig fmt: error for invalid bit range" {4155test "zig fmt: error for invalid bit range" {
4138 try testError(4156 try testError(
4139 \\var x: []align(0:0:0)u8 = bar;4157 \\var x: []align(0:0:0)u8 = bar;
lib/std/zig/render.zig+10
...@@ -1449,6 +1449,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full....@@ -1449,6 +1449,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
1449 .identifier => {},1449 .identifier => {},
1450 .keyword_anytype => {1450 .keyword_anytype => {
1451 try renderToken(ais, tree, last_param_token, .comma); // anytype1451 try renderToken(ais, tree, last_param_token, .comma); // anytype
1452 if (token_tags[last_param_token + 1] == .comma)
1453 last_param_token += 1;
1452 continue;1454 continue;
1453 },1455 },
1454 .r_paren => break,1456 .r_paren => break,
...@@ -1462,6 +1464,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full....@@ -1462,6 +1464,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
1462 }1464 }
1463 if (token_tags[last_param_token] == .keyword_anytype) {1465 if (token_tags[last_param_token] == .keyword_anytype) {
1464 try renderToken(ais, tree, last_param_token, .comma); // anytype1466 try renderToken(ais, tree, last_param_token, .comma); // anytype
1467 if (token_tags[last_param_token + 1] == .comma)
1468 last_param_token += 1;
1465 continue;1469 continue;
1466 }1470 }
1467 const param = fn_proto.ast.params[param_i];1471 const param = fn_proto.ast.params[param_i];
...@@ -2363,6 +2367,12 @@ fn renderContainerDocComments(ais: *Ais, tree: ast.Tree, start_token: ast.TokenI...@@ -2363,6 +2367,12 @@ fn renderContainerDocComments(ais: *Ais, tree: ast.Tree, start_token: ast.TokenI
2363 while (token_tags[tok] == .container_doc_comment) : (tok += 1) {2367 while (token_tags[tok] == .container_doc_comment) : (tok += 1) {
2364 try renderToken(ais, tree, tok, .newline);2368 try renderToken(ais, tree, tok, .newline);
2365 }2369 }
2370 // Render extra newline if there is one between final container doc comment and
2371 // the next token. If the next token is a doc comment, that code path
2372 // will have its own logic to insert a newline.
2373 if (token_tags[tok] != .doc_comment) {
2374 try renderExtraNewlineToken(ais, tree, tok);
2375 }
2366}2376}
23672377
2368fn tokenSliceForRender(tree: ast.Tree, token_index: ast.TokenIndex) []const u8 {2378fn tokenSliceForRender(tree: ast.Tree, token_index: ast.TokenIndex) []const u8 {