From b1dc23b9cc5b597b5d3565a77d14f5353ac77722 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Tue, 3 Dec 2024 19:02:56 +0200 Subject: [PATCH] Properly handle opening of file-less excerpts (cherry-pick #21465) (#21472) Cherry-picked Properly handle opening of file-less excerpts (#21465) Follow-up of https://github.com/zed-industries/zed/pull/20491 and https://github.com/zed-industries/zed/pull/20469 Closes https://github.com/zed-industries/zed/issues/21369 Release Notes: - Fixed file-less excerpts always opening instead of activating Co-authored-by: Kirill Bulatov --- crates/editor/src/editor.rs | 37 +++++++++++++++++++++++++++++-- crates/editor/src/editor_tests.rs | 2 +- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index ed73639da64e2af0e19627897fd778e49d753c0f..f72869e15f20a3c076fc60242b66617b11be866a 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -12873,8 +12873,41 @@ impl Editor { }; for (buffer, (ranges, scroll_offset)) in new_selections_by_buffer { - let editor = - workspace.open_project_item::(pane.clone(), buffer, true, true, cx); + let editor = buffer + .read(cx) + .file() + .is_none() + .then(|| { + // Handle file-less buffers separately: those are not really the project items, so won't have a paroject path or entity id, + // so `workspace.open_project_item` will never find them, always opening a new editor. + // Instead, we try to activate the existing editor in the pane first. + let (editor, pane_item_index) = + pane.read(cx).items().enumerate().find_map(|(i, item)| { + let editor = item.downcast::()?; + let singleton_buffer = + editor.read(cx).buffer().read(cx).as_singleton()?; + if singleton_buffer == buffer { + Some((editor, i)) + } else { + None + } + })?; + pane.update(cx, |pane, cx| { + pane.activate_item(pane_item_index, true, true, cx) + }); + Some(editor) + }) + .flatten() + .unwrap_or_else(|| { + workspace.open_project_item::( + pane.clone(), + buffer, + true, + true, + cx, + ) + }); + editor.update(cx, |editor, cx| { let autoscroll = match scroll_offset { Some(scroll_offset) => Autoscroll::top_relative(scroll_offset as usize), diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 669134ef1028d7e1807e18c755d03f67e9217c1f..353301199c38c665dcf489b104f06b3e1bf1dbda 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -11700,7 +11700,7 @@ async fn test_mutlibuffer_in_navigation_history(cx: &mut gpui::TestAppContext) { multi_buffer_editor.update(cx, |editor, cx| { editor.change_selections(Some(Autoscroll::Next), cx, |s| { - s.select_ranges(Some(60..70)) + s.select_ranges(Some(70..70)) }); editor.open_excerpts(&OpenExcerpts, cx); });