diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2021-03-17 23:20:03 +0100 |
|---|---|---|
| committer | DanielGavin <danielgavin5@hotmail.com> | 2021-03-17 23:20:03 +0100 |
| commit | ddec7b7488fb52d5eb43e6a6aa849058856da39c (patch) | |
| tree | 60b6c029f2083f26659ddc520fc3abd35a641244 /src | |
| parent | 8f530ec8e275f447e2c7fb4738b86abf729ec916 (diff) | |
fixed union crash
Diffstat (limited to 'src')
| -rw-r--r-- | src/index/collector.odin | 4 | ||||
| -rw-r--r-- | src/server/completion.odin | 78 |
2 files changed, 66 insertions, 16 deletions
diff --git a/src/index/collector.odin b/src/index/collector.odin index 4bf2aca..410a92a 100644 --- a/src/index/collector.odin +++ b/src/index/collector.odin @@ -157,11 +157,11 @@ collect_union_fields :: proc (collection: ^SymbolCollection, union_type: ast.Uni } else if selector, ok := variant.derived.(ast.Selector_Expr); ok { if ident, ok := selector.field.derived.(ast.Ident); ok { - append(&names, ident.name); + append(&names, get_index_unique_string(collection, ident.name)); } } - append(&types, variant); + append(&types, clone_type(variant, collection.allocator, &collection.unique_strings)); } value := SymbolUnionValue { diff --git a/src/server/completion.odin b/src/server/completion.odin index e19bd0a..9793845 100644 --- a/src/server/completion.odin +++ b/src/server/completion.odin @@ -16,6 +16,14 @@ import "core:slice" import "shared:common" import "shared:index" +Completion_Type :: enum { + Implicit, + Selector, + Switch_Type, + Identifier, + Comp_Lit, +} + get_completion_list :: proc (document: ^Document, position: common.Position) -> (CompletionList, bool) { list: CompletionList; @@ -33,16 +41,47 @@ get_completion_list :: proc (document: ^Document, position: common.Position) -> get_locals(document.ast, position_context.function, &ast_context, &position_context); } + completion_type: Completion_Type = .Identifier; + if position_context.implicit { - get_implicit_completion(&ast_context, &position_context, &list); - } else if position_context.switch_type_stmt != nil && position_context.case_clause != nil { - get_type_switch_Completion(&ast_context, &position_context, &list); - } else if position_context.comp_lit != nil && is_lhs_comp_lit(&position_context) { + completion_type = .Implicit; + } + + if position_context.comp_lit != nil && is_lhs_comp_lit(&position_context) { + completion_type = .Comp_Lit; + } + + if position_context.selector != nil { + completion_type = .Selector; + } + + if position_context.switch_type_stmt != nil && position_context.case_clause != nil { + + if assign, ok := position_context.switch_type_stmt.tag.derived.(ast.Assign_Stmt); ok && assign.rhs != nil && len(assign.rhs) == 1 { + + if symbol, ok := resolve_type_expression(&ast_context, assign.rhs[0]); ok { + + if union_value, ok := symbol.value.(index.SymbolUnionValue); ok { + completion_type = .Switch_Type; + } + + } + + } + + } + + switch completion_type { + case .Comp_Lit: get_comp_lit_completion(&ast_context, &position_context, &list); - } else if position_context.selector != nil { - get_selector_completion(&ast_context, &position_context, &list); - } else { + case .Identifier: get_identifier_completion(&ast_context, &position_context, &list); + case .Implicit: + get_implicit_completion(&ast_context, &position_context, &list); + case .Selector: + get_selector_completion(&ast_context, &position_context, &list); + case .Switch_Type: + get_type_switch_Completion(&ast_context, &position_context, &list); } return list, true; @@ -813,19 +852,30 @@ get_type_switch_Completion :: proc (ast_context: ^AstContext, position_context: if union_value, ok := unwrap_union(ast_context, assign.rhs[0]); ok { - for name in union_value.names { + for name, i in union_value.names { if name in used_unions { continue; } - item := CompletionItem { - label = name, - kind = .EnumMember, - detail = name, - }; + if symbol, ok := resolve_type_expression(ast_context, union_value.types[i]); ok { - append(&items, item); + item := CompletionItem { + kind = .EnumMember, + }; + + if symbol.pkg == ast_context.document_package { + item.label = fmt.aprintf("%v", name); + item.detail = item.label; + } + + else { + item.label = fmt.aprintf("%v.%v", path.base(symbol.pkg, false, context.temp_allocator), name); + item.detail = item.label; + } + + append(&items, item); + } } } } |