feature. See also
. The project being documented here (as the example) is the Zig library itself.
Render.renderContainerField
fn renderContainerField(
r: *Render,
container: Container,
field_param: Ast.full.ContainerField,
space: Space,
) Error!void
File
Code
fn renderContainerField(
r: *Render,
container: Container,
field_param: Ast.full.ContainerField,
space: Space,
) Error!void {
const tree = r.tree;
const ais = r.ais;
var field = field_param;
if (container != .tuple) field.convertToNonTupleLike(&tree);
const quote: QuoteBehavior = switch (container) {
.@"enum" => .eagerly_unquote_except_underscore,
.tuple, .other => .eagerly_unquote,
};
if (field.comptime_token) |t| {
try renderToken(r, t, .maybe_space);
}
const last_component: enum {
value,
@"align",
type,
identifier,
} = if (field.ast.value_expr != .none)
.value
else if (field.ast.align_expr != .none)
.@"align"
else if (field.ast.type_expr != .none)
.type
else if (!field.ast.tuple_like)
.identifier
else
unreachable;
if (!field.ast.tuple_like) {
if (last_component == .identifier) {
return renderIdentifierComma(r, field.ast.main_token, space, quote);
}
const this_space: Space = if (field.ast.type_expr != .none) .none else .space;
try renderIdentifier(r, field.ast.main_token, this_space, quote);
}
if (field.ast.type_expr.unwrap()) |type_expr| {
if (!field.ast.tuple_like) {
try renderToken(r, field.ast.main_token + 1, .maybe_space);
}
if (last_component == .type) {
return renderExpressionComma(r, type_expr, space);
}
try renderExpression(r, type_expr, .space);
}
if (field.ast.align_expr.unwrap()) |align_expr| {
const align_token = tree.firstToken(align_expr) - 2;
try renderToken(r, align_token, .none);
try renderToken(r, align_token + 1, .none);
try renderExpression(r, align_expr, .none);
const rparen = tree.lastToken(align_expr) + 1;
if (last_component == .@"align") {
return renderTokenComma(r, rparen, space);
}
try renderToken(r, rparen, .space);
}
if (field.ast.value_expr.unwrap()) |value_expr| {
assert(last_component == .value);
const eq_token = tree.firstToken(value_expr) - 1;
const seperate_line = !tree.tokensOnSameLine(eq_token, eq_token + 1) or
tree.tokenTag(eq_token + 1) == .multiline_string_literal_line;
const eq_space: Space = if (seperate_line) .newline else .space;
try ais.pushIndent(.after_equals);
try renderToken(r, eq_token, eq_space);
if (eq_space == .space) {
ais.popIndent();
return renderExpressionComma(r, value_expr, space);
}
const maybe_comma = tree.lastToken(value_expr) + 1;
if (tree.tokenTag(maybe_comma) == .comma) {
try renderExpression(r, value_expr, .none);
ais.popIndent();
try renderToken(r, maybe_comma, .newline);
} else {
try renderExpression(r, value_expr, space);
ais.popIndent();
}
} else unreachable;
}