Class: Solargraph::Shell

Inherits:
Thor
  • Object
show all
Includes:
ServerMethods
Defined in:
lib/solargraph/shell.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ServerMethods

#available_port

Class Method Details

.exit_on_failure?Boolean

Tell Thor to ensure the process exits with status 1 if any error happens.

Returns:

  • (Boolean)


12
13
14
# File 'lib/solargraph/shell.rb', line 12

def self.exit_on_failure?
  true
end

Instance Method Details

#cache(gem, version = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • gem (String)
  • version (String, nil) (defaults to: nil)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/solargraph/shell.rb', line 106

def cache gem, version = nil
  gemspec = Gem::Specification.find_by_name(gem, version)

  if options[:rebuild] || !PinCache.has_yard?(gemspec)
    pins = GemPins.build_yard_pins(['yard-activesupport-concern'], gemspec)
    PinCache.serialize_yard_gem(gemspec, pins)
  end

  workspace = Solargraph::Workspace.new(Dir.pwd)
  rbs_map = RbsMap.from_gemspec(gemspec, workspace.rbs_collection_path, workspace.rbs_collection_config_path)
  if options[:rebuild] || !PinCache.has_rbs_collection?(gemspec, rbs_map.cache_key)
    # cache pins even if result is zero, so we don't retry building pins
    pins = rbs_map.pins || []
    PinCache.serialize_rbs_collection_gem(gemspec, rbs_map.cache_key, pins)
  end
end

#clearvoid

This method returns an undefined value.



94
95
96
97
# File 'lib/solargraph/shell.rb', line 94

def clear
  puts "Deleting all cached documentation (gems, core and stdlib)"
  Solargraph::PinCache.clear
end

#config(directory = '.') ⇒ void

This method returns an undefined value.

Parameters:

  • directory (String) (defaults to: '.')


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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

#gems(*names) ⇒ void

This method returns an undefined value.

Parameters:

  • names (Array<String>)


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/solargraph/shell.rb', line 153

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

#listvoid

This method returns an undefined value.



253
254
255
256
257
# File 'lib/solargraph/shell.rb', line 253

def list
  workspace = Solargraph::Workspace.new(options[:directory])
  puts workspace.filenames unless options[:count]
  puts "#{workspace.filenames.length} files total."
end

#pin(path) ⇒ void

This method returns an undefined value.

Parameters:

  • path (String)

    The path to the method pin, e.g. 'Class#method' or 'Class.method'



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/solargraph/shell.rb', line 267

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

#reportersvoid

This method returns an undefined value.



171
172
173
# File 'lib/solargraph/shell.rb', line 171

def reporters
  puts Solargraph::Diagnostics.reporters
end

#scanvoid

This method returns an undefined value.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/solargraph/shell.rb', line 227

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

#socketvoid

This method returns an undefined value.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

#stdiovoid

This method returns an undefined value.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

#typecheck(*files) ⇒ void

This method returns an undefined value.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/solargraph/shell.rb', line 185

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

#uncache(*gems) ⇒ void

This method returns an undefined value.

Parameters:

  • gems (Array<String>)

Raises:

  • (ArgumentError)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/solargraph/shell.rb', line 131

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

#versionvoid

This method returns an undefined value.



20
21
22
# File 'lib/solargraph/shell.rb', line 20

def version
  puts Solargraph::VERSION
end