Skip to content

Commit

Permalink
[ruby/irb] Display aliases in help message
Browse files Browse the repository at this point in the history
(ruby/irb#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`
```

ruby/irb@2a0eacc891
  • Loading branch information
st0012 authored and matzbot committed Nov 26, 2023
1 parent 688faa9 commit cc5d1bf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/irb/cmd/show_cmds.rb
Expand Up @@ -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")
Expand Down
14 changes: 13 additions & 1 deletion lib/irb/context.rb
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions lib/irb/init.rb
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions test/irb/test_cmd.rb
Expand Up @@ -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
Expand Down

0 comments on commit cc5d1bf

Please sign in to comment.