authorgravatar for 47984692+luehmann@users.noreply.github.comPhilipp Lühmann <47984692+luehmann@users.noreply.github.com> 2022-06-28 21:38:28+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-06-28 21:38:28+02:00
logbb2929ba083d3c5d86cae1d83cba0abd43ffa3d5
treea3cc33668cc0aa4bc6f5584dc59d111c61611611
parent8974cee5a1f67db42a83a0907d8b9e842b979072
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

zig fmt: fix idempotency with newlines surrounding doc comment

Fixes: https://github.com/ziglang/zig/issues/11802

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

lib/std/os/linux.zig-1
...@@ -4683,7 +4683,6 @@ pub const prctl_mm_map = extern struct {...@@ -4683,7 +4683,6 @@ pub const prctl_mm_map = extern struct {
4683};4683};
46844684
4685pub const NETLINK = struct {4685pub const NETLINK = struct {
4686
4687 /// Routing/device hook4686 /// Routing/device hook
4688 pub const ROUTE = 0;4687 pub const ROUTE = 0;
46894688
lib/std/zig/parser_test.zig+35
...@@ -4154,6 +4154,41 @@ test "zig fmt: container doc comments" {...@@ -4154,6 +4154,41 @@ test "zig fmt: container doc comments" {
4154 );4154 );
4155}4155}
41564156
4157test "zig fmt: remove newlines surrounding doc comment" {
4158 try testTransform(
4159 \\
4160 \\
4161 \\
4162 \\/// doc comment
4163 \\
4164 \\fn foo() void {}
4165 \\
4166 ,
4167 \\/// doc comment
4168 \\fn foo() void {}
4169 \\
4170 );
4171}
4172
4173test "zig fmt: remove newlines surrounding doc comment within container decl" {
4174 try testTransform(
4175 \\const Foo = struct {
4176 \\
4177 \\
4178 \\ /// doc comment
4179 \\
4180 \\ fn foo() void {}
4181 \\};
4182 \\
4183 ,
4184 \\const Foo = struct {
4185 \\ /// doc comment
4186 \\ fn foo() void {}
4187 \\};
4188 \\
4189 );
4190}
4191
4157test "zig fmt: anytype struct field" {4192test "zig fmt: anytype struct field" {
4158 try testError(4193 try testError(
4159 \\pub const Pointer = struct {4194 \\pub const Pointer = struct {
lib/std/zig/render.zig+11-1
...@@ -2482,7 +2482,17 @@ fn renderDocComments(ais: *Ais, tree: Ast, end_token: Ast.TokenIndex) Error!void...@@ -2482,7 +2482,17 @@ fn renderDocComments(ais: *Ais, tree: Ast, end_token: Ast.TokenIndex) Error!void
2482 }2482 }
2483 const first_tok = tok;2483 const first_tok = tok;
2484 if (first_tok == end_token) return;2484 if (first_tok == end_token) return;
2485 try renderExtraNewlineToken(ais, tree, first_tok);2485
2486 if (first_tok != 0) {
2487 const prev_token_tag = token_tags[first_tok - 1];
2488
2489 // Prevent accidental use of `renderDocComments` for a function argument doc comment
2490 assert(prev_token_tag != .l_paren);
2491
2492 if (prev_token_tag != .l_brace) {
2493 try renderExtraNewlineToken(ais, tree, first_tok);
2494 }
2495 }
24862496
2487 while (token_tags[tok] == .doc_comment) : (tok += 1) {2497 while (token_tags[tok] == .doc_comment) : (tok += 1) {
2488 try renderToken(ais, tree, tok, .newline);2498 try renderToken(ais, tree, tok, .newline);