Skip to content

Commit 90b2203

Browse files
ConradIrwinmaan2003
andcommitted
Faster completion support for remotes
Before this change we cancled every completion task on each keystroke. This works fine when the latency to the LSP is low, but on a remote that may be a few hundred ms away, this means you are always having to wait until the completions show. Co-Authored-By: Maan2003 <[email protected]>
1 parent 550ceec commit 90b2203

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

crates/editor/src/editor.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ pub struct Editor {
550550
signature_help_state: SignatureHelpState,
551551
auto_signature_help: Option<bool>,
552552
find_all_references_task_sources: Vec<Anchor>,
553+
backup_completion_task: CompletionId,
553554
next_completion_id: CompletionId,
554555
completion_documentation_pre_resolve_debounce: DebouncedDelay,
555556
available_code_actions: Option<(Location, Arc<[CodeAction]>)>,
@@ -1895,6 +1896,7 @@ impl Editor {
18951896
signature_help_state: SignatureHelpState::default(),
18961897
auto_signature_help: None,
18971898
find_all_references_task_sources: Vec::new(),
1899+
backup_completion_task: 0,
18981900
next_completion_id: 0,
18991901
completion_documentation_pre_resolve_debounce: DebouncedDelay::new(),
19001902
next_inlay_id: 0,
@@ -4218,16 +4220,42 @@ impl Editor {
42184220
}),
42194221
trigger_kind,
42204222
};
4223+
let snapshot = buffer.read(cx).snapshot();
4224+
let classifier = snapshot.char_classifier_at(&buffer_position);
4225+
let first_char = options
4226+
.trigger
4227+
.as_ref()
4228+
.and_then(|trigger| trigger.chars().next());
4229+
let should_cancel_backup = first_char.is_some_and(|char| !classifier.is_word(char));
42214230
let completions = provider.completions(&buffer, buffer_position, completion_context, cx);
42224231
let sort_completions = provider.sort_completions();
42234232

42244233
let id = post_inc(&mut self.next_completion_id);
4234+
if should_cancel_backup {
4235+
self.backup_completion_task = id;
4236+
}
4237+
dbg!(
4238+
id,
4239+
&options.trigger,
4240+
&should_cancel_backup,
4241+
self.backup_completion_task
4242+
);
42254243
let task = cx.spawn(|this, mut cx| {
42264244
async move {
42274245
this.update(&mut cx, |this, _| {
4228-
this.completion_tasks.retain(|(task_id, _)| *task_id >= id);
4246+
this.completion_tasks.retain(|(task_id, _)| {
4247+
if *task_id >= id || *task_id == this.backup_completion_task {
4248+
true
4249+
} else {
4250+
dbg!("dropping", id);
4251+
false
4252+
}
4253+
});
42294254
})?;
42304255
let completions = completions.await.log_err();
4256+
cx.background_executor()
4257+
.timer(Duration::from_millis(1000))
4258+
.await;
42314259
let menu = if let Some(completions) = completions {
42324260
let mut menu = CompletionsMenu {
42334261
id,
@@ -4253,13 +4281,24 @@ impl Editor {
42534281
DebouncedDelay::new(),
42544282
)),
42554283
};
4256-
menu.filter(query.as_deref(), cx.background_executor().clone())
4284+
let completion_query = this.update(&mut cx, |this, cx| {
4285+
Self::completion_query(&this.buffer.read(cx).read(cx), position)
4286+
})?;
4287+
let query = completion_query.or(query);
4288+
menu.filter(dbg!(query.as_deref()), cx.background_executor().clone())
42574289
.await;
42584290

4291+
this.update(&mut cx, |editor, cx| {
4292+
dbg!("got here!");
4293+
editor.backup_completion_task = editor.next_completion_id.saturating_sub(1);
4294+
})?;
4295+
42594296
if menu.matches.is_empty() {
42604297
None
42614298
} else {
42624299
this.update(&mut cx, |editor, cx| {
4300+
editor.backup_completion_task =
4301+
editor.next_completion_id.saturating_sub(1);
42634302
let completions = menu.completions.clone();
42644303
let matches = menu.matches.clone();
42654304

0 commit comments

Comments
 (0)