]> git.eshelyaron.com Git - emacs.git/commit
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)
commite3ecb931e42a425cb2b75dfc1782897400662c6a
tree42101e883a123b98cd98e22eca44e2c8c5e4efe9
parentd107be2a31a9720caa9defab718e89fdf528f1e5
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.

(cherry picked from commit c5b97b7b321c873dbe8a36d27781435ba10a2278)
lisp/progmodes/eglot.el