Skip to content

Commit 848c1a7

Browse files
committed
trace more dependencies
1 parent 9dce5ba commit 848c1a7

File tree

1 file changed

+82
-29
lines changed
  • apps/language_server/lib/language_server

1 file changed

+82
-29
lines changed

apps/language_server/lib/language_server/tracer.ex

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -199,43 +199,44 @@ defmodule ElixirLS.LanguageServer.Tracer do
199199
:ok
200200
end
201201

202-
def trace({kind, meta, module, name, arity}, %Macro.Env{} = env)
202+
def trace({kind, meta, module, name, arity} = event, %Macro.Env{} = env)
203203
when kind in [:imported_function, :imported_macro, :remote_function, :remote_macro] do
204-
register_call(meta, module, name, arity, kind, env)
204+
register_call(meta, module, name, arity, kind, event, env)
205205
end
206206

207-
def trace({:imported_quoted, meta, module, name, arities}, env) do
207+
def trace({:imported_quoted, meta, module, name, arities} = event, %Macro.Env{} = env) do
208208
for arity <- arities do
209-
register_call(meta, module, name, arity, :imported_quoted, env)
209+
register_call(meta, module, name, arity, :imported_quoted, event, env)
210210
end
211211

212212
:ok
213213
end
214214

215-
def trace({kind, meta, name, arity}, %Macro.Env{} = env)
215+
def trace({kind, meta, name, arity} = event, %Macro.Env{} = env)
216216
when kind in [:local_function, :local_macro] do
217-
register_call(meta, env.module, name, arity, kind, env)
217+
register_call(meta, env.module, name, arity, kind, event, env)
218218
end
219219

220-
def trace({:alias_reference, meta, module}, %Macro.Env{} = env) do
221-
register_call(meta, module, nil, nil, :alias_reference, env)
220+
def trace({:alias_reference, meta, module} = event, %Macro.Env{} = env) do
221+
register_call(meta, module, nil, nil, :alias_reference, event, env)
222222
end
223223

224-
def trace({:alias, meta, module, _as, _opts}, %Macro.Env{} = env) do
225-
register_call(meta, module, nil, nil, :alias, env)
224+
def trace({:alias, meta, module, _as, _opts} = event, %Macro.Env{} = env) do
225+
register_call(meta, module, nil, nil, :alias, event, env)
226226
end
227227

228-
def trace({kind, meta, module, _opts}, %Macro.Env{} = env) when kind in [:import, :require] do
229-
register_call(meta, module, nil, nil, kind, env)
228+
def trace({kind, meta, module, _opts} = event, %Macro.Env{} = env)
229+
when kind in [:import, :require] do
230+
register_call(meta, module, nil, nil, kind, event, env)
230231
end
231232

232-
def trace({:struct_expansion, meta, name, _assocs}, %Macro.Env{} = env) do
233-
register_call(meta, name, nil, nil, :struct_expansion, env)
233+
def trace({:struct_expansion, meta, name, _assocs} = event, %Macro.Env{} = env) do
234+
register_call(meta, name, nil, nil, :struct_expansion, event, env)
234235
end
235236

236-
def trace({:alias_expansion, meta, as, alias}, %Macro.Env{} = env) do
237-
register_call(meta, as, nil, nil, :alias_expansion_as, env)
238-
register_call(meta, alias, nil, nil, :alias_expansion, env)
237+
def trace({:alias_expansion, meta, as, alias} = event, %Macro.Env{} = env) do
238+
register_call(meta, as, nil, nil, :alias_expansion_as, event, env)
239+
register_call(meta, alias, nil, nil, :alias_expansion, event, env)
239240
end
240241

241242
def trace(_trace, _env) do
@@ -283,43 +284,95 @@ defmodule ElixirLS.LanguageServer.Tracer do
283284
}
284285
end
285286

286-
defp register_call(meta, module, name, arity, kind, env) do
287+
defp register_call(meta, module, name, arity, kind, event, env) do
287288
if in_project_sources?(env.file) do
288-
do_register_call(meta, module, name, arity, kind, env)
289+
do_register_call(meta, module, name, arity, kind, event, env)
289290
end
290291

291292
:ok
292293
end
293294

294-
defp do_register_call(meta, module, name, arity, kind, env) do
295+
defp do_register_call(meta, module, name, arity, kind, event, env) do
295296
callee = {module, name, arity}
296297

297298
line = meta[:line]
298299
column = meta[:column]
299300

301+
# Determine reference type based on kind (similar to Mix.Tasks.Xref)
302+
reference_type = determine_reference_type(event, env)
303+
304+
# Store call info with reference type
305+
call_info = %{
306+
kind: kind,
307+
reference_type: reference_type,
308+
caller_module: env.module,
309+
caller_function: env.function
310+
}
311+
300312
# TODO meta can have last or maybe other?
301313
# last
302314
# end_of_expression
303315
# closing
304316

305-
:ets.insert(table_name(:calls), {{callee, env.file, line, column}, kind})
317+
:ets.insert(table_name(:calls), {{callee, env.file, line, column}, call_info})
306318
end
307319

320+
# Determine reference type based on trace kind (following Mix.Tasks.Xref logic)
321+
def determine_reference_type({:alias_reference, _meta, module}, %Macro.Env{} = env)
322+
when env.module != module do
323+
case env do
324+
%Macro.Env{function: nil} -> :compile
325+
%Macro.Env{context: nil} -> :runtime
326+
%Macro.Env{} -> nil
327+
end
328+
end
329+
330+
def determine_reference_type({:require, meta, _module, _opts}, _env),
331+
do: require_mode(meta)
332+
333+
def determine_reference_type({:struct_expansion, _meta, _module, _keys}, _env),
334+
do: :export
335+
336+
def determine_reference_type({:remote_function, _meta, _module, _function, _arity}, env),
337+
do: mode(env)
338+
339+
def determine_reference_type({:remote_macro, _meta, _module, _function, _arity}, _env),
340+
do: :compile
341+
342+
def determine_reference_type({:imported_function, _meta, _module, _function, _arity}, env),
343+
do: mode(env)
344+
345+
def determine_reference_type({:imported_macro, _meta, _module, _function, _arity}, _env),
346+
do: :compile
347+
348+
def determine_reference_type(_event, _env),
349+
do: nil
350+
351+
defp require_mode(meta), do: if(meta[:from_macro], do: :compile, else: :export)
352+
353+
defp mode(%Macro.Env{function: nil}), do: :compile
354+
defp mode(_), do: :runtime
355+
308356
def get_trace do
309357
# TODO get by callee
310358
table = table_name(:calls)
311359
:ets.safe_fixtable(table, true)
312360

313361
try do
314362
:ets.tab2list(table)
315-
|> Enum.map(fn {{callee, file, line, column}, kind} ->
316-
%{
317-
callee: callee,
318-
file: file,
319-
line: line,
320-
column: column,
321-
kind: kind
322-
}
363+
|> Enum.map(fn
364+
# Handle new format with call_info map
365+
{{callee, file, line, column}, %{} = call_info} ->
366+
%{
367+
callee: callee,
368+
file: file,
369+
line: line,
370+
column: column,
371+
kind: call_info.kind,
372+
reference_type: call_info.reference_type,
373+
caller_module: call_info.caller_module,
374+
caller_function: call_info.caller_function
375+
}
323376
end)
324377
|> Enum.group_by(fn %{callee: callee} -> callee end)
325378
after

0 commit comments

Comments
 (0)