Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(completions): more accurate inside with/for #482

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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