Commit efa751c339

Shritesh Bhattarai <shritesh@shritesh.com>
2019-03-27 16:58:25
Add doc_comments to param decl
1 parent 8557570
std/zig/ast.zig
@@ -940,6 +940,7 @@ pub const Node = struct {
 
     pub const ParamDecl = struct {
         base: Node,
+        doc_comments: ?*DocComment,
         comptime_token: ?TokenIndex,
         noalias_token: ?TokenIndex,
         name_token: ?TokenIndex,
std/zig/parse.zig
@@ -854,12 +854,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
             },
 
             State.ParamDecl => |fn_proto| {
+                const comments = try eatDocComments(arena, &tok_it, &tree);
                 if (eatToken(&tok_it, &tree, Token.Id.RParen)) |_| {
                     continue;
                 }
                 const param_decl = try arena.create(ast.Node.ParamDecl);
                 param_decl.* = ast.Node.ParamDecl{
                     .base = ast.Node{ .id = ast.Node.Id.ParamDecl },
+                    .doc_comments = comments,
                     .comptime_token = null,
                     .noalias_token = null,
                     .name_token = null,
std/zig/parser_test.zig
@@ -75,6 +75,26 @@ test "zig fmt: correctly move doc comments on struct fields" {
     );
 }
 
+test "zig fmt: doc comments on param decl" {
+    try testCanonical(
+        \\pub const Allocator = struct {
+        \\    shrinkFn: fn (
+        \\        self: *Allocator,
+        \\        /// Guaranteed to be the same as what was returned from most recent call to
+        \\        /// `allocFn`, `reallocFn`, or `shrinkFn`.
+        \\        old_mem: []u8,
+        \\        /// Guaranteed to be the same as what was returned from most recent call to
+        \\        /// `allocFn`, `reallocFn`, or `shrinkFn`.
+        \\        old_alignment: u29,
+        \\        /// Guaranteed to be less than or equal to `old_mem.len`.
+        \\        new_byte_count: usize,
+        \\        /// Guaranteed to be less than or equal to `old_alignment`.
+        \\        new_alignment: u29,
+        \\    ) []u8,
+        \\};
+    );
+}
+
 test "zig fmt: preserve space between async fn definitions" {
     try testCanonical(
         \\async fn a() void {}
std/zig/render.zig
@@ -1137,7 +1137,7 @@ fn renderExpression(
                 var it = fn_proto.params.iterator(0);
                 while (it.next()) |param_decl_node| {
                     try stream.writeByteNTimes(' ', new_indent);
-                    try renderParamDecl(allocator, stream, tree, indent, start_col, param_decl_node.*, Space.Comma);
+                    try renderParamDecl(allocator, stream, tree, new_indent, start_col, param_decl_node.*, Space.Comma);
                 }
                 try stream.writeByteNTimes(' ', indent);
             }
@@ -1779,6 +1779,8 @@ fn renderParamDecl(
 ) (@typeOf(stream).Child.Error || Error)!void {
     const param_decl = @fieldParentPtr(ast.Node.ParamDecl, "base", base);
 
+    try renderDocComments(tree, stream, param_decl, indent, start_col);
+
     if (param_decl.comptime_token) |comptime_token| {
         try renderToken(tree, stream, comptime_token, indent, start_col, Space.Space);
     }