@@ -199,43 +199,44 @@ defmodule ElixirLS.LanguageServer.Tracer do
199
199
:ok
200
200
end
201
201
202
- def trace ( { kind , meta , module , name , arity } , % Macro.Env { } = env )
202
+ def trace ( { kind , meta , module , name , arity } = event , % Macro.Env { } = env )
203
203
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 )
205
205
end
206
206
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
208
208
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 )
210
210
end
211
211
212
212
:ok
213
213
end
214
214
215
- def trace ( { kind , meta , name , arity } , % Macro.Env { } = env )
215
+ def trace ( { kind , meta , name , arity } = event , % Macro.Env { } = env )
216
216
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 )
218
218
end
219
219
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 )
222
222
end
223
223
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 )
226
226
end
227
227
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 )
230
231
end
231
232
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 )
234
235
end
235
236
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 )
239
240
end
240
241
241
242
def trace ( _trace , _env ) do
@@ -283,43 +284,95 @@ defmodule ElixirLS.LanguageServer.Tracer do
283
284
}
284
285
end
285
286
286
- defp register_call ( meta , module , name , arity , kind , env ) do
287
+ defp register_call ( meta , module , name , arity , kind , event , env ) do
287
288
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 )
289
290
end
290
291
291
292
:ok
292
293
end
293
294
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
295
296
callee = { module , name , arity }
296
297
297
298
line = meta [ :line ]
298
299
column = meta [ :column ]
299
300
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
+
300
312
# TODO meta can have last or maybe other?
301
313
# last
302
314
# end_of_expression
303
315
# closing
304
316
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 } )
306
318
end
307
319
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
+
308
356
def get_trace do
309
357
# TODO get by callee
310
358
table = table_name ( :calls )
311
359
:ets . safe_fixtable ( table , true )
312
360
313
361
try do
314
362
: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
+ }
323
376
end )
324
377
|> Enum . group_by ( fn % { callee: callee } -> callee end )
325
378
after
0 commit comments