zig fmt: fix doc comments on fn paramaters without comma

This commit is contained in:
Kendall Condon 2025-07-28 16:39:20 -04:00
parent 11b89e7e75
commit c6845524ce
2 changed files with 64 additions and 6 deletions

View file

@ -1617,6 +1617,27 @@ fn renderBuiltinCall(
return renderParamList(r, builtin_token + 1, params, space); return renderParamList(r, builtin_token + 1, params, space);
} }
fn isOneLineFnProto(
tree: Ast,
fn_proto: Ast.full.FnProto,
lparen: Ast.TokenIndex,
rparen: Ast.TokenIndex,
) bool {
const trailing_comma = tree.tokenTag(rparen - 1) == .comma;
if (trailing_comma or hasComment(tree, lparen, rparen))
return false;
// Check that there are no doc comments
var after_last_param = lparen + 1;
for (fn_proto.ast.params) |expr| {
// Looking before each param is insufficient since anytype is not included in `params`
if (hasDocComment(tree, after_last_param, tree.firstToken(expr)))
return false;
after_last_param = tree.lastToken(expr) + 1;
}
return !hasDocComment(tree, after_last_param, rparen);
}
fn renderFnProto(r: *Render, fn_proto: Ast.full.FnProto, space: Space) Error!void { fn renderFnProto(r: *Render, fn_proto: Ast.full.FnProto, space: Space) Error!void {
const tree = r.tree; const tree = r.tree;
const ais = r.ais; const ais = r.ais;
@ -1677,8 +1698,7 @@ fn renderFnProto(r: *Render, fn_proto: Ast.full.FnProto, space: Space) Error!voi
// The params list is a sparse set that does *not* include anytype or ... parameters. // The params list is a sparse set that does *not* include anytype or ... parameters.
const trailing_comma = tree.tokenTag(rparen - 1) == .comma; if (isOneLineFnProto(tree, fn_proto, lparen, rparen)) {
if (!trailing_comma and !hasComment(tree, lparen, rparen)) {
// Render all on one line, no trailing comma. // Render all on one line, no trailing comma.
try renderToken(r, lparen, .none); // ( try renderToken(r, lparen, .none); // (
@ -1687,10 +1707,7 @@ fn renderFnProto(r: *Render, fn_proto: Ast.full.FnProto, space: Space) Error!voi
while (true) { while (true) {
last_param_token += 1; last_param_token += 1;
switch (tree.tokenTag(last_param_token)) { switch (tree.tokenTag(last_param_token)) {
.doc_comment => { .doc_comment => unreachable,
try renderToken(r, last_param_token, .newline);
continue;
},
.ellipsis3 => { .ellipsis3 => {
try renderToken(r, last_param_token, .none); // ... try renderToken(r, last_param_token, .none); // ...
break; break;

View file

@ -6448,6 +6448,47 @@ test "zig fmt: whitespace with multiline strings" {
); );
} }
test "zig fmt: doc comments on fn parameters" {
try testTransform(
\\extern fn foo(
\\ /// Bitmap
\\ active: u64
\\) void;
\\extern fn bar(
\\ bits: u6,
\\ /// Bitmap
\\ active: u64
\\) void;
\\extern fn baz(
\\ /// Bitmap
\\ active: anytype
\\) void;
\\
,
\\extern fn foo(
\\ /// Bitmap
\\ active: u64,
\\) void;
\\extern fn bar(
\\ bits: u6,
\\ /// Bitmap
\\ active: u64,
\\) void;
\\extern fn baz(
\\ /// Bitmap
\\ active: anytype,
\\) void;
\\
);
try testCanonical(
\\extern fn foo(x: struct {
\\ /// Bitmap
\\ active: u64,
\\}) void;
\\
);
}
test "recovery: top level" { test "recovery: top level" {
try testError( try testError(
\\test "" {inline} \\test "" {inline}