From 82c8a0b2cb1373afeb9f87339cc3aaccdffb31ff Mon Sep 17 00:00:00 2001 From: Brad Lewis <22850972+BradLewis@users.noreply.github.com> Date: Sat, 12 Jul 2025 13:34:12 -0400 Subject: Add comments when hovering basic struct fields and no longer show the underlying type --- src/server/documentation.odin | 2 +- src/server/hover.odin | 29 +++++++++++--------- src/server/symbol.odin | 5 ---- tests/hover_test.odin | 62 ++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 75 insertions(+), 23 deletions(-) diff --git a/src/server/documentation.odin b/src/server/documentation.odin index 733851b..df0b523 100644 --- a/src/server/documentation.odin +++ b/src/server/documentation.odin @@ -174,7 +174,7 @@ get_short_signature :: proc(ast_context: ^AstContext, symbol: Symbol) -> string strings.write_string(&sb, pointer_prefix) build_string_node(v.ident, &sb, false) } - if symbol.type == .Field && symbol.comment != "" { + if symbol.comment != "" { fmt.sbprintf(&sb, " %s", symbol.comment) } return strings.to_string(sb) diff --git a/src/server/hover.odin b/src/server/hover.odin index 7ced6c1..7044ad2 100644 --- a/src/server/hover.odin +++ b/src/server/hover.odin @@ -165,19 +165,20 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> &ast_context, position_context.value_decl.names[0], ); ok { - symbol.type = .Field - symbol.range = common.get_token_range(field.node, ast_context.file.src) - symbol.type_name = symbol.name - symbol.type_pkg = symbol.pkg - symbol.pkg = struct_symbol.name - symbol.name = identifier.name if value, ok := struct_symbol.value.(SymbolStructValue); ok { + symbol.type = .Field + symbol.range = common.get_token_range(field.node, ast_context.file.src) + symbol.type_name = symbol.name + symbol.type_pkg = symbol.pkg + symbol.pkg = struct_symbol.name + symbol.name = identifier.name symbol.comment = get_comment(value.comments[field_index + name_index]) - } + symbol.doc = get_doc(value.docs[field_index + name_index], context.temp_allocator) - symbol.signature = get_short_signature(&ast_context, symbol) - hover.contents = write_hover_content(&ast_context, symbol) - return hover, true, true + symbol.signature = get_short_signature(&ast_context, symbol) + hover.contents = write_hover_content(&ast_context, symbol) + return hover, true, true + } } } } @@ -202,11 +203,11 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> symbol.type_pkg = symbol.pkg symbol.pkg = bit_field_symbol.name symbol.name = identifier.name - symbol.signature = get_bit_field_field_signature(value, i) - if value, ok := bit_field_symbol.value.(SymbolBitFieldValue); ok { symbol.comment = get_comment(value.comments[i]) + symbol.doc = get_doc(value.docs[i], context.temp_allocator) } + symbol.signature = get_bit_field_field_signature(value, i) hover.contents = write_hover_content(&ast_context, symbol) return hover, true, true } @@ -320,7 +321,9 @@ get_hover_information :: proc(document: ^Document, position: common.Position) -> symbol.type_pkg = symbol.pkg symbol.name = name symbol.pkg = selector.name - symbol.signature = get_signature(&ast_context, symbol) + symbol.comment = get_comment(v.comments[i]) + symbol.doc = get_doc(v.docs[i], context.temp_allocator) + symbol.signature = get_short_signature(&ast_context, symbol) hover.contents = write_hover_content(&ast_context, symbol) return hover, true, true } diff --git a/src/server/symbol.odin b/src/server/symbol.odin index 5ca9c17..08a6788 100644 --- a/src/server/symbol.odin +++ b/src/server/symbol.odin @@ -19,11 +19,6 @@ SymbolAndNode :: struct { node: ^ast.Node, } -UsingInfo :: struct { - from_index: int, - is_using: bool, -} - SymbolStructValue :: struct { names: []string, ranges: []common.Range, diff --git a/tests/hover_test.odin b/tests/hover_test.odin index 2a0059d..0c9aa76 100644 --- a/tests/hover_test.odin +++ b/tests/hover_test.odin @@ -511,7 +511,7 @@ ast_hover_foreign_package_name_collision :: proc(t: ^testing.T) { packages = packages[:], } - test.expect_hover(t, &source, "node.bar: ^my_package.bar :: struct {}") + test.expect_hover(t, &source, "node.bar: ^my_package.bar") } @(test) ast_hover_struct :: proc(t: ^testing.T) { @@ -680,7 +680,7 @@ ast_hover_struct_field_complex_definition :: proc(t: ^testing.T) { `, } - test.expect_hover(t, &source, "Foo.bar: ^test.Bar // inline docs") + test.expect_hover(t, &source, "Foo.bar: ^test.Bar // inline docs\n Docs") } @(test) @@ -971,7 +971,7 @@ ast_hover_distinguish_names_correctly_variable_assignment :: proc(t: ^testing.T) `, } - test.expect_hover(t, &source, "Foo.bar: ^test.Bar :: struct {\n\tbar: int,\n}") + test.expect_hover(t, &source, "Foo.bar: ^test.Bar") } @(test) @@ -1007,7 +1007,7 @@ ast_hover_struct_field_use :: proc(t: ^testing.T) { `, } - test.expect_hover(t, &source, "Bar.foo: test.Foo :: struct {\n\tvalue: int,\n}") + test.expect_hover(t, &source, "Bar.foo: test.Foo") } @(test) @@ -2327,6 +2327,60 @@ ast_hover_overload_proc_strings_from_different_packages :: proc(t: ^testing.T) { "my_package.foo: proc(a: string, b: int)", ) } + +@(test) +ast_hover_struct_field_should_show_docs_and_comments :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + + Foo :: struct { + // a docs + a: int, // a comment + } + + main :: proc() { + foo := Foo{} + foo.a{*} + } + `, + } + test.expect_hover( t, &source, "Foo.a: int // a comment\n a docs") +} + +@(test) +ast_hover_struct_field_should_show_docs_and_comments_on_field :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + + Foo :: struct { + // a docs + a{*}: int, // a comment + } + `, + } + test.expect_hover( t, &source, "Foo.a: int // a comment\n a docs") +} + +@(test) +ast_hover_struct_field_should_show_docs_and_comments_on_struct_types :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + + Foo :: struct { + // bar docs + bar: Bar, // bar comment + } + + Bar :: struct {} + + main :: proc() { + foo := Foo{} + foo.bar{*} + } + `, + } + test.expect_hover( t, &source, "Foo.bar: test.Bar // bar comment\n bar docs") +} /* Waiting for odin fix -- cgit v1.2.3