class RubyLsp::Document::Scanner
def find_char_position(position)
def find_char_position(position) # Find the character index for the beginning of the requested line until @current_line == position[:line] @pos += 1 until LINE_BREAK == @source[@pos] @pos += 1 @current_line += 1 end # The final position is the beginning of the line plus the requested column. If the encoding is UTF-16, we also # need to adjust for surrogate pairs requested_position = @pos + position[:character] if @encoding == Constant::PositionEncodingKind::UTF16 requested_position -= utf_16_character_position_correction(@pos, requested_position) end requested_position end
def initialize(source, encoding)
def initialize(source, encoding) @current_line = T.let(0, Integer) @pos = T.let(0, Integer) @source = T.let(source.codepoints, T::Array[Integer]) @encoding = encoding end
def utf_16_character_position_correction(current_position, requested_position)
def utf_16_character_position_correction(current_position, requested_position) utf16_unicode_correction = 0 until current_position == requested_position codepoint = @source[current_position] utf16_unicode_correction += 1 if codepoint && codepoint > SURROGATE_PAIR_START current_position += 1 end utf16_unicode_correction end