Skip to content

Commit

Permalink
Display aliases in help message
Browse files Browse the repository at this point in the history
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`
```
  • Loading branch information
st0012 committed Nov 26, 2023
1 parent fa9ecf9 commit acff99f
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,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
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,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 acff99f

Please sign in to comment.