Skip to content

Commit 24ddf88

Browse files
authored
Merge pull request #177 from axelson/dont-return-snippets-to-clients-that-dont-support-them
Don't return snippets to clients that don't support snippets
2 parents 9a21cca + 5246864 commit 24ddf88

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Meta:
55

66
Bug Fixes:
77
- 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)
8+
- 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)
89

910
### v0.3.2: 28 Mar 2020
1011

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -588,33 +588,37 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
588588
end
589589

590590
defp items_to_json(items, snippets_supported) do
591+
items =
592+
Enum.reject(items, fn item ->
593+
!snippets_supported && snippet?(item)
594+
end)
595+
591596
for {item, idx} <- Enum.with_index(items) do
592-
json = %{
593-
"label" => item.label,
594-
"kind" => completion_kind(item.kind),
595-
"detail" => item.detail,
596-
"documentation" => %{"value" => item.documentation, kind: "markdown"},
597-
"filterText" => item.filter_text,
598-
"sortText" => String.pad_leading(to_string(idx), 8, "0")
599-
}
597+
item_to_json(item, idx, snippets_supported)
598+
end
599+
end
600600

601-
json =
601+
defp item_to_json(item, idx, snippets_supported) do
602+
json = %{
603+
"label" => item.label,
604+
"kind" => completion_kind(item.kind),
605+
"detail" => item.detail,
606+
"documentation" => %{"value" => item.documentation, kind: "markdown"},
607+
"filterText" => item.filter_text,
608+
"sortText" => String.pad_leading(to_string(idx), 8, "0"),
609+
"insertText" => item.insert_text,
610+
"insertTextFormat" =>
602611
if snippets_supported do
603-
Map.merge(json, %{
604-
"insertText" => item.insert_text,
605-
"insertTextFormat" => insert_text_format(:snippet)
606-
})
612+
insert_text_format(:snippet)
607613
else
608-
regex = Regex.recompile!(~r/\$(\{.*\})|(\$\d+).*$/)
609-
text = Regex.replace(regex, item.insert_text, "")
610-
611-
Map.merge(json, %{
612-
"insertText" => text,
613-
"insertTextFormat" => insert_text_format(:plain_text)
614-
})
614+
insert_text_format(:plain_text)
615615
end
616+
}
616617

617-
for {k, v} <- json, not is_nil(v), into: %{}, do: {k, v}
618-
end
618+
for {k, v} <- json, not is_nil(v), into: %{}, do: {k, v}
619+
end
620+
621+
defp snippet?(item) do
622+
item.kind == :snippet || String.match?(item.insert_text, ~r/\$\d/)
619623
end
620624
end

apps/language_server/test/providers/completion_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
5656
end
5757
end
5858

59+
test "unless with snippets not supported does not return a completion" do
60+
text = """
61+
defmodule MyModule do
62+
require Logger, as: LAlias
63+
64+
def dummy_function() do
65+
unless
66+
# ^
67+
end
68+
end
69+
"""
70+
71+
{line, char} = {4, 10}
72+
TestUtils.assert_has_cursor_char(text, line, char)
73+
74+
{:ok, %{"items" => items}} = Completion.completion(text, line, char, true)
75+
assert length(items) == 1
76+
77+
{:ok, %{"items" => items}} = Completion.completion(text, line, char, false)
78+
assert length(items) == 0
79+
end
80+
5981
test "provides completions for protocol functions" do
6082
text = """
6183
defimpl ElixirLS.LanguageServer.Fixtures.ExampleProtocol, for: MyModule do

0 commit comments

Comments
 (0)