Class: Solargraph::ApiMap

Inherits:
Object
  • Object
show all
Includes:
SourceToYard, NodeMethods
Defined in:
lib/solargraph/api_map.rb,
lib/solargraph/api_map/cache.rb,
lib/solargraph/api_map/config.rb,
lib/solargraph/api_map/source.rb,
lib/solargraph/api_map/source_to_yard.rb

Defined Under Namespace

Modules: SourceToYard Classes: Cache, Config, Source

Constant Summary collapse

KEYWORDS =
[
  '__ENCODING__', '__LINE__', '__FILE__', 'BEGIN', 'END', 'alias', 'and',
  'begin', 'break', 'case', 'class', 'def', 'defined?', 'do', 'else',
  'elsif', 'end', 'ensure', 'false', 'for', 'if', 'in', 'module', 'next',
  'nil', 'not', 'or', 'redo', 'rescue', 'retry', 'return', 'self', 'super',
  'then', 'true', 'undef', 'unless', 'until', 'when', 'while', 'yield'
].freeze
METHODS_RETURNING_SELF =
[
  'clone', 'dup', 'freeze', 'taint', 'untaint'
].freeze
@@source_cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeMethods

#const_from, #infer_literal_node_type, #pack_name, #resolve_node_signature, #unpack_name

Constructor Details

#initialize(workspace = nil) ⇒ ApiMap

Returns a new instance of ApiMap.

Parameters:

  • workspace (String) (defaults to: nil)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/solargraph/api_map.rb', line 36

def initialize workspace = nil
  @workspace = workspace.gsub(/\\/, '/') unless workspace.nil?
  clear
  require_extensions
  unless @workspace.nil?
    workspace_files.concat (self.config.included - self.config.excluded)
    workspace_files.each do |wf|
      begin
        @@source_cache[wf] ||= Source.load(wf)
      rescue Parser::SyntaxError => e
        STDERR.puts "Failed to load #{wf}: #{e.message}"
      end
    end
  end
  @sources = {}
  @virtual_source = nil
  @virtual_filename = nil
  @stale = true
  @yard_stale = true
  refresh
  yard_map
end

Instance Attribute Details

#workspace ⇒ String (readonly)

The root directory of the project. The ApiMap will search here for additional files to parse and analyze.

Returns:

  • (String)


33
34
35
# File 'lib/solargraph/api_map.rb', line 33

def workspace
  @workspace
end

Class Method Details

.get_keywords ⇒ Array<Solargraph::Suggestion>

Returns:



151
152
153
154
155
# File 'lib/solargraph/api_map.rb', line 151

def self.get_keywords
  @keyword_suggestions ||= KEYWORDS.map{ |s|
    Suggestion.new(s.to_s, kind: Suggestion::KEYWORD, detail: 'Keyword')
  }.freeze
end

Instance Method Details

#append_source(code, filename) ⇒ Solargraph::ApiMap::Source



127
128
129
# File 'lib/solargraph/api_map.rb', line 127

def append_source code, filename
  virtualize code, filename
end

#config(reload = false) ⇒ Solargraph::ApiMap::Config



60
61
62
63
# File 'lib/solargraph/api_map.rb', line 60

def config reload = false
  @config = ApiMap::Config.new(@workspace) if @config.nil? or reload
  @config
end

#document(path) ⇒ Array<YARD::CodeObject::Base>

Returns:

  • (Array<YARD::CodeObject::Base>)


589
590
591
592
593
594
595
596
597
# File 'lib/solargraph/api_map.rb', line 589

def document path
  refresh
  rake_yard(@sources.values) if @yard_stale
  @yard_stale = false
  docs = []
  docs.push code_object_at(path) unless code_object_at(path).nil?
  docs.concat yard_map.document(path)
  docs
end

#find_fully_qualified_namespace(name, root = '', skip = []) ⇒ String

Returns:

  • (String)


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/solargraph/api_map.rb', line 210

def find_fully_qualified_namespace name, root = '', skip = []
  refresh
  return nil if skip.include?(root)
  skip.push root
  if name == ''
    if root == ''
      return ''
    else
      return find_fully_qualified_namespace(root, '', skip)
    end
  else
    if (root == '')
      return name unless namespace_map[name].nil?
      get_include_strings_from(*file_nodes).each { |i|
        reroot = "#{root == '' ? '' : root + '::'}#{i}"
        recname = find_fully_qualified_namespace name.to_s, reroot, skip
        return recname unless recname.nil?
      }
    else
      roots = root.to_s.split('::')
      while roots.length > 0
        fqns = roots.join('::') + '::' + name
        return fqns unless namespace_map[fqns].nil?
        roots.pop
      end
      return name unless namespace_map[name].nil?
      get_include_strings_from(*file_nodes).each { |i|
        recname = find_fully_qualified_namespace name, i, skip
        return recname unless recname.nil?
      }
    end
  end
  result = yard_map.find_fully_qualified_namespace(name, root)
  if result.nil?
    result = live_map.get_fqns(name, root)
  end
  result
end

#find_namespace_pins(fqns) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/solargraph/api_map.rb', line 198

def find_namespace_pins fqns
  set = nil
  if fqns.include?('::')
    set = @namespace_pins[fqns.split('::')[0..-2].join('::')]
  else
    set = @namespace_pins['']
  end
  return [] if set.nil?
  set.select{|p| p.path == fqns}
end

#get_class_variable_pins(namespace) ⇒ Array<Solargraph::Pin::ClassVariable>

Returns:



273
274
275
276
# File 'lib/solargraph/api_map.rb', line 273

def get_class_variable_pins(namespace)
  refresh
  @cvar_pins[namespace] || []
end

#get_class_variables(namespace) ⇒ Array<Solargraph::Suggestion>

Returns:



279
280
281
282
283
284
285
286
287
# File 'lib/solargraph/api_map.rb', line 279

def get_class_variables(namespace)
  refresh
  result = []
  cp = @cvar_pins[namespace]
  unless cp.nil?
    result.concat suggest_unique_variables(cp)
  end
  result
end

#get_comment_for(node) ⇒ Object

Deprecated.

Use get_docstring_for instead.



146
147
148
# File 'lib/solargraph/api_map.rb', line 146

def get_comment_for node
  get_docstring_for node
end

#get_constant_pins(namespace, root) ⇒ Array<Solargraph::Pin::Constant>

Returns:



173
174
175
176
# File 'lib/solargraph/api_map.rb', line 173

def get_constant_pins namespace, root
  fqns = find_fully_qualified_namespace(namespace, root)
  @const_pins[fqns] || []
end

#get_constants(namespace, root = '') ⇒ Array<Solargraph::Suggestion>

Returns:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/solargraph/api_map.rb', line 179

def get_constants namespace, root = ''
  result = []
  skip = []
  fqns = find_fully_qualified_namespace(namespace, root)
  if fqns.empty?
    result.concat inner_get_constants('', skip, false, :public)
  else
    parts = fqns.split('::')
    resolved = find_namespace_pins(parts.join('::'))
    resolved.each do |pin|
      visi = :public
      visi = :private if root != '' and pin.path == fqns
      result.concat inner_get_constants(pin.path, skip, true, visi)
    end
  end
  result.concat yard_map.get_constants(fqns)
  result
end

#get_docstring_for(node) ⇒ YARD::Docstring

Get the docstring associated with a node.

Parameters:

  • node (AST::Node)

Returns:

  • (YARD::Docstring)


139
140
141
142
143
# File 'lib/solargraph/api_map.rb', line 139

def get_docstring_for node
  filename = get_filename_for(node)
  return nil if @sources[filename].nil?
  @sources[filename].docstring_for(node)
end

#get_filename_for(node) ⇒ String

Returns:

  • (String)


296
297
298
299
300
301
# File 'lib/solargraph/api_map.rb', line 296

def get_filename_for(node)
  @sources.each do |filename, source|
    return source.filename if source.include?(node)
  end
  nil
end

#get_global_variable_pins ⇒ Object



341
342
343
344
345
346
347
# File 'lib/solargraph/api_map.rb', line 341

def get_global_variable_pins
  globals = []
  @sources.values.each do |s|
    globals.concat s.global_variable_pins
  end
  globals
end

#get_global_variables ⇒ Array<Solargraph::Suggestion>

Returns:



333
334
335
336
337
338
339
# File 'lib/solargraph/api_map.rb', line 333

def get_global_variables
  globals = []
  @sources.values.each do |s|
    globals.concat s.global_variable_pins
  end
  suggest_unique_variables globals
end

#get_include_strings_from(*nodes) ⇒ Array<String>

Returns:

  • (Array<String>)


513
514
515
516
517
518
519
520
521
522
523
# File 'lib/solargraph/api_map.rb', line 513

def get_include_strings_from *nodes
  arr = []
  nodes.each { |node|
    next unless node.kind_of?(AST::Node)
    arr.push unpack_name(node.children[2]) if (node.type == :send and node.children[1] == :include)
    node.children.each { |n|
      arr += get_include_strings_from(n) if n.kind_of?(AST::Node) and n.type != :class and n.type != :module and n.type != :sclass
    }
  }
  arr
end

#get_instance_methods(namespace, root = '', visibility: [:public]) ⇒ Array<Solargraph::Suggestion>

Get an array of instance methods that are available in the specified namespace.

Returns:



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/solargraph/api_map.rb', line 477

def get_instance_methods(namespace, root = '', visibility: [:public])
  refresh
  namespace = clean_namespace_string(namespace)
  if namespace.end_with?('#class')
    return get_methods(namespace.split('#').first, root, visibility: visibility)
  elsif namespace.end_with?('#module')
    return get_methods(namespace.split('#').first, root, visibility: visibility)
  end
  meths = []
  meths += inner_get_instance_methods(namespace, root, [], visibility) #unless has_yardoc?
  fqns = find_fully_qualified_namespace(namespace, root)
  yard_meths = yard_map.get_instance_methods(fqns, '', visibility: visibility)
  if yard_meths.any?
    meths.concat yard_meths
  else
    type = get_namespace_type(fqns)
    if type == :class
      meths += yard_map.get_instance_methods('Object')
    elsif type == :module
      meths += yard_map.get_instance_methods('Module')
    end
  end
  if namespace == '' and root == ''
    config.domains.each do |d|
      meths.concat get_instance_methods(d)
    end
  end
  strings = meths.map(&:to_s)
  live_map.get_methods(namespace, root, 'instance', visibility.include?(:private)).each do |m|
    next if strings.include?(m) or !m.match(/^[a-z]/i)
    meths.push Suggestion.new(m, kind: Suggestion::METHOD, docstring: YARD::Docstring.new('(defined at runtime)'), path: "#{fqns}##{m}")
  end
  meths
end

#get_instance_variable_pins(namespace, scope = :instance) ⇒ Array<Solargraph::Pin::InstanceVariable>



256
257
258
259
# File 'lib/solargraph/api_map.rb', line 256

def get_instance_variable_pins(namespace, scope = :instance)
  refresh
  (@ivar_pins[namespace] || []).select{ |pin| pin.scope == scope }
end

#get_instance_variables(namespace, scope = :instance) ⇒ Array<Solargraph::Suggestion>

Returns:



262
263
264
265
266
267
268
269
270
# File 'lib/solargraph/api_map.rb', line 262

def get_instance_variables(namespace, scope = :instance)
  refresh
  result = []
  ip = @ivar_pins[namespace]
  unless ip.nil?
    result.concat suggest_unique_variables(ip.select{ |pin| pin.scope == scope })
  end
  result
end

#get_methods(namespace, root = '', visibility: [:public]) ⇒ Array<Solargraph::Suggestion>

Get an array of singleton methods that are available in the specified namespace.

Returns:



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/solargraph/api_map.rb', line 432

def get_methods(namespace, root = '', visibility: [:public])
  refresh
  namespace = clean_namespace_string(namespace)
  fqns = find_fully_qualified_namespace(namespace, root)
  meths = []
  skip = []
  meths.concat inner_get_methods(namespace, root, skip)
  yard_meths = yard_map.get_methods(fqns, '', visibility: visibility)
  if yard_meths.any?
    meths.concat yard_meths
  else
    type = get_namespace_type(fqns)
    if type == :class
      meths.concat yard_map.get_instance_methods('Class')
    else
      meths.concat yard_map.get_instance_methods('Module')
    end
  end
  news = meths.select{|s| s.label == 'new'}
  unless news.empty?
    if @method_pins[fqns]
      inits = @method_pins[fqns].select{|p| p.name == 'initialize'}
      meths -= news unless inits.empty?
      inits.each do |pin|
        meths.push Suggestion.new('new', kind: pin.kind, docstring: pin.docstring, detail: pin.namespace, arguments: pin.parameters, path: pin.path)
      end
    end
  end
  if namespace == '' and root == ''
    config.domains.each do |d|
      meths.concat get_instance_methods(d)
    end
  end
  strings = meths.map(&:to_s)
  live_map.get_methods(fqns, '', 'class', visibility.include?(:private)).each do |m|
    next if strings.include?(m) or !m.match(/^[a-z]/i)
    meths.push Suggestion.new(m, kind: Suggestion::METHOD, docstring: YARD::Docstring.new('(defined at runtime)'), path: "#{fqns}.#{m}")
  end
  meths
end

#get_namespace_nodes(fqns) ⇒ Object



249
250
251
252
253
# File 'lib/solargraph/api_map.rb', line 249

def get_namespace_nodes(fqns)
  return file_nodes if fqns == '' or fqns.nil?
  refresh
  namespace_map[fqns] || []
end

#get_namespace_type(fqns) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/solargraph/api_map.rb', line 415

def get_namespace_type fqns
  return nil if fqns.nil?
  type = nil
  nodes = get_namespace_nodes(fqns)
  unless nodes.nil? or nodes.empty? or !nodes[0].kind_of?(AST::Node)
    type = nodes[0].type if [:class, :module].include?(nodes[0].type)
  end
  if type.nil?
    type = yard_map.get_namespace_type(fqns)
  end
  type
end

#get_path_suggestions(path) ⇒ Array<String>

Returns:

  • (Array<String>)


552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/solargraph/api_map.rb', line 552

def get_path_suggestions path
  refresh
  result = []
  if path.include?('#')
    # It's an instance method
    parts = path.split('#')
    result = get_instance_methods(parts[0], '', visibility: [:public, :private, :protected]).select{|s| s.label == parts[1]}
  elsif path.include?('.')
    # It's a class method
    parts = path.split('.')
    result = get_methods(parts[0], '', visibility: [:public, :private, :protected]).select{|s| s.label == parts[1]}
  else
    # It's a class or module
    parts = path.split('::')
    np = @namespace_pins[parts[0..-2].join('::')]
    unless np.nil?
      result.concat np.select{|p| p.name == parts.last}.map{|p| pin_to_suggestion(p)}
    end
    result.concat yard_map.objects(path)
  end
  result
end

#get_source_for(node) ⇒ Solargraph::ApiMap::Source



304
305
306
307
308
309
# File 'lib/solargraph/api_map.rb', line 304

def get_source_for(node)
  @sources.each do |filename, source|
    return source if source.include?(node)
  end
  nil
end

#get_symbols ⇒ Array<Solargraph::Pin::Symbol>

Returns:



290
291
292
293
# File 'lib/solargraph/api_map.rb', line 290

def get_symbols
  refresh
  @symbol_pins.uniq(&:label)
end

#infer_assignment_node_type(node, namespace) ⇒ String

Returns:

  • (String)


350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/solargraph/api_map.rb', line 350

def infer_assignment_node_type node, namespace
  name_i = (node.type == :casgn ? 1 : 0) 
  sig_i = (node.type == :casgn ? 2 : 1)
  type = infer_literal_node_type(node.children[sig_i])
  if type.nil?
    sig = resolve_node_signature(node.children[sig_i])
    # Avoid infinite loops from variable assignments that reference themselves
    return nil if node.children[name_i].to_s == sig.split('.').first
    type = infer_signature_type(sig, namespace)
  end
  type
end

#infer_class_variable(var, namespace) ⇒ String

Returns:

  • (String)


322
323
324
325
326
327
328
329
330
# File 'lib/solargraph/api_map.rb', line 322

def infer_class_variable(var, namespace)
  refresh
  fqns = find_fully_qualified_namespace(namespace)
  pins = @cvar_pins[fqns]
  return nil if pins.nil?
  pin = pins.select{|p| p.name == var}.first
  return nil if pin.nil?
  pin.return_type
end

#infer_instance_variable(var, namespace, scope) ⇒ String

Returns:

  • (String)


312
313
314
315
316
317
318
319
# File 'lib/solargraph/api_map.rb', line 312

def infer_instance_variable(var, namespace, scope)
  refresh
  pins = @ivar_pins[namespace]
  return nil if pins.nil?
  pin = pins.select{|p| p.name == var and p.scope == scope}.first
  return nil if pin.nil?
  pin.return_type
end

#infer_signature_type(signature, namespace, scope: :class) ⇒ String

Returns:

  • (String)


364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/solargraph/api_map.rb', line 364

def infer_signature_type signature, namespace, scope: :class
  namespace ||= ''
  if cache.has_signature_type?(signature, namespace, scope)
    return cache.get_signature_type(signature, namespace, scope)
  end
  return nil if signature.nil? or signature.empty?
  if !signature.include?('.')
    fqns = find_fully_qualified_namespace(signature, namespace)
    unless fqns.nil? or fqns.empty?
      type = (get_namespace_type(fqns) == :class ? 'Class' : 'Module')
      return "#{type}<#{fqns}>"
    end
  end
  result = nil
  if namespace.end_with?('#class')
    result = infer_signature_type signature, namespace[0..-7], scope: (scope == :class ? :instance : :class)
  else
    parts = signature.split('.', 2)
    if parts[0].start_with?('@@')
      type = infer_class_variable(parts[0], namespace)
      if type.nil? or parts.empty?
        result = inner_infer_signature_type(parts[1], type, scope: :instance)
      else
        result = type
      end
    elsif parts[0].start_with?('@')
      type = infer_instance_variable(parts[0], namespace, scope)
      if type.nil? or parts.empty?
        result = inner_infer_signature_type(parts[1], type, scope: :instance)
      else
        result = type
      end
    else
      type = find_fully_qualified_namespace(parts[0], namespace)
      if type.nil?
        # It's a method call
        type = inner_infer_signature_type(parts[0], namespace, scope: scope)
        if parts[1].nil?
          result = type
        else
          result = inner_infer_signature_type(parts[1], type, scope: :instance)
        end
      else
        result = inner_infer_signature_type(parts[1], type, scope: :class)
      end
    end
  end
  cache.set_signature_type signature, namespace, scope, result
  result
end

#live_map ⇒ Solargraph::LiveMap

Returns:



89
90
91
# File 'lib/solargraph/api_map.rb', line 89

def live_map
  @live_map ||= Solargraph::LiveMap.new(self)
end

#namespace_exists?(name, root = '') ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/solargraph/api_map.rb', line 163

def namespace_exists? name, root = ''
  !find_fully_qualified_namespace(name, root).nil?
end

#namespaces ⇒ Array<String>

Returns:

  • (Array<String>)


158
159
160
161
# File 'lib/solargraph/api_map.rb', line 158

def namespaces
  refresh
  namespace_map.keys
end

#namespaces_in(name, root = '') ⇒ Object

Deprecated.

Use get_constants instead.



168
169
170
# File 'lib/solargraph/api_map.rb', line 168

def namespaces_in name, root = ''
  get_constants name, root
end

#refresh(force = false) ⇒ Object



131
132
133
# File 'lib/solargraph/api_map.rb', line 131

def refresh force = false
  process_maps if @stale or force
end

#required ⇒ Array<String>

An array of required paths in the workspace.

Returns:

  • (Array<String>)


75
76
77
# File 'lib/solargraph/api_map.rb', line 75

def required
  @required ||= []
end

#search(query) ⇒ Object



575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/solargraph/api_map.rb', line 575

def search query
  refresh
  rake_yard(@sources.values) if @yard_stale
  @yard_stale = false
  found = []
  code_object_paths.each do |k|
    if found.empty? or (query.include?('.') or query.include?('#')) or !(k.include?('.') or k.include?('#'))
      found.push k if k.downcase.include?(query.downcase)
    end
  end
  found.concat(yard_map.search(query)).uniq.sort
end

#sources ⇒ Object



547
548
549
# File 'lib/solargraph/api_map.rb', line 547

def sources
  @sources.values
end

#update(filename) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/solargraph/api_map.rb', line 525

def update filename
  filename.gsub!(/\\/, '/')
  if filename.end_with?('.rb')
    if @virtual_filename == filename
      @virtual_filename = nil
      @virtual_source = nil
    end
    if @workspace_files.include?(filename)
      eliminate filename
      @@source_cache[filename] = Source.load(filename)
      rebuild_local_yardoc #if @workspace_files.include?(filename)
      @stale = true
    else
      @workspace_files = config(true).calculated
      update filename if @workspace_files.include?(filename)
    end
  elsif File.basename(filename) == '.solargraph.yml'
    # @todo Finish refreshing the map
    @workspace_files = config(true).calculated
  end
end

#virtualize(code, filename = nil, cursor = nil) ⇒ Solargraph::ApiMap::Source

TODO:

Get rid of the cursor parameter. Tracking stubbed lines is the better option.

Parameters:

  • code (String)
  • filename (String) (defaults to: nil)

Returns:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/solargraph/api_map.rb', line 99

def virtualize code, filename = nil, cursor = nil
  workspace_files.delete_if do |f|
    if File.exist?(f)
      false
    else
      eliminate f
      true
    end
  end
  if filename.nil? or filename.end_with?('.rb')
    eliminate @virtual_filename unless @virtual_source.nil? or @virtual_filename == filename or workspace_files.include?(@virtual_filename)
    @virtual_filename = filename
    @virtual_source = Source.fix(code, filename, cursor)
    unless filename.nil? or workspace_files.include?(filename)
      current_files = @workspace_files
      @workspace_files = config(true).calculated
      (current_files - @workspace_files).each { |f| eliminate f }
    end
    process_virtual
  else
    unless filename.nil?
      # @todo Handle special files like .solargraph.yml
    end
  end
  @virtual_source
end

#workspace_files ⇒ Object

An array of all workspace files included in the map.

@return



68
69
70
# File 'lib/solargraph/api_map.rb', line 68

def workspace_files
  @workspace_files ||= []
end

#yard_map ⇒ Solargraph::YardMap

Returns:



80
81
82
83
84
85
86
# File 'lib/solargraph/api_map.rb', line 80

def yard_map
  refresh
  if @yard_map.nil? || @yard_map.required.to_set != required.to_set
    @yard_map = Solargraph::YardMap.new(required: required, workspace: workspace)
  end
  @yard_map
end