Don't assume that cloning on split will reuse the same underlying model

Antonio Scandurra and Max Brunsfeld created

Co-Authored-By: Max Brunsfeld <max@zed.dev>

Change summary

crates/diagnostics/src/diagnostics.rs | 6 +++---
crates/editor/src/items.rs            | 6 +++---
crates/find/src/project_find.rs       | 6 +++---
crates/workspace/src/pane.rs          | 2 +-
crates/workspace/src/workspace.rs     | 9 +++++----
5 files changed, 15 insertions(+), 14 deletions(-)

Detailed changes

crates/diagnostics/src/diagnostics.rs 🔗

@@ -28,7 +28,7 @@ use std::{
     sync::Arc,
 };
 use util::TryFutureExt;
-use workspace::{ItemNavHistory, ItemViewHandle as _, Workspace};
+use workspace::{ItemHandle, ItemNavHistory, ItemViewHandle as _, Workspace};
 
 action!(Deploy);
 action!(OpenExcerpts);
@@ -536,8 +536,8 @@ impl workspace::Item for ProjectDiagnostics {
 }
 
 impl workspace::ItemView for ProjectDiagnosticsEditor {
-    fn item_id(&self, _: &AppContext) -> usize {
-        self.model.id()
+    fn item(&self, _: &AppContext) -> Box<dyn ItemHandle> {
+        Box::new(self.model.clone())
     }
 
     fn tab_content(&self, style: &theme::Tab, _: &AppContext) -> ElementBox {

crates/editor/src/items.rs 🔗

@@ -158,11 +158,11 @@ impl WeakItemHandle for WeakMultiBufferItemHandle {
 }
 
 impl ItemView for Editor {
-    fn item_id(&self, cx: &AppContext) -> usize {
+    fn item(&self, cx: &AppContext) -> Box<dyn ItemHandle> {
         if let Some(buffer) = self.buffer.read(cx).as_singleton() {
-            buffer.id()
+            Box::new(BufferItemHandle(buffer))
         } else {
-            self.buffer.id()
+            Box::new(MultiBufferItemHandle(self.buffer.clone()))
         }
     }
 

crates/find/src/project_find.rs 🔗

@@ -12,7 +12,7 @@ use std::{
     ops::Range,
     path::PathBuf,
 };
-use workspace::{Item, ItemNavHistory, ItemView, Settings, Workspace};
+use workspace::{Item, ItemHandle, ItemNavHistory, ItemView, Settings, Workspace};
 
 action!(Deploy);
 action!(Search);
@@ -223,8 +223,8 @@ impl ItemView for ProjectFindView {
             .update(cx, |editor, cx| editor.deactivated(cx));
     }
 
-    fn item_id(&self, _: &gpui::AppContext) -> usize {
-        self.model.id()
+    fn item(&self, _: &gpui::AppContext) -> Box<dyn ItemHandle> {
+        Box::new(self.model.clone())
     }
 
     fn tab_content(&self, style: &theme::Tab, _: &gpui::AppContext) -> ElementBox {

crates/workspace/src/pane.rs 🔗

@@ -283,7 +283,7 @@ impl Pane {
         item_view.added_to_pane(cx);
         let item_idx = cmp::min(self.active_item_index + 1, self.item_views.len());
         self.item_views
-            .insert(item_idx, (item_view.item_id(cx), item_view));
+            .insert(item_idx, (item_view.item(cx).id(), item_view));
         self.activate_item(item_idx, cx);
         cx.notify();
     }

crates/workspace/src/workspace.rs 🔗

@@ -153,7 +153,7 @@ pub trait Item: Entity + Sized {
 pub trait ItemView: View {
     fn deactivated(&mut self, _: &mut ViewContext<Self>) {}
     fn navigate(&mut self, _: Box<dyn Any>, _: &mut ViewContext<Self>) {}
-    fn item_id(&self, cx: &AppContext) -> usize;
+    fn item(&self, cx: &AppContext) -> Box<dyn ItemHandle>;
     fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox;
     fn project_path(&self, cx: &AppContext) -> Option<ProjectPath>;
     fn clone_on_split(&self, _: ItemNavHistory, _: &mut ViewContext<Self>) -> Option<Self>
@@ -225,7 +225,7 @@ pub trait WeakItemHandle {
 }
 
 pub trait ItemViewHandle: 'static {
-    fn item_id(&self, cx: &AppContext) -> usize;
+    fn item(&self, cx: &AppContext) -> Box<dyn ItemHandle>;
     fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox;
     fn project_path(&self, cx: &AppContext) -> Option<ProjectPath>;
     fn boxed_clone(&self) -> Box<dyn ItemViewHandle>;
@@ -361,8 +361,8 @@ impl dyn ItemViewHandle {
 }
 
 impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
-    fn item_id(&self, cx: &AppContext) -> usize {
-        self.read(cx).item_id(cx)
+    fn item(&self, cx: &AppContext) -> Box<dyn ItemHandle> {
+        self.read(cx).item(cx)
     }
 
     fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox {
@@ -1059,6 +1059,7 @@ impl Workspace {
         if let Some(item) = pane.read(cx).active_item() {
             let nav_history = new_pane.read(cx).nav_history().clone();
             if let Some(clone) = item.clone_on_split(nav_history, cx.as_mut()) {
+                self.items.insert(clone.item(cx).downgrade());
                 new_pane.update(cx, |new_pane, cx| new_pane.add_item_view(clone, cx));
             }
         }