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 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/server/hover.odin') 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 -- 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/hover.odin') 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 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/hover.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