summaryrefslogtreecommitdiff
path: root/lib/irb/workspace.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/irb/workspace.rb')
-rw-r--r--lib/irb/workspace.rb48
1 files changed, 17 insertions, 31 deletions
diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb
index 2c4c40f348..4c3b5e425e 100644
--- a/lib/irb/workspace.rb
+++ b/lib/irb/workspace.rb
@@ -1,14 +1,8 @@
-# frozen_string_literal: false
+# frozen_string_literal: true
#
# irb/workspace-binding.rb -
-# $Release Version: 0.9.6$
-# $Revision$
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
-# --
-#
-#
-#
require "delegate"
@@ -96,11 +90,11 @@ EOF
IRB.conf[:__MAIN__] = @main
@main.singleton_class.class_eval do
private
- define_method(:exit) do |*a, &b|
- # Do nothing, will be overridden
- end
define_method(:binding, Kernel.instance_method(:binding))
define_method(:local_variables, Kernel.instance_method(:local_variables))
+ # Define empty method to avoid delegator warning, will be overridden.
+ define_method(:exit) {|*a, &b| }
+ define_method(:exit!) {|*a, &b| }
end
@binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, *@binding.source_location)
end
@@ -114,8 +108,12 @@ EOF
# <code>IRB.conf[:__MAIN__]</code>
attr_reader :main
+ def load_commands_to_main
+ main.extend ExtendCommandBundle
+ end
+
# Evaluate the given +statements+ within the context of this workspace.
- def evaluate(context, statements, file = __FILE__, line = __LINE__)
+ def evaluate(statements, file = __FILE__, line = __LINE__)
eval(statements, @binding, file, line)
end
@@ -128,6 +126,8 @@ EOF
end
# error message manipulator
+ # WARN: Rails patches this method to filter its own backtrace. Be cautious when changing it.
+ # See: https://github.com/rails/rails/blob/main/railties/lib/rails/commands/console/console_command.rb#L8:~:text=def,filter_backtrace
def filter_backtrace(bt)
return nil if bt =~ /\/irb\/.*\.rb/
return nil if bt =~ /\/irb\.rb/
@@ -142,11 +142,7 @@ EOF
end
def code_around_binding
- if @binding.respond_to?(:source_location)
- file, pos = @binding.source_location
- else
- file, pos = @binding.eval('[__FILE__, __LINE__]')
- end
+ file, pos = @binding.source_location
if defined?(::SCRIPT_LINES__[file]) && lines = ::SCRIPT_LINES__[file]
code = ::SCRIPT_LINES__[file].join('')
@@ -158,30 +154,20 @@ EOF
end
end
- # NOT using #use_colorize? of IRB.conf[:MAIN_CONTEXT] because this method may be called before IRB::Irb#run
- use_colorize = IRB.conf.fetch(:USE_COLORIZE, true)
- if use_colorize
- lines = Color.colorize_code(code).lines
- else
- lines = code.lines
- end
+ lines = Color.colorize_code(code).lines
pos -= 1
start_pos = [pos - 5, 0].max
end_pos = [pos + 5, lines.size - 1].min
- if use_colorize
- fmt = " %2s #{Color.colorize("%#{end_pos.to_s.length}d", [:BLUE, :BOLD])}: %s"
- else
- fmt = " %2s %#{end_pos.to_s.length}d: %s"
- end
+ line_number_fmt = Color.colorize("%#{end_pos.to_s.length}d", [:BLUE, :BOLD])
+ fmt = " %2s #{line_number_fmt}: %s"
+
body = (start_pos..end_pos).map do |current_pos|
sprintf(fmt, pos == current_pos ? '=>' : '', current_pos + 1, lines[current_pos])
end.join("")
- "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}#{Color.clear if use_colorize}\n"
- end
- def IRB.delete_caller
+ "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}#{Color.clear}\n"
end
end
end