repl_type_completor 0.1.16 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2dce7fd408fd473356245c6b83f8ba3240ebe39946a7e9a929cbfdde6cd138a1
|
|
4
|
+
data.tar.gz: 78002d089560ede6af424b70a46f10f0b65218858bdf6dee8ec63c07bf7b2b45
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3347f5df32b086a04a916d4e1b8def4352c392cc1a59564e2e87eceda50c4e50933117a8c9c72ca0e92d004a77f312d16d02a2923082de2df3babdc60e545426
|
|
7
|
+
data.tar.gz: 1c0916a0129ec721e14c20c166290f41db39a97f832b32dde332cef8140a3c47723ebef94cff4623d1e2738c3174070cfac462d5db88a4f4b9b7932213241919
|
|
@@ -68,7 +68,7 @@ module ReplTypeCompletor
|
|
|
68
68
|
in [:aref, key_type, name, receiver_type]
|
|
69
69
|
key_type = key_type == :string ? String : Symbol
|
|
70
70
|
keys = receiver_type.types.grep(Types::InstanceType).select do |t|
|
|
71
|
-
Hash == t.klass
|
|
71
|
+
Hash == t.klass && t.instances
|
|
72
72
|
end.flat_map do |t|
|
|
73
73
|
t.instances.flat_map(&:keys).grep(key_type).uniq
|
|
74
74
|
end
|
|
@@ -3,9 +3,17 @@
|
|
|
3
3
|
require 'set'
|
|
4
4
|
require_relative 'types'
|
|
5
5
|
require_relative 'scope'
|
|
6
|
-
require 'prism'
|
|
7
|
-
|
|
8
6
|
module ReplTypeCompletor
|
|
7
|
+
# Prefer Ruby::Prism, the parser built into the interpreter (Ruby 4.1+),
|
|
8
|
+
# over the prism gem. Every reference to Prism in this gem is lexically
|
|
9
|
+
# inside the ReplTypeCompletor module, so it resolves to this constant.
|
|
10
|
+
if defined?(Ruby::Prism)
|
|
11
|
+
Prism = Ruby::Prism
|
|
12
|
+
else
|
|
13
|
+
require 'prism'
|
|
14
|
+
Prism = ::Prism
|
|
15
|
+
end
|
|
16
|
+
|
|
9
17
|
class TypeAnalyzer
|
|
10
18
|
class DigTarget
|
|
11
19
|
def initialize(parents, receiver, &block)
|
|
@@ -37,6 +37,10 @@ module ReplTypeCompletor
|
|
|
37
37
|
gem_sig_path = File.expand_path("#{spec.gem_dir}/sig")
|
|
38
38
|
loader.add(library: spec.name, version: spec.version) if Dir.exist?(gem_sig_path) && expanded_sig_path != gem_sig_path
|
|
39
39
|
end
|
|
40
|
+
# A gem's sig/manifest.yaml can declare dependencies on stdlib libraries that
|
|
41
|
+
# the installed RBS version does not ship (e.g. prism >= 2.0 depends on pp,
|
|
42
|
+
# which was added in RBS 3.3). Skip those instead of failing to load everything.
|
|
43
|
+
loader.libs.delete_if { |lib| !loader.has_library?(library: lib.name, version: lib.version) }
|
|
40
44
|
end
|
|
41
45
|
|
|
42
46
|
# Hack to make this thread priority lower, not to block the main thread.
|
data/lib/repl_type_completor.rb
CHANGED
|
@@ -45,7 +45,7 @@ module ReplTypeCompletor
|
|
|
45
45
|
|
|
46
46
|
def info
|
|
47
47
|
require 'rbs'
|
|
48
|
-
prism_info = "Prism: #{Prism::VERSION}"
|
|
48
|
+
prism_info = "#{Prism}: #{Prism::VERSION}"
|
|
49
49
|
rbs_info = "RBS: #{RBS::VERSION}"
|
|
50
50
|
if rbs_load_error
|
|
51
51
|
rbs_info << " #{rbs_load_error.inspect}"
|
|
@@ -69,7 +69,7 @@ module ReplTypeCompletor
|
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
def analyze_code(code, binding = Object::TOPLEVEL_BINDING)
|
|
72
|
-
ast = Prism.parse(code, scopes: [binding.local_variables]).value
|
|
72
|
+
ast = Prism.parse(code, scopes: [binding.local_variables], **prism_parse_options).value
|
|
73
73
|
*parents, target_node = find_target ast, code.bytesize
|
|
74
74
|
return unless target_node
|
|
75
75
|
|
|
@@ -81,6 +81,8 @@ module ReplTypeCompletor
|
|
|
81
81
|
return if target_node.opening == "?" || (target_node.closing_loc && !target_node.closing.empty?)
|
|
82
82
|
|
|
83
83
|
call_node = single_arg_call_node(parents) # require("target") or require_relative("target")
|
|
84
|
+
return unless call_node
|
|
85
|
+
|
|
84
86
|
if call_node.receiver.nil? && (call_node.name == :require || call_node.name == :require_relative)
|
|
85
87
|
[call_node.name, target_node.content]
|
|
86
88
|
elsif call_node.receiver && call_node.name == :[] # object["target"]
|
|
@@ -135,6 +137,18 @@ module ReplTypeCompletor
|
|
|
135
137
|
end
|
|
136
138
|
end
|
|
137
139
|
|
|
140
|
+
# Parse with the syntax of the running Ruby so that completion matches what will be executed.
|
|
141
|
+
# Fall back to Prism's default when `version: "current"` is not available (Prism < 1.6)
|
|
142
|
+
# or the running Ruby version is unknown to the installed Prism (Ruby < 3.3, or a newer Ruby).
|
|
143
|
+
def prism_parse_options
|
|
144
|
+
@prism_parse_options ||= begin
|
|
145
|
+
Prism.parse('', version: 'current')
|
|
146
|
+
{ version: 'current' }.freeze
|
|
147
|
+
rescue ArgumentError # includes Prism::CurrentVersionError
|
|
148
|
+
{}.freeze
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
138
152
|
def find_target(node, position)
|
|
139
153
|
# Skip because location of these nodes gives location of whole block
|
|
140
154
|
return if node.is_a?(Prism::NumberedParametersNode) || node.is_a?(Prism::ItParametersNode)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: repl_type_completor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.17
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tompng
|
|
@@ -13,14 +13,14 @@ dependencies:
|
|
|
13
13
|
name: prism
|
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
|
15
15
|
requirements:
|
|
16
|
-
- - "
|
|
16
|
+
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
18
|
version: '1.0'
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
|
-
- - "
|
|
23
|
+
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
25
|
version: '1.0'
|
|
26
26
|
- !ruby/object:Gem::Dependency
|