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/documentation.odin | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/server/documentation.odin') 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, "}") -- 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/documentation.odin') 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