[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#61412: [PATCH v2] Add inlay hints to eglot
From: |
Chinmay Dalal |
Subject: |
bug#61412: [PATCH v2] Add inlay hints to eglot |
Date: |
Wed, 15 Feb 2023 18:26:15 +0530 |
This uses different faces based on the "kind" of the hint:
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#inlayHintKind
---
lisp/progmodes/eglot.el | 85 ++++++++++++++++++++++++++++++++++++++++-
1 file changed, 84 insertions(+), 1 deletion(-)
diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 6caf589..101644f 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -404,6 +404,10 @@ This can be useful when using docker to run a language
server.")
(when (assoc 'flex completion-styles-alist)
(add-to-list 'completion-category-defaults '(eglot (styles flex basic))))
+(defcustom eglot-inlay-hints t
+ "If non-nil, enable inlay hints."
+ :type 'boolean)
+
;;; Constants
;;;
@@ -1624,7 +1628,8 @@ under cursor."
(const :tag "Highlight links in document" :documentLinkProvider)
(const :tag "Decorate color references" :colorProvider)
(const :tag "Fold regions of buffer" :foldingRangeProvider)
- (const :tag "Execute custom commands" :executeCommandProvider)))
+ (const :tag "Execute custom commands" :executeCommandProvider)
+ (const :tag "Inlay hints" :inlayHintProvider)))
(defun eglot--server-capable (&rest feats)
"Determine if current server is capable of FEATS."
@@ -1845,6 +1850,8 @@ If it is activated, also signal textDocument/didOpen."
(when (and buffer-file-name (eglot-current-server))
(setq eglot--diagnostics nil)
(eglot--managed-mode)
+ (unless (not eglot-inlay-hints)
+ (eglot-inlay-mode))
(eglot--signal-textDocument/didOpen))))
(add-hook 'find-file-hook 'eglot--maybe-activate-editing-mode)
@@ -3456,6 +3463,82 @@ If NOERROR, return predicate, else erroring function."
(revert-buffer)
(pop-to-buffer (current-buffer)))))
+(defface eglot-inlay-hint-face
+ '((t (:height 0.8 :inherit shadow)))
+ "Face used for inlay hint overlays.")
+
+(defface eglot-type-hint-face
+ '((t (:inherit eglot-inlay-hint)))
+ "Face used for type hints.")
+
+(defface eglot-parameter-hint-face
+ '((t (:inherit eglot-inlay-hint)))
+ "Face used for parameter hints.")
+
+(define-minor-mode eglot-inlay-mode
+ "Mode for displaying inlay hints."
+ :lighter " inlay"
+ (if eglot-inlay-mode
+ (progn
+ (add-hook 'after-save-hook 'eglot--update-hints 0 t)
+ (eglot--update-hints))
+ (progn
+ (remove-hook 'after-save-hook 'eglot--update-hints t)
+ (eglot--remove-hints))))
+
+(defun eglot--inlay-handler (buffer hints)
+ "Apply vector of inlay hints HINTS on buffer BUFFER."
+ (seq-doseq (hint hints)
+ (let* ((position (plist-get hint :position))
+ (line (plist-get position :line))
+ (character (plist-get position :character))
+ (kind (plist-get hint :kind))
+ (face (cond ((eq kind 1)
+ 'eglot-type-hint-face)
+ ((eq kind 2)
+ 'eglot-parameter-hint-face)
+ (t
+ 'eglot-inlay-hint-face)))
+ (label (plist-get hint :label)))
+ (when (stringp label)
+ (with-current-buffer buffer
+ (eglot--widening
+ (goto-char (point-min))
+ (forward-line line)
+ (eglot-move-to-column character)
+ (let ((overlay (make-overlay (point) (point))))
+ (overlay-put overlay 'before-string (propertize
+ (concat (if (plist-get
hint :paddingLeft) " " "")
+ label
+ (if (plist-get
hint :paddingRight) " " ""))
+ 'face face))
+ (overlay-put overlay 'is-eglot-inlay-hint t))))))))
+
+(defun eglot--remove-hints ()
+ "Remove inlay hints from the buffer."
+ (remove-overlays nil nil 'is-eglot-inlay-hint t))
+
+(defun eglot--update-hints ()
+ "Request inlay hints for the current buffer and apply them."
+ (unless (eglot--server-capable :inlayHintProvider)
+ (eglot--error "This LSP server isn't an :inlayHintProvider"))
+ ;; Remove existing hints
+ (eglot--remove-hints)
+ (let ((buffer (current-buffer)))
+ (jsonrpc-async-request
+ (eglot--current-server-or-lose)
+ :textDocument/inlayHint
+ (list
+ :textDocument (eglot--TextDocumentIdentifier)
+ :range (list
+ :start (list :line 0 :character 0)
+ :end (list
+ :line (count-lines (point-min) (point-max))
+ :character 0)))
+ :success-fn (lambda (hints)
+ (eglot--inlay-handler buffer hints))
+ :deferred t)))
+
;;; Hacks
;;;
--
2.39.1
- bug#61412: [PATCH] Add inlay hints to eglot, Chinmay Dalal, 2023/02/11
- bug#61412: [PATCH v3] Add inlay hints to eglot, Chinmay Dalal, 2023/02/15
- bug#61412: [PATCH v2] Add inlay hints to eglot,
Chinmay Dalal <=
- bug#61412: Inlay activation, Chinmay Dalal, 2023/02/15
- bug#61412: Inlay activation, Eli Zaretskii, 2023/02/15
- bug#61412: Inlay activation, Eli Zaretskii, 2023/02/15
- bug#61412: Inlay activation, Chinmay Dalal, 2023/02/15
- bug#61412: Inlay hints implementation, João Távora, 2023/02/21
- bug#61412: Inlay hints implementation, Eli Zaretskii, 2023/02/21
- bug#61412: Inlay hints implementation, Dimitri Belopopsky, 2023/02/21