From 4f08dcdf1d67f5dd9895a91a8cc101941aa5f131 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Sun, 9 Jun 2024 14:01:19 -0400 Subject: [PATCH] Filter instance variable owners before deduping --- lib/ruby_indexer/lib/ruby_indexer/index.rb | 4 ++-- lib/ruby_indexer/test/index_test.rb | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/index.rb b/lib/ruby_indexer/lib/ruby_indexer/index.rb index 2e3a7d7b1..bbe401cf8 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/index.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/index.rb @@ -413,8 +413,8 @@ def instance_variable_completion_candidates(name, owner_name) entries = T.cast(prefix_search(name).flatten, T::Array[Entry::InstanceVariable]) ancestors = linearized_ancestors_of(owner_name) - variables = entries.uniq(&:name) - variables.select! { |e| ancestors.any?(e.owner&.name) } + variables = entries.select { |e| ancestors.any?(e.owner&.name) } + variables.uniq!(&:name) variables end diff --git a/lib/ruby_indexer/test/index_test.rb b/lib/ruby_indexer/test/index_test.rb index 05d6e746a..31f9ea1b0 100644 --- a/lib/ruby_indexer/test/index_test.rb +++ b/lib/ruby_indexer/test/index_test.rb @@ -1105,5 +1105,25 @@ class Index foo_entry = T.must(@index.resolve("CONST", ["Namespace", "Index"])&.first) assert_equal(1, foo_entry.location.start_line) end + + def test_instance_variables_completions_from_different_owners_with_conflicting_names + index(<<~RUBY) + class Foo + def initialize + @bar = 1 + end + end + + class Bar + def initialize + @bar = 2 + end + end + RUBY + + entry = T.must(@index.instance_variable_completion_candidates("@", "Bar")&.first) + assert_equal("@bar", entry.name) + assert_equal("Bar", T.must(entry.owner).name) + end end end