Class: Solargraph::ApiMap

Inherits:
Object
  • Object
show all
Includes:
NodeMethods, YardMethods
Defined in:
lib/solargraph/api_map.rb,
lib/solargraph/api_map/cache.rb,
lib/solargraph/api_map/config.rb,
lib/solargraph/api_map/attr_pin.rb,
lib/solargraph/api_map/cvar_pin.rb,
lib/solargraph/api_map/ivar_pin.rb,
lib/solargraph/api_map/method_pin.rb

Defined Under Namespace

Classes: AttrPin, Cache, Config, CvarPin, IvarPin, MethodPin

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
MAPPABLE_NODES =
[
  :array, :hash, :str, :dstr, :int, :float, :block, :class, :sclass,
  :module, :def, :defs, :ivasgn, :gvasgn, :lvasgn, :cvasgn, :casgn,
  :or_asgn, :const, :lvar, :args, :kwargs
].freeze
MAPPABLE_METHODS =
[
  :include, :extend, :require, :autoload, :attr_reader, :attr_writer,
  :attr_accessor, :private, :public, :protected
].freeze
METHODS_RETURNING_SELF =
[
  'clone', 'dup', 'freeze', 'taint', 'untaint'
]
@@yard_map_cache =
{}
@@semaphore =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from YardMethods

#yard_options

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)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/solargraph/api_map.rb', line 53

def initialize workspace = nil
  @workspace = workspace.gsub(/\\/, '/') unless workspace.nil?
  clear
  unless @workspace.nil?
    config = ApiMap::Config.new(@workspace)
    config.included.each { |f|
      unless config.excluded.include?(f)
        append_file f
      end
    }
  end
end

Instance Attribute Details

#requiredArray<String> (readonly)

Returns:

  • (Array<String>)


50
51
52
# File 'lib/solargraph/api_map.rb', line 50

def required
  @required
end

#workspaceString (readonly)

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

Returns:

  • (String)


47
48
49
# File 'lib/solargraph/api_map.rb', line 47

def workspace
  @workspace
end

Class Method Details

.currentObject



475
476
477
478
479
480
481
# File 'lib/solargraph/api_map.rb', line 475

def self.current
  if @current.nil?
    @current = ApiMap.new
    @current.merge(Parser::CurrentRuby.parse(File.read("#{Solargraph::STUB_PATH}/ruby/2.3.0/core.rb")))
  end
  @current
end

.get_keywordsArray<Solargraph::Suggestion>

Returns:



128
129
130
131
132
# File 'lib/solargraph/api_map.rb', line 128

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

Instance Method Details

#append_file(filename) ⇒ AST::Node

Add a file to the map.

Parameters:

  • filename (String)

Returns:

  • (AST::Node)


78
79
80
# File 'lib/solargraph/api_map.rb', line 78

def append_file filename
  append_source File.read(filename), filename
end

#append_node(node, comments, filename = nil) ⇒ AST::Node

Add an AST node to the map.

Returns:

  • (AST::Node)


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/solargraph/api_map.rb', line 101

def append_node node, comments, filename = nil
  @stale = true
  @file_comments[filename] = associate_comments(node, comments)
  mapified = reduce(node, @file_comments[filename])
  root = AST::Node.new(:begin, [filename])
  root = root.append mapified
  @file_nodes[filename] = root
  @required.uniq!
  #process_maps
  root
end

#append_source(text, filename = nil) ⇒ AST::Node

Add a string of source code to the map.

Parameters:

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

Returns:

  • (AST::Node)


87
88
89
90
91
92
93
94
95
96
# File 'lib/solargraph/api_map.rb', line 87

def append_source text, filename = nil
  @file_source[filename] = text
  begin
    node, comments = Parser::CurrentRuby.parse_with_comments(text)
    append_node(node, comments, filename)
  rescue Parser::SyntaxError => e
    STDERR.puts "Error parsing '#{filename}': #{e.message}"
    nil
  end
end

#code_for(node) ⇒ Object



495
496
497
498
499
500
501
# File 'lib/solargraph/api_map.rb', line 495

def code_for node
  src = @file_source[get_filename_for(node)]
  return nil if src.nil?
  b = node.location.expression.begin.begin_pos
  e = node.location.expression.end.end_pos
  src[b..e].strip.gsub(/,$/, '')
end

#find_class_variable_assignment(var, node) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/solargraph/api_map.rb', line 312

def find_class_variable_assignment(var, node)
  node.children.each { |c|
    next unless c.kind_of?(AST::Node)
    if c.type == :cvasgn
      if c.children[0].to_s == var
        return c
      end
    else
      inner = find_class_variable_assignment(var, c)
      return inner unless inner.nil?
    end
  }
  nil
end

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/solargraph/api_map.rb', line 158

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.values).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.values).each { |i|
        recname = find_fully_qualified_namespace name, i, skip
        return recname unless recname.nil?
      }
    end
  end
  yard_map.find_fully_qualified_namespace(name, root)
end

#find_instance_variable_assignment(var, node, scope) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/solargraph/api_map.rb', line 295

def find_instance_variable_assignment(var, node, scope)
  node.children.each { |c|
    if c.kind_of?(AST::Node)
      is_inst = !find_parent(c, :def).nil?
      if c.type == :ivasgn and ( (scope == :instance and is_inst) or (scope != :instance and !is_inst) )
        if c.children[0].to_s == var
          return c
        end
      else
        inner = find_instance_variable_assignment(var, c, scope)
        return inner unless inner.nil?
      end
    end
  }
  nil
end

#find_parent(node, *types) ⇒ Object



223
224
225
226
227
228
229
# File 'lib/solargraph/api_map.rb', line 223

def find_parent(node, *types)
  parents = @parent_stack[node]
  parents.each { |p|
    return p if types.include?(p.type)
  }
  nil
end

#get_class_variables(namespace) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/solargraph/api_map.rb', line 211

def get_class_variables(namespace)
  refresh
  result = []
  ip = @cvar_pins[namespace]
  unless ip.nil?
    ip.each do |pin|
      result.push pin.suggestion(self)
    end
  end
  result
end

#get_comment_for(node) ⇒ YARD::Docstring

Get the docstring associated with a node.

Parameters:

  • node (AST::Node)

Returns:

  • (YARD::Docstring)


121
122
123
124
125
# File 'lib/solargraph/api_map.rb', line 121

def get_comment_for node
  filename = get_filename_for(node)
  return nil if @file_comments[filename].nil?
  @file_comments[filename][node.loc]
end

#get_filename_for(node) ⇒ Object



238
239
240
241
# File 'lib/solargraph/api_map.rb', line 238

def get_filename_for(node)
  root = get_root_for(node)
  root.nil? ? nil : root.children[0]
end

#get_global_variablesObject



327
328
329
330
# File 'lib/solargraph/api_map.rb', line 327

def get_global_variables
  # TODO: Get them
  []
end

#get_include_strings_from(*nodes) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
# File 'lib/solargraph/api_map.rb', line 483

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
    }
  }
  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:



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/solargraph/api_map.rb', line 439

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)
  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(namespace, root)
    if type == :class
      meths += yard_map.get_instance_methods('Object')
    elsif type == :module
      meths += yard_map.get_instance_methods('Module')
    end
  end
  meths
end

#get_instance_variables(namespace, scope = :instance) ⇒ Object



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

def get_instance_variables(namespace, scope = :instance)
  refresh
  result = []
  ip = @ivar_pins[namespace]
  unless ip.nil?
    ip.select{ |pin| pin.scope == scope }.each do |pin|
      result.push pin.suggestion(self)
    end
  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:



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/solargraph/api_map.rb', line 416

def get_methods(namespace, root = '', visibility: [:public])
  refresh
  namespace = clean_namespace_string(namespace)
  meths = []
  meths += inner_get_methods(namespace, root, []) #unless has_yardoc?
  yard_meths = yard_map.get_methods(namespace, root, visibility: visibility)
  if yard_meths.any?
    meths.concat yard_meths
  else
    type = get_namespace_type(namespace, root)
    if type == :class
      meths += yard_map.get_instance_methods('Class')
    elsif type == :module
      meths += yard_map.get_methods('Module')
    end
    meths
  end
end

#get_namespace_nodes(fqns) ⇒ Object



193
194
195
196
197
# File 'lib/solargraph/api_map.rb', line 193

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

#get_namespace_type(namespace, root = '') ⇒ Object



402
403
404
405
406
407
408
409
410
# File 'lib/solargraph/api_map.rb', line 402

def get_namespace_type namespace, root = ''
  type = nil
  fqns = find_fully_qualified_namespace(namespace, root)
  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
  type
end

#get_root_for(node) ⇒ Object



231
232
233
234
235
236
# File 'lib/solargraph/api_map.rb', line 231

def get_root_for(node)
  s = @parent_stack[node]
  return nil if s.nil?
  return node if s.empty?
  s.last
end

#get_superclass(namespace, root = '') ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/solargraph/api_map.rb', line 462

def get_superclass(namespace, root = '')
  fqns = find_fully_qualified_namespace(namespace, root)
  nodes = get_namespace_nodes(fqns)
  nodes.each { |n|
    if n.kind_of?(AST::Node)
      if n.type == :class and !n.children[1].nil?
        return unpack_name(n.children[1])
      end
    end
  }
  return nil
end

#infer_assignment_node_type(node, namespace) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/solargraph/api_map.rb', line 332

def infer_assignment_node_type node, namespace
  type = cache.get_assignment_node_type(node, namespace)
  if type.nil?
    cmnt = get_comment_for(node)
    if cmnt.nil?
      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
    else
      t = cmnt.tag(:type)
      if t.nil?
        sig = resolve_node_signature(node.children[1])
        type = infer_signature_type(sig, namespace)
      else
        type = t.types[0]
      end
    end
    cache.set_assignment_node_type(node, namespace, type)
  end
  type
end

#infer_class_variable(var, namespace) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/solargraph/api_map.rb', line 270

def infer_class_variable(var, namespace)
  result = nil
  vn = nil
  fqns = find_fully_qualified_namespace(namespace)
  unless fqns.nil?
    get_namespace_nodes(fqns).each { |node|
      vn = find_class_variable_assignment(var, node)
      break unless vn.nil?
    }
  end
  unless vn.nil?
    cmnt = get_comment_for(vn)
    unless cmnt.nil?
      tag = cmnt.tag(:type)
      result = tag.types[0] unless tag.nil? or tag.types.empty?
    end
    result = infer_literal_node_type(vn.children[1]) if result.nil?
    if result.nil?
      signature = resolve_node_signature(vn.children[1])
      result = infer_signature_type(signature, namespace)
    end
  end
  result
end

#infer_instance_variable(var, namespace, scope) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/solargraph/api_map.rb', line 256

def infer_instance_variable(var, namespace, scope)
  result = nil
  vn = nil
  fqns = find_fully_qualified_namespace(namespace)
  unless fqns.nil?
    get_namespace_nodes(fqns).each { |node|
      vn = find_instance_variable_assignment(var, node, scope)
      break unless vn.nil?
    }
  end
  result = infer_assignment_node_type(vn, namespace) unless vn.nil?
  result
end

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



360
361
362
363
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
# File 'lib/solargraph/api_map.rb', line 360

def infer_signature_type signature, namespace, scope: :class
  cached = cache.get_signature_type(signature, namespace, scope)
  return cached unless cached.nil?
  return nil if signature.nil? or signature.empty?
  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

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

Returns:

  • (Boolean)


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

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

#namespacesObject



134
135
136
137
# File 'lib/solargraph/api_map.rb', line 134

def namespaces
  refresh
  @namespace_map.keys
end

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/solargraph/api_map.rb', line 143

def namespaces_in name, root = ''
  refresh
  result = []
  result += inner_namespaces_in(name, root, [])
  result += yard_map.get_constants name, root
  fqns = find_fully_qualified_namespace(name, root)
  unless fqns.nil?
    nodes = get_namespace_nodes(fqns)
    get_include_strings_from(*nodes).each { |i|
      result += yard_map.get_constants(i, root)
    }
  end
  result
end

#refresh(force = false) ⇒ Object



113
114
115
# File 'lib/solargraph/api_map.rb', line 113

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

#update_yardocObject

Update the YARD documentation for the current workspace.



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/solargraph/api_map.rb', line 505

def update_yardoc
  if workspace.nil?
    STDERR.puts "No workspace specified for yardoc update."
  else
    Dir.chdir(workspace) do
      STDERR.puts "Updating the yardoc for #{workspace}..."
      cmd = "yardoc -e #{Solargraph::YARD_EXTENSION_FILE}"
      STDERR.puts "Update yardoc with #{cmd}"
      STDERR.puts `#{cmd}`
      unless $?.success?
        STDERR.puts "There was an error processing the workspace yardoc."
      end
    end
    @@semaphore.synchronize {
      @@yard_map_cache.clear
    }
    end
end

#yard_mapSolargraph::YardMap

Returns:



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

def yard_map
  @@semaphore.synchronize {
    @yard_map ||= @@yard_map_cache[[required, workspace]] || Solargraph::YardMap.new(required: required, workspace: workspace)
    @@yard_map_cache[[required, workspace]] ||= @yard_map
  }
end

#yardoc_has_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/solargraph/api_map.rb', line 243

def yardoc_has_file?(file)
  return false if workspace.nil?
  if @yardoc_files.nil?
    @yardoc_files = []
    yard_options[:include].each { |glob|
      Dir[File.join workspace, glob].each { |f|
        @yardoc_files.push File.absolute_path(f)
      }
    }
  end
  @yardoc_files.include?(file)
end