Sha256: 126c29ef9cd2b42413e992947cdd0346cf05531d559008508c438fedbd91354f

Contents?: true

Size: 1.8 KB

Versions: 161

Compression:

Stored size: 1.8 KB

Contents

class Pry
  class Code

    # Represents a range of lines in a code listing.
    #
    # @api private
    class CodeRange

      # @param [Integer] start_line
      # @param [Integer?] end_line
      def initialize(start_line, end_line = nil)
        @start_line = start_line
        @end_line   = end_line
        force_set_end_line
      end

      # @param [Array<LOC>] lines
      # @return [Range]
      def indices_range(lines)
        Range.new(*indices(lines))
      end

      private

      def start_line; @start_line; end
      def end_line; @end_line; end

      # If `end_line` is equal to `nil`, then calculate it from the first
      # parameter, `start_line`. Otherwise, leave it as it is.
      # @return [void]
      def force_set_end_line
        if start_line.is_a?(Range)
          set_end_line_from_range
        else
          @end_line ||= start_line
        end
      end

      # Finds indices of `start_line` and `end_line` in the given Array of
      # +lines+.
      #
      # @param [Array<LOC>] lines
      # @return [Array<Integer>]
      def indices(lines)
        [find_start_index(lines), find_end_index(lines)]
      end

      # @return [Integer]
      def find_start_index(lines)
        return start_line if start_line < 0
        lines.index { |loc| loc.lineno >= start_line } || lines.length
      end

      # @return [Integer]
      def find_end_index(lines)
        return end_line if end_line < 0
        (lines.index { |loc| loc.lineno > end_line } || 0) - 1
      end

      # For example, if the range is 4..10, then `start_line` would be equal to
      # 4 and `end_line` to 10.
      # @return [void]
      def set_end_line_from_range
        @end_line = start_line.last
        @end_line -= 1 if start_line.exclude_end?
        @start_line = start_line.first
      end
    end

  end
end

Version data entries

161 entries across 154 versions & 35 rubygems

Version Path
pry-0.10.4 lib/pry/code/code_range.rb
pry-0.10.4-java lib/pry/code/code_range.rb
pry-0.11.0.pre lib/pry/code/code_range.rb
pry-0.11.0.pre-java lib/pry/code/code_range.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/pry-0.10.2-java/lib/pry/code/code_range.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/pry-0.10.1/lib/pry/code/code_range.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/pry-0.10.1/lib/pry/code/code_range.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/pry-0.10.1/lib/pry/code/code_range.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/pry-0.10.3-java/lib/pry/code/code_range.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/pry-0.10.3-java/lib/pry/code/code_range.rb
logstash-codec-json-2.0.3 vendor/gems/pry-0.10.3-java/lib/pry/code/code_range.rb
logstash-input-beats-0.9.2 vendor/jruby/1.9/gems/pry-0.10.3-java/lib/pry/code/code_range.rb
logstash-input-beats-0.9.1 vendor/jruby/1.9/gems/pry-0.10.3-java/lib/pry/code/code_range.rb
mastermind_adeybee-0.1.4 vendor/bundle/ruby/2.2.0/gems/pry-0.10.2/lib/pry/code/code_range.rb
pry-0.10.3 lib/pry/code/code_range.rb
pry-0.10.3-java lib/pry/code/code_range.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/pry-0.10.2/lib/pry/code/code_range.rb
mastermind_adeybee-0.1.3 vendor/bundle/ruby/2.2.0/gems/pry-0.10.2/lib/pry/code/code_range.rb
mastermind_adeybee-0.1.2 vendor/bundle/ruby/2.2.0/gems/pry-0.10.2/lib/pry/code/code_range.rb
mastermind_adeybee-0.1.1 vendor/bundle/ruby/2.2.0/gems/pry-0.10.2/lib/pry/code/code_range.rb