Skip to content

Commit

Permalink
do not suggest aliasing when hint has more than one part
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Jul 13, 2023
1 parent 3d2aed4 commit 41ccb84
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
9 changes: 6 additions & 3 deletions lib/elixir_sense/core/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -969,9 +969,9 @@ defmodule ElixirSense.Core.Introspection do
iex> ElixirSense.Core.Introspection.get_module_subtype(Mix.Tasks.Run)
:task
iex> ElixirSense.Core.Introspection.get_module_subtype(NotExistingModule)
nil
:alias
iex> ElixirSense.Core.Introspection.get_module_subtype(Elixir)
nil
:alias
"""
@spec get_module_subtype(module()) :: module_subtype()
def get_module_subtype(module) do
Expand Down Expand Up @@ -999,7 +999,10 @@ defmodule ElixirSense.Core.Introspection do
:task
end

not has_func.(:module_info, 1) ->
module == Elixir ->
:alias

not has_func.(:module_info, 1) and match?("Elixir." <> _, Atom.to_string(module)) ->
:alias

true ->
Expand Down
23 changes: 17 additions & 6 deletions lib/elixir_sense/providers/suggestion/complete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,19 @@ defmodule ElixirSense.Providers.Suggestion.Complete do
list = Enum.take(parts, length(parts) - 1)

case value_from_alias(list, env) do
{:ok, alias} -> expand_aliases(alias, hint, [], false, env, filter, opts)
:error -> no()
{:ok, alias} ->
expand_aliases(
alias,
hint,
[],
false,
env,
filter,
Keyword.put(opts, :required_alias, false)
)

:error ->
no()
end
end
end
Expand Down Expand Up @@ -528,7 +539,7 @@ defmodule ElixirSense.Providers.Suggestion.Complete do
Matcher.match?(name, hint) do
%{
kind: :module,
type: :alias,
type: :elixir,
name: name,
full_name: inspect(mod),
desc: {"", %{}},
Expand Down Expand Up @@ -664,10 +675,10 @@ defmodule ElixirSense.Providers.Suggestion.Complete do
Matcher.match?(module_part, hint)
end)
|> Enum.reduce(acc, fn {module_part, index}, acc1 ->
required_alias = Enum.slice(module_parts, 0..index)
required_alias = required_alias |> Module.concat() |> Atom.to_string()
required_alias = Enum.slice(module_parts, 0..index)
required_alias = required_alias |> Module.concat() |> Atom.to_string()

[{module_part, required_alias} | acc1]
[{module_part, required_alias} | acc1]
end)
end
else
Expand Down
28 changes: 20 additions & 8 deletions test/elixir_sense/providers/suggestion/complete_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,11 @@ defmodule ElixirSense.Providers.Suggestion.CompleteTest do
assert [
%{
metadata: %{},
name: "Chars",
full_name: "String.Chars",
required_alias: "String.Chars"
name: "CallerWithAliasesAndImports",
full_name:
"ElixirSense.Providers.ReferencesTest.Modules.CallerWithAliasesAndImports",
required_alias:
"ElixirSense.Providers.ReferencesTest.Modules.CallerWithAliasesAndImports"
},
%{
metadata: %{},
Expand All @@ -316,11 +318,9 @@ defmodule ElixirSense.Providers.Suggestion.CompleteTest do
},
%{
metadata: %{},
name: "CallerWithAliasesAndImports",
full_name:
"ElixirSense.Providers.ReferencesTest.Modules.CallerWithAliasesAndImports",
required_alias:
"ElixirSense.Providers.ReferencesTest.Modules.CallerWithAliasesAndImports"
name: "Chars",
full_name: "String.Chars",
required_alias: "String.Chars"
}
] = expand(~c"Char", %Env{}, required_alias: true)
end
Expand All @@ -345,6 +345,18 @@ defmodule ElixirSense.Providers.Suggestion.CompleteTest do
refute Enum.find(results, fn expansion -> expansion[:required_alias] == Elixir end)
end

test "does not suggest required_alias for when hint has more than one part" do
results = expand(~c"Elixir.Char", %Env{}, required_alias: true)

refute Enum.find(results, fn expansion -> expansion[:required_alias] == String.Chars end)

results = expand(~c"String.", %Env{}, required_alias: true)

refute Enum.find(results, fn expansion ->
expansion[:required_alias] == String.Tokenizer.Security
end)
end

test "elixir submodule no completion" do
assert expand(~c"IEx.Xyz") == []
end
Expand Down
2 changes: 1 addition & 1 deletion test/elixir_sense/suggestions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1977,7 +1977,7 @@ defmodule ElixirSense.SuggestionsTest do
type: :module,
name: "Elixir",
full_name: "Elixir",
subtype: nil,
subtype: :alias,
summary: "",
metadata: %{}
} = Enum.at(list, 0)
Expand Down

0 comments on commit 41ccb84

Please sign in to comment.