class Solargraph::Pin::LocalVariable

Attributes

presence[R]

@return [Range]

Public Class Methods

new(assignment: nil, presence: nil, **splat) click to toggle source
Calls superclass method Solargraph::Pin::BaseVariable::new
# File lib/solargraph/pin/local_variable.rb, line 9
def initialize assignment: nil, presence: nil, **splat
  super(**splat)
  @assignment = assignment
  @presence = presence
end

Public Instance Methods

try_merge!(pin) click to toggle source
# File lib/solargraph/pin/local_variable.rb, line 15
def try_merge! pin
  return false unless super
  @presence = pin.presence
  true
end
visible_at?(other_closure, other_loc) click to toggle source

@param other_loc [Location]

# File lib/solargraph/pin/local_variable.rb, line 35
def visible_at?(other_closure, other_loc)
  return true if location.filename == other_loc.filename &&
    presence.include?(other_loc.range.start) &&
    match_named_closure(other_closure, closure)
end
visible_from?(other, position) click to toggle source

@param other [Pin::Base] The caller's block @param position [Position, Array(Integer, Integer)] The caller's position @return [Boolean]

# File lib/solargraph/pin/local_variable.rb, line 24
def visible_from?(other, position)
  position = Position.normalize(position)
  other.filename == filename &&
    match_tags(other.full_context.tag, full_context.tag) &&
    (other == closure ||
      (closure.location.range.contain?(other.location.range.start) && closure.location.range.contain?(other.location.range.ending))
    ) &&
    presence.contain?(position)
end

Private Instance Methods

match_named_closure(needle, haystack) click to toggle source
# File lib/solargraph/pin/local_variable.rb, line 55
def match_named_closure needle, haystack
  return true if needle == haystack
  cursor = haystack
  until cursor.nil?
    return true if needle.path == cursor.path
    return false if cursor.path && !cursor.path.empty?
    cursor = cursor.closure
  end
  false
end
match_tags(tag1, tag2) click to toggle source

@param tag1 [String] @param tag2 [String] @return [Boolean]

# File lib/solargraph/pin/local_variable.rb, line 46
def match_tags tag1, tag2
  # @todo This is an unfortunate hack made necessary by a discrepancy in
  #   how tags indicate the root namespace. The long-term solution is to
  #   standardize it, whether it's `Class<>`, an empty string, or
  #   something else.
  tag1 == tag2 ||
    (['', 'Class<>'].include?(tag1) && ['', 'Class<>'].include?(tag2))
end