Skip to content

Commit 6ec0b39

Browse files
committed
trace on_load and on_definition
1 parent a2bb2e2 commit 6ec0b39

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

lib/elixir_sense/core/compiler.ex

+22-10
Original file line numberDiff line numberDiff line change
@@ -1748,15 +1748,16 @@ defmodule ElixirSense.Core.Compiler do
17481748
{_result, state, e_env} = expand(block, state, %{env | module: full})
17491749

17501750
# here we handle module callbacks. Only before_compile macro callbacks are expanded as they
1751-
# affect module body. Func before_compile callbacks are not executed. after_compile and after_verify
1751+
# affect module body. Func before_compile callbacks are not executed. on_definition after_compile and after_verify
17521752
# are not executed as we do not preform a real compilation
17531753
{state, _e_env} =
1754-
~w(before_compile after_compile after_verify)a
1754+
~w(before_compile after_compile after_verify on_definition on_load)a
17551755
|> Enum.reduce({state, e_env}, fn attribute, {state, e_env} ->
17561756
for args <- Map.get(state.attribute_store, {full, attribute}, []) do
1757-
case args do
1758-
{module, fun} -> [module, fun]
1759-
module -> [module, :"__#{attribute}__"]
1757+
case {attribute, args} do
1758+
{:on_load, function} -> function
1759+
{_, {module, fun}} -> [module, fun]
1760+
{_, module} -> [module, :"__#{attribute}__"]
17601761
end
17611762
end
17621763
|> Enum.reduce({state, e_env}, fn target, {state, env} ->
@@ -1773,14 +1774,25 @@ defmodule ElixirSense.Core.Compiler do
17731774
:before_compile -> [env]
17741775
:after_compile -> [env, <<>>]
17751776
:after_verify -> [full]
1777+
:on_definition -> [env, nil, nil, [], [], []]
1778+
:on_load -> []
17761779
end
17771780

17781781
ast =
1779-
{:__block__, [],
1780-
[
1781-
{:require, [], [hd(target)]},
1782-
{{:., [line: meta[:line]], target}, [line: meta[:line]], args}
1783-
]}
1782+
case target do
1783+
function when is_atom(function) ->
1784+
{:__block__, [],
1785+
[
1786+
{function, [line: meta[:line]], args}
1787+
]}
1788+
1789+
[module, _function] ->
1790+
{:__block__, [],
1791+
[
1792+
{:require, [], [module]},
1793+
{{:., [line: meta[:line]], target}, [line: meta[:line]], args}
1794+
]}
1795+
end
17841796

17851797
{_result, state, env} = expand(ast, state, env)
17861798
{State.remove_func_vars_scope(state, state_orig), env}

test/elixir_sense/core/metadata_builder_test.exs

+42-3
Original file line numberDiff line numberDiff line change
@@ -7547,6 +7547,15 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
75477547
IO.inspect(module)
75487548
:ok
75497549
end
7550+
7551+
def __on_definition__(env, kind, name, args, guards, body) do
7552+
IO.inspect(env)
7553+
IO.inspect(kind)
7554+
IO.inspect(name)
7555+
IO.inspect(args)
7556+
IO.inspect(guards)
7557+
IO.inspect(body)
7558+
end
75507559
end
75517560

75527561
test "registers module callback calls" do
@@ -7557,6 +7566,12 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
75577566
@before_compile ElixirSense.Core.MetadataBuilderTest.ModuleCallbacks
75587567
@after_compile ElixirSense.Core.MetadataBuilderTest.ModuleCallbacks
75597568
@after_verify ElixirSense.Core.MetadataBuilderTest.ModuleCallbacks
7569+
@on_definition ElixirSense.Core.MetadataBuilderTest.ModuleCallbacks
7570+
@on_load :load_check
7571+
7572+
def load_check do
7573+
:ok
7574+
end
75607575
end
75617576
"""
75627577
|> string_to_state
@@ -7617,9 +7632,33 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
76177632
)
76187633
end)
76197634

7620-
# TODO: on_load and on_definition callbacks are not registered
7621-
# https://github.com/elixir-lang/elixir/issues/14427
7622-
# assert [] = state.calls[1]
7635+
assert state.calls[1]
7636+
|> Enum.any?(fn info ->
7637+
match?(
7638+
%ElixirSense.Core.State.CallInfo{
7639+
arity: 6,
7640+
position: {1, nil},
7641+
mod: ElixirSense.Core.MetadataBuilderTest.ModuleCallbacks,
7642+
func: :__on_definition__,
7643+
kind: :remote_function
7644+
},
7645+
info
7646+
)
7647+
end)
7648+
7649+
assert state.calls[1]
7650+
|> Enum.any?(fn info ->
7651+
match?(
7652+
%ElixirSense.Core.State.CallInfo{
7653+
arity: 0,
7654+
position: {1, nil},
7655+
mod: WithCallbacks,
7656+
func: :load_check,
7657+
kind: :local_function
7658+
},
7659+
info
7660+
)
7661+
end)
76237662
end
76247663

76257664
defmodule StructExpansion do

0 commit comments

Comments
 (0)