Class: Solargraph::Range

Inherits:
Object
  • Object
show all
Includes:
Equality
Defined in:
lib/solargraph/range.rb

Overview

A pair of Positions that compose a section of text in code.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Equality

#eql?, #freeze, #hash

Constructor Details

#initialize(start, ending) ⇒ Range

Returns a new instance of Range.

Parameters:



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

def initialize start, ending
  @start = start
  @ending = ending
end

Instance Attribute Details

#endingPosition (readonly)

Returns:



13
14
15
# File 'lib/solargraph/range.rb', line 13

def ending
  @ending
end

#startPosition (readonly)

Returns:



10
11
12
# File 'lib/solargraph/range.rb', line 10

def start
  @start
end

Class Method Details

.from_expr(expr) ⇒ Range

Get a range from a Parser range, usually found in Parser::AST::Node#location#expression.

Parameters:

  • expr (Parser::Source::Range)

Returns:



95
96
97
# File 'lib/solargraph/range.rb', line 95

def self.from_expr expr
  from_to(expr.line, expr.column, expr.last_line, expr.last_column)
end

.from_node(node) ⇒ Range?

Get a range from a node.

Parameters:

  • node (Parser::AST::Node)

Returns:



84
85
86
87
88
# File 'lib/solargraph/range.rb', line 84

def self.from_node node
  if node&.loc && node.loc.expression
    from_expr(node.loc.expression)
  end
end

.from_to(l1, c1, l2, c2) ⇒ Range

Create a range from a pair of lines and characters.

Parameters:

  • l1 (Integer)

    Starting line

  • c1 (Integer)

    Starting character

  • l2 (Integer)

    Ending line

  • c2 (Integer)

    Ending character

Returns:



76
77
78
# File 'lib/solargraph/range.rb', line 76

def self.from_to l1, c1, l2, c2
  Range.new(Position.new(l1, c1), Position.new(l2, c2))
end

Instance Method Details

#<=>(other) ⇒ Object

Parameters:

  • other (BasicObject)


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

def <=>(other)
  return nil unless other.is_a?(Range)
  if start == other.start
    ending <=> other.ending
  else
    start <=> other.start
  end
end

#==(other) ⇒ Object



99
100
101
102
# File 'lib/solargraph/range.rb', line 99

def == other
  return false unless other.is_a?(Range)
  start == other.start && ending == other.ending
end

#contain?(position) ⇒ Boolean

True if the specified position is inside the range.

Parameters:

  • position (Position, Array(Integer, Integer))

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/solargraph/range.rb', line 52

def contain? position
  position = Position.normalize(position)
  return false if position.line < start.line || position.line > ending.line
  return false if position.line == start.line && position.character < start.character
  return false if position.line == ending.line && position.character > ending.character
  true
end

#include?(position) ⇒ Boolean

True if the range contains the specified position and the position does not precede it.

Parameters:

  • position (Position, Array(Integer, Integer))

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/solargraph/range.rb', line 64

def include? position
  position = Position.normalize(position)
  contain?(position) && !(position.line == start.line && position.character == start.character)
end

#inspectObject



104
105
106
# File 'lib/solargraph/range.rb', line 104

def inspect
  "#<#{self.class} #{start.inspect} to #{ending.inspect}>"
end

#to_hashHash<Symbol, Position>

Get a hash of the range. This representation is suitable for use in the language server protocol.

Returns:



41
42
43
44
45
46
# File 'lib/solargraph/range.rb', line 41

def to_hash
  {
    start: start.to_hash,
    end: ending.to_hash
  }
end