diff options
| author | Bradley Lewis <22850972+BradLewis@users.noreply.github.com> | 2025-10-30 05:12:27 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-30 05:12:27 -0400 |
| commit | a6357c1cd8085774765d2618e5a34aaf163d560e (patch) | |
| tree | d7a8c0e571e527f5d2b8c752d9feea4cc1c776ae | |
| parent | 98b1ebda9888e4527db9432c37274244fdd5e6ae (diff) | |
| parent | 8e434d13a3cad539fdeb0352bf9f386928dfb00f (diff) | |
Merge pull request #1136 from BradLewis/feat/hover-local-consts-like-globals
Update local consts hover info to behave like global consts
| -rw-r--r-- | src/server/analysis.odin | 2 | ||||
| -rw-r--r-- | src/server/locals.odin | 49 | ||||
| -rw-r--r-- | tests/hover_test.odin | 28 |
3 files changed, 74 insertions, 5 deletions
diff --git a/src/server/analysis.odin b/src/server/analysis.odin index fdc1956..56d7843 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -1852,6 +1852,8 @@ resolve_local_identifier :: proc(ast_context: ^AstContext, node: ast.Ident, loca } return_symbol.flags |= {.Local} + return_symbol.value_expr = local.value_expr + return_symbol.type_expr = local.type_expr return return_symbol, ok } diff --git a/src/server/locals.odin b/src/server/locals.odin index 3de2923..337e557 100644 --- a/src/server/locals.odin +++ b/src/server/locals.odin @@ -1,6 +1,5 @@ package server -import "core:fmt" import "core:log" import "core:odin/ast" @@ -12,6 +11,8 @@ LocalFlag :: enum { DocumentLocal :: struct { lhs: ^ast.Expr, rhs: ^ast.Expr, + type_expr: ^ast.Expr, + value_expr: ^ast.Expr, offset: int, resolved_global: bool, //Some locals have already been resolved and are now in global space local_global: bool, //Some locals act like globals, i.e. functions defined inside functions. @@ -33,6 +34,8 @@ store_local :: proc( flags: bit_set[LocalFlag], pkg: string, parameter: bool, + type_expr: ^ast.Expr = nil, + value_expr: ^ast.Expr = nil, ) { local_group := get_local_group(ast_context) local_stack := &local_group[name] @@ -47,6 +50,8 @@ store_local :: proc( DocumentLocal { lhs = lhs, rhs = rhs, + type_expr = type_expr, + value_expr = value_expr, offset = offset, resolved_global = resolved_global, local_global = local_global, @@ -309,7 +314,13 @@ get_locals_value_decl :: proc(file: ast.File, value_decl: ast.Value_Decl, ast_co flags: bit_set[LocalFlag] if value_decl.is_mutable { flags |= {.Mutable} - + } + value_expr: ^ast.Expr + if len(value_decl.values) > i { + if is_variable_declaration(value_decl.values[i]) { + flags |= {.Variable} + value_expr = value_decl.values[i] + } } store_local( ast_context, @@ -322,6 +333,8 @@ get_locals_value_decl :: proc(file: ast.File, value_decl: ast.Value_Decl, ast_co flags, "", false, + value_decl.type, + value_expr, ) } return @@ -350,8 +363,12 @@ get_locals_value_decl :: proc(file: ast.File, value_decl: ast.Value_Decl, ast_co flags: bit_set[LocalFlag] expr := results[result_i] + value_expr: ^ast.Expr if is_variable_declaration(expr) { flags |= {.Variable} + if len(value_decl.values) > i { + value_expr = value_decl.values[i] + } } if value_decl.is_mutable { flags |= {.Mutable} @@ -368,6 +385,8 @@ get_locals_value_decl :: proc(file: ast.File, value_decl: ast.Value_Decl, ast_co flags, get_package_from_node(results[result_i]^), false, + value_decl.type, + value_expr, ) } } @@ -502,7 +521,18 @@ get_locals_using :: proc(expr: ^ast.Expr, ast_context: ^AstContext) { selector.expr = expr selector.field = new_type(ast.Ident, v.types[i].pos, v.types[i].end, ast_context.allocator) selector.field.name = name - store_local(ast_context, expr, selector, 0, name, false, ast_context.non_mutable_only, {.Mutable}, "", false) + store_local( + ast_context, + expr, + selector, + 0, + name, + false, + ast_context.non_mutable_only, + {.Mutable}, + "", + false, + ) } case SymbolBitFieldValue: for name, i in v.names { @@ -510,7 +540,18 @@ get_locals_using :: proc(expr: ^ast.Expr, ast_context: ^AstContext) { selector.expr = expr selector.field = new_type(ast.Ident, v.types[i].pos, v.types[i].end, ast_context.allocator) selector.field.name = name - store_local(ast_context, expr, selector, 0, name, false, ast_context.non_mutable_only, {.Mutable}, "", false) + store_local( + ast_context, + expr, + selector, + 0, + name, + false, + ast_context.non_mutable_only, + {.Mutable}, + "", + false, + ) } } } diff --git a/tests/hover_test.odin b/tests/hover_test.odin index 4a99d19..1ee56d4 100644 --- a/tests/hover_test.odin +++ b/tests/hover_test.odin @@ -2930,7 +2930,7 @@ ast_hover_builtin_max_mix_const :: proc(t: ^testing.T) { } `, } - test.expect_hover(t, &source, "test.m :: 4.6") + test.expect_hover(t, &source, "test.m :: max(1, 2.0, 3, 4.6)") } @(test) @@ -5368,6 +5368,32 @@ ast_hover_parapoly_other_package :: proc(t: ^testing.T) { } test.expect_hover(t, &source, "my_package.bar :: proc(_: $T)\n Docs!\n\n// Comment!") } + +@(test) +ast_hover_local_const_binary_expr :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + main :: proc() { + f{*}oo :: 1 + 2 + } + + `, + } + test.expect_hover(t, &source, "test.foo :: 1 + 2") +} + +@(test) +ast_hover_local_const_binary_expr_with_type :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + main :: proc() { + f{*}oo : i32 : 1 + 2 + } + + `, + } + test.expect_hover(t, &source, "test.foo : i32 : 1 + 2") +} /* Waiting for odin fix |