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
Source
# File lib/solargraph/position.rb, line 96 def self.from_offset text, offset raise InvalidOffsetError if offset > text.length cursor = 0 line = 0 character = offset newline_index = -1 while (newline_index = text.index("\n", newline_index + 1)) && newline_index < offset line += 1 character = offset - newline_index - 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 position for the specified text and offset.
@raise [InvalidOffsetError] if the offset is outside the text range
@param text [String] @param offset [Integer] @return [Position]
Source
# File lib/solargraph/position.rb, line 85 def self.line_char_to_offset text, line, character to_offset(text, 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]
Source
# File lib/solargraph/position.rb, line 19 def initialize line, character @line = line @character = character end
@param line [Integer] @param character [Integer]
Source
# File lib/solargraph/position.rb, line 120 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
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]
Source
# File lib/solargraph/position.rb, line 59 def self.to_offset text, position return 0 if text.empty? newline_index = -1 line = -1 last_line_index = 0 while (newline_index = text.index("\n", newline_index + 1)) && line <= position.line line += 1 break if line == position.line line_length = newline_index - last_line_index last_line_index = newline_index end last_line_index += 1 if position.line > 0 last_line_index + position.character end
Get a numeric offset for the specified text and position.
@param text [String] @param position [Position] @return [Integer]
Public Instance Methods
Source
# File lib/solargraph/position.rb, line 30 def <=>(other) return nil unless other.is_a?(Position) if line == other.line character <=> other.character else line <=> other.line end end
@param other [Position]
Source
# File lib/solargraph/position.rb, line 126 def == other return false unless other.is_a?(Position) # @sg-ignore https://github.com/castwide/solargraph/pull/1114 line == other.line and character == other.character end
Source
# File lib/solargraph/position.rb, line 50 def inspect "#<#{self.class} #{line}, #{character}>" end
Source
# File lib/solargraph/position.rb, line 43 def to_hash { line: line, character: character } end
Get a hash of the position. This representation is suitable for use in the language server protocol.
@return [Hash]
Protected Instance Methods
Source
# File lib/solargraph/position.rb, line 25 def equality_fields [line, character] end
@sg-ignore Fix “Not enough arguments to Module#protected”