Subject: bug#59149: Feature Request: Report progress of long requests in Eglot

From: Danny Freeman <[email-masked]>
Date: Wed, 23 Nov 2022 09:12:12 -0500

Stephen Leake <[email-masked]> writes:

> Note that this is checking the _server_ capabilities; since LSP does not
> define "projess" as a capability, no server will ever advertise that
> it is supported.
>
> So we can't use eglot-ignores-server-capabilities; we could maybe
> introduce eglot-ignored-client-capabilities.
>
> But we already have a mechanism for that; eglot-stay-out-of.
>
> So a user can do:
>
> (add-to-list 'eglot-stay-out-of 'progress)
>
> And in eglot-handle-notification ($/progress) we check for that:
>
> (unless (eglot--stay-out-of-p 'progress)

Yeah, I think this will be the way to go as well.

> There's probably a way to fold that check into the cl-defmethod
> dispatching parameters; I did not look into that.

If there is I do not know how to do it either. I think a boolean check
in the body of the function is fine (and probably more clear).

> I just ran into the progress reporter in eglot--apply-text-edits; I'd
> like to turn that off, but leave other progress-reporters on.
> 
> So we need something more fine-grained:
> 
> (setq eglot-progress-reporter-disable '(apply-text-edits))

I believe that is a different progress reporter, unrelated to the one I
would like to introduce. It is not a progress report that come from the
lsp server, so I don't think it would be good to conflate them.

If the user wants to ignore specific progress indicators from the lsp
servers then they could implement an empty version of
eglot-handle-notification that matches their progress token.

```
(cl-defmethod eglot-handle-notification
  (_server (_method (eql $/progress)) &key (token (eql "THE PROGRESS TOKEN")) _value))
```

That would be more targeted. I also suspect it would be a pretty rare
occurrence. I personally think the best way to decide on adding more
configuration variables to eglot is to release this into the wild and
see what kind of feedback we receive.

Responding to João from a couple of threads ago:

> Thanks Danny, this doesn't look bad at all, though I have yet to
> understand how it works.  I can half-see why progress status should be a
> server property, but it would be better if you provided an automated
> test, or maybe just a step-by-step sequence diagram (which can be in
> plain text and language) that clarifies this.  An animated gif that
> might also work.

I think I can explain it with a sample of my server logs:

The first thing that happens is the lsp server sends a progress BEGIN
message, which indicates to the client that it is starting some long
running process.

```
[server-notification] Wed Nov 23 08:55:22 2022:
(:jsonrpc "2.0" :method "$/progress" :params
          (:token "clojure-lsp" :value
                  (:kind "begin" :title "clojure-lsp" :percentage 0)))
```

Following that is a series of REPORT type progress messages. These
indicate to the client that the work is ongoing. They might have a
`percentage` key somewhere between 0 and 100 to show how the work is
progressing.

```
[server-notification] Wed Nov 23 08:55:22 2022:
(:jsonrpc "2.0" :method "$/progress" :params
          (:token "clojure-lsp" :value
                  (:kind "report" :title "Finding kondo config" :percentage 5)))
[server-notification] Wed Nov 23 08:55:22 2022:
(:jsonrpc "2.0" :method "$/progress" :params
          (:token "clojure-lsp" :value
                  (:kind "report" :title "Finding cache" :percentage 10)))
[server-notification] Wed Nov 23 08:55:22 2022:
(:jsonrpc "2.0" :method "$/progress" :params
          (:token "clojure-lsp" :value
                  (:kind "report" :title "Copying kondo configs" :percentage 15)))
[server-notification] Wed Nov 23 08:55:22 2022:
(:jsonrpc "2.0" :method "$/progress" :params
          (:token "clojure-lsp" :value
                  (:kind "report" :title "Resolving config paths" :percentage 15)))
[server-notification] Wed Nov 23 08:55:22 2022:
(:jsonrpc "2.0" :method "$/progress" :params
          (:token "clojure-lsp" :value
                  (:kind "report" :title "Analyzing project files" :percentage 20)))

... this goes on for a while

[server-notification] Wed Nov 23 08:55:22 2022:
(:jsonrpc "2.0" :method "$/progress" :params
          (:token "clojure-lsp" :value
                  (:kind "report" :title "Analyzing project files" :percentage 99)))
```

Finally, the server sends an END type progress message, indicating that
the work is complete, and the client can stop showing the progress
message.

```
[server-notification] Wed Nov 23 08:55:22 2022:
(:jsonrpc "2.0" :method "$/progress" :params
          (:token "clojure-lsp" :value
                  (:kind "end" :title "Project analyzed" :percentage 100)))
```

The result of this patch is a series of messages that display in the
minibuffer:

```
[eglot:website] clojure-lsp:  
[eglot:website] clojure-lsp: 5% Finding kondo config
[eglot:website] clojure-lsp: 10% Finding cache
[eglot:website] clojure-lsp: 15% Copying kondo configs
[eglot:website] clojure-lsp: 20% Analyzing project files
[eglot:website] clojure-lsp: 26% Analyzing project files
[eglot:website] clojure-lsp: 33% Analyzing project files
[eglot:website] clojure-lsp: 39% Analyzing project files
[eglot:website] clojure-lsp: 46% Analyzing project files
[eglot:website] clojure-lsp: 52% Analyzing project files
[eglot:website] clojure-lsp: 59% Analyzing project files
[eglot:website] clojure-lsp: 66% Analyzing project files
[eglot:website] clojure-lsp: 72% Analyzing project files
[eglot:website] clojure-lsp: 79% Analyzing project files
[eglot:website] clojure-lsp: 85% Analyzing project files
[eglot:website] clojure-lsp: 92% Analyzing project files
[eglot:website] clojure-lsp: 99% Analyzing project files
[eglot:website] clojure-lsp: done
```

Does that help?

Attached is an updated patch that uses `eglot-stay-out-of`

>From 54bc6b70c8a6da8853db1bcc45ca51796b98b6c2 Mon Sep 17 00:00:00 2001
From: dannyfreeman <[email-masked]>
Date: Wed, 9 Nov 2022 08:46:45 -0500
Subject: [PATCH] Eglot: Display progress notifications in minibuffer as they
 arrive

The LSP spec describes methods for reporting progress on long running
jobs to the client:

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#progress
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workDoneProgress

This change reports those notifications in the minibuffer as they come
in. It shows a percent indicator (if the server provides theme), or a
spinner. This change should open the door for writing a "cancel long
running request" command, which are identified by these progress
notifications. See
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_workDoneProgress_cancel
---
 lisp/progmodes/eglot.el | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index e057b12e0e..3dc71b148c 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -821,6 +821,9 @@ eglot-lsp-server
    (project
     :documentation "Project associated with server."
     :accessor eglot--project)
+   (progress-reporter-alist
+    :documentation "Alist of (PROGRESS-TOKEN . PROGRESS-REPORTER)."
+    :accessor eglot--progress-reporter-alist)
    (inhibit-autoreconnect
     :initform t
     :documentation "Generalized boolean inhibiting auto-reconnection if true."
@@ -2037,6 +2040,43 @@ eglot-handle-notification
   (_server (_method (eql telemetry/event)) &rest _any)
   "Handle notification telemetry/event.") ;; noop, use events buffer
 
+(defun eglot--progress-report-message (title message)
+  "Format a $/progress report message, given a TITLE and/or MESSAGE string."
+  (cond
+   ((and title message) (format "%s %s" title message))
+   (title title)
+   (message message)))
+
+(defun eglot--progress-reporter (server token)
+  "Get a prgress-reporter identified by the progress TOKEN from the SERVER ."
+  (cdr (assoc token (eglot--progress-reporter-alist server))))
+
+(defun eglot--progress-reporter-delete (server token)
+  "Delete progress-reporters identified by the progress TOKEN from the SERVER."
+  (setf (eglot--progress-reporter-alist server)
+        (assoc-delete-all token (eglot--progress-reporter-alist server))))
+
+(cl-defmethod eglot-handle-notification
+  (server (_method (eql $/progress)) &key token value)
+  "Handle a $/progress notification identified by TOKEN from the SERVER."
+  (unless (eglot--stay-out-of-p 'progress)
+    (cl-destructuring-bind (&key kind title percentage message) value
+      (pcase kind
+        ("begin" (let* ((prefix (format (concat "[eglot] %s %s:" (when percentage " "))
+                                        (eglot-project-nickname server) token))
+                        (pr (if percentage
+                                (make-progress-reporter prefix 0 100 percentage 1 0)
+                              (make-progress-reporter prefix nil nil nil 1 0))))
+                   (eglot--progress-reporter-delete server token)
+                   (setf (eglot--progress-reporter-alist server)
+                         (cons (cons token pr) (eglot--progress-reporter-alist server)))
+                   (progress-reporter-update pr percentage (eglot--progress-report-message title message))))
+        ("report" (when-let ((pr (eglot--progress-reporter server token)))
+                    (progress-reporter-update pr percentage (eglot--progress-report-message title message))))
+        ("end" (when-let ((pr (eglot--progress-reporter server token)))
+                 (progress-reporter-done pr)
+                 (eglot--progress-reporter-delete server token)))))))
+
 (cl-defmethod eglot-handle-notification
   (_server (_method (eql textDocument/publishDiagnostics)) &key uri diagnostics
            &allow-other-keys) ; FIXME: doesn't respect `eglot-strict-mode'
-- 
2.38.1

-- 
Danny Freeman