From b67be5ded31105b89e64ef2eba8ee981c92583d7 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 9 Feb 2022 17:09:03 -0800 Subject: [PATCH] Add MultiBufferItemHandle Co-Authored-By: Nathan Sobo --- crates/diagnostics/src/diagnostics.rs | 6 +-- crates/editor/src/editor.rs | 2 +- crates/editor/src/items.rs | 70 +++++++++++++++++++++++++-- crates/workspace/src/pane.rs | 2 +- crates/workspace/src/workspace.rs | 10 ++-- 5 files changed, 74 insertions(+), 16 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 3438cddd6d..ee2b98d569 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -538,10 +538,8 @@ impl workspace::Item for ProjectDiagnostics { } impl workspace::ItemView for ProjectDiagnosticsEditor { - type ItemHandle = ModelHandle; - - fn item_handle(&self, _: &AppContext) -> Self::ItemHandle { - self.model.clone() + fn item_id(&self, _: &AppContext) -> usize { + self.model.id() } fn tab_content(&self, style: &theme::Tab, _: &AppContext) -> ElementBox { diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 6044e8d548..e607c1779a 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -27,7 +27,7 @@ use gpui::{ text_layout, AppContext, ClipboardItem, Element, ElementBox, Entity, ModelHandle, MutableAppContext, RenderContext, Task, View, ViewContext, WeakModelHandle, WeakViewHandle, }; -use items::BufferItemHandle; +use items::{BufferItemHandle, MultiBufferItemHandle}; use itertools::Itertools as _; use language::{ AnchorRangeExt as _, BracketPair, Buffer, CodeAction, Completion, CompletionLabel, Diagnostic, diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 0aeb32dbd0..9f868c2764 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -25,6 +25,12 @@ pub struct BufferItemHandle(pub ModelHandle); #[derive(Clone)] struct WeakBufferItemHandle(WeakModelHandle); +#[derive(Clone)] +pub struct MultiBufferItemHandle(pub ModelHandle); + +#[derive(Clone)] +struct WeakMultiBufferItemHandle(WeakModelHandle); + impl PathOpener for BufferOpener { fn open( &self, @@ -87,6 +93,48 @@ impl ItemHandle for BufferItemHandle { } } +impl ItemHandle for MultiBufferItemHandle { + fn add_view( + &self, + window_id: usize, + workspace: &Workspace, + nav_history: Rc>, + cx: &mut MutableAppContext, + ) -> Box { + let weak_buffer = self.0.downgrade(); + Box::new(cx.add_view(window_id, |cx| { + let mut editor = Editor::for_buffer( + self.0.clone(), + crate::settings_builder(weak_buffer, workspace.settings()), + Some(workspace.project().clone()), + cx, + ); + editor.nav_history = Some(ItemNavHistory::new(nav_history, &cx.handle())); + editor + })) + } + + fn boxed_clone(&self) -> Box { + Box::new(self.clone()) + } + + fn to_any(&self) -> gpui::AnyModelHandle { + self.0.clone().into() + } + + fn downgrade(&self) -> Box { + Box::new(WeakMultiBufferItemHandle(self.0.downgrade())) + } + + fn project_path(&self, _: &AppContext) -> Option { + None + } + + fn id(&self) -> usize { + self.0.id() + } +} + impl WeakItemHandle for WeakBufferItemHandle { fn upgrade(&self, cx: &AppContext) -> Option> { self.0 @@ -99,11 +147,25 @@ impl WeakItemHandle for WeakBufferItemHandle { } } -impl ItemView for Editor { - type ItemHandle = BufferItemHandle; +impl WeakItemHandle for WeakMultiBufferItemHandle { + fn upgrade(&self, cx: &AppContext) -> Option> { + self.0 + .upgrade(cx) + .map(|buffer| Box::new(MultiBufferItemHandle(buffer)) as Box) + } - fn item_handle(&self, cx: &AppContext) -> Self::ItemHandle { - BufferItemHandle(self.buffer.read(cx).as_singleton().unwrap()) + fn id(&self) -> usize { + self.0.id() + } +} + +impl ItemView for Editor { + fn item_id(&self, cx: &AppContext) -> usize { + if let Some(buffer) = self.buffer.read(cx).as_singleton() { + buffer.id() + } else { + self.buffer.id() + } } fn navigate(&mut self, data: Box, cx: &mut ViewContext) { diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 05b7b7e19b..fc2681161c 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -279,7 +279,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_handle(cx).id(), item_view)); + .insert(item_idx, (item_view.item_id(cx), item_view)); self.activate_item(item_idx, cx); cx.notify(); } diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 86d5271224..c52e55141d 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -150,11 +150,9 @@ pub trait Item: Entity + Sized { } pub trait ItemView: View { - type ItemHandle: ItemHandle; - fn deactivated(&mut self, _: &mut ViewContext) {} fn navigate(&mut self, _: Box, _: &mut ViewContext) {} - fn item_handle(&self, cx: &AppContext) -> Self::ItemHandle; + fn item_id(&self, cx: &AppContext) -> usize; fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox; fn project_path(&self, cx: &AppContext) -> Option; fn clone_on_split(&self, _: &mut ViewContext) -> Option @@ -222,7 +220,7 @@ pub trait WeakItemHandle { } pub trait ItemViewHandle: 'static { - fn item_handle(&self, cx: &AppContext) -> Box; + fn item_id(&self, cx: &AppContext) -> usize; fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox; fn project_path(&self, cx: &AppContext) -> Option; fn boxed_clone(&self) -> Box; @@ -354,8 +352,8 @@ impl dyn ItemViewHandle { } impl ItemViewHandle for ViewHandle { - fn item_handle(&self, cx: &AppContext) -> Box { - Box::new(self.read(cx).item_handle(cx)) + fn item_id(&self, cx: &AppContext) -> usize { + self.read(cx).item_id(cx) } fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox {