From 8b59087d32bfba5b1fb53bdb0b03fa460b8ba6a9 Mon Sep 17 00:00:00 2001
From: Sofus Addington <sofus@addington.dk>
Date: Mon, 3 Mar 2025 07:05:48 +0100
Subject: [PATCH] Skip additional dispatch and eliminate helper function

---
 helix-term/src/application.rs          |  1 -
 helix-term/src/handlers/diagnostics.rs | 85 ++++++++------------------
 helix-view/src/handlers/lsp.rs         |  5 --
 3 files changed, 27 insertions(+), 64 deletions(-)

diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs
index 2d41c34e..92ab8261 100644
--- a/helix-term/src/application.rs
+++ b/helix-term/src/application.rs
@@ -761,7 +761,6 @@ impl Application {
                             uri,
                             params.version,
                             params.diagnostics,
-                            None,
                         );
                     }
                     Notification::ShowMessage(params) => {
diff --git a/helix-term/src/handlers/diagnostics.rs b/helix-term/src/handlers/diagnostics.rs
index 9e28ef52..a93fc396 100644
--- a/helix-term/src/handlers/diagnostics.rs
+++ b/helix-term/src/handlers/diagnostics.rs
@@ -1,4 +1,4 @@
-use std::collections::{HashMap, HashSet};
+use std::collections::HashSet;
 use std::time::Duration;
 
 use helix_core::diagnostic::DiagnosticProvider;
@@ -46,23 +46,11 @@ pub(super) fn register_hooks(handlers: &Handlers) {
     });
 
     register_hook!(move |event: &mut DocumentDidOpen<'_>| {
-        if event
+        for language_server in event
             .doc
-            .has_language_server_with_feature(LanguageServerFeature::PullDiagnostics)
+            .language_servers_with_feature(LanguageServerFeature::PullDiagnostics)
         {
-            let document_id = event.doc.id();
-            job::dispatch_blocking(move |editor, _| {
-                let Some(doc) = editor.document(document_id) else {
-                    return;
-                };
-
-                let language_servers =
-                    doc.language_servers_with_feature(LanguageServerFeature::PullDiagnostics);
-
-                for language_server in language_servers {
-                    pull_diagnostics_for_document(doc, language_server);
-                }
-            })
+            pull_diagnostics_for_document(event.doc, language_server);
         }
 
         Ok(())
@@ -162,64 +150,45 @@ fn handle_pull_diagnostics_response(
     uri: Uri,
     document_id: DocumentId,
 ) {
-    let Some(doc) = editor.document_mut(document_id) else {
-        return;
-    };
-
-    match response {
+    let (result_id, related_documents) = match response {
         lsp::DocumentDiagnosticReport::Full(report) => {
-            // Diagnostic for requested file
             editor.handle_lsp_diagnostics(
                 provider,
                 uri,
                 None,
                 report.full_document_diagnostic_report.items,
+            );
+
+            (
                 report.full_document_diagnostic_report.result_id,
-            );
-
-            // Diagnostic for related files
-            handle_document_diagnostic_report_kind(
-                editor,
-                document_id,
                 report.related_documents,
-                provider,
-            );
+            )
         }
-        lsp::DocumentDiagnosticReport::Unchanged(report) => {
-            doc.previous_diagnostic_id =
-                Some(report.unchanged_document_diagnostic_report.result_id);
+        lsp::DocumentDiagnosticReport::Unchanged(report) => (
+            Some(report.unchanged_document_diagnostic_report.result_id),
+            report.related_documents,
+        ),
+    };
 
-            handle_document_diagnostic_report_kind(
-                editor,
-                document_id,
-                report.related_documents,
-                provider,
-            );
-        }
-    }
-}
+    if let Some(doc) = editor.document_mut(document_id) {
+        doc.previous_diagnostic_id = result_id;
+    };
 
-fn handle_document_diagnostic_report_kind(
-    editor: &mut Editor,
-    document_id: DocumentId,
-    report: Option<HashMap<lsp::Url, lsp::DocumentDiagnosticReportKind>>,
-    provider: DiagnosticProvider,
-) {
-    for (url, report) in report.into_iter().flatten() {
-        match report {
+    for (url, report) in related_documents.into_iter().flatten() {
+        let result_id = match report {
             lsp::DocumentDiagnosticReportKind::Full(report) => {
                 let Ok(uri) = Uri::try_from(url) else {
-                    return;
+                    continue;
                 };
 
-                editor.handle_lsp_diagnostics(provider, uri, None, report.items, report.result_id);
-            }
-            lsp::DocumentDiagnosticReportKind::Unchanged(report) => {
-                let Some(doc) = editor.document_mut(document_id) else {
-                    return;
-                };
-                doc.previous_diagnostic_id = Some(report.result_id);
+                editor.handle_lsp_diagnostics(provider, uri, None, report.items);
+                report.result_id
             }
+            lsp::DocumentDiagnosticReportKind::Unchanged(report) => Some(report.result_id),
+        };
+
+        if let Some(doc) = editor.document_mut(document_id) {
+            doc.previous_diagnostic_id = result_id;
         }
     }
 }
diff --git a/helix-view/src/handlers/lsp.rs b/helix-view/src/handlers/lsp.rs
index 989cd936..b9932727 100644
--- a/helix-view/src/handlers/lsp.rs
+++ b/helix-view/src/handlers/lsp.rs
@@ -285,7 +285,6 @@ impl Editor {
         uri: Uri,
         version: Option<i32>,
         mut diagnostics: Vec<lsp::Diagnostic>,
-        report_id: Option<String>,
     ) {
         let doc = self
             .documents
@@ -367,10 +366,6 @@ impl Editor {
                 Some(diagnostic_provider),
             );
 
-            if report_id.is_some() {
-                doc.previous_diagnostic_id = report_id;
-            }
-
             let doc = doc.id();
             helix_event::dispatch(DiagnosticsDidChange { editor: self, doc });
         }