Skip to content

Commit

Permalink
Filter instance variable owners before deduping
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Jun 10, 2024
1 parent f44da30 commit 4f08dcd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4f08dcd

Please sign in to comment.