Module: MilkTea::LSP::Server::ServerRename

Included in:
MilkTea::LSP::Server
Defined in:
lib/milk_tea/lsp/server/rename.rb

Instance Method Summary collapse

Instance Method Details

#allow_hover_last_good_fallback?(uri) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
# File 'lib/milk_tea/lsp/server/rename.rb', line 26

def allow_hover_last_good_fallback?(uri)
  return true unless @workspace.dependency_resolution_mode == :frozen

  path = uri_to_path(uri)
  return true unless path && File.file?(path)

  DependencyResolution.resolve(path, mode: @workspace.dependency_resolution_mode).ok?
rescue StandardError
  true
end

#ast_name_range(name, line, column) ⇒ Object



426
427
428
# File 'lib/milk_tea/lsp/server/rename.rb', line 426

def ast_name_range(name, line, column)
  { name: name, line: line, column: column, length: name.length }
end

#collect_struct_field_changes(uri, field_name, new_name) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
# File 'lib/milk_tea/lsp/server/rename.rb', line 477

def collect_struct_field_changes(uri, field_name, new_name)
  tokens = @workspace.get_tokens(uri) || []
  edits = []
  tokens.each_with_index do |tok, i|
    next unless tok.type == :identifier && tok.lexeme == field_name
    next unless struct_field_edit_context?(tokens, i, nil)

    edits << { range: token_to_range(tok), newText: new_name }
  end
  edits.empty? ? nil : edits
end

#declaration_name_range(node) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/milk_tea/lsp/server/rename.rb', line 407

def declaration_name_range(node)
  if node.is_a?(AST::MatchArm) || node.is_a?(AST::MatchExprArm)
    return nil unless node.binding_name && node.binding_line && node.binding_column

    return {
      name: node.binding_name,
      line: node.binding_line,
      column: node.binding_column,
      length: node.binding_name.length,
    }
  end

  return nil if node.is_a?(AST::ForStmt)
  return nil unless node.respond_to?(:name)
  return nil unless node.name.is_a?(String) && node.line && node.column

  ast_name_range(node.name, node.line, node.column)
end

#definition_identity_key(location) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/milk_tea/lsp/server/rename.rb', line 161

def definition_identity_key(location)
  uri = location[:uri] || location['uri']
  range = location[:range] || location['range']
  return nil unless uri && range

  start = range[:start] || range['start']
  return nil unless start

  line = start[:line] || start['line']
  character = start[:character] || start['character']
  return nil if line.nil? || character.nil?

  [uri, line, character]
end

#each_ast_node(node) {|node| ... } ⇒ Object

Yields:

  • (node)


430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/milk_tea/lsp/server/rename.rb', line 430

def each_ast_node(node, &block)
  return if node.nil?

  if node.is_a?(Array)
    node.each { |item| each_ast_node(item, &block) }
    return
  end

  class_name = node.class.name
  return unless class_name && class_name.start_with?('MilkTea::AST::')

  yield node

  if node.respond_to?(:members)
    node.members.each do |member|
      next unless member.is_a?(Symbol)
      each_ast_node(node.public_send(member), &block)
    end
  end
end

#enum_member_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/milk_tea/lsp/server/rename.rb', line 251

def enum_member_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name)
  tokens = @workspace.get_tokens(uri) || []

  cursor_index = tokens.index { |t| t.line == token.line && t.column == token.column }
  return nil unless cursor_index

  cursor_is_enum_member = variant_enum_member_declaration?(tokens, cursor_index) ||
                          type_name_member_access?(tokens, cursor_index, facts)
  return nil unless cursor_is_enum_member

  changes = {}
  [uri, *@workspace.related_open_document_uris(uri)].uniq.each do |doc_uri|
    doc_tokens = @workspace.get_tokens(doc_uri) || []
    doc_facts = doc_uri == uri ? facts : @workspace.get_facts(doc_uri)
    edits = doc_tokens.each_with_index.filter_map do |tok, i|
      next unless tok.type == :identifier && tok.lexeme == token.lexeme

      enum_member_decl = variant_enum_member_declaration?(doc_tokens, i)
      enum_member_access = type_name_member_access?(doc_tokens, i, doc_facts)
      next unless enum_member_decl || enum_member_access

      {
        range: token_to_range(tok),
        newText: new_name,
      }
    end

    changes[doc_uri] = edits unless edits.empty?
  end

  return nil if changes.empty?
  changes
end

#find_declaration_ast_node_at(ast, name, line, char) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/milk_tea/lsp/server/rename.rb', line 393

def find_declaration_ast_node_at(ast, name, line, char)
  each_ast_node(ast) do |node|
    range = declaration_name_range(node)
    next unless range
    next unless range[:name] == name
    next unless range[:line] == line
    next unless char >= range[:column] && char < range[:column] + range[:length]

    return node
  end

  nil
end

#find_extending_type_for_method(ast, method_node) ⇒ Object



588
589
590
591
592
593
594
# File 'lib/milk_tea/lsp/server/rename.rb', line 588

def find_extending_type_for_method(ast, method_node)
  each_ast_node(ast) do |node|
    next unless node.is_a?(AST::ExtendingBlock)
    return node.type_name if node.methods.any? { |m| m.object_id == method_node.object_id }
  end
  nil
end

#find_identifier_ast_node_at(ast, name, line, char) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/milk_tea/lsp/server/rename.rb', line 380

def find_identifier_ast_node_at(ast, name, line, char)
  each_ast_node(ast) do |node|
    next unless node.is_a?(AST::Identifier)
    next unless node.name == name
    next unless node.line == line
    next unless char >= node.column && char < node.column + name.length

    return node
  end

  nil
end

#find_method_at_position(uri, token, facts) ⇒ Object



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/milk_tea/lsp/server/rename.rb', line 571

def find_method_at_position(uri, token, facts)
  ast = @workspace.get_ast(uri)
  return nil unless ast

  each_ast_node(ast) do |node|
    next unless node.is_a?(AST::MethodDef)
    next unless node.line == token.line
    next unless token.column >= node.column && token.column < node.column + node.name.length

    type_name = find_extending_type_for_method(ast, node)
    next unless type_name

    { receiver_type: facts.types[type_name], method_name: node.name }
  end
  nil
end

#find_struct_containing_field(facts, field_name) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/milk_tea/lsp/server/rename.rb', line 527

def find_struct_containing_field(facts, field_name)
  facts.types.each_value do |type|
    target = if type.is_a?(Types::GenericStructDefinition)
              type
            elsif type.is_a?(Types::Struct) || type.is_a?(Types::StructInstance)
              type.is_a?(Types::StructInstance) ? type.definition : type
            else
              next
            end
    next unless target.respond_to?(:fields) && target.fields.key?(field_name)
    return type
  end
  nil
end

#handle_prepare_rename(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/milk_tea/lsp/server/rename.rb', line 7

def handle_prepare_rename(params)
  uri      = params['textDocument']['uri']
  lsp_line = params['position']['line']
  lsp_char = params['position']['character']

  token = @workspace.find_token_at(uri, lsp_line, lsp_char)
  return nil unless name_like_token?(token)

  { range: token_to_range(token), placeholder: token.lexeme }
rescue StandardError => e
  warn "Error in prepareRename handler: #{e.message}"
  nil
end

#handle_rename(params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/milk_tea/lsp/server/rename.rb', line 37

def handle_rename(params)
  uri      = params['textDocument']['uri']
  lsp_line = params['position']['line']
  lsp_char = params['position']['character']
  new_name = params['newName'].to_s

  token = @workspace.find_token_at(uri, lsp_line, lsp_char)
  return nil unless name_like_token?(token)

  import_alias_changes = import_alias_rename_changes(uri, token, lsp_line, lsp_char, nil, new_name)
  return { changes: import_alias_changes } if import_alias_changes

  if (facts = @workspace.get_facts(uri))
    sc = struct_field_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name)
    return { changes: sc } if sc && !sc.empty?

    mc = method_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name)
    return { changes: mc } if mc && !mc.empty?
  end

  enum_member_changes = enum_member_rename_changes(uri, token, lsp_line, lsp_char, nil, new_name)
  return { changes: enum_member_changes } if enum_member_changes

  if (facts = @workspace.get_facts(uri))
    scoped_changes = scoped_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name)
    return { changes: scoped_changes } if scoped_changes

    enum_member_changes = enum_member_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name)
    return { changes: enum_member_changes } if enum_member_changes
  end

  workspace_symbol_changes = workspace_symbol_identity_rename_changes(uri, token, lsp_line, lsp_char, new_name)
  return { changes: workspace_symbol_changes } if workspace_symbol_changes

  lexical_changes = lexical_rename_changes_in_document(uri, token.lexeme, new_name)
  return { changes: lexical_changes } if lexical_changes

  nil
rescue StandardError => e
  warn "Error in rename handler: #{e.message}"
  nil
end

#import_alias_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/milk_tea/lsp/server/rename.rb', line 198

def import_alias_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name)
  alias_name = token.lexeme
  if facts
    return nil unless facts.imports.key?(alias_name)
  end

  ast = @workspace.get_ast(uri)
  return nil unless ast

  import_node = ast.imports.find do |imp|
    (imp.alias_name || imp.path.parts.last) == alias_name
  end
  return nil unless import_node

  # Validate the cursor is at the declaration site or a module-qualifier usage (alias followed by dot).
  tokens = @workspace.get_tokens(uri) || []

  cursor_at_declaration = token.line == import_node.line && token.column == import_node.column
  unless cursor_at_declaration
    # token was already located by find_token_at; verify it's used as a module qualifier
    tok_idx = tokens.index { |t| t.line == token.line && t.column == token.column }
    return nil unless tok_idx && tokens[tok_idx + 1]&.type == :dot
  end

  edits = []

  # Declaration site
  decl_char = import_node.column - 1
  edits << {
    range: {
      start: { line: import_node.line - 1, character: decl_char },
      end:   { line: import_node.line - 1, character: decl_char + alias_name.length },
    },
    newText: new_name,
  }

  # Usage sites: every token with this lexeme immediately followed by a dot
  tokens.each_with_index do |tok, i|
    next unless tok.type == :identifier && tok.lexeme == alias_name
    next unless tokens[i + 1]&.type == :dot

    edits << {
      range: {
        start: { line: tok.line - 1, character: tok.column - 1 },
        end:   { line: tok.line - 1, character: tok.column - 1 + alias_name.length },
      },
      newText: new_name,
    }
  end

  { uri => edits }
end

#lexical_rename_changes_in_document(uri, name, new_name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/milk_tea/lsp/server/rename.rb', line 80

def lexical_rename_changes_in_document(uri, name, new_name)
  tokens = @workspace.get_tokens(uri) || []
  edits = tokens.flat_map do |tok|
    result = []

    if tok.type == :fstring && tok.literal.is_a?(Array)
      tok.literal.each do |part|
        next unless part.is_a?(Hash) && part[:kind] == :expr
        next unless part[:line] && part[:column]

        expr_tokens = MilkTea::Lexer.lex(part[:source], path: uri_to_path(uri))
        expr_tokens.each do |etok|
          next unless name_like_token?(etok) && etok.lexeme == name

          result << {
            range: {
              start: { line: part[:line] - 1, character: part[:column] + etok.column - 2 },
              end:   { line: part[:line] - 1, character: part[:column] + etok.column - 2 + etok.lexeme.length },
            },
            newText: new_name,
          }
        end
      rescue MilkTea::LexError
        nil
      end
    end

    if name_like_token?(tok) && tok.lexeme == name
      result << { range: token_to_range(tok), newText: new_name }
    end

    result
  end

  return nil if edits.empty?

  { uri => edits }
end

#local_binding_declaration_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


319
320
321
322
323
324
325
# File 'lib/milk_tea/lsp/server/rename.rb', line 319

def local_binding_declaration_node?(node)
  node.is_a?(AST::LocalDecl) ||
    node.is_a?(AST::Param) ||
    node.is_a?(AST::ForBinding) ||
    node.is_a?(AST::MatchArm) ||
    node.is_a?(AST::MatchExprArm)
end

#local_binding_id?(uri, facts, binding_id) ⇒ Boolean

Returns:

  • (Boolean)


302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/milk_tea/lsp/server/rename.rb', line 302

def local_binding_id?(uri, facts, binding_id)
  ast = @workspace.get_ast(uri)
  return false unless ast

  resolution = facts.binding_resolution
  return false unless resolution

  each_ast_node(ast) do |node|
    next unless local_binding_declaration_node?(node)
    next unless resolution.declaration_binding_ids[node.object_id] == binding_id

    return true
  end

  false
end

#method_rename_changes(uri, token, _lsp_line, _lsp_char, facts, new_name) ⇒ Object



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/milk_tea/lsp/server/rename.rb', line 542

def method_rename_changes(uri, token, _lsp_line, _lsp_char, facts, new_name)
  method_name = token.lexeme
  method_info = find_method_at_position(uri, token, facts)
  return nil unless method_info

  receiver_type = method_info[:receiver_type]
  return nil unless receiver_type

  changes = {}
  [uri, *@workspace.related_open_document_uris(uri)].uniq.each do |doc_uri|
    edits = []
    doc_tokens = @workspace.get_tokens(doc_uri) || []
    doc_tokens.each_with_index do |tok, i|
      next unless tok.type == :identifier && tok.lexeme == method_name

      is_call = i > 0 && doc_tokens[i - 1].type == :dot
      is_def = doc_uri == uri && tok.line == token.line && tok.column == token.column
      next unless is_call || is_def

      edits << { range: token_to_range(tok), newText: new_name }
    end

    changes[doc_uri] = edits unless edits.empty?
  end

  return nil if changes.empty?
  changes
end

#name_like_token?(tok) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/milk_tea/lsp/server/rename.rb', line 21

def name_like_token?(tok)
  return false if tok.nil?
  tok.type == :identifier || Token::KEYWORDS.value?(tok.type)
end

#rename_target_binding_id(uri, token, lsp_line, lsp_char, facts) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/milk_tea/lsp/server/rename.rb', line 327

def rename_target_binding_id(uri, token, lsp_line, lsp_char, facts)
  resolution = facts.binding_resolution
  return nil unless resolution

  line = lsp_line + 1
  char = lsp_char + 1
  ast = @workspace.get_ast(uri)
  return nil unless ast

  identifier_node = find_identifier_ast_node_at(ast, token.lexeme, line, char)
  if identifier_node && (id = resolution.identifier_binding_ids[identifier_node.object_id])
    return id
  end

  declaration_node = find_declaration_ast_node_at(ast, token.lexeme, line, char)
  if declaration_node && (id = resolution.declaration_binding_ids[declaration_node.object_id])
    return id
  end

  resolve_local_hover_binding(facts, token.lexeme, line, char)&.id
end

#scoped_binding_occurrence_ranges(uri, name, facts, binding_id, include_declaration:) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/milk_tea/lsp/server/rename.rb', line 349

def scoped_binding_occurrence_ranges(uri, name, facts, binding_id, include_declaration:)
  ast = @workspace.get_ast(uri)
  return [] unless ast

  resolution = facts.binding_resolution
  return [] unless resolution

  ranges = []
  seen = Set.new

  each_ast_node(ast) do |node|
    range = nil

    if node.is_a?(AST::Identifier) && node.name == name && resolution.identifier_binding_ids[node.object_id] == binding_id
      range = ast_name_range(node.name, node.line, node.column)
    elsif include_declaration && resolution.declaration_binding_ids[node.object_id] == binding_id
      range = declaration_name_range(node)
    end

    next unless range

    key = [range[:line], range[:column], range[:length]]
    next if seen.include?(key)

    seen << key
    ranges << range
  end

  ranges
end

#scoped_local_reference_locations(uri, token, lsp_line, lsp_char, facts, include_declaration:) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/milk_tea/lsp/server/rename.rb', line 285

def scoped_local_reference_locations(uri, token, lsp_line, lsp_char, facts, include_declaration:)
  binding_id = rename_target_binding_id(uri, token, lsp_line, lsp_char, facts)
  return nil unless binding_id
  return nil unless local_binding_id?(uri, facts, binding_id)

  ranges = scoped_binding_occurrence_ranges(uri, token.lexeme, facts, binding_id, include_declaration: include_declaration)
  ranges.map do |range|
    {
      uri: uri,
      range: {
        start: { line: range[:line] - 1, character: range[:column] - 1 },
        end: { line: range[:line] - 1, character: range[:column] - 1 + range[:length] },
      },
    }
  end
end

#scoped_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/milk_tea/lsp/server/rename.rb', line 176

def scoped_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name)
  binding_id = rename_target_binding_id(uri, token, lsp_line, lsp_char, facts)
  unless binding_id
    return import_alias_rename_changes(uri, token, lsp_line, lsp_char, facts, new_name)
  end

  ranges = scoped_binding_occurrence_ranges(uri, token.lexeme, facts, binding_id, include_declaration: true)
  return nil if ranges.empty?

  edits = ranges.map do |range|
    {
      range: {
        start: { line: range[:line] - 1, character: range[:column] - 1 },
        end: { line: range[:line] - 1, character: range[:column] - 1 + range[:length] },
      },
      newText: new_name,
    }
  end

  { uri => edits }
end

#skip_trivia_forward(tokens, start_index) ⇒ Object



518
519
520
521
522
523
524
525
# File 'lib/milk_tea/lsp/server/rename.rb', line 518

def skip_trivia_forward(tokens, start_index)
  i = start_index
  while i < tokens.length
    return i unless tokens[i].type == :whitespace || tokens[i].type == :newline || tokens[i].type == :comment
    i += 1
  end
  nil
end

#struct_field_cursor_context?(tokens, index) ⇒ Boolean

Returns:

  • (Boolean)


489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/milk_tea/lsp/server/rename.rb', line 489

def struct_field_cursor_context?(tokens, index)
  next_nt = skip_trivia_forward(tokens, index + 1)
  return true if next_nt && tokens[next_nt]&.type == :colon

  return true if index > 0 && tokens[index - 1].type == :dot

  if index > 0 && (tokens[index - 1].type == :lparen || tokens[index - 1].type == :comma)
    next_nt = skip_trivia_forward(tokens, index + 1)
    return true if next_nt && (tokens[next_nt]&.type == :equal || tokens[next_nt]&.type == :colon)
    return true if next_nt.nil? || !(tokens[next_nt]&.type == :equal || tokens[next_nt]&.type == :colon)
  end

  false
end

#struct_field_edit_context?(tokens, index, _struct_type) ⇒ Boolean

Returns:

  • (Boolean)


504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/milk_tea/lsp/server/rename.rb', line 504

def struct_field_edit_context?(tokens, index, _struct_type)
  return true if index > 0 && tokens[index - 1].type == :dot

  next_nt = skip_trivia_forward(tokens, index + 1)
  return true if next_nt && tokens[next_nt]&.type == :colon

  if index > 0 && (tokens[index - 1].type == :lparen || tokens[index - 1].type == :comma)
    next_nt = skip_trivia_forward(tokens, index + 1)
    return true if next_nt.nil? || (tokens[next_nt]&.type == :equal || tokens[next_nt]&.type == :colon) || tokens[next_nt]&.type == :comma || tokens[next_nt]&.type == :rparen
  end

  false
end

#struct_field_rename_changes(uri, token, _lsp_line, _lsp_char, facts, new_name) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/milk_tea/lsp/server/rename.rb', line 451

def struct_field_rename_changes(uri, token, _lsp_line, _lsp_char, facts, new_name)
  field_name = token.lexeme
  tokens = @workspace.get_tokens(uri) || []
  idx = tokens.index { |t| t.line == token.line && t.column == token.column }
  return nil unless idx

  cursor_in_field_context = struct_field_cursor_context?(tokens, idx)
  return nil unless cursor_in_field_context

  struct_type = find_struct_containing_field(facts, field_name)
  return nil unless struct_type

  changes = {}
  edits = collect_struct_field_changes(uri, field_name, new_name)
  changes[uri] = edits if edits
  related_uris = @workspace.related_open_document_uris(uri)
  related_uris.each do |related_uri|
    next if related_uri == uri
    related_changes = collect_struct_field_changes(related_uri, field_name, new_name)
    changes[related_uri] = related_changes if related_changes
  end

  return nil if changes.empty?
  changes
end

#workspace_symbol_identity_rename_changes(uri, token, lsp_line, lsp_char, new_name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/milk_tea/lsp/server/rename.rb', line 119

def workspace_symbol_identity_rename_changes(uri, token, lsp_line, lsp_char, new_name)
  target_location = resolve_definition_location({
    'textDocument' => { 'uri' => uri },
    'position' => { 'line' => lsp_line, 'character' => lsp_char },
  }, stages: nil)
  return nil unless target_location

  target_identity = definition_identity_key(target_location)
  return nil unless target_identity

  related_uris = @workspace.related_open_document_uris(uri)
  return nil if related_uris.empty?

  changes = {}
  related_uris.each do |related_uri|
    tokens = @workspace.get_tokens(related_uri) || []
    next if tokens.empty?

    edits = tokens.filter_map do |candidate|
      next unless name_like_token?(candidate) && candidate.lexeme == token.lexeme

      candidate_location = resolve_definition_location({
        'textDocument' => { 'uri' => related_uri },
        'position' => { 'line' => candidate.line - 1, 'character' => candidate.column - 1 },
      }, stages: nil)
      next unless candidate_location
      next unless definition_identity_key(candidate_location) == target_identity

      {
        range: token_to_range(candidate),
        newText: new_name,
      }
    end

    changes[related_uri] = edits unless edits.empty?
  end

  return nil if changes.empty?

  changes
end