class Solargraph::Position
The zero-based line and column numbers of a position in a string.
Attributes
@return [Integer]
@return [Integer]
@return [Integer]
Public Class Methods
Get a position for the specified text and offset.
@param text [String] @param offset [Integer] @return [Position]
# File lib/solargraph/position.rb, line 62 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
Get a numeric offset for the specified text and a position identified by its line and character.
@param text [String] @param line [Integer] @param character [Integer] @return [Integer]
# File lib/solargraph/position.rb, line 53 def self.line_char_to_offset text, line, character to_offset(text, Position.new(line, character)) end
@param line [Integer] @param character [Integer]
# File lib/solargraph/position.rb, line 16 def initialize line, character @line = line @character = character end
A helper method for generating positions from arrays of integers. The original parameter is returned if it is already a position.
@raise [ArgumentError] if the object cannot be converted to a position.
@param object [Position, Array(Integer, Integer)] @return [Position]
# File lib/solargraph/position.rb, line 88 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
Get a numeric offset for the specified text and position.
@param text [String] @param position [Position] @return [Integer]
# File lib/solargraph/position.rb, line 41 def self.to_offset text, position return 0 if text.empty? text.lines[0...position.line].sum(&:length) + position.character end
Public Instance Methods
# File lib/solargraph/position.rb, line 94 def == other return false unless other.is_a?(Position) line == other.line and character == other.character end
# File lib/solargraph/position.rb, line 32 def inspect "#<#{self.class} #{line}, #{character}>" end
Get a hash of the position. This representation is suitable for use in the language server protocol.
@return [Hash]
# File lib/solargraph/position.rb, line 25 def to_hash { line: line, character: character } end