mirror of
https://github.com/ziglang/zig.git
synced 2024-12-03 17:50:13 +00:00
std.zig.render: fix switch rendering
This commit is contained in:
parent
0cc8435a83
commit
3b52e5a221
@ -1896,11 +1896,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,
|
||||
};
|
||||
@ -2869,7 +2870,7 @@ pub const full = struct {
|
||||
pub const Components = struct {
|
||||
switch_token: TokenIndex,
|
||||
condition: Node.Index,
|
||||
sub_range: Node.Index,
|
||||
cases: []const Node.Index,
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -7598,9 +7598,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 .{
|
||||
|
@ -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);
|
||||
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user