[ruby/irb] Stop using ExtendCommandBundle internally

(https://github.com/ruby/irb/pull/925)

This module was used to extend both commands and helpers when they're not
separated. Now that they are, and we have a Command module, we should move
command-related logic to the Command module and update related references.

This will make the code easier to understand and refactor in the future.

https://github.com/ruby/irb/commit/f74ec97236
This commit is contained in:
Stan Lo 2024-04-21 02:55:51 +08:00 committed by git
parent 2b11bcb84e
commit f16c6ac4fd
6 changed files with 117 additions and 109 deletions

View File

@ -1120,7 +1120,7 @@ module IRB
code.force_encoding(@context.io.encoding) code.force_encoding(@context.io.encoding)
if (command, arg = parse_command(code)) if (command, arg = parse_command(code))
command_class = ExtendCommandBundle.load_command(command) command_class = Command.load_command(command)
Statement::Command.new(code, command_class, arg) Statement::Command.new(code, command_class, arg)
else else
is_assignment_expression = @scanner.assignment_expression?(code, local_variables: @context.local_variables) is_assignment_expression = @scanner.assignment_expression?(code, local_variables: @context.local_variables)
@ -1142,7 +1142,7 @@ module IRB
# Check visibility # Check visibility
public_method = !!Kernel.instance_method(:public_method).bind_call(@context.main, command) rescue false public_method = !!Kernel.instance_method(:public_method).bind_call(@context.main, command) rescue false
private_method = !public_method && !!Kernel.instance_method(:method).bind_call(@context.main, command) rescue false private_method = !public_method && !!Kernel.instance_method(:method).bind_call(@context.main, command) rescue false
if ExtendCommandBundle.execute_as_command?(command, public_method: public_method, private_method: private_method) if Command.execute_as_command?(command, public_method: public_method, private_method: private_method)
[command, arg] [command, arg]
end end
end end

View File

@ -18,12 +18,6 @@ module IRB # :nodoc:
def register(name, command_class) def register(name, command_class)
@commands[name] = [command_class, []] @commands[name] = [command_class, []]
end end
# This API is for IRB's internal use only and may change at any time.
# Please do NOT use it.
def _register_with_aliases(name, command_class, *aliases)
@commands[name] = [command_class, aliases]
end
end end
end end
end end

View File

@ -11,7 +11,7 @@ module IRB
if command_name.empty? if command_name.empty?
help_message help_message
else else
if command_class = ExtendCommandBundle.load_command(command_name) if command_class = Command.load_command(command_name)
command_class.help_message || command_class.description command_class.help_message || command_class.description
else else
"Can't find command `#{command_name}`. Please check the command name and try again.\n\n" "Can't find command `#{command_name}`. Please check the command name and try again.\n\n"
@ -23,7 +23,7 @@ module IRB
private private
def help_message def help_message
commands_info = IRB::ExtendCommandBundle.all_commands_info commands_info = IRB::Command.all_commands_info
commands_grouped_by_categories = commands_info.group_by { |cmd| cmd[:category] } commands_grouped_by_categories = commands_info.group_by { |cmd| cmd[:category] }
user_aliases = irb_context.instance_variable_get(:@user_aliases) user_aliases = irb_context.instance_variable_get(:@user_aliases)

View File

@ -88,7 +88,7 @@ module IRB
def command_completions(preposing, target) def command_completions(preposing, target)
if preposing.empty? && !target.empty? if preposing.empty? && !target.empty?
IRB::ExtendCommandBundle.command_names.select { _1.start_with?(target) } IRB::Command.command_names.select { _1.start_with?(target) }
else else
[] []
end end

View File

@ -30,166 +30,25 @@ require_relative "command/whereami"
require_relative "command/history" require_relative "command/history"
module IRB module IRB
ExtendCommand = Command module Command
# Installs the default irb extensions command bundle.
module ExtendCommandBundle
# See #install_alias_method.
NO_OVERRIDE = 0 NO_OVERRIDE = 0
# See #install_alias_method.
OVERRIDE_PRIVATE_ONLY = 0x01 OVERRIDE_PRIVATE_ONLY = 0x01
# See #install_alias_method.
OVERRIDE_ALL = 0x02 OVERRIDE_ALL = 0x02
Command._register_with_aliases(:irb_context, Command::Context, class << self
[ # This API is for IRB's internal use only and may change at any time.
[:context, NO_OVERRIDE], # Please do NOT use it.
[:conf, NO_OVERRIDE], def _register_with_aliases(name, command_class, *aliases)
], @commands[name] = [command_class, aliases]
) end
Command._register_with_aliases(:irb_exit, Command::Exit, def all_commands_info
[:exit, OVERRIDE_PRIVATE_ONLY],
[:quit, OVERRIDE_PRIVATE_ONLY],
[:irb_quit, OVERRIDE_PRIVATE_ONLY]
)
Command._register_with_aliases(:irb_exit!, Command::ForceExit,
[:exit!, OVERRIDE_PRIVATE_ONLY]
)
Command._register_with_aliases(:irb_current_working_workspace, Command::CurrentWorkingWorkspace,
[:cwws, NO_OVERRIDE],
[:pwws, NO_OVERRIDE],
[:irb_print_working_workspace, OVERRIDE_ALL],
[:irb_cwws, OVERRIDE_ALL],
[:irb_pwws, OVERRIDE_ALL],
[:irb_current_working_binding, OVERRIDE_ALL],
[:irb_print_working_binding, OVERRIDE_ALL],
[:irb_cwb, OVERRIDE_ALL],
[:irb_pwb, OVERRIDE_ALL],
)
Command._register_with_aliases(:irb_change_workspace, Command::ChangeWorkspace,
[:chws, NO_OVERRIDE],
[:cws, NO_OVERRIDE],
[:irb_chws, OVERRIDE_ALL],
[:irb_cws, OVERRIDE_ALL],
[:irb_change_binding, OVERRIDE_ALL],
[:irb_cb, OVERRIDE_ALL],
[:cb, NO_OVERRIDE],
)
Command._register_with_aliases(:irb_workspaces, Command::Workspaces,
[:workspaces, NO_OVERRIDE],
[:irb_bindings, OVERRIDE_ALL],
[:bindings, NO_OVERRIDE],
)
Command._register_with_aliases(:irb_push_workspace, Command::PushWorkspace,
[:pushws, NO_OVERRIDE],
[:irb_pushws, OVERRIDE_ALL],
[:irb_push_binding, OVERRIDE_ALL],
[:irb_pushb, OVERRIDE_ALL],
[:pushb, NO_OVERRIDE],
)
Command._register_with_aliases(:irb_pop_workspace, Command::PopWorkspace,
[:popws, NO_OVERRIDE],
[:irb_popws, OVERRIDE_ALL],
[:irb_pop_binding, OVERRIDE_ALL],
[:irb_popb, OVERRIDE_ALL],
[:popb, NO_OVERRIDE],
)
Command._register_with_aliases(:irb_load, Command::Load)
Command._register_with_aliases(:irb_require, Command::Require)
Command._register_with_aliases(:irb_source, Command::Source,
[:source, NO_OVERRIDE]
)
Command._register_with_aliases(:irb, Command::IrbCommand)
Command._register_with_aliases(:irb_jobs, Command::Jobs,
[:jobs, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_fg, Command::Foreground,
[:fg, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_kill, Command::Kill,
[:kill, OVERRIDE_PRIVATE_ONLY]
)
Command._register_with_aliases(:irb_debug, Command::Debug,
[:debug, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_edit, Command::Edit,
[:edit, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_break, Command::Break)
Command._register_with_aliases(:irb_catch, Command::Catch)
Command._register_with_aliases(:irb_next, Command::Next)
Command._register_with_aliases(:irb_delete, Command::Delete,
[:delete, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_step, Command::Step,
[:step, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_continue, Command::Continue,
[:continue, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_finish, Command::Finish,
[:finish, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_backtrace, Command::Backtrace,
[:backtrace, NO_OVERRIDE],
[:bt, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_debug_info, Command::Info,
[:info, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_help, Command::Help,
[:help, NO_OVERRIDE],
[:show_cmds, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_show_doc, Command::ShowDoc,
[:show_doc, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_info, Command::IrbInfo)
Command._register_with_aliases(:irb_ls, Command::Ls,
[:ls, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_measure, Command::Measure,
[:measure, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_show_source, Command::ShowSource,
[:show_source, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_whereami, Command::Whereami,
[:whereami, NO_OVERRIDE]
)
Command._register_with_aliases(:irb_history, Command::History,
[:history, NO_OVERRIDE],
[:hist, NO_OVERRIDE]
)
def self.all_commands_info
user_aliases = IRB.CurrentContext.command_aliases.each_with_object({}) do |(alias_name, target), result| user_aliases = IRB.CurrentContext.command_aliases.each_with_object({}) do |(alias_name, target), result|
result[target] ||= [] result[target] ||= []
result[target] << alias_name result[target] << alias_name
end end
Command.commands.map do |command_name, (command_class, aliases)| commands.map do |command_name, (command_class, aliases)|
aliases = aliases.map { |a| a.first } aliases = aliases.map { |a| a.first }
if additional_aliases = user_aliases[command_name] if additional_aliases = user_aliases[command_name]
@ -205,13 +64,13 @@ module IRB
end end
end end
def self.command_override_policies def command_override_policies
@@command_override_policies ||= Command.commands.flat_map do |cmd_name, (cmd_class, aliases)| @@command_override_policies ||= commands.flat_map do |cmd_name, (cmd_class, aliases)|
[[cmd_name, OVERRIDE_ALL]] + aliases [[cmd_name, OVERRIDE_ALL]] + aliases
end.to_h end.to_h
end end
def self.execute_as_command?(name, public_method:, private_method:) def execute_as_command?(name, public_method:, private_method:)
case command_override_policies[name] case command_override_policies[name]
when OVERRIDE_ALL when OVERRIDE_ALL
true true
@ -222,20 +81,175 @@ module IRB
end end
end end
def self.command_names def command_names
command_override_policies.keys.map(&:to_s) command_override_policies.keys.map(&:to_s)
end end
# Convert a command name to its implementation class if such command exists # Convert a command name to its implementation class if such command exists
def self.load_command(command) def load_command(command)
command = command.to_sym command = command.to_sym
Command.commands.each do |command_name, (command_class, aliases)| commands.each do |command_name, (command_class, aliases)|
if command_name == command || aliases.any? { |alias_name, _| alias_name == command } if command_name == command || aliases.any? { |alias_name, _| alias_name == command }
return command_class return command_class
end end
end end
nil nil
end end
end
_register_with_aliases(:irb_context, Command::Context,
[
[:context, NO_OVERRIDE],
[:conf, NO_OVERRIDE],
],
)
_register_with_aliases(:irb_exit, Command::Exit,
[:exit, OVERRIDE_PRIVATE_ONLY],
[:quit, OVERRIDE_PRIVATE_ONLY],
[:irb_quit, OVERRIDE_PRIVATE_ONLY]
)
_register_with_aliases(:irb_exit!, Command::ForceExit,
[:exit!, OVERRIDE_PRIVATE_ONLY]
)
_register_with_aliases(:irb_current_working_workspace, Command::CurrentWorkingWorkspace,
[:cwws, NO_OVERRIDE],
[:pwws, NO_OVERRIDE],
[:irb_print_working_workspace, OVERRIDE_ALL],
[:irb_cwws, OVERRIDE_ALL],
[:irb_pwws, OVERRIDE_ALL],
[:irb_current_working_binding, OVERRIDE_ALL],
[:irb_print_working_binding, OVERRIDE_ALL],
[:irb_cwb, OVERRIDE_ALL],
[:irb_pwb, OVERRIDE_ALL],
)
_register_with_aliases(:irb_change_workspace, Command::ChangeWorkspace,
[:chws, NO_OVERRIDE],
[:cws, NO_OVERRIDE],
[:irb_chws, OVERRIDE_ALL],
[:irb_cws, OVERRIDE_ALL],
[:irb_change_binding, OVERRIDE_ALL],
[:irb_cb, OVERRIDE_ALL],
[:cb, NO_OVERRIDE],
)
_register_with_aliases(:irb_workspaces, Command::Workspaces,
[:workspaces, NO_OVERRIDE],
[:irb_bindings, OVERRIDE_ALL],
[:bindings, NO_OVERRIDE],
)
_register_with_aliases(:irb_push_workspace, Command::PushWorkspace,
[:pushws, NO_OVERRIDE],
[:irb_pushws, OVERRIDE_ALL],
[:irb_push_binding, OVERRIDE_ALL],
[:irb_pushb, OVERRIDE_ALL],
[:pushb, NO_OVERRIDE],
)
_register_with_aliases(:irb_pop_workspace, Command::PopWorkspace,
[:popws, NO_OVERRIDE],
[:irb_popws, OVERRIDE_ALL],
[:irb_pop_binding, OVERRIDE_ALL],
[:irb_popb, OVERRIDE_ALL],
[:popb, NO_OVERRIDE],
)
_register_with_aliases(:irb_load, Command::Load)
_register_with_aliases(:irb_require, Command::Require)
_register_with_aliases(:irb_source, Command::Source,
[:source, NO_OVERRIDE]
)
_register_with_aliases(:irb, Command::IrbCommand)
_register_with_aliases(:irb_jobs, Command::Jobs,
[:jobs, NO_OVERRIDE]
)
_register_with_aliases(:irb_fg, Command::Foreground,
[:fg, NO_OVERRIDE]
)
_register_with_aliases(:irb_kill, Command::Kill,
[:kill, OVERRIDE_PRIVATE_ONLY]
)
_register_with_aliases(:irb_debug, Command::Debug,
[:debug, NO_OVERRIDE]
)
_register_with_aliases(:irb_edit, Command::Edit,
[:edit, NO_OVERRIDE]
)
_register_with_aliases(:irb_break, Command::Break)
_register_with_aliases(:irb_catch, Command::Catch)
_register_with_aliases(:irb_next, Command::Next)
_register_with_aliases(:irb_delete, Command::Delete,
[:delete, NO_OVERRIDE]
)
_register_with_aliases(:irb_step, Command::Step,
[:step, NO_OVERRIDE]
)
_register_with_aliases(:irb_continue, Command::Continue,
[:continue, NO_OVERRIDE]
)
_register_with_aliases(:irb_finish, Command::Finish,
[:finish, NO_OVERRIDE]
)
_register_with_aliases(:irb_backtrace, Command::Backtrace,
[:backtrace, NO_OVERRIDE],
[:bt, NO_OVERRIDE]
)
_register_with_aliases(:irb_debug_info, Command::Info,
[:info, NO_OVERRIDE]
)
_register_with_aliases(:irb_help, Command::Help,
[:help, NO_OVERRIDE],
[:show_cmds, NO_OVERRIDE]
)
_register_with_aliases(:irb_show_doc, Command::ShowDoc,
[:show_doc, NO_OVERRIDE]
)
_register_with_aliases(:irb_info, Command::IrbInfo)
_register_with_aliases(:irb_ls, Command::Ls,
[:ls, NO_OVERRIDE]
)
_register_with_aliases(:irb_measure, Command::Measure,
[:measure, NO_OVERRIDE]
)
_register_with_aliases(:irb_show_source, Command::ShowSource,
[:show_source, NO_OVERRIDE]
)
_register_with_aliases(:irb_whereami, Command::Whereami,
[:whereami, NO_OVERRIDE]
)
_register_with_aliases(:irb_history, Command::History,
[:history, NO_OVERRIDE],
[:hist, NO_OVERRIDE]
)
end
ExtendCommand = Command
# For backward compatibility, we need to keep this module:
# - As a container of helper methods
# - As a place to register commands with the deprecated def_extend_command method
module ExtendCommandBundle
# For backward compatibility
NO_OVERRIDE = Command::NO_OVERRIDE
OVERRIDE_PRIVATE_ONLY = Command::OVERRIDE_PRIVATE_ONLY
OVERRIDE_ALL = Command::OVERRIDE_ALL
# Deprecated. Doesn't have any effect. # Deprecated. Doesn't have any effect.
@EXTEND_COMMANDS = [] @EXTEND_COMMANDS = []
@ -243,7 +257,7 @@ module IRB
# Drepcated. Use Command.regiser instead. # Drepcated. Use Command.regiser instead.
def self.def_extend_command(cmd_name, cmd_class, _, *aliases) def self.def_extend_command(cmd_name, cmd_class, _, *aliases)
Command._register_with_aliases(cmd_name, cmd_class, *aliases) Command._register_with_aliases(cmd_name, cmd_class, *aliases)
@@command_override_policies = nil Command.class_variable_set(:@@command_override_policies, nil)
end end
end end
end end

View File

@ -213,11 +213,11 @@ module TestIRB
class CustomCommandTestCase < CommandTestCase class CustomCommandTestCase < CommandTestCase
def setup def setup
@commands_backup = IRB::Command.commands @commands_backup = IRB::Command.commands
IRB::ExtendCommandBundle.class_variable_set(:@@command_override_policies, nil) IRB::Command.class_variable_set(:@@command_override_policies, nil)
end end
def teardown def teardown
IRB::ExtendCommandBundle.class_variable_set(:@@command_override_policies, nil) IRB::Command.class_variable_set(:@@command_override_policies, nil)
IRB::Command.instance_variable_set(:@commands, @commands_backup) IRB::Command.instance_variable_set(:@commands, @commands_backup)
end end
end end
@ -232,7 +232,7 @@ module TestIRB
end end
def test_arg def test_arg
IRB::Command._register_with_aliases(:print_arg, PrintArgCommand, [:pa, IRB::ExtendCommandBundle::OVERRIDE_ALL]) IRB::Command._register_with_aliases(:print_arg, PrintArgCommand, [:pa, IRB::Command::OVERRIDE_ALL])
out, err = execute_lines("print_arg\n") out, err = execute_lines("print_arg\n")
assert_empty err assert_empty err
assert_include(out, 'arg=""') assert_include(out, 'arg=""')
@ -265,7 +265,7 @@ module TestIRB
end end
def test_def_extend_command def test_def_extend_command
IRB::Command._register_with_aliases(:foobar, FooBarCommand, [:fbalias, IRB::ExtendCommandBundle::OVERRIDE_ALL]) IRB::ExtendCommandBundle.def_extend_command(:foobar, FooBarCommand, nil, [:fbalias, IRB::Command::OVERRIDE_ALL])
out, err = execute_lines("foobar\n") out, err = execute_lines("foobar\n")
assert_empty err assert_empty err
assert_include(out, "FooBar executed") assert_include(out, "FooBar executed")