Skip to content

Commit

Permalink
Ignore nesting when resolving top level constants (#1048)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Sep 29, 2023
1 parent 9facb66 commit 5e4b3f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def fuzzy_search(query)
# 3. Baz
sig { params(name: String, nesting: T::Array[String]).returns(T.nilable(T::Array[Entry])) }
def resolve(name, nesting)
if name.start_with?("::")
name = name.delete_prefix("::")
results = @entries[name] || @entries[follow_aliased_namespace(name)]
return results.map { |e| e.is_a?(Entry::UnresolvedAlias) ? resolve_alias(e) : e } if results
end

nesting.length.downto(0).each do |i|
namespace = T.must(nesting[0...i]).join("::")
full_name = namespace.empty? ? name : "#{namespace}::#{name}"
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 @@ -158,5 +158,25 @@ class Foo::Baz
results = @index.prefix_search("Ba", ["Foo"]).map { |entries| entries.map(&:name) }
assert_equal([["Foo::Bar", "Foo::Bar"], ["Foo::Baz"]], results)
end

def test_resolve_normalizes_top_level_names
@index.index_single(IndexablePath.new("/fake", "/fake/path/foo.rb"), <<~RUBY)
class Bar; end
module Foo
class Bar; end
end
RUBY

entries = @index.resolve("::Foo::Bar", [])
refute_nil(entries)

assert_equal("Foo::Bar", entries.first.name)

entries = @index.resolve("::Bar", ["Foo"])
refute_nil(entries)

assert_equal("Bar", entries.first.name)
end
end
end

0 comments on commit 5e4b3f7

Please sign in to comment.