From 050dd67e7466d08844c1e08b4a849f3fb69eb865 Mon Sep 17 00:00:00 2001 From: Xu Li Date: Sat, 24 Jun 2023 04:48:31 +0800 Subject: [PATCH 1/2] complete with parens if there are remote calls --- .../language_server/providers/completion.ex | 5 ++++- .../test/providers/completion_test.exs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/apps/language_server/lib/language_server/providers/completion.ex b/apps/language_server/lib/language_server/providers/completion.ex index f077a606f..21cb927b1 100644 --- a/apps/language_server/lib/language_server/providers/completion.ex +++ b/apps/language_server/lib/language_server/providers/completion.ex @@ -1030,6 +1030,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do } = info %{ + prefix: prefix, pipe_before?: pipe_before?, capture_before?: capture_before?, text_after_cursor: text_after_cursor @@ -1038,7 +1039,9 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do locals_without_parens = Keyword.get(options, :locals_without_parens) signature_help_supported? = Keyword.get(options, :signature_help_supported, false) signature_after_complete? = Keyword.get(options, :signature_after_complete, true) - with_parens? = function_name_with_parens?(name, arity, locals_without_parens) + + remote_calls? = String.contains?(prefix, ".") + with_parens? = remote_calls? || function_name_with_parens?(name, arity, locals_without_parens) trigger_signature? = signature_help_supported? && ((arity == 1 && !pipe_before?) || arity > 1) diff --git a/apps/language_server/test/providers/completion_test.exs b/apps/language_server/test/providers/completion_test.exs index 4388d8d08..017486f67 100644 --- a/apps/language_server/test/providers/completion_test.exs +++ b/apps/language_server/test/providers/completion_test.exs @@ -829,6 +829,25 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do assert item["command"] == @signature_command end + test "complete with parens if there are remote calls" do + text = """ + defmodule MyModule do + def dummy_function() do + Map.drop + # ^ + end + end + """ + + {line, char} = {2, 12} + TestUtils.assert_has_cursor_char(text, line, char) + + opts = Keyword.merge(@supports, locals_without_parens: MapSet.new(drop: 2)) + {:ok, %{"items" => [item]}} = Completion.completion(text, line, char, opts) + + assert item["insertText"] == "drop($1)$0" + end + test "function with arity 0 does not triggers signature" do text = """ defmodule MyModule do From d2af01483b0bcc78fbba90dd9fac400ece800a1a Mon Sep 17 00:00:00 2001 From: Xu Li Date: Sat, 24 Jun 2023 22:52:43 +0800 Subject: [PATCH 2/2] move to context and using Code.Fragment.cursor_context/1 --- .../lib/language_server/providers/completion.ex | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/language_server/lib/language_server/providers/completion.ex b/apps/language_server/lib/language_server/providers/completion.ex index 21cb927b1..e632f7b34 100644 --- a/apps/language_server/lib/language_server/providers/completion.ex +++ b/apps/language_server/lib/language_server/providers/completion.ex @@ -138,6 +138,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do text_before_cursor: text_before_cursor, text_after_cursor: text_after_cursor, prefix: prefix, + remote_calls?: match?({:dot, _, _}, Code.Fragment.cursor_context(prefix)), def_before: def_before, pipe_before?: Regex.match?(Regex.recompile!(~r/\|>\s*#{prefix}$/), text_before_cursor), capture_before?: Regex.match?(Regex.recompile!(~r/&#{prefix}$/), text_before_cursor), @@ -1030,7 +1031,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do } = info %{ - prefix: prefix, + remote_calls?: remote_calls?, pipe_before?: pipe_before?, capture_before?: capture_before?, text_after_cursor: text_after_cursor @@ -1039,8 +1040,6 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do locals_without_parens = Keyword.get(options, :locals_without_parens) signature_help_supported? = Keyword.get(options, :signature_help_supported, false) signature_after_complete? = Keyword.get(options, :signature_after_complete, true) - - remote_calls? = String.contains?(prefix, ".") with_parens? = remote_calls? || function_name_with_parens?(name, arity, locals_without_parens) trigger_signature? = signature_help_supported? && ((arity == 1 && !pipe_before?) || arity > 1)