From c5b97b7b321c873dbe8a36d27781435ba10a2278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Thu, 17 Apr 2025 00:31:07 +0100 Subject: [PATCH] Eglot: be aware of LSP version of contextual diagnostics 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. --- lisp/progmodes/eglot.el | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 8231c542d29..b077f4a6207 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -1240,6 +1240,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.") @@ -2556,6 +2559,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) @@ -2658,8 +2674,6 @@ Value is (TRUENAME . (:uri STR)), where STR is what is sent to the server on textDocument/didOpen and similar calls. TRUENAME is the expensive cached value of `file-truename'.") -(defvar-local eglot--versioned-identifier 0) - (cl-defmethod eglot-handle-notification (server (_method (eql textDocument/publishDiagnostics)) &key uri diagnostics version @@ -2715,7 +2729,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) @@ -3983,7 +3998,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 @@ -4000,10 +4015,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)))))