aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrad Lewis <22850972+BradLewis@users.noreply.github.com>2025-07-06 20:47:27 -0400
committerBrad Lewis <22850972+BradLewis@users.noreply.github.com>2025-07-06 20:48:31 -0400
commit4a78b4ad1dca9a1218c4df2d97bfb5d9465bc063 (patch)
tree3eea76835d1180692492190988999c753434ac43
parentd4b195cf6dba5894f9597c935106d190860d9de8 (diff)
Correctly filter used unions when ptr
-rw-r--r--src/server/completion.odin18
-rw-r--r--tests/completions_test.odin27
2 files changed, 41 insertions, 4 deletions
diff --git a/src/server/completion.odin b/src/server/completion.odin
index 13a589d..a84631f 100644
--- a/src/server/completion.odin
+++ b/src/server/completion.odin
@@ -1581,6 +1581,18 @@ search_for_packages :: proc(fullpath: string) -> []string {
return packages[:]
}
+get_used_switch_name :: proc(node: ^ast.Expr) -> (string, bool) {
+ #partial switch n in node.derived {
+ case ^ast.Ident:
+ return n.name, true
+ case ^ast.Selector_Expr:
+ return n.field.name, true
+ case ^ast.Pointer_Type:
+ return get_used_switch_name(n.elem)
+ }
+ return "", false
+}
+
get_type_switch_completion :: proc(
ast_context: ^AstContext,
position_context: ^DocumentPositionContext,
@@ -1595,10 +1607,8 @@ get_type_switch_completion :: proc(
for stmt in block.stmts {
if case_clause, ok := stmt.derived.(^ast.Case_Clause); ok {
for name in case_clause.list {
- if ident, ok := name.derived.(^ast.Ident); ok {
- used_unions[ident.name] = true
- } else if selector, ok := name.derived.(^ast.Selector_Expr); ok {
- used_unions[selector.field.name] = true
+ if n, ok := get_used_switch_name(name); ok {
+ used_unions[n] = true
}
}
}
diff --git a/tests/completions_test.odin b/tests/completions_test.odin
index f8bb410..41f0566 100644
--- a/tests/completions_test.odin
+++ b/tests/completions_test.odin
@@ -3780,3 +3780,30 @@ ast_completion_union_switch_remove_used_cases :: proc(t: ^testing.T) {
test.expect_completion_details(t, &source, "", {"Foo2", "Foo3"}, {"Foo1"})
}
+
+@(test)
+ast_completion_union_switch_remove_used_cases_ptr :: proc(t: ^testing.T) {
+ source := test.Source {
+ main = `package test
+ Foo1 :: struct{}
+ Foo2 :: struct{}
+ Foo3 :: struct{}
+ Foo :: union {
+ ^Foo1,
+ ^Foo2,
+ ^Foo3,
+ }
+
+ main :: proc() {
+ foo: Foo
+
+ switch v in Foo {
+ case ^Foo1:
+ case F{*}
+ }
+ }
+ `,
+ }
+
+ test.expect_completion_details(t, &source, "", {"^Foo2", "^Foo3"}, {"^Foo1"})
+}