diff options
Diffstat (limited to 'lib/irb')
-rw-r--r-- | lib/irb/color.rb | 4 | ||||
-rw-r--r-- | lib/irb/command.rb | 8 | ||||
-rw-r--r-- | lib/irb/command/base.rb | 4 | ||||
-rw-r--r-- | lib/irb/command/chws.rb | 5 | ||||
-rw-r--r-- | lib/irb/command/debug.rb | 17 | ||||
-rw-r--r-- | lib/irb/command/exit.rb | 4 | ||||
-rw-r--r-- | lib/irb/command/force_exit.rb | 4 | ||||
-rw-r--r-- | lib/irb/command/help.rb | 30 | ||||
-rw-r--r-- | lib/irb/command/subirb.rb | 5 | ||||
-rw-r--r-- | lib/irb/completion.rb | 2 | ||||
-rw-r--r-- | lib/irb/context.rb | 29 | ||||
-rw-r--r-- | lib/irb/default_commands.rb | 207 | ||||
-rw-r--r-- | lib/irb/helper_method.rb | 29 | ||||
-rw-r--r-- | lib/irb/helper_method/base.rb | 16 | ||||
-rw-r--r-- | lib/irb/helper_method/conf.rb | 11 | ||||
-rw-r--r-- | lib/irb/init.rb | 35 | ||||
-rw-r--r-- | lib/irb/input-method.rb | 1 | ||||
-rw-r--r-- | lib/irb/ruby-lex.rb | 46 | ||||
-rw-r--r-- | lib/irb/version.rb | 4 | ||||
-rw-r--r-- | lib/irb/workspace.rb | 20 |
20 files changed, 299 insertions, 182 deletions
diff --git a/lib/irb/color.rb b/lib/irb/color.rb index ad8670160c..fca942b28b 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -79,12 +79,12 @@ module IRB # :nodoc: class << self def colorable? - supported = $stdout.tty? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb')) + supported = $stdout.tty? && (/mswin|mingw/.match?(RUBY_PLATFORM) || (ENV.key?('TERM') && ENV['TERM'] != 'dumb')) # because ruby/debug also uses irb's color module selectively, # irb won't be activated in that case. if IRB.respond_to?(:conf) - supported && IRB.conf.fetch(:USE_COLORIZE, true) + supported && !!IRB.conf.fetch(:USE_COLORIZE, true) else supported end diff --git a/lib/irb/command.rb b/lib/irb/command.rb index 19fde56356..68a4b52727 100644 --- a/lib/irb/command.rb +++ b/lib/irb/command.rb @@ -16,13 +16,7 @@ module IRB # :nodoc: # Registers a command with the given name. # Aliasing is intentionally not supported at the moment. def register(name, command_class) - @commands[name] = [command_class, []] - 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] + @commands[name.to_sym] = [command_class, []] end end end diff --git a/lib/irb/command/base.rb b/lib/irb/command/base.rb index b078b48237..1d406630a2 100644 --- a/lib/irb/command/base.rb +++ b/lib/irb/command/base.rb @@ -18,12 +18,12 @@ module IRB class << self def category(category = nil) @category = category if category - @category + @category || "No category" end def description(description = nil) @description = description if description - @description + @description || "No description provided." end def help_message(help_message = nil) diff --git a/lib/irb/command/chws.rb b/lib/irb/command/chws.rb index e0a406885f..ef456d0961 100644 --- a/lib/irb/command/chws.rb +++ b/lib/irb/command/chws.rb @@ -15,7 +15,7 @@ module IRB description "Show the current workspace." def execute(_arg) - irb_context.main + puts "Current workspace: #{irb_context.main}" end end @@ -30,7 +30,8 @@ module IRB obj = eval(arg, irb_context.workspace.binding) irb_context.change_workspace(obj) end - irb_context.main + + puts "Current workspace: #{irb_context.main}" end end end diff --git a/lib/irb/command/debug.rb b/lib/irb/command/debug.rb index f9aca0a672..8a091a49ed 100644 --- a/lib/irb/command/debug.rb +++ b/lib/irb/command/debug.rb @@ -8,11 +8,6 @@ module IRB category "Debugging" description "Start the debugger of debug.gem." - BINDING_IRB_FRAME_REGEXPS = [ - '<internal:prelude>', - binding.method(:irb).source_location.first, - ].map { |file| /\A#{Regexp.escape(file)}:\d+:in (`|'Binding#)irb'\z/ } - def execute(_arg) execute_debug_command end @@ -36,7 +31,7 @@ module IRB # 3. Insert a debug breakpoint at `Irb#debug_break` with the intended command. # 4. Exit the current Irb#run call via `throw :IRB_EXIT`. # 5. `Irb#debug_break` will be called and trigger the breakpoint, which will run the intended command. - unless binding_irb? + unless irb_context.from_binding? puts "Debugging commands are only available when IRB is started with binding.irb" return end @@ -60,16 +55,6 @@ module IRB throw :IRB_EXIT end end - - private - - def binding_irb? - caller.any? do |frame| - BINDING_IRB_FRAME_REGEXPS.any? do |regexp| - frame.match?(regexp) - end - end - end end class DebugCommand < Debug diff --git a/lib/irb/command/exit.rb b/lib/irb/command/exit.rb index 3109ec16e3..b4436f0343 100644 --- a/lib/irb/command/exit.rb +++ b/lib/irb/command/exit.rb @@ -8,10 +8,8 @@ module IRB category "IRB" description "Exit the current irb session." - def execute(*) + def execute(_arg) IRB.irb_exit - rescue UncaughtThrowError - Kernel.exit end end end diff --git a/lib/irb/command/force_exit.rb b/lib/irb/command/force_exit.rb index c2c5542e24..14086aa849 100644 --- a/lib/irb/command/force_exit.rb +++ b/lib/irb/command/force_exit.rb @@ -8,10 +8,8 @@ module IRB category "IRB" description "Exit the current process." - def execute(*) + def execute(_arg) throw :IRB_EXIT, true - rescue UncaughtThrowError - Kernel.exit! end end end diff --git a/lib/irb/command/help.rb b/lib/irb/command/help.rb index c9f16e05bc..c2018f9b30 100644 --- a/lib/irb/command/help.rb +++ b/lib/irb/command/help.rb @@ -11,7 +11,7 @@ module IRB if command_name.empty? help_message else - if command_class = ExtendCommandBundle.load_command(command_name) + if command_class = Command.load_command(command_name) command_class.help_message || command_class.description else "Can't find command `#{command_name}`. Please check the command name and try again.\n\n" @@ -23,20 +23,14 @@ module IRB private def help_message - commands_info = IRB::ExtendCommandBundle.all_commands_info + commands_info = IRB::Command.all_commands_info + helper_methods_info = IRB::HelperMethod.all_helper_methods_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 + commands_grouped_by_categories["Helper methods"] = helper_methods_info if irb_context.with_debugger # Remove the original "Debugging" category commands_grouped_by_categories.delete("Debugging") - # Add an empty "Debugging (from debug.gem)" category at the end - commands_grouped_by_categories["Debugging (from debug.gem)"] = [] end longest_cmd_name_length = commands_info.map { |c| c[:display_name].length }.max @@ -44,15 +38,31 @@ module IRB output = StringIO.new help_cmds = commands_grouped_by_categories.delete("Help") + no_category_cmds = commands_grouped_by_categories.delete("No category") + aliases = irb_context.instance_variable_get(:@user_aliases).map do |alias_name, target| + { display_name: alias_name, description: "Alias for `#{target}`" } + end + # Display help commands first add_category_to_output("Help", help_cmds, output, longest_cmd_name_length) + # Display the rest of the commands grouped by categories commands_grouped_by_categories.each do |category, cmds| add_category_to_output(category, cmds, output, longest_cmd_name_length) end + # Display commands without a category + if no_category_cmds + add_category_to_output("No category", no_category_cmds, output, longest_cmd_name_length) + end + + # Display aliases + add_category_to_output("Aliases", aliases, output, longest_cmd_name_length) + # Append the debugger help at the end if irb_context.with_debugger + # Add "Debugging (from debug.gem)" category as title + add_category_to_output("Debugging (from debug.gem)", [], output, longest_cmd_name_length) output.puts DEBUGGER__.help end diff --git a/lib/irb/command/subirb.rb b/lib/irb/command/subirb.rb index 138d61c930..85af28c1a5 100644 --- a/lib/irb/command/subirb.rb +++ b/lib/irb/command/subirb.rb @@ -49,6 +49,7 @@ module IRB extend_irb_context IRB.irb(nil, *obj) + puts IRB.JobManager.inspect end end @@ -65,7 +66,7 @@ module IRB end extend_irb_context - IRB.JobManager + puts IRB.JobManager.inspect end end @@ -90,6 +91,7 @@ module IRB raise CommandArgumentError.new("Please specify the id of target IRB job (listed in the `jobs` command).") unless key IRB.JobManager.switch(key) + puts IRB.JobManager.inspect end end @@ -112,6 +114,7 @@ module IRB extend_irb_context IRB.JobManager.kill(*keys) + puts IRB.JobManager.inspect end end end diff --git a/lib/irb/completion.rb b/lib/irb/completion.rb index 8a1df11561..a3d89373c3 100644 --- a/lib/irb/completion.rb +++ b/lib/irb/completion.rb @@ -88,7 +88,7 @@ module IRB def command_completions(preposing, target) if preposing.empty? && !target.empty? - IRB::ExtendCommandBundle.command_names.select { _1.start_with?(target) } + IRB::Command.command_names.select { _1.start_with?(target) } else [] end diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 836b8d2625..aafce7aade 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -73,11 +73,12 @@ module IRB self.prompt_mode = IRB.conf[:PROMPT_MODE] - if IRB.conf[:SINGLE_IRB] or !defined?(IRB::JobManager) - @irb_name = IRB.conf[:IRB_NAME] - else - @irb_name = IRB.conf[:IRB_NAME]+"#"+IRB.JobManager.n_jobs.to_s + @irb_name = IRB.conf[:IRB_NAME] + + unless IRB.conf[:SINGLE_IRB] or !defined?(IRB::JobManager) + @irb_name = @irb_name + "#" + IRB.JobManager.n_jobs.to_s end + self.irb_path = "(" + @irb_name + ")" case input_method @@ -85,7 +86,7 @@ module IRB @io = nil case use_multiline? when nil - if STDIN.tty? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline? + if term_interactive? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline? # Both of multiline mode and singleline mode aren't specified. @io = RelineInputMethod.new(build_completor) else @@ -99,7 +100,7 @@ module IRB unless @io case use_singleline? when nil - if (defined?(ReadlineInputMethod) && STDIN.tty? && + if (defined?(ReadlineInputMethod) && term_interactive? && IRB.conf[:PROMPT_MODE] != :INF_RUBY) @io = ReadlineInputMethod.new else @@ -151,6 +152,11 @@ module IRB @command_aliases = @user_aliases.merge(KEYWORD_ALIASES) end + private def term_interactive? + return true if ENV['TEST_IRB_FORCE_INTERACTIVE'] + STDIN.tty? && ENV['TERM'] != 'dumb' + 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 @@ -587,18 +593,23 @@ module IRB def evaluate(statement, line_no) # :nodoc: @line_no = line_no - result = nil case statement when Statement::EmptyInput return when Statement::Expression result = evaluate_expression(statement.code, line_no) + set_last_value(result) when Statement::Command - result = statement.command_class.execute(self, statement.arg) + statement.command_class.execute(self, statement.arg) + set_last_value(nil) end - set_last_value(result) + nil + end + + def from_binding? + @irb.from_binding end def evaluate_expression(code, line_no) # :nodoc: diff --git a/lib/irb/default_commands.rb b/lib/irb/default_commands.rb index d680655fee..1bbc68efa7 100644 --- a/lib/irb/default_commands.rb +++ b/lib/irb/default_commands.rb @@ -30,35 +30,88 @@ require_relative "command/whereami" require_relative "command/history" module IRB - ExtendCommand = Command - - # Installs the default irb extensions command bundle. - module ExtendCommandBundle - # See #install_alias_method. + module Command NO_OVERRIDE = 0 - # See #install_alias_method. OVERRIDE_PRIVATE_ONLY = 0x01 - # See #install_alias_method. OVERRIDE_ALL = 0x02 - Command._register_with_aliases(:irb_context, Command::Context, - [ - [:context, NO_OVERRIDE], - [:conf, NO_OVERRIDE], - ], + class << self + # 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.to_sym] = [command_class, aliases] + end + + def all_commands_info + user_aliases = IRB.CurrentContext.command_aliases.each_with_object({}) do |(alias_name, target), result| + result[target] ||= [] + result[target] << alias_name + end + + commands.map do |command_name, (command_class, aliases)| + aliases = aliases.map { |a| a.first } + + if additional_aliases = user_aliases[command_name] + aliases += additional_aliases + end + + display_name = aliases.shift || command_name + { + display_name: display_name, + description: command_class.description, + category: command_class.category + } + end + end + + def command_override_policies + @@command_override_policies ||= commands.flat_map do |cmd_name, (cmd_class, aliases)| + [[cmd_name, OVERRIDE_ALL]] + aliases + end.to_h + end + + def execute_as_command?(name, public_method:, private_method:) + case command_override_policies[name] + when OVERRIDE_ALL + true + when OVERRIDE_PRIVATE_ONLY + !public_method + when NO_OVERRIDE + !public_method && !private_method + end + end + + def command_names + command_override_policies.keys.map(&:to_s) + end + + # Convert a command name to its implementation class if such command exists + def load_command(command) + command = command.to_sym + commands.each do |command_name, (command_class, aliases)| + if command_name == command || aliases.any? { |alias_name, _| alias_name == command } + return command_class + end + end + nil + end + end + + _register_with_aliases(:irb_context, Command::Context, + [:context, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_exit, Command::Exit, + _register_with_aliases(:irb_exit, Command::Exit, [:exit, OVERRIDE_PRIVATE_ONLY], [:quit, OVERRIDE_PRIVATE_ONLY], [:irb_quit, OVERRIDE_PRIVATE_ONLY] ) - Command._register_with_aliases(:irb_exit!, Command::ForceExit, + _register_with_aliases(:irb_exit!, Command::ForceExit, [:exit!, OVERRIDE_PRIVATE_ONLY] ) - Command._register_with_aliases(:irb_current_working_workspace, Command::CurrentWorkingWorkspace, + _register_with_aliases(:irb_current_working_workspace, Command::CurrentWorkingWorkspace, [:cwws, NO_OVERRIDE], [:pwws, NO_OVERRIDE], [:irb_print_working_workspace, OVERRIDE_ALL], @@ -70,7 +123,7 @@ module IRB [:irb_pwb, OVERRIDE_ALL], ) - Command._register_with_aliases(:irb_change_workspace, Command::ChangeWorkspace, + _register_with_aliases(:irb_change_workspace, Command::ChangeWorkspace, [:chws, NO_OVERRIDE], [:cws, NO_OVERRIDE], [:irb_chws, OVERRIDE_ALL], @@ -80,13 +133,13 @@ module IRB [:cb, NO_OVERRIDE], ) - Command._register_with_aliases(:irb_workspaces, Command::Workspaces, + _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, + _register_with_aliases(:irb_push_workspace, Command::PushWorkspace, [:pushws, NO_OVERRIDE], [:irb_pushws, OVERRIDE_ALL], [:irb_push_binding, OVERRIDE_ALL], @@ -94,7 +147,7 @@ module IRB [:pushb, NO_OVERRIDE], ) - Command._register_with_aliases(:irb_pop_workspace, Command::PopWorkspace, + _register_with_aliases(:irb_pop_workspace, Command::PopWorkspace, [:popws, NO_OVERRIDE], [:irb_popws, OVERRIDE_ALL], [:irb_pop_binding, OVERRIDE_ALL], @@ -102,140 +155,98 @@ module IRB [: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, + _register_with_aliases(:irb_load, Command::Load) + _register_with_aliases(:irb_require, Command::Require) + _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, + _register_with_aliases(:irb, Command::IrbCommand) + _register_with_aliases(:irb_jobs, Command::Jobs, [:jobs, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_fg, Command::Foreground, + _register_with_aliases(:irb_fg, Command::Foreground, [:fg, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_kill, Command::Kill, + _register_with_aliases(:irb_kill, Command::Kill, [:kill, OVERRIDE_PRIVATE_ONLY] ) - Command._register_with_aliases(:irb_debug, Command::Debug, + _register_with_aliases(:irb_debug, Command::Debug, [:debug, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_edit, Command::Edit, + _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, + _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] ) - Command._register_with_aliases(:irb_step, Command::Step, + _register_with_aliases(:irb_step, Command::Step, [:step, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_continue, Command::Continue, + _register_with_aliases(:irb_continue, Command::Continue, [:continue, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_finish, Command::Finish, + _register_with_aliases(:irb_finish, Command::Finish, [:finish, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_backtrace, Command::Backtrace, + _register_with_aliases(:irb_backtrace, Command::Backtrace, [:backtrace, NO_OVERRIDE], [:bt, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_debug_info, Command::Info, + _register_with_aliases(:irb_debug_info, Command::Info, [:info, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_help, Command::Help, + _register_with_aliases(:irb_help, Command::Help, [:help, NO_OVERRIDE], [:show_cmds, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_show_doc, Command::ShowDoc, + _register_with_aliases(:irb_show_doc, Command::ShowDoc, [:show_doc, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_info, Command::IrbInfo) + _register_with_aliases(:irb_info, Command::IrbInfo) - Command._register_with_aliases(:irb_ls, Command::Ls, + _register_with_aliases(:irb_ls, Command::Ls, [:ls, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_measure, Command::Measure, + _register_with_aliases(:irb_measure, Command::Measure, [:measure, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_show_source, Command::ShowSource, + _register_with_aliases(:irb_show_source, Command::ShowSource, [:show_source, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_whereami, Command::Whereami, + _register_with_aliases(:irb_whereami, Command::Whereami, [:whereami, NO_OVERRIDE] ) - Command._register_with_aliases(:irb_history, Command::History, + _register_with_aliases(:irb_history, Command::History, [:history, NO_OVERRIDE], [:hist, NO_OVERRIDE] ) + end - def self.all_commands_info - user_aliases = IRB.CurrentContext.command_aliases.each_with_object({}) do |(alias_name, target), result| - result[target] ||= [] - result[target] << alias_name - end - - Command.commands.map do |command_name, (command_class, aliases)| - aliases = aliases.map { |a| a.first } - - if additional_aliases = user_aliases[command_name] - aliases += additional_aliases - end - - display_name = aliases.shift || command_name - { - display_name: display_name, - description: command_class.description, - category: command_class.category - } - end - end - - def self.command_override_policies - @@command_override_policies ||= Command.commands.flat_map do |cmd_name, (cmd_class, aliases)| - [[cmd_name, OVERRIDE_ALL]] + aliases - end.to_h - end - - def self.execute_as_command?(name, public_method:, private_method:) - case command_override_policies[name] - when OVERRIDE_ALL - true - when OVERRIDE_PRIVATE_ONLY - !public_method - when NO_OVERRIDE - !public_method && !private_method - end - end - - def self.command_names - command_override_policies.keys.map(&:to_s) - end + ExtendCommand = Command - # Convert a command name to its implementation class if such command exists - def self.load_command(command) - command = command.to_sym - Command.commands.each do |command_name, (command_class, aliases)| - if command_name == command || aliases.any? { |alias_name, _| alias_name == command } - return command_class - end - end - nil - end + # 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. @EXTEND_COMMANDS = [] @@ -243,7 +254,7 @@ module IRB # Drepcated. Use Command.regiser instead. def self.def_extend_command(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 diff --git a/lib/irb/helper_method.rb b/lib/irb/helper_method.rb new file mode 100644 index 0000000000..f1f6fff915 --- /dev/null +++ b/lib/irb/helper_method.rb @@ -0,0 +1,29 @@ +require_relative "helper_method/base" + +module IRB + module HelperMethod + @helper_methods = {} + + class << self + attr_reader :helper_methods + + def register(name, helper_class) + @helper_methods[name] = helper_class + + if defined?(HelpersContainer) + HelpersContainer.install_helper_methods + end + end + + def all_helper_methods_info + @helper_methods.map do |name, helper_class| + { display_name: name, description: helper_class.description } + end + end + end + + # Default helper_methods + require_relative "helper_method/conf" + register(:conf, HelperMethod::Conf) + end +end diff --git a/lib/irb/helper_method/base.rb b/lib/irb/helper_method/base.rb new file mode 100644 index 0000000000..a68001ed28 --- /dev/null +++ b/lib/irb/helper_method/base.rb @@ -0,0 +1,16 @@ +require "singleton" + +module IRB + module HelperMethod + class Base + include Singleton + + class << self + def description(description = nil) + @description = description if description + @description + end + end + end + end +end diff --git a/lib/irb/helper_method/conf.rb b/lib/irb/helper_method/conf.rb new file mode 100644 index 0000000000..718ed279c0 --- /dev/null +++ b/lib/irb/helper_method/conf.rb @@ -0,0 +1,11 @@ +module IRB + module HelperMethod + class Conf < Base + description "Returns the current IRB context." + + def execute + IRB.CurrentContext + end + end + end +end diff --git a/lib/irb/init.rb b/lib/irb/init.rb index 355047519c..7dc08912ef 100644 --- a/lib/irb/init.rb +++ b/lib/irb/init.rb @@ -52,6 +52,7 @@ module IRB # :nodoc: IRB.init_error IRB.parse_opts(argv: argv) IRB.run_config + IRB.validate_config IRB.load_modules unless @CONF[:PROMPT][@CONF[:PROMPT_MODE]] @@ -427,6 +428,40 @@ module IRB # :nodoc: @irbrc_files end + def IRB.validate_config + conf[:IRB_NAME] = conf[:IRB_NAME].to_s + + irb_rc = conf[:IRB_RC] + unless irb_rc.nil? || irb_rc.respond_to?(:call) + raise_validation_error "IRB.conf[:IRB_RC] should be a callable object. Got #{irb_rc.inspect}." + end + + back_trace_limit = conf[:BACK_TRACE_LIMIT] + unless back_trace_limit.is_a?(Integer) + raise_validation_error "IRB.conf[:BACK_TRACE_LIMIT] should be an integer. Got #{back_trace_limit.inspect}." + end + + prompt = conf[:PROMPT] + unless prompt.is_a?(Hash) + msg = "IRB.conf[:PROMPT] should be a Hash. Got #{prompt.inspect}." + + if prompt.is_a?(Symbol) + msg += " Did you mean to set `IRB.conf[:PROMPT_MODE]`?" + end + + raise_validation_error msg + end + + eval_history = conf[:EVAL_HISTORY] + unless eval_history.nil? || eval_history.is_a?(Integer) + raise_validation_error "IRB.conf[:EVAL_HISTORY] should be an integer. Got #{eval_history.inspect}." + end + end + + def IRB.raise_validation_error(msg) + raise TypeError, msg, @irbrc_files + end + # loading modules def IRB.load_modules for m in @CONF[:LOAD_MODULES] diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index e5adb350e8..684527edc4 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -67,6 +67,7 @@ module IRB # # See IO#gets for more information. def gets + puts if @stdout.tty? # workaround for debug compatibility test print @prompt line = @stdin.gets @line[@line_no += 1] = line diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index cfe36be83f..f6ac7f0f5f 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -219,28 +219,7 @@ module IRB :unrecoverable_error rescue SyntaxError => e case e.message - when /unterminated (?:string|regexp) meets end of file/ - # "unterminated regexp meets end of file" - # - # example: - # / - # - # "unterminated string meets end of file" - # - # example: - # ' - return :recoverable_error - when /syntax error, unexpected end-of-input/ - # "syntax error, unexpected end-of-input, expecting keyword_end" - # - # example: - # if true - # hoge - # if false - # fuga - # end - return :recoverable_error - when /syntax error, unexpected keyword_end/ + when /unexpected keyword_end/ # "syntax error, unexpected keyword_end" # # example: @@ -250,7 +229,7 @@ module IRB # example: # end return :unrecoverable_error - when /syntax error, unexpected '\.'/ + when /unexpected '\.'/ # "syntax error, unexpected '.'" # # example: @@ -262,6 +241,27 @@ module IRB # example: # method / f / return :unrecoverable_error + when /unterminated (?:string|regexp) meets end of file/ + # "unterminated regexp meets end of file" + # + # example: + # / + # + # "unterminated string meets end of file" + # + # example: + # ' + return :recoverable_error + when /unexpected end-of-input/ + # "syntax error, unexpected end-of-input, expecting keyword_end" + # + # example: + # if true + # hoge + # if false + # fuga + # end + return :recoverable_error else return :other_error end diff --git a/lib/irb/version.rb b/lib/irb/version.rb index 9a7b12766b..c41917329c 100644 --- a/lib/irb/version.rb +++ b/lib/irb/version.rb @@ -5,7 +5,7 @@ # module IRB # :nodoc: - VERSION = "1.12.0" + VERSION = "1.13.1" @RELEASE_VERSION = VERSION - @LAST_UPDATE_DATE = "2024-03-06" + @LAST_UPDATE_DATE = "2024-05-05" end diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index 1490f7b478..d24d1cc38d 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -6,6 +6,8 @@ require "delegate" +require_relative "helper_method" + IRB::TOPLEVEL_BINDING = binding module IRB # :nodoc: class WorkSpace @@ -109,9 +111,9 @@ EOF attr_reader :main def load_helper_methods_to_main - if !(class<<main;ancestors;end).include?(ExtendCommandBundle) - main.extend ExtendCommandBundle - end + ancestors = class<<main;ancestors;end + main.extend ExtendCommandBundle if !ancestors.include?(ExtendCommandBundle) + main.extend HelpersContainer if !ancestors.include?(HelpersContainer) end # Evaluate the given +statements+ within the context of this workspace. @@ -172,4 +174,16 @@ EOF "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}#{Color.clear}\n" end end + + module HelpersContainer + def self.install_helper_methods + HelperMethod.helper_methods.each do |name, helper_method_class| + define_method name do |*args, **opts, &block| + helper_method_class.instance.execute(*args, **opts, &block) + end unless method_defined?(name) + end + end + + install_helper_methods + end end |