Skip to content

Commit

Permalink
fix(completions): more accurate inside with/for
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed May 16, 2024
1 parent 8f6561e commit 9e4f69b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
src = self.outPath;
inherit version elixir;
pname = "next-ls-deps";
hash = "sha256-sIV8/KS5hcIiqk5sSwcIEnPFZNvefzvNyt6af829ri8=";
hash = "sha256-qdJf3A+k28J2EDtaC8pLE7HgqzKuCjCWmfezx62wyUs=";
mixEnv = "prod";
};

Expand Down
15 changes: 11 additions & 4 deletions priv/monkey/_next_ls_private_compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1538,8 +1538,8 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
defp expand_local(_meta, fun, args, state, env) when fun in [:for, :with] do
{params, blocks} =
Enum.split_while(args, fn
{:<-, _, _} -> true
_ -> false
[{:do, _} | _] -> false
_ -> true
end)

{_, state, penv} =
Expand All @@ -1549,9 +1549,16 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
end

{blocks, state} =
for {type, block} <- blocks, reduce: {[], state} do
for {type, block} <- List.first(blocks, []), reduce: {[], state} do
{acc, state} ->
{res, state, _env} = expand(block, state, penv)
env =
if type == :do do
penv
else
env
end

{res, state, env} = expand(block, state, env)
{[{type, res} | acc], state}
end

Expand Down
73 changes: 73 additions & 0 deletions test/next_ls/completions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,79 @@ defmodule NextLS.CompletionsTest do
assert_match %{"kind" => 6, "label" => "ast1"} not in results
end

test "with else", %{client: client, foo: foo} do
uri = uri(foo)

did_open(client, foo, """
defmodule Foo do
def run(doug) do
completion_item =
with {:ok, darrel} <- completion_item.data do
darrel
else
%{"uri" => uri, "data" => data} ->
d
end
end
""")

request client, %{
method: "textDocument/completion",
id: 2,
jsonrpc: "2.0",
params: %{
textDocument: %{uri: uri},
position: %{
line: 7,
character: 11
}
}
}

assert_result 2, results

assert_match %{"kind" => 6, "label" => "doug"} in results
assert_match %{"kind" => 6, "label" => "data"} in results

assert_match %{"kind" => 6, "label" => "darrel"} not in results
end

test "for comprehension", %{client: client, foo: foo} do
uri = uri(foo)

did_open(client, foo, """
defmodule Foo do
def run(items) do
for item <- items,
iname = item.name,
String.starts_with?(name, "Mitch") do
i
end
end
""")

request client, %{
method: "textDocument/completion",
id: 2,
jsonrpc: "2.0",
params: %{
textDocument: %{uri: uri},
position: %{
line: 5,
character: 7
}
}
}

assert_result 2, results

assert_match %{"kind" => 6, "label" => "item"} in results
assert_match %{"kind" => 6, "label" => "iname"} in results

assert_match %{"kind" => 6, "label" => "items"} in results
end

test "variables show up in test blocks", %{client: client, foo: foo} do
uri = uri(foo)

Expand Down

0 comments on commit 9e4f69b

Please sign in to comment.