Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Fix panic when confirming completion across buffers (#59471)
Neel2ec0e6c2d702
diff --git a/crates/editor/src/completions.rs b/crates/editor/src/completions.rsindex e2f0be07ce0..8ad37dcfd63 100644--- a/crates/editor/src/completions.rs+++ b/crates/editor/src/completions.rs@@ -830,14 +830,19 @@ impl Editor {let old_text = buffer.text_for_range(replace_range.clone()).collect::<String>();- let lookbehind = newest_range_buffer- .start- .to_offset(buffer_snapshot)- .saturating_sub(replace_range.start.to_offset(&buffer_snapshot));- let lookahead = replace_range- .end- .to_offset(&buffer_snapshot)- .saturating_sub(newest_range_buffer.end.to_offset(&buffer));+ let (lookbehind, lookahead) = if buffer.remote_id() == buffer_snapshot.remote_id() {+ let lookbehind = newest_range_buffer+ .start+ .to_offset(&buffer)+ .saturating_sub(replace_range.start.to_offset(&buffer));+ let lookahead = replace_range+ .end+ .to_offset(&buffer)+ .saturating_sub(newest_range_buffer.end.to_offset(&buffer));+ (lookbehind, lookahead)+ } else {+ (0, 0)+ };let prefix = &old_text[..old_text.len().saturating_sub(lookahead)];let suffix = &old_text[lookbehind.min(old_text.len())..];diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex dc50483f496..145ceaa6943 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -17779,6 +17779,202 @@ async fn test_completion_in_multibuffer_with_replace_range(cx: &mut TestAppConte})}+#[gpui::test]+async fn test_completion_in_multibuffer_with_newest_selection_in_other_buffer(+ cx: &mut TestAppContext,+) {+ init_test(cx, |_| {});++ let main_text = indoc! {"+ fn main() {+ 10.satu+ }+ "};+ let other_text = indoc! {"+ const VALUE: u32 = 0;+ "};++ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(+ path!("/a"),+ json!({+ "main.rs": main_text,+ "other.rs": other_text,+ }),+ )+ .await;++ let project = Project::test(fs, [path!("/a").as_ref()], cx).await;+ let language_registry = project.read_with(cx, |project, _| project.languages().clone());+ language_registry.add(rust_lang());+ let mut fake_servers = language_registry.register_fake_lsp(+ "Rust",+ FakeLspAdapter {+ capabilities: lsp::ServerCapabilities {+ completion_provider: Some(lsp::CompletionOptions {+ resolve_provider: None,+ ..lsp::CompletionOptions::default()+ }),+ ..lsp::ServerCapabilities::default()+ },+ ..FakeLspAdapter::default()+ },+ );+ let window = cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));+ let workspace = window+ .read_with(cx, |mw, _| mw.workspace().clone())+ .unwrap();+ let cx = &mut VisualTestContext::from_window(*window, cx);+ let main_buffer = project+ .update(cx, |project, cx| {+ project.open_local_buffer(path!("/a/main.rs"), cx)+ })+ .await+ .unwrap();+ let other_buffer = project+ .update(cx, |project, cx| {+ project.open_local_buffer(path!("/a/other.rs"), cx)+ })+ .await+ .unwrap();++ let multi_buffer = cx.new(|cx| {+ let mut multi_buffer = MultiBuffer::new(Capability::ReadWrite);+ multi_buffer.set_excerpts_for_path(+ PathKey::sorted(0),+ main_buffer.clone(),+ [Point::zero()..main_buffer.read(cx).max_point()],+ 0,+ cx,+ );+ multi_buffer.set_excerpts_for_path(+ PathKey::sorted(1),+ other_buffer.clone(),+ [Point::zero()..other_buffer.read(cx).max_point()],+ 0,+ cx,+ );+ multi_buffer+ });++ let editor = workspace.update_in(cx, |_, window, cx| {+ cx.new(|cx| {+ Editor::new(+ EditorMode::Full {+ scale_ui_elements_with_buffer_font_size: false,+ show_active_line_background: false,+ sizing_behavior: SizingBehavior::Default,+ },+ multi_buffer.clone(),+ Some(project.clone()),+ window,+ cx,+ )+ })+ });++ let pane = workspace.update_in(cx, |workspace, _, _| workspace.active_pane().clone());+ pane.update_in(cx, |pane, window, cx| {+ pane.add_item(Box::new(editor.clone()), true, true, None, window, cx);+ });++ let fake_server = fake_servers.next().await.unwrap();+ cx.run_until_parked();++ // Place the (only, and therefore newest) selection in `main.rs`, right after `satu`.+ let main_cursor = editor.update(cx, |editor, cx| {+ let multibuffer_snapshot = editor.buffer().read(cx).snapshot(cx);+ let main_snapshot = main_buffer.read(cx).snapshot();+ let anchor = main_snapshot.anchor_before(Point::new(1, 11));+ multibuffer_snapshot+ .buffer_anchor_range_to_anchor_range(anchor..anchor)+ .expect("main.rs cursor position should map into the multibuffer")+ });+ editor.update_in(cx, |editor, window, cx| {+ editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {+ s.select_anchor_ranges([main_cursor.clone()]);+ });+ });++ editor.update_in(cx, |editor, window, cx| {+ editor.show_completions(&ShowCompletions, window, cx);+ });++ fake_server+ .set_request_handler::<lsp::request::Completion, _, _>(move |_, _| async move {+ let completion_item = lsp::CompletionItem {+ label: "saturating_sub()".into(),+ text_edit: Some(lsp::CompletionTextEdit::InsertAndReplace(+ lsp::InsertReplaceEdit {+ new_text: "saturating_sub()".to_owned(),+ insert: lsp::Range::new(+ lsp::Position::new(1, 7),+ lsp::Position::new(1, 11),+ ),+ replace: lsp::Range::new(+ lsp::Position::new(1, 7),+ lsp::Position::new(1, 11),+ ),+ },+ )),+ ..lsp::CompletionItem::default()+ };++ Ok(Some(lsp::CompletionResponse::Array(vec![completion_item])))+ })+ .next()+ .await+ .unwrap();++ cx.condition(&editor, |editor, _| editor.context_menu_visible())+ .await;++ // Move the newest selection into `other.rs` while keeping the completions menu+ // open.+ let other_cursor = editor.update(cx, |editor, cx| {+ let multibuffer_snapshot = editor.buffer().read(cx).snapshot(cx);+ let other_snapshot = other_buffer.read(cx).snapshot();+ let anchor = other_snapshot.anchor_before(Point::new(0, 6));+ multibuffer_snapshot+ .buffer_anchor_range_to_anchor_range(anchor..anchor)+ .expect("other.rs cursor position should map into the multibuffer")+ });+ editor.update_in(cx, |editor, window, cx| {+ editor.change_selections(+ SelectionEffects::no_scroll().completions(false),+ window,+ cx,+ |s| s.select_anchor_ranges([other_cursor.clone()]),+ );+ assert!(+ editor.context_menu_visible(),+ "the completions menu should still be open after moving the cursor"+ );+ });++ // This used to panic with `invalid anchor - buffer id does not match`.+ editor+ .update_in(cx, |editor, window, cx| {+ editor+ .confirm_completion_replace(&ConfirmCompletionReplace, window, cx)+ .unwrap()+ })+ .await+ .unwrap();++ // The completion targets `main.rs`, so it should still be applied there.+ main_buffer.read_with(cx, |buffer, _| {+ assert_eq!(+ buffer.text(),+ indoc! {"+ fn main() {+ 10.saturating_sub()+ }+ "}+ );+ });+}+#[gpui::test]async fn test_completion(cx: &mut TestAppContext) {init_test(cx, |_| {});