Skip to content

Commit b6dbcfe

Browse files
committed
locals are defined only in defs
1 parent 6b3ccbf commit b6dbcfe

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

apps/elixir_ls_utils/lib/completion_engine.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,11 @@ defmodule ElixirLS.Utils.CompletionEngine do
467467
cursor_position
468468
)
469469

470-
current_module_locals =
470+
current_module_locals = if env.module && env.function do
471471
match_module_funs(env.module, hint, exact?, false, :all, env, metadata, cursor_position)
472+
else
473+
[]
474+
end
472475

473476
imported_locals =
474477
{env.functions, env.macros}
@@ -1070,11 +1073,14 @@ defmodule ElixirLS.Utils.CompletionEngine do
10701073
%Metadata{} = metadata,
10711074
cursor_position
10721075
) do
1073-
case metadata.mods_funs_to_positions[{mod, nil, nil}] do
1074-
nil ->
1076+
cond do
1077+
not Map.has_key?(metadata.mods_funs_to_positions, {mod, nil, nil}) ->
10751078
[]
1079+
# mod == env.module and is_nil(env.function) ->
1080+
# # locals are not available in module body
1081+
# []
10761082

1077-
_funs ->
1083+
true ->
10781084
# local macros are available after definition
10791085
# local functions are hoisted
10801086
for {{^mod, f, a}, %State.ModFunInfo{} = info} <- metadata.mods_funs_to_positions,

apps/elixir_ls_utils/test/complete_test.exs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ defmodule ElixirLS.Utils.CompletionEngineTest do
12871287
}
12881288
}
12891289

1290-
assert [_ | _] = expand(~c"my_f", env, metadata)
1290+
assert [_ | _] = expand(~c"my_f", %{env | function: {:foo, 1}}, metadata)
12911291

12921292
assert [
12931293
%{
@@ -1297,31 +1297,34 @@ defmodule ElixirLS.Utils.CompletionEngineTest do
12971297
type: :function,
12981298
spec: "@spec my_fun_priv(atom, integer) :: boolean"
12991299
}
1300-
] = expand(~c"my_fun_pr", env, metadata)
1300+
] = expand(~c"my_fun_pr", %{env | function: {:foo, 1}}, metadata)
13011301

13021302
assert [
13031303
%{name: "my_fun_pub", origin: "MyModule", type: :function}
1304-
] = expand(~c"my_fun_pu", env, metadata)
1304+
] = expand(~c"my_fun_pu", %{env | function: {:foo, 1}}, metadata)
13051305

13061306
assert [
13071307
%{name: "my_macro_priv", origin: "MyModule", type: :macro}
1308-
] = expand(~c"my_macro_pr", env, metadata)
1308+
] = expand(~c"my_macro_pr", %{env | function: {:foo, 1}}, metadata)
13091309

13101310
assert [
13111311
%{name: "my_macro_pub", origin: "MyModule", type: :macro}
1312-
] = expand(~c"my_macro_pu", env, metadata)
1312+
] = expand(~c"my_macro_pu", %{env | function: {:foo, 1}}, metadata)
13131313

13141314
assert [
13151315
%{name: "my_guard_priv", origin: "MyModule", type: :macro}
1316-
] = expand(~c"my_guard_pr", env, metadata)
1316+
] = expand(~c"my_guard_pr", %{env | function: {:foo, 1}}, metadata)
13171317

13181318
assert [
13191319
%{name: "my_guard_pub", origin: "MyModule", type: :macro}
1320-
] = expand(~c"my_guard_pu", env, metadata)
1320+
] = expand(~c"my_guard_pu", %{env | function: {:foo, 1}}, metadata)
13211321

13221322
assert [
13231323
%{name: "my_delegated", origin: "MyModule", type: :function}
1324-
] = expand(~c"my_de", env, metadata)
1324+
] = expand(~c"my_de", %{env | function: {:foo, 1}}, metadata)
1325+
1326+
# locals are not available in module body
1327+
assert [] == expand(~c"my_f", %{env | function: nil}, metadata)
13251328
end
13261329

13271330
test "complete remote funs from imported module" do
@@ -2230,7 +2233,7 @@ defmodule ElixirLS.Utils.CompletionEngineTest do
22302233
needed_require: nil,
22312234
visibility: :public
22322235
}
2233-
] = expand(~c"inf", %Env{requires: [], module: MyModule}, metadata)
2236+
] = expand(~c"inf", %Env{requires: [], module: MyModule, function: {:foo, 1}}, metadata)
22342237
end
22352238

22362239
if Version.match?(System.version(), ">= 1.14.0") do

0 commit comments

Comments
 (0)