Skip to content

Commit

Permalink
fixup! Add dependency injection inference
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Aguiar committed May 16, 2021
1 parent c2254c1 commit 8b74fea
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
17 changes: 13 additions & 4 deletions lib/elixir_sense/core/binding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,23 @@ defmodule ElixirSense.Core.Binding do

# dependency injection
def do_expand(env, {:call, {:atom, Application}, fun, args}, stack)
when fun in ~w(get_env get_env! fetch_env fetch_env!)a do
when fun in ~w(compile_env!)a do
# `Application.compile_env!/2` underneath works like `fetch_env!/2`
do_expand(env, {:call, {:atom, Application}, :fetch_env!, args}, stack)
end

def do_expand(env, {:call, {:atom, Application}, fun, args}, stack)
when fun in ~w(compile_env)a do
# `Application.compile_env/3` underneath works like `get_env/3`
do_expand(env, {:call, {:atom, Application}, :get_env, args}, stack)
end

def do_expand(_env, {:call, {:atom, Application}, fun, args}, _stack)
when fun in ~w(get_env fetch_env!)a do
try do
mod = apply(Application, fun, Enum.map(args, &elem(&1, 1)))

case mod do
{:ok, mod} when is_atom(mod) ->
expand(env, {:atom, mod}, stack)

nil ->
:none

Expand Down
4 changes: 2 additions & 2 deletions test/elixir_sense/core/binding_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule ElixirSense.Core.BindingTest do
test "misconfigured dependency injection" do
Application.delete_env(:elixir_sense, :some_attribute)

fetchers = [:get_env, :fetch_env, :fetch_env!]
fetchers = [:get_env, :fetch_env!, :compile_env, :compile_env!]

Enum.each(fetchers, fn fetcher ->
assert :none ==
Expand All @@ -47,7 +47,7 @@ defmodule ElixirSense.Core.BindingTest do
test "dependency injection without default value" do
Application.put_env(:elixir_sense, :some_attribute, ElixirSenseExample.SameModule)

fetchers = [:get_env, :fetch_env, :fetch_env!]
fetchers = [:get_env, :fetch_env!, :compile_env, :compile_env!]

Enum.each(fetchers, fn fetcher ->
assert {:atom, ElixirSenseExample.SameModule} ==
Expand Down
38 changes: 36 additions & 2 deletions test/elixir_sense/providers/suggestion/complete_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -856,17 +856,51 @@ defmodule ElixirSense.Providers.Suggestion.CompleteTest do
},
attributes: [
%AttributeInfo{
name: :some_module,
name: :get_module,
type:
{:call, {:atom, Application}, :get_env,
[atom: :elixir_sense, atom: :some_attribute, atom: Some.OtherModule]}
},
%AttributeInfo{
name: :compile_module,
type:
{:call, {:atom, Application}, :compile_env,
[atom: :elixir_sense, atom: :some_attribute, atom: Some.OtherModule]}
},
%AttributeInfo{
name: :fetch_module,
type:
{:call, {:atom, Application}, :fetch_env!,
[atom: :elixir_sense, atom: :other_attribute]}
},
%AttributeInfo{
name: :compile_bang_module,
type:
{:call, {:atom, Application}, :compile_env!,
[atom: :elixir_sense, atom: :other_attribute]}
}
]
}

assert [
%{name: "my_fun_other_pub", origin: "Some.OtherModule"}
] = expand('@some_module.my_f', env)
] = expand('@get_module.my_f', env)

assert [
%{name: "my_fun_other_pub", origin: "Some.OtherModule"}
] = expand('@compile_module.my_f', env)

Application.put_env(:elixir_sense, :other_attribute, Some.OtherModule)

assert [
%{name: "my_fun_other_pub", origin: "Some.OtherModule"}
] = expand('@fetch_module.my_f', env)

assert [
%{name: "my_fun_other_pub", origin: "Some.OtherModule"}
] = expand('@compile_bang_module.my_f', env)
after
Application.delete_env(:elixir_sense, :other_attribute)
end

test "complete modules" do
Expand Down

0 comments on commit 8b74fea

Please sign in to comment.