Skip to content

Commit

Permalink
Properly handle opening of file-less excerpts (cherry-pick #21465) (#…
Browse files Browse the repository at this point in the history
…21472)

Cherry-picked Properly handle opening of file-less excerpts (#21465)

Follow-up of #20491 and
#20469
Closes #21369

Release Notes:

- Fixed file-less excerpts always opening instead of activating

Co-authored-by: Kirill Bulatov <[email protected]>
  • Loading branch information
1 parent 4b90302 commit b1dc23b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
37 changes: 35 additions & 2 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12873,8 +12873,41 @@ impl Editor {
};

for (buffer, (ranges, scroll_offset)) in new_selections_by_buffer {
let editor =
workspace.open_project_item::<Self>(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::<Editor>()?;
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::<Self>(
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),
Expand Down
2 changes: 1 addition & 1 deletion crates/editor/src/editor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit b1dc23b

Please sign in to comment.