diff --git a/apps/language_server/lib/language_server/providers/completion.ex b/apps/language_server/lib/language_server/providers/completion.ex index 202441bc3..2f06a9861 100644 --- a/apps/language_server/lib/language_server/providers/completion.ex +++ b/apps/language_server/lib/language_server/providers/completion.ex @@ -587,6 +587,11 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do "defmodule #{suggest_module_name(file_path)}$1 do\n\t$0\nend" end + defp snippet_for({"Kernel", "defprotocol"}, %{file_path: file_path}) + when is_binary(file_path) do + "defprotocol #{suggest_module_name(file_path)}$1 do\n\t$0\nend" + end + defp snippet_for(key, %{pipe_before?: true}) do # Get pipe-friendly version of snippet if available, otherwise fallback to standard Map.get(@pipe_func_snippets, key) || Map.get(@func_snippets, key) diff --git a/apps/language_server/test/providers/completion_test.exs b/apps/language_server/test/providers/completion_test.exs index b14afd083..941b32242 100644 --- a/apps/language_server/test/providers/completion_test.exs +++ b/apps/language_server/test/providers/completion_test.exs @@ -990,6 +990,62 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do "insertText" => "defmodule $1 do\n\t$0\nend" } = first end + + test "will suggest defprotocol with protocol_name snippet when file path matches **/lib/**/*.ex" do + text = """ + defpro + # ^ + """ + + {line, char} = {0, 6} + + TestUtils.assert_has_cursor_char(text, line, char) + + assert {:ok, %{"items" => [first | _] = _items}} = + Completion.completion( + text, + line, + char, + @supports + |> Keyword.put( + :file_path, + "/some/path/my_project/lib/my_project/sub_folder/my_file.ex" + ) + ) + + assert %{ + "label" => "defprotocol", + "insertText" => "defprotocol MyProject.SubFolder.MyFile$1 do\n\t$0\nend" + } = first + end + + test "will suggest defprotocol without protocol_name snippet when file path does not match expected patterns" do + text = """ + defpro + # ^ + """ + + {line, char} = {0, 6} + + TestUtils.assert_has_cursor_char(text, line, char) + + assert {:ok, %{"items" => [first | _] = _items}} = + Completion.completion( + text, + line, + char, + @supports + |> Keyword.put( + :file_path, + "/some/path/my_project/lib/my_project/sub_folder/my_file.heex" + ) + ) + + assert %{ + "label" => "defprotocol", + "insertText" => "defprotocol $1 do\n\t$0\nend" + } = first + end end describe "generic suggestions" do