Skip to content

Commit

Permalink
std.zig.render: fix switch rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mlugg committed May 3, 2024
1 parent e962ba9 commit ac6e2e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
5 changes: 3 additions & 2 deletions lib/std/zig/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1909,11 +1909,12 @@ pub fn switchFull(tree: Ast, node: Node.Index) full.Switch {
.keyword_switch => .{ main_token, null },
else => unreachable,
};
const extra = tree.extraData(data.rhs, Ast.Node.SubRange);
return .{
.ast = .{
.switch_token = switch_token,
.condition = data.lhs,
.sub_range = data.rhs,
.cases = tree.extra_data[extra.start..extra.end],
},
.label_token = label_token,
};
Expand Down Expand Up @@ -2880,7 +2881,7 @@ pub const full = struct {
pub const Components = struct {
switch_token: TokenIndex,
condition: Node.Index,
sub_range: Node.Index,
cases: []const Node.Index,
};
};

Expand Down
5 changes: 2 additions & 3 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7621,9 +7621,8 @@ fn switchExpr(
const node_tags = tree.nodes.items(.tag);
const main_tokens = tree.nodes.items(.main_token);
const token_tags = tree.tokens.items(.tag);
const operand_node = node_datas[node].lhs;
const extra = tree.extraData(node_datas[node].rhs, Ast.Node.SubRange);
const case_nodes = tree.extra_data[extra.start..extra.end];
const operand_node = switch_full.ast.condition;
const case_nodes = switch_full.ast.cases;

const need_rl = astgen.nodes_need_rl.contains(node);
const block_ri: ResultInfo = if (need_rl) ri else .{
Expand Down
57 changes: 24 additions & 33 deletions lib/std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -693,39 +693,27 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
return renderToken(r, datas[node].rhs, space);
},

.@"break" => {
.@"break", .@"continue" => {
const main_token = main_tokens[node];
const label_token = datas[node].lhs;
const target = datas[node].rhs;
if (label_token == 0 and target == 0) {
try renderToken(r, main_token, space); // break keyword
try renderToken(r, main_token, space); // break/continue
} else if (label_token == 0 and target != 0) {
try renderToken(r, main_token, .space); // break keyword
try renderToken(r, main_token, .space); // break/continue
try renderExpression(r, target, space);
} else if (label_token != 0 and target == 0) {
try renderToken(r, main_token, .space); // break keyword
try renderToken(r, label_token - 1, .none); // colon
try renderToken(r, main_token, .space); // break/continue
try renderToken(r, label_token - 1, .none); // :
try renderIdentifier(r, label_token, space, .eagerly_unquote); // identifier
} else if (label_token != 0 and target != 0) {
try renderToken(r, main_token, .space); // break keyword
try renderToken(r, label_token - 1, .none); // colon
try renderToken(r, main_token, .space); // break/continue
try renderToken(r, label_token - 1, .none); // :
try renderIdentifier(r, label_token, .space, .eagerly_unquote); // identifier
try renderExpression(r, target, space);
}
},

.@"continue" => {
const main_token = main_tokens[node];
const label = datas[node].lhs;
if (label != 0) {
try renderToken(r, main_token, .space); // continue
try renderToken(r, label - 1, .none); // :
return renderIdentifier(r, label, space, .eagerly_unquote); // label
} else {
return renderToken(r, main_token, space); // continue
}
},

.@"return" => {
if (datas[node].lhs != 0) {
try renderToken(r, main_tokens[node], .space);
Expand Down Expand Up @@ -845,26 +833,29 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
.@"switch",
.switch_comma,
=> {
const switch_token = main_tokens[node];
const condition = datas[node].lhs;
const extra = tree.extraData(datas[node].rhs, Ast.Node.SubRange);
const cases = tree.extra_data[extra.start..extra.end];
const rparen = tree.lastToken(condition) + 1;
const full = tree.switchFull(node);

try renderToken(r, switch_token, .space); // switch keyword
try renderToken(r, switch_token + 1, .none); // lparen
try renderExpression(r, condition, .none); // condition expression
try renderToken(r, rparen, .space); // rparen
if (full.label_token) |label_token| {
try renderIdentifier(r, label_token, .none, .eagerly_unquote); // label
try renderToken(r, label_token + 1, .space); // :
}

const rparen = tree.lastToken(full.ast.condition) + 1;

try renderToken(r, full.ast.switch_token, .space); // switch
try renderToken(r, full.ast.switch_token + 1, .none); // (
try renderExpression(r, full.ast.condition, .none); // condition expression
try renderToken(r, rparen, .space); // )

ais.pushIndentNextLine();
if (cases.len == 0) {
try renderToken(r, rparen + 1, .none); // lbrace
if (full.ast.cases.len == 0) {
try renderToken(r, rparen + 1, .none); // {
} else {
try renderToken(r, rparen + 1, .newline); // lbrace
try renderExpressions(r, cases, .comma);
try renderToken(r, rparen + 1, .newline); // {
try renderExpressions(r, full.ast.cases, .comma);
}
ais.popIndent();
return renderToken(r, tree.lastToken(node), space); // rbrace
return renderToken(r, tree.lastToken(node), space); // }
},

.switch_case_one,
Expand Down

0 comments on commit ac6e2e7

Please sign in to comment.