repl_type_completor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +26 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +37 -0
- data/lib/repl_type_completor/methods.rb +11 -0
- data/lib/repl_type_completor/require_paths.rb +75 -0
- data/lib/repl_type_completor/result.rb +145 -0
- data/lib/repl_type_completor/scope.rb +409 -0
- data/lib/repl_type_completor/type_analyzer.rb +1179 -0
- data/lib/repl_type_completor/types.rb +428 -0
- data/lib/repl_type_completor/version.rb +5 -0
- data/lib/repl_type_completor.rb +146 -0
- data/sig/repl_type_completor.rbs +19 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b1a82aeb5a01426335efe986d61dcb745051091bc6dedc93d332cf6bacaa0351
|
4
|
+
data.tar.gz: 6b669b71a1d01fb394e0bb2ec8776bb3d68ecade407ed6fbb36f87400cba4fae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 03b215d8f2123c52047513e321b30f8d32159f318ca233b10de48df5b53ba04223b979024afe8d44d7e2a0e6f78595d2b4c4354f23fa90e3aae4f7b5de83e4bf
|
7
|
+
data.tar.gz: 879d342d1b77f30ebdc328ce1a4dc2cc4dc60d7179c7d2af384ae236d4b7e830c410d3c78c8750c70f4ea06b6612417cae26d9b18deb5b11d609be29798f5c34
|
data/Gemfile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in repl_type_completor.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'rake', '~> 13.0'
|
9
|
+
gem 'test-unit'
|
10
|
+
gem 'test-unit-ruby-core'
|
11
|
+
|
12
|
+
if ENV['GEMFILE_PRISM_VERSION']
|
13
|
+
if ENV['GEMFILE_PRISM_VERSION'] == 'latest'
|
14
|
+
gem 'prism', github: 'ruby/prism'
|
15
|
+
else
|
16
|
+
gem 'prism', ENV['GEMFILE_PRISM_VERSION']
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
if ENV['GEMFILE_RBS_VERSION']
|
21
|
+
if ENV['GEMFILE_RBS_VERSION'] == 'latest'
|
22
|
+
gem 'rbs', github: 'ruby/rbs'
|
23
|
+
else
|
24
|
+
gem 'rbs', ENV['GEMFILE_RBS_VERSION']
|
25
|
+
end
|
26
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 tompng
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# ReplTypeCompletor
|
2
|
+
|
3
|
+
ReplTypeCompletor is a type based completor for REPL.
|
4
|
+
It uses RBS type information, performs static type analytics, uses dynamic runtime information from binding.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
9
|
+
|
10
|
+
Install the gem and add to the application's Gemfile by executing:
|
11
|
+
|
12
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
13
|
+
|
14
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
15
|
+
|
16
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Require the library
|
21
|
+
```ruby
|
22
|
+
require 'repl_type_completor'
|
23
|
+
```
|
24
|
+
|
25
|
+
Load RBS with one of these. It will load core library signatures, `./rbs_collection.yaml` and `./sig/**/*.rbs`.
|
26
|
+
```ruby
|
27
|
+
ReplTypeCompletor.preload_rbs # Recommended. Preload using thread
|
28
|
+
ReplTypeCompletor.load_rbs # Could take a seconds in large projects
|
29
|
+
```
|
30
|
+
|
31
|
+
Now you can get completion candidates.
|
32
|
+
```ruby
|
33
|
+
array = [1, 2, 3]
|
34
|
+
code_to_complete = 'array.map do str = _1.chr; str.up'
|
35
|
+
result = ReplTypeCompletor.analyze(code_to_complete, binding: binding, filename: __FILE__)
|
36
|
+
result.completion_candidates #=> ["case", "case!", "to"]
|
37
|
+
result.doc_namespace('case') #=> "String#upcase"
|
38
|
+
```
|
39
|
+
|
40
|
+
## Development
|
41
|
+
|
42
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
43
|
+
|
44
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/repl_type_completor.
|
49
|
+
|
50
|
+
When something is wrong, these methods will provide some debug information.
|
51
|
+
```ruby
|
52
|
+
ReplTypeCompletor.info
|
53
|
+
ReplTypeCompletor.rbs_load_started?
|
54
|
+
ReplTypeCompletor.rbs_loaded?
|
55
|
+
ReplTypeCompletor.rbs_load_error
|
56
|
+
ReplTypeCompletor.last_completion_error
|
57
|
+
ReplTypeCompletor.analyze(code_to_complete, binding: binding, filename: __FILE__)
|
58
|
+
```
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/repl_type_completor/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
# To make sure they have been correctly setup for Ruby CI.
|
13
|
+
desc "Run each repl_type_completor test file in isolation."
|
14
|
+
task :test_in_isolation do
|
15
|
+
failed = false
|
16
|
+
|
17
|
+
FileList["test/repl_type_completor/test_*.rb"].each do |test_file|
|
18
|
+
ENV["TEST"] = test_file
|
19
|
+
begin
|
20
|
+
Rake::Task["test"].execute
|
21
|
+
rescue => e
|
22
|
+
failed = true
|
23
|
+
msg = "Test '#{test_file}' failed when being executed in isolation. Please make sure 'rake test TEST=#{test_file}' passes."
|
24
|
+
separation_line = '=' * msg.length
|
25
|
+
|
26
|
+
puts <<~MSG
|
27
|
+
#{separation_line}
|
28
|
+
#{msg}
|
29
|
+
#{separation_line}
|
30
|
+
MSG
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
fail "Some tests failed when being executed in isolation" if failed
|
35
|
+
end
|
36
|
+
|
37
|
+
task default: %i[test test_in_isolation]
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ReplTypeCompletor
|
4
|
+
module Methods
|
5
|
+
OBJECT_SINGLETON_CLASS_METHOD = Object.instance_method(:singleton_class)
|
6
|
+
OBJECT_INSTANCE_VARIABLES_METHOD = Object.instance_method(:instance_variables)
|
7
|
+
OBJECT_INSTANCE_VARIABLE_GET_METHOD = Object.instance_method(:instance_variable_get)
|
8
|
+
OBJECT_CLASS_METHOD = Object.instance_method(:class)
|
9
|
+
MODULE_NAME_METHOD = Module.instance_method(:name)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ReplTypeCompletor
|
4
|
+
module RequirePaths
|
5
|
+
class << self
|
6
|
+
def require_completions(target_path)
|
7
|
+
*dir, target = target_path.split('/', -1)
|
8
|
+
target ||= ''
|
9
|
+
paths = with_cache [:require_completions, dir] do
|
10
|
+
gem_and_system_load_paths.flat_map do |load_path|
|
11
|
+
base_dir = File.absolute_path(File.join(load_path, *dir))
|
12
|
+
with_cache [:requireable_paths, base_dir] do
|
13
|
+
requireable_paths(base_dir)
|
14
|
+
end
|
15
|
+
end.sort
|
16
|
+
end
|
17
|
+
paths.filter_map do |path|
|
18
|
+
[*dir, path].join('/') if path.start_with?(target)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def require_relative_completions(target_path, source_file)
|
23
|
+
source_dir = source_file ? File.dirname(source_file) : Dir.pwd
|
24
|
+
*dir, target = target_path.split('/', -1)
|
25
|
+
target ||= ''
|
26
|
+
base_dir = File.absolute_path(File.join(source_dir, *dir))
|
27
|
+
paths = with_cache [:requireable_paths, base_dir] do
|
28
|
+
requireable_paths(base_dir)
|
29
|
+
end
|
30
|
+
paths.filter_map do |path|
|
31
|
+
[*dir, path].join('/') if path.start_with?(target)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def with_cache(key)
|
38
|
+
@cache ||= {}
|
39
|
+
@cache[key] ||= yield
|
40
|
+
end
|
41
|
+
|
42
|
+
def gem_paths
|
43
|
+
return [] unless defined?(Gem::Specification)
|
44
|
+
|
45
|
+
Gem::Specification.latest_specs(true).flat_map do |spec|
|
46
|
+
spec.require_paths.map do |path|
|
47
|
+
File.absolute_path?(path) ? path : File.join(spec.full_gem_path, path)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def gem_and_system_load_paths
|
53
|
+
@gem_and_system_load_paths ||= (gem_paths | $LOAD_PATH).filter_map do |path|
|
54
|
+
if path.respond_to?(:to_path)
|
55
|
+
path.to_path
|
56
|
+
else
|
57
|
+
String(path) rescue nil
|
58
|
+
end
|
59
|
+
end.sort
|
60
|
+
end
|
61
|
+
|
62
|
+
def requireable_paths(base_dir)
|
63
|
+
ext = ".{rb,#{RbConfig::CONFIG['DLEXT']}}"
|
64
|
+
ext_regexp = /\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/
|
65
|
+
files = Dir.glob(["*#{ext}", "*/*#{ext}"], base: base_dir).map { |p| p.sub(ext_regexp, '') }
|
66
|
+
dirs = Dir.glob('*/*', base: base_dir).filter_map do |path|
|
67
|
+
"#{path}/" if File.directory?(File.join(base_dir, path))
|
68
|
+
end
|
69
|
+
(files + dirs).sort
|
70
|
+
rescue Errno::EPERM
|
71
|
+
[]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'require_paths'
|
4
|
+
|
5
|
+
module ReplTypeCompletor
|
6
|
+
class Result
|
7
|
+
HIDDEN_METHODS = %w[Namespace TypeName] # defined by rbs, should be hidden
|
8
|
+
RESERVED_WORDS = %w[
|
9
|
+
__ENCODING__ __LINE__ __FILE__
|
10
|
+
BEGIN END
|
11
|
+
alias and
|
12
|
+
begin break
|
13
|
+
case class
|
14
|
+
def defined? do
|
15
|
+
else elsif end ensure
|
16
|
+
false for
|
17
|
+
if in
|
18
|
+
module
|
19
|
+
next nil not
|
20
|
+
or
|
21
|
+
redo rescue retry return
|
22
|
+
self super
|
23
|
+
then true
|
24
|
+
undef unless until
|
25
|
+
when while
|
26
|
+
yield
|
27
|
+
]
|
28
|
+
|
29
|
+
def initialize(analyze_result, binding, source_file)
|
30
|
+
@analyze_result = analyze_result
|
31
|
+
@binding = binding
|
32
|
+
@source_file = source_file
|
33
|
+
end
|
34
|
+
|
35
|
+
def completion_candidates
|
36
|
+
verbose, $VERBOSE = $VERBOSE, nil
|
37
|
+
candidates = case @analyze_result
|
38
|
+
in [:require, name]
|
39
|
+
RequirePaths.require_completions(name)
|
40
|
+
in [:require_relative, name]
|
41
|
+
RequirePaths.require_relative_completions(name, @source_file)
|
42
|
+
in [:call_or_const, name, type, self_call]
|
43
|
+
((self_call ? type.all_methods : type.methods).map(&:to_s) - HIDDEN_METHODS) | type.constants
|
44
|
+
in [:const, name, type, scope]
|
45
|
+
if type
|
46
|
+
scope_constants = type.types.flat_map do |t|
|
47
|
+
scope.table_module_constants(t.module_or_class) if t.is_a?(Types::SingletonType)
|
48
|
+
end
|
49
|
+
(scope_constants.compact | type.constants.map(&:to_s)).sort
|
50
|
+
else
|
51
|
+
scope.constants.sort | RESERVED_WORDS
|
52
|
+
end
|
53
|
+
in [:ivar, name, scope]
|
54
|
+
ivars = scope.instance_variables.sort
|
55
|
+
name == '@' ? ivars + scope.class_variables.sort : ivars
|
56
|
+
in [:cvar, name, scope]
|
57
|
+
scope.class_variables
|
58
|
+
in [:gvar, name, scope]
|
59
|
+
scope.global_variables
|
60
|
+
in [:symbol, name]
|
61
|
+
Symbol.all_symbols.map { _1.inspect[1..] }
|
62
|
+
in [:call, name, type, self_call]
|
63
|
+
(self_call ? type.all_methods : type.methods).map(&:to_s) - HIDDEN_METHODS
|
64
|
+
in [:lvar_or_method, name, scope]
|
65
|
+
scope.self_type.all_methods.map(&:to_s) | scope.local_variables | RESERVED_WORDS
|
66
|
+
else
|
67
|
+
[]
|
68
|
+
end
|
69
|
+
candidates.select { _1.start_with?(name) }.map { _1[name.size..] }
|
70
|
+
rescue Exception => e
|
71
|
+
ReplTypeCompletor.handle_exception(e)
|
72
|
+
[]
|
73
|
+
ensure
|
74
|
+
$VERBOSE = verbose
|
75
|
+
end
|
76
|
+
|
77
|
+
def doc_namespace(matched)
|
78
|
+
verbose, $VERBOSE = $VERBOSE, nil
|
79
|
+
case @analyze_result
|
80
|
+
in [:call_or_const, prefix, type, _self_call]
|
81
|
+
call_or_const_doc type, prefix + matched
|
82
|
+
in [:const, prefix, type, scope]
|
83
|
+
if type
|
84
|
+
call_or_const_doc type, prefix + matched
|
85
|
+
else
|
86
|
+
value_doc scope[prefix + matched]
|
87
|
+
end
|
88
|
+
in [:gvar, prefix, scope]
|
89
|
+
value_doc scope[prefix + matched]
|
90
|
+
in [:ivar, prefix, scope]
|
91
|
+
value_doc scope[prefix + matched]
|
92
|
+
in [:cvar, prefix, scope]
|
93
|
+
value_doc scope[prefix + matched]
|
94
|
+
in [:call, prefix, type, _self_call]
|
95
|
+
method_doc type, prefix + matched
|
96
|
+
in [:lvar_or_method, prefix, scope]
|
97
|
+
if scope.local_variables.include?(prefix + matched)
|
98
|
+
value_doc scope[prefix + matched]
|
99
|
+
else
|
100
|
+
method_doc scope.self_type, prefix + matched
|
101
|
+
end
|
102
|
+
else
|
103
|
+
end
|
104
|
+
rescue Exception => e
|
105
|
+
ReplTypeCompletor.handle_exception(e)
|
106
|
+
nil
|
107
|
+
ensure
|
108
|
+
$VERBOSE = verbose
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
def method_doc(type, name)
|
114
|
+
type = type.types.find { _1.all_methods.include? name.to_sym }
|
115
|
+
case type
|
116
|
+
when Types::SingletonType
|
117
|
+
"#{Types.class_name_of(type.module_or_class)}.#{name}"
|
118
|
+
when Types::InstanceType
|
119
|
+
"#{Types.class_name_of(type.klass)}##{name}"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def call_or_const_doc(type, name)
|
124
|
+
if name =~ /\A[A-Z]/
|
125
|
+
type = type.types.grep(Types::SingletonType).find { _1.module_or_class.const_defined?(name) }
|
126
|
+
type.module_or_class == Object ? name : "#{Types.class_name_of(type.module_or_class)}::#{name}" if type
|
127
|
+
else
|
128
|
+
method_doc(type, name)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def value_doc(type)
|
133
|
+
return unless type
|
134
|
+
type.types.each do |t|
|
135
|
+
case t
|
136
|
+
when Types::SingletonType
|
137
|
+
return Types.class_name_of(t.module_or_class)
|
138
|
+
when Types::InstanceType
|
139
|
+
return Types.class_name_of(t.klass)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
nil
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|