summaryrefslogtreecommitdiff
path: root/lib/irb/context.rb
diff options
context:
space:
mode:
authortomoya ishida <[email protected]>2023-11-08 11:46:24 +0900
committergit <[email protected]>2023-11-08 02:46:33 +0000
commite34401046566ad1938b1eec654a6bf69b1319102 (patch)
tree27515a6c6e783818906480910d27fab3c6e41e97 /lib/irb/context.rb
parent7ed37388fb9c0e85325b4e3db2ffbfca3f4179ad (diff)
[ruby/irb] Type based completion using Prism and RBS
(https://github.com/ruby/irb/pull/708) * Add completor using prism and rbs * Add TypeCompletion test * Switchable completors: RegexpCompletor and TypeCompletion::Completor * Add completion info to irb_info * Complete reserved words * Fix [*] (*) {**} and prism's change of KeywordParameterNode * Fix require, frozen_string_literal * Drop prism<=0.16.0 support * Add Completor.last_completion_error for debug report * Retrieve `self` and `Module.nesting` in more safe way * Support BasicObject * Handle lvar and ivar get exception correctly * Skip ivar reference test of non-self object in ruby < 3.2 * BaseScope to RootScope, move method objects constant under Methods * Remove unused Splat struct * Drop deeply nested array/hash type calculation from actual object. Now, calculation depth is 1 * Refactor loading rbs in test, change preload_in_thread not to cache Thread object * Use new option added in prism 0.17.1 to parse code with localvars * Add Prism version check and warn when :type completor cannot be enabled * build_type_completor should skip truffleruby (because endless method definition is not supported) https://github.com/ruby/irb/commit/1048c7ed7a
Diffstat (limited to 'lib/irb/context.rb')
-rw-r--r--lib/irb/context.rb41
1 files changed, 39 insertions, 2 deletions
diff --git a/lib/irb/context.rb b/lib/irb/context.rb
index a20510d73c..5dfe9d0d71 100644
--- a/lib/irb/context.rb
+++ b/lib/irb/context.rb
@@ -86,14 +86,14 @@ module IRB
when nil
if STDIN.tty? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline?
# Both of multiline mode and singleline mode aren't specified.
- @io = RelineInputMethod.new
+ @io = RelineInputMethod.new(build_completor)
else
@io = nil
end
when false
@io = nil
when true
- @io = RelineInputMethod.new
+ @io = RelineInputMethod.new(build_completor)
end
unless @io
case use_singleline?
@@ -149,6 +149,43 @@ module IRB
@command_aliases = IRB.conf[:COMMAND_ALIASES]
end
+ private def build_completor
+ completor_type = IRB.conf[:COMPLETOR]
+ case completor_type
+ when :regexp
+ return RegexpCompletor.new
+ when :type
+ completor = build_type_completor
+ return completor if completor
+ else
+ warn "Invalid value for IRB.conf[:COMPLETOR]: #{completor_type}"
+ end
+ # Fallback to RegexpCompletor
+ RegexpCompletor.new
+ end
+
+ TYPE_COMPLETION_REQUIRED_PRISM_VERSION = '0.17.1'
+
+ private def build_type_completor
+ unless Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0') && RUBY_ENGINE != 'truffleruby'
+ warn 'TypeCompletion requires RUBY_VERSION >= 3.0.0'
+ return
+ end
+ begin
+ require 'prism'
+ rescue LoadError => e
+ warn "TypeCompletion requires Prism: #{e.message}"
+ return
+ end
+ unless Gem::Version.new(Prism::VERSION) >= Gem::Version.new(TYPE_COMPLETION_REQUIRED_PRISM_VERSION)
+ warn "TypeCompletion requires Prism::VERSION >= #{TYPE_COMPLETION_REQUIRED_PRISM_VERSION}"
+ return
+ end
+ require 'irb/type_completion/completor'
+ TypeCompletion::Types.preload_in_thread
+ TypeCompletion::Completor.new
+ end
+
def save_history=(val)
IRB.conf[:SAVE_HISTORY] = val
end