Sha256: e178d9619393effba704e74ef5021a19ff152c076423c54e9d178df8e9331211

Contents?: true

Size: 875 Bytes

Versions: 14

Compression:

Stored size: 875 Bytes

Contents

module RBS
  class Buffer
    attr_reader :name
    attr_reader :content
    attr_reader :lines
    attr_reader :ranges

    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

    def pos_to_loc(pos)
      index = ranges.bsearch_index do |range|
        pos < range.end
      end

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

    def loc_to_pos(loc)
      line, column = loc

      if range = ranges[line - 1]
        range.begin + column
      else
        last_position
      end
    end

    def last_position
      content.size
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
rbs-0.11.0 lib/rbs/buffer.rb
rbs-0.10.0 lib/rbs/buffer.rb
rbs-0.9.1 lib/rbs/buffer.rb
rbs-0.9.0 lib/rbs/buffer.rb
rbs-0.8.0 lib/rbs/buffer.rb
rbs-0.7.0 lib/rbs/buffer.rb
rbs-0.6.0 lib/rbs/buffer.rb
rbs-0.5.0 lib/rbs/buffer.rb
rbs-0.4.0 lib/rbs/buffer.rb
rbs-0.3.1 lib/rbs/buffer.rb
rbs-0.3.0 lib/rbs/buffer.rb
rbs-0.2.0 lib/rbs/buffer.rb
steep-0.16.0 vendor/ruby-signature/lib/rbs/buffer.rb
steep-0.15.0 vendor/ruby-signature/lib/rbs/buffer.rb