Skip to content

Commit

Permalink
Add entries_for
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Aug 28, 2024
1 parent 421e879 commit 67727d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,18 @@ def existing_or_new_singleton_class(name)
singleton
end

sig do
type_parameters(:T).params(
path: String,
type: T.nilable(T::Class[T.all(T.type_parameter(:T), Entry)]),
).returns(T.nilable(T::Array[T.type_parameter(:T)]))
end
def entries_for(path, type = nil)
type = Entry if type.nil?

@files_to_entries[path]&.grep(type)&.grep_v(RubyIndexer::Entry::SingletonClass)
end

private

# Runs the registered included hooks
Expand Down
17 changes: 17 additions & 0 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1822,5 +1822,22 @@ class Child < Namespace::Parent
@index.linearized_ancestors_of("Foo::Child::<Class:Child>"),
)
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")
assert_equal(["Foo", "Bar", "my_def", "my_singleton_def"], entries.map(&:name))

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

0 comments on commit 67727d2

Please sign in to comment.