From 10e11b60e56941cb664648dcebfd4db3d2efed30 Mon Sep 17 00:00:00 2001 From: yujiri8 Date: Fri, 2 Sep 2022 14:12:20 -0400 Subject: zig fmt: don't delete container doc comments Fixes #12617 --- lib/std/zig/parser_test.zig | 15 +++++++++++++++ lib/std/zig/render.zig | 9 ++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index ab7bea7367..0e1817ffab 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -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 diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 333fb80d88..bc59ddc279 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -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. -- cgit v1.2.3