Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Store namespace with method #1121

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,26 @@ class Method < Entry
sig { returns(T::Array[Parameter]) }
attr_reader :parameters

sig { returns(T.any(NilClass, Entry::Class, Entry::Module)) }
attr_reader :namespace

# rubocop:disable Metrics/ParameterLists
sig do
params(
name: String,
file_path: String,
location: Prism::Location,
comments: T::Array[String],
parameters_node: T.nilable(Prism::ParametersNode),
namespace: T.any(NilClass, Entry::Class, Entry::Module),
).void
end
def initialize(name, file_path, location, comments, parameters_node)
def initialize(name, file_path, location, comments, parameters_node, namespace)
super(name, file_path, location, comments)
@parameters = T.let(list_params(parameters_node), T::Array[Parameter])
@namespace = T.let(namespace, T.any(NilClass, Entry::Class, Entry::Module))
end
# rubocop:enable Metrics/ParameterLists

private

Expand Down
21 changes: 16 additions & 5 deletions lib/ruby_indexer/lib/ruby_indexer/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ def initialize(index, parse_result, file_path)
end,
T::Hash[Integer, Prism::Comment],
)
@class_or_module_entry = T.let(nil, T.any(NilClass, Entry::Class, Entry::Module))

super()
end

sig { override.params(node: Prism::ClassNode).void }
def visit_class_node(node)
add_class_entry(node)
add_class_or_module_entry(node)
end

sig { override.params(node: Prism::SingletonClassNode).void }
Expand Down Expand Up @@ -144,7 +145,9 @@ def visit_def_node(node)
else
return
end
@index << entry_class.new(method_name, @file_path, node.location, comments, node.parameters)
name = @class_or_module_entry

@index << entry_class.new(method_name, @file_path, node.location, comments, node.parameters, name)
end

private
Expand Down Expand Up @@ -223,14 +226,15 @@ def add_module_entry(node)

comments = collect_comments(node)

@index << Entry::Module.new(fully_qualify_name(name), @file_path, node.location, comments)
@class_or_module_entry = Entry::Module.new(fully_qualify_name(name), @file_path, node.location, comments)
@index << @class_or_module_entry
@stack << name
visit_child_nodes(node)
@stack.pop
end

sig { params(node: Prism::ClassNode).void }
def add_class_entry(node)
def add_class_or_module_entry(node)
name = node.constant_path.location.slice
return visit_child_nodes(node) unless /^[A-Z:]/.match?(name)

Expand All @@ -242,7 +246,14 @@ def add_class_entry(node)
superclass.slice
end

@index << Entry::Class.new(fully_qualify_name(name), @file_path, node.location, comments, parent_class)
@class_or_module_entry = Entry::Class.new(
fully_qualify_name(name),
@file_path,
node.location,
comments,
parent_class,
)
@index << @class_or_module_entry
@stack << name
visit(node.body)
@stack.pop
Expand Down
35 changes: 28 additions & 7 deletions lib/ruby_indexer/test/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,27 @@ def bar
end
RUBY

assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5", "Foo")
end

def test_top_level_method
index(<<~RUBY)
def bar
end
RUBY

assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:0-0:1-3", nil)
end

def test_module_method_with_no_parameters
index(<<~RUBY)
module Foo
def bar
end
end
RUBY

assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5", "Foo")
end

def test_singleton_method_using_class_self
Expand All @@ -29,8 +49,8 @@ def baz
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")
assert_entry("bar", Entry::SingletonMethod, "/fake/path/foo.rb:2-4:3-7", "Foo")
assert_entry("baz", Entry::InstanceMethod, "/fake/path/foo.rb:6-2:7-5", "Foo")
end

def test_singleton_method_using_class_self_with_nesting
Expand All @@ -45,7 +65,8 @@ def bar
end
RUBY

assert_entry("bar", Entry::SingletonMethod, "/fake/path/foo.rb:3-6:4-9")
# TODO: I think this isn't correct
assert_entry("bar", Entry::SingletonMethod, "/fake/path/foo.rb:3-6:4-9", "Foo::Nested")
end

def test_singleton_method_using_class
Expand All @@ -56,7 +77,7 @@ def self.bar
end
RUBY

assert_entry("bar", Entry::SingletonMethod, "/fake/path/foo.rb:1-2:2-5")
assert_entry("bar", Entry::SingletonMethod, "/fake/path/foo.rb:1-2:2-5", "Foo")
end

def test_singleton_method_using_other_receiver_is_not_indexed
Expand All @@ -78,7 +99,7 @@ def bar(a)
end
RUBY

assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5", "Foo")
entry = T.must(@index["bar"].first)
assert_equal(1, entry.parameters.length)
parameter = entry.parameters.first
Expand All @@ -94,7 +115,7 @@ def bar((a, (b, )))
end
RUBY

assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5")
assert_entry("bar", Entry::InstanceMethod, "/fake/path/foo.rb:1-2:2-5", "Foo")
entry = T.must(@index["bar"].first)
assert_equal(1, entry.parameters.length)
parameter = entry.parameters.first
Expand Down
3 changes: 2 additions & 1 deletion lib/ruby_indexer/test/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def index(source)
@index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), source)
end

def assert_entry(expected_name, type, expected_location)
def assert_entry(expected_name, type, expected_location, expected_namespace = nil)
entries = @index[expected_name]
refute_empty(entries, "Expected #{expected_name} to be indexed")

Expand All @@ -28,6 +28,7 @@ def assert_entry(expected_name, type, expected_location)
":#{location.end_line - 1}-#{location.end_column}"

assert_equal(expected_location, location_string)
assert_equal(expected_namespace, entry.namespace.name) if expected_namespace
end

def refute_entry(expected_name)
Expand Down