Class: Solargraph::CodeMap

Inherits:
Object
  • Object
show all
Includes:
NodeMethods
Defined in:
lib/solargraph/code_map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeMethods

#const_from, #infer, #pack_name, #resolve_node_signature, #stack_node_signature, #unpack_name, #yard_options

Constructor Details

#initialize(code: '', filename: nil, workspace: nil, api_map: nil) ⇒ CodeMap

Returns a new instance of CodeMap.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/solargraph/code_map.rb', line 12

def initialize code: '', filename: nil, workspace: nil, api_map: nil
  unless workspace.nil?
    workspace = workspace.gsub(File::ALT_SEPARATOR, File::SEPARATOR) unless File::ALT_SEPARATOR.nil?
  end
  if workspace.nil? and !filename.nil?
    filename = filename.gsub(File::ALT_SEPARATOR, File::SEPARATOR) unless File::ALT_SEPARATOR.nil?
    workspace = CodeMap.find_workspace(filename)
  end
  @workspace = workspace
  @filename = filename
  @api_map = api_map
  @code = code.gsub(/\r/, '')
  tries = 0
  tmp = @code
  begin
    # HACK: The current file is parsed with a trailing underscore to fix
    # incomplete trees resulting from short scripts (e.g., a lone variable
    # assignment).
    node, comments = Parser::CurrentRuby.parse_with_comments(tmp + "\n_")
    @node = self.api_map.append_node(node, comments, filename)
    @parsed = tmp
    @code.freeze
    @parsed.freeze
  rescue Parser::SyntaxError => e
    if tries < 10
      tries += 1
      if tries == 10 and e.message.include?('token $end')
        tmp += "\nend"
      else
        spot = e.diagnostic.location.begin_pos
        repl = '_'
        if tmp[spot] == '@' or tmp[spot] == ':'
          # Stub unfinished instance variables and symbols
          spot -= 1
        elsif tmp[spot - 1] == '.'
          # Stub unfinished method calls
          repl = '#' if spot == tmp.length or tmp[spot] == '\n'
          spot -= 2
        else
          # Stub the whole line
          spot = beginning_of_line_from(tmp, spot)
          repl = '#'
          if tmp[spot+1..-1].rstrip == 'end'
            repl= 'end;end'
          end
        end
        tmp = tmp[0..spot] + repl + tmp[spot+repl.length+1..-1].to_s
      end
      retry
    end
    raise e
  end
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/solargraph/code_map.rb', line 6

def code
  @code
end

#nodeObject

Returns the value of attribute node.



5
6
7
# File 'lib/solargraph/code_map.rb', line 5

def node
  @node
end

#parsedObject (readonly)

Returns the value of attribute parsed.



7
8
9
# File 'lib/solargraph/code_map.rb', line 7

def parsed
  @parsed
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



8
9
10
# File 'lib/solargraph/code_map.rb', line 8

def workspace
  @workspace
end

Class Method Details

.find_workspace(filename) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/solargraph/code_map.rb', line 71

def self.find_workspace filename
  return nil if filename.nil?
  dirname = filename
  lastname = nil
  result = nil
  until dirname == lastname
    if File.file?("#{dirname}/Gemfile")
      result = dirname
      break
    end
    lastname = dirname
    dirname = File.dirname(dirname)
  end
  result ||= File.dirname(filename)
  result.gsub!(File::ALT_SEPARATOR, File::SEPARATOR) unless File::ALT_SEPARATOR.nil?
  result
end

Instance Method Details

#api_mapSolargraph::ApiMap

Returns:



67
68
69
# File 'lib/solargraph/code_map.rb', line 67

def api_map
  @api_map ||= ApiMap.new(workspace)
end

#build_signature(node, parts) ⇒ Array<String>

Build a signature from the specified node. This method returns the node as an array of strings.

Returns:

  • (Array<String>)


449
450
451
452
453
454
455
456
457
458
# File 'lib/solargraph/code_map.rb', line 449

def build_signature(node, parts)
  if node.kind_of?(AST::Node)
    if node.type == :send
      parts.unshift node.children[1].to_s
    elsif node.type == :const
      parts.unshift unpack_name(node)
    end
    build_signature(node.children[0], parts)
  end
end

#get_instance_variables_at(index) ⇒ Object



192
193
194
195
196
# File 'lib/solargraph/code_map.rb', line 192

def get_instance_variables_at(index)
  node = parent_node_from(index, :def, :defs, :class, :module)
  ns = namespace_at(index) || ''
  api_map.get_instance_variables(ns, (node.type == :def ? :instance : :class))
end

#get_local_variables_and_methods_at(index) ⇒ Array<Solargraph::Suggestion>

Get an array of local variables and methods that can be accessed from the specified location in the code.

Returns:



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/solargraph/code_map.rb', line 483

def get_local_variables_and_methods_at(index)
  result = []
  local = parent_node_from(index, :class, :module, :def, :defs) || @node
  result += get_local_variables_from(local)
  scope = namespace_at(index) || @node
  if local.type == :def
    result += api_map.get_instance_methods(scope, visibility: [:public, :private, :protected])
  else
    result += api_map.get_methods(scope, visibility: [:public, :private, :protected])
  end
  if local.type == :def or local.type == :defs
    result += get_method_arguments_from local
  end
  result += api_map.get_methods('Kernel')
  result
end

#get_offset(line, col) ⇒ Integer

Get the offset of the specified line and column. The offset (also called the “index”) is typically used to identify the cursor’s location in the code when generating suggestions. The line and column numbers should start at zero.

Returns:

  • (Integer)


95
96
97
98
99
100
101
102
103
# File 'lib/solargraph/code_map.rb', line 95

def get_offset line, col
  offset = 0
  if line > 0
    @code.lines[0..line - 1].each { |l|
      offset += l.length
    }
  end
  offset + col
end

#get_signature_at(index) ⇒ String

Get the signature at the specified index. A signature is a method call that can start with a constant, method, or variable and does not include any method arguments. Examples:

Code Signature


String.new String.new y.split(‘, ’).length y.split.length

Returns:

  • (String)


413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/solargraph/code_map.rb', line 413

def get_signature_at index
  brackets = 0
  squares = 0
  parens = 0
  signature = ''
  index -=1
  while index >= 0
    break if brackets > 0 or parens > 0 or squares > 0
    char = @code[index, 1]
    if char == ')'
      parens -=1
    elsif char == ']'
      squares -=1
    elsif char == '}'
      brackets -= 1
    elsif char == '('
      parens += 1
    elsif char == '{'
      brackets += 1
    elsif char == '['
      squares += 1
    end
    if brackets == 0 and parens == 0 and squares == 0
      break if ['"', "'", ',', ' ', "\t", "\n"].include?(char)
      signature = char + signature if char.match(/[a-z0-9:\._@]/i)
      break if char == '@'
    end
    index -= 1
  end
  signature
end

#get_snippets_at(index) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/solargraph/code_map.rb', line 460

def get_snippets_at(index)
  result = []
  Snippets.definitions.each_pair { |name, detail|
    matched = false
    prefix = detail['prefix']
    while prefix.length > 0
      if @code[index-prefix.length, prefix.length] == prefix
        matched = true
        break
      end
      prefix = prefix[0..-2]
    end
    if matched
      result.push Suggestion.new(detail['prefix'], kind: Suggestion::SNIPPET, detail: name, insert: detail['body'].join("\r\n"))
    end
  }
  result
end

#get_type_comment(node) ⇒ Object



392
393
394
395
396
397
398
399
400
# File 'lib/solargraph/code_map.rb', line 392

def get_type_comment node
  obj = nil
  cmnt = api_map.get_comment_for(node)
  unless cmnt.nil?
    tag = cmnt.tag(:type)
    obj = tag.types[0] unless tag.nil? or tag.types.empty?
  end
  obj
end

#infer_signature_at(index) ⇒ Object



303
304
305
306
307
# File 'lib/solargraph/code_map.rb', line 303

def infer_signature_at index
  signature = get_signature_at(index)
  node = parent_node_from(index, :class, :module, :def, :defs) || @node
  infer_signature_from_node signature, node
end

#infer_signature_from_node(signature, node) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/solargraph/code_map.rb', line 318

def infer_signature_from_node signature, node
  inferred = nil
  parts = signature.split('.')
  ns_here = namespace_from(node)
  start = parts[0]
  return nil if start.nil?
  remainder = parts[1..-1]
  scope = :instance
  var = find_local_variable_node(start, node)
  if var.nil?
    if start.start_with?('@')
      scope2 = (node.type == :def ? :instance : :class)
      type = api_map.infer_instance_variable(start, ns_here, scope2)
    else
      if node.type == :def or node.type == :defs
        args = get_method_arguments_from(node).keep_if{|a| a.label == start}
        if args.empty?
          scope = :class
          type = api_map.find_fully_qualified_namespace(start, ns_here)
          if type.nil?
            # It's a method call
            sig_scope = (node.type == :def ? :instance : :class)
            type = api_map.infer_signature_type(start, ns_here, scope: sig_scope)
          else
            return nil if remainder.empty?
          end
        else
          cmnt = api_map.get_comment_for(node)
          unless cmnt.nil?
            params = cmnt.tags(:param)
            unless params.nil?
              params.each do |p|
                if p.name == args[0].label
                  type = p.types[0]
                  break
                end
              end
            end
          end
        end
      else
        scope = :class
        type = api_map.find_fully_qualified_namespace(start, ns_here)
        if type.nil?
          # It's a method call
          sig_scope = (node.type == :def ? :instance : :class)
          type = api_map.infer_signature_type(start, ns_here, scope: sig_scope)
        else
          return nil if remainder.empty?
        end
      end
    end
  else
    # Signature starts with a local variable
    type = get_type_comment(var)
    type = infer(var.children[1]) if type.nil?
    if type.nil?
      vsig = resolve_node_signature(var.children[1])
      type = infer_signature_from_node vsig, node
    end
  end
  unless type.nil?
    inferred = api_map.infer_signature_type(remainder.join('.'), type, scope: scope)
  end
  inferred
end

#local_variable_in_node?(name, node) ⇒ Boolean

Returns:

  • (Boolean)


309
310
311
312
313
314
315
316
# File 'lib/solargraph/code_map.rb', line 309

def local_variable_in_node?(name, node)
  return true unless find_local_variable_node(name, node).nil?
  if node.type == :def or node.type == :defs
    args = get_method_arguments_from(node).keep_if{|a| a.label == name}
    return true unless args.empty?
  end
  false
end

#namespace_at(index) ⇒ String

Get the namespace at the specified location. For example, given the code ‘class Foo; def bar; end; end`, index 14 (the center) is in the “Foo” namespace.

Returns:

  • (String)


139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/solargraph/code_map.rb', line 139

def namespace_at(index)
  tree = tree_at(index)
  return nil if tree.length == 0
  slice = tree
  parts = []
  slice.reverse.each { |n|
    if n.type == :class or n.type == :module
      c = const_from(n.children[0])
      parts.push c
    end
  }
  parts.join("::")
end

#namespace_from(node) ⇒ String

Get the namespace for the specified node. For example, given the code ‘class Foo; def bar; end; end`, the node for `def bar` is in the “Foo” namespace.

Returns:

  • (String)


158
159
160
161
162
163
164
# File 'lib/solargraph/code_map.rb', line 158

def namespace_from(node)
  if node.respond_to?(:loc)
    namespace_at(node.loc.expression.begin_pos)
  else
    ''
  end
end

#node_at(index) ⇒ Object



112
113
114
# File 'lib/solargraph/code_map.rb', line 112

def node_at(index)
  tree_at(index).first
end

#parent_node_from(index, *types) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/solargraph/code_map.rb', line 124

def parent_node_from(index, *types)
  arr = tree_at(index)
  arr.each { |a|
    if a.kind_of?(AST::Node) and (types.empty? or types.include?(a.type))
      return a
    end
  }
  @node
end

#phrase_at(index) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/solargraph/code_map.rb', line 166

def phrase_at index
  word = ''
  cursor = index - 1
  while cursor > -1
    char = @code[cursor, 1]
    break if char.nil? or char == ''
    break unless char.match(/[\s;=\(\)\[\]\{\}]/).nil?
    word = char + word
    cursor -= 1
  end
  word
end

#resolve_object_at(index) ⇒ Object



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
# File 'lib/solargraph/code_map.rb', line 268

def resolve_object_at index
  return [] if string_at?(index)
  signature = get_signature_at(index)
  cursor = index
  while @code[cursor] =~ /[a-z0-9_\?]/i
    signature += @code[cursor]
    cursor += 1
    break if cursor >= @code.length
  end
  return [] if signature.to_s == ''
  path = nil
  ns_here = namespace_at(index)
  node = parent_node_from(index, :class, :module, :def, :defs) || @node
  parts = signature.split('.')
  if parts.length > 1
    beginner = parts[0..-2].join('.')
    type = infer_signature_from_node(beginner, node)
    ender = parts.last
    path = "#{type}##{ender}"
  else
    if local_variable_in_node?(signature, node)
      path = infer_signature_from_node(signature, node)
    elsif signature.start_with?('@')
      path = api_map.infer_instance_variable(signature, ns_here, (node.type == :def ? :instance : :class))
    else
      path = signature
    end
    if path.nil?
      path = api_map.find_fully_qualified_namespace(signature, ns_here)
    end
  end
  return [] if path.nil?
  return api_map.yard_map.objects(path, ns_here)
end

#signatures_at(index) ⇒ Object



261
262
263
264
265
266
# File 'lib/solargraph/code_map.rb', line 261

def signatures_at index
  sig = signature_index_before(index)
  return [] if sig.nil?
  word = word_at(sig)
  suggest_at(sig).reject{|s| s.label != word}
end

#string_at?(index) ⇒ Boolean

Determine if the specified index is inside a string.

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/solargraph/code_map.rb', line 119

def string_at?(index)
  n = node_at(index)
  n.kind_of?(AST::Node) and n.type == :str
end

#suggest_at(index, filtered: false, with_snippets: false) ⇒ Array<Suggestions>

Get suggestions for code completion at the specified location in the source.

Returns:

  • (Array<Suggestions>)

    The completion suggestions



202
203
204
205
206
207
208
209
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
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/solargraph/code_map.rb', line 202

def suggest_at index, filtered: false, with_snippets: false
  return [] if string_at?(index) or string_at?(index - 1)
  result = []
  phrase = phrase_at(index)
  signature = get_signature_at(index)
  namespace = namespace_at(index)
  if signature.include?('.')
    # Check for literals first
    type = infer(node_at(index - 2))
    if type.nil?
      nearest = @code[0, index].rindex('.')
      revised = signature[0..nearest-index-1]
      type = infer_signature_at(nearest) unless revised.empty?
      if !type.nil?
        result.concat api_map.get_instance_methods(type) unless type.nil?
      elsif !revised.include?('.')
        fqns = api_map.find_fully_qualified_namespace(revised, namespace)
        result.concat api_map.get_methods(fqns) unless fqns.nil?
      end
    else
      result.concat api_map.get_instance_methods(type)
    end
  elsif signature.start_with?('@')
    result.concat get_instance_variables_at(index)
  elsif phrase.start_with?('$')
    result.concat api_map.get_global_variables
  elsif phrase.include?('::')
    parts = phrase.split('::', -1)
    ns = parts[0..-2].join('::')
    if parts.last.include?('.')
      ns = parts[0..-2].join('::') + '::' + parts.last[0..parts.last.index('.')-1]
      result = api_map.get_methods(ns)
    else
      result = api_map.namespaces_in(ns, namespace)
    end
  else
    current_namespace = namespace_at(index)
    parts = current_namespace.to_s.split('::')
    result += get_snippets_at(index) if with_snippets
    result += get_local_variables_and_methods_at(index)
    result += ApiMap.get_keywords
    while parts.length > 0
      ns = parts.join('::')
      result += api_map.namespaces_in(ns, namespace)
      parts.pop
    end
    result += api_map.namespaces_in('')
    result += api_map.get_instance_methods('Kernel')
    #unless @filename.nil? or @api_map.yardoc_has_file?(@filename)
    #  m = @code.match(/# +@bind \[([a-z0-9_:]*)/i)
    #  unless m.nil?
    #    @api_map.get_instance_methods(m[1])
    #  end
    #end
  end
  result = reduce_starting_with(result, word_at(index)) if filtered
  result.uniq{|s| s.path}.sort{|a,b| a.label <=> b.label}
end

#suggest_for_signature_at(index) ⇒ Object



385
386
387
388
389
390
# File 'lib/solargraph/code_map.rb', line 385

def suggest_for_signature_at index
  result = []
  type = infer_signature_at(index)
  result.concat api_map.get_instance_methods(type) unless type.nil?
  result
end

#tree_at(index) ⇒ Object



105
106
107
108
109
110
# File 'lib/solargraph/code_map.rb', line 105

def tree_at(index)
  arr = []
  arr.push @node
  inner_node_at(index, @node, arr)
  arr
end

#word_at(index) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/solargraph/code_map.rb', line 179

def word_at index
  word = ''
  cursor = index - 1
  while cursor > -1
    char = @code[cursor, 1]
    break if char.nil? or char == ''
    break unless char.match(/[a-z0-9_]/i)
    word = char + word
    cursor -= 1
  end
  word
end