Skip to content

Commit

Permalink
Build complete ancestors list with RBS classes and modules
Browse files Browse the repository at this point in the history
1. Start indexing modules through RBS as well
2. Make `Object` the default parent for indexed classes
  • Loading branch information
st0012 committed Jun 11, 2024
1 parent b212f74 commit 0da2dd0
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 63 deletions.
2 changes: 2 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def on_class_node_enter(node)
parent_class = case superclass
when Prism::ConstantReadNode, Prism::ConstantPathNode
superclass.slice
else
"::Object"
end

nesting = name.start_with?("::") ? [name.delete_prefix("::")] : @stack + [name.delete_prefix("::")]
Expand Down
38 changes: 37 additions & 1 deletion lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(index)
end

sig { void }
def index_core_classes
def index_ruby_core
loader = RBS::EnvironmentLoader.new
RBS::Environment.from_loader(loader).resolve_type_names

Expand All @@ -34,6 +34,8 @@ def process_declaration(declaration, pathname)
case declaration
when RBS::AST::Declarations::Class
handle_class_declaration(declaration, pathname)
when RBS::AST::Declarations::Module
handle_module_declaration(declaration, pathname)
else # rubocop:disable Style/EmptyElse
# Other kinds not yet handled
end
Expand All @@ -47,9 +49,21 @@ def handle_class_declaration(declaration, pathname)
comments = Array(declaration.comment&.string)
parent_class = declaration.super_class&.name&.name&.to_s
class_entry = Entry::Class.new(nesting, file_path, location, comments, parent_class)
add_declaration_mixins_to_entry(declaration, class_entry)
@index << class_entry
end

sig { params(declaration: RBS::AST::Declarations::Module, pathname: Pathname).void }
def handle_module_declaration(declaration, pathname)
nesting = [declaration.name.name.to_s]
file_path = pathname.to_s
location = to_ruby_indexer_location(declaration.location)
comments = Array(declaration.comment&.string)
module_entry = Entry::Module.new(nesting, file_path, location, comments)
add_declaration_mixins_to_entry(declaration, module_entry)
@index << module_entry
end

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

sig do
params(
declaration: T.any(RBS::AST::Declarations::Class, RBS::AST::Declarations::Module),
entry: Entry::Namespace,
).void
end
def add_declaration_mixins_to_entry(declaration, entry)
declaration.each_mixin do |mixin|
name = mixin.name.name.to_s
mixin_operation =
case mixin
when RBS::AST::Members::Include
Entry::Include.new(name)
when RBS::AST::Members::Extend
Entry::Extend.new(name)
when RBS::AST::Members::Prepend
Entry::Prepend.new(name)
end
entry.mixin_operations << mixin_operation if mixin_operation
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby_indexer/test/classes_and_modules_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class FinalThing < Something::Baz
assert_equal("Bar", foo.parent_class)

baz = T.must(@index["Baz"].first)
assert_nil(baz.parent_class)
assert_equal("::Object", baz.parent_class)

qux = T.must(@index["Something::Qux"].first)
assert_equal("::Baz", qux.parent_class)
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_indexer/test/constant_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_variable_path_constants_are_ignored
self.class::FOO = 1
RUBY

assert_no_entries
assert_no_indexed_entries
end

def test_private_constant_indexing
Expand Down
Loading

0 comments on commit 0da2dd0

Please sign in to comment.