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

feat: include do as a completions item/snippet #472

Merged
merged 1 commit into from
May 13, 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
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
Loading