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.9.7 lib/cc/analyzer/source_buffer.rb
codeclimate-0.9.6 lib/cc/analyzer/source_buffer.rb
codeclimate-0.9.5 lib/cc/analyzer/source_buffer.rb
codeclimate-0.9.4 lib/cc/analyzer/source_buffer.rb
codeclimate-0.9.3 lib/cc/analyzer/source_buffer.rb
codeclimate-0.9.2 lib/cc/analyzer/source_buffer.rb
codeclimate-0.9.1 lib/cc/analyzer/source_buffer.rb
codeclimate-0.9.0 lib/cc/analyzer/source_buffer.rb
codeclimate-0.8.1 lib/cc/analyzer/source_buffer.rb
codeclimate-0.8.0 lib/cc/analyzer/source_buffer.rb
codeclimate-0.7.2 lib/cc/analyzer/source_buffer.rb
codeclimate-0.7.0 lib/cc/analyzer/source_buffer.rb
codeclimate-0.6.4 lib/cc/analyzer/source_buffer.rb
codeclimate-0.6.3 lib/cc/analyzer/source_buffer.rb
codeclimate-0.6.2 lib/cc/analyzer/source_buffer.rb
codeclimate-0.6.1 lib/cc/analyzer/source_buffer.rb
codeclimate-0.6.0 lib/cc/analyzer/source_buffer.rb
codeclimate-0.5.0 lib/cc/analyzer/source_buffer.rb
codeclimate-0.4.4 lib/cc/analyzer/source_buffer.rb
codeclimate-0.4.3 lib/cc/analyzer/source_buffer.rb