Sha256: 8fe6e466fe6f7ec908552ffd08f81252e6bc806a042eb3b33eae02ca6d84001d

Contents?: true

Size: 989 Bytes

Versions: 57

Compression:

Stored size: 989 Bytes

Contents

# Adapted from https://github.com/whitequark/parser/blob/master/lib/parser/source/buffer.rb
module CC
  module Analyzer
    class SourceBuffer
      attr_reader :name
      attr_reader :source

      def initialize(name, source)
        @name = name
        @source = source
      end

      def decompose_position(position)
        line_no, line_begin = line_for(position)

        [ 1 + line_no, position - line_begin ]
      end

      def line_count
        @source.lines.count
      end

    private

      def line_for(position)
        line_begins.bsearch do |line, line_begin|
          line_begin <= position
        end
      end

      def line_begins
        unless @line_begins
          @line_begins, index = [ [ 0, 0 ] ], 1

          @source.each_char do |char|
            if char == "\n"
              @line_begins.unshift [ @line_begins.length, index ]
            end

            index += 1
          end
        end

        @line_begins
      end

    end
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
codeclimate-0.0.24 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.23 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.22 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.21 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.18 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.17 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.16 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.15 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.14 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.13 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.11 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.10 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.9 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.7 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.6 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.5 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.1 lib/cc/analyzer/source_buffer.rb