Class: Pry::Command::Cat::ExceptionFormatter

Inherits:
AbstractFormatter show all
Includes:
Helpers::Text
Defined in:
lib/pry/commands/cat/exception_formatter.rb

Constant Summary

Constants included from Helpers::Text

Helpers::Text::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Text

#bold, #default, #indent, #no_color, #no_pager, #strip_color, #with_line_numbers

Methods inherited from AbstractFormatter

#between_lines, #code_type, #decorate, #use_line_numbers?

Methods included from Helpers::BaseHelpers

#colorize_code, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Methods included from Helpers::CommandHelpers

#absolute_index_number, #absolute_index_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #unindent

Methods included from Helpers::OptionsHelpers

#method_object, method_object, method_options, #method_options

Constructor Details

#initialize(exception, pry_instance, opts) ⇒ ExceptionFormatter

Returns a new instance of ExceptionFormatter.



12
13
14
15
16
# File 'lib/pry/commands/cat/exception_formatter.rb', line 12

def initialize(exception, pry_instance, opts)
  @ex = exception
  @opts = opts
  @pry_instance = pry_instance
end

Instance Attribute Details

#exObject (readonly)

Returns the value of attribute ex.



7
8
9
# File 'lib/pry/commands/cat/exception_formatter.rb', line 7

def ex
  @ex
end

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/pry/commands/cat/exception_formatter.rb', line 8

def opts
  @opts
end

#pry_instanceObject (readonly)

Returns the value of attribute pry_instance.



9
10
11
# File 'lib/pry/commands/cat/exception_formatter.rb', line 9

def pry_instance
  @pry_instance
end

Instance Method Details

#backtrace_fileObject (private)



56
57
58
# File 'lib/pry/commands/cat/exception_formatter.rb', line 56

def backtrace_file
  Array(ex.bt_source_location_for(backtrace_level)).first
end

#backtrace_levelObject (private)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pry/commands/cat/exception_formatter.rb', line 37

def backtrace_level
  @backtrace_level ||=
    begin
      bl =
        if opts[:ex].nil?
          ex.bt_index
        else
          ex.bt_index = absolute_index_number(opts[:ex], ex.backtrace.size)
        end

      increment_backtrace_level
      bl
    end
end

#backtrace_lineObject (private)



60
61
62
# File 'lib/pry/commands/cat/exception_formatter.rb', line 60

def backtrace_line
  Array(ex.bt_source_location_for(backtrace_level)).last
end

#check_for_errorsObject (private)

Raises:



64
65
66
67
68
69
# File 'lib/pry/commands/cat/exception_formatter.rb', line 64

def check_for_errors
  raise CommandError, "No exception found." unless ex
  return if backtrace_file

  raise CommandError, "The given backtrace level is out of bounds."
end

#code_window_sizeObject (private)



33
34
35
# File 'lib/pry/commands/cat/exception_formatter.rb', line 33

def code_window_size
  pry_instance.config.default_window_size || 5
end

#formatObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pry/commands/cat/exception_formatter.rb', line 18

def format
  check_for_errors
  set_file_and_dir_locals(
    backtrace_file, pry_instance, pry_instance.current_context
  )
  code = decorate(
    Pry::Code.from_file(backtrace_file)
      .between(*start_and_end_line_for_code_window)
      .with_marker(backtrace_line)
  )
  "#{header}#{code}"
end

#headerObject (private)



78
79
80
81
82
83
84
85
86
# File 'lib/pry/commands/cat/exception_formatter.rb', line 78

def header
  unindent(
    "#{bold 'Exception:'} #{ex.class}: #{ex.message}\n" \
    "--\n" \
    "#{bold('From:')} #{backtrace_file}:#{backtrace_line} @ " \
    "#{bold("level: #{backtrace_level}")} of backtrace " \
    "(of #{ex.backtrace.size - 1}).\n\n"
  )
end

#increment_backtrace_levelObject (private)



52
53
54
# File 'lib/pry/commands/cat/exception_formatter.rb', line 52

def increment_backtrace_level
  ex.inc_bt_index
end

#start_and_end_line_for_code_windowObject (private)



71
72
73
74
75
76
# File 'lib/pry/commands/cat/exception_formatter.rb', line 71

def start_and_end_line_for_code_window
  start_line = backtrace_line - code_window_size
  start_line = 1 if start_line < 1

  [start_line, backtrace_line + code_window_size]
end