Sha256: a9bb71ba0c3f439e07f7bbfd5ae82be041ce344124671e08ccc5b5ea32868a15

Contents?: true

Size: 1022 Bytes

Versions: 16

Compression:

Stored size: 1022 Bytes

Contents

module Goodcheck
  class Buffer
    attr_reader :path
    attr_reader :content

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

    def line_ranges
      unless @line_ranges
        @line_ranges = []

        start_position = 0

        content.split(/\n/, -1).each do |line|
          range = start_position...(start_position + line.bytesize)
          @line_ranges << range
          start_position = range.end + 1
        end
      end

      @line_ranges
    end

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

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

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

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
goodcheck-2.4.3 lib/goodcheck/buffer.rb
goodcheck-2.4.2 lib/goodcheck/buffer.rb
goodcheck-2.4.1 lib/goodcheck/buffer.rb
goodcheck-2.4.0 lib/goodcheck/buffer.rb
goodcheck-2.3.2 lib/goodcheck/buffer.rb
goodcheck-2.3.1 lib/goodcheck/buffer.rb
goodcheck-2.3.0 lib/goodcheck/buffer.rb
goodcheck-2.2.0 lib/goodcheck/buffer.rb
goodcheck-2.1.2 lib/goodcheck/buffer.rb
goodcheck-2.1.1 lib/goodcheck/buffer.rb
goodcheck-2.1.0 lib/goodcheck/buffer.rb
goodcheck-1.7.1 lib/goodcheck/buffer.rb
goodcheck-1.7.0 lib/goodcheck/buffer.rb
goodcheck-1.6.0 lib/goodcheck/buffer.rb
goodcheck-1.5.1 lib/goodcheck/buffer.rb
goodcheck-1.5.0 lib/goodcheck/buffer.rb