Skip to content

Commit aa53226

Browse files
committed
First iteration of rbs command
1 parent 025f2c8 commit aa53226

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

lib/solargraph/api_map.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def get_namespace_pins namespace, context
237237
def qualify tag, context_tag = ''
238238
return tag if ['self', nil].include?(tag)
239239
context_type = ComplexType.parse(context_tag)
240-
type = ComplexType.parse(tag)
240+
type = ComplexType.try_parse(tag)
241241
fqns = qualify_namespace(type.rooted_namespace, context_type.rooted_namespace)
242242
return nil if fqns.nil?
243243
fqns + type.substring

lib/solargraph/doc_map.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def resolve_path_to_gemspec path
8787
gemspec = Gem::Specification.find_by_path(path)
8888
if gemspec.nil?
8989
gem_name_guess = path.split('/').first
90+
return nil if gem_name_guess.to_s.empty?
9091
begin
9192
# this can happen when the gem is included via a local path in
9293
# a Gemfile; Gem doesn't try to index the paths in that case.

lib/solargraph/shell.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
require 'thor'
44
require 'yard'
5+
require 'sord'
56

67
module Solargraph
78
class Shell < Thor
89
include Solargraph::ServerMethods
10+
include ApiMap::SourceToYard
911

1012
# Tell Thor to ensure the process exits with status 1 if any error happens.
1113
def self.exit_on_failure?
@@ -263,6 +265,29 @@ def gems *names
263265
end
264266
end
265267

268+
desc 'rbs', 'Generate RBS definitions'
269+
option :inference, type: :boolean, desc: 'Enhance definitions with type inference', default: true
270+
def rbs
271+
api_map = Solargraph::ApiMap.load('.')
272+
pins = api_map.source_maps.flat_map(&:pins)
273+
store = Solargraph::ApiMap::Store.new(pins)
274+
if options[:inference]
275+
store.method_pins.each do |pin|
276+
if pin.return_type.undefined?
277+
type = pin.typify(api_map)
278+
type = pin.probe(api_map) if type.undefined?
279+
pin.docstring.add_tag YARD::Tags::Tag.new('return', nil, type.items.map(&:to_s))
280+
pin.instance_variable_set(:@return_type, type)
281+
end
282+
end
283+
end
284+
rake_yard(store)
285+
YARD::Registry.save(false, '.yardoc')
286+
YARD::Registry.load('.yardoc')
287+
FileUtils.mkdir_p('sig')
288+
`sord gen sig/solargraph.rbs --rbs --no-regenerate`
289+
end
290+
266291
private
267292

268293
# @param pin [Solargraph::Pin::Base]

solargraph.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
3737
s.add_runtime_dependency 'rbs', '~> 3.3'
3838
s.add_runtime_dependency 'reverse_markdown', '>= 2.0', '< 4'
3939
s.add_runtime_dependency 'rubocop', '~> 1.38'
40+
s.add_runtime_dependency 'sord', '~> 7.0'
4041
s.add_runtime_dependency 'thor', '~> 1.0'
4142
s.add_runtime_dependency 'tilt', '~> 2.0'
4243
s.add_runtime_dependency 'yard', '~> 0.9', '>= 0.9.24'

0 commit comments

Comments
 (0)