Skip to content

Commit c69c9cd

Browse files
committed
pass cursor position to binding
1 parent 3aae909 commit c69c9cd

File tree

17 files changed

+137
-61
lines changed

17 files changed

+137
-61
lines changed

apps/elixir_ls_utils/lib/completion_engine.ex

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ defmodule ElixirLS.Utils.CompletionEngine do
261261
) do
262262
filter = struct_module_filter(only_structs, env, metadata)
263263

264-
case expand_dot_path(path, env, metadata) do
264+
case expand_dot_path(path, env, metadata, cursor_position) do
265265
{:ok, {:atom, mod}} when hint == "" ->
266266
expand_aliases(
267267
mod,
@@ -290,23 +290,38 @@ defmodule ElixirLS.Utils.CompletionEngine do
290290
end
291291

292292
# elixir >= 1.14
293-
defp expand_dot_path({:var, ~c"__MODULE__"}, %State.Env{} = env, %Metadata{} = _metadata) do
293+
defp expand_dot_path(
294+
{:var, ~c"__MODULE__"},
295+
%State.Env{} = env,
296+
%Metadata{} = _metadata,
297+
_cursor_position
298+
) do
294299
if env.module != nil and Introspection.elixir_module?(env.module) do
295300
{:ok, {:atom, env.module}}
296301
else
297302
:error
298303
end
299304
end
300305

301-
defp expand_dot_path({:var, var}, %State.Env{} = env, %Metadata{} = metadata) do
302-
value_from_binding({:variable, List.to_atom(var), :any}, env, metadata)
306+
defp expand_dot_path({:var, var}, %State.Env{} = env, %Metadata{} = metadata, cursor_position) do
307+
value_from_binding({:variable, List.to_atom(var), :any}, env, metadata, cursor_position)
303308
end
304309

305-
defp expand_dot_path({:module_attribute, attribute}, %State.Env{} = env, %Metadata{} = metadata) do
306-
value_from_binding({:attribute, List.to_atom(attribute)}, env, metadata)
310+
defp expand_dot_path(
311+
{:module_attribute, attribute},
312+
%State.Env{} = env,
313+
%Metadata{} = metadata,
314+
cursor_position
315+
) do
316+
value_from_binding({:attribute, List.to_atom(attribute)}, env, metadata, cursor_position)
307317
end
308318

309-
defp expand_dot_path({:alias, hint}, %State.Env{} = env, %Metadata{} = metadata) do
319+
defp expand_dot_path(
320+
{:alias, hint},
321+
%State.Env{} = env,
322+
%Metadata{} = metadata,
323+
_cursor_position
324+
) do
310325
alias = hint |> List.to_string() |> String.split(".") |> value_from_alias(env, metadata)
311326

312327
case alias do
@@ -319,7 +334,8 @@ defmodule ElixirLS.Utils.CompletionEngine do
319334
defp expand_dot_path(
320335
{:alias, {:local_or_var, var}, hint},
321336
%State.Env{} = env,
322-
%Metadata{} = metadata
337+
%Metadata{} = metadata,
338+
_cursor_position
323339
) do
324340
case var do
325341
~c"__MODULE__" ->
@@ -339,9 +355,10 @@ defmodule ElixirLS.Utils.CompletionEngine do
339355
defp expand_dot_path(
340356
{:alias, {:module_attribute, attribute}, hint},
341357
%State.Env{} = env,
342-
%Metadata{} = metadata
358+
%Metadata{} = metadata,
359+
cursor_position
343360
) do
344-
case value_from_binding({:attribute, List.to_atom(attribute)}, env, metadata) do
361+
case value_from_binding({:attribute, List.to_atom(attribute)}, env, metadata, cursor_position) do
345362
{:ok, {:atom, atom}} ->
346363
if Introspection.elixir_module?(atom) do
347364
alias_suffix = hint |> List.to_string() |> String.split(".")
@@ -360,26 +377,46 @@ defmodule ElixirLS.Utils.CompletionEngine do
360377
end
361378
end
362379

363-
defp expand_dot_path({:alias, _, _hint}, %State.Env{} = _env, %Metadata{} = _metadata) do
380+
defp expand_dot_path(
381+
{:alias, _, _hint},
382+
%State.Env{} = _env,
383+
%Metadata{} = _metadata,
384+
_cursor_position
385+
) do
364386
:error
365387
end
366388

367-
defp expand_dot_path({:unquoted_atom, var}, %State.Env{} = _env, %Metadata{} = _metadata) do
389+
defp expand_dot_path(
390+
{:unquoted_atom, var},
391+
%State.Env{} = _env,
392+
%Metadata{} = _metadata,
393+
_cursor_position
394+
) do
368395
{:ok, {:atom, List.to_atom(var)}}
369396
end
370397

371-
defp expand_dot_path({:dot, parent, call}, %State.Env{} = env, %Metadata{} = metadata) do
372-
case expand_dot_path(parent, env, metadata) do
398+
defp expand_dot_path(
399+
{:dot, parent, call},
400+
%State.Env{} = env,
401+
%Metadata{} = metadata,
402+
cursor_position
403+
) do
404+
case expand_dot_path(parent, env, metadata, cursor_position) do
373405
{:ok, expanded} ->
374-
value_from_binding({:call, expanded, List.to_atom(call), []}, env, metadata)
406+
value_from_binding(
407+
{:call, expanded, List.to_atom(call), []},
408+
env,
409+
metadata,
410+
cursor_position
411+
)
375412

376413
:error ->
377414
:error
378415
end
379416
end
380417

381418
# elixir >= 1.15
382-
defp expand_dot_path(:expr, %State.Env{} = _env, %Metadata{} = _metadata) do
419+
defp expand_dot_path(:expr, %State.Env{} = _env, %Metadata{} = _metadata, _cursor_position) do
383420
# TODO expand expression
384421
:error
385422
end
@@ -467,11 +504,12 @@ defmodule ElixirLS.Utils.CompletionEngine do
467504
cursor_position
468505
)
469506

470-
current_module_locals = if env.module && env.function do
471-
match_module_funs(env.module, hint, exact?, false, :all, env, metadata, cursor_position)
472-
else
473-
[]
474-
end
507+
current_module_locals =
508+
if env.module && env.function do
509+
match_module_funs(env.module, hint, exact?, false, :all, env, metadata, cursor_position)
510+
else
511+
[]
512+
end
475513

476514
imported_locals =
477515
{env.functions, env.macros}
@@ -681,7 +719,7 @@ defmodule ElixirLS.Utils.CompletionEngine do
681719
cursor_position,
682720
only_structs
683721
) do
684-
case value_from_binding({:attribute, List.to_atom(attribute)}, env, metadata) do
722+
case value_from_binding({:attribute, List.to_atom(attribute)}, env, metadata, cursor_position) do
685723
{:ok, {:atom, atom}} ->
686724
if Introspection.elixir_module?(atom) do
687725
expand_aliases("#{atom}.#{hint}", env, metadata, cursor_position, only_structs, [])
@@ -1512,9 +1550,14 @@ defmodule ElixirLS.Utils.CompletionEngine do
15121550
end
15131551
end
15141552

1515-
defp value_from_binding(binding_ast, %State.Env{} = env, %Metadata{} = metadata) do
1553+
defp value_from_binding(
1554+
binding_ast,
1555+
%State.Env{} = env,
1556+
%Metadata{} = metadata,
1557+
cursor_position
1558+
) do
15161559
case Binding.expand(
1517-
Binding.from_env(env, metadata),
1560+
Binding.from_env(env, metadata, cursor_position),
15181561
binding_ast
15191562
) do
15201563
:none -> :error

apps/elixir_ls_utils/test/complete_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,8 @@ defmodule ElixirLS.Utils.CompletionEngineTest do
22332233
needed_require: nil,
22342234
visibility: :public
22352235
}
2236-
] = expand(~c"inf", %Env{requires: [], module: MyModule, function: {:foo, 1}}, metadata)
2236+
] =
2237+
expand(~c"inf", %Env{requires: [], module: MyModule, function: {:foo, 1}}, metadata)
22372238
end
22382239

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

apps/language_server/lib/language_server/providers/completion/generic_reducer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.GenericReducer do
4242
module_store: acc.context.module_store
4343
}
4444

45-
case Util.func_call_chain(text_before, env, buffer_metadata) do
45+
case Util.func_call_chain(text_before, env, buffer_metadata, cursor_context.cursor_position) do
4646
[func_call | _] = chain ->
4747
if function_exported?(reducer, :suggestions, 4) do
4848
try do

apps/language_server/lib/language_server/providers/completion/reducers/params.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.Params do
2727
def add_options(hint, env, buffer_metadata, cursor_context, acc) do
2828
prefix = cursor_context.text_before
2929

30-
binding_env = Binding.from_env(env, buffer_metadata)
30+
binding_env = Binding.from_env(env, buffer_metadata, cursor_context.cursor_position)
3131

3232
%Metadata{mods_funs_to_positions: mods_funs, types: metadata_types} = buffer_metadata
3333

apps/language_server/lib/language_server/providers/completion/reducers/record.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.Record do
5555
} = metadata,
5656
cursor_position
5757
) do
58-
binding_env = ElixirSense.Core.Binding.from_env(env, metadata)
58+
binding_env = ElixirSense.Core.Binding.from_env(env, metadata, cursor_position)
5959

6060
# check if we are inside local or remote call arguments and parameter is 0, 1 or 2
6161
# record fields can specified on 0, 1 and 2 position in the argument list

apps/language_server/lib/language_server/providers/completion/reducers/struct.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.Struct do
2929
def add_fields(hint, env, buffer_metadata, context, acc) do
3030
text_before = context.text_before
3131

32-
case find_struct_fields(hint, text_before, env, buffer_metadata) do
32+
case find_struct_fields(hint, text_before, env, buffer_metadata, context.cursor_position) do
3333
{[], _} ->
3434
{:cont, acc}
3535

@@ -57,9 +57,10 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.Struct do
5757
module: module,
5858
aliases: aliases
5959
} = env,
60-
%Metadata{} = buffer_metadata
60+
%Metadata{} = buffer_metadata,
61+
cursor_position
6162
) do
62-
binding_env = ElixirSense.Core.Binding.from_env(env, buffer_metadata)
63+
binding_env = ElixirSense.Core.Binding.from_env(env, buffer_metadata, cursor_position)
6364

6465
case Source.which_struct(text_before, module) do
6566
{type, fields_so_far, elixir_prefix, var} ->

apps/language_server/lib/language_server/providers/completion/reducers/type_specs.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.TypeSpecs do
3232
"""
3333

3434
# We only list type specs when inside typespec scope
35-
def add_types(hint, env, file_metadata, %{at_module_body?: _}, acc) do
35+
def add_types(
36+
hint,
37+
env,
38+
file_metadata,
39+
%{at_module_body?: _, cursor_position: cursor_position},
40+
acc
41+
) do
3642
if match?({_, _}, env.typespec) do
3743
%State.Env{
3844
aliases: aliases,
@@ -41,7 +47,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.TypeSpecs do
4147

4248
%Metadata{mods_funs_to_positions: mods_funs, types: metadata_types} = file_metadata
4349

44-
binding_env = Binding.from_env(env, file_metadata)
50+
binding_env = Binding.from_env(env, file_metadata, cursor_position)
4551

4652
{mod, hint} =
4753
hint

apps/language_server/lib/language_server/providers/definition/locator.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
6363
} = env,
6464
metadata
6565
) do
66-
binding_env = Binding.from_env(env, metadata)
66+
binding_env = Binding.from_env(env, metadata, context.begin)
6767

6868
type = SurroundContext.to_binding(context.context, module)
6969

apps/language_server/lib/language_server/providers/hover/docs.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ defmodule ElixirLS.LanguageServer.Providers.Hover.Docs do
106106
} = env,
107107
metadata
108108
) do
109-
binding_env = Binding.from_env(env, metadata)
109+
binding_env = Binding.from_env(env, metadata, context.begin)
110110

111111
type = SurroundContext.to_binding(context.context, module)
112112

apps/language_server/lib/language_server/providers/implementation/locator.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ defmodule ElixirLS.LanguageServer.Providers.Implementation.Locator do
5858
} = env,
5959
metadata
6060
) do
61-
binding_env = Binding.from_env(env, metadata)
61+
binding_env = Binding.from_env(env, metadata, context.begin)
6262

6363
type = SurroundContext.to_binding(context.context, module)
6464

0 commit comments

Comments
 (0)