Separate timeout for language servers without interfile dependencies

This commit is contained in:
Sofus Addington 2025-03-08 06:11:46 +01:00
parent 9be3681f0e
commit b8f47d7ca9
No known key found for this signature in database
GPG key ID: 57579341E1199840

View file

@ -57,27 +57,45 @@ pub(super) fn register_hooks(handlers: &Handlers) {
}
#[derive(Debug)]
pub(super) struct PullDiagnosticsHandler {}
pub(super) struct PullDiagnosticsHandler {
no_inter_file_dependency_timeout: Option<tokio::time::Instant>,
}
impl PullDiagnosticsHandler {
pub fn new() -> PullDiagnosticsHandler {
PullDiagnosticsHandler {}
PullDiagnosticsHandler {
no_inter_file_dependency_timeout: None,
}
}
}
const TIMEOUT: Duration = Duration::from_millis(500);
const TIMEOUT_NO_INTER_FILE_DEPENDENCY: Duration = Duration::from_millis(125);
impl helix_event::AsyncHook for PullDiagnosticsHandler {
type Event = PullDiagnosticsEvent;
fn handle_event(
&mut self,
event: Self::Event,
existing_debounce: Option<tokio::time::Instant>,
timeout: Option<tokio::time::Instant>,
) -> Option<tokio::time::Instant> {
if existing_debounce.is_none() {
dispatch_pull_diagnostic_for_document(event.document_id);
if timeout.is_none() {
dispatch_pull_diagnostic_for_document(event.document_id, false);
self.no_inter_file_dependency_timeout = Some(Instant::now());
}
Some(Instant::now() + Duration::from_millis(500))
if self
.no_inter_file_dependency_timeout
.is_some_and(|nifd_timeout| {
nifd_timeout.duration_since(Instant::now()) > TIMEOUT_NO_INTER_FILE_DEPENDENCY
})
{
dispatch_pull_diagnostic_for_document(event.document_id, true);
self.no_inter_file_dependency_timeout = Some(Instant::now());
};
Some(Instant::now() + TIMEOUT)
}
fn finish_debounce(&mut self) {
@ -85,14 +103,33 @@ impl helix_event::AsyncHook for PullDiagnosticsHandler {
}
}
fn dispatch_pull_diagnostic_for_document(document_id: DocumentId) {
fn dispatch_pull_diagnostic_for_document(
document_id: DocumentId,
exclude_language_servers_without_inter_file_dependency: bool,
) {
job::dispatch_blocking(move |editor, _| {
let Some(doc) = editor.document(document_id) else {
return;
};
let language_servers =
doc.language_servers_with_feature(LanguageServerFeature::PullDiagnostics);
let language_servers = doc
.language_servers_with_feature(LanguageServerFeature::PullDiagnostics)
.filter(|ls| {
if !exclude_language_servers_without_inter_file_dependency {
return true;
};
ls.capabilities()
.diagnostic_provider
.as_ref()
.is_some_and(|dp| match dp {
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(doc, language_server);