From efa751c3390cab0b5742051e02809d2ccfc62cc8 Mon Sep 17 00:00:00 2001 From: Shritesh Bhattarai Date: Wed, 27 Mar 2019 10:58:25 -0500 Subject: [PATCH] Add doc_comments to param decl --- std/zig/ast.zig | 1 + std/zig/parse.zig | 2 ++ std/zig/parser_test.zig | 20 ++++++++++++++++++++ std/zig/render.zig | 4 +++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/std/zig/ast.zig b/std/zig/ast.zig index 7149d4b3e..9aba59f77 100644 --- a/std/zig/ast.zig +++ b/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, diff --git a/std/zig/parse.zig b/std/zig/parse.zig index 93d15975f..5214a0575 100644 --- a/std/zig/parse.zig +++ b/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, diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index 3be6b8806..371795394 100644 --- a/std/zig/parser_test.zig +++ b/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 {} diff --git a/std/zig/render.zig b/std/zig/render.zig index 1179eaa41..f1b598272 100644 --- a/std/zig/render.zig +++ b/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); }