Add doc_comments to param decl

master
Shritesh Bhattarai 2019-03-27 10:58:25 -05:00
parent 85575704a4
commit efa751c339
4 changed files with 26 additions and 1 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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 {}

View File

@ -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);
}