Skip to content

Commit b59907e

Browse files
committed
Provide alias sensitive completion to special forms
1 parent f9e0f25 commit b59907e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/iex/lib/iex/autocomplete.ex

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ defmodule IEx.Autocomplete do
1818
%{kind: :variable, name: "utf32"}
1919
]
2020

21+
@alias_only_atoms ~w(alias import require)a
22+
@alias_only_charlists ~w(alias import require)c
23+
2124
@doc """
2225
Provides one helper function that is injected into connecting
2326
remote nodes to properly handle autocompletion.
@@ -62,7 +65,11 @@ defmodule IEx.Autocomplete do
6265
expand_typespecs(expansion, shell, &get_module_types/1)
6366

6467
{:dot, path, hint} ->
65-
expand_dot(path, List.to_string(hint), false, shell)
68+
if alias = alias_only(path, hint, code, shell) do
69+
expand_aliases(List.to_string(alias), shell)
70+
else
71+
expand_dot(path, List.to_string(hint), false, shell)
72+
end
6673

6774
{:dot_arity, path, hint} ->
6875
expand_dot(path, List.to_string(hint), true, shell)
@@ -80,6 +87,9 @@ defmodule IEx.Autocomplete do
8087
{:local_arity, local} ->
8188
expand_local(List.to_string(local), true, shell)
8289

90+
{:local_call, local} when local in @alias_only_charlists ->
91+
expand_aliases("", shell)
92+
8393
{:local_call, local} ->
8494
expand_local_call(List.to_atom(local), shell)
8595

@@ -382,6 +392,9 @@ defmodule IEx.Autocomplete do
382392
[cursor, pairs, {:|, _, [{variable, _, nil} | _]}, {:%{}, _, _} | _] ->
383393
container_context_map(cursor, pairs, variable, shell)
384394

395+
[cursor, {special_form, _, [cursor]} | _] when special_form in @alias_only_atoms ->
396+
:alias_only
397+
385398
[cursor | tail] ->
386399
case remove_operators(tail, cursor) do
387400
[{:"::", _, [_, _]}, {:<<>>, _, [_ | _]} | _] -> :bitstring_modifier
@@ -425,6 +438,16 @@ defmodule IEx.Autocomplete do
425438

426439
## Aliases and modules
427440

441+
defp alias_only(path, hint, code, shell) do
442+
with {:alias, alias} <- path,
443+
[] <- hint,
444+
:alias_only <- container_context(code, shell) do
445+
alias ++ [?.]
446+
else
447+
_ -> nil
448+
end
449+
end
450+
428451
defp expand_aliases(all, shell) do
429452
case String.split(all, ".") do
430453
[hint] ->

lib/iex/test/iex/autocomplete_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ defmodule IEx.AutocompleteTest do
470470
assert ~c"size/1" in entries
471471
end
472472

473+
test "completion for aliases in special forms" do
474+
assert {:yes, ~c"", entries} = expand(~c"alias ")
475+
assert ~c"Atom" in entries
476+
refute ~c"is_atom" in entries
477+
478+
assert {:yes, ~c"Range", []} = expand(~c"alias Date.")
479+
end
480+
473481
test "ignore invalid Elixir module literals" do
474482
defmodule(:"Elixir.IEx.AutocompleteTest.Unicodé", do: nil)
475483
assert expand(~c"IEx.AutocompleteTest.Unicod") == {:no, ~c"", []}

0 commit comments

Comments
 (0)