Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Preserve preview tabs for references multibuffers (#61021)

Shuhei Kadowakib0f145f4aec6
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index b81634785a9..830613be16a 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -29895,6 +29895,89 @@ async fn test_find_all_references_editor_reuse(cx: &mut TestAppContext) {
);
});
}
+
+#[gpui::test]
+async fn test_find_all_references_preserves_preview_tab(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ cx.update(|cx| {
+ SettingsStore::update_global(cx, |store, cx| {
+ store.update_user_settings(cx, |settings: &mut SettingsContent| {
+ let preview_tabs = settings.preview_tabs.get_or_insert_default();
+ preview_tabs.enabled = Some(true);
+ preview_tabs.enable_preview_multibuffer_from_code_navigation = Some(true);
+ });
+ });
+ });
+
+ let mut cx = EditorLspTestContext::new_rust(
+ lsp::ServerCapabilities {
+ references_provider: Some(lsp::OneOf::Left(true)),
+ ..lsp::ServerCapabilities::default()
+ },
+ cx,
+ )
+ .await;
+
+ // Calling `set_state` here will make it such that the underlying buffer
+ // will now report edits since its `preview_version` field, which is set
+ // when the buffer is first created. That is one of the conditions that
+ // determines whether a pane's item should remain as the preview item, or
+ // not, as such, if that field is not updated accordingly, we'd end up not
+ // having a preview item at the end of the test.
+ cx.set_state(
+ &r#"
+ fn one() {
+ let mut a = two();
+ }
+
+ fn ˇtwo() {}"#
+ .unindent(),
+ );
+ cx.executor().run_until_parked();
+
+ cx.lsp
+ .set_request_handler::<lsp::request::References, _, _>(move |params, _| async move {
+ Ok(Some(vec![
+ lsp::Location {
+ uri: params.text_document_position.text_document.uri.clone(),
+ range: lsp::Range::new(lsp::Position::new(0, 16), lsp::Position::new(0, 19)),
+ },
+ lsp::Location {
+ uri: params.text_document_position.text_document.uri,
+ range: lsp::Range::new(lsp::Position::new(4, 4), lsp::Position::new(4, 7)),
+ },
+ ]))
+ });
+
+ // Using the `editor: find all references` action adds excerpts to the
+ // multibuffer, which emits `ItemEvent::Edit`. When the pane handles that
+ // event it decides whether to keep its preview item and, if the buffer
+ // reports any edits since its `preview_version`, the item is unpreviewed.
+ // We also need to run until the executor is parked because the
+ // `ItemEvent::Edit` is delivered later, after the item has been registered
+ // with the pane.
+ assert_eq!(
+ cx.update_editor(|editor, window, cx| {
+ editor.find_all_references(&FindAllReferences::default(), window, cx)
+ })
+ .expect("Editor::find_all_references should return a task")
+ .await
+ .expect("Should be able to find all references"),
+ Navigated::Yes
+ );
+ cx.executor().run_until_parked();
+
+ cx.update_workspace(|workspace, _window, cx| {
+ let active_pane = workspace.active_pane().read(cx);
+
+ assert_eq!(
+ active_pane.preview_item_id(),
+ Some(active_pane.active_item().unwrap().item_id()),
+ "The preview item id should be the same as the id of the multibuffer"
+ );
+ });
+}
+
#[gpui::test]
async fn test_find_enclosing_node_with_task(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/editor/src/navigation.rs b/crates/editor/src/navigation.rs
index 3d4b42a3054..55a70109953 100644
--- a/crates/editor/src/navigation.rs
+++ b/crates/editor/src/navigation.rs
@@ -2294,6 +2294,11 @@ impl Editor {
pane.update(cx, |pane, cx| {
if allow_preview && !was_existing {
destination_index = pane.replace_preview_item_id(item.item_id(), window, cx);
+ editor.update(cx, |editor, cx| {
+ editor
+ .buffer
+ .update(cx, |buffer, cx| buffer.refresh_preview(cx))
+ });
}
if was_existing && !allow_preview {
pane.unpreview_item_if_preview(item.item_id());