zig fmt: suspend blocks

This commit is contained in:
Andrew Kelley 2021-02-09 20:35:43 -07:00
parent ebf04c56e1
commit 7295d4b807
3 changed files with 41 additions and 25 deletions

View File

@ -459,7 +459,6 @@ pub const Tree = struct {
.Try,
.Await,
.OptionalType,
.Suspend,
.Resume,
.Nosuspend,
.Comptime,
@ -625,7 +624,6 @@ pub const Tree = struct {
},
.ArrayInitDotTwo,
.BuiltinCallTwo,
.BlockTwo,
.StructInitDotTwo,
.ContainerDeclTwo,
@ -640,6 +638,18 @@ pub const Tree = struct {
return main_tokens[n] + end_offset;
}
},
.BuiltinCallTwo => {
if (datas[n].rhs != 0) {
end_offset += 1; // for the rparen/rbrace
n = datas[n].rhs;
} else if (datas[n].lhs != 0) {
end_offset += 1; // for the rparen/rbrace
n = datas[n].lhs;
} else {
end_offset += 2; // for the lparen and rparen
return main_tokens[n] + end_offset;
}
},
.ArrayInitDotTwoComma,
.BuiltinCallTwoComma,
.BlockTwoSemicolon,
@ -861,6 +871,13 @@ pub const Tree = struct {
assert(extra.else_expr != 0);
n = extra.else_expr;
},
.Suspend => {
if (datas[n].lhs != 0) {
n = datas[n].lhs;
} else {
return main_tokens[n] + end_offset;
}
},
// These are not supported by lastToken() because implementation would
// require recursion due to the optional comma followed by rbrace.

View File

@ -2124,18 +2124,18 @@ test "zig fmt: error set declaration" {
// \\
// );
//}
//
//test "zig fmt: resume from suspend block" {
// try testCanonical(
// \\fn foo() void {
// \\ suspend {
// \\ resume @frame();
// \\ }
// \\}
// \\
// );
//}
//
test "zig fmt: resume from suspend block" {
try testCanonical(
\\fn foo() void {
\\ suspend {
\\ resume @frame();
\\ }
\\}
\\
);
}
//test "zig fmt: comments before error set decl" {
// try testCanonical(
// \\const UnexpectedError = error{

View File

@ -238,17 +238,16 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
return renderExpression(ais, tree, block, space);
},
.Suspend => unreachable, // TODO
//.Suspend => {
// const suspend_node = @fieldParentPtr(ast.Node.Suspend, "base", base);
// if (suspend_node.body) |body| {
// try renderToken(ais, tree, suspend_node.suspend_token, Space.Space);
// return renderExpression(ais, tree, body, space);
// } else {
// return renderToken(ais, tree, suspend_node.suspend_token, space);
// }
//},
.Suspend => {
const suspend_token = main_tokens[node];
const body = datas[node].lhs;
if (body != 0) {
try renderToken(ais, tree, suspend_token, .Space);
return renderExpression(ais, tree, body, space);
} else {
return renderToken(ais, tree, suspend_token, space);
}
},
.Catch => {
const main_token = main_tokens[node];