branch: elpa/flycheck
commit 433196eb0d1b9be6bd3348e6b2d07cbc740ffad9
Author: Bozhidar Batsov <[email protected]>
Commit: Bozhidar Batsov <[email protected]>

    [#2387] Keep the eldoc window shut for every display of a request
    
    Flycheck asks Eldoc to document interactively, so that the echo area is
    refreshed after commands Eldoc doesn't know, and drops that request on
    its way to eldoc-display-in-buffer, which would otherwise open the
    *eldoc* window.  It did that for the first display only.  But the eager
    strategies, eldoc-documentation-compose-eagerly and
    eldoc-documentation-enthusiast, display once per source as each answers,
    and with a language server the second display opened the window.
    
    The flag now says whether the last interactive request was Flycheck's,
    set by advice on eldoc-print-current-symbol-info, so it covers every
    display of that request and ends when M-x eldoc or similar asks for
    documentation.  It's global, like the window, as Eldoc before Emacs 31
    displays in whatever buffer is current when a source answers.
    
    M-x eldoc no longer has Flycheck schedule a refresh of its own: one
    landing while a slow source holds up the user's request would keep the
    window shut that the user asked for.
---
 CHANGELOG.md                     |  1 +
 flycheck.el                      | 87 ++++++++++++++++++++++++++--------------
 test/specs/test-error-display.el | 66 +++++++++++++++++++++---------
 3 files changed, 105 insertions(+), 49 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 747b96a813..826545f341 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -36,6 +36,7 @@
 
 ### Bugs fixed
 
+- [#2406](https://github.com/flycheck/flycheck/pull/2406): Stop the `*eldoc*` 
window popping open with an eager `eldoc-documentation-strategy`, such as 
`eldoc-documentation-compose-eagerly`, which displays again each time a source 
answers ([#2387](https://github.com/flycheck/flycheck/issues/2387)).
 - [#2302](https://github.com/flycheck/flycheck/pull/2302): `proselint` errors 
no longer extend one character past the text they flag with proselint 0.14.
 - [#2302](https://github.com/flycheck/flycheck/pull/2302): `proselint` detects 
the tool's version from what `proselint --version` prints rather than whether 
it succeeds, so a development build of proselint, which accepts the option 
again, is no longer run with the arguments of 0.14.
 - [#2396](https://github.com/flycheck/flycheck/pull/2396): A language server 
running on a remote host is stopped there when Flycheck gives up on it, so a 
server that never answers the `initialize` handshake no longer leaves a copy of 
itself behind on every attempt.
diff --git a/flycheck.el b/flycheck.el
index 19cb23f9eb..2afcdb3338 100644
--- a/flycheck.el
+++ b/flycheck.el
@@ -1610,6 +1610,9 @@ just return nil."
   (when (lookup-key global-map [menu-bar tools])
     (easy-menu-remove-item nil '("Tools") (cadr flycheck-mode-menu-map)))
   (remove-hook 'kill-emacs-hook #'flycheck-global-teardown)
+  (advice-remove 'eldoc-display-in-buffer 
#'flycheck--eldoc-suppress-doc-window)
+  (advice-remove 'eldoc-print-current-symbol-info
+                 #'flycheck--eldoc-note-request)
   ;; Or the next set of the watched variable calls an unbound function,
   ;; from inside Eglot's process filter of all places
   (remove-variable-watcher 'flymake-list-only-diagnostics
@@ -3480,12 +3483,14 @@ ARG is ‘toggle’; disable the mode otherwise."
     (flycheck-clear)
     (flycheck--sync-margin)
     (add-hook 'eldoc-documentation-functions #'flycheck-eldoc-function nil t)
-    ;; Guarded by a buffer-local flag that only Flycheck sets, so this is
-    ;; inert everywhere else.  It stays for the session: Eldoc displays for
-    ;; whichever buffer answered last, so removing it when one buffer turns
+    ;; Neither advice changes anything until Flycheck first asks Eldoc to
+    ;; document.  They stay for the session: Eldoc displays for whichever
+    ;; buffer answered last, so removing them when one buffer turns
     ;; Flycheck off would break the buffers that still have it on.
     (advice-add 'eldoc-display-in-buffer
                 :around #'flycheck--eldoc-suppress-doc-window)
+    (advice-add 'eldoc-print-current-symbol-info
+                :before #'flycheck--eldoc-note-request)
     ;; `global-eldoc-mode' may have skipped this buffer because no
     ;; documentation source was registered when it made its decision;
     ;; give it another chance now that Flycheck provides one.  Buffers
@@ -8700,8 +8705,8 @@ avoid slowing down editing when the error list is hidden."
   "Whether errors at point are displayed through Eldoc."
   (eq flycheck-display-errors-function #'flycheck-display-errors-via-eldoc))
 
-(defvar-local flycheck--eldoc-refresh-pending nil
-  "Non-nil while a Flycheck-initiated Eldoc refresh has yet to be displayed.
+(defvar flycheck--eldoc-own-request nil
+  "Non-nil while the last interactive Eldoc request was Flycheck's.
 
 Flycheck documents interactively so that the echo area is refreshed even
 after a command Eldoc does not recognise, but that request should not
@@ -8709,24 +8714,39 @@ reach `eldoc-display-in-buffer', which reads it as 
\\[eldoc-doc-buffer]
 and pops the documentation window open.  Jumping to an error should not
 rearrange the frame.
 
-This cannot be a `let' around the request.  Eldoc gathers documentation
-from every registered source and displays it once they have all
-answered, so a single asynchronous source, such as a language server, is
-enough to push the display past the end of any dynamic binding.  A flag
-that lives until the display happens survives that.")
+Eldoc gathers documentation from every registered source and displays
+it once they have all answered, so a single asynchronous source, such as
+a language server, is enough to push the display past the end of the
+request, and of the binding of `flycheck--eldoc-requesting' that marks
+it.  Nor can the first display end it: an eager
+`eldoc-documentation-strategy', such as
+`eldoc-documentation-compose-eagerly', displays again each time a source
+answers.  So the flag lasts until something else asks Eldoc to document
+interactively, like \\[eldoc], which may then open the window.
+
+It is global, like the window: before Emacs 31, Eldoc displays in
+whichever buffer is current when a source answers.")
+
+(defvar flycheck--eldoc-requesting nil
+  "Non-nil while Flycheck itself asks Eldoc to document.")
+
+(defun flycheck--eldoc-note-request (&optional interactive)
+  "Note whether an INTERACTIVE Eldoc request is Flycheck's own.
+
+Runs before `eldoc-print-current-symbol-info'.  Eldoc's idle timer
+documents non-interactively, which never opens the window, so only an
+interactive request changes anything."
+  (when interactive
+    (setq flycheck--eldoc-own-request flycheck--eldoc-requesting)))
 
 (defun flycheck--eldoc-suppress-doc-window (display docs interactive)
   "Keep a Flycheck-initiated refresh from popping the Eldoc window open.
 
 DISPLAY is `eldoc-display-in-buffer', called with DOCS and INTERACTIVE.
 The documentation buffer is still brought up to date; only the request to
-put it on screen is dropped, and only for the refresh Flycheck asked
-for."
-  (if flycheck--eldoc-refresh-pending
-      (progn
-        (setq flycheck--eldoc-refresh-pending nil)
-        (funcall display docs nil))
-    (funcall display docs interactive)))
+put it on screen is dropped, and only while Flycheck made the last
+interactive request."
+  (funcall display docs (and interactive (not flycheck--eldoc-own-request))))
 
 (defun flycheck-display-errors-via-eldoc (_errors)
   "Trigger Eldoc to document the errors at point.
@@ -8734,19 +8754,19 @@ for."
 Eldoc computes its documentation from all of its registered
 sources, including `flycheck-eldoc-function', so refreshing it
 shows the Flycheck errors at point alongside e.g. Eglot's
-documentation.  This works from any display entry point --
-interactive commands, error navigation, automatic display after a
-check -- whether or not variable `eldoc-mode' is enabled.
+documentation.  This works from any display entry point (interactive
+commands, error navigation, automatic display after a check), whether
+or not variable `eldoc-mode' is enabled.
 
 Eldoc is asked to document interactively, because reaching this
 function already means Flycheck decided to show the errors at
 point.  Left to its own devices Eldoc keeps out of the echo area
 unless the command that ran is one of `eldoc-message-commands',
 which error navigation is not, and the errors would go unseen.
-`flycheck--eldoc-refresh-pending' keeps that request from reaching the
+`flycheck--eldoc-own-request' keeps that request from reaching the
 documentation window."
-  (setq flycheck--eldoc-refresh-pending t)
-  (eldoc-print-current-symbol-info t))
+  (let ((flycheck--eldoc-requesting t))
+    (eldoc-print-current-symbol-info t)))
 
 (defun flycheck-eldoc-function (callback &rest _ignored)
   "Document the Flycheck errors at point by calling CALLBACK.
@@ -8811,15 +8831,20 @@ If there are no errors, clears the error messages at 
point."
   "Whether Eldoc itself will refresh the echo area for this command.
 
 Eldoc only writes to the echo area after a command registered in
-`eldoc-message-commands' -- ordinary motion and editing.  After any
-other command, such as a jump from `consult-flycheck', it computes its
-documentation but leaves the echo area alone, so Flycheck has to ask
-for the display itself."
+`eldoc-message-commands', ordinary motion and editing, or one that asks
+it to document, like \\[eldoc].  After any other command, such as a jump
+from `consult-flycheck', it computes its documentation but leaves the
+echo area alone, so Flycheck has to ask for the display itself."
   (and (flycheck--display-errors-via-eldoc-p)
-       (bound-and-true-p eldoc-mode)
-       (symbolp this-command)
-       this-command
-       (intern-soft (symbol-name this-command) eldoc-message-commands)
+       (or
+        ;; That request includes Flycheck's errors already, and a second
+        ;; one from Flycheck, while the first waits for a slow source,
+        ;; would keep its window shut
+        (memq this-command '(eldoc eldoc-print-current-symbol-info))
+        (and (bound-and-true-p eldoc-mode)
+             (symbolp this-command)
+             this-command
+             (intern-soft (symbol-name this-command) eldoc-message-commands)))
        t))
 
 (defun flycheck-display-error-at-point-soon ()
diff --git a/test/specs/test-error-display.el b/test/specs/test-error-display.el
index 88c69952ee..c3af608dab 100644
--- a/test/specs/test-error-display.el
+++ b/test/specs/test-error-display.el
@@ -90,33 +90,58 @@
       ;; jump, but `eldoc-display-in-buffer' reads the same flag as a
       ;; request to display the *eldoc* window.  See #2201.
       (flycheck-buttercup-with-temp-buffer
-        (let ((asked 'unset))
-          (cl-letf (((symbol-function 'eldoc-print-current-symbol-info) 
#'ignore))
-            (flycheck-display-errors-via-eldoc nil))
-          ;; Eldoc may only get round to displaying long after the request,
-          ;; so the suppression has to outlive it rather than ride on a
-          ;; dynamic binding
-          (flycheck--eldoc-suppress-doc-window
-           (lambda (_docs interactive) (setq asked interactive)) nil t)
-          (expect asked :to-be nil))))
+        (flycheck-mode 1)
+        ;; An eager strategy displays once per source as it answers, and
+        ;; the source answering late does so after the request returned.
+        ;; Each display is as interactive as the request was (#2387).
+        (setq-local eldoc-documentation-strategy
+                    #'eldoc-documentation-compose-eagerly)
+        (setq-local eldoc-documentation-functions
+                    (list (lambda (callback) (funcall callback "now") t)
+                          (lambda (callback)
+                            (run-at-time 0.01 nil callback "later")
+                            t)))
+        (let ((displays 0))
+          (setq-local eldoc-display-functions
+                      (list #'eldoc-display-in-buffer
+                            (lambda (&rest _) (cl-incf displays))))
+          (spy-on 'eldoc-doc-buffer)
+          ;; Wait for every display a request starts: a late one left
+          ;; behind would land in some later spec
+          (cl-flet ((await (n)
+                      (let ((deadline (+ (float-time) 5)))
+                        (while (and (< displays n) (< (float-time) deadline))
+                          (accept-process-output nil 0.02)))))
+            (flycheck-display-errors-via-eldoc nil)
+            (await 2)
+            (expect displays :to-equal 2)
+            (expect 'eldoc-doc-buffer :not :to-have-been-called)
+            ;; An explicit `M-x eldoc' still opens the window
+            (call-interactively #'eldoc)
+            (await 4)
+            (expect 'eldoc-doc-buffer :to-have-been-called-times 2)))))
 
     (it "only suppresses the window for the refresh it asked for"
       (flycheck-buttercup-with-temp-buffer
-        (let ((asked 'unset))
+        (let ((asked 'unset)
+              (flycheck--eldoc-own-request nil))
           ;; nothing pending: an explicit `M-x eldoc' must still display
           (flycheck--eldoc-suppress-doc-window
            (lambda (_docs interactive) (setq asked interactive)) nil t)
           (expect asked :to-be t))))
 
-    (it "suppresses the window once, not for every later display"
+    (it "keeps the window shut across Eldoc's own idle requests"
+      ;; They document non-interactively, so they never open the window,
+      ;; and a late answer to Flycheck's request may still follow them
       (flycheck-buttercup-with-temp-buffer
-        (let ((asked nil))
-          (cl-letf (((symbol-function 'eldoc-print-current-symbol-info) 
#'ignore))
-            (flycheck-display-errors-via-eldoc nil))
-          (dotimes (_ 2)
-            (flycheck--eldoc-suppress-doc-window
-             (lambda (_docs interactive) (push interactive asked)) nil t))
-          (expect (nreverse asked) :to-equal '(nil t)))))
+        (let ((asked 'unset)
+              (flycheck--eldoc-own-request nil))
+          (let ((flycheck--eldoc-requesting t))
+            (flycheck--eldoc-note-request t))
+          (flycheck--eldoc-note-request nil)
+          (flycheck--eldoc-suppress-doc-window
+           (lambda (_docs interactive) (setq asked interactive)) nil t)
+          (expect asked :to-be nil))))
 
     (it "asks eldoc to document interactively"
       ;; Left to itself Eldoc keeps out of the echo area unless the
@@ -144,6 +169,11 @@
           (expect flycheck-display-error-at-point-timer :to-be-truthy)
           (flycheck-cancel-error-display-error-at-point-timer))
         (eldoc-mode -1)
+        ;; `M-x eldoc' documents with or without it, and a refresh from
+        ;; the timer could keep the window it asked for shut (#2387)
+        (let ((this-command 'eldoc))
+          (flycheck-display-error-at-point-soon)
+          (expect flycheck-display-error-at-point-timer :not :to-be-truthy))
         ;; Without eldoc-mode the timer must pick up the slack
         (flycheck-display-error-at-point-soon)
         (expect flycheck-display-error-at-point-timer :to-be-truthy)

Reply via email to