renderStructInit
Function parameters
Parameters
Type definitions in this namespace
Types
Functions in this namespace
Functions
Error sets in this namespace
Error Sets
Source
Implementation
fn renderStructInit(
r: *Render,
struct_node: Ast.Node.Index,
struct_init: Ast.full.StructInit,
space: Space,
) Error!void {
const tree = r.tree;
const ais = r.ais;
if (struct_init.ast.type_expr.unwrap()) |type_expr| {
try renderExpression(r, type_expr, .none); // T
} else {
try renderToken(r, struct_init.ast.lbrace - 1, .none); // .
}
if (struct_init.ast.fields.len == 0) {
try ais.pushIndent(.normal);
try renderToken(r, struct_init.ast.lbrace, .none); // lbrace
ais.popIndent();
return renderToken(r, struct_init.ast.lbrace + 1, space); // rbrace
}
const rbrace = tree.lastToken(struct_node);
const trailing_comma = tree.tokenTag(rbrace - 1) == .comma;
if (trailing_comma or hasComment(tree, struct_init.ast.lbrace, rbrace)) {
// Render one field init per line.
try ais.pushIndent(.normal);
try renderToken(r, struct_init.ast.lbrace, .newline);
try renderToken(r, struct_init.ast.lbrace + 1, .none); // .
try renderIdentifier(r, struct_init.ast.lbrace + 2, .space, .eagerly_unquote); // name
// Don't output a space after the = if expression is a multiline string,
// since then it will start on the next line.
const field_node = struct_init.ast.fields[0];
const expr = tree.nodeTag(field_node);
var space_after_equal: Space = if (expr == .multiline_string_literal) .none else .space;
try renderToken(r, struct_init.ast.lbrace + 3, space_after_equal); // =
try ais.pushSpace(.comma);
try renderExpressionFixup(r, field_node, .comma);
ais.popSpace();
for (struct_init.ast.fields[1..]) |field_init| {
const init_token = tree.firstToken(field_init);
try renderExtraNewlineToken(r, init_token - 3);
try renderToken(r, init_token - 3, .none); // .
try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name
space_after_equal = if (tree.nodeTag(field_init) == .multiline_string_literal) .none else .space;
try renderToken(r, init_token - 1, space_after_equal); // =
try ais.pushSpace(.comma);
try renderExpressionFixup(r, field_init, .comma);
ais.popSpace();
}
ais.popIndent();
} else {
// Render all on one line, no trailing comma.
try renderToken(r, struct_init.ast.lbrace, .space);
for (struct_init.ast.fields) |field_init| {
const init_token = tree.firstToken(field_init);
try renderToken(r, init_token - 3, .none); // .
try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name
try renderToken(r, init_token - 1, .space); // =
try renderExpressionFixup(r, field_init, .comma_space);
}
}
return renderToken(r, rbrace, space);
}