Class: Byebug::SourceFileFormatter

Inherits:
Object
  • Object
show all
Includes:
Helpers::FileHelper
Defined in:
lib/byebug/source_file_formatter.rb

Overview

Formats specific line ranges in a source file

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::FileHelper

#get_line, #get_lines, #n_lines, #normalize, #shortpath, #virtual_file?

Constructor Details

#initialize(file, annotator) ⇒ SourceFileFormatter

Returns a new instance of SourceFileFormatter.



15
16
17
18
# File 'lib/byebug/source_file_formatter.rb', line 15

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

Instance Attribute Details

#annotatorObject (readonly)

Returns the value of attribute annotator.



13
14
15
# File 'lib/byebug/source_file_formatter.rb', line 13

def annotator
  @annotator
end

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/byebug/source_file_formatter.rb', line 13

def file
  @file
end

Instance Method Details

#amend(line, ceiling) ⇒ Object



67
68
69
# File 'lib/byebug/source_file_formatter.rb', line 67

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

#amend_final(line) ⇒ Object



51
52
53
# File 'lib/byebug/source_file_formatter.rb', line 51

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

#amend_initial(line) ⇒ Object



47
48
49
# File 'lib/byebug/source_file_formatter.rb', line 47

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

#lines(min, max) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/byebug/source_file_formatter.rb', line 20

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

#lines_around(center) ⇒ Object



33
34
35
# File 'lib/byebug/source_file_formatter.rb', line 33

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

#max_initial_lineObject



55
56
57
# File 'lib/byebug/source_file_formatter.rb', line 55

def max_initial_line
  max_line - size + 1
end

#max_lineObject



59
60
61
# File 'lib/byebug/source_file_formatter.rb', line 59

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

#range_around(center) ⇒ Object



37
38
39
# File 'lib/byebug/source_file_formatter.rb', line 37

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

#range_from(min) ⇒ Object



41
42
43
44
45
# File 'lib/byebug/source_file_formatter.rb', line 41

def range_from(min)
  first = amend_initial(min)

  [first, first + size - 1]
end

#sizeObject



63
64
65
# File 'lib/byebug/source_file_formatter.rb', line 63

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