Add separate event for pulling visible documents diagnostics

pull/11315/head
Sofus Addington 2025-05-08 08:27:33 +02:00
parent ec10b7e741
commit 43cbbcd586
No known key found for this signature in database
GPG Key ID: 57579341E1199840
4 changed files with 78 additions and 16 deletions

View File

@ -1,6 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use arc_swap::ArcSwap; use arc_swap::ArcSwap;
use diagnostics::PullAllDocumentsDiagnosticHandler;
use helix_event::AsyncHook; use helix_event::AsyncHook;
use crate::config::Config; use crate::config::Config;
@ -28,6 +29,7 @@ pub fn setup(config: Arc<ArcSwap<Config>>) -> Handlers {
let auto_save = AutoSaveHandler::new().spawn(); let auto_save = AutoSaveHandler::new().spawn();
let document_colors = DocumentColorsHandler::default().spawn(); let document_colors = DocumentColorsHandler::default().spawn();
let pull_diagnostics = PullDiagnosticsHandler::new().spawn(); let pull_diagnostics = PullDiagnosticsHandler::new().spawn();
let pull_all_documents_diagnostics = PullAllDocumentsDiagnosticHandler::new().spawn();
let handlers = Handlers { let handlers = Handlers {
completions: helix_view::handlers::completion::CompletionHandler::new(event_tx), completions: helix_view::handlers::completion::CompletionHandler::new(event_tx),
@ -35,6 +37,7 @@ pub fn setup(config: Arc<ArcSwap<Config>>) -> Handlers {
auto_save, auto_save,
document_colors, document_colors,
pull_diagnostics, pull_diagnostics,
pull_all_documents_diagnostics,
}; };
helix_view::handlers::register_hooks(&handlers); helix_view::handlers::register_hooks(&handlers);

View File

@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::time::Duration; use std::time::Duration;
use tokio::time::Instant; use tokio::time::Instant;
@ -11,7 +12,7 @@ use helix_view::events::{
DiagnosticsDidChange, DocumentDidChange, DocumentDidOpen, LanguageServerInitialized, DiagnosticsDidChange, DocumentDidChange, DocumentDidOpen, LanguageServerInitialized,
}; };
use helix_view::handlers::diagnostics::DiagnosticEvent; use helix_view::handlers::diagnostics::DiagnosticEvent;
use helix_view::handlers::lsp::PullDiagnosticsEvent; use helix_view::handlers::lsp::{PullAllDocumentsDiagnosticsEvent, PullDiagnosticsEvent};
use helix_view::handlers::Handlers; use helix_view::handlers::Handlers;
use helix_view::{DocumentId, Editor}; use helix_view::{DocumentId, Editor};
@ -35,6 +36,7 @@ pub(super) fn register_hooks(handlers: &Handlers) {
}); });
let tx = handlers.pull_diagnostics.clone(); let tx = handlers.pull_diagnostics.clone();
let tx_all_documents = handlers.pull_all_documents_diagnostics.clone();
register_hook!(move |event: &mut DocumentDidChange<'_>| { register_hook!(move |event: &mut DocumentDidChange<'_>| {
if event if event
.doc .doc
@ -42,6 +44,7 @@ pub(super) fn register_hooks(handlers: &Handlers) {
{ {
let document_id = event.doc.id(); let document_id = event.doc.id();
send_blocking(&tx, PullDiagnosticsEvent { document_id }); send_blocking(&tx, PullDiagnosticsEvent { document_id });
send_blocking(&tx_all_documents, PullAllDocumentsDiagnosticsEvent {});
} }
Ok(()) Ok(())
}); });
@ -74,11 +77,15 @@ pub(super) fn register_hooks(handlers: &Handlers) {
} }
#[derive(Debug)] #[derive(Debug)]
pub(super) struct PullDiagnosticsHandler {} pub(super) struct PullDiagnosticsHandler {
document_ids: HashSet<DocumentId>,
}
impl PullDiagnosticsHandler { impl PullDiagnosticsHandler {
pub fn new() -> Self { pub fn new() -> Self {
PullDiagnosticsHandler {} PullDiagnosticsHandler {
document_ids: Default::default(),
}
} }
} }
@ -87,31 +94,80 @@ impl helix_event::AsyncHook for PullDiagnosticsHandler {
fn handle_event( fn handle_event(
&mut self, &mut self,
_event: Self::Event, event: Self::Event,
_timeout: Option<tokio::time::Instant>, _timeout: Option<tokio::time::Instant>,
) -> Option<tokio::time::Instant> { ) -> Option<tokio::time::Instant> {
self.document_ids.insert(event.document_id);
Some(Instant::now() + Duration::from_millis(125)) Some(Instant::now() + Duration::from_millis(125))
} }
fn finish_debounce(&mut self) { fn finish_debounce(&mut self) {
dispatch_pull_diagnostic_for_open_documents(); let document_ids = self.document_ids.clone();
job::dispatch_blocking(move |editor, _| {
for document_id in document_ids {
let document = editor.document(document_id);
let Some(document) = document else {
return;
};
let language_servers = document
.language_servers_with_feature(LanguageServerFeature::PullDiagnostics)
.filter(|ls| ls.is_initialized());
for language_server in language_servers {
pull_diagnostics_for_document(document, language_server);
}
}
})
} }
} }
fn dispatch_pull_diagnostic_for_open_documents() { #[derive(Debug)]
job::dispatch_blocking(move |editor, _| { pub(super) struct PullAllDocumentsDiagnosticHandler {}
let documents = editor.documents.values();
for document in documents { impl PullAllDocumentsDiagnosticHandler {
let language_servers = document pub fn new() -> Self {
.language_servers_with_feature(LanguageServerFeature::PullDiagnostics) PullAllDocumentsDiagnosticHandler {}
.filter(|ls| ls.is_initialized()); }
}
for language_server in language_servers { impl helix_event::AsyncHook for PullAllDocumentsDiagnosticHandler {
pull_diagnostics_for_document(document, language_server); type Event = PullAllDocumentsDiagnosticsEvent;
fn handle_event(
&mut self,
_event: Self::Event,
_timeout: Option<tokio::time::Instant>,
) -> Option<tokio::time::Instant> {
Some(Instant::now() + Duration::from_millis(500))
}
fn finish_debounce(&mut self) {
job::dispatch_blocking(move |editor, _| {
for document in editor.documents.values() {
let language_servers = document
.language_servers_with_feature(LanguageServerFeature::PullDiagnostics)
.filter(|ls| ls.is_initialized())
.filter(|ls| {
ls.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
}
},
)
});
for language_server in language_servers {
pull_diagnostics_for_document(document, language_server);
}
} }
} })
}) }
} }
pub fn pull_diagnostics_for_document( pub fn pull_diagnostics_for_document(

View File

@ -23,6 +23,7 @@ pub struct Handlers {
pub auto_save: Sender<AutoSaveEvent>, pub auto_save: Sender<AutoSaveEvent>,
pub document_colors: Sender<lsp::DocumentColorsEvent>, pub document_colors: Sender<lsp::DocumentColorsEvent>,
pub pull_diagnostics: Sender<lsp::PullDiagnosticsEvent>, pub pull_diagnostics: Sender<lsp::PullDiagnosticsEvent>,
pub pull_all_documents_diagnostics: Sender<lsp::PullAllDocumentsDiagnosticsEvent>,
} }
impl Handlers { impl Handlers {

View File

@ -34,6 +34,8 @@ pub struct PullDiagnosticsEvent {
pub document_id: DocumentId, pub document_id: DocumentId,
} }
pub struct PullAllDocumentsDiagnosticsEvent {}
#[derive(Debug)] #[derive(Debug)]
pub struct ApplyEditError { pub struct ApplyEditError {
pub kind: ApplyEditErrorKind, pub kind: ApplyEditErrorKind,