Module: MilkTea::LSP::Server::ServerCallHierarchy

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

Instance Method Summary collapse

Instance Method Details

#handle_incoming_calls(params) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/milk_tea/lsp/server/call_hierarchy.rb', line 48

def handle_incoming_calls(params)
  item = params["item"] || params[:item]
  data = item["data"] || item[:data]
  return [] unless data.is_a?(Hash) || data.respond_to?(:[])

  target_name = data["name"] || data[:name]
  return [] if target_name.to_s.empty?

  target_uri = data["uri"] || data[:uri]
  candidate_uris = nil
  if target_uri
    target_facts = @workspace.get_facts(target_uri)
    if target_facts&.module_name
      importing = @workspace.reverse_import_dependents_for(target_facts.module_name)
      if importing && !importing.empty?
        candidate_uris = importing.to_a + [target_uri]
      end
    end
  end

  refs = if candidate_uris
           @workspace.find_all_references_in(target_name, candidate_uris)
         else
           @workspace.find_all_references(target_name)
         end
  return [] if refs.empty?

  caller_map = {}
  refs.each do |ref|
    call_uri = ref[:uri]
    ref_line = ref[:range][:start][:line] + 1

    caller_func = find_enclosing_function(call_uri, ref_line)
    next unless caller_func
    next if caller_func[:name] == target_name && call_uri == data["uri"]

    key = [call_uri, caller_func[:name]]
    caller_map[key] ||= { uri: call_uri, name: caller_func[:name], ranges: [], kind: caller_func[:kind] }
    caller_map[key][:ranges] << ref[:range]
  end

  caller_map.map do |_, caller|
    {
      from: {
        name: caller[:name],
        kind: caller[:kind],
        uri: caller[:uri],
        range: caller[:ranges].first,
        selectionRange: caller[:ranges].first,
        data: { name: caller[:name], uri: caller[:uri], kind: caller[:kind] },
      },
      fromRanges: caller[:ranges],
    }
  end
end

#handle_outgoing_calls(params) ⇒ Object



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
# File 'lib/milk_tea/lsp/server/call_hierarchy.rb', line 104

def handle_outgoing_calls(params)
  item = params["item"] || params[:item]
  data = item["data"] || item[:data]
  return [] unless data.is_a?(Hash) || data.respond_to?(:[])

  target_name = data["name"] || data[:name]
  target_uri = data["uri"] || data[:uri]
  return [] if target_name.to_s.empty?

  facts = @workspace.get_facts(target_uri)
  return [] unless facts

  func_binding = facts.functions[target_name]
  unless func_binding
    facts.methods&.each_value do |method_map|
      func_binding = method_map[target_name]
      break if func_binding
    end
  end
  return [] unless func_binding
  return [] unless func_binding.ast.respond_to?(:body)

  callees = collect_call_callees(func_binding.ast.body, facts)
  return [] if callees.empty?

  result = []
  callees.each do |callee_name, ranges|
    def_entry = @workspace.find_definition_token_global(callee_name, preferred_uri: target_uri)
    def_uri = def_entry ? def_entry[:uri].to_s : target_uri
    def_range = def_entry ? token_range(def_entry[:token]) : ranges.first

    result << {
      to: {
        name: callee_name,
        kind: 12,
        uri: def_uri,
        range: def_range,
        selectionRange: def_range,
        data: { name: callee_name, uri: def_uri, kind: 12 },
      },
      fromRanges: ranges,
    }
  end

  result.take(50)
end

#handle_prepare_call_hierarchy(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/milk_tea/lsp/server/call_hierarchy.rb', line 7

def handle_prepare_call_hierarchy(params)
  text_document = params["textDocument"]
  position = params["position"]
  uri = text_document["uri"]
  lsp_line = position["line"]
  lsp_char = position["character"]

  facts = @workspace.get_facts(uri)
  return nil unless facts

  token = @workspace.find_token_at(uri, lsp_line, lsp_char)
  return nil unless token
  return nil unless token.type == :identifier

  func_name = token.lexeme

  func_binding = facts.functions[func_name]
  if func_binding
    return [build_call_hierarchy_item(func_name, uri, func_binding, facts)]
  end

  dot_recv = @workspace.find_dot_receiver(uri, lsp_line, lsp_char)
  if dot_recv
    if (module_binding = facts.imports[dot_recv])
      if module_binding.functions.key?(func_name)
        return [build_call_hierarchy_item(func_name, uri, module_binding.functions[func_name], facts)]
      end
    end
  end

  if facts.methods
    facts.methods.each_value do |method_map|
      if method_map.key?(func_name)
        return [build_call_hierarchy_item(func_name, uri, method_map[func_name], facts)]
      end
    end
  end

  nil
end