From 8fd95cac8b24ae6311009ea96cc9947a7b430d87 Mon Sep 17 00:00:00 2001 From: Brad Lewis <22850972+BradLewis@users.noreply.github.com> Date: Thu, 3 Jul 2025 12:36:27 -0400 Subject: Add enum completion to map keys --- src/server/completion.odin | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/server') diff --git a/src/server/completion.odin b/src/server/completion.odin index 4544143..b4f86a8 100644 --- a/src/server/completion.odin +++ b/src/server/completion.odin @@ -1196,8 +1196,24 @@ get_implicit_completion :: proc( symbol, ok = resolve_type_expression(ast_context, position_context.index.expr) } - if array, ok := symbol.value.(SymbolFixedArrayValue); ok { - if enum_value, ok := unwrap_enum(ast_context, array.len); ok { + #partial switch v in symbol.value { + case SymbolFixedArrayValue: + if enum_value, ok := unwrap_enum(ast_context, v.len); ok { + for name in enum_value.names { + item := CompletionItem { + label = name, + kind = .EnumMember, + detail = name, + } + + append(&items, item) + } + + list.items = items[:] + return + } + case SymbolMapValue: + if enum_value, ok := unwrap_enum(ast_context, v.key); ok { for name in enum_value.names { item := CompletionItem { label = name, -- cgit v1.2.3 From 650ddc73125112ad8e4fe638114c0cda2854c993 Mon Sep 17 00:00:00 2001 From: Brad Lewis <22850972+BradLewis@users.noreply.github.com> Date: Thu, 3 Jul 2025 13:30:30 -0400 Subject: Add hover information to enum implicit and explicit assignments --- src/server/hover.odin | 13 +++++++++++++ tests/hover_test.odin | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) (limited to 'src/server') diff --git a/src/server/hover.odin b/src/server/hover.odin index 52663a8..aaddd08 100644 --- a/src/server/hover.odin +++ b/src/server/hover.odin @@ -301,6 +301,19 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> } } } + case SymbolEnumValue: + for name, i in v.names { + if name == field { + symbol := Symbol { + name = selector.name, + pkg = selector.pkg, + } + // TODO: update this to go through some kind of common `get_signature` + symbol.signature = fmt.tprintf(".%s", name) + hover.contents = write_hover_content(&ast_context, symbol) + return hover, true, true + } + } } } else if position_context.implicit_selector_expr != nil { implicit_selector := position_context.implicit_selector_expr diff --git a/tests/hover_test.odin b/tests/hover_test.odin index 3d2ec9d..7359260 100644 --- a/tests/hover_test.odin +++ b/tests/hover_test.odin @@ -1899,6 +1899,42 @@ ast_hover_bitset_enum_for_loop :: proc(t: ^testing.T) { } test.expect_hover(t, &source, "test.f: test.Foo :: enum {\n\tA,\n\tB,\n}") } + +@(test) +ast_hover_enum_field_assignment :: proc(t: ^testing.T) { + source :=test.Source { + main = `package test + Foo :: enum { + A, + B, + } + + main :: proc() { + a := Foo.A{*} + } + ` + } + test.expect_hover(t, &source, "test.Foo: .A") +} + + +@(test) +ast_hover_enum_field_implicit_assignment :: proc(t: ^testing.T) { + source :=test.Source { + main = `package test + Foo :: enum { + A, + B, + } + + main :: proc() { + a: Foo + a = .A{*} + } + ` + } + test.expect_hover(t, &source, "test.Foo: .A") +} /* Waiting for odin fix -- cgit v1.2.3 From 5ff21d6abb6747f584b488a61d2048e298dbf786 Mon Sep 17 00:00:00 2001 From: Brad Lewis <22850972+BradLewis@users.noreply.github.com> Date: Thu, 3 Jul 2025 16:49:07 -0400 Subject: Add hover information for enum fields --- src/server/hover.odin | 50 ++++++++++++++++++++++++++++++++++++++++++-------- tests/hover_test.odin | 26 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 8 deletions(-) (limited to 'src/server') diff --git a/src/server/hover.odin b/src/server/hover.odin index aaddd08..39f6814 100644 --- a/src/server/hover.odin +++ b/src/server/hover.odin @@ -112,12 +112,44 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> } } - if position_context.struct_type != nil { - for field in position_context.struct_type.fields.list { - for name in field.names { - if position_in_node(name, position_context.position) { - if identifier, ok := name.derived.(^ast.Ident); ok && field.type != nil { - if position_context.value_decl != nil && len(position_context.value_decl.names) != 0 { + if position_context.value_decl != nil && len(position_context.value_decl.names) != 0 { + if position_context.enum_type != nil { + if enum_symbol, ok := resolve_type_expression(&ast_context, position_context.value_decl.names[0]); ok { + for field in position_context.enum_type.fields { + if ident, ok := field.derived.(^ast.Ident); ok { + if position_in_node(ident, position_context.position) { + symbol := Symbol { + pkg = ast_context.current_package, + name = enum_symbol.name, + range = common.get_token_range(ident, ast_context.file.src), + signature = fmt.tprintf(".%s", ident.name), + } + hover.contents = write_hover_content(&ast_context, symbol) + return hover, true, true + } + } else if value, ok := field.derived.(^ast.Field_Value); ok { + if position_in_node(value.field, position_context.position) { + if ident, ok := value.field.derived.(^ast.Ident); ok { + symbol := Symbol { + pkg = ast_context.current_package, + range = common.get_token_range(value.field, ast_context.file.src), + name = enum_symbol.name, + signature = fmt.tprintf(".%s", ident.name), + } + hover.contents = write_hover_content(&ast_context, symbol) + } + return hover, true, true + } + } + } + } + } + + if position_context.struct_type != nil { + for field in position_context.struct_type.fields.list { + for name in field.names { + if position_in_node(name, position_context.position) { + if identifier, ok := name.derived.(^ast.Ident); ok && field.type != nil { if symbol, ok := resolve_type_expression(&ast_context, field.type); ok { if struct_symbol, ok := resolve_type_expression( &ast_context, @@ -139,7 +171,9 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> break } } - if index != -1 && value.comments[index] != nil && len(value.comments[index].list) > 0 { + if index != -1 && + value.comments[index] != nil && + len(value.comments[index].list) > 0 { symbol.comment = value.comments[index].list[0].text } } @@ -306,7 +340,7 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> if name == field { symbol := Symbol { name = selector.name, - pkg = selector.pkg, + pkg = selector.pkg, } // TODO: update this to go through some kind of common `get_signature` symbol.signature = fmt.tprintf(".%s", name) diff --git a/tests/hover_test.odin b/tests/hover_test.odin index 7359260..18c2ced 100644 --- a/tests/hover_test.odin +++ b/tests/hover_test.odin @@ -1935,6 +1935,32 @@ ast_hover_enum_field_implicit_assignment :: proc(t: ^testing.T) { } test.expect_hover(t, &source, "test.Foo: .A") } + +@(test) +ast_hover_enum_field_definition :: proc(t: ^testing.T) { + source :=test.Source { + main = `package test + Foo :: enum { + A{*}, + B, + } + ` + } + test.expect_hover(t, &source, "test.Foo: .A") +} + +@(test) +ast_hover_enum_field_definition_with_type :: proc(t: ^testing.T) { + source :=test.Source { + main = `package test + Foo :: enum { + A{*} = 1, + B, + } + ` + } + test.expect_hover(t, &source, "test.Foo: .A") +} /* Waiting for odin fix -- cgit v1.2.3 From 2edc388c40b4431cdfdc3d915fe9a139768aa783 Mon Sep 17 00:00:00 2001 From: Brad Lewis <22850972+BradLewis@users.noreply.github.com> Date: Thu, 3 Jul 2025 16:59:11 -0400 Subject: Add SymbolMapValue to selector resolve in the index case --- src/server/analysis.odin | 7 +++++-- tests/hover_test.odin | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'src/server') diff --git a/src/server/analysis.odin b/src/server/analysis.odin index 7170935..0a2fe1d 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -1842,8 +1842,11 @@ resolve_implicit_selector :: proc( } } - if array, ok := symbol.value.(SymbolFixedArrayValue); ok { - return resolve_type_expression(ast_context, array.len) + #partial switch value in symbol.value { + case SymbolFixedArrayValue: + return resolve_type_expression(ast_context, value.len) + case SymbolMapValue: + return resolve_type_expression(ast_context, value.key) } } diff --git a/tests/hover_test.odin b/tests/hover_test.odin index 18c2ced..5e6a66f 100644 --- a/tests/hover_test.odin +++ b/tests/hover_test.odin @@ -1961,6 +1961,23 @@ ast_hover_enum_field_definition_with_type :: proc(t: ^testing.T) { } test.expect_hover(t, &source, "test.Foo: .A") } + +@(test) +ast_hover_enum_map_key :: proc(t: ^testing.T) { + source :=test.Source { + main = `package test + Foo :: enum { + A = 1, + B, + } + main :: proc() { + m: map[Foo]int + m[.A{*}] = 2 + } + ` + } + test.expect_hover(t, &source, "test.Foo: .A") +} /* Waiting for odin fix -- cgit v1.2.3 From 85d65fcfb761b49df6c03e1ed8e0523556c0bb62 Mon Sep 17 00:00:00 2001 From: Brad Lewis <22850972+BradLewis@users.noreply.github.com> Date: Thu, 3 Jul 2025 18:37:02 -0400 Subject: Add enum base type and values to the enum symbol and update hover to include that information --- src/server/analysis.odin | 20 ++++++++++++-------- src/server/collector.odin | 19 +++++++++++-------- src/server/documentation.odin | 18 +++++++++++++++++- src/server/symbol.odin | 11 ++++++++--- tests/hover_test.odin | 14 ++++++++++++++ 5 files changed, 62 insertions(+), 20 deletions(-) (limited to 'src/server') diff --git a/src/server/analysis.odin b/src/server/analysis.odin index 0a2fe1d..597c2cd 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -2757,33 +2757,37 @@ make_symbol_enum_from_ast :: proc( names := make([dynamic]string, ast_context.allocator) ranges := make([dynamic]common.Range, ast_context.allocator) + values := make([dynamic]^ast.Expr, ast_context.allocator) for n in v.fields { - name, range := get_enum_field_name_and_range(n, ast_context.file.src) + name, range, value := get_enum_field_name_range_value(n, ast_context.file.src) append(&names, name) append(&ranges, range) + append(&values, value) } symbol.value = SymbolEnumValue { - names = names[:], - ranges = ranges[:], + names = names[:], + ranges = ranges[:], + base_type = v.base_type, + values = values[:], } return symbol } -get_enum_field_name_and_range :: proc(n: ^ast.Expr, document_text: string) -> (string, common.Range) { +get_enum_field_name_range_value :: proc(n: ^ast.Expr, document_text: string) -> (string, common.Range, ^ast.Expr) { if ident, ok := n.derived.(^ast.Ident); ok { - return ident.name, common.get_token_range(ident, document_text) + return ident.name, common.get_token_range(ident, document_text), nil } if field, ok := n.derived.(^ast.Field_Value); ok { if ident, ok := field.field.derived.(^ast.Ident); ok { - return ident.name, common.get_token_range(ident, document_text) + return ident.name, common.get_token_range(ident, document_text), field.value } else if binary, ok := field.field.derived.(^ast.Binary_Expr); ok { - return binary.left.derived.(^ast.Ident).name, common.get_token_range(binary, document_text) + return binary.left.derived.(^ast.Ident).name, common.get_token_range(binary, document_text), binary.right } } - return "", {} + return "", {}, nil } make_symbol_bitset_from_ast :: proc( diff --git a/src/server/collector.odin b/src/server/collector.odin index b2c2d66..22aba08 100644 --- a/src/server/collector.odin +++ b/src/server/collector.odin @@ -151,7 +151,7 @@ collect_struct_fields :: proc( } value := to_symbol_struct_value(b) - value.poly = cast(^ast.Field_List)clone_type(struct_type.poly_params, collection.allocator, &collection.unique_strings) + value.poly = cast(^ast.Field_List)clone_type(struct_type.poly_params, collection.allocator, &collection.unique_strings) return value } @@ -189,23 +189,26 @@ collect_bit_field_fields :: proc( collect_enum_fields :: proc( collection: ^SymbolCollection, - fields: []^ast.Expr, + enum_type: ast.Enum_Type, package_map: map[string]string, file: ast.File, ) -> SymbolEnumValue { names := make([dynamic]string, 0, collection.allocator) ranges := make([dynamic]common.Range, 0, collection.allocator) + values := make([dynamic]^ast.Expr, 0, collection.allocator) - //ERROR no hover on n in the for, but elsewhere is fine - for n in fields { - name, range := get_enum_field_name_and_range(n, file.src) + for n in enum_type.fields { + name, range, value := get_enum_field_name_range_value(n, file.src) append(&names, strings.clone(name, collection.allocator)) append(&ranges, range) + append(&values, clone_type(value, collection.allocator, &collection.unique_strings)) } value := SymbolEnumValue { - names = names[:], - ranges = ranges[:], + names = names[:], + ranges = ranges[:], + values = values[:], + base_type = clone_type(enum_type.base_type, collection.allocator, &collection.unique_strings), } return value @@ -529,7 +532,7 @@ collect_symbols :: proc(collection: ^SymbolCollection, file: ast.File, uri: stri case ^ast.Enum_Type: token = v^ token_type = .Enum - symbol.value = collect_enum_fields(collection, v.fields, package_map, file) + symbol.value = collect_enum_fields(collection, v^, package_map, file) symbol.signature = "enum" case ^ast.Union_Type: token = v^ diff --git a/src/server/documentation.odin b/src/server/documentation.odin index c0853d7..26dd9cf 100644 --- a/src/server/documentation.odin +++ b/src/server/documentation.odin @@ -19,10 +19,26 @@ get_signature :: proc(ast_context: ^AstContext, symbol: Symbol) -> string { append_variable_full_name(&sb, ast_context, symbol, pointer_prefix) strings.write_string(&sb, " :: ") } - strings.write_string(&sb, "enum {\n") + + longestNameLen := 0 + for name in v.names { + if len(name) > longestNameLen { + longestNameLen = len(name) + } + } + strings.write_string(&sb, "enum ") + if v.base_type != nil { + build_string_node(v.base_type, &sb, false) + strings.write_string(&sb, " ") + } + strings.write_string(&sb, "{\n") for i in 0 ..< len(v.names) { strings.write_string(&sb, "\t") strings.write_string(&sb, v.names[i]) + if v.values[i] != nil { + fmt.sbprintf(&sb, "%*s= ", longestNameLen - len(v.names[i]) + 1, "") + build_string_node(v.values[i], &sb, false) + } strings.write_string(&sb, ",\n") } strings.write_string(&sb, "}") diff --git a/src/server/symbol.odin b/src/server/symbol.odin index 2d99d22..fddc775 100644 --- a/src/server/symbol.odin +++ b/src/server/symbol.odin @@ -67,8 +67,10 @@ SymbolAggregateValue :: struct { } SymbolEnumValue :: struct { - names: []string, - ranges: []common.Range, + names: []string, + values: []^ast.Expr, + base_type: ^ast.Expr, + ranges: []common.Range, } SymbolUnionValue :: struct { @@ -345,7 +347,10 @@ write_struct_type :: proc( } write_symbol_struct_value :: proc( - ast_context: ^AstContext, b: ^SymbolStructValueBuilder, v: SymbolStructValue, base_using_index: int + ast_context: ^AstContext, + b: ^SymbolStructValueBuilder, + v: SymbolStructValue, + base_using_index: int, ) { base_index := len(b.names) for name in v.names { diff --git a/tests/hover_test.odin b/tests/hover_test.odin index 5e6a66f..194fbea 100644 --- a/tests/hover_test.odin +++ b/tests/hover_test.odin @@ -1978,6 +1978,20 @@ ast_hover_enum_map_key :: proc(t: ^testing.T) { } test.expect_hover(t, &source, "test.Foo: .A") } + +@(test) +ast_hover_enum_defintion_with_base_type :: proc(t: ^testing.T) { + source :=test.Source { + main = `package test + F{*}oo :: enum u8 { + A = 1, + Bar = 2, + C = 3, + } + ` + } + test.expect_hover(t, &source, "test.Foo: enum u8 {\n\tA = 1,\n\tBar = 2,\n\tC = 3,\n}") +} /* Waiting for odin fix -- cgit v1.2.3 From eac2491844c158cbf70146f3b61a9b82a87b8de5 Mon Sep 17 00:00:00 2001 From: Brad Lewis <22850972+BradLewis@users.noreply.github.com> Date: Thu, 3 Jul 2025 19:33:23 -0400 Subject: Add enum values to field hover documentation --- src/server/documentation.odin | 12 ++++++++- src/server/hover.odin | 61 +++++++++++++++++++++++++------------------ tests/definition_test.odin | 25 +++++++++++++++++- tests/hover_test.odin | 4 +-- 4 files changed, 72 insertions(+), 30 deletions(-) (limited to 'src/server') diff --git a/src/server/documentation.odin b/src/server/documentation.odin index 26dd9cf..b52d259 100644 --- a/src/server/documentation.odin +++ b/src/server/documentation.odin @@ -35,7 +35,7 @@ get_signature :: proc(ast_context: ^AstContext, symbol: Symbol) -> string { for i in 0 ..< len(v.names) { strings.write_string(&sb, "\t") strings.write_string(&sb, v.names[i]) - if v.values[i] != nil { + if i < len(v.values) && v.values[i] != nil { fmt.sbprintf(&sb, "%*s= ", longestNameLen - len(v.names[i]) + 1, "") build_string_node(v.values[i], &sb, false) } @@ -227,6 +227,16 @@ get_short_signature :: proc(ast_context: ^AstContext, symbol: Symbol) -> string return "" } +get_enum_field_signature :: proc(value: SymbolEnumValue, index: int) -> string { + sb := strings.builder_make(context.temp_allocator) + fmt.sbprintf(&sb, ".%s", value.names[index]) + if index < len(value.values) && value.values[index] != nil { + strings.write_string(&sb, " = ") + build_string_node(value.values[index], &sb, false) + } + return strings.to_string(sb) +} + write_symbol_type_information :: proc(ast_context: ^AstContext, sb: ^strings.Builder, symbol: Symbol, pointer_prefix: string) { append_type_pkg := false pkg_name := get_pkg_name(ast_context, symbol.type_pkg) diff --git a/src/server/hover.odin b/src/server/hover.odin index 39f6814..4965621 100644 --- a/src/server/hover.odin +++ b/src/server/hover.odin @@ -115,30 +115,40 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> if position_context.value_decl != nil && len(position_context.value_decl.names) != 0 { if position_context.enum_type != nil { if enum_symbol, ok := resolve_type_expression(&ast_context, position_context.value_decl.names[0]); ok { - for field in position_context.enum_type.fields { - if ident, ok := field.derived.(^ast.Ident); ok { - if position_in_node(ident, position_context.position) { - symbol := Symbol { - pkg = ast_context.current_package, - name = enum_symbol.name, - range = common.get_token_range(ident, ast_context.file.src), - signature = fmt.tprintf(".%s", ident.name), + if v, ok := enum_symbol.value.(SymbolEnumValue); ok { + for field in position_context.enum_type.fields { + if ident, ok := field.derived.(^ast.Ident); ok { + if position_in_node(ident, position_context.position) { + for name, i in v.names { + if name == ident.name { + symbol := Symbol { + pkg = ast_context.current_package, + name = enum_symbol.name, + range = common.get_token_range(ident, ast_context.file.src), + signature = get_enum_field_signature(v, i), + } + hover.contents = write_hover_content(&ast_context, symbol) + return hover, true, true + } + } } - hover.contents = write_hover_content(&ast_context, symbol) - return hover, true, true - } - } else if value, ok := field.derived.(^ast.Field_Value); ok { - if position_in_node(value.field, position_context.position) { - if ident, ok := value.field.derived.(^ast.Ident); ok { - symbol := Symbol { - pkg = ast_context.current_package, - range = common.get_token_range(value.field, ast_context.file.src), - name = enum_symbol.name, - signature = fmt.tprintf(".%s", ident.name), + } else if value, ok := field.derived.(^ast.Field_Value); ok { + if position_in_node(value.field, position_context.position) { + if ident, ok := value.field.derived.(^ast.Ident); ok { + for name, i in v.names { + if name == ident.name { + symbol := Symbol { + pkg = ast_context.current_package, + range = common.get_token_range(value.field, ast_context.file.src), + name = enum_symbol.name, + signature = get_enum_field_signature(v, i), + } + hover.contents = write_hover_content(&ast_context, symbol) + } + } } - hover.contents = write_hover_content(&ast_context, symbol) + return hover, true, true } - return hover, true, true } } } @@ -339,11 +349,10 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> for name, i in v.names { if name == field { symbol := Symbol { - name = selector.name, - pkg = selector.pkg, + name = selector.name, + pkg = selector.pkg, + signature = get_enum_field_signature(v, i), } - // TODO: update this to go through some kind of common `get_signature` - symbol.signature = fmt.tprintf(".%s", name) hover.contents = write_hover_content(&ast_context, symbol) return hover, true, true } @@ -356,7 +365,7 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> case SymbolEnumValue: for name, i in v.names { if strings.compare(name, implicit_selector.field.name) == 0 { - symbol.signature = fmt.tprintf(".%s", name) + symbol.signature = get_enum_field_signature(v, i) hover.contents = write_hover_content(&ast_context, symbol) return hover, true, true } diff --git a/tests/definition_test.odin b/tests/definition_test.odin index 981adf2..6a61449 100644 --- a/tests/definition_test.odin +++ b/tests/definition_test.odin @@ -471,7 +471,6 @@ ast_goto_variable_field_definition_with_selector_expr :: proc(t: ^testing.T) { test.expect_definition_locations(t, &source, {location}) } - @(test) ast_goto_struct_definition_with_empty_line_at_top_of_file :: proc(t: ^testing.T) { source := test.Source { @@ -494,3 +493,27 @@ ast_goto_struct_definition_with_empty_line_at_top_of_file :: proc(t: ^testing.T) test.expect_definition_locations(t, &source, {location}) } + +@(test) +ast_goto_enum_from_map_key :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + + Foo :: enum { + A, + B, + } + + main :: proc() { + m: map[Foo]int + m[.A{*}] = 2 + } + `, + } + + location := common.Location { + range = {start = {line = 3, character = 3}, end = {line = 3, character = 4}}, + } + + test.expect_definition_locations(t, &source, {location}) +} diff --git a/tests/hover_test.odin b/tests/hover_test.odin index 194fbea..20e9643 100644 --- a/tests/hover_test.odin +++ b/tests/hover_test.odin @@ -1959,7 +1959,7 @@ ast_hover_enum_field_definition_with_type :: proc(t: ^testing.T) { } ` } - test.expect_hover(t, &source, "test.Foo: .A") + test.expect_hover(t, &source, "test.Foo: .A = 1") } @(test) @@ -1976,7 +1976,7 @@ ast_hover_enum_map_key :: proc(t: ^testing.T) { } ` } - test.expect_hover(t, &source, "test.Foo: .A") + test.expect_hover(t, &source, "test.Foo: .A = 1") } @(test) -- cgit v1.2.3