authorgravatar for yunsangho@kakao.comyunsh1 <yunsangho@kakao.com> 2023-10-09 02:03:20+09:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-12-08 16:05:46+02:00
log7e4177a4b1b0288917e14f36981fdf837fbdd257
tree9dedde1709e8864e429bf6d438ceb2b7ddfb5979
parent6a12fd62c17ea2e054456e71d2a02ac995aa617a

fmt: Skip extra newline if doc_comment exists


2 files changed, 66 insertions(+), 1 deletions(-)

lib/std/zig/parser_test.zig+63
......@@ -4270,6 +4270,69 @@ test "zig fmt: remove newlines surrounding doc comment" {
42704270 );
42714271}
42724272
4273test "zig fmt: remove newlines surrounding doc comment between members" {
4274 try testTransform(
4275 \\f1: i32,
4276 \\
4277 \\
4278 \\/// doc comment
4279 \\
4280 \\f2: i32,
4281 \\
4282 ,
4283 \\f1: i32,
4284 \\
4285 \\/// doc comment
4286 \\f2: i32,
4287 \\
4288 );
4289}
4290
4291test "zig fmt: remove newlines surrounding doc comment between members within container decl (1)" {
4292 try testTransform(
4293 \\const Foo = struct {
4294 \\ fn foo() void {}
4295 \\
4296 \\
4297 \\ /// doc comment
4298 \\
4299 \\
4300 \\ fn bar() void {}
4301 \\};
4302 \\
4303 ,
4304 \\const Foo = struct {
4305 \\ fn foo() void {}
4306 \\
4307 \\ /// doc comment
4308 \\ fn bar() void {}
4309 \\};
4310 \\
4311 );
4312}
4313
4314test "zig fmt: remove newlines surrounding doc comment between members within container decl (2)" {
4315 try testTransform(
4316 \\const Foo = struct {
4317 \\ fn foo() void {}
4318 \\ /// doc comment 1
4319 \\
4320 \\ /// doc comment 2
4321 \\
4322 \\ fn bar() void {}
4323 \\};
4324 \\
4325 ,
4326 \\const Foo = struct {
4327 \\ fn foo() void {}
4328 \\ /// doc comment 1
4329 \\ /// doc comment 2
4330 \\ fn bar() void {}
4331 \\};
4332 \\
4333 );
4334}
4335
42734336test "zig fmt: remove newlines surrounding doc comment within container decl" {
42744337 try testTransform(
42754338 \\const Foo = struct {
lib/std/zig/render.zig+3-1
......@@ -3188,8 +3188,10 @@ fn renderExtraNewlineToken(r: *Render, token_index: Ast.TokenIndex) Error!void {
31883188 else
31893189 token_starts[token_index - 1] + tokenSliceForRender(tree, token_index - 1).len;
31903190
3191 // If there is a comment present, it will handle the empty line
3191 // If there is a immediately preceding comment or doc_comment,
3192 // skip it because required extra newline has already been rendered.
31923193 if (mem.indexOf(u8, tree.source[prev_token_end..token_start], "//") != null) return;
3194 if (token_index > 0 and tree.tokens.items(.tag)[token_index - 1] == .doc_comment) return;
31933195
31943196 // Iterate backwards to the end of the previous token, stopping if a
31953197 // non-whitespace character is encountered or two newlines have been found.