Class: Solargraph::Position
- Inherits:
-
Object
- Object
- Solargraph::Position
- 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
-
#initialize(line, character) ⇒ Position
constructor
A new instance of Position.
- #inspect ⇒ Object
-
#to_hash ⇒ Hash
Get a hash of the position.
Constructor Details
#initialize(line, character) ⇒ Position
Returns a new instance of Position.
17 18 19 20 |
# File 'lib/solargraph/position.rb', line 17 def initialize line, character @line = line @character = character end |
Instance Attribute Details
#character ⇒ Integer (readonly) Also known as: column
11 12 13 |
# File 'lib/solargraph/position.rb', line 11 def character @character end |
#line ⇒ Integer (readonly)
8 9 10 |
# File 'lib/solargraph/position.rb', line 8 def line @line end |
Class Method Details
.from_offset(text, offset) ⇒ Position
Get a position for the specified text and offset.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/solargraph/position.rb', line 63 def self.from_offset text, offset cursor = 0 line = 0 character = nil text.lines.each do |l| line_length = l.length char_length = l.chomp.length if cursor + char_length >= offset character = offset - cursor break end cursor += line_length line += 1 end character = 0 if character.nil? and (cursor - offset).between?(0, 1) raise InvalidOffsetError if character.nil? 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.
54 55 56 |
# File 'lib/solargraph/position.rb', line 54 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.
89 90 91 92 93 |
# File 'lib/solargraph/position.rb', line 89 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.
42 43 44 45 |
# File 'lib/solargraph/position.rb', line 42 def self.to_offset text, position return 0 if text.empty? text.lines[0...position.line].sum(&:length) + position.character end |
Instance Method Details
#==(other) ⇒ Object
95 96 97 98 |
# File 'lib/solargraph/position.rb', line 95 def == other return false unless other.is_a?(Position) line == other.line and character == other.character end |
#inspect ⇒ Object
33 34 35 |
# File 'lib/solargraph/position.rb', line 33 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.
26 27 28 29 30 31 |
# File 'lib/solargraph/position.rb', line 26 def to_hash { line: line, character: character } end |