Commit 3110a73486

Isaac Freund <ifreund@ifreund.xyz>
2021-02-10 16:35:37
zig fmt: implement Tree.lastToken() for array types
1 parent 8c4f3e5
Changed files (2)
lib/std/zig/ast.zig
@@ -529,6 +529,7 @@ pub const Tree = struct {
             .PtrTypeSentinel,
             .PtrType,
             .PtrTypeBitRange,
+            .ArrayType,
             .SwitchCaseOne,
             .SwitchCase,
             => n = datas[n].rhs,
@@ -902,6 +903,10 @@ pub const Tree = struct {
                     return main_tokens[n] + end_offset;
                 }
             },
+            .ArrayTypeSentinel => {
+                const extra = tree.extraData(datas[n].rhs, Node.ArrayTypeSentinel);
+                n = extra.elem_type;
+            },
 
             // These are not supported by lastToken() because implementation would
             // require recursion due to the optional comma followed by rbrace.
@@ -917,8 +922,6 @@ pub const Tree = struct {
             .TaggedUnionEnumTag => unreachable, // TODO
             .TaggedUnionEnumTagComma => unreachable, // TODO
             .SwitchRange => unreachable, // TODO
-            .ArrayType => unreachable, // TODO
-            .ArrayTypeSentinel => unreachable, // TODO
         };
     }
 
lib/std/zig/parser_test.zig
@@ -353,6 +353,19 @@ test "zig fmt: anytype struct field" {
     );
 }
 
+test "zig fmt: array types last token" {
+    try testCanonical(
+        \\test {
+        \\    const x = [40]u32;
+        \\}
+        \\
+        \\test {
+        \\    const x = [40:0]u32;
+        \\}
+        \\
+    );
+}
+
 //test "zig fmt: sentinel-terminated array type" {
 //    try testCanonical(
 //        \\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 {