Class: Solargraph::Location

Inherits:
Object
  • Object
show all
Includes:
Comparable, Equality
Defined in:
lib/solargraph/location.rb

Overview

A pointer to a section of source text identified by its filename and Range.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Equality

#eql?, #freeze, #hash

Constructor Details

#initialize(filename, range) ⇒ Location

Returns a new instance of Location.

Parameters:



19
20
21
22
23
24
# File 'lib/solargraph/location.rb', line 19

def initialize filename, range
  raise 'Use nil to represent no-file' if filename&.empty?

  @filename = filename
  @range = range
end

Instance Attribute Details

#filenameString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/solargraph/location.rb', line 12

def filename
  @filename
end

#rangeSolargraph::Range (readonly)

Returns:



15
16
17
# File 'lib/solargraph/location.rb', line 15

def range
  @range
end

Class Method Details

.from_node(node) ⇒ Location?

Parameters:

  • node (Parser::AST::Node, nil)

Returns:



59
60
61
62
63
64
65
66
67
# File 'lib/solargraph/location.rb', line 59

def self.from_node node
  return nil if node.nil? || node.loc.nil?
  filename = node.loc.expression.source_buffer.name
  # @sg-ignore flow sensitive typing needs to create separate ranges for postfix if
  filename = nil if filename.empty?
  range = Range.from_node(node)
  # @sg-ignore Need to add nil check here
  new(filename, range)
end

Instance Method Details

#<=>(other) ⇒ Object

Parameters:

  • other (self)


27
28
29
30
31
32
33
34
# File 'lib/solargraph/location.rb', line 27

def <=> other
  return nil unless other.is_a?(Location)
  if filename == other.filename
    range <=> other.range
  else
    filename <=> other.filename
  end
end

#==(other) ⇒ Object

Parameters:

  • other (BasicObject)


70
71
72
73
# File 'lib/solargraph/location.rb', line 70

def == other
  return false unless other.is_a?(Location)
  filename == other.filename and range == other.range
end

#contain?(location) ⇒ Boolean

Parameters:

  • location (self)

Returns:

  • (Boolean)


41
42
43
# File 'lib/solargraph/location.rb', line 41

def contain? location
  range.contain?(location.range.start) && range.contain?(location.range.ending) && filename == location.filename
end

#inspectObject



75
76
77
# File 'lib/solargraph/location.rb', line 75

def inspect
  "#<#{self.class} #{filename}, #{range.inspect}>"
end

#rbs?Boolean

Returns:

  • (Boolean)


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

def rbs?
  filename.end_with?('.rbs')
end

#to_hashHash

Returns:

  • (Hash)


50
51
52
53
54
55
# File 'lib/solargraph/location.rb', line 50

def to_hash
  {
    filename: filename,
    range: range.to_hash
  }
end

#to_sObject



45
46
47
# File 'lib/solargraph/location.rb', line 45

def to_s
  inspect
end