Skip to content

Commit 6a3b1d8

Browse files
committed
Merge remote-tracking branch 'origin/main' into signature-help
2 parents ad3957c + e99b7a0 commit 6a3b1d8

File tree

6 files changed

+120
-63
lines changed

6 files changed

+120
-63
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## [0.22.3](https://github.com/elixir-tools/next-ls/compare/v0.22.2...v0.22.3) (2024-05-15)
4+
5+
6+
### Bug Fixes
7+
8+
* ensure some elixir internals are ready ([#478](https://github.com/elixir-tools/next-ls/issues/478)) ([f4685d0](https://github.com/elixir-tools/next-ls/commit/f4685d01266b4afb7f557d9a361fc7770aa22ec6)), closes [#467](https://github.com/elixir-tools/next-ls/issues/467)
9+
10+
## [0.22.2](https://github.com/elixir-tools/next-ls/compare/v0.22.1...v0.22.2) (2024-05-14)
11+
12+
13+
### Bug Fixes
14+
15+
* **completions:** work in guards ([#475](https://github.com/elixir-tools/next-ls/issues/475)) ([e0573ab](https://github.com/elixir-tools/next-ls/commit/e0573ab23c439313ed2546015f12a21dfe573d1d))
16+
317
## [0.22.1](https://github.com/elixir-tools/next-ls/compare/v0.22.0...v0.22.1) (2024-05-13)
418

519

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}: let
1515
inherit (nixpkgs) lib;
1616

17-
version = "0.22.1"; # x-release-please-version
17+
version = "0.22.3"; # x-release-please-version
1818

1919
# Helper to provide system-specific attributes
2020
forAllSystems = f:

lib/next_ls/runtime.ex

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,32 @@ defmodule NextLS.Runtime do
231231
true <- connect(node, port, 120) do
232232
NextLS.Logger.info(logger, "Connected to node #{node}")
233233

234-
:next_ls
235-
|> :code.priv_dir()
236-
|> Path.join("monkey/_next_ls_private_compiler.ex")
237-
|> then(&:rpc.call(node, Code, :compile_file, [&1]))
238-
|> tap(fn
239-
{:badrpc, error} ->
240-
NextLS.Logger.error(logger, "Bad RPC call to node #{node}: #{inspect(error)}")
241-
send(me, {:cancel, error})
242-
243-
_ ->
244-
:ok
245-
end)
246-
247-
{:ok, _} = :rpc.call(node, :_next_ls_private_compiler, :start, [])
248-
249-
send(me, {:node, node})
234+
result =
235+
:next_ls
236+
|> :code.priv_dir()
237+
|> Path.join("monkey/_next_ls_private_compiler.ex")
238+
|> then(fn path ->
239+
if await_config_table(node, 5) do
240+
:rpc.call(node, Code, :compile_file, [path])
241+
else
242+
{:badrpc, "internal ets table not found"}
243+
end
244+
end)
245+
|> then(fn
246+
{:badrpc, error} ->
247+
NextLS.Logger.error(logger, "Bad RPC call to node #{node}: #{inspect(error)}")
248+
send(me, {:cancel, error})
249+
:error
250+
251+
_ ->
252+
:ok
253+
end)
254+
255+
if result == :ok do
256+
{:ok, _} = :rpc.call(node, :_next_ls_private_compiler, :start, [])
257+
258+
send(me, {:node, node})
259+
end
250260
else
251261
error ->
252262
send(me, {:cancel, error})
@@ -275,6 +285,20 @@ defmodule NextLS.Runtime do
275285
end
276286
end
277287

288+
defp await_config_table(_node, 0) do
289+
false
290+
end
291+
292+
defp await_config_table(node, attempts) do
293+
# this is an Elixir implementation detail, handle with care
294+
if :undefined == :rpc.call(node, :ets, :whereis, [:elixir_config]) do
295+
Process.sleep(100)
296+
await_config_table(node, attempts - 1)
297+
else
298+
true
299+
end
300+
end
301+
278302
@impl GenServer
279303
def handle_call(:ready?, _from, state) when is_ready(state) do
280304
{:reply, true, state}

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule NextLS.MixProject do
22
use Mix.Project
33

4-
@version "0.22.1" # x-release-please-version
4+
@version "0.22.3" # x-release-please-version
55

66
def project do
77
[

priv/monkey/_next_ls_private_compiler.ex

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,14 +1032,13 @@ defmodule :_next_ls_private_compiler do
10321032
@moduledoc false
10331033

10341034
def start do
1035+
Code.put_compiler_option(:parser_options, columns: true, token_metadata: true)
1036+
10351037
children = [
10361038
:_next_ls_private_compiler_worker
10371039
]
10381040

1039-
# See https://hexdocs.pm/elixir/Supervisor.html
1040-
# for other strategies and supported options
1041-
opts = [strategy: :one_for_one, name: :_next_ls_private_application_supervisor]
1042-
{:ok, pid} = Supervisor.start_link(children, opts)
1041+
{:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one, name: :_next_ls_private_application_supervisor)
10431042
Process.unlink(pid)
10441043
{:ok, pid}
10451044
end
@@ -1049,7 +1048,6 @@ defmodule :_next_ls_private_compiler do
10491048
def compile do
10501049
# keep stdout on this node
10511050
Process.group_leader(self(), Process.whereis(:user))
1052-
Code.put_compiler_option(:parser_options, columns: true, token_metadata: true)
10531051

10541052
Code.put_compiler_option(:tracers, [NextLSPrivate.DepTracer | @tracers])
10551053

@@ -1425,39 +1423,36 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
14251423
end
14261424
end
14271425

1428-
defp expand_macro(_meta, Kernel, type, [{name, _, params}, [{_, block}]], _callback, state, env)
1429-
when type in [:def, :defp] and is_tuple(block) and is_atom(name) and is_list(params) do
1430-
{_, state, penv} =
1431-
for p <- params, reduce: {nil, state, env} do
1432-
{_, state, penv} ->
1433-
expand_pattern(p, state, penv)
1434-
end
1435-
1436-
{res, state, _env} = expand(block, state, penv)
1426+
defp expand_macro(_meta, Kernel, type, args, _callback, state, env)
1427+
when type in [:def, :defmacro, :defp, :defmacrop] do
1428+
# extract the name, params, guards, and blocks
1429+
{name, params, guards, blocks} =
1430+
case args do
1431+
[{:when, _, [{name, _, params} | guards]} | maybe_blocks] ->
1432+
{name, params, guards, maybe_blocks}
14371433

1438-
arity = length(List.wrap(params))
1439-
functions = Map.update(state.functions, env.module, [{name, arity}], &Keyword.put_new(&1, name, arity))
1440-
{res, put_in(state.functions, functions), env}
1441-
end
1434+
[{name, _, params} | maybe_blocks] ->
1435+
{name, params, [], maybe_blocks}
1436+
end
14421437

1443-
defp expand_macro(_meta, Kernel, type, [{name, _, params}, block], _callback, state, env)
1444-
when type in [:defmacro, :defmacrop] do
1445-
{_res, state, penv} = expand(params, state, env)
1446-
{res, state, _env} = expand(block, state, penv)
1438+
blocks = List.first(blocks, [])
14471439

1448-
arity = length(List.wrap(params))
1449-
macros = Map.update(state.macros, env.module, [{name, arity}], &Keyword.put_new(&1, name, arity))
1450-
{res, put_in(state.macros, macros), env}
1451-
end
1452-
1453-
defp expand_macro(_meta, Kernel, type, [{name, _, params}, blocks], _callback, state, env)
1454-
when type in [:def, :defp] and is_atom(name) and is_list(params) and is_list(blocks) do
1440+
# collect the environment from the parameters
1441+
# parameters are always patterns
14551442
{_, state, penv} =
14561443
for p <- params, reduce: {nil, state, env} do
14571444
{_, state, penv} ->
14581445
expand_pattern(p, state, penv)
14591446
end
14601447

1448+
# expand guards, which includes the env from params
1449+
{_, state, _} =
1450+
for guard <- guards, reduce: {nil, state, penv} do
1451+
{_, state, env} ->
1452+
expand(guard, state, env)
1453+
end
1454+
1455+
# expand the blocks, there could be `:do`, `:after`, `:catch`, etc
14611456
{blocks, state} =
14621457
for {type, block} <- blocks, reduce: {[], state} do
14631458
{acc, state} ->
@@ -1467,26 +1462,21 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
14671462

14681463
arity = length(List.wrap(params))
14691464

1470-
functions = Map.update(state.functions, env.module, [{name, arity}], &Keyword.put_new(&1, name, arity))
1471-
{Enum.reverse(blocks), put_in(state.functions, functions), env}
1472-
end
1473-
1474-
defp expand_macro(_meta, Kernel, type, [{_name, _, params}, blocks], _callback, state, env)
1475-
when type in [:def, :defp] and is_list(params) and is_list(blocks) do
1476-
{_, state, penv} =
1477-
for p <- params, reduce: {nil, state, env} do
1478-
{_, state, penv} ->
1479-
expand_pattern(p, state, penv)
1465+
# determine which key to save this function in state
1466+
state_key =
1467+
case type do
1468+
type when type in [:def, :defp] -> :functions
1469+
type when type in [:defmacro, :defmacrop] -> :macros
14801470
end
14811471

1482-
{blocks, state} =
1483-
for {type, block} <- blocks, reduce: {[], state} do
1484-
{acc, state} ->
1485-
{res, state, _env} = expand(block, state, penv)
1486-
{[{type, res} | acc], state}
1472+
funcs =
1473+
if is_atom(name) do
1474+
Map.update(state[state_key], env.module, [{name, arity}], &Keyword.put_new(&1, name, arity))
1475+
else
1476+
state[state_key]
14871477
end
14881478

1489-
{Enum.reverse(blocks), state, env}
1479+
{Enum.reverse(blocks), put_in(state[state_key], funcs), env}
14901480
end
14911481

14921482
defp expand_macro(meta, Kernel, :@, [{name, _, p}] = args, callback, state, env) when is_list(p) do

test/next_ls/completions_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,4 +809,33 @@ defmodule NextLS.CompletionsTest do
809809
assert_match %{"kind" => 6, "label" => "items"} in results
810810
assert_match %{"kind" => 6, "label" => "item"} not in results
811811
end
812+
813+
test "parameters are available inside guards", %{client: client, foo: foo} do
814+
uri = uri(foo)
815+
816+
did_open(client, foo, """
817+
defmodule Foo do
818+
def run(items) when is_list(i
819+
end
820+
""")
821+
822+
request client, %{
823+
method: "textDocument/completion",
824+
id: 2,
825+
jsonrpc: "2.0",
826+
params: %{
827+
textDocument: %{
828+
uri: uri
829+
},
830+
position: %{
831+
line: 1,
832+
character: 31
833+
}
834+
}
835+
}
836+
837+
assert_result 2, results
838+
839+
assert_match %{"kind" => 6, "label" => "items"} in results
840+
end
812841
end

0 commit comments

Comments
 (0)