From de9933761c56287a27f3c348a99434ddab175094 Mon Sep 17 00:00:00 2001 From: Michael Raymond Date: Sat, 28 Mar 2020 15:44:40 +0000 Subject: [PATCH] std.zig.render: fix newlines before DocComments --- lib/std/zig/parser_test.zig | 29 +++++++++++++++++++++++++++++ lib/std/zig/render.zig | 6 +++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index daeb197913..136e323fe6 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -373,6 +373,35 @@ test "zig fmt: correctly move doc comments on struct fields" { ); } +test "zig fmt: correctly space struct fields with doc comments" { + try testTransform( + \\pub const S = struct { + \\ /// A + \\ a: u8, + \\ /// B + \\ /// B (cont) + \\ b: u8, + \\ + \\ + \\ /// C + \\ c: u8, + \\}; + \\ + , + \\pub const S = struct { + \\ /// A + \\ a: u8, + \\ /// B + \\ /// B (cont) + \\ b: u8, + \\ + \\ /// C + \\ c: u8, + \\}; + \\ + ); +} + test "zig fmt: doc comments on param decl" { try testCanonical( \\pub const Allocator = struct { diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 37d058bebf..c5e5201680 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -187,12 +187,16 @@ fn renderExtraNewline(tree: *ast.Tree, stream: var, start_col: *usize, node: *as const first_token = node.firstToken(); var prev_token = first_token; if (prev_token == 0) return; + var newline_threshold: usize = 2; while (tree.tokens.at(prev_token - 1).id == .DocComment) { + if (tree.tokenLocation(tree.tokens.at(prev_token - 1).end, prev_token).line == 1) { + newline_threshold += 1; + } prev_token -= 1; } const prev_token_end = tree.tokens.at(prev_token - 1).end; const loc = tree.tokenLocation(prev_token_end, first_token); - if (loc.line >= 2) { + if (loc.line >= newline_threshold) { try stream.writeByte('\n'); start_col.* = 0; }