Skip to content

Commit

Permalink
Use RBS to index Ruby core methods
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Jun 11, 2024
1 parent 5286df2 commit 8655952
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def handle_class_declaration(declaration, pathname)
end

@index << class_entry
declaration.members.each do |member|
next unless member.is_a?(RBS::AST::Members::MethodDefinition)

handle_member(member, class_entry)
end
end

sig { params(declaration: RBS::AST::Declarations::Module, pathname: Pathname).void }
Expand All @@ -76,8 +81,15 @@ def handle_module_declaration(declaration, pathname)
comments = Array(declaration.comment&.string)
class_entry = Entry::Module.new(nesting, file_path, location, comments)
@index << class_entry
declaration.members.each do |member|
next unless member.is_a?(RBS::AST::Members::MethodDefinition)

handle_member(member, class_entry)
end
end

Array.new(1)

sig { params(rbs_location: RBS::Location).returns(RubyIndexer::Location) }
def to_ruby_indexer_location(rbs_location)
RubyIndexer::Location.new(
Expand All @@ -87,5 +99,27 @@ def to_ruby_indexer_location(rbs_location)
rbs_location.end_column,
)
end

sig { params(member: RBS::AST::Members::MethodDefinition, class_entry: T.any(Entry::Class, Entry::Module)).void }
def handle_member(member, class_entry)
name = member.name.name
file_path = member.location.buffer.name
location = to_ruby_indexer_location(member.location)
comments = Array(member.comment&.string)
parameters_node = nil
visibility = Entry::Visibility::PUBLIC
owner = class_entry

klass = member.instance? ? Entry::InstanceMethod : Entry::SingletonMethod
entry = klass.new(
name,
file_path,
location,
comments,
parameters_node,
visibility,
owner)
@index << entry if entry
end
end
end
14 changes: 14 additions & 0 deletions lib/ruby_indexer/test/rbs_indexer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,19 @@ def test_index_core_modules
assert_equal(0, entry.location.start_column)
assert_operator(entry.location.end_column, :>, 0)
end

def test_index_methods
entries = @index["initialize"]
refute_nil(entries)
entry = entries.find { |entry| entry.owner.name == "Array" }
assert_match(%r{/gems/rbs-.*/core/array.rbs}, entry.file_path)
assert_equal("array.rbs", entry.file_name)

# Using fixed positions would be fragile, so let's just check some basics.
assert_operator(entry.location.start_line, :>, 0)
assert_operator(entry.location.end_line, :>, entry.location.start_line)
assert_equal(2, entry.location.start_column)
assert_operator(entry.location.end_column, :>, 0)
end
end
end

0 comments on commit 8655952

Please sign in to comment.