Skip to content

Commit 6a2f7b7

Browse files
committed
lsp-completion--annotate: support async completion resolve
1 parent 9b10410 commit 6a2f7b7

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

lsp-completion.el

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,56 @@ This will help minimize popup flickering issue in `company-mode'."
177177
'lsp-completion-markers markers
178178
'lsp-completion-prefix prefix)))
179179

180+
(defun lsp-completion--fix-resolve-data (item)
181+
"Patch `CompletionItem' ITEM for rust-analyzer otherwise resolve will fail.
182+
See #2675"
183+
(let ((data (lsp:completion-item-data? item)))
184+
(when (lsp-member? data :import_for_trait_assoc_item)
185+
(unless (lsp-get data :import_for_trait_assoc_item)
186+
(lsp-put data :import_for_trait_assoc_item :json-false)))))
187+
188+
(defun lsp-completion--resolve (item)
189+
"Resolve completion ITEM."
190+
(cl-assert item nil "Completion item must not be nil")
191+
(lsp-completion--fix-resolve-data item)
192+
(or (ignore-errors
193+
(when (lsp-feature? "completionItem/resolve")
194+
(lsp-request "completionItem/resolve"
195+
(lsp-delete (lsp-copy item) :_emacsStartPoint))))
196+
item))
197+
198+
(defun lsp-completion--resolve-async (item callback &optional cleanup-fn)
199+
"Resolve completion ITEM asynchronously with CALLBACK.
200+
The CLEANUP-FN will be called to cleanup."
201+
(cl-assert item nil "Completion item must not be nil")
202+
(lsp-completion--fix-resolve-data item)
203+
(ignore-errors
204+
(if (lsp-feature? "completionItem/resolve")
205+
(lsp-request-async "completionItem/resolve"
206+
(lsp-delete (lsp-copy item) :_emacsStartPoint)
207+
(lambda (result)
208+
(funcall callback result)
209+
(when cleanup-fn (funcall cleanup-fn)))
210+
:error-handler (lambda (err)
211+
(when cleanup-fn (funcall cleanup-fn))
212+
(error (lsp:json-error-message err)))
213+
:cancel-handler cleanup-fn
214+
:mode 'alive)
215+
(funcall callback item)
216+
(when cleanup-fn (funcall cleanup-fn)))))
217+
180218
(defun lsp-completion--annotate (item)
181219
"Annotate ITEM detail."
182-
(-let (((&CompletionItem :detail? :kind? :label-details?) (plist-get (text-properties-at 0 item)
183-
'lsp-completion-item)))
220+
(-let (((completion-item &as &CompletionItem :detail? :kind? :label-details?)
221+
(get-text-property 0 'lsp-completion-item item)))
222+
(unless (get-text-property 0 'lsp-completion-resolved item)
223+
(lsp-completion--resolve-async
224+
completion-item
225+
(lambda (resolved-item)
226+
(let ((len (length item)))
227+
(put-text-property 0 len 'lsp-completion-item resolved-item item)
228+
(put-text-property 0 len 'lsp-completion-resolved t item)))))
229+
184230
(concat (when (and lsp-completion-show-detail detail?)
185231
(concat " " (s-replace "\r" "" detail?)))
186232
(when (and lsp-completion-show-label-description label-details?)
@@ -705,44 +751,6 @@ The return is nil or in range of (0, inf)."
705751
(unless (zerop len)
706752
(/ score-numerator (1+ score-denominator) 1.0))))
707753

708-
(defun lsp-completion--fix-resolve-data (item)
709-
"Patch `CompletionItem' ITEM for rust-analyzer otherwise resolve will fail.
710-
See #2675"
711-
(let ((data (lsp:completion-item-data? item)))
712-
(when (lsp-member? data :import_for_trait_assoc_item)
713-
(unless (lsp-get data :import_for_trait_assoc_item)
714-
(lsp-put data :import_for_trait_assoc_item :json-false)))))
715-
716-
(defun lsp-completion--resolve (item)
717-
"Resolve completion ITEM."
718-
(cl-assert item nil "Completion item must not be nil")
719-
(lsp-completion--fix-resolve-data item)
720-
(or (ignore-errors
721-
(when (lsp-feature? "completionItem/resolve")
722-
(lsp-request "completionItem/resolve"
723-
(lsp-delete (lsp-copy item) :_emacsStartPoint))))
724-
item))
725-
726-
(defun lsp-completion--resolve-async (item callback &optional cleanup-fn)
727-
"Resolve completion ITEM asynchronously with CALLBACK.
728-
The CLEANUP-FN will be called to cleanup."
729-
(cl-assert item nil "Completion item must not be nil")
730-
(lsp-completion--fix-resolve-data item)
731-
(ignore-errors
732-
(if (lsp-feature? "completionItem/resolve")
733-
(lsp-request-async "completionItem/resolve"
734-
(lsp-delete (lsp-copy item) :_emacsStartPoint)
735-
(lambda (result)
736-
(funcall callback result)
737-
(when cleanup-fn (funcall cleanup-fn)))
738-
:error-handler (lambda (err)
739-
(when cleanup-fn (funcall cleanup-fn))
740-
(error (lsp:json-error-message err)))
741-
:cancel-handler cleanup-fn
742-
:mode 'alive)
743-
(funcall callback item)
744-
(when cleanup-fn (funcall cleanup-fn)))))
745-
746754

747755
;;;###autoload
748756
(defun lsp-completion--enable ()

0 commit comments

Comments
 (0)