Commit 4c8caf3343

Isaac Freund <ifreund@ifreund.xyz>
2021-02-10 19:40:19
zig fmt: implement Tree.lastToken() for all nodes
1 parent 515d492
Changed files (2)
lib/std/zig/ast.zig
@@ -538,6 +538,7 @@ pub const Tree = struct {
             .ArrayType,
             .SwitchCaseOne,
             .SwitchCase,
+            .SwitchRange,
             => n = datas[n].rhs,
 
             .FieldAccess,
@@ -580,8 +581,21 @@ pub const Tree = struct {
                 }
                 n = tree.extra_data[params.end - 1]; // last parameter
             },
-            .CallComma, .AsyncCallComma => {
-                end_offset += 2; // for the comma+rparen
+            .TaggedUnionEnumTag => {
+                const members = tree.extraData(datas[n].rhs, Node.SubRange);
+                if (members.end - members.start == 0) {
+                    end_offset += 4; // for the rparen + rparen + lbrace + rbrace
+                    n = datas[n].lhs;
+                } else {
+                    end_offset += 1; // for the rbrace
+                    n = tree.extra_data[members.end - 1]; // last parameter
+                }
+            },
+            .CallComma,
+            .AsyncCallComma,
+            .TaggedUnionEnumTagComma,
+            => {
+                end_offset += 2; // for the comma + rparen/rbrace
                 const params = tree.extraData(datas[n].rhs, Node.SubRange);
                 assert(params.end > params.start);
                 n = tree.extra_data[params.end - 1]; // last parameter
@@ -942,10 +956,6 @@ pub const Tree = struct {
                 const extra = tree.extraData(datas[n].rhs, Node.ArrayTypeSentinel);
                 n = extra.elem_type;
             },
-
-            .TaggedUnionEnumTag => unreachable, // TODO
-            .TaggedUnionEnumTagComma => unreachable, // TODO
-            .SwitchRange => unreachable, // TODO
         };
     }
 
lib/std/zig/parser_test.zig
@@ -841,6 +841,25 @@ test "zig fmt: tagged union with enum values" {
     );
 }
 
+test "zig fmt: tagged union enum tag last token" {
+    try testCanonical(
+        \\test {
+        \\    const U = union(enum(u32)) {};
+        \\}
+        \\
+        \\test {
+        \\    const U = union(enum(u32)) { foo };
+        \\}
+        \\
+        \\test {
+        \\    const U = union(enum(u32)) {
+        \\        foo,
+        \\    };
+        \\}
+        \\
+    );
+}
+
 test "zig fmt: allowzero pointer" {
     try testCanonical(
         \\const T = [*]allowzero const u8;