Skip to content

Commit

Permalink
Provide alias sensitive completion to special forms
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 19, 2023
1 parent f9e0f25 commit b59907e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/iex/lib/iex/autocomplete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ defmodule IEx.Autocomplete do
%{kind: :variable, name: "utf32"}
]

@alias_only_atoms ~w(alias import require)a
@alias_only_charlists ~w(alias import require)c

@doc """
Provides one helper function that is injected into connecting
remote nodes to properly handle autocompletion.
Expand Down Expand Up @@ -62,7 +65,11 @@ defmodule IEx.Autocomplete do
expand_typespecs(expansion, shell, &get_module_types/1)

{:dot, path, hint} ->
expand_dot(path, List.to_string(hint), false, shell)
if alias = alias_only(path, hint, code, shell) do
expand_aliases(List.to_string(alias), shell)
else
expand_dot(path, List.to_string(hint), false, shell)
end

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

{:local_call, local} when local in @alias_only_charlists ->
expand_aliases("", shell)

{:local_call, local} ->
expand_local_call(List.to_atom(local), shell)

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

[cursor, {special_form, _, [cursor]} | _] when special_form in @alias_only_atoms ->
:alias_only

[cursor | tail] ->
case remove_operators(tail, cursor) do
[{:"::", _, [_, _]}, {:<<>>, _, [_ | _]} | _] -> :bitstring_modifier
Expand Down Expand Up @@ -425,6 +438,16 @@ defmodule IEx.Autocomplete do

## Aliases and modules

defp alias_only(path, hint, code, shell) do
with {:alias, alias} <- path,
[] <- hint,
:alias_only <- container_context(code, shell) do
alias ++ [?.]
else
_ -> nil
end
end

defp expand_aliases(all, shell) do
case String.split(all, ".") do
[hint] ->
Expand Down
8 changes: 8 additions & 0 deletions lib/iex/test/iex/autocomplete_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ defmodule IEx.AutocompleteTest do
assert ~c"size/1" in entries
end

test "completion for aliases in special forms" do
assert {:yes, ~c"", entries} = expand(~c"alias ")
assert ~c"Atom" in entries
refute ~c"is_atom" in entries

assert {:yes, ~c"Range", []} = expand(~c"alias Date.")
end

test "ignore invalid Elixir module literals" do
defmodule(:"Elixir.IEx.AutocompleteTest.Unicodé", do: nil)
assert expand(~c"IEx.AutocompleteTest.Unicod") == {:no, ~c"", []}
Expand Down

0 comments on commit b59907e

Please sign in to comment.