include anytype parameters in inlay hint function parameter count (#1263)

This commit is contained in:
Techatrix 2023-06-24 21:17:07 +00:00 committed by GitHub
parent 7487308948
commit 0b1fc7eb6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 14 deletions

View File

@ -84,27 +84,31 @@ fn writeCallHint(builder: *Builder, call: Ast.full.Call, decl_handle: Analyser.D
var buffer: [1]Ast.Node.Index = undefined; var buffer: [1]Ast.Node.Index = undefined;
const fn_proto = decl_tree.fullFnProto(&buffer, fn_node) orelse return; const fn_proto = decl_tree.fullFnProto(&buffer, fn_node) orelse return;
var i: usize = 0; var params = try std.ArrayListUnmanaged(Ast.full.FnProto.Param).initCapacity(builder.arena, fn_proto.ast.params.len);
var it = fn_proto.iterate(&decl_tree); defer params.deinit(builder.arena);
if (tree.tokens.items(.tag)[call.ast.lparen - 2] == .period and var it = fn_proto.iterate(&decl_tree);
call.ast.params.len + 1 == fn_proto.ast.params.len and while (ast.nextFnParam(&it)) |param| {
try builder.analyser.hasSelfParam(decl_handle.handle, fn_proto)) try params.append(builder.arena, param);
{
_ = ast.nextFnParam(&it);
} }
while (ast.nextFnParam(&it)) |param| : (i += 1) { const has_self_param = tree.tokens.items(.tag)[call.ast.lparen - 2] == .period and
if (i >= call.ast.params.len) break; call.ast.params.len + 1 == params.items.len and
try builder.analyser.hasSelfParam(decl_handle.handle, fn_proto);
const parameters = params.items[@intFromBool(has_self_param)..];
const arguments = call.ast.params;
const min_len = @min(parameters.len, arguments.len);
for (parameters[0..min_len], call.ast.params[0..min_len]) |param, arg| {
const name_token = param.name_token orelse continue; const name_token = param.name_token orelse continue;
const name = decl_tree.tokenSlice(name_token); const name = decl_tree.tokenSlice(name_token);
if (builder.config.inlay_hints_hide_redundant_param_names or builder.config.inlay_hints_hide_redundant_param_names_last_token) { if (builder.config.inlay_hints_hide_redundant_param_names or builder.config.inlay_hints_hide_redundant_param_names_last_token) {
const last_param_token = tree.lastToken(call.ast.params[i]); const last_arg_token = tree.lastToken(arg);
const param_name = tree.tokenSlice(last_param_token); const arg_name = tree.tokenSlice(last_arg_token);
if (std.mem.eql(u8, param_name, name)) { if (std.mem.eql(u8, arg_name, name)) {
if (tree.firstToken(call.ast.params[i]) == last_param_token) { if (tree.firstToken(arg) == last_arg_token) {
if (builder.config.inlay_hints_hide_redundant_param_names) if (builder.config.inlay_hints_hide_redundant_param_names)
continue; continue;
} else { } else {
@ -125,7 +129,7 @@ fn writeCallHint(builder: *Builder, call: Ast.full.Call, decl_handle: Analyser.D
offsets.nodeToSlice(decl_tree, param.type_expr); offsets.nodeToSlice(decl_tree, param.type_expr);
try builder.appendParameterHint( try builder.appendParameterHint(
tree.firstToken(call.ast.params[i]), tree.firstToken(arg),
name, name,
tooltip, tooltip,
no_alias, no_alias,

View File

@ -48,6 +48,11 @@ test "inlayhints - function self parameter" {
\\const foo: Foo = .{}; \\const foo: Foo = .{};
\\const _ = foo.bar(<alpha>5,<beta>""); \\const _ = foo.bar(<alpha>5,<beta>"");
); );
try testInlayHints(
\\const Foo = struct { pub fn bar(self: Foo, alpha: u32, beta: anytype) void {} };
\\const foo: Foo = .{};
\\const _ = foo.bar(<alpha>5,<beta>4);
);
try testInlayHints( try testInlayHints(
\\const Foo = struct { pub fn bar(self: Foo, alpha: u32, beta: []const u8) void {} }; \\const Foo = struct { pub fn bar(self: Foo, alpha: u32, beta: []const u8) void {} };
\\const _ = Foo.bar(<self>undefined,<alpha>5,<beta>""); \\const _ = Foo.bar(<self>undefined,<alpha>5,<beta>"");