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" {...@@ -5057,6 +5057,21 @@ test "zig fmt: make single-line if no trailing comma" {
5057 );5057 );
5058}5058}
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
5060test "zig fmt: make single-line if no trailing comma" {5075test "zig fmt: make single-line if no trailing comma" {
5061 try testCanonical(5076 try testCanonical(
5062 \\// Test trailing comma syntax5077 \\// Test trailing comma syntax
lib/std/zig/render.zig+6-3
...@@ -1933,12 +1933,15 @@ fn renderContainerDecl(...@@ -1933,12 +1933,15 @@ fn renderContainerDecl(
1933 break :one_line;1933 break :one_line;
1934 }1934 }
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.
1937 for (token_tags[lbrace + 1 .. rbrace - 1]) |tag| {1940 for (token_tags[lbrace + 1 .. rbrace - 1]) |tag| {
1938 if (tag == .doc_comment) break :one_line;1941 if (tag == .doc_comment) break :one_line;
1939 }1942 }
19401943
1941 // 3. The container has non-field members.1944 // 4. The container has non-field members.
1942 for (container_decl.ast.members) |member| {1945 for (container_decl.ast.members) |member| {
1943 if (!node_tags[member].isContainerField()) break :one_line;1946 if (!node_tags[member].isContainerField()) break :one_line;
1944 }1947 }
...@@ -2358,7 +2361,7 @@ fn renderSpace(ais: *Ais, tree: Ast, token_index: Ast.TokenIndex, lexeme_len: us...@@ -2358,7 +2361,7 @@ fn renderSpace(ais: *Ais, tree: Ast, token_index: Ast.TokenIndex, lexeme_len: us
2358 }2361 }
2359}2362}
23602363
2361/// Returns true if there exists a comment between any of the tokens from2364/// Returns true if there exists a line comment between any of the tokens from
2362/// `start_token` to `end_token`. This is used to determine if e.g. a2365/// `start_token` to `end_token`. This is used to determine if e.g. a
2363/// fn_proto should be wrapped and have a trailing comma inserted even if2366/// fn_proto should be wrapped and have a trailing comma inserted even if
2364/// there is none in the source.2367/// there is none in the source.