[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#61412: closed ([PATCH] Add inlay hints to eglot)

From: GNU bug Tracking System
Subject: bug#61412: closed ([PATCH] Add inlay hints to eglot)
Date: Sat, 25 Feb 2023 00:21:02 +0000

Your message dated Sat, 25 Feb 2023 00:21:40 +0000
with message-id 
and subject line Re: bug#61412: Inlay hints implementation
has caused the debbugs.gnu.org bug report #61412,
regarding [PATCH] Add inlay hints to eglot
to be marked as done.

(If you believe you have received this mail in error, please contact

61412: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=61412
GNU Bug Tracking System
Contact help-debbugs@gnu.org with problems
--- Begin Message --- Subject: [PATCH] Add inlay hints to eglot Date: Sat, 11 Feb 2023 13:43:36 +0530
Hello all, I have improved upon the patch from bug#61066.

>From the original email:
> - I can't figure out a way to show the hints on a document without causing 
> lags or timeouts from the lsp server
This might have been because they were sending an asynchronous request, waiting
for the response, collecting the hints and displaying them. I have instead used
`jsonrpc-async-request` with a handler function which should remedy this issue.
I have tried this with rust-analyzer and faced no lags.

The original patch also checked the wrong condition (`cond 
in `eglot-inlay-mode` and had a superfluous `(run-hooks 
(presumably copy-pasted from `define-minor-mode eglot--managed-mode`), I have
fixed that.

>From a reply to the original email:
> AFAIU, inlay hints provide information of the same kind as ElDoc and
> in similar manner from the display and UX POV.  So I think this
> feature should work via ElDoc, not as a separate from-the-scratch
> implementation.
This can't be done via ElDoc because the purpose of inlay hints is to display
variable types and parameter names without moving the cursor to the location.
One can already see this information if they move their cursor to the variable
or function and wait for the "hover" - inlay hints were added to the spec after
this which (IMHO) clearly means something like overlays should be used.

A couple of issues at present:
1. I have not implemented "complex" hints (whose `label` is an array of
`InlayHintLabelPart` instead of a string) because I don't know what to do when
there are multiple labels at the same location.
rust-analyzer uses these hints to show a clickable link at the end of a function
block, which points to the beginning of the function.

2. I need to save the buffer or disable and re-enable `eglot-inlay-mode` to
get hints for the first time after opening a file, even though I call 
once in `eglot-inlay-mode`.

Chinmay Dalal
 lisp/progmodes/eglot.el | 70 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 69 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 6caf589..0d5e63e 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 
 (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)
+      (unless (not eglot-inlay-hints)
+        (eglot-inlay-mode))
 (add-hook 'find-file-hook 'eglot--maybe-activate-editing-mode)
@@ -3456,6 +3463,67 @@ If NOERROR, return predicate, else erroring function."
       (pop-to-buffer (current-buffer)))))
+(defface eglot-inlay-hint
+  '((t (:height 0.8 :inherit shadow)))
+  "Face used for inlay hint overlays.")
+(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))
+           (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 'eglot-inlay-hint))
+              (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

--- End Message ---
--- Begin Message --- Subject: Re: bug#61412: Inlay hints implementation Date: Sat, 25 Feb 2023 00:21:40 +0000
Inlay hints are not implemented in Eglot as of a few days ago.

I'm going to go ahead and close this bug. Please open new bugs for any
missing features
or problems with the new functionality.


--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]