Skip to content

Commit

Permalink
do not show inlay hint on builtins with trivial parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Mar 2, 2024
1 parent f25fafe commit 70cf64e
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 16 deletions.
139 changes: 128 additions & 11 deletions src/features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,130 @@ const Config = @import("../Config.zig");

const data = @import("version_data");

/// don't show inlay hints for the given builtin functions
/// this option is rare and is therefore build-only and
/// non-configurable at runtime
pub const inlay_hints_exclude_builtins: []const u8 = &.{};
/// don't show inlay hints for builtin functions whose parameter names carry no
/// meaningful information or are trivial deductible based on the builtin name.
const excluded_builtins_set = blk: {
@setEvalBranchQuota(2000);
break :blk std.ComptimeStringMap(void, &.{
.{"addrSpaceCast"},
.{"addWithOverflow"},
.{"alignCast"},
.{"alignOf"},
.{"as"},
// .{"atomicLoad"},
// .{"atomicRmw"},
// .{"atomicStore"},
.{"bitCast"},
.{"bitOffsetOf"},
.{"bitSizeOf"},
.{"breakpoint"}, // no parameters
// .{"mulAdd"},
.{"byteSwap"},
.{"bitReverse"},
.{"offsetOf"},
// .{"call"},
.{"cDefine"},
.{"cImport"},
.{"cInclude"},
.{"clz"},
// .{"cmpxchgStrong"},
// .{"cmpxchgWeak"},
// .{"compileError"},
.{"compileLog"}, // variadic
.{"constCast"},
.{"ctz"},
.{"cUndef"},
// .{"cVaArg"},
// .{"cVaCopy"},
// .{"cVaEnd"},
// .{"cVaStart"},
.{"divExact"},
.{"divFloor"},
.{"divTrunc"},
.{"embedFile"},
.{"enumFromInt"},
.{"errorFromInt"},
.{"errorName"},
.{"errorReturnTrace"}, // no parameters
.{"errorCast"},
// .{"export"},
// .{"extern"},
// .{"fence"},
// .{"field"},
// .{"fieldParentPtr"},
.{"floatCast"},
.{"floatFromInt"},
.{"frameAddress"}, // no parameters
// .{"hasDecl"},
// .{"hasField"},
.{"import"},
.{"inComptime"}, // no parameters
.{"intCast"},
.{"intFromBool"},
.{"intFromEnum"},
.{"intFromError"},
.{"intFromFloat"},
.{"intFromPtr"},
.{"max"},
// .{"memcpy"},
// .{"memset"},
.{"min"},
// .{"wasmMemorySize"},
// .{"wasmMemoryGrow"},
.{"mod"},
.{"mulWithOverflow"},
// .{"panic"},
.{"popCount"},
// .{"prefetch"},
.{"ptrCast"},
.{"ptrFromInt"},
.{"rem"},
.{"returnAddress"}, // no parameters
// .{"select"},
// .{"setAlignStack"},
.{"setCold"},
.{"setEvalBranchQuota"},
.{"setFloatMode"},
.{"setRuntimeSafety"},
// .{"shlExact"},
// .{"shlWithOverflow"},
// .{"shrExact"},
// .{"shuffle"},
.{"sizeOf"},
// .{"splat"},
// .{"reduce"},
.{"src"}, // no parameters
.{"sqrt"},
.{"sin"},
.{"cos"},
.{"tan"},
.{"exp"},
.{"exp2"},
.{"log"},
.{"log2"},
.{"log10"},
.{"abs"},
.{"floor"},
.{"ceil"},
.{"trunc"},
.{"round"},
.{"subWithOverflow"},
.{"tagName"},
.{"This"}, // no parameters
.{"trap"}, // no parameters
.{"truncate"},
.{"Type"},
.{"typeInfo"},
.{"typeName"},
.{"TypeOf"}, // variadic
// .{"unionInit"},
// .{"Vector"},
.{"volatileCast"},
// .{"workGroupId"},
// .{"workGroupSize"},
// .{"workItemId"},
});
};

pub const InlayHint = struct {
index: usize,
Expand Down Expand Up @@ -416,20 +536,17 @@ fn writeNodeInlayHint(
=> {
if (!builder.config.inlay_hints_show_parameter_name or !builder.config.inlay_hints_show_builtin) return;

const name = tree.tokenSlice(main_tokens[node]);
if (name.len < 2 or excluded_builtins_set.has(name[1..])) return;

var buffer: [2]Ast.Node.Index = undefined;
const params = ast.builtinCallParams(tree, node, &buffer).?;

if (params.len == 0) return;

const name = tree.tokenSlice(main_tokens[node]);

outer: for (data.builtins) |builtin| {
for (data.builtins) |builtin| {
if (!std.mem.eql(u8, builtin.name, name)) continue;

for (inlay_hints_exclude_builtins) |builtin_name| {
if (std.mem.eql(u8, builtin_name, name)) break :outer;
}

try writeBuiltinHint(builder, params, builtin.arguments);
}
},
Expand Down
22 changes: 17 additions & 5 deletions tests/lsp_features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,25 @@ test "resolve alias" {
test "builtin call" {
try testInlayHints(
\\const _ = @memcpy(<dest>"",<source>"");
\\const _ = @sizeOf(<T>u32);
\\const _ = @TypeOf(5);
, .{
.kind = .Parameter,
});
\\const _ = @Vector(<len>4,<Element>u32);
\\const _ = @compileError(<msg>"");
, .{ .kind = .Parameter });

// exclude variadics
try testInlayHints(
\\const _ = @compileLog(2, 3);
\\const _ = @TypeOf(null, 5);
, .{ .kind = .Parameter });

// exclude other builtins
try testInlayHints(
\\const _ = @sizeOf(u32);
\\const _ = @max(2, 4);
, .{ .kind = .Parameter });

try testInlayHints(
\\const _ = @memcpy("","");
\\const _ = @TypeOf(null, 5);
\\const _ = @sizeOf(u32);
\\const _ = @TypeOf(5);
, .{
Expand Down

0 comments on commit 70cf64e

Please sign in to comment.