From 032f6da25fe7152bd52e47342fd13ff6d894e171 Mon Sep 17 00:00:00 2001 From: ima1zumi <52617472+ima1zumi@users.noreply.github.com> Date: Tue, 21 Nov 2023 09:04:36 +0900 Subject: [PATCH] Enable Setting Completer Type through `IRB_COMPLETOR` (#771) I propose introducing the capability to set the IRB completion kinds via an environment variable, specifically `IRB_COMPLETOR=type`. This feature aims to enhance the Rails console experience by allowing Rails users to specify their preferred completion more conveniently. Currently, when using the Rails console, there's no straightforward way to globally set the type completion across a Rails application repository. It's possible to configure this setting by placing a `.irbrc` file at the project root. However, using a .irbrc file is not ideal as it allows for broad configurations and can potentially affect the production environment. My suggestion focuses on allowing users to set the completion to 'type' in a minimal. This enhancement would be particularly beneficial for teams writing RBS in their Rails applications. This type completer, integrated with RBS, would enhance completion accuracy, improving the Rails console experience. --- README.md | 1 + lib/irb/init.rb | 2 +- test/irb/test_init.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a6cd6b3b4..483fb9a63 100644 --- a/README.md +++ b/README.md @@ -314,6 +314,7 @@ irb(main):002> a.first. # Completes Integer methods - `NO_COLOR`: Assigning a value to it disables IRB's colorization. - `IRB_USE_AUTOCOMPLETE`: Setting it to `false` disables IRB's autocompletion. +- `IRB_COMPLETOR`: Configures IRB's auto-completion behavior, allowing settings for either `regexp` or `type`. - `VISUAL`: Its value would be used to open files by the `edit` command. - `EDITOR`: Its value would be used to open files by the `edit` command if `VISUAL` is unset. - `IRBRC`: The file specified would be evaluated as IRB's rc-file. diff --git a/lib/irb/init.rb b/lib/irb/init.rb index 470903b45..4df285ce6 100644 --- a/lib/irb/init.rb +++ b/lib/irb/init.rb @@ -76,7 +76,7 @@ def IRB.init_config(ap_path) @CONF[:USE_SINGLELINE] = false unless defined?(ReadlineInputMethod) @CONF[:USE_COLORIZE] = (nc = ENV['NO_COLOR']).nil? || nc.empty? @CONF[:USE_AUTOCOMPLETE] = ENV.fetch("IRB_USE_AUTOCOMPLETE", "true") != "false" - @CONF[:COMPLETOR] = :regexp + @CONF[:COMPLETOR] = ENV.fetch("IRB_COMPLETOR", "regexp").to_sym @CONF[:INSPECT_MODE] = true @CONF[:USE_TRACER] = false @CONF[:USE_LOADER] = false diff --git a/test/irb/test_init.rb b/test/irb/test_init.rb index e330cc5e8..b6a8f5529 100644 --- a/test/irb/test_init.rb +++ b/test/irb/test_init.rb @@ -120,6 +120,34 @@ def test_use_autocomplete_environment_variable IRB.conf[:USE_AUTOCOMPLETE] = orig_use_autocomplete_conf end + def test_completor_environment_variable + orig_use_autocomplete_env = ENV['IRB_COMPLETOR'] + orig_use_autocomplete_conf = IRB.conf[:COMPLETOR] + + ENV['IRB_COMPLETOR'] = nil + IRB.setup(__FILE__) + assert_equal(:regexp, IRB.conf[:COMPLETOR]) + + ENV['IRB_COMPLETOR'] = 'regexp' + IRB.setup(__FILE__) + assert_equal(:regexp, IRB.conf[:COMPLETOR]) + + ENV['IRB_COMPLETOR'] = 'type' + IRB.setup(__FILE__) + assert_equal(:type, IRB.conf[:COMPLETOR]) + + ENV['IRB_COMPLETOR'] = 'regexp' + IRB.setup(__FILE__, argv: ['--type-completor']) + assert_equal :type, IRB.conf[:COMPLETOR] + + ENV['IRB_COMPLETOR'] = 'type' + IRB.setup(__FILE__, argv: ['--regexp-completor']) + assert_equal :regexp, IRB.conf[:COMPLETOR] + ensure + ENV['IRB_COMPLETOR'] = orig_use_autocomplete_env + IRB.conf[:COMPLETOR] = orig_use_autocomplete_conf + end + def test_completor_setup_with_argv orig_completor_conf = IRB.conf[:COMPLETOR]