Skip to content

Commit

Permalink
Add entries_for method to indexer (#2500)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Aug 28, 2024
1 parent f17117a commit a4612cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,17 @@ def existing_or_new_singleton_class(name)
singleton
end

sig do
type_parameters(:T).params(
path: String,
type: T::Class[T.all(T.type_parameter(:T), Entry)],
).returns(T.nilable(T::Array[T.type_parameter(:T)]))
end
def entries_for(path, type)
entries = @files_to_entries[path]
entries&.grep(type)
end

private

# Runs the registered included hooks
Expand Down
21 changes: 21 additions & 0 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1842,5 +1842,26 @@ class Foo
candidates = @index.method_completion_candidates("=", "Foo")
assert_equal(["==", "==="], candidates.map(&:name))
end

def test_entries_for
index(<<~RUBY)
class Foo; end
module Bar
def my_def; end
def self.my_singleton_def; end
end
RUBY

entries = @index.entries_for("/fake/path/foo.rb", Entry)
assert_equal(["Foo", "Bar", "my_def", "Bar::<Class:Bar>", "my_singleton_def"], entries.map(&:name))

entries = @index.entries_for("/fake/path/foo.rb", RubyIndexer::Entry::Namespace)
assert_equal(["Foo", "Bar", "Bar::<Class:Bar>"], entries.map(&:name))
end

def test_entries_for_returns_nil_if_no_matches
assert_nil(@index.entries_for("non_existing_file.rb", Entry::Namespace))
end
end
end

0 comments on commit a4612cf

Please sign in to comment.