Class: Solargraph::Position
- Inherits:
-
Object
- Object
- Solargraph::Position
- Includes:
- Equality
- Defined in:
- lib/solargraph/position.rb
Overview
The zero-based line and column numbers of a position in a string.
Instance Attribute Summary collapse
- #character ⇒ Integer (also: #column) readonly
- #line ⇒ Integer readonly
Class Method Summary collapse
-
.from_offset(text, offset) ⇒ Position
Get a position for the specified text and offset.
-
.line_char_to_offset(text, line, character) ⇒ Integer
Get a numeric offset for the specified text and a position identified by its line and character.
-
.normalize(object) ⇒ Position
A helper method for generating positions from arrays of integers.
-
.to_offset(text, position) ⇒ Integer
Get a numeric offset for the specified text and position.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(line, character) ⇒ Position
constructor
A new instance of Position.
- #inspect ⇒ Object
-
#to_hash ⇒ Hash
Get a hash of the position.
Methods included from Equality
Constructor Details
#initialize(line, character) ⇒ Position
Returns a new instance of Position.
19 20 21 22 |
# File 'lib/solargraph/position.rb', line 19 def initialize line, character @line = line @character = character end |
Instance Attribute Details
#character ⇒ Integer (readonly) Also known as: column
13 14 15 |
# File 'lib/solargraph/position.rb', line 13 def character @character end |
#line ⇒ Integer (readonly)
10 11 12 |
# File 'lib/solargraph/position.rb', line 10 def line @line end |
Class Method Details
.from_offset(text, offset) ⇒ Position
Get a position for the specified text and offset.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/solargraph/position.rb', line 94 def self.from_offset text, offset raise InvalidOffsetError if offset > text.length cursor = 0 line = 0 character = offset newline_index = -1 # @sg-ignore Typechecker thinks `newline_index` inside of the assignment # can be nil while (newline_index = text.index("\n", newline_index + 1)) && newline_index < offset line += 1 # @sg-ignore `newline_index` is always an Integer inside the while block character = offset - newline_index - 1 end character = 0 if character.nil? && (cursor - offset).between?(0, 1) raise InvalidOffsetError if character.nil? # @sg-ignore flow sensitive typing needs to handle 'raise if' Position.new(line, character) end |
.line_char_to_offset(text, line, character) ⇒ Integer
Get a numeric offset for the specified text and a position identified by its line and character.
83 84 85 |
# File 'lib/solargraph/position.rb', line 83 def self.line_char_to_offset text, line, character to_offset(text, Position.new(line, character)) end |
.normalize(object) ⇒ Position
A helper method for generating positions from arrays of integers. The original parameter is returned if it is already a position.
122 123 124 125 126 |
# File 'lib/solargraph/position.rb', line 122 def self.normalize object return object if object.is_a?(Position) return Position.new(object[0], object[1]) if object.is_a?(Array) raise ArgumentError, "Unable to convert #{object.class} to Position" end |
.to_offset(text, position) ⇒ Integer
Get a numeric offset for the specified text and position.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/solargraph/position.rb', line 54 def self.to_offset text, position return 0 if text.empty? newline_index = -1 line = -1 last_line_index = 0 # @sg-ignore Typechecker thinks `newline_index` inside of the assignment # can be nil while (newline_index = text.index("\n", newline_index + 1)) && line <= position.line line += 1 break if line == position.line last_line_index = newline_index end last_line_index += 1 if position.line.positive? # @sg-ignore `last_line_index` is always an Integer because `newline_index` # is never nil inside the while block last_line_index + position.character end |
Instance Method Details
#<=>(other) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/solargraph/position.rb', line 25 def <=> other return nil unless other.is_a?(Position) if line == other.line character <=> other.character else line <=> other.line end end |
#==(other) ⇒ Object
128 129 130 131 |
# File 'lib/solargraph/position.rb', line 128 def == other return false unless other.is_a?(Position) line == other.line and character == other.character end |
#inspect ⇒ Object
45 46 47 |
# File 'lib/solargraph/position.rb', line 45 def inspect "#<#{self.class} #{line}, #{character}>" end |
#to_hash ⇒ Hash
Get a hash of the position. This representation is suitable for use in the language server protocol.
38 39 40 41 42 43 |
# File 'lib/solargraph/position.rb', line 38 def to_hash { line: line, character: character } end |