authorgravatar for yujiri@disroot.orgyujiri8 <yujiri@disroot.org> 2022-09-02 14:12:20-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-09-02 20:12:20+02:00
log10e11b60e56941cb664648dcebfd4db3d2efed30
treea0339837da59c1b49c47fc76991312b2e99f2a59
parent4a08c6dd51ea41cc560dd30d51991d3d0c36ff6a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

zig fmt: don't delete container doc comments

Fixes #12617

2 files changed, 21 insertions(+), 3 deletions(-)

lib/std/zig/parser_test.zig+15
......@@ -5057,6 +5057,21 @@ test "zig fmt: make single-line if no trailing comma" {
50575057 );
50585058}
50595059
5060test "zig fmt: preserve container doc comment in container without trailing comma" {
5061 try testTransform(
5062 \\const A = enum(u32) {
5063 \\//! comment
5064 \\_ };
5065 \\
5066 ,
5067 \\const A = enum(u32) {
5068 \\ //! comment
5069 \\ _,
5070 \\};
5071 \\
5072 );
5073}
5074
50605075test "zig fmt: make single-line if no trailing comma" {
50615076 try testCanonical(
50625077 \\// Test trailing comma syntax
lib/std/zig/render.zig+6-3
......@@ -1933,12 +1933,15 @@ fn renderContainerDecl(
19331933 break :one_line;
19341934 }
19351935
1936 // 2. A member of the container has a doc comment.
1936 // 2. The container has a container comment.
1937 if (token_tags[lbrace + 1] == .container_doc_comment) break :one_line;
1938
1939 // 3. A member of the container has a doc comment.
19371940 for (token_tags[lbrace + 1 .. rbrace - 1]) |tag| {
19381941 if (tag == .doc_comment) break :one_line;
19391942 }
19401943
1941 // 3. The container has non-field members.
1944 // 4. The container has non-field members.
19421945 for (container_decl.ast.members) |member| {
19431946 if (!node_tags[member].isContainerField()) break :one_line;
19441947 }
......@@ -2358,7 +2361,7 @@ fn renderSpace(ais: *Ais, tree: Ast, token_index: Ast.TokenIndex, lexeme_len: us
23582361 }
23592362}
23602363
2361/// Returns true if there exists a comment between any of the tokens from
2364/// Returns true if there exists a line comment between any of the tokens from
23622365/// `start_token` to `end_token`. This is used to determine if e.g. a
23632366/// fn_proto should be wrapped and have a trailing comma inserted even if
23642367/// there is none in the source.