|
3 | 3 | # irb/lib/tracer.rb -
|
4 | 4 | # by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
5 | 5 | #
|
6 |
| - |
| 6 | +# Loading the gem "tracer" will cause it to extend IRB commands with: |
| 7 | +# https://github.com/ruby/tracer/blob/v0.2.2/lib/tracer/irb.rb |
7 | 8 | begin
|
8 | 9 | require "tracer"
|
9 | 10 | rescue LoadError
|
10 | 11 | $stderr.puts "Tracer extension of IRB is enabled but tracer gem wasn't found."
|
11 |
| - module IRB |
12 |
| - class Context |
13 |
| - def use_tracer=(opt) |
14 |
| - # do nothing |
15 |
| - end |
16 |
| - end |
17 |
| - end |
18 | 12 | return # This is about to disable loading below
|
19 | 13 | end
|
20 | 14 |
|
21 | 15 | module IRB
|
22 |
| - |
23 |
| - # initialize tracing function |
24 |
| - def IRB.initialize_tracer |
25 |
| - Tracer.verbose = false |
26 |
| - Tracer.add_filter { |
27 |
| - |event, file, line, id, binding, *rests| |
28 |
| - /^#{Regexp.quote(@CONF[:IRB_LIB_PATH])}/ !~ file and |
29 |
| - File::basename(file) != "irb.rb" |
30 |
| - } |
31 |
| - end |
32 |
| - |
33 |
| - class Context |
34 |
| - # Whether Tracer is used when evaluating statements in this context. |
35 |
| - # |
36 |
| - # See +lib/tracer.rb+ for more information. |
37 |
| - attr_reader :use_tracer |
38 |
| - alias use_tracer? use_tracer |
39 |
| - |
40 |
| - # Sets whether or not to use the Tracer library when evaluating statements |
41 |
| - # in this context. |
42 |
| - # |
43 |
| - # See +lib/tracer.rb+ for more information. |
44 |
| - def use_tracer=(opt) |
45 |
| - if opt |
46 |
| - Tracer.set_get_line_procs(@irb_path) { |
47 |
| - |line_no, *rests| |
48 |
| - @io.line(line_no) |
49 |
| - } |
50 |
| - elsif !opt && @use_tracer |
51 |
| - Tracer.off |
52 |
| - end |
53 |
| - @use_tracer=opt |
54 |
| - end |
55 |
| - end |
56 |
| - |
57 | 16 | class WorkSpace
|
58 | 17 | alias __evaluate__ evaluate
|
59 | 18 | # Evaluate the context of this workspace and use the Tracer library to
|
60 | 19 | # output the exact lines of code are being executed in chronological order.
|
61 | 20 | #
|
62 |
| - # See +lib/tracer.rb+ for more information. |
63 |
| - def evaluate(context, statements, file = nil, line = nil) |
64 |
| - if context.use_tracer? && file != nil && line != nil |
65 |
| - Tracer.on |
66 |
| - begin |
| 21 | + # See https://github.com/ruby/tracer for more information. |
| 22 | + def evaluate(statements, file = __FILE__, line = __LINE__) |
| 23 | + if IRB.conf[:USE_TRACER] == true |
| 24 | + CallTracer.new(colorize: Color.colorable?).start do |
67 | 25 | __evaluate__(statements, file, line)
|
68 |
| - ensure |
69 |
| - Tracer.off |
70 | 26 | end
|
71 | 27 | else
|
72 |
| - __evaluate__(statements, file || __FILE__, line || __LINE__) |
| 28 | + __evaluate__(statements, file, line) |
73 | 29 | end
|
74 | 30 | end
|
75 | 31 | end
|
76 |
| - |
77 |
| - IRB.initialize_tracer |
78 | 32 | end
|
0 commit comments