Skip to content

Commit 3d7d8c3

Browse files
authored
Add a do autocompletion (#593)
* Add a `do` autocompletion For some clients typing `do` will result in autocompletion which is typically not what the developer wants, an example autocompletion is `defoverridable`. So this PR works around that by adding `do` as an autocompletion when the exact text entered is `"do"`. * formatting * Formatting on elixir 1.12
1 parent 193459b commit 3d7d8c3

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

apps/language_server/lib/language_server/providers/completion.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
137137
ElixirSense.suggestions(text, line + 1, character + 1)
138138
|> maybe_reject_derived_functions(context, options)
139139
|> Enum.map(&from_completion_item(&1, context, options))
140+
|> maybe_add_do(context)
140141

141142
items_json =
142143
items
@@ -148,6 +149,23 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
148149
{:ok, %{"isIncomplete" => is_incomplete(items_json), "items" => items_json}}
149150
end
150151

152+
defp maybe_add_do(completion_items, context) do
153+
if String.ends_with?(context.text_before_cursor, " do") && context.text_after_cursor == "" do
154+
item = %__MODULE__{
155+
label: "do",
156+
kind: :keyword,
157+
detail: "keyword",
158+
insert_text: "do\n$0\nend",
159+
tags: [],
160+
priority: 0
161+
}
162+
163+
[item | completion_items]
164+
else
165+
completion_items
166+
end
167+
end
168+
151169
## Helpers
152170

153171
defp is_incomplete(items) do

apps/language_server/test/providers/completion_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
3131
end
3232
end
3333

34+
test "do is returned" do
35+
text = """
36+
defmodule MyModule do
37+
require Logger
38+
39+
def fun do
40+
# ^
41+
end
42+
end
43+
"""
44+
45+
{line, char} = {3, 12}
46+
TestUtils.assert_has_cursor_char(text, line, char)
47+
48+
{:ok, %{"items" => [first_item | _items]}} =
49+
Completion.completion(text, line, char, @supports)
50+
51+
assert first_item["label"] == "do"
52+
end
53+
3454
test "returns all Logger completions on normal require" do
3555
text = """
3656
defmodule MyModule do

0 commit comments

Comments
 (0)