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)