Skip to content

Commit 2b748a9

Browse files
committed
fix(completions): completions inside alias/import/require special forms
Closes #421
1 parent 1bb590e commit 2b748a9

File tree

2 files changed

+78
-17
lines changed

2 files changed

+78
-17
lines changed

Diff for: priv/monkey/_next_ls_private_compiler.ex

+35-17
Original file line numberDiff line numberDiff line change
@@ -1213,38 +1213,56 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
12131213
expand({form, meta, [arg, []]}, state, env)
12141214
end
12151215

1216-
defp expand({:alias, meta, [arg, opts]}, state, env) do
1216+
defp expand({:alias, meta, [arg, opts]} = node, state, env) do
12171217
{arg, state, env} = expand(arg, state, env)
12181218
{opts, state, env} = expand_directive_opts(opts, state, env)
12191219

1220-
# An actual compiler would raise if the alias fails.
1221-
case Macro.Env.define_alias(env, meta, arg, [trace: false] ++ opts) do
1222-
{:ok, env} -> {arg, state, env}
1223-
{:error, _} -> {arg, state, env}
1220+
case arg do
1221+
{:__aliases__, _, _} ->
1222+
# An actual compiler would raise if the alias fails.
1223+
case Macro.Env.define_alias(env, meta, arg, [trace: false] ++ opts) do
1224+
{:ok, env} -> {arg, state, env}
1225+
{:error, _} -> {arg, state, env}
1226+
end
1227+
1228+
_ ->
1229+
{node, state, env}
12241230
end
12251231
end
12261232

1227-
defp expand({:require, meta, [arg, opts]}, state, env) do
1233+
defp expand({:require, meta, [arg, opts]} = node, state, env) do
12281234
{arg, state, env} = expand(arg, state, env)
12291235
{opts, state, env} = expand_directive_opts(opts, state, env)
12301236

1231-
# An actual compiler would raise if the module is not defined or if the require fails.
1232-
case Macro.Env.define_require(env, meta, arg, [trace: false] ++ opts) do
1233-
{:ok, env} -> {arg, state, env}
1234-
{:error, _} -> {arg, state, env}
1237+
case arg do
1238+
{:__aliases__, _, _} ->
1239+
# An actual compiler would raise if the module is not defined or if the require fails.
1240+
case Macro.Env.define_require(env, meta, arg, [trace: false] ++ opts) do
1241+
{:ok, env} -> {arg, state, env}
1242+
{:error, _} -> {arg, state, env}
1243+
end
1244+
1245+
_ ->
1246+
{node, state, env}
12351247
end
12361248
end
12371249

1238-
defp expand({:import, meta, [arg, opts]}, state, env) do
1250+
defp expand({:import, meta, [arg, opts]} = node, state, env) do
12391251
{arg, state, env} = expand(arg, state, env)
12401252
{opts, state, env} = expand_directive_opts(opts, state, env)
12411253

1242-
# An actual compiler would raise if the module is not defined or if the import fails.
1243-
with true <- is_atom(arg) and Code.ensure_loaded?(arg),
1244-
{:ok, env} <- Macro.Env.define_import(env, meta, arg, [trace: false] ++ opts) do
1245-
{arg, state, env}
1246-
else
1247-
_ -> {arg, state, env}
1254+
case arg do
1255+
{:__aliases__, _, _} ->
1256+
# An actual compiler would raise if the module is not defined or if the import fails.
1257+
with true <- is_atom(arg) and Code.ensure_loaded?(arg),
1258+
{:ok, env} <- Macro.Env.define_import(env, meta, arg, [trace: false] ++ opts) do
1259+
{arg, state, env}
1260+
else
1261+
_ -> {arg, state, env}
1262+
end
1263+
1264+
_ ->
1265+
{node, state, env}
12481266
end
12491267
end
12501268

Diff for: test/next_ls/completions_test.exs

+43
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ defmodule NextLS.CompletionsTest do
3131
end
3232
""")
3333

34+
baz = Path.join(cwd, "my_proj/lib/baz.ex")
35+
36+
File.write!(baz, """
37+
defmodule Foo.Bar.Baz do
38+
def run() do
39+
:ok
40+
end
41+
end
42+
""")
43+
3444
[foo: foo, cwd: cwd]
3545
end
3646

@@ -342,4 +352,37 @@ defmodule NextLS.CompletionsTest do
342352
}
343353
]
344354
end
355+
356+
test "inside alias special form", %{client: client, foo: foo} do
357+
uri = uri(foo)
358+
359+
did_open(client, foo, """
360+
defmodule Foo do
361+
alias Foo.Bar.
362+
363+
def run() do
364+
:ok
365+
end
366+
end
367+
""")
368+
369+
request client, %{
370+
method: "textDocument/completion",
371+
id: 2,
372+
jsonrpc: "2.0",
373+
params: %{
374+
textDocument: %{
375+
uri: uri
376+
},
377+
position: %{
378+
line: 1,
379+
character: 16
380+
}
381+
}
382+
}
383+
384+
assert_result 2, [
385+
%{"data" => _, "documentation" => _, "insertText" => "Baz", "kind" => 9, "label" => "Baz"}
386+
]
387+
end
345388
end

0 commit comments

Comments
 (0)