From 67727d218fa1bd483e6642494f950760fd3af917 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Wed, 28 Aug 2024 10:11:43 -0400 Subject: [PATCH] Add entries_for --- lib/ruby_indexer/lib/ruby_indexer/index.rb | 12 ++++++++++++ lib/ruby_indexer/test/index_test.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/ruby_indexer/lib/ruby_indexer/index.rb b/lib/ruby_indexer/lib/ruby_indexer/index.rb index 0c126588b2..d14f2812b9 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/index.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/index.rb @@ -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 diff --git a/lib/ruby_indexer/test/index_test.rb b/lib/ruby_indexer/test/index_test.rb index 4a959db080..41b383fc39 100644 --- a/lib/ruby_indexer/test/index_test.rb +++ b/lib/ruby_indexer/test/index_test.rb @@ -1822,5 +1822,22 @@ class Child < Namespace::Parent @index.linearized_ancestors_of("Foo::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