Sha256: e452e9275347ff27251ab859bde6fd06770389b1a2669f45ddafdcb520a9d7c3

Contents?: true

Size: 1.31 KB

Versions: 18

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require "byebug/helpers/file"
require "byebug/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

18 entries across 18 versions & 8 rubygems

Version Path
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/byebug-10.0.2/lib/byebug/source_file_formatter.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/byebug-10.0.2/lib/byebug/source_file_formatter.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/byebug-10.0.2/lib/byebug/source_file_formatter.rb
talon_one-2.0.0 vendor/bundle/ruby/2.3.0/gems/byebug-11.0.1/lib/byebug/source_file_formatter.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/byebug-11.0.1/lib/byebug/source_file_formatter.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/byebug-11.0.1/lib/byebug/source_file_formatter.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/byebug-11.0.1/lib/byebug/source_file_formatter.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/byebug-10.0.2/lib/byebug/source_file_formatter.rb
byebug-11.0.1 lib/byebug/source_file_formatter.rb
byebug-11.0.0 lib/byebug/source_file_formatter.rb
jets-0.5.5 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/source_file_formatter.rb
jets-0.5.4 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/source_file_formatter.rb
jets-0.5.3 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/source_file_formatter.rb
jets-0.5.2 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/source_file_formatter.rb
jets-0.5.1 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/source_file_formatter.rb
byebug-10.0.2 lib/byebug/source_file_formatter.rb
byebug-10.0.1 lib/byebug/source_file_formatter.rb
byebug-10.0.0 lib/byebug/source_file_formatter.rb