Class: Solargraph::SourceMap::Clip

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/source_map/clip.rb

Overview

A static analysis tool for obtaining definitions, completions, signatures, and type inferences from a cursor.

Instance Method Summary collapse

Constructor Details

#initialize(api_map, cursor) ⇒ Clip

Returns a new instance of Clip.

Parameters:



9
10
11
12
# File 'lib/solargraph/source_map/clip.rb', line 9

def initialize api_map, cursor
  @api_map = api_map
  @cursor = cursor
end

Instance Method Details

#completeCompletion

Returns:



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
# File 'lib/solargraph/source_map/clip.rb', line 23

def complete
  return package_completions(api_map.get_symbols) if cursor.chain.literal? && cursor.chain.links.last.word == '<Symbol>'
  return Completion.new([], cursor.range) if cursor.chain.literal? || cursor.comment?
  result = []
  if cursor.chain.constant? || cursor.start_of_constant?
    if cursor.chain.undefined?
      type = cursor.chain.base.infer(api_map, context_pin, locals)
    else
      full = cursor.chain.links.first.word
      if full.include?('::')
        type = ComplexType.parse(full.split('::')[0..-2].join('::'))
      elsif cursor.chain.links.length > 1
        type = ComplexType.parse(full)
      else
        type = ComplexType::UNDEFINED
      end
    end
    result.concat api_map.get_constants(type.undefined? ? '' : type.namespace, cursor.start_of_constant? ? '' : context_pin.context.namespace)
  else
    type = cursor.chain.base.infer(api_map, context_pin, locals)
    result.concat api_map.get_complex_type_methods(type, context_pin.context.namespace, cursor.chain.links.length == 1)
    if cursor.chain.links.length == 1
      if cursor.word.start_with?('@@')
        return package_completions(api_map.get_class_variable_pins(context_pin.context.namespace))
      elsif cursor.word.start_with?('@')
        return package_completions(api_map.get_instance_variable_pins(context_pin.context.namespace, context_pin.context.scope))
      elsif cursor.word.start_with?('$')
        return package_completions(api_map.get_global_variable_pins)
      end
      result.concat locals
      result.concat api_map.get_constants('', context_pin.context.namespace)
      result.concat api_map.get_methods(context_pin.context.namespace, scope: context_pin.context.scope, visibility: [:public, :private, :protected])
      result.concat api_map.get_methods('Kernel')
      result.concat ApiMap.keywords
    end
  end
  package_completions(result)
end

#defineArray<Pin::Base>

Returns:



15
16
17
18
19
20
# File 'lib/solargraph/source_map/clip.rb', line 15

def define
  return [] if cursor.comment? || cursor.chain.literal?
  result = cursor.chain.define(api_map, context_pin, locals)
  result.concat(source_map.pins.select{ |p| p.location.range.start.line == cursor.position.line }) if result.empty?
  result
end

#inferComplexType

Returns:



70
71
72
# File 'lib/solargraph/source_map/clip.rb', line 70

def infer
  cursor.chain.infer(api_map, context_pin, locals)
end

#localsArray<Solargraph::Pin::Base>

Get an array of all the locals that are visible from the cursors’s position. Locals can be local variables, method parameters, or block parameters. The array starts with the nearest local pin.

Returns:



79
80
81
82
83
# File 'lib/solargraph/source_map/clip.rb', line 79

def locals
  @locals ||= source_map.locals.select { |pin|
    pin.visible_from?(block, Position.new(cursor.position.line, (cursor.position.column.zero? ? 0 : cursor.position.column - 1)))
  }.reverse
end

#signifyArray<Pin::Base>

Returns:



63
64
65
66
67
# File 'lib/solargraph/source_map/clip.rb', line 63

def signify
  return [] unless cursor.argument?
  clip = Clip.new(api_map, cursor.recipient)
  clip.define.select{|pin| pin.kind == Pin::METHOD}
end