Skip to content

Commit

Permalink
Add a new config - elixirLS.autoInsertRequiredAlias (#881)
Browse files Browse the repository at this point in the history
Co-authored-by: c4710n <[email protected]>
  • Loading branch information
c4710n and c4710n committed May 6, 2023
1 parent 2a98fc4 commit bb09876
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ Below is a list configuration options supported by ElixirLS language server. Ple
<dt>elixirLS.fetchDeps</dt><dd>Automatically fetch project dependencies when compiling</dd>
<dt>elixirLS.suggestSpecs</dt><dd>Suggest @spec annotations inline using Dialyzer's inferred success typings (Requires Dialyzer)</dd>
<dt>elixirLS.trace.server</dt><dd>Traces the communication between VS Code and the Elixir language server.</dd>
<dt>elixirLS.autoInsertRequiredAlias</dt><dd>Enable auto-insert required alias. By default, it's true, which means enabled.</dd>
<dt>elixirLS.signatureAfterComplete</dt><dd>Show signature help after confirming autocomplete</dd>
<dt>elixirLS.enableTestLenses</dt><dd>Show code lenses to run tests in terminal</dd>
<dt>elixirLS.additionalWatchedExtensions</dt><dd>Additional file types capable of triggering a build on change</dd>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
)

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

defp build_suggestions(text, line, character, options) do
required_alias = Keyword.get(options, :auto_insert_required_alias, true)
ElixirSense.suggestions(text, line, character, required_alias: required_alias)
end

defp maybe_add_do(completion_items, context) do
if String.ends_with?(context.text_before_cursor, " do") && context.text_after_cursor == "" do
item = %__MODULE__{
Expand Down
6 changes: 5 additions & 1 deletion apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@ defmodule ElixirLS.LanguageServer.Server do
end

defp handle_request(completion_req(_id, uri, line, character), state = %__MODULE__{}) do
settings = state.settings || %{}

source_file = get_source_file(state, uri)

snippets_supported =
Expand Down Expand Up @@ -710,7 +712,8 @@ defmodule ElixirLS.LanguageServer.Server do
end
|> MapSet.new()

signature_after_complete = Map.get(state.settings || %{}, "signatureAfterComplete", true)
auto_insert_required_alias = Map.get(settings, "autoInsertRequiredAlias", true)
signature_after_complete = Map.get(settings, "signatureAfterComplete", true)

path =
case uri do
Expand All @@ -725,6 +728,7 @@ defmodule ElixirLS.LanguageServer.Server do
tags_supported: tags_supported,
signature_help_supported: signature_help_supported,
locals_without_parens: locals_without_parens,
auto_insert_required_alias: auto_insert_required_alias,
signature_after_complete: signature_after_complete,
file_path: path
)
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 @@ -443,6 +443,35 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
] = item["additionalTextEdits"]
end

test "suggests nothing when auto_insert_required_alias is false" do
supports = Keyword.put(@supports, :auto_insert_required_alias, false)

text = """
defmodule MyModule do
@moduledoc \"\"\"
This
is a
long
moduledoc
\"\"\"
def dummy_function() do
ExampleS
# ^
end
end
"""

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

{:ok, %{"items" => items}} = Completion.completion(text, line, char, supports)

# nothing is suggested
assert [] = items
end

test "no crash on first line" do
text = "defmodule MyModule do"

Expand Down

0 comments on commit bb09876

Please sign in to comment.