emacs-devel
[Top][All Lists]
Advanced

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

Re: Eglot "inlay hints" landed


From: João Távora
Subject: Re: Eglot "inlay hints" landed
Date: Thu, 23 Feb 2023 19:26:10 +0000

On Thu, Feb 23, 2023 at 6:01 PM Eli Zaretskii <eliz@gnu.org> wrote:
>
> > From: João Távora <joaotavora@gmail.com>
> > Date: Thu, 23 Feb 2023 17:46:08 +0000
> > Cc: dalal.chinmay.0101@gmail.com, emacs-devel@gnu.org, 
> > dimitri@belopopsky.com,
> >       luangruo@yahoo.com
> >
> > > That's no for jit-lock to do.  And I don't see how it could be
> > > relevant to the issue we are discussing.  How do you do this now?
> >
> > I don't.  I was just pointing out that jit-lock by itself doesn't
> > solve this A -> B dependency, which you seemed to suggest it does when
> > you wrote:
> >
> > > > If there's a change in A that affects B, jit-lock will call
> > > > fontification-functions in both A and B, each one when it's about to
> > > > display the corresponding window.
>
> That was written under the assumption that the overlays in B are
> already updated.  Then redisplay will know it must redraw B.
>
> > > window-scroll-functions cannot promise that, since
> > > they are only called "when the window is scrolled", and there's more
> > > to that condition than meets the eye, believe me.
> >
> > I believe you.  But as far as I can tell so far, it's the least
> > imperfect of the methods, and I haven't seen demonstrations of
> > problems so far, only your speculation of hypothetical problems.
>
> I actually gave you a recipe for demonstrating the problems I have in
> mind: scroll the window under pixel-scroll-precision-mode.  AFAIK, we
> don't call window-scroll-functions in that case.

I don't have a pixel-scroll-enabled Emacs to test.  Can you scroll
large portions of the window like that and w-s-functions will never
get called?  I'd say that's a bug we should fix.

> Another situation where we don't call window-scroll-functions is when
> the user types into the buffer.

That is handled by Eglot's after-change-functions already so it
isn't a problem.

> Yet another situation is when you type "C-x 1" to delete all the other
> windows on the frame, leaving the current window that now shows more
> stuff than before.

Reproduced.  Easy enough to fix with window-configuration-change-functions.
I pushed a fix.

> > I'd love to switch over to the jit-lock implementation as it has
> > potential to be much neater.  But I can't seem to get it to not
> > over-request stuff. I attach the patch I've been trying, and it's
> > clearly got some thinkos when you test it.  It doesn't help that
> > `window-start` and `window-end` aren't -- apparently -- reliable
> > when called from a jit-lock function.
>
> Sorry, I don't have time for that ATM, but maybe Stefan will want to
> comment.

I'll keep trying. I think what I'd want is for the "contextual" call
with the big chunk about to be jit-refontified remember the smaller
adjacent chunks and make a request for the big chunk + the smaller
chunks.  Here's the latest version of the patch using a timer to
achieve that.  It's pretty gross, but I hope it expresses the idea.

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index eea8be6d1aa..251f6f11090 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -3507,14 +3507,47 @@ eglot-lazy-inlay-hints
 (defun eglot--inlay-hints-fully ()
   (eglot--widening (eglot--update-hints-1 (point-min) (point-max))))

-(cl-defun eglot--inlay-hints-lazily (&optional (buffer (current-buffer)))
-  (eglot--when-live-buffer buffer
-    (when eglot--managed-mode
-      (dolist (window (get-buffer-window-list nil nil 'visible))
-        (eglot--update-hints-1 (window-start window) (window-end window))))))
+(defvar-local eglot--inlay-hints-outstanding nil
+  "List of regions")
+
+(defvar-local eglot--inlay-hints-outstanding-timer nil
+  "Timer")
+
+(defun eglot--update-hints (from to)
+  "Jit-lock function for Eglot inlay hints."
+  (if eglot--inlay-hints-outstanding-timer
+      (cancel-timer eglot--inlay-hints-outstanding-timer))
+  (push (cons from to) eglot--inlay-hints-outstanding)
+  (setq eglot--inlay-hints-outstanding-timer
+        (run-with-timer
+         ;; FIXME: Gross
+         (+ 0.1 (max eglot-lazy-inlay-hints jit-lock-context-time))
+         nil (lambda ()
+               (trace-values eglot--inlay-hints-outstanding)
+               (cl-loop with region< = (lambda (r1 r2)
+                                         (if (= (car r1) (car r2))
+                                             (< (cdr r1) (cdr r2))
+                                           (< (car r1) (car r2))))
+                        with sorted = (cl-sort
eglot--inlay-hints-outstanding region<)
+                        with coalesced = (cl-reduce (lambda (acc x)
+                                                      (cond ((and acc
(>= (cdr (car acc))
+
    (car x)))
+                                                             (setcdr
(car acc) (cdr x))
+                                                             acc)
+                                                            (t
+                                                             (push x acc)
+                                                             acc)))
+                                                    sorted
+                                                    :initial-value nil)
+                        for r in coalesced
+                        do (eglot--update-hints-1 (max (car r) (point-min))
+                                                  (min (cdr r) (point-max)))
+                        finally
+                        (setq eglot--inlay-hints-outstanding nil
+                              eglot--inlay-hints-outstanding-timer nil))))))

 (defun eglot--update-hints-1 (from to)
-  "Request LSP inlay hints and annotate current buffer from FROM to TO."
+  "Do actual work for `eglot--update-hints', including LSP request."
   (let* ((buf (current-buffer))
          (paint-hint
           (eglot--lambda ((InlayHint) position kind label paddingLeft
paddingRight)
@@ -3545,36 +3578,6 @@ eglot--update-hints-1
                       (mapc paint-hint hints))))
      :deferred 'eglot--update-hints-1)))

-(defun eglot--inlay-hints-after-scroll (window display-start)
-  (cl-macrolet ((wsetq (sym val) `(set-window-parameter window ',sym ,val))
-                (wgetq (sym) `(window-parameter window ',sym)))
-    (let ((buf (window-buffer window))
-          (timer (wgetq eglot--inlay-hints-timer))
-          (last-display-start (wgetq eglot--last-inlay-hint-display-start)))
-      (when (and eglot-lazy-inlay-hints
-                 ;; FIXME: If `window' is _not_ the selected window,
-                 ;; then for some unknown reason probably related to
-                 ;; the overlays added later to the buffer, the scroll
-                 ;; function will be called indefinitely.  Not sure if
-                 ;; an Emacs bug, but prevent useless duplicate calls
-                 ;; by saving and examining `display-start' fixes it.
-                 (not (eql last-display-start display-start)))
-        (when timer (cancel-timer timer))
-        (wsetq eglot--last-inlay-hint-display-start
-               display-start)
-        (wsetq eglot--inlay-hints-timer
-               (run-at-time
-                eglot-lazy-inlay-hints
-                nil (lambda ()
-                      (eglot--when-live-buffer buf
-                        (when (eq buf (window-buffer window))
-                          (eglot--update-hints-1 (window-start window)
-                                                 (window-end window))
-                          (wsetq eglot--inlay-hints-timer nil))))))))))
-
-(defun eglot--inlay-hints-after-window-config-change ()
-  (eglot--update-hints-1 (window-start) (window-end)))
-
 (define-minor-mode eglot-inlay-hints-mode
   "Minor mode for annotating buffers with LSP server's inlay hints."
   :global nil
@@ -3584,28 +3587,15 @@ eglot-inlay-hints-mode
            (eglot--warn
             "No :inlayHintProvider support. Inlay hints will not work."))
           (eglot-lazy-inlay-hints
-           (add-hook 'eglot--document-changed-hook
-                     #'eglot--inlay-hints-lazily t t)
-           (add-hook 'window-scroll-functions
-                     #'eglot--inlay-hints-after-scroll nil t)
-           (add-hook 'window-configuration-change-hook
-                     #'eglot--inlay-hints-after-window-config-change nil t)
-           ;; Maybe there isn't a window yet for current buffer,
-           ;; so `run-at-time' ensures this runs after redisplay.
-           (run-at-time 0 nil #'eglot--inlay-hints-lazily))
+           (jit-lock-register #'eglot--update-hints))
           (t
            (add-hook 'eglot--document-changed-hook
                      #'eglot--inlay-hints-fully nil t)
            (eglot--inlay-hints-fully))))
         (t
-         (remove-hook 'window-configuration-change-hook
-                      #'eglot--inlay-hints-after-window-config-change)
-         (remove-hook 'eglot--document-changed-hook
-                      #'eglot--inlay-hints-lazily t)
+         (jit-lock-unregister #'eglot--update-hints)
          (remove-hook 'eglot--document-changed-hook
                       #'eglot--inlay-hints-fully t)
-         (remove-hook 'window-scroll-functions
-                      #'eglot--inlay-hints-after-scroll t)
          (remove-overlays nil nil 'eglot--inlay-hint t))))



reply via email to

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