Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display aliases in help message #788

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's purely internal and only for this command at the moment, I don't plan to expose an API for it.

And I don't directly read IRB.conf[:COMMAND_ALIASES] here because if the user updates it in the session, help message and the actual behaviour will be out of sync.


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)
st0012 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading