Class: Solargraph::Range
- Inherits:
-
Object
- Object
- Solargraph::Range
- 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
- #ending ⇒ Position readonly
- #start ⇒ Position readonly
Class Method Summary collapse
-
.from_expr(expr) ⇒ Range
Get a range from a Parser range, usually found in Parser::AST::Node#location#expression.
-
.from_node(node) ⇒ Range?
Get a range from a node.
-
.from_to(l1, c1, l2, c2) ⇒ Range
Create a range from a pair of lines and characters.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#contain?(position) ⇒ Boolean
True if the specified position is inside the range.
-
#include?(position) ⇒ Boolean
True if the range contains the specified position and the position does not precede it.
-
#initialize(start, ending) ⇒ Range
constructor
A new instance of Range.
- #inspect ⇒ Object
-
#to_hash ⇒ Hash{Symbol => Position}
Get a hash of the range.
Methods included from Equality
Constructor Details
#initialize(start, ending) ⇒ Range
Returns a new instance of Range.
17 18 19 20 |
# File 'lib/solargraph/range.rb', line 17 def initialize start, ending @start = start @ending = ending end |
Instance Attribute Details
#ending ⇒ Position (readonly)
13 14 15 |
# File 'lib/solargraph/range.rb', line 13 def ending @ending end |
#start ⇒ Position (readonly)
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.
94 95 96 |
# File 'lib/solargraph/range.rb', line 94 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.
84 85 86 87 |
# File 'lib/solargraph/range.rb', line 84 def self.from_node node return unless node&.loc&.expression from_expr(node.loc.expression) end |
Instance Method Details
#<=>(other) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/solargraph/range.rb', line 23 def <=> other return nil unless other.is_a?(Range) if start == other.start ending <=> other.ending else start <=> other.start end end |
#==(other) ⇒ Object
98 99 100 101 |
# File 'lib/solargraph/range.rb', line 98 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.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/solargraph/range.rb', line 47 def contain? position position = Position.normalize(position) # @sg-ignore flow sensitive typing should be able to handle redefinition return false if position.line < start.line || position.line > ending.line # @sg-ignore flow sensitive typing should be able to handle redefinition return false if position.line == start.line && position.character < start.character # @sg-ignore flow sensitive typing should be able to handle redefinition 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.
@sg-ignore flow sensitive typing should be able to handle redefinition
63 64 65 66 67 |
# File 'lib/solargraph/range.rb', line 63 def include? position position = Position.normalize(position) # @sg-ignore flow sensitive typing should be able to handle redefinition contain?(position) && !(position.line == start.line && position.character == start.character) end |
#inspect ⇒ Object
103 104 105 |
# File 'lib/solargraph/range.rb', line 103 def inspect "#<#{self.class} #{start.inspect} to #{ending.inspect}>" end |
#to_hash ⇒ Hash{Symbol => Position}
Get a hash of the range. This representation is suitable for use in the language server protocol.
36 37 38 39 40 41 |
# File 'lib/solargraph/range.rb', line 36 def to_hash { start: start.to_hash, end: ending.to_hash } end |