class Solargraph::Position

The zero-based line and column numbers of a position in a string.

Attributes

character[R]

@return [Integer]

column[R]

@return [Integer]

line[R]

@return [Integer]

Public Class Methods

from_offset(text, offset) click to toggle source

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
line_char_to_offset(text, line, character) click to toggle source

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
new(line, character) click to toggle source

@param line [Integer] @param character [Integer]

# File lib/solargraph/position.rb, line 16
def initialize line, character
  @line = line
  @character = character
end
normalize(object) click to toggle source

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
to_offset(text, position) click to toggle source

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

==(other) click to toggle source
# File lib/solargraph/position.rb, line 94
def == other
  return false unless other.is_a?(Position)
  line == other.line and character == other.character
end
inspect() click to toggle source
# File lib/solargraph/position.rb, line 32
def inspect
  "#<#{self.class} #{line}, #{character}>"
end
to_hash() click to toggle source

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