Skip to content

Commit

Permalink
feat: include do as a completions item/snippet (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg authored May 13, 2024
1 parent 535d0ee commit 13a344b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,9 @@ defmodule NextLS do
:file ->
{name, GenLSP.Enumerations.CompletionItemKind.file(), ""}

:reserved ->
{name, GenLSP.Enumerations.CompletionItemKind.keyword(), ""}

:keyword ->
{name, GenLSP.Enumerations.CompletionItemKind.field(), ""}

Expand Down
20 changes: 16 additions & 4 deletions lib/next_ls/autocomplete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ defmodule NextLS.Autocomplete do
expand_dot_call(path, List.to_atom(hint), runtime, env)

:expr ->
expand_container_context(code, :expr, "", runtime, env) || expand_local_or_var(code, "", runtime, env)
expand_container_context(code, :expr, "", runtime, env) ||
expand_local_or_var(code, "", runtime, env)

{:local_or_var, local_or_var} ->
hint = List.to_string(local_or_var)
Expand Down Expand Up @@ -93,7 +94,8 @@ defmodule NextLS.Autocomplete do
expand_local(List.to_string(operator), true, runtime, env)

{:operator_call, operator} when operator in ~w(|)c ->
expand_container_context(code, :expr, "", runtime, env) || expand_local_or_var("", "", runtime, env)
expand_container_context(code, :expr, "", runtime, env) ||
expand_local_or_var("", "", runtime, env)

{:operator_call, _operator} ->
expand_local_or_var("", "", runtime, env)
Expand Down Expand Up @@ -244,7 +246,15 @@ defmodule NextLS.Autocomplete do
## Expand local or var

defp expand_local_or_var(code, hint, runtime, env) do
format_expansion(match_var(code, hint, runtime, env) ++ match_local(hint, false, runtime, env))
format_expansion(
match_keywords(hint) ++ match_var(code, hint, runtime, env) ++ match_local(hint, false, runtime, env)
)
end

defp match_keywords(hint) do
for %{name: name} = kw <- [%{kind: :reserved, name: "do"}], String.starts_with?(name, hint) do
kw
end
end

defp expand_local(hint, exact?, runtime, env) do
Expand Down Expand Up @@ -419,7 +429,9 @@ defmodule NextLS.Autocomplete do
alias = value_from_alias(aliases, env),
true <-
Keyword.keyword?(pairs) and ensure_loaded?(alias, runtime) and
NextLS.Runtime.execute!(runtime, do: Kernel.function_exported?(alias, :__struct__, 1)) do
NextLS.Runtime.execute!(runtime,
do: Kernel.function_exported?(alias, :__struct__, 1)
) do
{:struct, alias, pairs}
else
_ -> nil
Expand Down
12 changes: 12 additions & 0 deletions lib/next_ls/snippet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ defmodule NextLS.Snippet do

def get(label, trigger_character, opts \\ [])

def get("do", nil, _opts) do
%{
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
insert_text: """
do
$0
end
"""
}
end

def get("defmodule/2", nil, opts) do
path = Keyword.get(opts, :uri)

Expand Down

0 comments on commit 13a344b

Please sign in to comment.