summaryrefslogtreecommitdiff
path: root/lib/irb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/irb')
-rw-r--r--lib/irb/cmd/show_source.rb18
-rw-r--r--lib/irb/context.rb9
-rw-r--r--lib/irb/extend-command.rb14
3 files changed, 40 insertions, 1 deletions
diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb
index f8a17822df..1fcff3e897 100644
--- a/lib/irb/cmd/show_source.rb
+++ b/lib/irb/cmd/show_source.rb
@@ -9,6 +9,24 @@ module IRB
module ExtendCommand
class ShowSource < Nop
+ class << self
+ def transform_args(args)
+ # Return a string literal as is for backward compatibility
+ if args.empty? || string_literal?(args)
+ args
+ else # Otherwise, consider the input as a String for convenience
+ args.strip.dump
+ end
+ end
+
+ private
+
+ def string_literal?(args)
+ sexp = Ripper.sexp(args)
+ sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
+ end
+ end
+
def execute(str = nil)
unless str.is_a?(String)
puts "Error: Expected a string but got #{str.inspect}"
diff --git a/lib/irb/context.rb b/lib/irb/context.rb
index d1ae2cb605..72c74f081d 100644
--- a/lib/irb/context.rb
+++ b/lib/irb/context.rb
@@ -484,9 +484,16 @@ module IRB
end
# Transform a non-identifier alias (ex: @, $)
- command = line.split(/\s/, 2).first
+ command, args = line.split(/\s/, 2)
if original = symbol_alias(command)
line = line.gsub(/\A#{Regexp.escape(command)}/, original.to_s)
+ command = original
+ end
+
+ # Hook command-specific transformation
+ command_class = ExtendCommandBundle.load_command(command)
+ if command_class&.respond_to?(:transform_args)
+ line = "#{command} #{command_class.transform_args(args)}"
end
set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb
index 08a258fc53..acc23c9920 100644
--- a/lib/irb/extend-command.rb
+++ b/lib/irb/extend-command.rb
@@ -147,6 +147,20 @@ module IRB # :nodoc:
]
+ # Convert a command name to its implementation class if such command exists
+ def self.load_command(command)
+ command = command.to_sym
+ @EXTEND_COMMANDS.each do |cmd_name, cmd_class, load_file, *aliases|
+ next if cmd_name != command && aliases.all? { |alias_name, _| alias_name != command }
+
+ if !defined?(ExtendCommand) || !ExtendCommand.const_defined?(cmd_class, false)
+ require_relative load_file
+ end
+ return ExtendCommand.const_get(cmd_class, false)
+ end
+ nil
+ end
+
# Installs the default irb commands:
#
# +irb_current_working_workspace+:: Context#main