From 962327425d04d9cb9bdba242602b89d409c1271c Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Mon, 29 Mar 2021 14:57:16 +0300 Subject: [PATCH] Fixed enum completion generation --- src/analysis.zig | 73 ++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index 6b0f853..6405ffb 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -2662,6 +2662,29 @@ fn makeScopeInternal( } } + const container_decl = switch (node_tag) { + .container_decl, .container_decl_trailing => tree.containerDecl(node_idx), + .container_decl_arg, .container_decl_arg_trailing => tree.containerDeclArg(node_idx), + .container_decl_two, .container_decl_two_trailing => blk: { + var buffer: [2]ast.Node.Index = undefined; + break :blk tree.containerDeclTwo(&buffer, node_idx); + }, + .tagged_union, .tagged_union_trailing => tree.taggedUnion(node_idx), + .tagged_union_enum_tag, .tagged_union_enum_tag_trailing => tree.taggedUnionEnumTag(node_idx), + .tagged_union_two, .tagged_union_two_trailing => blk: { + var buffer: [2]ast.Node.Index = undefined; + break :blk tree.taggedUnionTwo(&buffer, node_idx); + }, + else => null, + }; + + // Only tagged unions and enums should pass this + const can_have_enum_completions = if (container_decl) |container| blk: { + const kind = token_tags[container.ast.main_token]; + break :blk kind != .keyword_struct and + (kind != .keyword_union or container.ast.enum_token != null or container.ast.arg != 0); + } else false; + for (ast_decls) |*ptr_decl| { const decl = ptr_decl.*; if (tags[decl] == .@"usingnamespace") { @@ -2682,6 +2705,8 @@ fn makeScopeInternal( try tests.append(allocator, decl); continue; } + if (!can_have_enum_completions) + continue; const container_field = switch (tags[decl]) { .container_field => tree.containerField(decl), @@ -2691,45 +2716,15 @@ fn makeScopeInternal( }; if (container_field) |field| { - const empty_field = field.ast.type_expr == 0 and field.ast.value_expr == 0; - if (empty_field and node_tag == .root) { - continue; - } - - const container_decl = switch (node_tag) { - .container_decl, .container_decl_trailing => tree.containerDecl(node_idx), - .container_decl_arg, .container_decl_arg_trailing => tree.containerDeclArg(node_idx), - .container_decl_two, .container_decl_two_trailing => blk: { - var buffer: [2]ast.Node.Index = undefined; - break :blk tree.containerDeclTwo(&buffer, node_idx); - }, - .tagged_union, .tagged_union_trailing => tree.taggedUnion(node_idx), - .tagged_union_enum_tag, .tagged_union_enum_tag_trailing => tree.taggedUnionEnumTag(node_idx), - .tagged_union_two, .tagged_union_two_trailing => blk: { - var buffer: [2]ast.Node.Index = undefined; - break :blk tree.taggedUnionTwo(&buffer, node_idx); - }, - else => null, - }; - - if (container_decl) |container| { - const kind = token_tags[container.ast.main_token]; - if (empty_field and - (kind == .keyword_struct or (kind == .keyword_union and container.ast.arg == 0))) - { - continue; - } - - if (!std.mem.eql(u8, name, "_")) { - try enum_completions.put(allocator, .{ - .label = name, - .kind = .Constant, - .documentation = if (try getDocComments(allocator, tree, decl, .Markdown)) |docs| .{ - .kind = .Markdown, - .value = docs, - } else null, - }, {}); - } + if (!std.mem.eql(u8, name, "_")) { + try enum_completions.put(allocator, .{ + .label = name, + .kind = .Constant, + .documentation = if (try getDocComments(allocator, tree, decl, .Markdown)) |docs| .{ + .kind = .Markdown, + .value = docs, + } else null, + }, {}); } }