Skip to content

Commit

Permalink
consider anytype as a possible self parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Feb 21, 2024
1 parent b42459d commit 8cca7a1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,12 @@ pub fn firstParamIs(
func: Ast.full.FnProto,
expected: Type,
) error{OutOfMemory}!bool {
if (func.ast.params.len == 0) return false;

const tree = handle.tree;
var it = func.iterate(&tree);
const param = ast.nextFnParam(&it).?;
const param = ast.nextFnParam(&it) orelse return false;
if (param.anytype_ellipsis3) |token| {
if (tree.tokens.items(.tag)[token] == .keyword_anytype) return true;
}
if (param.type_expr == 0) return false;

const resolved_type = try analyser.resolveTypeOfNodeInternal(.{
Expand Down
34 changes: 32 additions & 2 deletions tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2091,7 +2091,7 @@ test "completion - anytype resolution based on callsite-references" {
});
}

test "builtin fn `field`" {
test "completion - builtin fn `field`" {
try testCompletion(
\\pub const chip_mod = struct {
\\ pub const devices = struct {
Expand Down Expand Up @@ -2248,7 +2248,7 @@ test "completion - combine doc comments of declaration and definition" {
});
}

test "hover - top-level doc comment" {
test "completion - top-level doc comment" {
try testCompletion(
\\//! B
\\
Expand All @@ -2270,6 +2270,36 @@ test "hover - top-level doc comment" {
});
}

test "completion - function `self` parameter detection" {
try testCompletion(
\\const S = struct {
\\ fn f(self: S) void {}
\\};
\\const s = S{};
\\s.<cursor>
, &.{
.{ .label = "f", .kind = .Method, .detail = "fn (self: S) void", .insert_text = "f()" },
});
try testCompletion(
\\const S = struct {
\\ fn f(self: @This()) void {}
\\};
\\const s = S{};
\\s.<cursor>
, &.{
.{ .label = "f", .kind = .Method, .detail = "fn (self: @This()) void", .insert_text = "f()" },
});
try testCompletion(
\\const S = struct {
\\ fn f(self: anytype) void {}
\\};
\\const s = S{};
\\s.<cursor>
, &.{
.{ .label = "f", .kind = .Method, .detail = "fn (self: anytype) void", .insert_text = "f()" },
});
}

test "completion - snippet - function with `self` parameter" {
try testCompletion(
\\const S = struct {
Expand Down
4 changes: 2 additions & 2 deletions tests/lsp_features/semantic_tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ test "semantic tokens - call" {
\\const alpha = foo(0);
, &.{
.{ "fn", .keyword, .{} },
.{ "foo", .function, .{ .declaration = true, .generic = true } },
.{ "foo", .method, .{ .declaration = true, .generic = true } },
.{ "a", .parameter, .{ .declaration = true } },
.{ "anytype", .type, .{} },
.{ "void", .type, .{} },
Expand All @@ -430,7 +430,7 @@ test "semantic tokens - call" {
.{ "const", .keyword, .{} },
.{ "alpha", .variable, .{ .declaration = true } },
.{ "=", .operator, .{} },
.{ "foo", .function, .{ .generic = true } },
.{ "foo", .method, .{ .generic = true } },
.{ "0", .number, .{} },
});
}
Expand Down
10 changes: 10 additions & 0 deletions tests/lsp_features/signature_help.zig
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ test "signature help - self parameter" {
, "fn foo(self: *@This(), a: u32, b: void) bool", 2);
}

test "signature help - self parameter is anytype" {
try testSignatureHelp(
\\const S = struct {
\\ fn foo(self: anytype, a: u32, b: void) bool {}
\\};
\\const s: S = undefined;
\\const foo = s.foo(3,<cursor>);
, "fn foo(self: anytype, a: u32, b: void) bool", 2);
}

test "signature help - anytype" {
try testSignatureHelp(
\\fn foo(a: u32, b: anytype, c: u32) void {
Expand Down

0 comments on commit 8cca7a1

Please sign in to comment.