authorgravatar for raymond.michael@gmail.comMichael Raymond <raymond.michael@gmail.com> 2020-03-28 15:44:40+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-29 22:41:39-04:00
logde9933761c56287a27f3c348a99434ddab175094
treec072535800e37351bf7cf0379474ce16b3086110
parentb717df786bbf9b42abf1db1ec3fdae183debfdea

std.zig.render: fix newlines before DocComments


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

lib/std/zig/parser_test.zig+29
...@@ -373,6 +373,35 @@ test "zig fmt: correctly move doc comments on struct fields" {...@@ -373,6 +373,35 @@ test "zig fmt: correctly move doc comments on struct fields" {
373 );373 );
374}374}
375375
376test "zig fmt: correctly space struct fields with doc comments" {
377 try testTransform(
378 \\pub const S = struct {
379 \\ /// A
380 \\ a: u8,
381 \\ /// B
382 \\ /// B (cont)
383 \\ b: u8,
384 \\
385 \\
386 \\ /// C
387 \\ c: u8,
388 \\};
389 \\
390 ,
391 \\pub const S = struct {
392 \\ /// A
393 \\ a: u8,
394 \\ /// B
395 \\ /// B (cont)
396 \\ b: u8,
397 \\
398 \\ /// C
399 \\ c: u8,
400 \\};
401 \\
402 );
403}
404
376test "zig fmt: doc comments on param decl" {405test "zig fmt: doc comments on param decl" {
377 try testCanonical(406 try testCanonical(
378 \\pub const Allocator = struct {407 \\pub const Allocator = struct {
lib/std/zig/render.zig+5-1
...@@ -187,12 +187,16 @@ fn renderExtraNewline(tree: *ast.Tree, stream: var, start_col: *usize, node: *as...@@ -187,12 +187,16 @@ fn renderExtraNewline(tree: *ast.Tree, stream: var, start_col: *usize, node: *as
187 const first_token = node.firstToken();187 const first_token = node.firstToken();
188 var prev_token = first_token;188 var prev_token = first_token;
189 if (prev_token == 0) return;189 if (prev_token == 0) return;
190 var newline_threshold: usize = 2;
190 while (tree.tokens.at(prev_token - 1).id == .DocComment) {191 while (tree.tokens.at(prev_token - 1).id == .DocComment) {
192 if (tree.tokenLocation(tree.tokens.at(prev_token - 1).end, prev_token).line == 1) {
193 newline_threshold += 1;
194 }
191 prev_token -= 1;195 prev_token -= 1;
192 }196 }
193 const prev_token_end = tree.tokens.at(prev_token - 1).end;197 const prev_token_end = tree.tokens.at(prev_token - 1).end;
194 const loc = tree.tokenLocation(prev_token_end, first_token);198 const loc = tree.tokenLocation(prev_token_end, first_token);
195 if (loc.line >= 2) {199 if (loc.line >= newline_threshold) {
196 try stream.writeByte('\n');200 try stream.writeByte('\n');
197 start_col.* = 0;201 start_col.* = 0;
198 }202 }