From 38db49891e90200bc24a01291d9aeff6fd8ecc11 Mon Sep 17 00:00:00 2001 From: tompng Date: Wed, 8 Nov 2023 21:30:24 +0900 Subject: [PATCH 1/2] Add command line option to select which completor to use --- README.md | 6 +++++- lib/irb/init.rb | 4 ++++ lib/irb/lc/help-message | 3 +++ lib/irb/lc/ja/help-message | 3 +++ man/irb.1 | 7 +++++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4896c7596..10c2ab01c 100644 --- a/README.md +++ b/README.md @@ -241,7 +241,11 @@ IRB's default completion `IRB::RegexpCompletor` uses Regexp. IRB has another exp ### How to Enable IRB::TypeCompletion -To enable IRB::TypeCompletion, write the code below to IRB's rc-file. +To enable IRB::TypeCompletion, run IRB with `--type-completor` option +``` +$ irb --type-completor +``` +Or write the code below to IRB's rc-file. ```ruby IRB.conf[:COMPLETOR] = :type # default is :regexp ``` diff --git a/lib/irb/init.rb b/lib/irb/init.rb index e9111974f..470903b45 100644 --- a/lib/irb/init.rb +++ b/lib/irb/init.rb @@ -330,6 +330,10 @@ def IRB.parse_opts(argv: ::ARGV) @CONF[:USE_AUTOCOMPLETE] = true when "--noautocomplete" @CONF[:USE_AUTOCOMPLETE] = false + when "--regexp-completor" + @CONF[:COMPLETOR] = :regexp + when "--type-completor" + @CONF[:COMPLETOR] = :type when /^--prompt-mode(?:=(.+))?/, /^--prompt(?:=(.+))?/ opt = $1 || argv.shift prompt_mode = opt.upcase.tr("-", "_").intern diff --git a/lib/irb/lc/help-message b/lib/irb/lc/help-message index 7e66100be..c7846b755 100644 --- a/lib/irb/lc/help-message +++ b/lib/irb/lc/help-message @@ -30,6 +30,9 @@ Usage: irb.rb [options] [programfile] [arguments] --nocolorize Don't use color-highlighting. --autocomplete Use auto-completion (default). --noautocomplete Don't use auto-completion. + --regexp-completor + Use regexp based completion (default). + --type-completor Use type based completion. --prompt prompt-mode, --prompt-mode prompt-mode Set prompt mode. Pre-defined prompt modes are: 'default', 'classic', 'simple', 'inf-ruby', 'xmp', 'null'. diff --git a/lib/irb/lc/ja/help-message b/lib/irb/lc/ja/help-message index 1c15d331e..cec339cf2 100644 --- a/lib/irb/lc/ja/help-message +++ b/lib/irb/lc/ja/help-message @@ -21,6 +21,9 @@ Usage: irb.rb [options] [programfile] [arguments] --nocolorize 色付けを利用しない. --autocomplete オートコンプリートを利用する. --noautocomplete オートコンプリートを利用しない. + --regexp-completor + 補完に正規表現を利用する. + --type-completor 補完に型情報を利用する. --prompt prompt-mode/--prompt-mode prompt-mode プロンプトモードを切替えます. 現在定義されているプ ロンプトモードは, default, simple, xmp, inf-rubyが diff --git a/man/irb.1 b/man/irb.1 index c589c99c7..93ef9b8f6 100644 --- a/man/irb.1 +++ b/man/irb.1 @@ -140,6 +140,13 @@ Use autocompletion. Don't use autocompletion. .Pp .Pp +.It Fl -regexp-completor +Use regexp based completion. +.Pp +.It Fl -type-completor +Use type based completion. +.Pp +.Pp .It Fl -verbose Show details. .Pp From b22a3e9bd951166ebafb125ace6cca47ad390c73 Mon Sep 17 00:00:00 2001 From: tompng Date: Thu, 9 Nov 2023 22:01:36 +0900 Subject: [PATCH 2/2] Add test for completor argv --- test/irb/test_init.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/irb/test_init.rb b/test/irb/test_init.rb index 9f3f2a109..e330cc5e8 100644 --- a/test/irb/test_init.rb +++ b/test/irb/test_init.rb @@ -120,6 +120,22 @@ def test_use_autocomplete_environment_variable IRB.conf[:USE_AUTOCOMPLETE] = orig_use_autocomplete_conf end + def test_completor_setup_with_argv + orig_completor_conf = IRB.conf[:COMPLETOR] + + # Default is :regexp + IRB.setup(__FILE__, argv: []) + assert_equal :regexp, IRB.conf[:COMPLETOR] + + IRB.setup(__FILE__, argv: ['--type-completor']) + assert_equal :type, IRB.conf[:COMPLETOR] + + IRB.setup(__FILE__, argv: ['--regexp-completor']) + assert_equal :regexp, IRB.conf[:COMPLETOR] + ensure + IRB.conf[:COMPLETOR] = orig_completor_conf + end + def test_noscript argv = %w[--noscript -- -f] IRB.setup(eval("__FILE__"), argv: argv)