renderBuiltinCall
Function parameters
Parameters
Type definitions in this namespace
Types
Functions in this namespace
Functions
Error sets in this namespace
Error Sets
Source
Implementation
fn renderBuiltinCall(
r: *Render,
builtin_token: Ast.TokenIndex,
params: []const Ast.Node.Index,
space: Space,
) Error!void {
const tree = r.tree;
const ais = r.ais;
try renderToken(r, builtin_token, .none); // @name
if (params.len == 0) {
try renderToken(r, builtin_token + 1, .none); // (
return renderToken(r, builtin_token + 2, space); // )
}
if (r.fixups.rebase_imported_paths) |prefix| {
const slice = tree.tokenSlice(builtin_token);
if (mem.eql(u8, slice, "@import")) f: {
const param = params[0];
const str_lit_token = tree.nodeMainToken(param);
assert(tree.tokenTag(str_lit_token) == .string_literal);
const token_bytes = tree.tokenSlice(str_lit_token);
const imported_string = std.zig.string_literal.parseAlloc(r.gpa, token_bytes) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.InvalidLiteral => break :f,
};
defer r.gpa.free(imported_string);
const new_string = try std.fs.path.resolvePosix(r.gpa, &.{ prefix, imported_string });
defer r.gpa.free(new_string);
try renderToken(r, builtin_token + 1, .none); // (
try ais.print("\"{f}\"", .{std.zig.fmtString(new_string)});
return renderToken(r, str_lit_token + 1, space); // )
}
}
const last_param = params[params.len - 1];
const after_last_param_token = tree.lastToken(last_param) + 1;
if (tree.tokenTag(after_last_param_token) != .comma) {
// Render all on one line, no trailing comma.
try renderToken(r, builtin_token + 1, .none); // (
for (params, 0..) |param_node, i| {
const first_param_token = tree.firstToken(param_node);
if (tree.tokenTag(first_param_token) == .multiline_string_literal_line or
hasSameLineComment(tree, first_param_token - 1))
{
try ais.pushIndent(.normal);
try renderExpression(r, param_node, .none);
ais.popIndent();
} else {
try renderExpression(r, param_node, .none);
}
if (i + 1 < params.len) {
const comma_token = tree.lastToken(param_node) + 1;
try renderToken(r, comma_token, .space); // ,
}
}
return renderToken(r, after_last_param_token, space); // )
} else {
// Render one param per line.
try ais.pushIndent(.normal);
try renderToken(r, builtin_token + 1, Space.newline); // (
for (params) |param_node| {
try ais.pushSpace(.comma);
try renderExpression(r, param_node, .comma);
ais.popSpace();
}
ais.popIndent();
return renderToken(r, after_last_param_token + 1, space); // )
}
}