From ce8fa6857c6f43898d5d51f34bfbebf472bc26d0 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sun, 12 Jan 2025 20:59:32 +0800 Subject: [PATCH] Group private methods together in `IRB::Context` (#1064) This makes them easier to find and matches the convention of the codebase. --- lib/irb/context.rb | 90 +++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 8d6545224..d87e8451e 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -155,11 +155,6 @@ def initialize(irb, workspace = nil, input_method = nil) @command_aliases = IRB.conf[:COMMAND_ALIASES].dup end - private def term_interactive? - return true if ENV['TEST_IRB_FORCE_INTERACTIVE'] - STDIN.tty? && ENV['TERM'] != 'dumb' - end - def use_tracer=(val) require_relative "ext/tracer" if val IRB.conf[:USE_TRACER] = val @@ -177,45 +172,6 @@ def use_loader=(val) __send__(__method__, val) end - private def build_completor - completor_type = IRB.conf[:COMPLETOR] - - # Gem repl_type_completor is added to bundled gems in Ruby 3.4. - # Use :type as default completor only in Ruby 3.4 or later. - verbose = !!completor_type - completor_type ||= RUBY_VERSION >= '3.4' ? :type : :regexp - - case completor_type - when :regexp - return RegexpCompletor.new - when :type - completor = build_type_completor(verbose: verbose) - return completor if completor - else - warn "Invalid value for IRB.conf[:COMPLETOR]: #{completor_type}" - end - # Fallback to RegexpCompletor - RegexpCompletor.new - end - - private def build_type_completor(verbose:) - if RUBY_ENGINE == 'truffleruby' - # Avoid SyntaxError. truffleruby does not support endless method definition yet. - warn 'TypeCompletor is not supported on TruffleRuby yet' if verbose - return - end - - begin - require 'repl_type_completor' - rescue LoadError => e - warn "TypeCompletor requires `gem repl_type_completor`: #{e.message}" if verbose - return - end - - ReplTypeCompletor.preload_rbs - TypeCompletor.new(self) - end - def save_history=(val) IRB.conf[:SAVE_HISTORY] = val end @@ -739,5 +695,51 @@ def safe_method_call_on_main(method_name) main_object = main Object === main_object ? main_object.__send__(method_name) : Object.instance_method(method_name).bind_call(main_object) end + + private + + def term_interactive? + return true if ENV['TEST_IRB_FORCE_INTERACTIVE'] + STDIN.tty? && ENV['TERM'] != 'dumb' + end + + def build_completor + completor_type = IRB.conf[:COMPLETOR] + + # Gem repl_type_completor is added to bundled gems in Ruby 3.4. + # Use :type as default completor only in Ruby 3.4 or later. + verbose = !!completor_type + completor_type ||= RUBY_VERSION >= '3.4' ? :type : :regexp + + case completor_type + when :regexp + return RegexpCompletor.new + when :type + completor = build_type_completor(verbose: verbose) + return completor if completor + else + warn "Invalid value for IRB.conf[:COMPLETOR]: #{completor_type}" + end + # Fallback to RegexpCompletor + RegexpCompletor.new + end + + def build_type_completor(verbose:) + if RUBY_ENGINE == 'truffleruby' + # Avoid SyntaxError. truffleruby does not support endless method definition yet. + warn 'TypeCompletor is not supported on TruffleRuby yet' if verbose + return + end + + begin + require 'repl_type_completor' + rescue LoadError => e + warn "TypeCompletor requires `gem repl_type_completor`: #{e.message}" if verbose + return + end + + ReplTypeCompletor.preload_rbs + TypeCompletor.new(self) + end end end