Commit 272a0ab359

Andrew Kelley <andrew@ziglang.org>
2021-02-02 04:11:55
zig fmt: implement "line comment followed by top-level comptime"
1 parent 20554d3
Changed files (3)
lib/std/zig/parser_test.zig
@@ -5,6 +5,17 @@
 // and substantial portions of the software.
 test "zig fmt: simple top level comptime block" {
     try testCanonical(
+        \\// line comment
+        \\comptime {}
+        \\
+    );
+}
+
+test "zig fmt: two spaced line comments before decl" {
+    try testCanonical(
+        \\// line comment
+        \\
+        \\// another
         \\comptime {}
         \\
     );
lib/std/zig/render.zig
@@ -36,10 +36,11 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize, prefix: [
     var index: usize = start;
     var count: usize = 0;
     while (true) {
-        // Scan forward to the next line comment, counting newlines.
-        const comment_start = mem.indexOf(u8, tree.source[index..end], "//") orelse return count;
-        const newline = mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;
-        const untrimmed_comment = tree.source[comment_start..][0..newline];
+        const comment_start = index +
+            (mem.indexOf(u8, tree.source[index..end], "//") orelse return count);
+        const newline = comment_start +
+            mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;
+        const untrimmed_comment = tree.source[comment_start..newline];
         const trimmed_comment = mem.trimRight(u8, untrimmed_comment, " \r\t");
         if (count == 0) {
             count += 1;
@@ -52,7 +53,7 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize, prefix: [
             }
         }
         try ais.writer().print("{s}\n", .{trimmed_comment});
-        index += comment_start + newline;
+        index = newline + 1;
     }
 }
 
lib/std/zig/tokenizer.zig
@@ -1035,7 +1035,10 @@ pub const Tokenizer = struct {
                         result.tag = .ContainerDocComment;
                         state = .container_doc_comment;
                     },
-                    '\n' => state = .start,
+                    '\n' => {
+                        state = .start;
+                        result.loc.start = self.index + 1;
+                    },
                     '\t', '\r' => state = .line_comment,
                     else => {
                         state = .line_comment;
@@ -1061,7 +1064,10 @@ pub const Tokenizer = struct {
                     },
                 },
                 .line_comment => switch (c) {
-                    '\n' => state = .start,
+                    '\n' => {
+                        state = .start;
+                        result.loc.start = self.index + 1;
+                    },
                     '\t', '\r' => {},
                     else => self.checkLiteralCharacter(),
                 },
@@ -1499,6 +1505,18 @@ test "tokenizer" {
     testTokenize("test", &[_]Token.Tag{.Keyword_test});
 }
 
+test "line comment followed by top-level comptime" {
+    testTokenize(
+        \\// line comment
+        \\comptime {}
+        \\
+    , &[_]Token.Tag{
+        .Keyword_comptime,
+        .LBrace,
+        .RBrace,
+    });
+}
+
 test "tokenizer - unknown length pointer and then c pointer" {
     testTokenize(
         \\[*]u8