class Solargraph::Source::Chain::Constant

Public Class Methods

new(word) click to toggle source
# File lib/solargraph/source/chain/constant.rb, line 6
def initialize word
  @word = word
end

Public Instance Methods

resolve(api_map, name_pin, locals) click to toggle source
# File lib/solargraph/source/chain/constant.rb, line 10
def resolve api_map, name_pin, locals
  return [Pin::ROOT_PIN] if word.empty?
  if word.start_with?('::')
    base = word[2..-1]
    gates = ['']
  else
    base = word
    gates = crawl_gates(name_pin)
  end
  parts = base.split('::')
  gates.each do |gate|
    type = deep_constant_type(gate, api_map)
    # Use deep inference to resolve root
    parts[0..-2].each do |sym|
      pins = api_map.get_constants('', type.namespace).select{ |pin| pin.name == sym }
      type = first_pin_type(pins, api_map)
      break if type.undefined?
    end
    next if type.undefined?
    result = api_map.get_constants('', type.namespace).select { |pin| pin.name == parts.last }
    return result unless result.empty?
  end
  []
end

Private Instance Methods

crawl_gates(pin) click to toggle source
# File lib/solargraph/source/chain/constant.rb, line 37
def crawl_gates pin
  clos = pin
  until clos.nil?
    if clos.is_a?(Pin::Namespace)
      gates = clos.gates
      gates.push('') if gates.empty?
      return gates
    end
    clos = clos.closure
  end
  ['']
end
deep_constant_type(gate, api_map) click to toggle source
# File lib/solargraph/source/chain/constant.rb, line 61
def deep_constant_type(gate, api_map)
  type = ComplexType::ROOT
  return type if gate == ''
  gate.split('::').each do |word|
    pins = api_map.get_constants('', type.namespace).select { |pin| pin.name == word }
    type = first_pin_type(pins, api_map)
    break if type.undefined?
  end
  type
end
first_pin_type(pins, api_map) click to toggle source
# File lib/solargraph/source/chain/constant.rb, line 50
def first_pin_type(pins, api_map)
  type = ComplexType::UNDEFINED
  pins.each do |pin|
    type = pin.typify(api_map)
    break if type.defined?
    type = pin.probe(api_map)
    break if type.defined?
  end
  type
end