Repository: helix-editor/helix
File: helix-term/src/handlers/diagnostics.rs (Rust)

Code before:
}

impl helix_event::AsyncHook for PullDiagnosticsHandler {
    type Event = PullDiagnosticsEvent;

    fn handle_event(
        &mut self,
        event: Self::Event,
        _: Option<tokio::time::Instant>,
    ) -> Option<tokio::time::Instant> {
        match event {
            PullDiagnosticsEvent::Trigger => {}
        }
        Some(Instant::now() + Duration::from_millis(TIMEOUT))
    }

    fn finish_debounce(&mut self) {
        job::dispatch_blocking(move |editor, _| pull_diagnostic_for_current_doc(editor))
    }
}

fn pull_diagnostic_for_current_doc(editor: &mut Editor) {
    let (_, doc) = current!(editor);

    for language_server in doc.language_servers_with_feature(LanguageServerFeature::PullDiagnostics)
    {
        let future = language_server
            .text_document_diagnostic(doc.identifier(), doc.previous_diagnostic_id.clone());

        let original_path = doc
            .path()
            .expect("safety: the file has a path if there is a running language server")
            .to_owned();

        let Some(future) = future else {
            return;
        };

        let server_id = language_server.id();

        tokio::spawn(async move {
            match future.await {
                Ok(res) => {
                    job::dispatch(move |editor, _| {
                        let parsed_response: Option<lsp::DocumentDiagnosticReport> =
                            match serde_json::from_value(res) {
                                Ok(result) => Some(result),
                                Err(_) => None,
                            };

                        show_pull_diagnostics(editor, parsed_response, server_id, original_path)

Review comment (suggestion):
Could we check for a language server with a pull diagnostics feature in the hook first? That way we avoid dispatching a callback via `send_blocking` if the server doesn't support it (which will likely be the most common case)

Code after:
                send_blocking(&view.diagnostics_handler.events, DiagnosticEvent::Refresh)
            }
        }
        Ok(())
    });
    register_hook!(move |event: &mut OnModeSwitch<'_, '_>| {
        for (view, _) in event.cx.editor.tree.views_mut() {
            view.diagnostics_handler.active = event.new_mode != Mode::Insert;
        }
        Ok(())
    });

    let tx = handlers.pull_diagnostics.clone();
    let tx_all_documents = handlers.pull_all_documents_diagnostics.clone();
    register_hook!(move |event: &mut DocumentDidChange<'_>| {
        if event
            .doc
            .has_language_server_with_feature(LanguageServerFeature::PullDiagnostics)
            && !event.ghost_transaction
        {
            // Cancel the ongoing request, if present.
            event.doc.pull_diagnostic_controller.cancel();
            let document_id = event.doc.id();
            send_blocking(&tx, PullDiagnosticsEvent { document_id });

            let inter_file_dependencies_language_servers = event
                .doc
                .language_servers_with_feature(LanguageServerFeature::PullDiagnostics)
                .filter(|language_server| {
                    language_server
                        .capabilities()
                        .diagnostic_provider
                        .as_ref()
                        .is_some_and(|diagnostic_provider| match diagnostic_provider {
                            lsp::DiagnosticServerCapabilities::Options(options) => {
                                options.inter_file_dependencies
                            }

                            lsp::DiagnosticServerCapabilities::RegistrationOptions(options) => {
                                options.diagnostic_options.inter_file_dependencies
                            }
                        })
                })
                .map(|language_server| language_server.id())
                .collect();

            send_blocking(
                &tx_all_documents,
                PullAllDocumentsDiagnosticsEvent {
                    language_servers: inter_file_dependencies_language_servers,
                },