Skip to content

Add end keyword to completions #599

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

Merged
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
18 changes: 18 additions & 0 deletions apps/language_server/lib/language_server/providers/completion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
|> maybe_reject_derived_functions(context, options)
|> Enum.map(&from_completion_item(&1, context, options))
|> maybe_add_do(context)
|> maybe_add_end(context)

items_json =
items
Expand Down Expand Up @@ -166,6 +167,23 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
end
end

defp maybe_add_end(completion_items, context) do
if String.ends_with?(context.text_before_cursor, "end") && context.text_after_cursor == "" do
item = %__MODULE__{
label: "end",
kind: :keyword,
detail: "keyword",
insert_text: "end",
tags: [],
priority: 0
}

[item | completion_items]
else
completion_items
end
end

## Helpers

defp is_incomplete(items) do
Expand Down
29 changes: 29 additions & 0 deletions apps/language_server/test/providers/completion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
assert first_item["label"] == "do"
end

test "end is returned" do
text = """
defmodule MyModule do
require Logger

def engineering_department, do: :eng

def fun do
:ok
end
# ^
end
"""

{line, char} = {7, 5}
TestUtils.assert_has_cursor_char(text, line, char)

{:ok, %{"items" => [first_item | items]}} = Completion.completion(text, line, char, @supports)

assert first_item["label"] == "end"

completions =
items
|> Enum.filter(&(&1["detail"] =~ "engineering_department"))
|> Enum.map(& &1["insertText"])

assert completions == ["engineering_department()"]
end

test "returns all Logger completions on normal require" do
text = """
defmodule MyModule do
Expand Down