Sha256: aea4611e404c56c724841964d126ccfd3c9854eec40d52eb378a6a79707f5fc4

Contents?: true

Size: 1.32 KB

Versions: 6770

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require_relative "helpers/file"
require_relative "setting"

module Byebug
  #
  # Formats specific line ranges in a source file
  #
  class SourceFileFormatter
    include Helpers::FileHelper

    attr_reader :file, :annotator

    def initialize(file, annotator)
      @file = file
      @annotator = annotator
    end

    def lines(min, max)
      File.foreach(file).with_index.map do |line, lineno|
        next unless (min..max).cover?(lineno + 1)

        format(
          "%<annotation>s %<lineno>#{max.to_s.size}d: %<source>s",
          annotation: annotator.call(lineno + 1),
          lineno: lineno + 1,
          source: line
        )
      end
    end

    def lines_around(center)
      lines(*range_around(center))
    end

    def range_around(center)
      range_from(center - size / 2)
    end

    def range_from(min)
      first = amend_initial(min)

      [first, first + size - 1]
    end

    def amend_initial(line)
      amend(line, max_initial_line)
    end

    def amend_final(line)
      amend(line, max_line)
    end

    def max_initial_line
      max_line - size + 1
    end

    def max_line
      @max_line ||= n_lines(file)
    end

    def size
      [Setting[:listsize], max_line].min
    end

    def amend(line, ceiling)
      [ceiling, [1, line].max].min
    end
  end
end

Version data entries

6,770 entries across 6,767 versions & 30 rubygems

Version Path
ory-client-0.0.1.alpha37 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
ory-client-0.0.1.alpha36 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
ory-kratos-client-0.8.2.alpha1 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
ory-client-0.0.1.alpha31 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
primary_connect_proto-0.4.0 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
primary_connect_proto-0.3.0 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
primary_connect_proto-0.2.0 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
primary_connect_proto-0.1.0 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
ory-client-0.0.1.alpha30 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
ory-client-0.0.1.alpha29 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
primary_connect_proto-0.0.6 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
ory-client-0.0.1.alpha28 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
primary_connect_proto-0.0.5 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
ory-client-0.0.1.alpha27 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
ory-client-0.0.1.alpha24 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
primary_connect_proto-0.0.4 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
primary_connect_proto-0.0.3 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb
ory-client-0.0.1.alpha23 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/source_file_formatter.rb