Module: MilkTea::LSP::Server::ServerDefinition

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

Instance Method Summary collapse

Instance Method Details

#definition_file_ast(path) ⇒ Object



626
627
628
629
630
631
632
# File 'lib/milk_tea/lsp/server/definition.rb', line 626

def definition_file_ast(path)
  mtime_key = definition_file_mtime_key(path)
  cache_key = "#{path}:#{mtime_key}"
  @definition_file_ast_cache[cache_key] ||= begin
    MilkTea::Parser.parse(nil, path: path_to_uri(path), tokens: definition_file_tokens(path, mtime_key: mtime_key))
  end
end

#definition_file_mtime_key(path) ⇒ Object



602
603
604
605
606
607
# File 'lib/milk_tea/lsp/server/definition.rb', line 602

def definition_file_mtime_key(path)
  stat = File.stat(path)
  "#{stat.mtime.to_i}:#{stat.mtime.nsec}"
rescue StandardError
  'missing'
end

#definition_file_tokens(path, mtime_key: nil) ⇒ Object



609
610
611
612
613
614
# File 'lib/milk_tea/lsp/server/definition.rb', line 609

def definition_file_tokens(path, mtime_key: nil)
  cache_key = "#{path}:#{mtime_key || definition_file_mtime_key(path)}"
  @definition_file_token_cache[cache_key] ||= begin
    MilkTea::Lexer.lex(File.read(path), path: path_to_uri(path))
  end
end

#definition_lookup_tokens(path, current_uri: nil) ⇒ Object



616
617
618
619
620
621
622
623
624
# File 'lib/milk_tea/lsp/server/definition.rb', line 616

def definition_lookup_tokens(path, current_uri: nil)
  current_path = current_uri ? uri_to_path(current_uri) : nil
  if current_path && File.expand_path(current_path) == File.expand_path(path)
    workspace_tokens = @workspace.get_tokens(current_uri)
    return workspace_tokens if workspace_tokens
  end

  definition_file_tokens(path)
end

#enum_member_definition_location(current_uri, module_name, type_name, member_name) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/milk_tea/lsp/server/definition.rb', line 398

def enum_member_definition_location(current_uri, module_name, type_name, member_name)
  path = module_path_for_name(current_uri, module_name)
  return nil unless path

  token = find_enum_member_token_in_file(path, type_name, member_name)
  return nil unless token

  {
    uri: path_to_uri(File.expand_path(path)),
    range: token_to_range(token)
  }
end

#enum_member_value_text(current_uri, module_name, type_name, member_name) ⇒ Object



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/milk_tea/lsp/server/definition.rb', line 585

def enum_member_value_text(current_uri, module_name, type_name, member_name)
  path = module_path_for_name(current_uri, module_name)
  return nil unless path

  declaration = definition_file_ast(path)&.declarations&.find do |decl|
    (decl.is_a?(AST::EnumDecl) || decl.is_a?(AST::FlagsDecl)) && decl.name == type_name
  end
  return nil unless declaration

  member = declaration.members.find { |candidate| candidate.name == member_name }
  return nil unless member&.value

  MilkTea::PrettyPrinter::ASTFormatter.new.send(:render_expression, member.value)
rescue StandardError
  nil
end

#field_definition_location(current_uri, receiver_type, field_name) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/milk_tea/lsp/server/definition.rb', line 382

def field_definition_location(current_uri, receiver_type, field_name)
  owner_type = field_owner_type(receiver_type)
  return nil unless owner_type&.respond_to?(:name)

  path = module_path_for_name(current_uri, owner_type.module_name)
  return nil unless path

  token = find_field_token_in_type(path, owner_type.name, field_name, current_uri: current_uri)
  return nil unless token

  {
    uri: path_to_uri(File.expand_path(path)),
    range: token_to_range(token)
  }
end

#find_definition_token_in_file(path, name) ⇒ Object



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

def find_definition_token_in_file(path, name)
  tokens = definition_file_tokens(path)

  tokens.each_cons(2) do |kw_tok, id_tok|
    next unless MilkTea::LSP::Workspace::DEFINITION_KEYWORDS.include?(kw_tok.type)
    next unless id_tok.type == :identifier && id_tok.lexeme == name

    return id_tok
  end

  nil
rescue StandardError
  nil
end

#find_enum_member_token_in_body(tokens, header_index, member_name) ⇒ Object



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/milk_tea/lsp/server/definition.rb', line 562

def find_enum_member_token_in_body(tokens, header_index, member_name)
  header = tokens[header_index]
  i = header_index + 1

  while i < tokens.length
    token = tokens[i]

    if token.line > header.line && ![:newline, :indent, :dedent, :eof].include?(token.type) &&
        first_non_trivia_token_on_line?(tokens, i) && token.column <= header.column
      break
    end

    if token.type == :identifier && token.lexeme == member_name && token.line > header.line &&
        first_non_trivia_token_on_line?(tokens, i) && token.column > header.column
      return token
    end

    i += 1
  end

  nil
end

#find_enum_member_token_in_file(path, type_name, member_name) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/milk_tea/lsp/server/definition.rb', line 544

def find_enum_member_token_in_file(path, type_name, member_name)
  tokens = definition_file_tokens(path)

  tokens.each_with_index do |token, index|
    next unless [:enum, :flags].include?(token.type)

    name_index = next_non_trivia_token_index(tokens, index + 1)
    next unless name_index && tokens[name_index].type == :identifier && tokens[name_index].lexeme == type_name

    member_token = find_enum_member_token_in_body(tokens, index, member_name)
    return member_token if member_token
  end

  nil
rescue StandardError
  nil
end

#find_field_token_in_body(tokens, header_index, field_name) ⇒ Object



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/milk_tea/lsp/server/definition.rb', line 520

def find_field_token_in_body(tokens, header_index, field_name)
  header = tokens[header_index]
  i = header_index + 1

  while i < tokens.length
    token = tokens[i]

    if token.line > header.line && ![:newline, :indent, :dedent, :eof].include?(token.type) &&
        first_non_trivia_token_on_line?(tokens, i) && token.column <= header.column
      break
    end

    if token.type == :identifier && token.lexeme == field_name && token.line > header.line &&
        first_non_trivia_token_on_line?(tokens, i) && token.column > header.column
      colon_index = next_non_trivia_token_index(tokens, i + 1)
      return token if colon_index && tokens[colon_index].type == :colon
    end

    i += 1
  end

  nil
end

#find_field_token_in_type(path, type_name, field_name, current_uri: nil) ⇒ Object



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

def find_field_token_in_type(path, type_name, field_name, current_uri: nil)
  tokens = definition_lookup_tokens(path, current_uri: current_uri)

  tokens.each_with_index do |token, index|
    next unless [:struct, :union].include?(token.type)

    name_index = next_non_trivia_token_index(tokens, index + 1)
    next unless name_index && tokens[name_index].type == :identifier && tokens[name_index].lexeme == type_name

    field_token = find_field_token_in_body(tokens, index, field_name)
    return field_token if field_token
  end

  nil
rescue StandardError
  nil
end

#find_local_decl_node(node, name, before_line, &block) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/milk_tea/lsp/server/definition.rb', line 204

def find_local_decl_node(node, name, before_line, &block)
  return false if node.nil?

  if (node.is_a?(AST::LocalDecl) || node.is_a?(AST::ForBinding)) && node.name == name && node.line && node.line < before_line
    return true if yield node
  end

  if node.is_a?(Array)
    node.each { |item| return true if find_local_decl_node(item, name, before_line, &block) }
    return false
  end

  return false unless node.class.name&.start_with?("MilkTea::AST::")

  if node.respond_to?(:members)
    node.members.each do |member|
      next unless member.is_a?(Symbol)
      return true if find_local_decl_node(node.public_send(member), name, before_line, &block)
    end
  end

  false
end

#find_local_declaration_ast_node(uri, name, before_line) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/milk_tea/lsp/server/definition.rb', line 195

def find_local_declaration_ast_node(uri, name, before_line)
  ast = @workspace.get_ast(uri)
  return nil unless ast

  result = nil
  find_local_decl_node(ast, name, before_line) { |node| result = node; true }
  result
end

#handle_declaration(params) ⇒ Object



16
17
18
# File 'lib/milk_tea/lsp/server/definition.rb', line 16

def handle_declaration(params)
  handle_definition_request('textDocument/declaration', params, error_label: 'declaration')
end

#handle_definition(params) ⇒ Object



12
13
14
# File 'lib/milk_tea/lsp/server/definition.rb', line 12

def handle_definition(params)
  handle_definition_request('textDocument/definition', params, error_label: 'definition')
end

#handle_definition_request(method_name, params, error_label:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/milk_tea/lsp/server/definition.rb', line 54

def handle_definition_request(method_name, params, error_label:)
  stages = new_perf_stages
  total_start = stages ? monotonic_time : nil
  uri = params.dig('textDocument', 'uri')
  result_state = 'miss'

  location = resolve_definition_location(params, stages: stages)
  result_state = location ? 'hit' : 'miss'
  location
rescue StandardError => e
  result_state = 'error'
  warn "Error in #{error_label} handler: #{e.message}"
  nil
ensure
  log_request_stage_breakdown(method_name, total_start, uri: uri, stages: stages, summary: "result=#{result_state}")
end

#handle_implementation(params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/milk_tea/lsp/server/definition.rb', line 37

def handle_implementation(params)
  stages = new_perf_stages
  total_start = stages ? monotonic_time : nil
  uri = params.dig('textDocument', 'uri')
  result_state = 'miss'

  locations = resolve_implementation_locations(params, stages: stages)
  result_state = locations.empty? ? 'miss' : 'hit'
  locations
rescue StandardError => e
  result_state = 'error'
  warn "Error in implementation handler: #{e.message}"
  []
ensure
  log_request_stage_breakdown('textDocument/implementation', total_start, uri: uri, stages: stages, summary: "result=#{result_state}")
end

#handle_type_definition(params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/milk_tea/lsp/server/definition.rb', line 20

def handle_type_definition(params)
  stages = new_perf_stages
  total_start = stages ? monotonic_time : nil
  uri = params.dig('textDocument', 'uri')
  result_state = 'miss'

  location = resolve_type_definition_location(params, stages: stages)
  result_state = location ? 'hit' : 'miss'
  location
rescue StandardError => e
  result_state = 'error'
  warn "Error in typeDefinition handler: #{e.message}"
  nil
ensure
  log_request_stage_breakdown('textDocument/typeDefinition', total_start, uri: uri, stages: stages, summary: "result=#{result_state}")
end

#imported_module_name_from_ast(uri, alias_name) ⇒ Object



472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/milk_tea/lsp/server/definition.rb', line 472

def imported_module_name_from_ast(uri, alias_name)
  return nil if alias_name.nil? || alias_name.empty?

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

  import = ast.imports.find do |entry|
    resolved_alias = entry.alias_name || entry.path.parts.last
    resolved_alias == alias_name
  end
  import&.path&.to_s
rescue StandardError
  nil
end

#interface_implementation_locations(interface_binding) ⇒ Object



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/milk_tea/lsp/server/definition.rb', line 657

def interface_implementation_locations(interface_binding)
  seen = Set.new
  @workspace.all_documents.filter_map do |doc_uri|
    facts = @workspace.get_facts(doc_uri)
    next unless facts

    facts.implemented_interfaces.each_with_object([]) do |(receiver_type, interfaces), locations|
      next unless interfaces.any? { |candidate| same_interface_binding?(candidate, interface_binding) }

      location = interface_receiver_definition_location(doc_uri, receiver_type)
      next unless location

      key = [location[:uri], location.dig(:range, :start, :line), location.dig(:range, :start, :character)]
      next if seen.include?(key)

      seen << key
      locations << location
    end
  end.flatten
end

#interface_method_implementation_locations(interface_binding, interface_method) ⇒ Object



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/milk_tea/lsp/server/definition.rb', line 678

def interface_method_implementation_locations(interface_binding, interface_method)
  seen = Set.new
  @workspace.all_documents.filter_map do |doc_uri|
    facts = @workspace.get_facts(doc_uri)
    next unless facts

    facts.implemented_interfaces.each_with_object([]) do |(receiver_type, interfaces), locations|
      next unless interfaces.any? { |candidate| same_interface_binding?(candidate, interface_binding) }

      method = methods_for_receiver_type(facts, receiver_type)[interface_method.name]
      next unless method

      module_name = receiver_module_name(receiver_type)
      location = module_member_binding_location(doc_uri, module_name, interface_method.name, method)
      location ||= module_member_definition_location(doc_uri, module_name, interface_method.name)
      next unless location

      key = [location[:uri], location.dig(:range, :start, :line), location.dig(:range, :start, :character)]
      next if seen.include?(key)

      seen << key
      locations << location
    end
  end.flatten
end

#interface_receiver_definition_location(current_uri, receiver_type) ⇒ Object



704
705
706
707
708
709
710
711
712
713
714
# File 'lib/milk_tea/lsp/server/definition.rb', line 704

def interface_receiver_definition_location(current_uri, receiver_type)
  receiver_type = receiver_type.definition if receiver_type.is_a?(Types::StructInstance) || receiver_type.is_a?(Types::VariantInstance)

  if receiver_type.module_name.nil? || receiver_type.module_name.empty?
    token = local_type_definition_token(current_uri, receiver_type.name)
    token ||= @workspace.find_definition_token(current_uri, receiver_type.name)
    return { uri: current_uri, range: token_to_range(token) } if token
  end

  module_member_definition_location(current_uri, receiver_type.module_name, receiver_type.name)
end

#is_builtin_name?(name, tokens, token_index) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/milk_tea/lsp/server/definition.rb', line 7

def is_builtin_name?(name, tokens, token_index)
  return false unless token_index
  builtin_hover_info(name, tokens, token_index)
end

#local_type_definition_token(uri, name) ⇒ Object



722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/milk_tea/lsp/server/definition.rb', line 722

def local_type_definition_token(uri, name)
  tokens = @workspace.get_tokens(uri)
  return nil unless tokens

  tokens.each_cons(2) do |kw_tok, id_tok|
    next unless [:struct, :opaque, :enum, :flags, :variant, :union].include?(kw_tok.type)
    next unless id_tok.type == :identifier && id_tok.lexeme == name

    return id_tok
  end

  nil
end

#module_definition_location(current_uri, module_name) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/milk_tea/lsp/server/definition.rb', line 356

def module_definition_location(current_uri, module_name)
  path = module_path_for_name(current_uri, module_name)
  return nil unless path

  {
    uri: path_to_uri(File.expand_path(path)),
    range: {
      start: { line: 0, character: 0 },
      end: { line: 0, character: 0 }
    }
  }
end

#module_member_binding_location(current_uri, module_name, member_name, binding) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/milk_tea/lsp/server/definition.rb', line 411

def module_member_binding_location(current_uri, module_name, member_name, binding)
  path = module_path_for_name(current_uri, module_name)
  return nil unless path

  ast = binding.respond_to?(:ast) ? binding.ast : nil
  if ast&.line && ast.column
    start_line = ast.line - 1
    start_char = ast.column - 1

    return {
      uri: path_to_uri(File.expand_path(path)),
      range: {
        start: { line: start_line, character: start_char },
        end: { line: start_line, character: start_char + member_name.length }
      }
    }
  end

  module_member_definition_location(current_uri, module_name, member_name)
end

#module_member_definition_location(current_uri, module_name, member_name) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/milk_tea/lsp/server/definition.rb', line 369

def module_member_definition_location(current_uri, module_name, member_name)
  path = module_path_for_name(current_uri, module_name)
  return nil unless path

  token = find_definition_token_in_file(path, member_name)
  return nil unless token

  {
    uri: path_to_uri(File.expand_path(path)),
    range: token_to_range(token)
  }
end

#module_path_for_name(current_uri, module_name) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/milk_tea/lsp/server/definition.rb', line 432

def module_path_for_name(current_uri, module_name)
  current_path = uri_to_path(current_uri)
  return nil unless current_path
  return current_path if module_name.nil? || module_name.empty?

  current_facts = @workspace.get_facts(current_uri)
  return current_path if current_facts&.module_name == module_name

  resolution = DependencyResolution.resolve(current_path, mode: @workspace.dependency_resolution_mode)
  module_roots = if resolution.ok?
    MilkTea::ModuleRoots.roots_for_path(current_path, locked: resolution.locked)
  else
    MilkTea::ModuleRoots.roots_for_path(current_path)
  end
  relative_path = File.join(*module_name.split('.')) + '.mt'
  resolved_path = module_roots.lazy.map { |root| File.join(root, relative_path) }.find { |candidate| File.file?(candidate) }
  return resolved_path if resolved_path

  workspace_root = @root_uri ? uri_to_path(@root_uri) : nil
  return nil unless workspace_root && File.directory?(workspace_root)

  workspace_candidate = File.join(workspace_root, relative_path)
  return workspace_candidate if File.file?(workspace_candidate)

  nil
rescue PackageLockError
  module_roots = MilkTea::ModuleRoots.roots_for_path(current_path)
  relative_path = File.join(*module_name.split('.')) + '.mt'
  resolved_path = module_roots.lazy.map { |root| File.join(root, relative_path) }.find { |candidate| File.file?(candidate) }
  return resolved_path if resolved_path

  workspace_root = @root_uri ? uri_to_path(@root_uri) : nil
  return nil unless workspace_root && File.directory?(workspace_root)

  workspace_candidate = File.join(workspace_root, relative_path)
  return workspace_candidate if File.file?(workspace_candidate)

  nil
end

#receiver_module_name(receiver_type) ⇒ Object



716
717
718
719
720
# File 'lib/milk_tea/lsp/server/definition.rb', line 716

def receiver_module_name(receiver_type)
  receiver_type = receiver_type.definition if receiver_type.is_a?(Types::StructInstance) || receiver_type.is_a?(Types::VariantInstance)

  receiver_type.module_name
end

#resolve_definition_location(params, stages: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
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
118
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/milk_tea/lsp/server/definition.rb', line 71

def resolve_definition_location(params, stages: nil)
  uri      = params['textDocument']['uri']
  lsp_line = params['position']['line']
  lsp_char = params['position']['character']

  context = measure_perf_stage(stages, 'context') { token_context_at(uri, lsp_line, lsp_char) }
  token = context&.fetch(:token, nil)
  return nil unless token&.type == :identifier

  tokens = context[:tokens]
  token_index = context[:token_index]
  return nil if token_index && module_declaration_info_at(tokens, token_index)

  if token_index && named_argument_label_token?(tokens, token_index)
    location = named_argument_definition_location(uri, token.lexeme, tokens, token_index, stages:)
    return location if location
  end

  import_info = token_index ? measure_perf_stage(stages, 'import_path') { import_path_info_at(tokens, token_index) } : nil
  if import_info
    return module_definition_location(uri, import_info[:module_name])
  end

  facts = measure_perf_stage(stages, 'facts') { @workspace.get_facts(uri) }
  if facts
    facts_location = measure_perf_stage(stages, 'facts_lookup') do
      location = nil
      dot_receiver = @workspace.find_dot_receiver(uri, lsp_line, lsp_char)
      dot_receiver_path = @workspace.find_dot_receiver_path(uri, lsp_line, lsp_char)
      imported_module_name = dot_receiver ? (facts.imports[dot_receiver]&.name || imported_module_name_from_ast(uri, dot_receiver)) : nil

      if token_index && (field_location = resolve_field_member_definition_location(uri, facts, tokens, token_index))
        location = field_location
      elsif token_index && (enum_member_location = resolve_enum_member_definition_location(uri, facts, tokens, token_index))
        location = enum_member_location
      elsif token_index && imported_module_name && module_member_access_info(tokens, token_index)
        location = module_member_definition_location(uri, imported_module_name, token.lexeme)
        location ||= module_definition_location(uri, imported_module_name)
      elsif (type_method = resolve_static_type_receiver_method(facts, dot_receiver, dot_receiver_path, token.lexeme))
        location = module_member_binding_location(uri, type_method[:module_name], token.lexeme, type_method[:binding]) ||
          module_member_definition_location(uri, type_method[:module_name], token.lexeme) ||
          module_definition_location(uri, type_method[:module_name])
      elsif (binding = method_binding_at_token(facts, token))
        location = module_member_binding_location(uri, facts.module_name, token.lexeme, binding)
        location ||= module_member_definition_location(uri, facts.module_name, token.lexeme)
      elsif (binding = facts.functions[token.lexeme])
        location = module_member_binding_location(uri, facts.module_name, token.lexeme, binding)
        location ||= module_member_definition_location(uri, facts.module_name, token.lexeme)
      elsif facts.interfaces[token.lexeme]
        location = module_member_definition_location(uri, facts.module_name, token.lexeme)
      elsif facts.types.key?(token.lexeme)
        location = module_member_definition_location(uri, facts.module_name, token.lexeme)
      elsif (binding = facts.values[token.lexeme])
        location = module_member_binding_location(uri, facts.module_name, token.lexeme, binding)
        location ||= module_member_definition_location(uri, facts.module_name, token.lexeme)
      else
        if imported_module_name
          location = module_member_definition_location(uri, imported_module_name, token.lexeme) || module_definition_location(uri, imported_module_name)
        elsif facts.imports.key?(token.lexeme)
          module_name = facts.imports.fetch(token.lexeme).name
          location = module_member_definition_location(uri, module_name, token.lexeme)
          location ||= module_definition_location(uri, module_name)
        elsif (module_name = imported_module_name_from_ast(uri, token.lexeme))
          location = module_definition_location(uri, module_name)
        end
      end

      location
    end

    return facts_location if facts_location
  end

  return nil if token_index && is_builtin_name?(token.lexeme, tokens, token_index)

  if facts && token_index && !facts_location
    local_location = measure_perf_stage(stages, 'local_binding') do
      name = token.lexeme
      line = lsp_line + 1
      char = lsp_char + 1
      local_binding = resolve_local_hover_binding(facts, name, line, char)
      next nil unless local_binding

      ast_node = local_binding.respond_to?(:ast) ? local_binding.ast : nil
      if ast_node&.line && ast_node.column
        {
          uri: uri,
          range: {
            start: { line: ast_node.line - 1, character: ast_node.column - 1 },
            end: { line: ast_node.line - 1, character: ast_node.column - 1 + name.length }
          }
        }
      else
        declaration_node = find_local_declaration_ast_node(uri, name, line)
        if declaration_node
          {
            uri: uri,
            range: {
              start: { line: declaration_node.line - 1, character: declaration_node.column - 1 },
              end: { line: declaration_node.line - 1, character: declaration_node.column - 1 + name.length }
            }
          }
        end
      end
    end
    return local_location if local_location
  end

  found = measure_perf_stage(stages, 'global_lookup') do
    @workspace.find_definition_token_global(
      token.lexeme,
      preferred_uri: uri,
      before_line: lsp_line + 1,
      before_char: lsp_char + 1,
    )
  end
  return nil unless found

  {
    uri: found[:uri],
    range: token_to_range(found[:token])
  }
end

#resolve_field_member_definition_location(current_uri, facts, tokens, token_index) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
284
285
286
287
# File 'lib/milk_tea/lsp/server/definition.rb', line 228

def resolve_field_member_definition_location(current_uri, facts, tokens, token_index)
  chain = member_access_chain_at(tokens, token_index)
  return nil unless chain

  hovered_segment = chain[:segments].find { |segment| segment[:token_index] == token_index }
  return nil unless hovered_segment && hovered_segment[:position].positive?

  current_type = resolve_dot_receiver_value_type(
    facts,
    chain[:segments].first[:name],
    chain[:line],
    chain[:char],
  )
  unless current_type
    first_name = chain[:segments].first[:name]
    current_type = facts.types[first_name]
  end
  return nil unless current_type

    chain[:segments][1..hovered_segment[:position]].each do |segment|
      field_receiver_type = project_field_receiver_type_for_completion(current_type, facts)
      if field_receiver_type.respond_to?(:field) && (field_type = field_receiver_type.field(segment[:name]))
        return field_definition_location(current_uri, field_receiver_type, segment[:name]) if segment[:token_index] == token_index

        current_type = field_type
        next
      end

      if current_type.respond_to?(:nested_types) && (nested = current_type.nested_types[segment[:name]])
        if segment[:token_index] == token_index
          decl = nested.respond_to?(:ast_declaration) ? nested.ast_declaration : nil
          if decl&.line && decl.column
            return {
              uri: current_uri,
              range: {
                start: { line: decl.line - 1, character: decl.column - 1 },
                end: { line: decl.line - 1, character: decl.column - 1 + segment[:name].length },
              },
            }
          end
          return nil
        end
        current_type = nested
        next
      end

      if segment[:token_index] == token_index
        method_receiver_type = project_method_receiver_type_for_completion(current_type)
        method_info = member_method_info_for_receiver_type(facts, method_receiver_type, segment[:name])
        return nil unless method_info

        return module_member_binding_location(current_uri, method_info[:module_name], segment[:name], method_info[:binding]) ||
          module_member_definition_location(current_uri, method_info[:module_name], segment[:name])
      end

    break
  end

  nil
end

#resolve_implementation_locations(params, stages: nil) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/milk_tea/lsp/server/definition.rb', line 330

def resolve_implementation_locations(params, stages: nil)
  uri      = params['textDocument']['uri']
  lsp_line = params['position']['line']
  lsp_char = params['position']['character']

  token = measure_perf_stage(stages, 'token') { @workspace.find_token_at(uri, lsp_line, lsp_char) }
  return [] unless token&.type == :identifier

  facts = measure_perf_stage(stages, 'facts') { @workspace.get_facts(uri) }
  return [] unless facts

  target = measure_perf_stage(stages, 'target_lookup') { resolve_interface_method_target_at_token(facts, token) }
  if target
    return measure_perf_stage(stages, 'implementation_lookup') do
      interface_method_implementation_locations(target[:interface], target[:method])
    end
  end

  interface_binding = measure_perf_stage(stages, 'binding_lookup') do
    resolve_interface_binding_at_position(uri, facts, token, lsp_line, lsp_char)
  end
  return [] unless interface_binding

  measure_perf_stage(stages, 'implementation_lookup') { interface_implementation_locations(interface_binding) }
end

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



634
635
636
637
638
639
640
641
642
# File 'lib/milk_tea/lsp/server/definition.rb', line 634

def resolve_interface_binding_at_position(uri, facts, token, lsp_line, lsp_char)
  binding = facts.interfaces[token.lexeme]
  return binding if binding

  dot_receiver = @workspace.find_dot_receiver(uri, lsp_line, lsp_char)
  return nil unless dot_receiver

  facts.imports[dot_receiver]&.interfaces&.fetch(token.lexeme, nil)
end

#resolve_interface_method_target_at_token(facts, token) ⇒ Object



644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/milk_tea/lsp/server/definition.rb', line 644

def resolve_interface_method_target_at_token(facts, token)
  facts.interfaces.each_value do |interface_binding|
    method_binding = interface_binding.methods[token.lexeme]
    next unless method_binding
    next unless method_binding.ast.line == token.line
    next unless method_binding.ast.column == token.column

    return { interface: interface_binding, method: method_binding }
  end

  nil
end

#resolve_type_definition_location(params, stages: nil) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/milk_tea/lsp/server/definition.rb', line 289

def resolve_type_definition_location(params, stages: nil)
  uri      = params['textDocument']['uri']
  lsp_line = params['position']['line']
  lsp_char = params['position']['character']

  context = measure_perf_stage(stages, 'context') { token_context_at(uri, lsp_line, lsp_char) }
  token = context&.fetch(:token, nil)
  return nil unless token&.type == :identifier

  name = token.lexeme
  tokens = context[:tokens]
  token_index = context[:token_index]

  import_info = token_index ? measure_perf_stage(stages, 'import_path') { import_path_info_at(tokens, token_index) } : nil
  if import_info
    return module_definition_location(uri, import_info[:module_name])
  end

  facts = measure_perf_stage(stages, 'facts') { @workspace.get_facts(uri) }
  return nil unless facts

  dot_receiver = @workspace.find_dot_receiver(uri, lsp_line, lsp_char)
  imported_module_name = dot_receiver ? (facts.imports[dot_receiver]&.name || imported_module_name_from_ast(uri, dot_receiver)) : nil

  if facts.types.key?(name) || facts.interfaces.key?(name)
    return module_member_definition_location(uri, facts.module_name, name)
  end

  if token_index && imported_module_name && module_member_access_info(tokens, token_index)
    return module_member_definition_location(uri, imported_module_name, name) ||
          module_definition_location(uri, imported_module_name)
  end

  if imported_module_name && (facts.types.key?(imported_module_name) || facts.interfaces.key?(imported_module_name))
    return module_member_definition_location(uri, imported_module_name, name) ||
          module_definition_location(uri, imported_module_name)
  end

  handle_definition_request('textDocument/typeDefinition', params, error_label: 'typeDefinition')
end

#same_interface_binding?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


736
737
738
# File 'lib/milk_tea/lsp/server/definition.rb', line 736

def same_interface_binding?(left, right)
  left.name == right.name && left.module_name == right.module_name
end