From 4ca8f82c9c5ac743dab5e767311a7cc08027bed5 Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Sun, 25 Jun 2023 23:18:50 +0200 Subject: [PATCH 1/2] exclude `_` enum member from completion --- src/analysis.zig | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index b0423fdcb..51d62d752 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -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) @@ -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 => {}, From 63784974fc0daa0ae3e411fb1ba567f155168650 Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Sun, 25 Jun 2023 22:22:43 +0200 Subject: [PATCH 2/2] do not stop iterating container members in non-exhaustive enum --- src/analysis.zig | 2 +- tests/lsp_features/completion.zig | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/analysis.zig b/src/analysis.zig index 51d62d752..be7827953 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -2735,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); diff --git a/tests/lsp_features/completion.zig b/tests/lsp_features/completion.zig index 77d216427..76dd8c1d6 100644 --- a/tests/lsp_features/completion.zig +++ b/tests/lsp_features/completion.zig @@ -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. + , &.{ + .{ .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. + , &.{ + .{ .label = "inner", .kind = .Function, .detail = "fn inner(_: E) void" }, + }); } test "completion - error union" {