Skip to content

Commit 9e4f69b

Browse files
committed
fix(completions): more accurate inside with/for
1 parent 8f6561e commit 9e4f69b

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

flake.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
src = self.outPath;
116116
inherit version elixir;
117117
pname = "next-ls-deps";
118-
hash = "sha256-sIV8/KS5hcIiqk5sSwcIEnPFZNvefzvNyt6af829ri8=";
118+
hash = "sha256-qdJf3A+k28J2EDtaC8pLE7HgqzKuCjCWmfezx62wyUs=";
119119
mixEnv = "prod";
120120
};
121121

priv/monkey/_next_ls_private_compiler.ex

+11-4
Original file line numberDiff line numberDiff line change
@@ -1538,8 +1538,8 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
15381538
defp expand_local(_meta, fun, args, state, env) when fun in [:for, :with] do
15391539
{params, blocks} =
15401540
Enum.split_while(args, fn
1541-
{:<-, _, _} -> true
1542-
_ -> false
1541+
[{:do, _} | _] -> false
1542+
_ -> true
15431543
end)
15441544

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

15511551
{blocks, state} =
1552-
for {type, block} <- blocks, reduce: {[], state} do
1552+
for {type, block} <- List.first(blocks, []), reduce: {[], state} do
15531553
{acc, state} ->
1554-
{res, state, _env} = expand(block, state, penv)
1554+
env =
1555+
if type == :do do
1556+
penv
1557+
else
1558+
env
1559+
end
1560+
1561+
{res, state, env} = expand(block, state, env)
15551562
{[{type, res} | acc], state}
15561563
end
15571564

test/next_ls/completions_test.exs

+73
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,79 @@ defmodule NextLS.CompletionsTest do
700700
assert_match %{"kind" => 6, "label" => "ast1"} not in results
701701
end
702702

703+
test "with else", %{client: client, foo: foo} do
704+
uri = uri(foo)
705+
706+
did_open(client, foo, """
707+
defmodule Foo do
708+
def run(doug) do
709+
completion_item =
710+
with {:ok, darrel} <- completion_item.data do
711+
darrel
712+
else
713+
%{"uri" => uri, "data" => data} ->
714+
d
715+
716+
end
717+
end
718+
""")
719+
720+
request client, %{
721+
method: "textDocument/completion",
722+
id: 2,
723+
jsonrpc: "2.0",
724+
params: %{
725+
textDocument: %{uri: uri},
726+
position: %{
727+
line: 7,
728+
character: 11
729+
}
730+
}
731+
}
732+
733+
assert_result 2, results
734+
735+
assert_match %{"kind" => 6, "label" => "doug"} in results
736+
assert_match %{"kind" => 6, "label" => "data"} in results
737+
738+
assert_match %{"kind" => 6, "label" => "darrel"} not in results
739+
end
740+
741+
test "for comprehension", %{client: client, foo: foo} do
742+
uri = uri(foo)
743+
744+
did_open(client, foo, """
745+
defmodule Foo do
746+
def run(items) do
747+
for item <- items,
748+
iname = item.name,
749+
String.starts_with?(name, "Mitch") do
750+
i
751+
end
752+
end
753+
""")
754+
755+
request client, %{
756+
method: "textDocument/completion",
757+
id: 2,
758+
jsonrpc: "2.0",
759+
params: %{
760+
textDocument: %{uri: uri},
761+
position: %{
762+
line: 5,
763+
character: 7
764+
}
765+
}
766+
}
767+
768+
assert_result 2, results
769+
770+
assert_match %{"kind" => 6, "label" => "item"} in results
771+
assert_match %{"kind" => 6, "label" => "iname"} in results
772+
773+
assert_match %{"kind" => 6, "label" => "items"} in results
774+
end
775+
703776
test "variables show up in test blocks", %{client: client, foo: foo} do
704777
uri = uri(foo)
705778

0 commit comments

Comments
 (0)