Skip to content

Commit

Permalink
Add missing declarations about stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
r7kamura committed Sep 14, 2022
1 parent 206b422 commit 5d33a36
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 17 deletions.
Binary file modified data/definitions_ruby_3_1
Binary file not shown.
52 changes: 42 additions & 10 deletions lib/rucoa/rbs/ruby_definitions_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

module Rucoa
module Rbs
# Load definitions from RBS's definitions about Ruby core and its standard libraries.
# Load definitions for Ruby core and standard libraries.
class RubyDefinitionsLoader
class << self
# @return [Array<Rucoa::Definitions::Base>]
def call
new.call
end
Expand All @@ -21,12 +22,15 @@ def call
ConstantDefinitionMapper.call(declaration: declaration)
]
when ::RBS::AST::Declarations::Class, ::RBS::AST::Declarations::Module
declaration.members.grep(::RBS::AST::Members::MethodDefinition).map do |method_definition|
MethodDefinitionMapper.call(
declaration: declaration,
method_definition: method_definition
)
end
[
ConstantDefinitionMapper.call(declaration: declaration)
] +
declaration.members.grep(::RBS::AST::Members::MethodDefinition).map do |method_definition|
MethodDefinitionMapper.call(
declaration: declaration,
method_definition: method_definition
)
end
else
[]
end
Expand All @@ -37,9 +41,37 @@ def call

# @return [Array<RBS::AST::Declarations::Base>]
def declarations
::RBS::Environment.from_loader(
::RBS::EnvironmentLoader.new
).resolve_type_names.declarations
class_declarations + constant_declarations
end

# @return [Array<RBS::AST::Declarations::Class>]
def class_declarations
environment.class_decls.values.flat_map do |multi_entry|
multi_entry.decls.map(&:decl)
end
end

# @return [Array<RBS::AST::Declarations::Constant>]
def constant_declarations
environment.constant_decls.values.map(&:decl)
end

# @return [RBS::Environment]
def environment
@environment ||= ::RBS::Environment.from_loader(
environment_loader
).resolve_type_names
end

# @return [RBS::EnvironmentLoader]
def environment_loader
loader = ::RBS::EnvironmentLoader.new
::RBS::Repository::DEFAULT_STDLIB_ROOT.children.sort.each do |pathname|
loader.add(
library: pathname.basename.to_s
)
end
loader
end
end
end
Expand Down
45 changes: 38 additions & 7 deletions spec/rucoa/handlers/text_document_completion_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
end
end

context 'with method head part' do
context 'with string.to_sy' do
let(:content) do
<<~RUBY
'10'.to_sy
Expand All @@ -88,7 +88,7 @@
)
end

it 'responds completion items' do
it 'completes to_sym' do
subject
expect(server.responses).to match(
[
Expand All @@ -105,8 +105,8 @@
end
end

context 'with method dot' do
it 'responds completion items' do
context 'with string.to_' do
it 'completes to_i' do
subject
expect(server.responses).to match(
[
Expand Down Expand Up @@ -136,7 +136,7 @@
end
end

context 'with constant head part' do
context 'with File::SE' do
let(:content) do
<<~RUBY
File::SE
Expand Down Expand Up @@ -167,7 +167,7 @@
end
end

context 'with constant ::' do
context 'with File::' do
let(:content) do
<<~RUBY
File::
Expand All @@ -181,7 +181,7 @@
)
end

it 'responds completion items' do
it 'comletes File::PATH_SEPARATOR and FILE::SEPARATOR' do
subject
expect(server.responses).to match(
[
Expand All @@ -200,5 +200,36 @@
)
end
end

context 'with URI::Generi' do
let(:content) do
<<~RUBY
URI::Generi
RUBY
end

let(:position) do
Rucoa::Position.new(
column: 8,
line: 1
)
end

it 'responds completion items' do
subject
expect(server.responses).to match(
[
hash_including(
'id' => 1,
'result' => [
hash_including(
'label' => 'Generic'
)
]
)
]
)
end
end
end
end

0 comments on commit 5d33a36

Please sign in to comment.