Class: Solargraph::Range
- Inherits:
-
Object
- Object
- Solargraph::Range
- Defined in:
- lib/solargraph/range.rb
Overview
A pair of positions that compose a section of text.
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
-
#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.
Constructor Details
#initialize(start, ending) ⇒ Range
Returns a new instance of Range.
15 16 17 18 |
# File 'lib/solargraph/range.rb', line 15 def initialize start, ending @start = start @ending = ending end |
Instance Attribute Details
#ending ⇒ Position (readonly)
11 12 13 |
# File 'lib/solargraph/range.rb', line 11 def ending @ending end |
Class Method Details
.from_expr(expr) ⇒ Range
Get a range from a Parser range, usually found in Parser::AST::Node#location#expression.
82 83 84 |
# File 'lib/solargraph/range.rb', line 82 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.
67 68 69 70 71 72 73 74 75 |
# File 'lib/solargraph/range.rb', line 67 def self.from_node node if defined?(RubyVM::AbstractSyntaxTree::Node) if node.is_a?(RubyVM::AbstractSyntaxTree::Node) Solargraph::Range.from_to(node.first_lineno - 1, node.first_column, node.last_lineno - 1, node.last_column) end elsif node.loc && node.loc.expression from_expr(node.loc.expression) end end |
Instance Method Details
#==(other) ⇒ Object
86 87 88 89 |
# File 'lib/solargraph/range.rb', line 86 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.
35 36 37 38 39 40 41 |
# File 'lib/solargraph/range.rb', line 35 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.
47 48 49 50 |
# File 'lib/solargraph/range.rb', line 47 def include? position position = Position.normalize(position) contain?(position) && !(position.line == start.line && position.character == start.character) end |
#inspect ⇒ Object
91 92 93 |
# File 'lib/solargraph/range.rb', line 91 def inspect "#<#{self.class} #{start.inspect} to #{ending.inspect}>" end |