From cc5d1bf026bcc5b4929a4f9d5e32d2fa5730348c Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sun, 26 Nov 2023 17:07:41 +0000 Subject: [PATCH] [ruby/irb] Display aliases in help message (https://github.com/ruby/irb/pull/788) Similar to Pry, it displays user-defined aliases in the help message with a dedicated section. With the current default aliases, it looks like: ``` ...other sections... Aliases $ Alias for `show_source` @ Alias for `whereami` ``` https://github.com/ruby/irb/commit/2a0eacc891 --- lib/irb/cmd/show_cmds.rb | 6 ++++++ lib/irb/context.rb | 14 +++++++++++++- lib/irb/init.rb | 4 ---- test/irb/test_cmd.rb | 10 ++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/irb/cmd/show_cmds.rb b/lib/irb/cmd/show_cmds.rb index 7d6b3ec2668b6f..a8d899e4ace17e 100644 --- a/lib/irb/cmd/show_cmds.rb +++ b/lib/irb/cmd/show_cmds.rb @@ -16,6 +16,12 @@ def execute(*args) commands_info = IRB::ExtendCommandBundle.all_commands_info commands_grouped_by_categories = commands_info.group_by { |cmd| cmd[:category] } + user_aliases = irb_context.instance_variable_get(:@user_aliases) + + commands_grouped_by_categories["Aliases"] = user_aliases.map do |alias_name, target| + { display_name: alias_name, description: "Alias for `#{target}`" } + end + if irb_context.with_debugger # Remove the original "Debugging" category commands_grouped_by_categories.delete("Debugging") diff --git a/lib/irb/context.rb b/lib/irb/context.rb index a7b8ca2c26506b..3442fbf4da5cf9 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -146,9 +146,21 @@ def initialize(irb, workspace = nil, input_method = nil) @newline_before_multiline_output = true end - @command_aliases = IRB.conf[:COMMAND_ALIASES] + @user_aliases = IRB.conf[:COMMAND_ALIASES].dup + @command_aliases = @user_aliases.merge(KEYWORD_ALIASES) end + # because all input will eventually be evaluated as Ruby code, + # command names that conflict with Ruby keywords need special workaround + # we can remove them once we implemented a better command system for IRB + KEYWORD_ALIASES = { + :break => :irb_break, + :catch => :irb_catch, + :next => :irb_next, + }.freeze + + private_constant :KEYWORD_ALIASES + private def build_completor completor_type = IRB.conf[:COMPLETOR] case completor_type diff --git a/lib/irb/init.rb b/lib/irb/init.rb index 9704e36cb10745..b69f68d53061e7 100644 --- a/lib/irb/init.rb +++ b/lib/irb/init.rb @@ -189,10 +189,6 @@ def IRB.init_config(ap_path) # Symbol aliases :'$' => :show_source, :'@' => :whereami, - # Keyword aliases - :break => :irb_break, - :catch => :irb_catch, - :next => :irb_next, } end diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb index 62ef7a5b70e2a7..55373c2e8aa3b2 100644 --- a/test/irb/test_cmd.rb +++ b/test/irb/test_cmd.rb @@ -680,6 +680,16 @@ def test_show_cmds assert_match(/List all available commands and their description/, out) assert_match(/Start the debugger of debug\.gem/, out) end + + def test_show_cmds_list_user_aliases + out, err = execute_lines( + "show_cmds\n" + ) + + assert_empty err + assert_match(/\$\s+Alias for `show_source`/, out) + assert_match(/@\s+Alias for `whereami`/, out) + end end class LsTest < CommandTestCase