std.zig.render: fix newlines before DocComments

This commit is contained in:
Michael Raymond 2020-03-28 15:44:40 +00:00 committed by Andrew Kelley
parent b717df786b
commit de9933761c
2 changed files with 34 additions and 1 deletions

View File

@ -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 {

View File

@ -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;
}