class Solargraph::Shell
Public Class Methods
Source
# File lib/solargraph/shell.rb, line 12 def self.exit_on_failure? true end
Tell Thor to ensure the process exits with status 1 if any error happens.
Public Instance Methods
Source
# File lib/solargraph/shell.rb, line 106 def cache gem, version = nil api_map = Solargraph::ApiMap.load(Dir.pwd) spec = Gem::Specification.find_by_name(gem, version) api_map.cache_gem(spec, rebuild: options[:rebuild], out: $stdout) end
@return [void] @param gem [String] @param version [String, nil]
Source
# File lib/solargraph/shell.rb, line 94 def clear puts "Deleting all cached documentation (gems, core and stdlib)" Solargraph::PinCache.clear end
@return [void]
Source
# File lib/solargraph/shell.rb, line 66 def config(directory = '.') matches = [] if options[:extensions] Gem::Specification.each do |g| if g.name.match(/^solargraph\-[A-Za-z0-9_\-]*?\-ext/) require g.name matches.push g.name end end end conf = Solargraph::Workspace::Config.new.raw_data unless matches.empty? matches.each do |m| conf['extensions'].push m end end # @param file [File] File.open(File.join(directory, '.solargraph.yml'), 'w') do |file| file.puts conf.to_yaml end STDOUT.puts "Configuration file initialized." end
@param directory [String] @return [void]
Source
# File lib/solargraph/shell.rb, line 142 def gems *names api_map = ApiMap.load('.') if names.empty? Gem::Specification.to_a.each { |spec| do_cache spec, api_map } STDERR.puts "Documentation cached for all #{Gem::Specification.count} gems." else names.each do |name| spec = Gem::Specification.find_by_name(*name.split('=')) do_cache spec, api_map rescue Gem::MissingSpecError warn "Gem '#{name}' not found" end STDERR.puts "Documentation cached for #{names.count} gems." end end
@param names [Array<String>] @return [void]
Source
# File lib/solargraph/shell.rb, line 242 def list workspace = Solargraph::Workspace.new(options[:directory]) puts workspace.filenames unless options[:count] puts "#{workspace.filenames.length} files total." end
@return [void]
Source
# File lib/solargraph/shell.rb, line 256 def pin path api_map = Solargraph::ApiMap.load_with_cache('.', $stderr) is_method = path.include?('#') || path.include?('.') if is_method && options[:stack] scope, ns, meth = if path.include? '#' [:instance, *path.split('#', 2)] else [:class, *path.split('.', 2)] end # @sg-ignore Wrong argument type for # Solargraph::ApiMap#get_method_stack: rooted_tag # expected String, received Array<String> pins = api_map.get_method_stack(ns, meth, scope: scope) else pins = api_map.get_path_pins path end # @type [Hash{Symbol => Pin::Base}] references = {} pin = pins.first case pin when nil $stderr.puts "Pin not found for path '#{path}'" exit 1 when Pin::Namespace if options[:references] superclass_tag = api_map.qualify_superclass(pin.return_type.tag) superclass_pin = api_map.get_path_pins(superclass_tag).first if superclass_tag references[:superclass] = superclass_pin if superclass_pin end end pins.each do |pin| if options[:typify] || options[:probe] type = ComplexType::UNDEFINED type = pin.typify(api_map) if options[:typify] type = pin.probe(api_map) if options[:probe] && type.undefined? print_type(type) next end print_pin(pin) end references.each do |key, refpin| puts "\n# #{key.to_s.capitalize}:\n\n" print_pin(refpin) end end
@param path [String] The path to the method pin, e.g. ‘Class#method’ or ‘Class.method’ @return [void]
Source
# File lib/solargraph/shell.rb, line 160 def reporters puts Solargraph::Diagnostics.reporters end
@return [void]
Source
# File lib/solargraph/shell.rb, line 216 def scan directory = File.realpath(options[:directory]) # @type [Solargraph::ApiMap, nil] api_map = nil time = Benchmark.measure { api_map = Solargraph::ApiMap.load_with_cache(directory, $stdout) api_map.pins.each do |pin| begin puts pin_description(pin) if options[:verbose] pin.typify api_map pin.probe api_map rescue StandardError => e STDERR.puts "Error testing #{pin_description(pin)} #{pin.location ? "at #{pin.location.filename}:#{pin.location.range.start.line + 1}" : ''}" STDERR.puts "[#{e.class}]: #{e.message}" STDERR.puts e.backtrace.join("\n") exit 1 end end } puts "Scanned #{directory} (#{api_map.pins.length} pins) in #{time.real} seconds." end
@return [void]
Source
# File lib/solargraph/shell.rb, line 28 def socket require 'backport' port = options[:port] port = available_port if port.zero? Backport.run do Signal.trap("INT") do Backport.stop end Signal.trap("TERM") do Backport.stop end # @sg-ignore Wrong argument type for Backport.prepare_tcp_server: adapter expected Backport::Adapter, received Module<Solargraph::LanguageServer::Transport::Adapter> Backport.prepare_tcp_server host: options[:host], port: port, adapter: Solargraph::LanguageServer::Transport::Adapter STDERR.puts "Solargraph is listening PORT=#{port} PID=#{Process.pid}" end end
@return [void]
Source
# File lib/solargraph/shell.rb, line 47 def stdio require 'backport' Backport.run do Signal.trap("INT") do Backport.stop end Signal.trap("TERM") do Backport.stop end # @sg-ignore Wrong argument type for Backport.prepare_stdio_server: adapter expected Backport::Adapter, received Module<Solargraph::LanguageServer::Transport::Adapter> Backport.prepare_stdio_server adapter: Solargraph::LanguageServer::Transport::Adapter STDERR.puts "Solargraph is listening on stdio PID=#{Process.pid}" end end
@return [void]
Source
# File lib/solargraph/shell.rb, line 174 def typecheck *files directory = File.realpath(options[:directory]) workspace = Solargraph::Workspace.new(directory) level = options[:level].to_sym rules = workspace.rules(level) api_map = Solargraph::ApiMap.load_with_cache(directory, $stdout) probcount = 0 if files.empty? files = api_map.source_maps.map(&:filename) else files.map! { |file| File.realpath(file) } end filecount = 0 time = Benchmark.measure { files.each do |file| checker = TypeChecker.new(file, api_map: api_map, level: options[:level].to_sym, workspace: workspace) problems = checker.problems next if problems.empty? problems.sort! { |a, b| a.location.range.start.line <=> b.location.range.start.line } puts problems.map { |prob| "#{prob.location.filename}:#{prob.location.range.start.line + 1} - #{prob.message}" }.join("\n") filecount += 1 probcount += problems.length end # " } puts "Typecheck finished in #{time.real} seconds." puts "#{probcount} problem#{probcount != 1 ? 's' : ''} found#{files.length != 1 ? " in #{filecount} of #{files.length} files" : ''}." # " exit 1 if probcount > 0 end
@return [void]
Source
# File lib/solargraph/shell.rb, line 120 def uncache *gems raise ArgumentError, 'No gems specified.' if gems.empty? gems.each do |gem| if gem == 'core' PinCache.uncache_core next end if gem == 'stdlib' PinCache.uncache_stdlib next end spec = Gem::Specification.find_by_name(gem) PinCache.uncache_gem(spec, out: $stdout) end end
@param gems [Array<String>] @return [void]
Source
# File lib/solargraph/shell.rb, line 20 def version puts Solargraph::VERSION end
@return [void]
Private Instance Methods
Source
# File lib/solargraph/shell.rb, line 326 def do_cache gemspec, api_map # @todo if the rebuild: option is passed as a positional arg, # typecheck doesn't complain on the below line api_map.cache_gem(gemspec, rebuild: options.rebuild, out: $stdout) end
@param gemspec [Gem::Specification] @param api_map [ApiMap] @return [void]
Source
# File lib/solargraph/shell.rb, line 309 def pin_description pin desc = if pin.path.nil? || pin.path.empty? if pin.closure "#{pin.closure.path} | #{pin.name}" else "#{pin.context.namespace} | #{pin.name}" end else pin.path end desc += " (#{pin.location.filename} #{pin.location.range.start.line})" if pin.location desc end
@param pin [Solargraph::Pin::Base] @return [String]
Source
# File lib/solargraph/shell.rb, line 344 def print_pin(pin) if options[:rbs] puts pin.to_rbs else puts pin.inspect end end
@param pin [Solargraph::Pin::Base] @return [void]
Source
# File lib/solargraph/shell.rb, line 334 def print_type(type) if options[:rbs] puts type.to_rbs else puts type.rooted_tag end end
@param type [ComplexType] @return [void]