Skip to content

Commit bb09876

Browse files
authored
Add a new config - elixirLS.autoInsertRequiredAlias (#881)
Co-authored-by: c4710n <c4710n@users.noreply.github.com>
1 parent 2a98fc4 commit bb09876

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ Below is a list configuration options supported by ElixirLS language server. Ple
301301
<dt>elixirLS.fetchDeps</dt><dd>Automatically fetch project dependencies when compiling</dd>
302302
<dt>elixirLS.suggestSpecs</dt><dd>Suggest @spec annotations inline using Dialyzer's inferred success typings (Requires Dialyzer)</dd>
303303
<dt>elixirLS.trace.server</dt><dd>Traces the communication between VS Code and the Elixir language server.</dd>
304+
<dt>elixirLS.autoInsertRequiredAlias</dt><dd>Enable auto-insert required alias. By default, it's true, which means enabled.</dd>
304305
<dt>elixirLS.signatureAfterComplete</dt><dd>Show signature help after confirming autocomplete</dd>
305306
<dt>elixirLS.enableTestLenses</dt><dd>Show code lenses to run tests in terminal</dd>
306307
<dt>elixirLS.additionalWatchedExtensions</dt><dd>Additional file types capable of triggering a build on change</dd>

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
155155
)
156156

157157
items =
158-
ElixirSense.suggestions(text, line, character, required_alias: true)
158+
build_suggestions(text, line, character, options)
159159
|> maybe_reject_derived_functions(context, options)
160160
|> Enum.map(&from_completion_item(&1, context, options))
161161
|> maybe_add_do(context)
@@ -171,6 +171,11 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
171171
{:ok, %{"isIncomplete" => is_incomplete(items_json), "items" => items_json}}
172172
end
173173

174+
defp build_suggestions(text, line, character, options) do
175+
required_alias = Keyword.get(options, :auto_insert_required_alias, true)
176+
ElixirSense.suggestions(text, line, character, required_alias: required_alias)
177+
end
178+
174179
defp maybe_add_do(completion_items, context) do
175180
if String.ends_with?(context.text_before_cursor, " do") && context.text_after_cursor == "" do
176181
item = %__MODULE__{

apps/language_server/lib/language_server/server.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,8 @@ defmodule ElixirLS.LanguageServer.Server do
670670
end
671671

672672
defp handle_request(completion_req(_id, uri, line, character), state = %__MODULE__{}) do
673+
settings = state.settings || %{}
674+
673675
source_file = get_source_file(state, uri)
674676

675677
snippets_supported =
@@ -710,7 +712,8 @@ defmodule ElixirLS.LanguageServer.Server do
710712
end
711713
|> MapSet.new()
712714

713-
signature_after_complete = Map.get(state.settings || %{}, "signatureAfterComplete", true)
715+
auto_insert_required_alias = Map.get(settings, "autoInsertRequiredAlias", true)
716+
signature_after_complete = Map.get(settings, "signatureAfterComplete", true)
714717

715718
path =
716719
case uri do
@@ -725,6 +728,7 @@ defmodule ElixirLS.LanguageServer.Server do
725728
tags_supported: tags_supported,
726729
signature_help_supported: signature_help_supported,
727730
locals_without_parens: locals_without_parens,
731+
auto_insert_required_alias: auto_insert_required_alias,
728732
signature_after_complete: signature_after_complete,
729733
file_path: path
730734
)

apps/language_server/test/providers/completion_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,35 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
443443
] = item["additionalTextEdits"]
444444
end
445445

446+
test "suggests nothing when auto_insert_required_alias is false" do
447+
supports = Keyword.put(@supports, :auto_insert_required_alias, false)
448+
449+
text = """
450+
defmodule MyModule do
451+
@moduledoc \"\"\"
452+
This
453+
is a
454+
long
455+
moduledoc
456+
457+
\"\"\"
458+
459+
def dummy_function() do
460+
ExampleS
461+
# ^
462+
end
463+
end
464+
"""
465+
466+
{line, char} = {10, 12}
467+
TestUtils.assert_has_cursor_char(text, line, char)
468+
469+
{:ok, %{"items" => items}} = Completion.completion(text, line, char, supports)
470+
471+
# nothing is suggested
472+
assert [] = items
473+
end
474+
446475
test "no crash on first line" do
447476
text = "defmodule MyModule do"
448477

0 commit comments

Comments
 (0)