Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1770,7 +1770,7 @@ fn collectFieldAccessTypes(
dot_context: EnumLiteralContext,
ty: Analyser.Type,
types_with_handles: *Analyser.Type.ArraySet,
) error{OutOfMemory}!void {
) error{ OutOfMemory, Canceled }!void {
const analyser = builder.analyser;

var node_type = ty;
Expand All @@ -1791,9 +1791,26 @@ fn collectFieldAccessTypes(
_ = try node_type.getAllTypesWithHandlesArraySet(analyser, types_with_handles);
return;
}
const has_self_param = try analyser.hasSelfParam(node_type);

const param_offset: usize = offs: {
const tree = &builder.orig_handle.tree;
const idx = switch (dot_context.type_info) {
.identifier_token_index => |idx| idx,
.expr_node_index => break :offs 0,
};

if (tree.tokenTag(idx - 1) != .period) break :offs 0;
const nodes = try ast.nodesOverlappingIndexIncludingParseErrors(analyser.arena, tree, tree.tokenStart(idx - 2));
defer analyser.arena.free(nodes);

const node = nodes[0];
const typ = try analyser.resolveTypeOfNode(.of(node, builder.orig_handle)) orelse break :offs 0;
if (typ.is_type_val) break :offs 0;
break :offs @intFromBool(analyser.firstParamIs(node_type, try typ.typeOf(analyser)));
};

const params = info.parameters;
const param_index = dot_context.fn_arg_index + @intFromBool(has_self_param);
const param_index = dot_context.fn_arg_index + param_offset;
if (param_index >= params.len) return;
const param_type = params[param_index].type;
_ = try param_type.getAllTypesWithHandlesArraySet(analyser, types_with_handles);
Expand Down
24 changes: 24 additions & 0 deletions tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,30 @@ test "function alias" {
.detail = "u32",
},
});
try testCompletion(
\\const S = struct {
\\ fn foo(_: S, _: enum{ alpha, beta }) void {}
\\ const bar = foo;
\\};
\\const s: S = undefined;
\\const baz = s.bar(.<cursor>);
, &.{
.{ .label = "alpha", .kind = .EnumMember },
.{ .label = "beta", .kind = .EnumMember },
});
try testCompletion(
\\const other_cont = struct {
\\ fn testFn(_: Test, _: enum { alpha, beta }) void {}
\\};
\\const Test = struct {
\\ pub const testFn = other_cont.testFn;
\\};
\\const idk: Test = undefined;
\\idk.testFn(.<cursor>);
, &.{
.{ .label = "alpha", .kind = .EnumMember },
.{ .label = "beta", .kind = .EnumMember },
});
}

test "generic function" {
Expand Down
Loading