Skip to content

Commit 6d5e8fa

Browse files
committed
First iteration of rbs command
1 parent bb3ace7 commit 6d5e8fa

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

lib/solargraph/doc_map.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def resolve_path_to_gemspecs path
140140
gemspec = Gem::Specification.find_by_path(path)
141141
if gemspec.nil?
142142
gem_name_guess = path.split('/').first
143+
return nil if gem_name_guess.to_s.empty?
143144
begin
144145
# this can happen when the gem is included via a local path in
145146
# a Gemfile; Gem doesn't try to index the paths in that case.

lib/solargraph/shell.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
require 'benchmark'
44
require 'thor'
55
require 'yard'
6+
require 'sord'
67

78
module Solargraph
89
class Shell < Thor
910
include Solargraph::ServerMethods
11+
include ApiMap::SourceToYard
1012

1113
# Tell Thor to ensure the process exits with status 1 if any error happens.
1214
def self.exit_on_failure?
@@ -236,6 +238,67 @@ def list
236238
puts "#{workspace.filenames.length} files total."
237239
end
238240

241+
desc 'cache', 'Cache a gem', hide: true
242+
# @return [void]
243+
# @param gem [String]
244+
# @param version [String, nil]
245+
def cache gem, version = nil
246+
spec = Gem::Specification.find_by_name(gem, version)
247+
pins = GemPins.build(spec)
248+
Cache.save('gems', "#{spec.name}-#{spec.version}.ser", pins)
249+
end
250+
251+
desc 'gems', 'Cache documentation for installed gems'
252+
option :rebuild, type: :boolean, desc: 'Rebuild existing documentation', default: false
253+
# @return [void]
254+
def gems *names
255+
if names.empty?
256+
Gem::Specification.to_a.each do |spec|
257+
next unless options.rebuild || !Yardoc.cached?(spec)
258+
259+
puts "Processing gem: #{spec.name} #{spec.version}"
260+
pins = GemPins.build(spec)
261+
Cache.save('gems', "#{spec.name}-#{spec.version}.ser", pins)
262+
end
263+
else
264+
names.each do |name|
265+
spec = Gem::Specification.find_by_name(name)
266+
if spec
267+
next unless options.rebuild || !Yardoc.cached?(spec)
268+
269+
puts "Processing gem: #{spec.name} #{spec.version}"
270+
pins = GemPins.build(spec)
271+
Cache.save('gems', "#{spec.name}-#{spec.version}.ser", pins)
272+
else
273+
warn "Gem '#{name}' not found"
274+
end
275+
end
276+
end
277+
end
278+
279+
desc 'rbs', 'Generate RBS definitions'
280+
option :inference, type: :boolean, desc: 'Enhance definitions with type inference', default: true
281+
def rbs
282+
api_map = Solargraph::ApiMap.load('.')
283+
pins = api_map.source_maps.flat_map(&:pins)
284+
store = Solargraph::ApiMap::Store.new(pins)
285+
if options[:inference]
286+
store.method_pins.each do |pin|
287+
if pin.return_type.undefined?
288+
type = pin.typify(api_map)
289+
type = pin.probe(api_map) if type.undefined?
290+
pin.docstring.add_tag YARD::Tags::Tag.new('return', nil, type.items.map(&:to_s))
291+
pin.instance_variable_set(:@return_type, type)
292+
end
293+
end
294+
end
295+
rake_yard(store)
296+
YARD::Registry.save(false, '.yardoc')
297+
YARD::Registry.load('.yardoc')
298+
FileUtils.mkdir_p('sig')
299+
`sord gen sig/solargraph.rbs --rbs --no-regenerate`
300+
end
301+
239302
private
240303

241304
# @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', '~> 3.0'
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)