Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

README section for IRB::TypeCompletion #746

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,75 @@ However, there are also some limitations to be aware of:
2. As IRB [doesn't currently support remote-connection](https://github.com/ruby/irb/issues/672), it can't be used with `debug.gem`'s remote debugging feature.
3. Access to the previous return value via the underscore `_` is not supported.

## Type Based Completion

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

### How to Enable IRB::TypeCompletion

To enable IRB::TypeCompletion, 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.

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)
# Not enabled
Completion: Autocomplete, RegexpCompletor
...
```
If you have `sig/` directory or `rbs_collection.lock.yaml` in current directory, IRB will load it.

### Advantage over Default IRB::RegexpCompletor

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

```ruby
irb(main):001> 'Ruby'.upcase.chars.s # Array methods (sample, select, shift, size)
```

```ruby
irb(main):001> 10.times.map(&:to_s).each do |s|
irb(main):002> s.up # String methods (upcase, upcase!, upto)
```

```ruby
irb(main):001> class User < ApplicationRecord
irb(main):002> def foo
irb(main):003> sa # save, save!
```

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.
```ruby
[0, 'a'].sample.
# Steep completes intersection of Integer methods and String methods
# IRB::TypeCompletion completes both Integer and String methods
```

Some features like type narrowing is not implemented.
```ruby
def f(arg = [0, 'a'].sample)
if arg.is_a?(String)
arg. # Completes both Integer and String methods
```

Unlike other static type checker, IRB::TypeCompletion uses runtime information to provide better completion.
```ruby
irb(main):001> a = [1]
=> [1]
irb(main):002> a.first. # Completes Integer methods
```

## Configuration

### Environment Variables
Expand Down