]> git.eshelyaron.com Git - emacs.git/commitdiff
Eglot: be aware of LSP version of contextual diagnostics
authorJoão Távora <joaotavora@gmail.com>
Wed, 16 Apr 2025 23:31:07 +0000 (00:31 +0100)
committerEshel Yaron <me@eshelyaron.com>
Thu, 17 Apr 2025 07:17:13 +0000 (09:17 +0200)
In certain situations, Eglot has to report to the server parts
of the diagnostics that it itself sent us.  The server may use
this as context to compute code actions, for example.  Eglot
selects diagnostics by asking Flymake, looking for the ones that
contain Eglot-specific cookies.

But when doing so, it is important to also be aware of the LSP
document version these each of these diagnostics pertain to,
since if a diagonstic in the buffer pertains to an older version
of the LSP document (because Flymake fired or the server hasn't
pushed a new set), that diagnostics 'eglot-lsp-data' cookie is
invalid and possibly harmful.

An example is when a diagnostic extends all the way to the end
of the buffer.  If we attempt to fix by shortening the buffer,
an Eldoc-started code actions request may be sent to the server
considering the soon-to-be-deleted Flymake diagnostic as
context.  But that diagnostic's 'eglot-lsp-data' cookie is no
longer valid and when processing that context we try to go past
point-max and burp an annoying error.

Best to check the version of the diagnostic (if we have it) and
ignore the ones that don't match the document version.

* lisp/progmodes/eglot.el (eglot--versioned-identifier): Move up.
(eglot--flymake-diagnostics, eglot--diag-to-lsp-diag): New helpers.
(eglot-handle-notification): Record LSP doc version in diagnostics.
(eglot--code-action-bounds)
(eglot--code-action-params): Use eglot--flymake-diagnostics.

(cherry picked from commit c5b97b7b321c873dbe8a36d27781435ba10a2278)

lisp/progmodes/eglot.el

index 2c5535a207f8a34b50a9e9c58eb92f6f7ac28821..685b67d4c27b4faf1854bfa194b3aa83409bf44a 100644 (file)
@@ -1238,6 +1238,9 @@ If optional MARKERS, make markers instead."
 (cl-defmethod initialize-instance :before ((_server eglot-lsp-server) &optional args)
   (cl-remf args :initializationOptions))
 
+(defvar-local eglot--versioned-identifier 0
+  "LSP document version.  Bumped on `eglot--after-change'.")
+
 (defvar eglot--servers-by-project (make-hash-table :test #'equal)
   "Keys are projects.  Values are lists of processes.")
 
@@ -2527,6 +2530,19 @@ still unanswered LSP requests to the server\n"))))
 (defalias 'eglot--make-diag #'flymake-make-diagnostic)
 (defalias 'eglot--diag-data #'flymake-diagnostic-data)
 
+(defun eglot--flymake-diagnostics (beg &optional end)
+  "Like `flymake-diagnostics', but for Eglot-specific diagnostics."
+  (cl-loop for diag in (flymake-diagnostics beg end)
+           for data = (eglot--diag-data diag)
+           for lsp-diag = (alist-get 'eglot-lsp-diag data)
+           for version = (alist-get 'eglot--doc-version data)
+           when (and lsp-diag (or (null version)
+                                  (= version eglot--versioned-identifier)))
+           collect diag))
+
+(defun eglot--diag-to-lsp-diag (diag)
+  (alist-get 'eglot-lsp-diag (eglot--diag-data diag)))
+
 (defvar eglot-diagnostics-map
   (let ((map (make-sparse-keymap)))
     (define-key map [mouse-2] #'eglot-code-actions-at-mouse)
@@ -2694,8 +2710,6 @@ expensive cached value of `file-truename'.")
                 (1+ (plist-get (plist-get range :end) :line)))))))
     (cons beg end)))
 
-(defvar-local eglot--versioned-identifier 0)
-
 (cl-defmethod eglot-handle-notification
   (server (_method (eql textDocument/publishDiagnostics))
           &key uri diagnostics version
@@ -2734,7 +2748,8 @@ expensive cached value of `file-truename'.")
                        (eglot--make-diag
                         (current-buffer) beg end
                         (eglot--diag-type severity)
-                        message `((eglot-lsp-diag . ,diag-spec))
+                        message `((eglot-lsp-diag . ,diag-spec)
+                                  (eglot--doc-version . ,version))
                         (when-let ((faces
                                     (cl-loop for tag across tags
                                              when (alist-get tag eglot--tag-faces)
@@ -4037,7 +4052,7 @@ edit proposed by the server."
   "Calculate appropriate bounds depending on region and point."
   (let (diags boftap)
     (cond ((use-region-p) `(,(region-beginning) ,(region-end)))
-          ((setq diags (flymake-diagnostics (point)))
+          ((setq diags (eglot--flymake-diagnostics (point)))
            (cl-loop for d in diags
                     minimizing (flymake-diagnostic-beg d) into beg
                     maximizing (flymake-diagnostic-end d) into end
@@ -4054,10 +4069,8 @@ edit proposed by the server."
                      :end (eglot--pos-to-lsp-position end))
         :context
         `(:diagnostics
-          [,@(cl-loop for diag in (flymake-diagnostics beg end)
-                      when (cdr (assoc 'eglot-lsp-diag
-                                       (eglot--diag-data diag)))
-                      collect it)]
+          [,@(mapcar #'eglot--diag-to-lsp-diag
+                     (eglot--flymake-diagnostics beg end))]
           ,@(when only `(:only [,only]))
           ,@(when triggerKind `(:triggerKind ,triggerKind)))))