Class: VER::ExceptionView

Inherits:
Tk::Tile::Treeview
  • Object
show all
Defined in:
lib/ver/exception_view.rb

Constant Summary collapse

OPTIONS =
{
  context: 7,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, options = {}) ⇒ ExceptionView

Returns a new instance of ExceptionView.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ver/exception_view.rb', line 9

def initialize(parent, options = {})
  super

  @frames = {}
  @context_size = OPTIONS[:context]
  @error_tags = ['error']
  @backtrace_tags = ['backtrace']

  setup_config
  setup_binds
end

Instance Attribute Details

#backtrace_tagsObject (readonly)

Returns the value of attribute backtrace_tags.



7
8
9
# File 'lib/ver/exception_view.rb', line 7

def backtrace_tags
  @backtrace_tags
end

#context_sizeObject (readonly)

Returns the value of attribute context_size.



7
8
9
# File 'lib/ver/exception_view.rb', line 7

def context_size
  @context_size
end

#error_tagsObject (readonly)

Returns the value of attribute error_tags.



7
8
9
# File 'lib/ver/exception_view.rb', line 7

def error_tags
  @error_tags
end

#framesObject (readonly)

Returns the value of attribute frames.



7
8
9
# File 'lib/ver/exception_view.rb', line 7

def frames
  @frames
end

#treeObject (readonly)

Returns the value of attribute tree.



7
8
9
# File 'lib/ver/exception_view.rb', line 7

def tree
  @tree
end

Instance Method Details

#on_treeview_openObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ver/exception_view.rb', line 46

def on_treeview_open
  item = focus_item
  frame = frames[item.id]

  case frame
  when Hash
    filename, lineno, first_lineno, context =
      frame.values_at(:filename, :lineno, :first_lineno, :context)

    context.each_with_index{|line, idx|
      line_lineno = first_lineno + idx + 1
      tags = line_lineno == lineno ? error_tags : backtrace_tags
      line_item = item.insert(:end,
        text: line, values: [line_lineno], tags: tags)
      frames[line_item.id] = [filename, lineno]
    }
  when Array
    filename, lineno = frame
    VER.find_or_create_buffer(filename, lineno){|buffer|
      pack_forget
    }
  end
rescue => ex # careful here, don't want infinite loop
  puts ex, ex.backtrace
end

#setup_bindsObject



37
38
39
40
41
42
43
44
# File 'lib/ver/exception_view.rb', line 37

def setup_binds
  bind('<<TreeviewOpen>>'){ on_treeview_open }

  bind('<Escape>'){
    pack_forget
    VER.layout.visible.first.show
  }
end

#setup_configObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ver/exception_view.rb', line 21

def setup_config
  configure(
    columns:        %w[line method],
    displaycolumns: %w[line method]
  )

  heading('#0',     text: 'File')
  heading('line',   text: 'Line')
  heading('method', text: 'Method')

  tag_configure('error', background: '#f88')
  tag_configure('backtrace', background: '#8f8')

  column('line', width: 50, stretch: false, anchor: :e)
end

#show(exception) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ver/exception_view.rb', line 72

def show(exception)
  clear

  # from Rack::ShowExceptions
  exception.backtrace.each do |line|
    next unless line =~ /(.*?):(\d+)(:in `(.*)')?/
    show_line($1, $2.to_i, $4)
  end

  focus
  pack expand: true, fill: :both
end

#show_line(filename, lineno, function) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ver/exception_view.rb', line 85

def show_line(filename, lineno, function)
  item = insert(nil, :end,
    text: filename, values: [lineno, function], tags: error_tags)

  # may fail from here on without issues.
  lines = ::File.readlines(filename)
  _lineno = lineno - 1

  first_lineno = [_lineno - context_size, 0].max
  last_lineno  = [_lineno + context_size, lines.size].min
  context = lines[first_lineno..last_lineno]

  frames[item.id] = {
    filename: filename,
    lineno: lineno,
    function: function,
    first_lineno: first_lineno,
    last_lineno: last_lineno,
    context: context,
  }
rescue => ex
  puts ex, ex.backtrace
end