Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed May 2, 2024
1 parent fcfc15f commit d0fd731
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
assert_result: 3,
assert_notification: 3,
notify: 2,
request: 2
request: 2,
assert_match: 1
],
line_length: 120,
import_deps: [:gen_lsp],
Expand Down
8 changes: 6 additions & 2 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,8 @@ defmodule NextLS do
for {runtime, %{uri: wuri}} <- entries, String.starts_with?(uri, wuri) do
ast = Sourceror.Zipper.node(with_cursor_zipper)

dbg(ast)

{ms, {:ok, {_, _, _, macro_env}}} =
:timer.tc(
fn ->
Expand All @@ -687,6 +689,8 @@ defmodule NextLS do
# |> Map.put(:attrs, macro_env.attrs)
# |> Map.put(:variables, macro_env.variables)

dbg(env.aliases)

doc =
document_slice
|> String.to_charlist()
Expand Down Expand Up @@ -720,10 +724,10 @@ defmodule NextLS do
{name, GenLSP.Enumerations.CompletionItemKind.struct(), ""}

:function ->
{"#{name}/#{symbol.arity}", GenLSP.Enumerations.CompletionItemKind.function(), symbol[:docs]}
{"#{name}/#{symbol.arity}", GenLSP.Enumerations.CompletionItemKind.function(), symbol[:docs] || ""}

:module ->
{name, GenLSP.Enumerations.CompletionItemKind.module(), symbol[:docs]}
{name, GenLSP.Enumerations.CompletionItemKind.module(), symbol[:docs] || ""}

:variable ->
{to_string(name), GenLSP.Enumerations.CompletionItemKind.variable(), ""}
Expand Down
13 changes: 12 additions & 1 deletion lib/next_ls/helpers/ast_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,18 @@ defmodule NextLS.ASTHelpers do
up && match?({:<-, _, _}, Zipper.node(up)) ->
up |> Zipper.insert_left({:__cursor__, [], []}) |> Zipper.down() |> Zipper.rightmost()

left && Zipper.node(left) == :do ->
up && match?([{{:__block__, _dom, [:do]}, _children}], Zipper.node(up)) ->
Zipper.update(up, fn [{{:__block__, dom, [:do]}, children}] ->
case children do
{:__block__, bom, children} ->
[{{:__block__, dom, [:do]}, {:__block__, bom, children ++ [{:__cursor__, [], []}]}}]

child ->
[{{:__block__, dom, [:do]}, {:__block__, [], [child, {:__cursor__, [], []}]}}]
end
end)

left && match?({:__block__, _, [:do]}, Zipper.node(left)) ->
Zipper.update(cursor_tree, fn n ->
{:__block__, [], [n, {:__cursor__, [], []}]}
end)
Expand Down
38 changes: 32 additions & 6 deletions priv/monkey/_next_ls_private_compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,7 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
end

defp expand({:__cursor__, _meta, _} = node, state, env) do
dbg(env)
Process.put(:cursor_env, {state, env})
{node, state, env}
end
Expand Down Expand Up @@ -1367,8 +1368,10 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
# we don't care when they are used.

defp expand({var, meta, ctx} = ast, state, %{context: :match} = env) when is_atom(var) and is_atom(ctx) do
dbg(var)
ctx = Keyword.get(meta, :context, ctx)
vv = Map.update(env.versioned_vars, var, %{var => ctx}, fn _ -> ctx end)
vv = Map.update(env.versioned_vars, var, ctx, fn _ -> ctx end)

{ast, state, %{env | versioned_vars: vv}}
end

Expand Down Expand Up @@ -1406,9 +1409,21 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do
end
end

defp expand_macro(_meta, Kernel, type, [{name, _, params}, block], _callback, state, env)
defp expand_macro(_meta, Kernel, type, [{name, _, params}, [{_, block}]], _callback, state, env)
when type in [:def, :defp] and is_tuple(block) and is_atom(name) and is_list(params) do
{res, state, _env} = expand(block, state, env)
dbg(params)

dbg(env)

{_, state, penv} =
for p <- params, reduce: {nil, state, env} do
{_, state, penv} ->
expand_pattern(p, state, penv)
end

dbg(penv)

{res, state, _env} = expand(block, state, penv)

arity = length(List.wrap(params))
functions = Map.update(state.functions, env.module, [{name, arity}], &Keyword.put_new(&1, name, arity))
Expand All @@ -1417,7 +1432,9 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do

defp expand_macro(_meta, Kernel, type, [{name, _, params}, block], _callback, state, env)
when type in [:defmacro, :defmacrop] do
{res, state, _env} = expand(block, state, env)
dbg(params)
{_res, state, penv} = expand(params, state, env)
{res, state, _env} = expand(block, state, penv)

arity = length(List.wrap(params))
macros = Map.update(state.macros, env.module, [{name, arity}], &Keyword.put_new(&1, name, arity))
Expand All @@ -1426,10 +1443,16 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do

defp expand_macro(_meta, Kernel, type, [{name, _, params}, blocks], _callback, state, env)
when type in [:def, :defp] and is_atom(name) and is_list(params) and is_list(blocks) do
{_, state, penv} =
for p <- params, reduce: {nil, state, env} do
{_, state, penv} ->
expand_pattern(p, state, penv)
end

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

Expand All @@ -1441,10 +1464,13 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do

defp expand_macro(_meta, Kernel, type, [{_name, _, params}, blocks], _callback, state, env)
when type in [:def, :defp] and is_list(params) and is_list(blocks) do
dbg(params)
{_res, state, penv} = expand(params, state, env)

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

Expand Down
60 changes: 47 additions & 13 deletions test/next_ls/completions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,14 @@ defmodule NextLS.CompletionsTest do
}
}

assert_result 2, [
%{
"data" => nil,
"documentation" => _,
"insertText" => "defmodule ${1:Foo} do\n $0\nend\n",
"kind" => 15,
"label" => "defmodule/2",
"insertTextFormat" => 2
}
]
assert_result 2, results

assert_match %{
"insertText" => "defmodule ${1:Foo} do\n $0\nend\n",
"kind" => 15,
"label" => "defmodule/2",
"insertTextFormat" => 2
} in results
end

test "aliases in document", %{client: client, foo: foo} do
Expand Down Expand Up @@ -451,9 +449,7 @@ defmodule NextLS.CompletionsTest do

assert_result 2, results

assert_match(
%{"data" => _, "documentation" => _, "insertText" => "Bing", "kind" => 9, "label" => "Bing"} in results
)
assert_match %{"data" => _, "insertText" => "Bing", "kind" => 9, "label" => "Bing"} in results
end

test "inside alias special form", %{client: client, foo: foo} do
Expand Down Expand Up @@ -583,4 +579,42 @@ defmodule NextLS.CompletionsTest do
}
]
end

test "variable and param completions", %{client: client, foo: foo} do
uri = uri(foo)

did_open(client, foo, """
defmodule Foo do
def run({vampire, %{foo: villain}}, [vim | vrest], vroom) do
var = "hi"
v
end
end
""")

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

assert_result 2, results

assert_match %{"kind" => 6, "label" => "vampire"} in results
assert_match %{"kind" => 6, "label" => "villain"} in results
assert_match %{"kind" => 6, "label" => "vim"} in results
assert_match %{"kind" => 6, "label" => "vrest"} in results
assert_match %{"kind" => 6, "label" => "vroom"} in results
assert_match %{"kind" => 6, "label" => "var"} in results
end
end

0 comments on commit d0fd731

Please sign in to comment.