class RBS::Buffer

Attributes

content[R]
lines[R]
name[R]
ranges[R]

Public Class Methods

new(name:, content:) click to toggle source
# File rbs-2.1.0/lib/rbs/buffer.rb, line 8
def initialize(name:, content:)
  @name = name
  @content = content

  @lines = content.lines

  @ranges = []
  offset = 0
  lines.each do |line|
    size = line.size
    range = offset...(offset+size)
    ranges << range
    offset += size
  end
end

Public Instance Methods

inspect() click to toggle source
# File rbs-2.1.0/lib/rbs/buffer.rb, line 50
def inspect
  "#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{lines.size} lines,>"
end
last_position() click to toggle source
# File rbs-2.1.0/lib/rbs/buffer.rb, line 46
def last_position
  content.size
end
loc_to_pos(loc) click to toggle source
# File rbs-2.1.0/lib/rbs/buffer.rb, line 36
def loc_to_pos(loc)
  line, column = loc

  if range = ranges[line - 1]
    range.begin + column
  else
    last_position
  end
end
pos_to_loc(pos) click to toggle source
# File rbs-2.1.0/lib/rbs/buffer.rb, line 24
def pos_to_loc(pos)
  index = ranges.bsearch_index do |range|
    pos < range.end ? true : false
  end

  if index
    [index + 1, pos - ranges[index].begin]
  else
    [ranges.size + 1, 0]
  end
end