preview tabs: Allow replacing preview tab when using code navigation (#10730)

This PR adds support for replacing the current preview tab when using
GoToDefinition. Previously a tab, that was navigated away from, was
converted into a permanent tab and the new tab was opened as preview.

Without `enable_preview_from_code_navigation`:


https://github.com/zed-industries/zed/assets/53836821/99840724-d6ff-4738-a9c4-ee71a0001634

With `enable_preview_from_code_navigation`:


https://github.com/zed-industries/zed/assets/53836821/8c60efcb-d597-40bf-b08b-13faf5a289b6

Note: In the future I would like to improve support for the navigation
history, because right now tabs that are not "normal" project items,
e.g. FindAllReferences cannot be reopened

Release Notes:

- Added support for replacing the current preview tab when using code
navigation (`enable_preview_from_code_navigation`)
This commit is contained in:
Bennet Bo Fenner 2024-04-29 20:47:01 +02:00 committed by GitHub
parent 9ff847753e
commit 20625e98ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 120 additions and 60 deletions

View file

@ -623,21 +623,13 @@ impl Pane {
self.activate_item(index, focus_item, focus_item, cx);
existing_item
} else {
let mut destination_index = None;
if allow_preview {
// If we are opening a new item as preview and we have an existing preview tab, remove it.
if let Some(item_idx) = self.preview_item_idx() {
let prev_active_item_index = self.active_item_index;
self.remove_item(item_idx, false, false, cx);
self.active_item_index = prev_active_item_index;
// If the item is being opened as preview and we have an existing preview tab,
// open the new item in the position of the existing preview tab.
if item_idx < self.items.len() {
destination_index = Some(item_idx);
}
}
}
// If the item is being opened as preview and we have an existing preview tab,
// open the new item in the position of the existing preview tab.
let destination_index = if allow_preview {
self.close_current_preview_item(cx)
} else {
None
};
let new_item = build_item(cx);
@ -651,6 +643,22 @@ impl Pane {
}
}
pub fn close_current_preview_item(&mut self, cx: &mut ViewContext<Self>) -> Option<usize> {
let Some(item_idx) = self.preview_item_idx() else {
return None;
};
let prev_active_item_index = self.active_item_index;
self.remove_item(item_idx, false, false, cx);
self.active_item_index = prev_active_item_index;
if item_idx < self.items.len() {
Some(item_idx)
} else {
None
}
}
pub fn add_item(
&mut self,
item: Box<dyn ItemHandle>,