Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix completions on non-exhaustive enums #1270

Merged
merged 2 commits into from
Jun 25, 2023
Merged
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
40 changes: 23 additions & 17 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,8 @@ pub fn getDeclNameToken(tree: Ast, node: Ast.Node.Index) ?Ast.TokenIndex {
.container_field,
.container_field_init,
.container_field_align,
=> {
const field = tree.fullContainerField(node).?.ast;
return field.main_token;
},

.identifier => main_token,
.identifier,
=> main_token,
.error_value => {
const tok = main_token + 2;
return if (tok >= tree.tokens.len)
Expand Down Expand Up @@ -2227,17 +2223,27 @@ fn iterateSymbolsContainerInternal(
for (scope_decls[container_scope_index].values()) |decl_index| {
const decl = &handle.document_scope.decls.items[@intFromEnum(decl_index)];
switch (decl.*) {
.ast_node => |node| {
if (node_tags[node].isContainerField()) {
if (!instance_access and !is_enum) continue;
if (instance_access and is_enum) continue;
} else if (node_tags[node] == .global_var_decl or
node_tags[node] == .local_var_decl or
node_tags[node] == .simple_var_decl or
node_tags[node] == .aligned_var_decl)
{
.ast_node => |node| switch (node_tags[node]) {
.container_field_init,
.container_field_align,
.container_field,
=> {
if (is_enum) {
if (instance_access) continue;
const field_name = offsets.tokenToSlice(tree, tree.nodes.items(.main_token)[node]);
if (std.mem.eql(u8, field_name, "_")) continue;
} else {
if (!instance_access) continue;
}
},
.global_var_decl,
.local_var_decl,
.simple_var_decl,
.aligned_var_decl,
=> {
if (instance_access) continue;
}
},
else => {},
},
.label_decl => continue,
else => {},
Expand Down Expand Up @@ -2729,7 +2735,7 @@ fn makeInnerScope(context: ScopeContext, tree: Ast, node_idx: Ast.Node.Index) er
if ((node_idx != 0 and token_tags[container_decl.ast.main_token] == .keyword_enum) or
container_decl.ast.enum_token != null)
{
if (std.mem.eql(u8, name, "_")) return;
if (std.mem.eql(u8, name, "_")) continue;

const doc = try getDocComments(allocator, tree, decl, .markdown);
errdefer if (doc) |d| allocator.free(d);
Expand Down
19 changes: 19 additions & 0 deletions tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,25 @@ test "completion - enum" {
.{ .label = "alpha", .kind = .EnumMember },
.{ .label = "beta", .kind = .EnumMember },
});
try testCompletion(
\\const E = enum {
\\ _,
\\ fn inner(_: E) void {}
\\};
\\const foo = E.<cursor>
, &.{
.{ .label = "inner", .kind = .Function, .detail = "fn inner(_: E) void" },
});
try testCompletion(
\\const E = enum {
\\ _,
\\ fn inner(_: E) void {}
\\};
\\const e: E = undefined;
\\const foo = e.<cursor>
, &.{
.{ .label = "inner", .kind = .Function, .detail = "fn inner(_: E) void" },
});
}

test "completion - error union" {
Expand Down