zig fmt: if nested

This commit is contained in:
Andrew Kelley 2021-02-21 20:25:31 -07:00
parent a17a5ca3a8
commit 621ad241d6
2 changed files with 29 additions and 21 deletions

View File

@ -1449,26 +1449,26 @@ test "zig fmt: if-else with comment before else" {
);
}
//test "zig fmt: if nested" {
// try testCanonical(
// \\pub fn foo() void {
// \\ return if ((aInt & bInt) >= 0)
// \\ if (aInt < bInt)
// \\ GE_LESS
// \\ else if (aInt == bInt)
// \\ GE_EQUAL
// \\ else
// \\ GE_GREATER
// \\ else if (aInt > bInt)
// \\ GE_LESS
// \\ else if (aInt == bInt)
// \\ GE_EQUAL
// \\ else
// \\ GE_GREATER;
// \\}
// \\
// );
//}
test "zig fmt: if nested" {
try testCanonical(
\\pub fn foo() void {
\\ return if ((aInt & bInt) >= 0)
\\ if (aInt < bInt)
\\ GE_LESS
\\ else if (aInt == bInt)
\\ GE_EQUAL
\\ else
\\ GE_GREATER
\\ else if (aInt > bInt)
\\ GE_LESS
\\ else if (aInt == bInt)
\\ GE_EQUAL
\\ else
\\ GE_GREATER;
\\}
\\
);
}
test "zig fmt: respect line breaks in if-else" {
try testCanonical(

View File

@ -984,7 +984,8 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
try renderToken(ais, tree, while_node.ast.while_token + 1, .none); // (
try renderExpression(ais, tree, while_node.ast.cond_expr, .none); // condition
if (nodeIsBlock(node_tags[while_node.ast.then_expr])) {
const then_tag = node_tags[while_node.ast.then_expr];
if (nodeIsBlock(then_tag) and !nodeIsIf(then_tag)) {
if (while_node.payload_token) |payload_token| {
try renderToken(ais, tree, payload_token - 2, .space); // )
try renderToken(ais, tree, payload_token - 1, .none); // |
@ -2128,6 +2129,13 @@ fn nodeIsBlock(tag: ast.Node.Tag) bool {
};
}
fn nodeIsIf(tag: ast.Node.Tag) bool {
return switch (tag) {
.@"if", .if_simple => true,
else => false,
};
}
fn nodeCausesSliceOpSpace(tag: ast.Node.Tag) bool {
return switch (tag) {
.@"catch",