Sha256: 4f10533481cf9ecb1adf64d17eb7ea9f45d5ab998f3cae9da9b9d491fc86e48e

Contents?: true

Size: 1005 Bytes

Versions: 9

Compression:

Stored size: 1005 Bytes

Contents

module Goodcheck
  class Buffer
    attr_reader :path
    attr_reader :content

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

    def line_starts
      unless @line_starts
        @line_starts = []

        start_position = 0

        content.lines.each do |line|
          range = start_position..(start_position + line.bytesize)
          @line_starts << range
          start_position = range.end
        end
      end

      @line_starts
    end

    def location_for_position(position)
      line_index = line_starts.bsearch_index do |range|
        position < range.end
      end

      if line_index
        [line_index + 1, position - line_starts[line_index].begin]
      end
    end

    def line(line_number)
      content.lines[line_number-1]
    end

    def position_for_location(line, column)
      if (range = line_starts[line-1])
        pos = range.begin + column
        if pos < range.end
          pos
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
goodcheck-1.4.1 lib/goodcheck/buffer.rb
goodcheck-1.4.0 lib/goodcheck/buffer.rb
goodcheck-1.3.1 lib/goodcheck/buffer.rb
goodcheck-1.2.0 lib/goodcheck/buffer.rb
goodcheck-1.1.0 lib/goodcheck/buffer.rb
goodcheck-1.0.0 lib/goodcheck/buffer.rb
goodcheck-0.3.0 lib/goodcheck/buffer.rb
goodcheck-0.2.0 lib/goodcheck/buffer.rb
goodcheck-0.1.0 lib/goodcheck/buffer.rb