Commit 988f1c6a6f

Andrew Kelley <andrew@ziglang.org>
2021-02-24 02:23:49
zig fmt: fn proto end with anytype and comma
also zig fmt: space after top level doc comment
1 parent 08107a5
Changed files (3)
lib/std/zig/parse.zig
@@ -3706,7 +3706,12 @@ const Parser = struct {
             const param = try p.expectParamDecl();
             if (param != 0) break param;
             switch (p.token_tags[p.nextToken()]) {
-                .comma => continue,
+                .comma => {
+                    if (p.eatToken(.r_paren)) |_| {
+                        return SmallSpan{ .zero_or_one = 0 };
+                    }
+                    continue;
+                },
                 .r_paren => return SmallSpan{ .zero_or_one = 0 },
                 else => {
                     // This is likely just a missing comma;
lib/std/zig/parser_test.zig
@@ -4134,6 +4134,24 @@ test "zig fmt: function params should align nicely" {
     );
 }
 
+test "zig fmt: fn proto end with anytype and comma" {
+    try testCanonical(
+        \\pub fn format(
+        \\    out_stream: anytype,
+        \\) !void {}
+        \\
+    );
+}
+
+test "zig fmt: space after top level doc comment" {
+    try testCanonical(
+        \\//! top level doc comment
+        \\
+        \\field: i32,
+        \\
+    );
+}
+
 test "zig fmt: error for invalid bit range" {
     try testError(
         \\var x: []align(0:0:0)u8 = bar;
lib/std/zig/render.zig
@@ -1449,6 +1449,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
                 .identifier => {},
                 .keyword_anytype => {
                     try renderToken(ais, tree, last_param_token, .comma); // anytype
+                    if (token_tags[last_param_token + 1] == .comma)
+                        last_param_token += 1;
                     continue;
                 },
                 .r_paren => break,
@@ -1462,6 +1464,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
             }
             if (token_tags[last_param_token] == .keyword_anytype) {
                 try renderToken(ais, tree, last_param_token, .comma); // anytype
+                if (token_tags[last_param_token + 1] == .comma)
+                    last_param_token += 1;
                 continue;
             }
             const param = fn_proto.ast.params[param_i];
@@ -2363,6 +2367,12 @@ fn renderContainerDocComments(ais: *Ais, tree: ast.Tree, start_token: ast.TokenI
     while (token_tags[tok] == .container_doc_comment) : (tok += 1) {
         try renderToken(ais, tree, tok, .newline);
     }
+    // Render extra newline if there is one between final container doc comment and
+    // the next token. If the next token is a doc comment, that code path
+    // will have its own logic to insert a newline.
+    if (token_tags[tok] != .doc_comment) {
+        try renderExtraNewlineToken(ais, tree, tok);
+    }
 }
 
 fn tokenSliceForRender(tree: ast.Tree, token_index: ast.TokenIndex) []const u8 {