Class: Solargraph::Source::SourceChainer

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

Overview

Information about a location in a source, including the location's word and signature, literal values at the base of signatures, and whether the location is inside a string or comment. ApiMaps use Fragments to provide results for completion and definition queries.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeMethods

const_from, drill_signature, get_node_end_position, get_node_start_position, infer_literal_node_type, pack_name, resolve_node_signature, returns_from, unpack_name

Constructor Details

#initialize(source, position) ⇒ SourceChainer

Returns a new instance of SourceChainer

Parameters:


25
26
27
28
29
# File 'lib/solargraph/source/source_chainer.rb', line 25

def initialize source, position
  @source = source
  @position = position
  @calculated_literal = false
end

Class Method Details

.chain(source, position) ⇒ Source::Chain

Parameters:

Returns:


17
18
19
20
# File 'lib/solargraph/source/source_chainer.rb', line 17

def chain source, position
  # raise "Not a source" unless source.is_a?(Source)
  new(source, position).chain
end

Instance Method Details

#chainSource::Chain

Returns:


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
# File 'lib/solargraph/source/source_chainer.rb', line 32

def chain
  return Chain.new([Chain::Literal.new('Symbol')]) if phrase.start_with?(':') && !phrase.start_with?('::')
  begin
    return Chain.new([]) if phrase.end_with?('..')
    if !source.repaired? && source.parsed? && source.synchronized?
      node = source.node_at(position.line, position.column)
    else
      node = nil
      node = source.node_at(fixed_position.line, fixed_position.column) unless source.error_ranges.any?{|r| r.nil? || r.include?(fixed_position)}
      # Exception for positions that chain literal nodes in unsynchronized sources
      node = nil unless source.synchronized? || !infer_literal_node_type(node).nil?
      node = Source.parse(fixed_phrase) if node.nil?
    end
  rescue Parser::SyntaxError
    return Chain.new([Chain::UNDEFINED_CALL])
  end
  return Chain.new([Chain::UNDEFINED_CALL]) if node.nil? || (node.type == :sym && !phrase.start_with?(':'))
  chain = NodeChainer.chain(node, source.filename)
  if source.repaired? || !source.parsed? || !source.synchronized?
    if end_of_phrase.strip == '.'
      chain.links.push Chain::UNDEFINED_CALL
    elsif end_of_phrase.strip == '::'
      chain.links.push Chain::UNDEFINED_CONSTANT
    end
  end
  chain
end