Add MultiBufferItemHandle

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-02-09 17:09:03 -08:00
parent ca697e1bba
commit b67be5ded3
5 changed files with 74 additions and 16 deletions

View File

@ -538,10 +538,8 @@ impl workspace::Item for ProjectDiagnostics {
}
impl workspace::ItemView for ProjectDiagnosticsEditor {
type ItemHandle = ModelHandle<ProjectDiagnostics>;
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 {

View File

@ -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,

View File

@ -25,6 +25,12 @@ pub struct BufferItemHandle(pub ModelHandle<Buffer>);
#[derive(Clone)]
struct WeakBufferItemHandle(WeakModelHandle<Buffer>);
#[derive(Clone)]
pub struct MultiBufferItemHandle(pub ModelHandle<MultiBuffer>);
#[derive(Clone)]
struct WeakMultiBufferItemHandle(WeakModelHandle<MultiBuffer>);
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<RefCell<NavHistory>>,
cx: &mut MutableAppContext,
) -> Box<dyn ItemViewHandle> {
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<dyn ItemHandle> {
Box::new(self.clone())
}
fn to_any(&self) -> gpui::AnyModelHandle {
self.0.clone().into()
}
fn downgrade(&self) -> Box<dyn WeakItemHandle> {
Box::new(WeakMultiBufferItemHandle(self.0.downgrade()))
}
fn project_path(&self, _: &AppContext) -> Option<ProjectPath> {
None
}
fn id(&self) -> usize {
self.0.id()
}
}
impl WeakItemHandle for WeakBufferItemHandle {
fn upgrade(&self, cx: &AppContext) -> Option<Box<dyn ItemHandle>> {
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<Box<dyn ItemHandle>> {
self.0
.upgrade(cx)
.map(|buffer| Box::new(MultiBufferItemHandle(buffer)) as Box<dyn ItemHandle>)
}
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<dyn std::any::Any>, cx: &mut ViewContext<Self>) {

View File

@ -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();
}

View File

@ -150,11 +150,9 @@ pub trait Item: Entity + Sized {
}
pub trait ItemView: View {
type ItemHandle: ItemHandle;
fn deactivated(&mut self, _: &mut ViewContext<Self>) {}
fn navigate(&mut self, _: Box<dyn Any>, _: &mut ViewContext<Self>) {}
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<ProjectPath>;
fn clone_on_split(&self, _: &mut ViewContext<Self>) -> Option<Self>
@ -222,7 +220,7 @@ pub trait WeakItemHandle {
}
pub trait ItemViewHandle: 'static {
fn item_handle(&self, cx: &AppContext) -> Box<dyn 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<ProjectPath>;
fn boxed_clone(&self) -> Box<dyn ItemViewHandle>;
@ -354,8 +352,8 @@ impl dyn ItemViewHandle {
}
impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
fn item_handle(&self, cx: &AppContext) -> Box<dyn ItemHandle> {
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 {