From 558f690a8d962b658986bc28fa1e5603098674c1 Mon Sep 17 00:00:00 2001 From: Brad Lewis <22850972+BradLewis@users.noreply.github.com> Date: Tue, 19 Aug 2025 12:19:01 -0400 Subject: Correctly resolve string indexing --- src/server/analysis.odin | 23 +++++++++++++++++++++++ src/server/documentation.odin | 11 ++++++++--- tests/hover_test.odin | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/server/analysis.odin b/src/server/analysis.odin index c1d0016..7c51b69 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -1238,6 +1238,24 @@ resolve_index_expr :: proc(ast_context: ^AstContext, v: ^ast.Index_Expr) -> (Sym ok = internal_resolve_type_expression(ast_context, v2.value, &symbol) case SymbolMultiPointerValue: ok = internal_resolve_type_expression(ast_context, v2.expr, &symbol) + case SymbolBasicValue: + if v2.ident.name == "string" { + v2.ident.name = "u8" + indexed.name = "u8" + return indexed, true + } + return {}, false + case SymbolUntypedValue: + if v2.type == .String { + value := SymbolBasicValue{ + ident = ast.new(ast.Ident, v2.tok.pos, v2.tok.pos), + } + value.ident.name = "u8" + indexed.name = "u8" + indexed.value = value + return indexed, true + } + return {}, false } @@ -1887,6 +1905,11 @@ resolve_slice_expression :: proc(ast_context: ^AstContext, slice_expr: ^ast.Slic return symbol, true } return {}, false + case SymbolBasicValue: + if v.ident.name == "string" { + return symbol, true + } + return {}, false case: return {}, false } diff --git a/src/server/documentation.odin b/src/server/documentation.odin index dc0784d..1cc804d 100644 --- a/src/server/documentation.odin +++ b/src/server/documentation.odin @@ -822,10 +822,15 @@ write_symbol_name :: proc(sb: ^strings.Builder, symbol: Symbol) { } write_symbol_type_information :: proc(sb: ^strings.Builder, ast_context: ^AstContext, symbol: Symbol) -> bool { - show_type_info := - (symbol.type == .Variable || symbol.type == .Field) && !(.Anonymous in symbol.flags) && symbol.type_name != "" + if symbol.type_name == "" { + return false + } + + if symbol.type != .Variable && symbol.type != .Field { + return false + } - if !show_type_info { + if .Anonymous in symbol.flags { return false } diff --git a/tests/hover_test.odin b/tests/hover_test.odin index 16bbf69..8d7f7c9 100644 --- a/tests/hover_test.odin +++ b/tests/hover_test.odin @@ -4261,6 +4261,45 @@ ast_hover_proc_within_for_loop :: proc(t: ^testing.T) { } test.expect_hover(t, &source, "test.foo: proc()") } + +@(test) +ast_hover_string_slice_range :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + main :: proc() { + foo: string + ba{*}r := foo[1:2] + } + `, + } + test.expect_hover(t, &source, "test.bar: string") +} + +@(test) +ast_hover_string_index :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + main :: proc() { + foo: string + ba{*}r := foo[1] + } + `, + } + test.expect_hover(t, &source, "test.bar: u8") +} + +@(test) +ast_hover_untyped_string_index :: proc(t: ^testing.T) { + source := test.Source { + main = `package test + main :: proc() { + foo := "hellope" + ba{*}r := foo[1] + } + `, + } + test.expect_hover(t, &source, "test.bar: u8") +} /* Waiting for odin fix -- cgit v1.2.3