Skip to content

Commit eaf7141

Browse files
authored
Fix usage of ...trigger-for-incomplete-completions (#3815)
Ensure that the completion session is the same as the previous incomplete completion before using trigger style `lsp/completion-trigger-kind-trigger-for-incomplete-completions`. Previously the internal function `lsp-completion--get-context` had no notion of whether the current completion request was the using the same context as the previous completion request. This internal function is only called by `lsp-completion-at-point`. `lsp-completion-at-point` does calculate whether this completion request is using the same context as the last result. We therefore now pass the information from `lsp-completion-at-point` down to `lsp-completion--get-context` and use it therein when making a determination about which trigger type of completion to use. This addresses issue #3028 where completions would stop working with the sourcekit-lsp language server for Swift. This language server in particular sends back incomplete completions frequently and also checks on its side to make sure any requested completion continuation context is the same as it was when originally requested. If not it cancels the request.
1 parent 9a85adf commit eaf7141

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

CHANGELOG.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
obsolete ~lsp-xml-format-quotes~.
5757
* Add [[https://github.com/oxalica/nil][nil]] support (additional server for Nix)
5858
* Add [[https://github.com/johnsoncodehk/volar/pull/1916][Volar 1.0]] support, refactor vue-language-server ~initializationOptions~, and remove multi server that is no longer needed.
59+
* Ensure that incomplete completions only trigger when retriggering from the same completion session #3028
5960
** Release 8.0.0
6061
* Add ~lsp-clients-angular-node-get-prefix-command~ to get the Angular server from another location which is still has ~/lib/node_modules~ in it.
6162
* Set ~lsp-clients-angular-language-server-command~ after the first connection to speed up subsequent connections.

lsp-completion.el

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,17 @@ The MARKERS and PREFIX value will be attached to each candidate."
394394
(lsp:completion-item-documentation?)
395395
(lsp--render-element)))
396396

397-
(defun lsp-completion--get-context (trigger-characters)
398-
"Get completion context with provided TRIGGER-CHARACTERS."
397+
(defun lsp-completion--get-context (trigger-characters same-session?)
398+
"Get completion context with provided TRIGGER-CHARACTERS and SAME-SESSION?."
399399
(let* ((triggered-by-char non-essential)
400400
(trigger-char (when triggered-by-char
401401
(lsp-completion--looking-back-trigger-characterp
402402
trigger-characters)))
403403
(trigger-kind (cond
404404
(trigger-char
405405
lsp/completion-trigger-kind-trigger-character)
406-
((equal (cl-second lsp-completion--cache) :incomplete)
406+
((and same-session?
407+
(equal (cl-second lsp-completion--cache) :incomplete))
407408
lsp/completion-trigger-kind-trigger-for-incomplete-completions)
408409
(t lsp/completion-trigger-kind-invoked))))
409410
(apply #'lsp-make-completion-context
@@ -457,7 +458,7 @@ The MARKERS and PREFIX value will be attached to each candidate."
457458
(-let* ((resp (lsp-request-while-no-input
458459
"textDocument/completion"
459460
(plist-put (lsp--text-document-position-params)
460-
:context (lsp-completion--get-context trigger-chars))))
461+
:context (lsp-completion--get-context trigger-chars same-session?))))
461462
(completed (and resp
462463
(not (and (lsp-completion-list? resp)
463464
(lsp:completion-list-is-incomplete resp)))))

test/lsp-completion-test.el

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@
8080
'("F" "daFo" "safo")
8181
'("F" "daFo" "safo"))))
8282

83-
(ert-deftest lsp-completion-test-get-context ()
83+
(ert-deftest lsp-completion-test-get-context-trigger-characters-no-cache ()
8484
(setq lsp-completion--cache nil)
8585
(cl-labels ((do-get-context (arg)
8686
(let ((non-essential arg))
87-
(lsp-completion--get-context '("_"))))
87+
(lsp-completion--get-context '("_") nil)))
8888
(do-test-trigger-kind (arg)
8989
(lsp:completion-context-trigger-kind
9090
(do-get-context arg)))
@@ -102,5 +102,32 @@
102102
(should (equal (do-test-trigger-kind t) 2))
103103
(should (equal (do-test-trigger-character t) "_")))))
104104

105+
(ert-deftest lsp-completion-test-get-context-incomplete-completion ()
106+
(setq lsp-completion--cache (list "not-used" :incomplete))
107+
(cl-labels ((do-get-context (arg same-session?)
108+
(let ((non-essential arg))
109+
(lsp-completion--get-context '("_") same-session?)))
110+
(do-test-trigger-kind (arg same-session?)
111+
(lsp:completion-context-trigger-kind
112+
(do-get-context arg same-session?))))
113+
(mocklet ((lsp-completion--looking-back-trigger-characterp))
114+
;; When the user manually invokes completion,
115+
(should (equal (do-test-trigger-kind nil nil) 1)) ;; and the session is different: expect a new completion
116+
(should (equal (do-test-trigger-kind nil t) 3)) ;; and the session is the same: expect a continued completion
117+
118+
;; When the user is typing a non-trigger character into the buffer,
119+
(should (equal (do-test-trigger-kind t nil) 1)) ;; and the session is different: expect a new completion
120+
(should (equal (do-test-trigger-kind t t) 3))) ;; and the session is the same: expect a continued completion
121+
122+
(mocklet ((lsp-completion--looking-back-trigger-characterp => "_"))
123+
;; When the user manually invokes completion,
124+
(should (equal (do-test-trigger-kind nil nil) 1)) ;; and the session is different: expect a new completion
125+
(should (equal (do-test-trigger-kind nil t) 3)) ;; and the session is the same: expect a continued completion
126+
127+
;; When the user is typing a trigger-character into the buffer,
128+
(should (equal (do-test-trigger-kind t nil) 2)) ;; and the session is different: expect a new trigger-character completion
129+
(should (equal (do-test-trigger-kind t t) 2))) ;; and the session is the same: expect a new trigger-character completion
130+
))
131+
105132
(provide 'lsp-completion-test)
106133
;;; lsp-completion-test.el ends here

0 commit comments

Comments
 (0)