Skip to content

Commit f4956cc

Browse files
Revert "Always wait for completion resolve before applying the completion edits (#18907)"
This reverts commit a62a2fa.
1 parent f6f5ad1 commit f4956cc

File tree

8 files changed

+62
-143
lines changed

8 files changed

+62
-143
lines changed

crates/collab/src/tests/editor_tests.rs

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -379,75 +379,51 @@ async fn test_collaborating_with_completion(cx_a: &mut TestAppContext, cx_b: &mu
379379
.next()
380380
.await
381381
.unwrap();
382+
cx_a.executor().finish_waiting();
383+
382384
// Open the buffer on the host.
383385
let buffer_a = project_a
384386
.update(cx_a, |p, cx| p.open_buffer((worktree_id, "main.rs"), cx))
385387
.await
386388
.unwrap();
389+
cx_a.executor().run_until_parked();
387390

388391
buffer_a.read_with(cx_a, |buffer, _| {
389392
assert_eq!(buffer.text(), "fn main() { a. }")
390393
});
391394

395+
// Confirm a completion on the guest.
396+
editor_b.update(cx_b, |editor, cx| {
397+
assert!(editor.context_menu_visible());
398+
editor.confirm_completion(&ConfirmCompletion { item_ix: Some(0) }, cx);
399+
assert_eq!(editor.text(cx), "fn main() { a.first_method() }");
400+
});
401+
392402
// Return a resolved completion from the host's language server.
393403
// The resolved completion has an additional text edit.
394404
fake_language_server.handle_request::<lsp::request::ResolveCompletionItem, _, _>(
395405
|params, _| async move {
396-
Ok(match params.label.as_str() {
397-
"first_method(…)" => lsp::CompletionItem {
398-
label: "first_method(…)".into(),
399-
detail: Some("fn(&mut self, B) -> C".into()),
400-
text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
401-
new_text: "first_method($1)".to_string(),
402-
range: lsp::Range::new(
403-
lsp::Position::new(0, 14),
404-
lsp::Position::new(0, 14),
405-
),
406-
})),
407-
additional_text_edits: Some(vec![lsp::TextEdit {
408-
new_text: "use d::SomeTrait;\n".to_string(),
409-
range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 0)),
410-
}]),
411-
insert_text_format: Some(lsp::InsertTextFormat::SNIPPET),
412-
..Default::default()
413-
},
414-
"second_method(…)" => lsp::CompletionItem {
415-
label: "second_method(…)".into(),
416-
detail: Some("fn(&mut self, C) -> D<E>".into()),
417-
text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
418-
new_text: "second_method()".to_string(),
419-
range: lsp::Range::new(
420-
lsp::Position::new(0, 14),
421-
lsp::Position::new(0, 14),
422-
),
423-
})),
424-
insert_text_format: Some(lsp::InsertTextFormat::SNIPPET),
425-
additional_text_edits: Some(vec![lsp::TextEdit {
426-
new_text: "use d::SomeTrait;\n".to_string(),
427-
range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 0)),
428-
}]),
429-
..Default::default()
430-
},
431-
_ => panic!("unexpected completion label: {:?}", params.label),
406+
assert_eq!(params.label, "first_method(…)");
407+
Ok(lsp::CompletionItem {
408+
label: "first_method(…)".into(),
409+
detail: Some("fn(&mut self, B) -> C".into()),
410+
text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
411+
new_text: "first_method($1)".to_string(),
412+
range: lsp::Range::new(lsp::Position::new(0, 14), lsp::Position::new(0, 14)),
413+
})),
414+
additional_text_edits: Some(vec![lsp::TextEdit {
415+
new_text: "use d::SomeTrait;\n".to_string(),
416+
range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 0)),
417+
}]),
418+
insert_text_format: Some(lsp::InsertTextFormat::SNIPPET),
419+
..Default::default()
432420
})
433421
},
434422
);
435-
cx_a.executor().finish_waiting();
436-
cx_a.executor().run_until_parked();
437423

438-
// Confirm a completion on the guest.
439-
editor_b
440-
.update(cx_b, |editor, cx| {
441-
assert!(editor.context_menu_visible());
442-
editor.confirm_completion(&ConfirmCompletion { item_ix: Some(0) }, cx)
443-
})
444-
.unwrap()
445-
.await
446-
.unwrap();
424+
// The additional edit is applied.
447425
cx_a.executor().run_until_parked();
448-
cx_b.executor().run_until_parked();
449426

450-
// The additional edit is applied.
451427
buffer_a.read_with(cx_a, |buffer, _| {
452428
assert_eq!(
453429
buffer.text(),
@@ -540,15 +516,9 @@ async fn test_collaborating_with_completion(cx_a: &mut TestAppContext, cx_b: &mu
540516
cx_b.executor().run_until_parked();
541517

542518
// When accepting the completion, the snippet is insert.
543-
editor_b
544-
.update(cx_b, |editor, cx| {
545-
assert!(editor.context_menu_visible());
546-
editor.confirm_completion(&ConfirmCompletion { item_ix: Some(0) }, cx)
547-
})
548-
.unwrap()
549-
.await
550-
.unwrap();
551519
editor_b.update(cx_b, |editor, cx| {
520+
assert!(editor.context_menu_visible());
521+
editor.confirm_completion(&ConfirmCompletion { item_ix: Some(0) }, cx);
552522
assert_eq!(
553523
editor.text(cx),
554524
"use d::SomeTrait;\nfn main() { a.first_method(); a.third_method(, , ) }"

crates/copilot/src/copilot_completion_provider.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,10 @@ mod tests {
363363

364364
// Confirming a completion inserts it and hides the context menu, without showing
365365
// the copilot suggestion afterwards.
366-
editor.confirm_completion(&Default::default(), cx).unwrap()
367-
})
368-
.await
369-
.unwrap();
370-
371-
cx.update_editor(|editor, cx| {
366+
editor
367+
.confirm_completion(&Default::default(), cx)
368+
.unwrap()
369+
.detach();
372370
assert!(!editor.context_menu_visible());
373371
assert!(!editor.has_active_inline_completion(cx));
374372
assert_eq!(editor.text(cx), "one.completion_a\ntwo\nthree\n");
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{ops::ControlFlow, time::Duration};
1+
use std::time::Duration;
22

33
use futures::{channel::oneshot, FutureExt};
44
use gpui::{Task, ViewContext};
@@ -7,7 +7,7 @@ use crate::Editor;
77

88
pub struct DebouncedDelay {
99
task: Option<Task<()>>,
10-
cancel_channel: Option<oneshot::Sender<ControlFlow<()>>>,
10+
cancel_channel: Option<oneshot::Sender<()>>,
1111
}
1212

1313
impl DebouncedDelay {
@@ -23,22 +23,17 @@ impl DebouncedDelay {
2323
F: 'static + Send + FnOnce(&mut Editor, &mut ViewContext<Editor>) -> Task<()>,
2424
{
2525
if let Some(channel) = self.cancel_channel.take() {
26-
channel.send(ControlFlow::Break(())).ok();
26+
_ = channel.send(());
2727
}
2828

29-
let (sender, mut receiver) = oneshot::channel::<ControlFlow<()>>();
29+
let (sender, mut receiver) = oneshot::channel::<()>();
3030
self.cancel_channel = Some(sender);
3131

3232
drop(self.task.take());
3333
self.task = Some(cx.spawn(move |model, mut cx| async move {
3434
let mut timer = cx.background_executor().timer(delay).fuse();
3535
futures::select_biased! {
36-
interrupt = receiver => {
37-
match interrupt {
38-
Ok(ControlFlow::Break(())) | Err(_) => return,
39-
Ok(ControlFlow::Continue(())) => {},
40-
}
41-
}
36+
_ = receiver => return,
4237
_ = timer => {}
4338
}
4439

@@ -47,11 +42,4 @@ impl DebouncedDelay {
4742
}
4843
}));
4944
}
50-
51-
pub fn start_now(&mut self) -> Option<Task<()>> {
52-
if let Some(channel) = self.cancel_channel.take() {
53-
channel.send(ControlFlow::Continue(())).ok();
54-
}
55-
self.task.take()
56-
}
5745
}

crates/editor/src/editor.rs

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4459,49 +4459,16 @@ impl Editor {
44594459
&mut self,
44604460
item_ix: Option<usize>,
44614461
intent: CompletionIntent,
4462-
cx: &mut ViewContext<Self>,
4463-
) -> Option<Task<anyhow::Result<()>>> {
4462+
cx: &mut ViewContext<Editor>,
4463+
) -> Option<Task<std::result::Result<(), anyhow::Error>>> {
4464+
use language::ToOffset as _;
4465+
44644466
let completions_menu = if let ContextMenu::Completions(menu) = self.hide_context_menu(cx)? {
44654467
menu
44664468
} else {
44674469
return None;
44684470
};
44694471

4470-
let mut resolve_task_store = completions_menu
4471-
.selected_completion_documentation_resolve_debounce
4472-
.lock();
4473-
let selected_completion_resolve = resolve_task_store.start_now();
4474-
let menu_pre_resolve = self
4475-
.completion_documentation_pre_resolve_debounce
4476-
.start_now();
4477-
drop(resolve_task_store);
4478-
4479-
Some(cx.spawn(|editor, mut cx| async move {
4480-
match (selected_completion_resolve, menu_pre_resolve) {
4481-
(None, None) => {}
4482-
(Some(resolve), None) | (None, Some(resolve)) => resolve.await,
4483-
(Some(resolve_1), Some(resolve_2)) => {
4484-
futures::join!(resolve_1, resolve_2);
4485-
}
4486-
}
4487-
if let Some(apply_edits_task) = editor.update(&mut cx, |editor, cx| {
4488-
editor.apply_resolved_completion(completions_menu, item_ix, intent, cx)
4489-
})? {
4490-
apply_edits_task.await?;
4491-
}
4492-
Ok(())
4493-
}))
4494-
}
4495-
4496-
fn apply_resolved_completion(
4497-
&mut self,
4498-
completions_menu: CompletionsMenu,
4499-
item_ix: Option<usize>,
4500-
intent: CompletionIntent,
4501-
cx: &mut ViewContext<'_, Editor>,
4502-
) -> Option<Task<anyhow::Result<Option<language::Transaction>>>> {
4503-
use language::ToOffset as _;
4504-
45054472
let mat = completions_menu
45064473
.matches
45074474
.get(item_ix.unwrap_or(completions_menu.selected_item))?;
@@ -4660,7 +4627,11 @@ impl Editor {
46604627
// so we should automatically call signature_help
46614628
self.show_signature_help(&ShowSignatureHelp, cx);
46624629
}
4663-
Some(apply_edits)
4630+
4631+
Some(cx.foreground_executor().spawn(async move {
4632+
apply_edits.await?;
4633+
Ok(())
4634+
}))
46644635
}
46654636

46664637
pub fn toggle_code_actions(&mut self, action: &ToggleCodeActions, cx: &mut ViewContext<Self>) {

crates/editor/src/editor_tests.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7996,7 +7996,7 @@ async fn test_completion(cx: &mut gpui::TestAppContext) {
79967996
.unwrap()
79977997
});
79987998
cx.assert_editor_state(indoc! {"
7999-
one.ˇ
7999+
one.second_completionˇ
80008000
two
80018001
three
80028002
"});
@@ -8029,9 +8029,9 @@ async fn test_completion(cx: &mut gpui::TestAppContext) {
80298029
cx.assert_editor_state(indoc! {"
80308030
one.second_completionˇ
80318031
two
8032-
thoverlapping additional editree
8033-
8034-
additional edit"});
8032+
three
8033+
additional edit
8034+
"});
80358035

80368036
cx.set_state(indoc! {"
80378037
one.second_completion
@@ -8091,8 +8091,8 @@ async fn test_completion(cx: &mut gpui::TestAppContext) {
80918091
});
80928092
cx.assert_editor_state(indoc! {"
80938093
one.second_completion
8094-
two siˇ
8095-
three siˇ
8094+
two sixth_completionˇ
8095+
three sixth_completionˇ
80968096
additional edit
80978097
"});
80988098

@@ -8133,11 +8133,9 @@ async fn test_completion(cx: &mut gpui::TestAppContext) {
81338133
.confirm_completion(&ConfirmCompletion::default(), cx)
81348134
.unwrap()
81358135
});
8136-
cx.assert_editor_state("editor.cloˇ");
8136+
cx.assert_editor_state("editor.closeˇ");
81378137
handle_resolve_completion_request(&mut cx, None).await;
81388138
apply_additional_edits.await.unwrap();
8139-
cx.assert_editor_state(indoc! {"
8140-
editor.closeˇ"});
81418139
}
81428140

81438141
#[gpui::test]
@@ -10142,7 +10140,7 @@ async fn test_completions_with_additional_edits(cx: &mut gpui::TestAppContext) {
1014210140
.confirm_completion(&ConfirmCompletion::default(), cx)
1014310141
.unwrap()
1014410142
});
10145-
cx.assert_editor_state(indoc! {"fn main() { let a = 2.ˇ; }"});
10143+
cx.assert_editor_state(indoc! {"fn main() { let a = 2.Some(2)ˇ; }"});
1014610144

1014710145
cx.handle_request::<lsp::request::ResolveCompletionItem, _, _>(move |_, _, _| {
1014810146
let task_completion_item = completion_item.clone();

crates/editor/src/hover_popover.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ mod tests {
810810
hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
811811
completion_provider: Some(lsp::CompletionOptions {
812812
trigger_characters: Some(vec![".".to_string(), ":".to_string()]),
813-
resolve_provider: Some(false),
813+
resolve_provider: Some(true),
814814
..Default::default()
815815
}),
816816
..Default::default()
@@ -902,15 +902,12 @@ mod tests {
902902
assert_eq!(counter.load(atomic::Ordering::Acquire), 1);
903903

904904
//apply a completion and check it was successfully applied
905-
let () = cx
906-
.update_editor(|editor, cx| {
907-
editor.context_menu_next(&Default::default(), cx);
908-
editor
909-
.confirm_completion(&ConfirmCompletion::default(), cx)
910-
.unwrap()
911-
})
912-
.await
913-
.unwrap();
905+
let _apply_additional_edits = cx.update_editor(|editor, cx| {
906+
editor.context_menu_next(&Default::default(), cx);
907+
editor
908+
.confirm_completion(&ConfirmCompletion::default(), cx)
909+
.unwrap()
910+
});
914911
cx.assert_editor_state(indoc! {"
915912
one.second_completionˇ
916913
two

crates/editor/src/test/editor_test_context.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use itertools::Itertools;
1313
use language::{Buffer, BufferSnapshot, LanguageRegistry};
1414
use multi_buffer::{ExcerptRange, ToPoint};
1515
use parking_lot::RwLock;
16-
use pretty_assertions::assert_eq;
1716
use project::{FakeFs, Project};
1817
use std::{
1918
any::TypeId,

crates/vim/src/normal/repeat.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ mod test {
387387
lsp::ServerCapabilities {
388388
completion_provider: Some(lsp::CompletionOptions {
389389
trigger_characters: Some(vec![".".to_string(), ":".to_string()]),
390-
resolve_provider: Some(false),
390+
resolve_provider: Some(true),
391391
..Default::default()
392392
}),
393393
..Default::default()
@@ -432,9 +432,7 @@ mod test {
432432
request.next().await;
433433
cx.condition(|editor, _| editor.context_menu_visible())
434434
.await;
435-
cx.simulate_keystrokes("down enter");
436-
cx.run_until_parked();
437-
cx.simulate_keystrokes("! escape");
435+
cx.simulate_keystrokes("down enter ! escape");
438436

439437
cx.assert_state(
440438
indoc! {"

0 commit comments

Comments
 (0)