Sha256: f2f803f04fd3bf179f1392bbb6041090b8e9e5b9e1734e80a2d503565deeaf9e

Contents?: true

Size: 1.81 KB

Versions: 6842

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

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

      attr_reader :start_line

      attr_reader :end_line

      # 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

6,842 entries across 6,837 versions & 33 rubygems

Version Path
ory-client-0.0.1.alpha31 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
primary_connect_proto-0.4.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
primary_connect_proto-0.3.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
primary_connect_proto-0.2.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
primary_connect_proto-0.1.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-client-0.0.1.alpha30 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-client-0.0.1.alpha29 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
primary_connect_proto-0.0.6 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-client-0.0.1.alpha28 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
primary_connect_proto-0.0.5 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-client-0.0.1.alpha27 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-client-0.0.1.alpha24 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
primary_connect_proto-0.0.4 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
primary_connect_proto-0.0.3 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-client-0.0.1.alpha23 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-kratos-client-0.8.0.alpha2 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-client-0.0.1.alpha21 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-keto-client-0.7.0.alpha1 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-keto-client-0.7.0.alpha0 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb
ory-kratos-client-0.7.6.alpha7 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/code/code_range.rb