diff --git a/lib/ruby_indexer/lib/ruby_indexer/visitor.rb b/lib/ruby_indexer/lib/ruby_indexer/visitor.rb index 142965ffe..4beeb8a00 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/visitor.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/visitor.rb @@ -10,6 +10,7 @@ def initialize(index, parse_result, file_path) @index = index @file_path = file_path @stack = T.let([], T::Array[String]) + @singleton = T.let(false, T::Boolean) @comments_by_line = T.let( parse_result.comments.to_h do |c| [c.location.start_line, c] @@ -25,6 +26,13 @@ def visit_class_node(node) add_class_entry(node) end + sig { override.params(node: Prism::SingletonClassNode).void } + def visit_singleton_class_node(node) + @singleton = true + super + @singleton = false + end + sig { override.params(node: Prism::ModuleNode).void } def visit_module_node(node) add_module_entry(node) @@ -126,7 +134,11 @@ def visit_def_node(node) comments = collect_comments(node) case node.receiver when nil - @index << Entry::InstanceMethod.new(method_name, @file_path, node.location, comments, node.parameters) + @index << if @singleton + Entry::SingletonMethod.new(method_name, @file_path, node.location, comments, node.parameters) + else + Entry::InstanceMethod.new(method_name, @file_path, node.location, comments, node.parameters) + end when Prism::SelfNode @index << Entry::SingletonMethod.new(method_name, @file_path, node.location, comments, node.parameters) end diff --git a/lib/ruby_indexer/test/method_test.rb b/lib/ruby_indexer/test/method_test.rb index 87d77df11..500c617e1 100644 --- a/lib/ruby_indexer/test/method_test.rb +++ b/lib/ruby_indexer/test/method_test.rb @@ -16,7 +16,39 @@ def bar assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5") end - def test_singleton_method_using_self_receiver + def test_singleton_method_using_class_self + index(<<~RUBY) + class Foo + class << self + def bar + end + end + + def baz + end + end + RUBY + + assert_entry("bar", Entry::SingletonMethod, "/fake/path/foo.rb:2-4:3-7") + assert_entry("baz", Entry::InstanceMethod, "/fake/path/foo.rb:6-2:7-5") + end + + def test_singleton_method_using_class_self_with_nesting + index(<<~RUBY) + class Foo + class << self + class Nested + def bar + end + end + end + end + RUBY + + assert_entry("bar", Entry::SingletonMethod, "/fake/path/foo.rb:3-6:4-9") + end + + def test_singleton_method_using_class index(<<~RUBY) class Foo def self.bar