@@ -11,17 +11,17 @@ class Completion
11
11
params (
12
12
response_builder : ResponseBuilders ::CollectionResponseBuilder [ Interface ::CompletionItem ] ,
13
13
global_state : GlobalState ,
14
- nesting : T :: Array [ String ] ,
14
+ node_context : NodeContext ,
15
15
typechecker_enabled : T ::Boolean ,
16
16
dispatcher : Prism ::Dispatcher ,
17
17
uri : URI ::Generic ,
18
18
) . void
19
19
end
20
- def initialize ( response_builder , global_state , nesting , typechecker_enabled , dispatcher , uri ) # rubocop:disable Metrics/ParameterLists
20
+ def initialize ( response_builder , global_state , node_context , typechecker_enabled , dispatcher , uri ) # rubocop:disable Metrics/ParameterLists
21
21
@response_builder = response_builder
22
22
@global_state = global_state
23
23
@index = T . let ( global_state . index , RubyIndexer ::Index )
24
- @nesting = nesting
24
+ @node_context = node_context
25
25
@typechecker_enabled = typechecker_enabled
26
26
@uri = uri
27
27
@@ -47,7 +47,7 @@ def on_constant_read_node_enter(node)
47
47
name = constant_name ( node )
48
48
return if name . nil?
49
49
50
- candidates = @index . prefix_search ( name , @nesting )
50
+ candidates = @index . prefix_search ( name , @node_context . nesting )
51
51
candidates . each do |entries |
52
52
complete_name = T . must ( entries . first ) . name
53
53
@response_builder << build_entry_completion (
@@ -161,20 +161,21 @@ def constant_path_completion(name, range)
161
161
T . must ( namespace ) . join ( "::" )
162
162
end
163
163
164
- namespace_entries = @index . resolve ( aliased_namespace , @nesting )
164
+ nesting = @node_context . nesting
165
+ namespace_entries = @index . resolve ( aliased_namespace , nesting )
165
166
return unless namespace_entries
166
167
167
168
real_namespace = @index . follow_aliased_namespace ( T . must ( namespace_entries . first ) . name )
168
169
169
170
candidates = @index . prefix_search (
170
171
"#{ real_namespace } ::#{ incomplete_name } " ,
171
- top_level_reference ? [ ] : @ nesting,
172
+ top_level_reference ? [ ] : nesting ,
172
173
)
173
174
candidates . each do |entries |
174
175
# The only time we may have a private constant reference from outside of the namespace is if we're dealing
175
176
# with ConstantPath and the entry name doesn't start with the current nesting
176
177
first_entry = T . must ( entries . first )
177
- next if first_entry . private? && !first_entry . name . start_with? ( "#{ @ nesting} ::" )
178
+ next if first_entry . private? && !first_entry . name . start_with? ( "#{ nesting } ::" )
178
179
179
180
constant_name = first_entry . name . delete_prefix ( "#{ real_namespace } ::" )
180
181
full_name = aliased_namespace . empty? ? constant_name : "#{ aliased_namespace } ::#{ constant_name } "
@@ -191,7 +192,7 @@ def constant_path_completion(name, range)
191
192
192
193
sig { params ( name : String , location : Prism ::Location ) . void }
193
194
def handle_instance_variable_completion ( name , location )
194
- @index . instance_variable_completion_candidates ( name , @nesting . join ( "::" ) ) . each do |entry |
195
+ @index . instance_variable_completion_candidates ( name , @node_context . nesting . join ( "::" ) ) . each do |entry |
195
196
variable_name = entry . name
196
197
197
198
@response_builder << Interface ::CompletionItem . new (
@@ -257,7 +258,7 @@ def complete_require_relative(node)
257
258
258
259
sig { params ( node : Prism ::CallNode , name : String ) . void }
259
260
def complete_self_receiver_method ( node , name )
260
- receiver_entries = @index [ @nesting . join ( "::" ) ]
261
+ receiver_entries = @index [ @node_context . nesting . join ( "::" ) ]
261
262
return unless receiver_entries
262
263
263
264
receiver = T . must ( receiver_entries . first )
@@ -351,13 +352,14 @@ def build_entry_completion(real_name, incomplete_name, range, entries, top_level
351
352
#
352
353
# Foo::B # --> completion inserts `Bar` instead of `Foo::Bar`
353
354
# end
354
- unless @nesting . join ( "::" ) . start_with? ( incomplete_name )
355
- @nesting . each do |namespace |
355
+ nesting = @node_context . nesting
356
+ unless nesting . join ( "::" ) . start_with? ( incomplete_name )
357
+ nesting . each do |namespace |
356
358
prefix = "#{ namespace } ::"
357
359
shortened_name = insertion_text . delete_prefix ( prefix )
358
360
359
361
# If a different entry exists for the shortened name, then there's a conflict and we should not shorten it
360
- conflict_name = "#{ @ nesting. join ( "::" ) } ::#{ shortened_name } "
362
+ conflict_name = "#{ nesting . join ( "::" ) } ::#{ shortened_name } "
361
363
break if real_name != conflict_name && @index [ conflict_name ]
362
364
363
365
insertion_text = shortened_name
@@ -399,8 +401,9 @@ def build_entry_completion(real_name, incomplete_name, range, entries, top_level
399
401
# ```
400
402
sig { params ( entry_name : String ) . returns ( T ::Boolean ) }
401
403
def top_level? ( entry_name )
402
- @nesting . length . downto ( 0 ) . each do |i |
403
- prefix = T . must ( @nesting [ 0 ...i ] ) . join ( "::" )
404
+ nesting = @node_context . nesting
405
+ nesting . length . downto ( 0 ) . each do |i |
406
+ prefix = T . must ( nesting [ 0 ...i ] ) . join ( "::" )
404
407
full_name = prefix . empty? ? entry_name : "#{ prefix } ::#{ entry_name } "
405
408
next if full_name == entry_name
406
409
0 commit comments