@@ -546,11 +546,12 @@ pub struct Editor {
546546 nav_history: Option<ItemNavHistory>,
547547 context_menu: RwLock<Option<ContextMenu>>,
548548 mouse_context_menu: Option<MouseContextMenu>,
549- completion_tasks: Vec<(CompletionId, Task<Option<()>>)>,
550549 signature_help_state: SignatureHelpState,
551550 auto_signature_help: Option<bool>,
552551 find_all_references_task_sources: Vec<Anchor>,
553- backup_completion_task: CompletionId,
552+ // invariant: backup_completion_task.is_some() -> latest_completion_task.is_some()
553+ backup_completion_task: Option<(CompletionId, Task<Option<()>>)>,
554+ latest_completion_task: Option<(CompletionId, Task<Option<()>>)>,
554555 next_completion_id: CompletionId,
555556 completion_documentation_pre_resolve_debounce: DebouncedDelay,
556557 available_code_actions: Option<(Location, Arc<[CodeAction]>)>,
@@ -1892,11 +1893,11 @@ impl Editor {
18921893 nav_history: None,
18931894 context_menu: RwLock::new(None),
18941895 mouse_context_menu: None,
1895- completion_tasks: Default::default(),
1896+ latest_completion_task: None,
1897+ backup_completion_task: None,
18961898 signature_help_state: SignatureHelpState::default(),
18971899 auto_signature_help: None,
18981900 find_all_references_task_sources: Vec::new(),
1899- backup_completion_task: 0,
19001901 next_completion_id: 0,
19011902 completion_documentation_pre_resolve_debounce: DebouncedDelay::new(),
19021903 next_inlay_id: 0,
@@ -4231,27 +4232,20 @@ impl Editor {
42314232 let sort_completions = provider.sort_completions();
42324233
42334234 let id = post_inc(&mut self.next_completion_id);
4234- if should_cancel_backup {
4235- self.backup_completion_task = id;
4236- }
42374235 dbg!(
42384236 id,
42394237 &options.trigger,
42404238 &should_cancel_backup,
4241- self.backup_completion_task
4239+ &self.backup_completion_task,
4240+ &self.latest_completion_task,
42424241 );
4242+ if should_cancel_backup {
4243+ self.backup_completion_task = None;
4244+ }
42434245 let task = cx.spawn(|this, mut cx| {
42444246 async move {
4245- this.update(&mut cx, |this, _| {
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- });
4254- })?;
4247+ // TODO: if a menu exists already, then filter it before wait for completions.
4248+ // looks like this is something else filtering the completion menu.
42554249 let completions = completions.await.log_err();
42564250 cx.background_executor()
42574251 .timer(Duration::from_millis(1000))
@@ -4288,17 +4282,10 @@ impl Editor {
42884282 menu.filter(dbg!(query.as_deref()), cx.background_executor().clone())
42894283 .await;
42904284
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-
42964285 if menu.matches.is_empty() {
42974286 None
42984287 } else {
42994288 this.update(&mut cx, |editor, cx| {
4300- editor.backup_completion_task =
4301- editor.next_completion_id.saturating_sub(1);
43024289 let completions = menu.completions.clone();
43034290 let matches = menu.matches.clone();
43044291
@@ -4338,13 +4325,33 @@ impl Editor {
43384325 _ => return,
43394326 }
43404327
4328+ let is_latest = if this
4329+ .latest_completion_task
4330+ .as_ref()
4331+ .map_or(false, |(task_id, _)| *task_id == id)
4332+ {
4333+ // we drop the both tasks
4334+ this.backup_completion_task = None;
4335+ this.latest_completion_task = None;
4336+ true
4337+ } else if this
4338+ .backup_completion_task
4339+ .as_ref()
4340+ .map_or(false, |(task_id, _)| *task_id == id)
4341+ {
4342+ this.backup_completion_task = None;
4343+ false
4344+ } else {
4345+ // the task didn't get cancelled, so we manually return
4346+ return;
4347+ };
43414348 if this.focus_handle.is_focused(cx) && menu.is_some() {
43424349 let menu = menu.unwrap();
43434350 *context_menu = Some(ContextMenu::Completions(menu));
43444351 drop(context_menu);
43454352 this.discard_inline_completion(false, cx);
43464353 cx.notify();
4347- } else if this.completion_tasks.len() <= 1 {
4354+ } else if is_latest {
43484355 // If there are no more completion tasks and the last menu was
43494356 // empty, we should hide it. If it was already hidden, we should
43504357 // also show the copilot completion when available.
@@ -4359,8 +4366,10 @@ impl Editor {
43594366 }
43604367 .log_err()
43614368 });
4362-
4363- self.completion_tasks.push((id, task));
4369+ let prev_task = std::mem::replace(&mut self.latest_completion_task, Some((id, task)));
4370+ if self.backup_completion_task.is_none() {
4371+ self.backup_completion_task = prev_task;
4372+ }
43644373 }
43654374
43664375 pub fn confirm_completion(
@@ -4622,7 +4631,8 @@ impl Editor {
46224631 return None;
46234632 }
46244633
4625- editor.completion_tasks.clear();
4634+ editor.latest_completion_task = None;
4635+ editor.backup_completion_task = None;
46264636 editor.discard_inline_completion(false, cx);
46274637 let task_context =
46284638 tasks
@@ -5230,7 +5240,7 @@ impl Editor {
52305240 let excerpt_id = cursor.excerpt_id;
52315241
52325242 if self.context_menu.read().is_none()
5233- && self.completion_tasks.is_empty ()
5243+ && self.latest_completion_task.is_none ()
52345244 && selection.start == selection.end
52355245 {
52365246 if let Some(provider) = self.inline_completion_provider() {
@@ -5402,7 +5412,8 @@ impl Editor {
54025412
54035413 fn hide_context_menu(&mut self, cx: &mut ViewContext<Self>) -> Option<ContextMenu> {
54045414 cx.notify();
5405- self.completion_tasks.clear();
5415+ self.latest_completion_task = None;
5416+ self.backup_completion_task = None;
54065417 let context_menu = self.context_menu.write().take();
54075418 if context_menu.is_some() {
54085419 self.update_visible_inline_completion(cx);
0 commit comments