zig fmt: implement Tree.lastToken() for array types

This commit is contained in:
Isaac Freund 2021-02-10 16:35:37 +01:00 committed by Andrew Kelley
parent 8c4f3e5a31
commit 3110a73486
2 changed files with 18 additions and 2 deletions

View File

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

View File

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