-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Index core classes using RBS (#2132)
Use RBS for indexing Core classes
- Loading branch information
Showing
9 changed files
with
6,753 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyIndexer | ||
class RBSIndexer | ||
extend T::Sig | ||
|
||
sig { params(index: Index).void } | ||
def initialize(index) | ||
@index = index | ||
end | ||
|
||
sig { void } | ||
def index_core_classes | ||
loader = RBS::EnvironmentLoader.new | ||
RBS::Environment.from_loader(loader).resolve_type_names | ||
|
||
loader.each_signature do |source, pathname, _buffer, declarations, _directives| | ||
process_signature(source, pathname, declarations) | ||
end | ||
end | ||
|
||
private | ||
|
||
sig { params(source: T.untyped, pathname: Pathname, declarations: T::Array[RBS::AST::Declarations::Base]).void } | ||
def process_signature(source, pathname, declarations) | ||
declarations.each do |declaration| | ||
process_declaration(declaration, pathname) | ||
end | ||
end | ||
|
||
sig { params(declaration: RBS::AST::Declarations::Base, pathname: Pathname).void } | ||
def process_declaration(declaration, pathname) | ||
case declaration | ||
when RBS::AST::Declarations::Class | ||
handle_class_declaration(declaration, pathname) | ||
else # rubocop:disable Style/EmptyElse | ||
# Other kinds not yet handled | ||
end | ||
end | ||
|
||
sig { params(declaration: RBS::AST::Declarations::Class, pathname: Pathname).void } | ||
def handle_class_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) | ||
parent_class = declaration.super_class&.name&.name&.to_s | ||
class_entry = Entry::Class.new(nesting, file_path, location, comments, parent_class) | ||
@index << class_entry | ||
end | ||
|
||
sig { params(rbs_location: RBS::Location).returns(RubyIndexer::Location) } | ||
def to_ruby_indexer_location(rbs_location) | ||
RubyIndexer::Location.new( | ||
rbs_location.start_line, | ||
rbs_location.end_line, | ||
rbs_location.start_column, | ||
rbs_location.end_column, | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require_relative "test_case" | ||
|
||
module RubyIndexer | ||
class RBSIndexerTest < TestCase | ||
def setup | ||
@index = RubyIndexer::Index.new | ||
RBSIndexer.new(@index).index_core_classes | ||
end | ||
|
||
def test_index_core_classes | ||
entries = @index["Array"] | ||
refute_nil(entries) | ||
assert_equal(1, entries.length) | ||
entry = entries.first | ||
assert_match(%r{/gems/rbs-.*/core/array.rbs}, entry.file_path) | ||
assert_equal("array.rbs", entry.file_name) | ||
assert_equal("Object", entry.parent_class) | ||
|
||
# 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(0, entry.location.start_column) | ||
assert_operator(entry.location.end_column, :>, 0) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.