summaryrefslogtreecommitdiff
path: root/lib/irb
diff options
context:
space:
mode:
authorNuno Silva <[email protected]>2024-02-06 16:46:41 +0000
committergit <[email protected]>2024-02-06 16:46:50 +0000
commit300dee1fe8275b7444007b418323544b571f585c (patch)
tree96c09964d8b53b7ee38f7c1f6b4e7e39424cb7b6 /lib/irb
parent26fac8f6fa8bf73e5f45c3028cd5bc186d89f666 (diff)
[ruby/irb] Fix usage of tracer gem and add tests
(https://github.com/ruby/irb/pull/857) The new tests are skipped when ruby below 3.1, as it was a default gem on it, and in a version we do not support. This also move definition of `use_tracer` to module Context instead of monkey patch. https://github.com/ruby/irb/commit/08834fbd5f
Diffstat (limited to 'lib/irb')
-rw-r--r--lib/irb/context.rb5
-rw-r--r--lib/irb/ext/tracer.rb60
-rw-r--r--lib/irb/extend-command.rb1
3 files changed, 12 insertions, 54 deletions
diff --git a/lib/irb/context.rb b/lib/irb/context.rb
index ac61b765c0..5b8791c3ba 100644
--- a/lib/irb/context.rb
+++ b/lib/irb/context.rb
@@ -161,6 +161,11 @@ module IRB
private_constant :KEYWORD_ALIASES
+ def use_tracer=(val)
+ require_relative "ext/tracer"
+ @use_tracer = val
+ end
+
private def build_completor
completor_type = IRB.conf[:COMPLETOR]
case completor_type
diff --git a/lib/irb/ext/tracer.rb b/lib/irb/ext/tracer.rb
index 3eaeb70ef2..53b2e62245 100644
--- a/lib/irb/ext/tracer.rb
+++ b/lib/irb/ext/tracer.rb
@@ -3,76 +3,30 @@
# irb/lib/tracer.rb -
# by Keiju ISHITSUKA([email protected])
#
-
+# Loading the gem "tracer" will cause it to extend IRB commands with:
+# https://github.com/ruby/tracer/blob/v0.2.2/lib/tracer/irb.rb
begin
require "tracer"
rescue LoadError
$stderr.puts "Tracer extension of IRB is enabled but tracer gem wasn't found."
- module IRB
- class Context
- def use_tracer=(opt)
- # do nothing
- end
- end
- end
return # This is about to disable loading below
end
module IRB
-
- # initialize tracing function
- def IRB.initialize_tracer
- Tracer.verbose = false
- Tracer.add_filter {
- |event, file, line, id, binding, *rests|
- /^#{Regexp.quote(@CONF[:IRB_LIB_PATH])}/ !~ file and
- File::basename(file) != "irb.rb"
- }
- end
-
- class Context
- # Whether Tracer is used when evaluating statements in this context.
- #
- # See +lib/tracer.rb+ for more information.
- attr_reader :use_tracer
- alias use_tracer? use_tracer
-
- # Sets whether or not to use the Tracer library when evaluating statements
- # in this context.
- #
- # See +lib/tracer.rb+ for more information.
- def use_tracer=(opt)
- if opt
- Tracer.set_get_line_procs(@irb_path) {
- |line_no, *rests|
- @io.line(line_no)
- }
- elsif !opt && @use_tracer
- Tracer.off
- end
- @use_tracer=opt
- end
- end
-
class WorkSpace
alias __evaluate__ evaluate
# Evaluate the context of this workspace and use the Tracer library to
# output the exact lines of code are being executed in chronological order.
#
- # See +lib/tracer.rb+ for more information.
- def evaluate(context, statements, file = nil, line = nil)
- if context.use_tracer? && file != nil && line != nil
- Tracer.on
- begin
+ # See https://github.com/ruby/tracer for more information.
+ def evaluate(statements, file = __FILE__, line = __LINE__)
+ if IRB.conf[:USE_TRACER] == true
+ CallTracer.new(colorize: Color.colorable?).start do
__evaluate__(statements, file, line)
- ensure
- Tracer.off
end
else
- __evaluate__(statements, file || __FILE__, line || __LINE__)
+ __evaluate__(statements, file, line)
end
end
end
-
- IRB.initialize_tracer
end
diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb
index 69d83080df..91ca96e91a 100644
--- a/lib/irb/extend-command.rb
+++ b/lib/irb/extend-command.rb
@@ -317,7 +317,6 @@ module IRB # :nodoc:
@EXTEND_COMMANDS = [
[:eval_history=, "ext/eval_history.rb"],
- [:use_tracer=, "ext/tracer.rb"],
[:use_loader=, "ext/use-loader.rb"],
]