zig fmt: don't delete container doc comments

Fixes #12617
This commit is contained in:
yujiri8 2022-09-02 14:12:20 -04:00 committed by GitHub
parent 4a08c6dd51
commit 10e11b60e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -5057,6 +5057,21 @@ test "zig fmt: make single-line if no trailing comma" {
);
}
test "zig fmt: preserve container doc comment in container without trailing comma" {
try testTransform(
\\const A = enum(u32) {
\\//! comment
\\_ };
\\
,
\\const A = enum(u32) {
\\ //! comment
\\ _,
\\};
\\
);
}
test "zig fmt: make single-line if no trailing comma" {
try testCanonical(
\\// Test trailing comma syntax

View File

@ -1933,12 +1933,15 @@ fn renderContainerDecl(
break :one_line;
}
// 2. A member of the container has a doc comment.
// 2. The container has a container comment.
if (token_tags[lbrace + 1] == .container_doc_comment) break :one_line;
// 3. A member of the container has a doc comment.
for (token_tags[lbrace + 1 .. rbrace - 1]) |tag| {
if (tag == .doc_comment) break :one_line;
}
// 3. The container has non-field members.
// 4. The container has non-field members.
for (container_decl.ast.members) |member| {
if (!node_tags[member].isContainerField()) break :one_line;
}
@ -2358,7 +2361,7 @@ fn renderSpace(ais: *Ais, tree: Ast, token_index: Ast.TokenIndex, lexeme_len: us
}
}
/// Returns true if there exists a comment between any of the tokens from
/// Returns true if there exists a line comment between any of the tokens from
/// `start_token` to `end_token`. This is used to determine if e.g. a
/// fn_proto should be wrapped and have a trailing comma inserted even if
/// there is none in the source.