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.4.1 lib/cc/analyzer/source_buffer.rb
codeclimate-0.4.0 lib/cc/analyzer/source_buffer.rb
codeclimate-0.3.2 lib/cc/analyzer/source_buffer.rb
codeclimate-0.3.1 lib/cc/analyzer/source_buffer.rb
codeclimate-0.3.0 lib/cc/analyzer/source_buffer.rb
codeclimate-0.2.12 lib/cc/analyzer/source_buffer.rb
codeclimate-0.2.11 lib/cc/analyzer/source_buffer.rb
codeclimate-0.2.9 lib/cc/analyzer/source_buffer.rb
codeclimate-0.2.7 lib/cc/analyzer/source_buffer.rb
codeclimate-0.2.6 lib/cc/analyzer/source_buffer.rb
codeclimate-0.2.4 lib/cc/analyzer/source_buffer.rb
codeclimate-0.2.2 lib/cc/analyzer/source_buffer.rb
codeclimate-0.2.1 lib/cc/analyzer/source_buffer.rb
codeclimate-0.2 lib/cc/analyzer/source_buffer.rb
codeclimate-0.1.5 lib/cc/analyzer/source_buffer.rb
codeclimate-0.1.4 lib/cc/analyzer/source_buffer.rb
codeclimate-0.1.3 lib/cc/analyzer/source_buffer.rb
codeclimate-0.1.2 lib/cc/analyzer/source_buffer.rb
codeclimate-0.1.0 lib/cc/analyzer/source_buffer.rb
codeclimate-0.0.25 lib/cc/analyzer/source_buffer.rb