From f07eb5f2c7bdbb021669bdebc50437f91a5a9d38 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 23 Aug 2023 13:17:56 +0100 Subject: [PATCH] Update `show_cmds`'s output when in irb:rdbg session --- lib/irb/cmd/show_cmds.rb | 15 +++++++++++++++ test/irb/test_debug_cmd.rb | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/irb/cmd/show_cmds.rb b/lib/irb/cmd/show_cmds.rb index cef6254ca..7d6b3ec26 100644 --- a/lib/irb/cmd/show_cmds.rb +++ b/lib/irb/cmd/show_cmds.rb @@ -15,6 +15,16 @@ class ShowCmds < Nop def execute(*args) commands_info = IRB::ExtendCommandBundle.all_commands_info commands_grouped_by_categories = commands_info.group_by { |cmd| cmd[:category] } + + if irb_context.with_debugger + # Remove the original "Debugging" category + commands_grouped_by_categories.delete("Debugging") + # Remove the `help` command as it's delegated to the debugger + commands_grouped_by_categories["Context"].delete_if { |cmd| cmd[:display_name] == :help } + # Add an empty "Debugging (from debug.gem)" category at the end + commands_grouped_by_categories["Debugging (from debug.gem)"] = [] + end + longest_cmd_name_length = commands_info.map { |c| c[:display_name].length }.max output = StringIO.new @@ -29,6 +39,11 @@ def execute(*args) output.puts end + # Append the debugger help at the end + if irb_context.with_debugger + output.puts DEBUGGER__.help + end + Pager.page_content(output.string) end end diff --git a/test/irb/test_debug_cmd.rb b/test/irb/test_debug_cmd.rb index 1bc7cb51c..d669c174e 100644 --- a/test/irb/test_debug_cmd.rb +++ b/test/irb/test_debug_cmd.rb @@ -345,6 +345,25 @@ def test_help_command_is_delegated_to_the_debugger assert_include(output, "### Frame control") end + def test_show_cmds_display_different_content_when_debugger_is_enabled + write_ruby <<~'ruby' + # disable pager + STDIN.singleton_class.define_method(:tty?) { false } + binding.irb + ruby + + output = run_ruby_file do + type "debug" + type "show_cmds" + type "continue" + end + + # IRB's commands should still be listed + assert_match(/show_cmds\s+List all available commands and their description\./, output) + # debug gem's commands should be appended at the end + assert_match(/Debugging \(from debug\.gem\)\s+### Control flow/, output) + end + def test_input_is_evaluated_in_the_context_of_the_current_thread write_ruby <<~'ruby' current_thread = Thread.current