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 diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb index 2728aa656a..531ea519f3 100644 --- a/test/irb/test_cmd.rb +++ b/test/irb/test_cmd.rb @@ -547,6 +547,40 @@ module TestIRB end def test_show_source + input = TestInputMethod.new([ + "show_source IRB.conf\n", + ]) + IRB.init_config(nil) + workspace = IRB::WorkSpace.new(self) + IRB.conf[:VERBOSE] = false + irb = IRB::Irb.new(workspace, input) + IRB.conf[:MAIN_CONTEXT] = irb.context + irb.context.return_format = "=> %s\n" + out, err = capture_output do + irb.eval_input + end + assert_empty err + assert_match(%r[/irb\.rb], out) + end + + def test_show_source_method + input = TestInputMethod.new([ + "p show_source('IRB.conf')\n", + ]) + IRB.init_config(nil) + workspace = IRB::WorkSpace.new(self) + IRB.conf[:VERBOSE] = false + irb = IRB::Irb.new(workspace, input) + IRB.conf[:MAIN_CONTEXT] = irb.context + irb.context.return_format = "=> %s\n" + out, err = capture_output do + irb.eval_input + end + assert_empty err + assert_match(%r[/irb\.rb], out) + end + + def test_show_source_string input = TestInputMethod.new([ "show_source 'IRB.conf'\n", ])