Skip to content

Commit 37c3402

Browse files
committed
properly handle locals in binding
1 parent b785a3a commit 37c3402

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/elixir_sense/core/binding.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ defmodule ElixirSense.Core.Binding do
1313
variables: [],
1414
attributes: [],
1515
current_module: nil,
16+
function: nil,
1617
functions: [],
1718
macros: [],
1819
specs: %{},
@@ -28,6 +29,7 @@ defmodule ElixirSense.Core.Binding do
2829
macros: env.macros,
2930
specs: metadata.specs,
3031
current_module: env.module,
32+
function: env.function,
3133
types: metadata.types,
3234
mods_funs: metadata.mods_funs_to_positions
3335
}
@@ -308,7 +310,13 @@ defmodule ElixirSense.Core.Binding do
308310
{functions, macros}
309311
|> Introspection.combine_imports()
310312

311-
candidate_targets = List.wrap(current_module) ++ combined_imports ++ [Kernel.SpecialForms]
313+
candidate_targets =
314+
if current_module && env.function do
315+
# locals are available only in defs
316+
[current_module]
317+
else
318+
[]
319+
end ++ combined_imports ++ [Kernel.SpecialForms]
312320

313321
# take first matching
314322
Enum.find_value(candidate_targets, fn

test/elixir_sense/core/binding_test.exs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,36 @@ defmodule ElixirSense.Core.BindingTest do
12401240
%VarInfo{version: 1, name: :ref, type: {:local_call, :fun, []}}
12411241
],
12421242
current_module: MyMod,
1243+
function: {:some, 0},
1244+
specs: %{
1245+
{MyMod, :fun, 0} => %SpecInfo{
1246+
specs: ["@spec fun() :: %MyMod{}"]
1247+
}
1248+
},
1249+
mods_funs: %{
1250+
{MyMod, :fun, 0} => %ModFunInfo{
1251+
params: [[]],
1252+
type: :defp
1253+
}
1254+
},
1255+
structs: %{
1256+
MyMod => %StructInfo{
1257+
fields: [abc: nil, __struct__: MyMod]
1258+
}
1259+
}
1260+
}),
1261+
{:variable, :ref, 1}
1262+
)
1263+
1264+
assert :none ==
1265+
Binding.expand(
1266+
@env
1267+
|> Map.merge(%{
1268+
variables: [
1269+
%VarInfo{version: 1, name: :ref, type: {:local_call, :fun, []}}
1270+
],
1271+
current_module: MyMod,
1272+
function: nil,
12431273
specs: %{
12441274
{MyMod, :fun, 0} => %SpecInfo{
12451275
specs: ["@spec fun() :: %MyMod{}"]
@@ -1270,6 +1300,7 @@ defmodule ElixirSense.Core.BindingTest do
12701300
%VarInfo{version: 1, name: :ref, type: {:local_call, :fun, []}}
12711301
],
12721302
current_module: MyMod,
1303+
function: {:some, 0},
12731304
specs: %{
12741305
{MyMod, :fun, 0} => %SpecInfo{
12751306
specs: ["@spec fun() :: t()"]
@@ -1305,6 +1336,7 @@ defmodule ElixirSense.Core.BindingTest do
13051336
%VarInfo{version: 1, name: :ref, type: {:local_call, :fun, []}}
13061337
],
13071338
current_module: MyMod,
1339+
function: {:some, 0},
13081340
specs: %{
13091341
{MyMod, :fun, 0} => %SpecInfo{
13101342
specs: ["@spec fun() :: t()"]
@@ -1443,6 +1475,7 @@ defmodule ElixirSense.Core.BindingTest do
14431475
@env
14441476
|> Map.merge(%{
14451477
current_module: MyMod,
1478+
function: {:some, 0},
14461479
specs: %{
14471480
{MyMod, :fun, 3} => %SpecInfo{
14481481
specs: ["@spec fun(integer(), integer(), any()) :: %MyMod{}"]

0 commit comments

Comments
 (0)