Skip to content

Commit

Permalink
Return resolved signature via company-docsig
Browse files Browse the repository at this point in the history
  • Loading branch information
wyuenho committed Dec 13, 2024
1 parent 28cb228 commit 71bc11c
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions lsp-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ See #2675"
(unless (lsp-get data :import_for_trait_assoc_item)
(lsp-put data :import_for_trait_assoc_item :json-false)))))

(defun lsp-completion-resolve (item)
(defun lsp-completion--resolve (item)
"Resolve completion ITEM.
ITEM can be string or a CompletionItem"
(cl-assert item nil "Completion item must not be nil")
Expand All @@ -212,9 +212,7 @@ ITEM can be string or a CompletionItem"
item))
(_ completion-item)))))

(defalias 'lsp-completion--resolve 'lsp-completion-resolve)

(defun lsp-completion-resolve-async (item callback &optional cleanup-fn)
(defun lsp-completion--resolve-async (item callback &optional cleanup-fn)
"Resolve completion ITEM asynchronously with CALLBACK.
The CLEANUP-FN will be called to cleanup."
(cl-assert item nil "Completion item must not be nil")
Expand Down Expand Up @@ -245,12 +243,10 @@ The CLEANUP-FN will be called to cleanup."
(funcall callback completion-item)
(when cleanup-fn (funcall cleanup-fn))))))

(defalias 'lsp-completion--resolve-async 'lsp-completion-resolve-async)

(defun lsp-completion--annotate (item)
"Annotate ITEM detail."
(defun lsp-completion--get-label-detail (item)
"Construct label detail from completion item ITEM."
(-let (((completion-item &as &CompletionItem :detail? :kind? :label-details?)
(get-text-property 0 'lsp-completion-unresolved-item item)))
item))
(concat (when (and lsp-completion-show-detail detail?)
(concat " " (s-replace "\r" "" detail?)))
(when (and lsp-completion-show-label-description label-details?)
Expand All @@ -260,6 +256,12 @@ The CLEANUP-FN will be called to cleanup."
(when-let* ((kind-name (and kind? (aref lsp-completion--item-kind kind?))))
(format " (%s)" kind-name))))))

(defun lsp-completion--annotate (cand)
"Annotation function for completion candidate CAND.
Returns unresolved completion item detail."
(lsp-completion--get-label-detail (get-text-property 0 'lsp-completion-unresolved-item cand)))

(defun lsp-completion--looking-back-trigger-characterp (trigger-characters)
"Return character if text before point match any of the TRIGGER-CHARACTERS."
(unless (= (point) (line-beginning-position))
Expand Down Expand Up @@ -457,12 +459,19 @@ The MARKERS and PREFIX value will be attached to each candidate."
(setq label-pos 0)))
matches)))

(defun lsp-completion--company-docsig (cand)
"Signature for completion candidate CAND.
Returns resolved completion item details."
(and (lsp-completion--resolve cand)
(lsp-completion--get-label-detail (get-text-property 0 'lsp-completion-item cand))))

(defun lsp-completion--get-documentation (item)
"Get doc comment for completion ITEM."
(or (get-text-property 0 'lsp-completion-item-doc item)
(-let* (((&CompletionItem :detail?
:documentation?)
(get-text-property 0 'lsp-completion-item (lsp-completion-resolve item)))
(get-text-property 0 'lsp-completion-item (lsp-completion--resolve item)))
(doc
(if (and detail? documentation?)
;; detail was resolved, that means the candidate list has no
Expand Down Expand Up @@ -644,6 +653,7 @@ The MARKERS and PREFIX value will be attached to each candidate."
(goto-char (1- (point))))
(and triggered-by-char? t)))
:company-match #'lsp-completion--company-match
:company-docsig #'lsp-completion--company-docsig
:company-doc-buffer (-compose #'lsp-doc-buffer
#'lsp-completion--get-documentation)
:exit-function
Expand All @@ -666,7 +676,7 @@ Others: CANDIDATES"
;; see #3498 typescript-language-server does not provide the
;; proper insertText without resolving.
(if (lsp-completion--find-workspace 'ts-ls)
(lsp-completion-resolve candidate)
(lsp-completion--resolve candidate)
candidate))
((&plist 'lsp-completion-item item
'lsp-completion-start-point start-point
Expand Down Expand Up @@ -708,14 +718,14 @@ Others: CANDIDATES"
(not (seq-empty-p additional-text-edits?)))
(lsp--apply-text-edits additional-text-edits? 'completion)
(-let [(callback cleanup-fn) (lsp--create-apply-text-edits-handlers)]
(lsp-completion-resolve-async
(lsp-completion--resolve-async
item
(-compose callback #'lsp:completion-item-additional-text-edits?)
cleanup-fn))))

(if (or resolved command?)
(when command? (lsp--execute-command command?))
(lsp-completion-resolve-async
(lsp-completion--resolve-async
item
(-lambda ((&CompletionItem? :command?))
(when command? (lsp--execute-command command?)))))
Expand Down

0 comments on commit 71bc11c

Please sign in to comment.