Commit de9933761c

Michael Raymond <raymond.michael@gmail.com>
2020-03-28 16:44:40
std.zig.render: fix newlines before DocComments
1 parent b717df7
Changed files (2)
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 {
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;
     }