From cae9e733a18b9806f74b37c8b820d8cd14376d30 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Tue, 21 Feb 2023 17:25:28 -0800 Subject: [PATCH] Add more default impls to the item trait Change pane to not split if the underlying item cannot be cloned --- crates/workspace/src/item.rs | 62 ++++++++++++++++++++----------- crates/workspace/src/workspace.rs | 16 +++++--- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index 0e28976151..a80b9f8d83 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -49,9 +49,11 @@ pub trait Item: View { } fn tab_content(&self, detail: Option, style: &theme::Tab, cx: &AppContext) -> ElementBox; - fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project::Item)); - fn is_singleton(&self, cx: &AppContext) -> bool; - fn set_nav_history(&mut self, _: ItemNavHistory, _: &mut ViewContext); + fn for_each_project_item(&self, _: &AppContext, _: &mut dyn FnMut(usize, &dyn project::Item)) {} + fn is_singleton(&self, _cx: &AppContext) -> bool { + false + } + fn set_nav_history(&mut self, _: ItemNavHistory, _: &mut ViewContext) {} fn clone_on_split(&self, _workspace_id: WorkspaceId, _: &mut ViewContext) -> Option where Self: Sized, @@ -64,23 +66,31 @@ pub trait Item: View { fn has_conflict(&self, _: &AppContext) -> bool { false } - fn can_save(&self, cx: &AppContext) -> bool; + fn can_save(&self, _cx: &AppContext) -> bool { + false + } fn save( &mut self, - project: ModelHandle, - cx: &mut ViewContext, - ) -> Task>; + _project: ModelHandle, + _cx: &mut ViewContext, + ) -> Task> { + unimplemented!("save() must be implemented if can_save() returns true") + } fn save_as( &mut self, - project: ModelHandle, - abs_path: PathBuf, - cx: &mut ViewContext, - ) -> Task>; + _project: ModelHandle, + _abs_path: PathBuf, + _cx: &mut ViewContext, + ) -> Task> { + unimplemented!("save_as() must be implemented if can_save() returns true") + } fn reload( &mut self, - project: ModelHandle, - cx: &mut ViewContext, - ) -> Task>; + _project: ModelHandle, + _cx: &mut ViewContext, + ) -> Task> { + unimplemented!("reload() must be implemented if can_save() returns true") + } fn git_diff_recalc( &mut self, _project: ModelHandle, @@ -88,7 +98,9 @@ pub trait Item: View { ) -> Task> { Task::ready(Ok(())) } - fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]>; + fn to_item_events(_event: &Self::Event) -> SmallVec<[ItemEvent; 2]> { + SmallVec::new() + } fn should_close_item_on_event(_: &Self::Event) -> bool { false } @@ -124,15 +136,21 @@ pub trait Item: View { fn added_to_workspace(&mut self, _workspace: &mut Workspace, _cx: &mut ViewContext) {} - fn serialized_item_kind() -> Option<&'static str>; + fn serialized_item_kind() -> Option<&'static str> { + None + } fn deserialize( - project: ModelHandle, - workspace: WeakViewHandle, - workspace_id: WorkspaceId, - item_id: ItemId, - cx: &mut ViewContext, - ) -> Task>>; + _project: ModelHandle, + _workspace: WeakViewHandle, + _workspace_id: WorkspaceId, + _item_id: ItemId, + _cx: &mut ViewContext, + ) -> Task>> { + unimplemented!( + "deserialize() must be implemented if serialized_item_kind() returns Some(_)" + ) + } } pub trait ItemHandle: 'static + fmt::Debug { diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 21bbe283d7..35cccf4884 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -1589,13 +1589,17 @@ impl Workspace { } let item = pane.read(cx).active_item()?; - let new_pane = self.add_pane(cx); - if let Some(clone) = item.clone_on_split(self.database_id(), cx.as_mut()) { - Pane::add_item(self, &new_pane, clone, true, true, None, cx); - } - self.center.split(&pane, &new_pane, direction).unwrap(); + let maybe_pane_handle = + if let Some(clone) = item.clone_on_split(self.database_id(), cx.as_mut()) { + let new_pane = self.add_pane(cx); + Pane::add_item(self, &new_pane, clone, true, true, None, cx); + self.center.split(&pane, &new_pane, direction).unwrap(); + Some(new_pane) + } else { + None + }; cx.notify(); - Some(new_pane) + maybe_pane_handle } pub fn split_pane_with_item(&mut self, action: &SplitWithItem, cx: &mut ViewContext) {