Sha256: 19fef2af0357d90c7198ef89616cf906048939ec4abd037123a38a43746ca354

Contents?: true

Size: 894 Bytes

Versions: 19

Compression:

Stored size: 894 Bytes

Contents

module Steep
  module AST
    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.bytesize
          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
          [1, pos]
        end
      end

      def loc_to_pos(loc)
        line, column = loc
        ranges[line - 1].begin + column
      end

      def source(range)
        content[range]
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
steep-0.14.0 lib/steep/ast/buffer.rb
steep-0.13.0 lib/steep/ast/buffer.rb
steep-0.12.0 lib/steep/ast/buffer.rb
steep-0.11.1 lib/steep/ast/buffer.rb
steep-0.11.0 lib/steep/ast/buffer.rb
steep-0.10.0 lib/steep/ast/buffer.rb
steep-0.9.0 lib/steep/ast/buffer.rb
steep-0.8.2 lib/steep/ast/buffer.rb
steep-0.8.1 lib/steep/ast/buffer.rb
steep-0.8.0 lib/steep/ast/buffer.rb
steep-0.7.1 lib/steep/ast/buffer.rb
steep-0.7.0 lib/steep/ast/buffer.rb
steep-0.6.0 lib/steep/ast/buffer.rb
steep-0.5.1 lib/steep/ast/buffer.rb
steep-0.5.0 lib/steep/ast/buffer.rb
steep-0.4.0 lib/steep/ast/buffer.rb
steep-0.3.0 lib/steep/ast/buffer.rb
steep-0.2.0 lib/steep/ast/buffer.rb
steep-0.1.0 lib/steep/ast/buffer.rb