Skip to content

Commit

Permalink
WIP: added true, false and nil as keywords for completion
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinKalb committed Sep 4, 2023
1 parent 8815767 commit a83f937
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
33 changes: 33 additions & 0 deletions apps/language_server/lib/language_server/providers/completion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
|> Enum.map(&from_completion_item(&1, context, options))
|> maybe_add_do(context)
|> maybe_add_end(context)
|> maybe_add_keywords(context)
|> Enum.reject(&is_nil/1)
|> sort_items()

Expand Down Expand Up @@ -238,8 +239,40 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
end
end

defp maybe_add_keywords(completion_items, %{text_after_cursor: ""} = context) do
kw = Map.get(context, :text_before_cursor) |> String.trim_leading() |> get_keyword()

if kw != "" do
item = %__MODULE__{
label: kw,
kind: :keyword,
detail: "keyword",
insert_text: kw,
tags: [],
priority: 0
}

[item | completion_items]
else
completion_items
end
end

defp maybe_add_keywords(completion_items, _context) do
completion_items
end

## Helpers

defp get_keyword(t) do
cond do
Enum.member?(["t", "tr", "tru", "true"], t) -> "true"
Enum.member?(["f", "fa", "fal", "fals", "false"], t) -> "false"
Enum.member?(["n", "ni", "nil"], t) -> "nil"
true -> ""
end
end

defp is_incomplete(items) do
if Enum.empty?(items) do
false
Expand Down
36 changes: 36 additions & 0 deletions apps/language_server/test/providers/completion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,42 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
end
end

describe "keyword completion" do
setup do
text = """
defmodule MyModule do
def dummy_function() do
t
#^
end
end
"""

%{text: text, location: {2, 5}}
end

test "first", context do
%{text: text, location: {line, char}} = context

TestUtils.assert_has_cursor_char(text, line, char)

opts = Keyword.merge(@supports, signature_after_complete: true)
{:ok, %{"items" => items}} = Completion.completion(text, line, char, opts)

[item] = items |> Enum.filter(&(&1["insertText"] == "true"))

assert %{
"detail" => "keyword",
"documentation" => %{:kind => "markdown", "value" => ""},
"insertText" => "true",
"insertTextFormat" => 2,
"kind" => 14,
"label" => "true",
"sortText" => "00000000"
} = item
end
end

describe "function completion" do
setup do
text = """
Expand Down

0 comments on commit a83f937

Please sign in to comment.