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:



11
12
13
14
15
16
17
18
19
# File 'lib/solargraph/source_map/clip.rb', line 11

def initialize api_map, cursor
  @api_map = api_map
  @cursor = cursor
  closure_pin = closure
  # @sg-ignore Need to add nil check here
  if closure_pin.is_a?(Pin::Block) && !Solargraph::Range.from_node(closure_pin.receiver).contain?(cursor.range.start)
    closure_pin.rebind(api_map)
  end
end

Instance Method Details

#completeCompletion

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/solargraph/source_map/clip.rb', line 41

def complete
  return package_completions([]) if !source_map.source.parsed? || cursor.string?
  if cursor.chain.literal? && cursor.chain.links.last.word == '<Symbol>'
    return package_completions(api_map.get_symbols)
  end
  return Completion.new([], cursor.range) if cursor.chain.literal?
  if cursor.comment?
    tag_complete
  else
    code_complete
  end
end

#defineArray<Pin::Base>

Returns Relevant pins for infering the type of the Cursor’s position.

Returns:

  • (Array<Pin::Base>)

    Relevant pins for infering the type of the Cursor’s position



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/solargraph/source_map/clip.rb', line 22

def define
  return [] if cursor.comment? || cursor.chain.literal?
  result = cursor.chain.define(api_map, closure, locals)
  result.concat file_global_methods
  if result.empty?
    result.concat((source_map.pins + source_map.locals).select do |p|
      # @sg-ignore Need to add nil check here
      p.name == cursor.word && p.location.range.contain?(cursor.position)
    end)
  end
  result
end

#gates::Array<String>

Returns:

  • (::Array<String>)


84
85
86
# File 'lib/solargraph/source_map/clip.rb', line 84

def gates
  closure.gates
end

#inferComplexType

Returns:



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/solargraph/source_map/clip.rb', line 62

def infer
  result = cursor.chain.infer(api_map, closure, locals)
  if result.tag == 'Class'
    # HACK: Exception to return BasicObject from Class#new
    dfn = cursor.chain.define(api_map, closure, locals).first
    return ComplexType.try_parse('::BasicObject') if dfn && dfn.path == 'Class#new'
  end
  # should receive result with selfs resolved from infer()
  Solargraph.assert_or_log(:clip_infer_self, 'Received selfy inference') if result.selfy?
  result
end

#locals::Array<Solargraph::Pin::LocalVariable>

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

def locals
  @locals ||= source_map.locals_at(location)
end

#signifyArray<Pin::Method>

Returns:



55
56
57
58
59
# File 'lib/solargraph/source_map/clip.rb', line 55

def signify
  return [] unless cursor.argument?
  chain = Parser.chain(cursor.recipient_node, cursor.filename)
  chain.define(api_map, context_pin, locals).select { |pin| pin.is_a?(Pin::Method) }
end

#translate(phrase) ⇒ Array<Solargraph::Pin::Base>

Parameters:

  • phrase (String)

Returns:



90
91
92
93
# File 'lib/solargraph/source_map/clip.rb', line 90

def translate phrase
  chain = Parser.chain(Parser.parse(phrase, cursor.filename, cursor.position.line))
  chain.define(api_map, closure, locals)
end

#typesArray<Pin::Base>

Returns:



36
37
38
# File 'lib/solargraph/source_map/clip.rb', line 36

def types
  infer.namespaces.map { |namespace| api_map.get_path_pins(namespace) }.flatten
end