Skip to content

Commit

Permalink
Use gem repl_type_completor, remove type_completion implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Nov 28, 2023
1 parent eec1329 commit 2fff82e
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 3,380 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ gem "test-unit-ruby-core"
gem "debug", github: "ruby/debug"

if RUBY_VERSION >= "3.0.0"
gem "rbs"
gem "prism", ">= 0.18.0"
# TODO: use released version
gem "repl_type_completor", github: "ruby/repl_type_completor"
end
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,26 +237,26 @@ However, there are also some limitations to be aware of:

## Type Based Completion

IRB's default completion `IRB::RegexpCompletor` uses Regexp. IRB has another experimental completion `IRB::TypeCompletion` that uses type analysis.
IRB's default completion `IRB::RegexpCompletor` uses Regexp. IRB has another experimental completion `IRB::TypeCompletor` that uses type analysis.

### How to Enable IRB::TypeCompletion
### How to Enable IRB::TypeCompletor

To enable IRB::TypeCompletion, run IRB with `--type-completor` option
To enable IRB::TypeCompletor, 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
```
You also need `gem prism` and `gem rbs` to use this feature.
You also need `gem repl_type_completor` to use this feature.

To check if it's enabled, type `irb_info` into IRB and see the `Completion` section.
```
irb(main):001> irb_info
...
# Enabled
Completion: Autocomplete, TypeCompletion::Completor(Prism: 0.17.1, RBS: 3.3.0)
Completion: Autocomplete, ReplTypeCompletor: 0.1.0, Prism: 0.18.0, RBS: 3.3.0
# Not enabled
Completion: Autocomplete, RegexpCompletor
...
Expand All @@ -265,7 +265,7 @@ If you have `sig/` directory or `rbs_collection.lock.yaml` in current directory,

### Advantage over Default IRB::RegexpCompletor

IRB::TypeCompletion can autocomplete chained methods, block parameters and more if type information is available.
IRB::TypeCompletor can autocomplete chained methods, block parameters and more if type information is available.
These are some examples IRB::RegexpCompletor cannot complete.

```ruby
Expand All @@ -287,11 +287,11 @@ As a trade-off, completion calculation takes more time than IRB::RegexpCompletor

### Difference between Steep's Completion

Compared with Steep, IRB::TypeCompletion has some difference and limitations.
Compared with Steep, IRB::TypeCompletor has some difference and limitations.
```ruby
[0, 'a'].sample.
# Steep completes intersection of Integer methods and String methods
# IRB::TypeCompletion completes both Integer and String methods
# IRB::TypeCompletor completes both Integer and String methods
```

Some features like type narrowing is not implemented.
Expand All @@ -301,7 +301,7 @@ def f(arg = [0, 'a'].sample)
arg. # Completes both Integer and String methods
```

Unlike other static type checker, IRB::TypeCompletion uses runtime information to provide better completion.
Unlike other static type checker, IRB::TypeCompletor uses runtime information to provide better completion.
```ruby
irb(main):001> a = [1]
=> [1]
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Rake::TestTask.new(:test) do |t|
t.libs << "test" << "test/lib"
t.libs << "lib"
t.ruby_opts << "-rhelper"
t.test_files = FileList["test/irb/test_*.rb", "test/irb/type_completion/test_*.rb"]
t.test_files = FileList["test/irb/test_*.rb"]
end

# To make sure they have been correctly setup for Ruby CI.
desc "Run each irb test file in isolation."
task :test_in_isolation do
failed = false

FileList["test/irb/test_*.rb", "test/irb/type_completion/test_*.rb"].each do |test_file|
FileList["test/irb/test_*.rb"].each do |test_file|
ENV["TEST"] = test_file
begin
Rake::Task["test"].execute
Expand Down
23 changes: 23 additions & 0 deletions lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ def retrieve_files_to_require_relative_from_current_dir
end
end

class TypeCompletor < BaseCompletor # :nodoc:
def initialize(source_file = nil)
require 'repl_type_completor'
ReplTypeCompletor.preload_rbs
@source_file = source_file
end

def inspect
ReplTypeCompletor.info
end

def completion_candidates(preposing, target, _postposing, bind:)
result = ReplTypeCompletor.analyze(preposing + target, binding: bind, filename: @source_file)
return [] unless result
result.completion_candidates.map { target + _1 }
end

def doc_namespace(preposing, matched, _postposing, bind:)
result = ReplTypeCompletor.analyze(preposing + matched, binding: bind, filename: @source_file)
result&.doc_namespace('')
end
end

class RegexpCompletor < BaseCompletor # :nodoc:
using Module.new {
refine ::Binding do
Expand Down
19 changes: 6 additions & 13 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,19 @@ def initialize(irb, workspace = nil, input_method = nil)
RegexpCompletor.new
end

TYPE_COMPLETION_REQUIRED_PRISM_VERSION = '0.18.0'

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'
if RUBY_ENGINE == 'truffleruby'
# Avoid SynatxError. truffleruby does not support endless method definition yet.
warn 'TypeCompletor is not supported on TruffleRuby yet'
return
end

begin
require 'prism'
TypeCompletor.new(irb_path)
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}"
warn "TypeCompletor requires `gem repl_type_completor`: #{e.message}"
return
end
require 'irb/type_completion/completor'
TypeCompletion::Types.preload_in_thread
TypeCompletion::Completor.new
end

def save_history=(val)
Expand Down
241 changes: 0 additions & 241 deletions lib/irb/type_completion/completor.rb

This file was deleted.

Loading

0 comments on commit 2fff82e

Please sign in to comment.