From 3c9d5d02153ca2d81e2780e1f6daeefc40de2706 Mon Sep 17 00:00:00 2001
From: Pascal Kuthe <pascal.kuthe@semimod.de>
Date: Sun, 12 Mar 2023 01:00:24 +0100
Subject: [PATCH] discard outdated diagnostics recived by the LS

---
 helix-lsp/src/client.rs       |  1 +
 helix-term/src/application.rs | 11 ++++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs
index c46bdd8c..f6bf5e39 100644
--- a/helix-lsp/src/client.rs
+++ b/helix-lsp/src/client.rs
@@ -387,6 +387,7 @@ impl Client {
                         ..Default::default()
                     }),
                     publish_diagnostics: Some(lsp::PublishDiagnosticsClientCapabilities {
+                        version_support: Some(true),
                         ..Default::default()
                     }),
                     inlay_hint: Some(lsp::InlayHintClientCapabilities {
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs
index 14636829..8803792a 100644
--- a/helix-term/src/application.rs
+++ b/helix-term/src/application.rs
@@ -709,7 +709,16 @@ impl Application {
                                 return;
                             }
                         };
-                        let doc = self.editor.document_by_path_mut(&path);
+                        let doc = self.editor.document_by_path_mut(&path).filter(|doc| {
+                            if let Some(version) = params.version {
+                                if version != doc.version() {
+                                    log::info!("Version ({version}) is out of date for {path:?} (expected ({}), dropping PublishDiagnostic notification", doc.version());
+                                    return false;
+                                }
+                            }
+
+                            true
+                        });
 
                         if let Some(doc) = doc {
                             let lang_conf = doc.language_config();