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

Don't return snippets to clients that don't support snippets #177

Merged
merged 1 commit into from
Apr 2, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Meta:

Bug Fixes:
- Fix `textDocument/documentSymbol` on a non-fully initialized server (thanks [Jason Axelson](https://github.com/axelson)) [#173](https://github.com/elixir-lsp/elixir-ls/pull/173)
- Don't return snippets to clients that don't declare `snippetSupport` for completions (thanks [Jason Axelson](https://github.com/axelson)) [#173](https://github.com/elixir-lsp/elixir-ls/pull/177)

### v0.3.2: 28 Mar 2020

Expand Down
48 changes: 26 additions & 22 deletions apps/language_server/lib/language_server/providers/completion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -588,33 +588,37 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
end

defp items_to_json(items, snippets_supported) do
items =
Enum.reject(items, fn item ->
!snippets_supported && snippet?(item)
end)

for {item, idx} <- Enum.with_index(items) do
json = %{
"label" => item.label,
"kind" => completion_kind(item.kind),
"detail" => item.detail,
"documentation" => %{"value" => item.documentation, kind: "markdown"},
"filterText" => item.filter_text,
"sortText" => String.pad_leading(to_string(idx), 8, "0")
}
item_to_json(item, idx, snippets_supported)
end
end

json =
defp item_to_json(item, idx, snippets_supported) do
json = %{
"label" => item.label,
"kind" => completion_kind(item.kind),
"detail" => item.detail,
"documentation" => %{"value" => item.documentation, kind: "markdown"},
"filterText" => item.filter_text,
"sortText" => String.pad_leading(to_string(idx), 8, "0"),
"insertText" => item.insert_text,
"insertTextFormat" =>
if snippets_supported do
Map.merge(json, %{
"insertText" => item.insert_text,
"insertTextFormat" => insert_text_format(:snippet)
})
insert_text_format(:snippet)
else
regex = Regex.recompile!(~r/\$(\{.*\})|(\$\d+).*$/)
text = Regex.replace(regex, item.insert_text, "")

Map.merge(json, %{
"insertText" => text,
"insertTextFormat" => insert_text_format(:plain_text)
})
insert_text_format(:plain_text)
end
}

for {k, v} <- json, not is_nil(v), into: %{}, do: {k, v}
end
for {k, v} <- json, not is_nil(v), into: %{}, do: {k, v}
end

defp snippet?(item) do
item.kind == :snippet || String.match?(item.insert_text, ~r/\$\d/)
end
end
22 changes: 22 additions & 0 deletions apps/language_server/test/providers/completion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
end
end

test "unless with snippets not supported does not return a completion" do
text = """
defmodule MyModule do
require Logger, as: LAlias

def dummy_function() do
unless
# ^
end
end
"""

{line, char} = {4, 10}
TestUtils.assert_has_cursor_char(text, line, char)

{:ok, %{"items" => items}} = Completion.completion(text, line, char, true)
assert length(items) == 1

{:ok, %{"items" => items}} = Completion.completion(text, line, char, false)
assert length(items) == 0
end

test "provides completions for protocol functions" do
text = """
defimpl ElixirLS.LanguageServer.Fixtures.ExampleProtocol, for: MyModule do
Expand Down