Sha256: 07db596c4b7f6c0975752f1c39971d7d730bb8bf012a9f31d47ab045fdd4ca1c

Contents?: true

Size: 1.13 KB

Versions: 46

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module RBS
  class Buffer
    attr_reader :name
    attr_reader :content

    def initialize(name:, content:)
      @name = name
      @content = content
    end

    def lines
      @lines ||= content.lines
    end

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

    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

    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

    def inspect
      "#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{lines.size} lines,>"
    end
  end
end

Version data entries

46 entries across 46 versions & 2 rubygems

Version Path
rbs-2.8.0 lib/rbs/buffer.rb
rbs-2.8.0.pre.1 lib/rbs/buffer.rb
rbs-2.7.0 lib/rbs/buffer.rb
rbs-2.7.0.pre.3 lib/rbs/buffer.rb
rbs-2.7.0.pre.2 lib/rbs/buffer.rb
rbs-2.7.0.pre.1 lib/rbs/buffer.rb