From 8e7960928809aa8a7edce8cdeec979949e0a5f74 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Tue, 4 Jun 2024 12:22:01 +0200 Subject: [PATCH] editor: Cancel ongoing completion requests more eagerly (#12630) Previously, we were: - cancelling previous requests only after the latest one has completed - always running the debounced documentation resolution to completion, even when we had no need for it. In this commit, we drop the ongoing completion requests as soon as the new one is fired. Fixes #5166 Release Notes: - Improved performance and reliability of completions in large Typescript projects Co-authored-by: Bennet Bo --- crates/editor/src/debounced_delay.rs | 6 +----- crates/editor/src/editor.rs | 13 ++++++------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/crates/editor/src/debounced_delay.rs b/crates/editor/src/debounced_delay.rs index b9d8ebf1037aeaf32ce08ed54675a624ad805ad1..0dbf36d49e38aac439117ea598f2eba29283a47d 100644 --- a/crates/editor/src/debounced_delay.rs +++ b/crates/editor/src/debounced_delay.rs @@ -29,13 +29,9 @@ impl DebouncedDelay { let (sender, mut receiver) = oneshot::channel::<()>(); self.cancel_channel = Some(sender); - let previous_task = self.task.take(); + drop(self.task.take()); self.task = Some(cx.spawn(move |model, mut cx| async move { let mut timer = cx.background_executor().timer(delay).fuse(); - if let Some(previous_task) = previous_task { - previous_task.await; - } - futures::select_biased! { _ = receiver => return, _ = timer => {} diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 3c472540c02876508b8d9a3a39248e0ef3c2545a..3b75fdab8f0ad46f35a5b4a234d52ee1beb26879 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -15,18 +15,17 @@ pub mod actions; mod blame_entry_tooltip; mod blink_manager; +mod debounced_delay; pub mod display_map; mod editor_settings; mod element; -mod hunk_diff; -mod inlay_hint_cache; - -mod debounced_delay; mod git; mod highlight_matching_bracket; mod hover_links; mod hover_popover; +mod hunk_diff; mod indent_guides; +mod inlay_hint_cache; mod inline_completion_provider; pub mod items; mod mouse_context_menu; @@ -3806,6 +3805,9 @@ impl Editor { let id = post_inc(&mut self.next_completion_id); let task = cx.spawn(|this, mut cx| { async move { + this.update(&mut cx, |this, _| { + this.completion_tasks.retain(|(task_id, _)| *task_id >= id); + })?; let completions = completions.await.log_err(); let menu = if let Some(completions) = completions { let mut menu = CompletionsMenu { @@ -3844,7 +3846,6 @@ impl Editor { let delay_ms = EditorSettings::get_global(cx) .completion_documentation_secondary_query_debounce; let delay = Duration::from_millis(delay_ms); - editor .completion_documentation_pre_resolve_debounce .fire_new(delay, cx, |editor, cx| { @@ -3865,8 +3866,6 @@ impl Editor { }; this.update(&mut cx, |this, cx| { - this.completion_tasks.retain(|(task_id, _)| *task_id >= id); - let mut context_menu = this.context_menu.write(); match context_menu.as_ref() { None => {}