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