Combine Workspace and WorkspaceView
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
co-authored by
Max Brunsfeld
parent
b801628230
commit
ed28bd3f95
@@ -1,11 +1,9 @@
|
||||
pub mod pane;
|
||||
pub mod pane_group;
|
||||
pub mod workspace;
|
||||
pub mod workspace_view;
|
||||
|
||||
pub use pane::*;
|
||||
pub use pane_group::*;
|
||||
pub use workspace::*;
|
||||
pub use workspace_view::*;
|
||||
|
||||
use crate::{
|
||||
@@ -68,9 +66,8 @@ fn open_paths(params: &OpenParams, app: &mut MutableAppContext) {
|
||||
log::info!("open new workspace");
|
||||
|
||||
// Add a new workspace if necessary
|
||||
let workspace = app.add_model(|ctx| Workspace::new(vec![], ctx));
|
||||
app.add_window(|ctx| {
|
||||
let view = WorkspaceView::new(workspace, params.settings.clone(), ctx);
|
||||
let mut view = WorkspaceView::new(0, params.settings.clone(), ctx);
|
||||
let open_paths = view.open_paths(¶ms.paths, ctx);
|
||||
ctx.foreground().spawn(open_paths).detach();
|
||||
view
|
||||
@@ -133,15 +130,7 @@ mod tests {
|
||||
let workspace_view_1 = app
|
||||
.root_view::<WorkspaceView>(app.window_ids().next().unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
workspace_view_1
|
||||
.read(app)
|
||||
.workspace
|
||||
.read(app)
|
||||
.worktrees()
|
||||
.len(),
|
||||
2
|
||||
);
|
||||
assert_eq!(workspace_view_1.read(app).worktrees().len(), 2);
|
||||
|
||||
app.dispatch_global_action(
|
||||
"workspace:open_paths",
|
||||
|
||||
@@ -1,194 +0,0 @@
|
||||
use super::ItemViewHandle;
|
||||
use crate::{
|
||||
editor::{Buffer, BufferView},
|
||||
settings::Settings,
|
||||
time::ReplicaId,
|
||||
watch,
|
||||
worktree::{Worktree, WorktreeHandle as _},
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
use gpui::{AppContext, Entity, ModelContext, ModelHandle};
|
||||
use smol::prelude::*;
|
||||
use std::{collections::hash_map::Entry, future};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
pub struct Workspace {
|
||||
replica_id: ReplicaId,
|
||||
worktrees: HashSet<ModelHandle<Worktree>>,
|
||||
buffers: HashMap<
|
||||
(usize, u64),
|
||||
postage::watch::Receiver<Option<Result<ModelHandle<Buffer>, Arc<anyhow::Error>>>>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl Workspace {
|
||||
pub fn new(paths: Vec<PathBuf>, ctx: &mut ModelContext<Self>) -> Self {
|
||||
let mut workspace = Self {
|
||||
replica_id: 0,
|
||||
worktrees: Default::default(),
|
||||
buffers: Default::default(),
|
||||
};
|
||||
workspace.open_paths(&paths, ctx);
|
||||
workspace
|
||||
}
|
||||
|
||||
pub fn worktrees(&self) -> &HashSet<ModelHandle<Worktree>> {
|
||||
&self.worktrees
|
||||
}
|
||||
|
||||
pub fn worktree_scans_complete(&self, ctx: &AppContext) -> impl Future<Output = ()> + 'static {
|
||||
let futures = self
|
||||
.worktrees
|
||||
.iter()
|
||||
.map(|worktree| worktree.read(ctx).scan_complete())
|
||||
.collect::<Vec<_>>();
|
||||
async move {
|
||||
for future in futures {
|
||||
future.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn contains_paths(&self, paths: &[PathBuf], app: &AppContext) -> bool {
|
||||
paths.iter().all(|path| self.contains_path(&path, app))
|
||||
}
|
||||
|
||||
pub fn contains_path(&self, path: &Path, app: &AppContext) -> bool {
|
||||
self.worktrees
|
||||
.iter()
|
||||
.any(|worktree| worktree.read(app).contains_abs_path(path))
|
||||
}
|
||||
|
||||
pub fn open_paths(
|
||||
&mut self,
|
||||
paths: &[PathBuf],
|
||||
ctx: &mut ModelContext<Self>,
|
||||
) -> Vec<(usize, Arc<Path>)> {
|
||||
paths
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(move |path| self.open_path(path, ctx))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn open_path(&mut self, path: PathBuf, ctx: &mut ModelContext<Self>) -> (usize, Arc<Path>) {
|
||||
for tree in self.worktrees.iter() {
|
||||
if let Ok(relative_path) = path.strip_prefix(tree.read(ctx).abs_path()) {
|
||||
return (tree.id(), relative_path.into());
|
||||
}
|
||||
}
|
||||
|
||||
let worktree = ctx.add_model(|ctx| Worktree::new(path.clone(), ctx));
|
||||
let worktree_id = worktree.id();
|
||||
ctx.observe(&worktree, Self::on_worktree_updated);
|
||||
self.worktrees.insert(worktree);
|
||||
ctx.notify();
|
||||
(worktree_id, Path::new("").into())
|
||||
}
|
||||
|
||||
pub fn open_entry(
|
||||
&mut self,
|
||||
(worktree_id, path): (usize, Arc<Path>),
|
||||
window_id: usize,
|
||||
settings: watch::Receiver<Settings>,
|
||||
ctx: &mut ModelContext<Self>,
|
||||
) -> LocalBoxFuture<'static, Result<Box<dyn ItemViewHandle>, Arc<anyhow::Error>>> {
|
||||
let worktree = match self.worktrees.get(&worktree_id).cloned() {
|
||||
Some(worktree) => worktree,
|
||||
None => {
|
||||
return future::ready(Err(Arc::new(anyhow!(
|
||||
"worktree {} does not exist",
|
||||
worktree_id
|
||||
))))
|
||||
.boxed_local();
|
||||
}
|
||||
};
|
||||
|
||||
let inode = match worktree.read(ctx).inode_for_path(&path) {
|
||||
Some(inode) => inode,
|
||||
None => {
|
||||
return future::ready(Err(Arc::new(anyhow!("path {:?} does not exist", path))))
|
||||
.boxed_local();
|
||||
}
|
||||
};
|
||||
|
||||
let file = match worktree.file(path.clone(), ctx.as_ref()) {
|
||||
Some(file) => file,
|
||||
None => {
|
||||
return future::ready(Err(Arc::new(anyhow!("path {:?} does not exist", path))))
|
||||
.boxed_local()
|
||||
}
|
||||
};
|
||||
|
||||
if let Entry::Vacant(entry) = self.buffers.entry((worktree_id, inode)) {
|
||||
let (mut tx, rx) = postage::watch::channel();
|
||||
entry.insert(rx);
|
||||
let history = file.load_history(ctx.as_ref());
|
||||
let replica_id = self.replica_id;
|
||||
let buffer = ctx
|
||||
.background_executor()
|
||||
.spawn(async move { Ok(Buffer::from_history(replica_id, history.await?)) });
|
||||
ctx.spawn(buffer, move |_, from_history_result, ctx| {
|
||||
*tx.borrow_mut() = Some(match from_history_result {
|
||||
Ok(buffer) => Ok(ctx.add_model(|_| buffer)),
|
||||
Err(error) => Err(Arc::new(error)),
|
||||
})
|
||||
})
|
||||
.detach()
|
||||
}
|
||||
|
||||
let mut watch = self.buffers.get(&(worktree_id, inode)).unwrap().clone();
|
||||
ctx.spawn(
|
||||
async move {
|
||||
loop {
|
||||
if let Some(load_result) = watch.borrow().as_ref() {
|
||||
return load_result.clone();
|
||||
}
|
||||
watch.next().await;
|
||||
}
|
||||
},
|
||||
move |_, load_result, ctx| {
|
||||
load_result.map(|buffer_handle| {
|
||||
Box::new(ctx.as_mut().add_view(window_id, |ctx| {
|
||||
BufferView::for_buffer(buffer_handle, Some(file), settings, ctx)
|
||||
})) as Box<dyn ItemViewHandle>
|
||||
})
|
||||
},
|
||||
)
|
||||
.boxed_local()
|
||||
}
|
||||
|
||||
fn on_worktree_updated(&mut self, _: ModelHandle<Worktree>, ctx: &mut ModelContext<Self>) {
|
||||
ctx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity for Workspace {
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub trait WorkspaceHandle {
|
||||
fn file_entries(&self, app: &AppContext) -> Vec<(usize, Arc<Path>)>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl WorkspaceHandle for ModelHandle<Workspace> {
|
||||
fn file_entries(&self, app: &AppContext) -> Vec<(usize, Arc<Path>)> {
|
||||
self.read(app)
|
||||
.worktrees()
|
||||
.iter()
|
||||
.flat_map(|tree| {
|
||||
let tree_id = tree.id();
|
||||
tree.read(app)
|
||||
.files(0)
|
||||
.map(move |f| (tree_id, f.path().clone()))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,12 @@
|
||||
use super::{pane, Pane, PaneGroup, SplitDirection, Workspace};
|
||||
use crate::{settings::Settings, watch};
|
||||
use super::{pane, Pane, PaneGroup, SplitDirection};
|
||||
use crate::{
|
||||
editor::{Buffer, BufferView},
|
||||
settings::Settings,
|
||||
time::ReplicaId,
|
||||
watch,
|
||||
worktree::{Worktree, WorktreeHandle},
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
use futures_core::{future::LocalBoxFuture, Future};
|
||||
use gpui::{
|
||||
color::rgbu, elements::*, json::to_string_pretty, keymap::Binding, AnyViewHandle, AppContext,
|
||||
@@ -7,8 +14,10 @@ use gpui::{
|
||||
ViewHandle,
|
||||
};
|
||||
use log::error;
|
||||
use smol::prelude::*;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
collections::{hash_map::Entry, HashMap, HashSet},
|
||||
future,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
@@ -123,23 +132,26 @@ pub struct State {
|
||||
}
|
||||
|
||||
pub struct WorkspaceView {
|
||||
pub workspace: ModelHandle<Workspace>,
|
||||
pub settings: watch::Receiver<Settings>,
|
||||
modal: Option<AnyViewHandle>,
|
||||
center: PaneGroup,
|
||||
panes: Vec<ViewHandle<Pane>>,
|
||||
active_pane: ViewHandle<Pane>,
|
||||
loading_entries: HashSet<(usize, Arc<Path>)>,
|
||||
replica_id: ReplicaId,
|
||||
worktrees: HashSet<ModelHandle<Worktree>>,
|
||||
buffers: HashMap<
|
||||
(usize, u64),
|
||||
postage::watch::Receiver<Option<Result<ModelHandle<Buffer>, Arc<anyhow::Error>>>>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl WorkspaceView {
|
||||
pub fn new(
|
||||
workspace: ModelHandle<Workspace>,
|
||||
replica_id: ReplicaId,
|
||||
settings: watch::Receiver<Settings>,
|
||||
ctx: &mut ViewContext<Self>,
|
||||
) -> Self {
|
||||
ctx.observe(&workspace, Self::workspace_updated);
|
||||
|
||||
let pane = ctx.add_view(|_| Pane::new(settings.clone()));
|
||||
let pane_id = pane.id();
|
||||
ctx.subscribe_to_view(&pane, move |me, _, event, ctx| {
|
||||
@@ -148,28 +160,52 @@ impl WorkspaceView {
|
||||
ctx.focus(&pane);
|
||||
|
||||
WorkspaceView {
|
||||
workspace,
|
||||
modal: None,
|
||||
center: PaneGroup::new(pane.id()),
|
||||
panes: vec![pane.clone()],
|
||||
active_pane: pane.clone(),
|
||||
loading_entries: HashSet::new(),
|
||||
settings,
|
||||
replica_id,
|
||||
worktrees: Default::default(),
|
||||
buffers: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn worktrees(&self) -> &HashSet<ModelHandle<Worktree>> {
|
||||
&self.worktrees
|
||||
}
|
||||
|
||||
pub fn contains_paths(&self, paths: &[PathBuf], app: &AppContext) -> bool {
|
||||
self.workspace.read(app).contains_paths(paths, app)
|
||||
paths.iter().all(|path| self.contains_path(&path, app))
|
||||
}
|
||||
|
||||
pub fn contains_path(&self, path: &Path, app: &AppContext) -> bool {
|
||||
self.worktrees
|
||||
.iter()
|
||||
.any(|worktree| worktree.read(app).contains_abs_path(path))
|
||||
}
|
||||
|
||||
pub fn worktree_scans_complete(&self, ctx: &AppContext) -> impl Future<Output = ()> + 'static {
|
||||
let futures = self
|
||||
.worktrees
|
||||
.iter()
|
||||
.map(|worktree| worktree.read(ctx).scan_complete())
|
||||
.collect::<Vec<_>>();
|
||||
async move {
|
||||
for future in futures {
|
||||
future.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_paths(
|
||||
&self,
|
||||
&mut self,
|
||||
paths: &[PathBuf],
|
||||
ctx: &mut ViewContext<Self>,
|
||||
) -> impl Future<Output = ()> {
|
||||
let entries = self
|
||||
.workspace
|
||||
.update(ctx, |workspace, ctx| workspace.open_paths(paths, ctx));
|
||||
let entries = self.open_paths2(paths, ctx);
|
||||
|
||||
let bg = ctx.background_executor().clone();
|
||||
let tasks = paths
|
||||
.iter()
|
||||
@@ -197,6 +233,33 @@ impl WorkspaceView {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_paths2(
|
||||
&mut self,
|
||||
paths: &[PathBuf],
|
||||
ctx: &mut ViewContext<Self>,
|
||||
) -> Vec<(usize, Arc<Path>)> {
|
||||
paths
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(move |path| self.open_path(path, ctx))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn open_path(&mut self, path: PathBuf, ctx: &mut ViewContext<Self>) -> (usize, Arc<Path>) {
|
||||
for tree in self.worktrees.iter() {
|
||||
if let Ok(relative_path) = path.strip_prefix(tree.read(ctx).abs_path()) {
|
||||
return (tree.id(), relative_path.into());
|
||||
}
|
||||
}
|
||||
|
||||
let worktree = ctx.add_model(|ctx| Worktree::new(path.clone(), ctx));
|
||||
let worktree_id = worktree.id();
|
||||
ctx.observe_model(&worktree, |_, _, ctx| ctx.notify());
|
||||
self.worktrees.insert(worktree);
|
||||
ctx.notify();
|
||||
(worktree_id, Path::new("").into())
|
||||
}
|
||||
|
||||
pub fn toggle_modal<V, F>(&mut self, ctx: &mut ViewContext<Self>, add_view: F)
|
||||
where
|
||||
V: 'static + View,
|
||||
@@ -244,9 +307,7 @@ impl WorkspaceView {
|
||||
self.loading_entries.insert(entry.clone());
|
||||
|
||||
let window_id = ctx.window_id();
|
||||
let future = self.workspace.update(ctx, |workspace, ctx| {
|
||||
workspace.open_entry(entry.clone(), window_id, self.settings.clone(), ctx)
|
||||
});
|
||||
let future = self.open_entry2(entry.clone(), window_id, self.settings.clone(), ctx);
|
||||
|
||||
Some(ctx.spawn(future, move |me, item_view, ctx| {
|
||||
me.loading_entries.remove(&entry);
|
||||
@@ -257,6 +318,78 @@ impl WorkspaceView {
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn open_entry2(
|
||||
&mut self,
|
||||
(worktree_id, path): (usize, Arc<Path>),
|
||||
window_id: usize,
|
||||
settings: watch::Receiver<Settings>,
|
||||
ctx: &mut ViewContext<Self>,
|
||||
) -> LocalBoxFuture<'static, Result<Box<dyn ItemViewHandle>, Arc<anyhow::Error>>> {
|
||||
let worktree = match self.worktrees.get(&worktree_id).cloned() {
|
||||
Some(worktree) => worktree,
|
||||
None => {
|
||||
return future::ready(Err(Arc::new(anyhow!(
|
||||
"worktree {} does not exist",
|
||||
worktree_id
|
||||
))))
|
||||
.boxed_local();
|
||||
}
|
||||
};
|
||||
|
||||
let inode = match worktree.read(ctx).inode_for_path(&path) {
|
||||
Some(inode) => inode,
|
||||
None => {
|
||||
return future::ready(Err(Arc::new(anyhow!("path {:?} does not exist", path))))
|
||||
.boxed_local();
|
||||
}
|
||||
};
|
||||
|
||||
let file = match worktree.file(path.clone(), ctx.as_ref()) {
|
||||
Some(file) => file,
|
||||
None => {
|
||||
return future::ready(Err(Arc::new(anyhow!("path {:?} does not exist", path))))
|
||||
.boxed_local()
|
||||
}
|
||||
};
|
||||
|
||||
if let Entry::Vacant(entry) = self.buffers.entry((worktree_id, inode)) {
|
||||
let (mut tx, rx) = postage::watch::channel();
|
||||
entry.insert(rx);
|
||||
let history = file.load_history(ctx.as_ref());
|
||||
let replica_id = self.replica_id;
|
||||
let buffer = ctx
|
||||
.background_executor()
|
||||
.spawn(async move { Ok(Buffer::from_history(replica_id, history.await?)) });
|
||||
ctx.spawn(buffer, move |_, from_history_result, ctx| {
|
||||
*tx.borrow_mut() = Some(match from_history_result {
|
||||
Ok(buffer) => Ok(ctx.add_model(|_| buffer)),
|
||||
Err(error) => Err(Arc::new(error)),
|
||||
})
|
||||
})
|
||||
.detach()
|
||||
}
|
||||
|
||||
let mut watch = self.buffers.get(&(worktree_id, inode)).unwrap().clone();
|
||||
ctx.spawn(
|
||||
async move {
|
||||
loop {
|
||||
if let Some(load_result) = watch.borrow().as_ref() {
|
||||
return load_result.clone();
|
||||
}
|
||||
watch.next().await;
|
||||
}
|
||||
},
|
||||
move |_, load_result, ctx| {
|
||||
load_result.map(|buffer_handle| {
|
||||
Box::new(ctx.as_mut().add_view(window_id, |ctx| {
|
||||
BufferView::for_buffer(buffer_handle, Some(file), settings, ctx)
|
||||
})) as Box<dyn ItemViewHandle>
|
||||
})
|
||||
},
|
||||
)
|
||||
.boxed_local()
|
||||
}
|
||||
|
||||
pub fn save_active_item(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
||||
self.active_pane.update(ctx, |pane, ctx| {
|
||||
if let Some(item) = pane.active_item() {
|
||||
@@ -288,10 +421,6 @@ impl WorkspaceView {
|
||||
};
|
||||
}
|
||||
|
||||
fn workspace_updated(&mut self, _: ModelHandle<Workspace>, ctx: &mut ViewContext<Self>) {
|
||||
ctx.notify();
|
||||
}
|
||||
|
||||
fn add_pane(&mut self, ctx: &mut ViewContext<Self>) -> ViewHandle<Pane> {
|
||||
let pane = ctx.add_view(|_| Pane::new(self.settings.clone()));
|
||||
let pane_id = pane.id();
|
||||
@@ -403,10 +532,31 @@ impl View for WorkspaceView {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub trait WorkspaceViewHandle {
|
||||
fn file_entries(&self, app: &AppContext) -> Vec<(usize, Arc<Path>)>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl WorkspaceViewHandle for ViewHandle<WorkspaceView> {
|
||||
fn file_entries(&self, app: &AppContext) -> Vec<(usize, Arc<Path>)> {
|
||||
self.read(app)
|
||||
.worktrees()
|
||||
.iter()
|
||||
.flat_map(|tree| {
|
||||
let tree_id = tree.id();
|
||||
tree.read(app)
|
||||
.files(0)
|
||||
.map(move |f| (tree_id, f.path().clone()))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{pane, Workspace, WorkspaceView};
|
||||
use crate::{editor::BufferView, settings, test::temp_tree, workspace::WorkspaceHandle as _};
|
||||
use super::{pane, WorkspaceView, WorkspaceViewHandle as _};
|
||||
use crate::{editor::BufferView, settings, test::temp_tree};
|
||||
use gpui::App;
|
||||
use serde_json::json;
|
||||
use std::{collections::HashSet, os::unix};
|
||||
@@ -423,7 +573,13 @@ mod tests {
|
||||
}));
|
||||
|
||||
let settings = settings::channel(&app.font_cache()).unwrap().1;
|
||||
let workspace = app.add_model(|ctx| Workspace::new(vec![dir.path().into()], ctx));
|
||||
|
||||
let (_, workspace) = app.add_window(|ctx| {
|
||||
let mut workspace = WorkspaceView::new(0, settings, ctx);
|
||||
smol::block_on(workspace.open_paths(&[dir.path().into()], ctx));
|
||||
workspace
|
||||
});
|
||||
|
||||
app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx))
|
||||
.await;
|
||||
let entries = app.read(|ctx| workspace.file_entries(ctx));
|
||||
@@ -431,12 +587,10 @@ mod tests {
|
||||
let file2 = entries[1].clone();
|
||||
let file3 = entries[2].clone();
|
||||
|
||||
let (_, workspace_view) =
|
||||
app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx));
|
||||
let pane = app.read(|ctx| workspace_view.read(ctx).active_pane().clone());
|
||||
let pane = app.read(|ctx| workspace.read(ctx).active_pane().clone());
|
||||
|
||||
// Open the first entry
|
||||
workspace_view
|
||||
workspace
|
||||
.update(&mut app, |w, ctx| w.open_entry(file1.clone(), ctx))
|
||||
.unwrap()
|
||||
.await;
|
||||
@@ -450,7 +604,7 @@ mod tests {
|
||||
});
|
||||
|
||||
// Open the second entry
|
||||
workspace_view
|
||||
workspace
|
||||
.update(&mut app, |w, ctx| w.open_entry(file2.clone(), ctx))
|
||||
.unwrap()
|
||||
.await;
|
||||
@@ -464,7 +618,7 @@ mod tests {
|
||||
});
|
||||
|
||||
// Open the first entry again. The existing pane item is activated.
|
||||
workspace_view.update(&mut app, |w, ctx| {
|
||||
workspace.update(&mut app, |w, ctx| {
|
||||
assert!(w.open_entry(file1.clone(), ctx).is_none())
|
||||
});
|
||||
app.read(|ctx| {
|
||||
@@ -477,7 +631,7 @@ mod tests {
|
||||
});
|
||||
|
||||
// Open the third entry twice concurrently. Only one pane item is added.
|
||||
workspace_view
|
||||
workspace
|
||||
.update(&mut app, |w, ctx| {
|
||||
let task = w.open_entry(file3.clone(), ctx).unwrap();
|
||||
assert!(w.open_entry(file3.clone(), ctx).is_none());
|
||||
@@ -505,22 +659,24 @@ mod tests {
|
||||
"b.txt": "",
|
||||
}));
|
||||
|
||||
let workspace = app.add_model(|ctx| Workspace::new(vec![dir1.path().into()], ctx));
|
||||
let settings = settings::channel(&app.font_cache()).unwrap().1;
|
||||
let (_, workspace_view) =
|
||||
app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx));
|
||||
let (_, workspace) = app.add_window(|ctx| {
|
||||
let mut workspace = WorkspaceView::new(0, settings, ctx);
|
||||
workspace.open_path(dir1.path().into(), ctx);
|
||||
workspace
|
||||
});
|
||||
app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx))
|
||||
.await;
|
||||
|
||||
// Open a file within an existing worktree.
|
||||
app.update(|ctx| {
|
||||
workspace_view.update(ctx, |view, ctx| {
|
||||
workspace.update(ctx, |view, ctx| {
|
||||
view.open_paths(&[dir1.path().join("a.txt")], ctx)
|
||||
})
|
||||
})
|
||||
.await;
|
||||
app.read(|ctx| {
|
||||
workspace_view
|
||||
workspace
|
||||
.read(ctx)
|
||||
.active_pane()
|
||||
.read(ctx)
|
||||
@@ -532,7 +688,7 @@ mod tests {
|
||||
|
||||
// Open a file outside of any existing worktree.
|
||||
app.update(|ctx| {
|
||||
workspace_view.update(ctx, |view, ctx| {
|
||||
workspace.update(ctx, |view, ctx| {
|
||||
view.open_paths(&[dir2.path().join("b.txt")], ctx)
|
||||
})
|
||||
})
|
||||
@@ -552,7 +708,7 @@ mod tests {
|
||||
);
|
||||
});
|
||||
app.read(|ctx| {
|
||||
workspace_view
|
||||
workspace
|
||||
.read(ctx)
|
||||
.active_pane()
|
||||
.read(ctx)
|
||||
@@ -577,14 +733,18 @@ mod tests {
|
||||
let dir = temp_dir.path();
|
||||
unix::fs::symlink(dir.join("hello.txt"), dir.join("hola.txt")).unwrap();
|
||||
|
||||
let workspace = app.add_model(|ctx| Workspace::new(vec![dir.into()], ctx));
|
||||
let settings = settings::channel(&app.font_cache()).unwrap().1;
|
||||
let (_, workspace_view) =
|
||||
app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx));
|
||||
let (_, workspace) = app.add_window(|ctx| {
|
||||
let mut workspace = WorkspaceView::new(0, settings, ctx);
|
||||
workspace.open_path(dir.into(), ctx);
|
||||
workspace
|
||||
});
|
||||
app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx))
|
||||
.await;
|
||||
|
||||
// Simultaneously open both the original file and the symlink to the same file.
|
||||
app.update(|ctx| {
|
||||
workspace_view.update(ctx, |view, ctx| {
|
||||
workspace.update(ctx, |view, ctx| {
|
||||
view.open_paths(&[dir.join("hello.txt"), dir.join("hola.txt")], ctx)
|
||||
})
|
||||
})
|
||||
@@ -592,7 +752,7 @@ mod tests {
|
||||
|
||||
// The same content shows up with two different editors.
|
||||
let buffer_views = app.read(|ctx| {
|
||||
workspace_view
|
||||
workspace
|
||||
.read(ctx)
|
||||
.active_pane()
|
||||
.read(ctx)
|
||||
@@ -635,17 +795,19 @@ mod tests {
|
||||
}));
|
||||
|
||||
let settings = settings::channel(&app.font_cache()).unwrap().1;
|
||||
let workspace = app.add_model(|ctx| Workspace::new(vec![dir.path().into()], ctx));
|
||||
let (window_id, workspace) = app.add_window(|ctx| {
|
||||
let mut workspace = WorkspaceView::new(0, settings, ctx);
|
||||
workspace.open_path(dir.path().into(), ctx);
|
||||
workspace
|
||||
});
|
||||
app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx))
|
||||
.await;
|
||||
let entries = app.read(|ctx| workspace.file_entries(ctx));
|
||||
let file1 = entries[0].clone();
|
||||
|
||||
let (window_id, workspace_view) =
|
||||
app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx));
|
||||
let pane_1 = app.read(|ctx| workspace_view.read(ctx).active_pane().clone());
|
||||
let pane_1 = app.read(|ctx| workspace.read(ctx).active_pane().clone());
|
||||
|
||||
workspace_view
|
||||
workspace
|
||||
.update(&mut app, |w, ctx| w.open_entry(file1.clone(), ctx))
|
||||
.unwrap()
|
||||
.await;
|
||||
@@ -658,14 +820,14 @@ mod tests {
|
||||
|
||||
app.dispatch_action(window_id, vec![pane_1.id()], "pane:split_right", ());
|
||||
app.update(|ctx| {
|
||||
let pane_2 = workspace_view.read(ctx).active_pane().clone();
|
||||
let pane_2 = workspace.read(ctx).active_pane().clone();
|
||||
assert_ne!(pane_1, pane_2);
|
||||
|
||||
let pane2_item = pane_2.read(ctx).active_item().unwrap();
|
||||
assert_eq!(pane2_item.entry_id(ctx.as_ref()), Some(file1.clone()));
|
||||
|
||||
ctx.dispatch_action(window_id, vec![pane_2.id()], "pane:close_active_item", ());
|
||||
let workspace_view = workspace_view.read(ctx);
|
||||
let workspace_view = workspace.read(ctx);
|
||||
assert_eq!(workspace_view.panes.len(), 1);
|
||||
assert_eq!(workspace_view.active_pane(), &pane_1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user