Skip to content

Commit 13a344b

Browse files
authoredMay 13, 2024
feat: include do as a completions item/snippet (#472)
1 parent 535d0ee commit 13a344b

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed
 

‎lib/next_ls.ex

+3
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,9 @@ defmodule NextLS do
678678
:file ->
679679
{name, GenLSP.Enumerations.CompletionItemKind.file(), ""}
680680

681+
:reserved ->
682+
{name, GenLSP.Enumerations.CompletionItemKind.keyword(), ""}
683+
681684
:keyword ->
682685
{name, GenLSP.Enumerations.CompletionItemKind.field(), ""}
683686

‎lib/next_ls/autocomplete.ex

+16-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ defmodule NextLS.Autocomplete do
6565
expand_dot_call(path, List.to_atom(hint), runtime, env)
6666

6767
:expr ->
68-
expand_container_context(code, :expr, "", runtime, env) || expand_local_or_var(code, "", runtime, env)
68+
expand_container_context(code, :expr, "", runtime, env) ||
69+
expand_local_or_var(code, "", runtime, env)
6970

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

9596
{:operator_call, operator} when operator in ~w(|)c ->
96-
expand_container_context(code, :expr, "", runtime, env) || expand_local_or_var("", "", runtime, env)
97+
expand_container_context(code, :expr, "", runtime, env) ||
98+
expand_local_or_var("", "", runtime, env)
9799

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

246248
defp expand_local_or_var(code, hint, runtime, env) do
247-
format_expansion(match_var(code, hint, runtime, env) ++ match_local(hint, false, runtime, env))
249+
format_expansion(
250+
match_keywords(hint) ++ match_var(code, hint, runtime, env) ++ match_local(hint, false, runtime, env)
251+
)
252+
end
253+
254+
defp match_keywords(hint) do
255+
for %{name: name} = kw <- [%{kind: :reserved, name: "do"}], String.starts_with?(name, hint) do
256+
kw
257+
end
248258
end
249259

250260
defp expand_local(hint, exact?, runtime, env) do
@@ -419,7 +429,9 @@ defmodule NextLS.Autocomplete do
419429
alias = value_from_alias(aliases, env),
420430
true <-
421431
Keyword.keyword?(pairs) and ensure_loaded?(alias, runtime) and
422-
NextLS.Runtime.execute!(runtime, do: Kernel.function_exported?(alias, :__struct__, 1)) do
432+
NextLS.Runtime.execute!(runtime,
433+
do: Kernel.function_exported?(alias, :__struct__, 1)
434+
) do
423435
{:struct, alias, pairs}
424436
else
425437
_ -> nil

‎lib/next_ls/snippet.ex

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ defmodule NextLS.Snippet do
33

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

6+
def get("do", nil, _opts) do
7+
%{
8+
kind: GenLSP.Enumerations.CompletionItemKind.snippet(),
9+
insert_text_format: GenLSP.Enumerations.InsertTextFormat.snippet(),
10+
insert_text: """
11+
do
12+
$0
13+
end
14+
"""
15+
}
16+
end
17+
618
def get("defmodule/2", nil, opts) do
719
path = Keyword.get(opts, :uri)
820

0 commit comments

Comments
 (0)
Please sign in to comment.