Eglot: support multiple labels in same inlay hint

Mainly the rust-analyzer LSP server uses this.  There are still more
things we could support, like tooltips and stuff.

* lisp/progmodes/eglot.el (lsp-interface-alist): Add
InlayHintLabelPart.
(eglot--update-hints-1): Support multiple labels for same hint.
This commit is contained in:
João Távora 2023-02-27 14:54:53 +00:00
parent 4a5eda7ed2
commit f601e9666d

View file

@ -486,7 +486,8 @@ This can be useful when using docker to run a language server.")
(WorkspaceEdit () (:changes :documentChanges)) (WorkspaceEdit () (:changes :documentChanges))
(WorkspaceSymbol (:name :kind) (:containerName :location :data)) (WorkspaceSymbol (:name :kind) (:containerName :location :data))
(InlayHint (:position :label) (:kind :textEdits :tooltip :paddingLeft (InlayHint (:position :label) (:kind :textEdits :tooltip :paddingLeft
:paddingRight :data))) :paddingRight :data))
(InlayHintLabelPart (:value) (:tooltip :location :command)))
"Alist (INTERFACE-NAME . INTERFACE) of known external LSP interfaces. "Alist (INTERFACE-NAME . INTERFACE) of known external LSP interfaces.
INTERFACE-NAME is a symbol designated by the spec as INTERFACE-NAME is a symbol designated by the spec as
@ -3596,20 +3597,29 @@ If NOERROR, return predicate, else erroring function."
(eglot--lambda ((InlayHint) position kind label paddingLeft paddingRight) (eglot--lambda ((InlayHint) position kind label paddingLeft paddingRight)
(goto-char (eglot--lsp-position-to-point position)) (goto-char (eglot--lsp-position-to-point position))
(when (or (> (point) to) (< (point) from)) (cl-return)) (when (or (> (point) to) (< (point) from)) (cl-return))
(let ((ov (make-overlay (point) (point))) (let ((left-pad (and paddingLeft
(left-pad (and paddingLeft (not (memq (char-before) '(32 9))))) (not (memq (char-before) '(32 9))) " "))
(right-pad (and paddingRight (not (memq (char-after) '(32 9))))) (right-pad (and paddingRight
(text (if (stringp label) (not (memq (char-after) '(32 9))) " ")))
label (plist-get (elt label 0) :value)))) (cl-flet
(overlay-put ov 'before-string ((do-it (text lpad rpad)
(propertize (let ((ov (make-overlay (point) (point))))
(concat (and left-pad " ") text (and right-pad " ")) (overlay-put ov 'before-string
'face (pcase kind (propertize
(1 'eglot-type-hint-face) (concat lpad text rpad)
(2 'eglot-parameter-hint-face) 'face (pcase kind
(_ 'eglot-inlay-hint-face)))) (1 'eglot-type-hint-face)
(overlay-put ov 'eglot--inlay-hint t) (2 'eglot-parameter-hint-face)
(overlay-put ov 'eglot--overlay t))))) (_ 'eglot-inlay-hint-face))))
(overlay-put ov 'eglot--inlay-hint t)
(overlay-put ov 'eglot--overlay t))))
(if (stringp label) (do-it label left-pad right-pad)
(cl-loop
for i from 0 for ldetail across label
do (eglot--dbind ((InlayHintLabelPart) value) ldetail
(do-it value
(and (zerop i) left-pad)
(and (= i (1- (length label))) right-pad))))))))))
(jsonrpc-async-request (jsonrpc-async-request
(eglot--current-server-or-lose) (eglot--current-server-or-lose)
:textDocument/inlayHint :textDocument/inlayHint