Start work on opening files

This commit is contained in:
Max Brunsfeld
2021-04-28 17:46:27 -07:00
parent 3d6336b12a
commit 88b88a8067
6 changed files with 132 additions and 23 deletions
+7 -3
View File
@@ -52,7 +52,7 @@ fn open_paths(params: &OpenParams, app: &mut MutableAppContext) {
if let Some(handle) = app.root_view::<WorkspaceView>(window_id) {
if handle.update(app, |view, ctx| {
if view.contains_paths(&params.paths, ctx.as_ref()) {
view.open_paths(&params.paths, ctx.as_mut());
view.open_paths(&params.paths, ctx);
log::info!("open paths on existing workspace");
true
} else {
@@ -67,8 +67,12 @@ 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(params.paths.clone(), ctx));
app.add_window(|ctx| WorkspaceView::new(workspace, params.settings.clone(), ctx));
let workspace = app.add_model(|ctx| Workspace::new(vec![], ctx));
app.add_window(|ctx| {
let view = WorkspaceView::new(workspace, params.settings.clone(), ctx);
view.open_paths(&params.paths, ctx);
view
});
}
fn quit(_: &(), app: &mut MutableAppContext) {
+16 -9
View File
@@ -117,23 +117,31 @@ impl Workspace {
.any(|worktree| worktree.read(app).contains_abs_path(path))
}
pub fn open_paths(&mut self, paths: &[PathBuf], ctx: &mut ModelContext<Self>) {
for path in paths.iter().cloned() {
self.open_path(path, ctx);
}
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()
}
pub fn open_path<'a>(&'a mut self, path: PathBuf, ctx: &mut ModelContext<Self>) {
fn open_path(&mut self, path: PathBuf, ctx: &mut ModelContext<Self>) -> (usize, Arc<Path>) {
for tree in self.worktrees.iter() {
if tree.read(ctx).contains_abs_path(&path) {
return;
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, ctx));
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(
@@ -174,7 +182,6 @@ impl Workspace {
let replica_id = self.replica_id;
let file = worktree.file(path.clone(), ctx.as_ref())?;
let history = file.load_history(ctx.as_ref());
// let buffer = async move { Ok(Buffer::from_history(replica_id, file, history.await?)) };
let (mut tx, rx) = watch::channel(None);
self.items.insert(item_key, OpenedItem::Loading(rx));
+79 -3
View File
@@ -161,9 +161,23 @@ impl WorkspaceView {
self.workspace.read(app).contains_paths(paths, app)
}
pub fn open_paths(&self, paths: &[PathBuf], app: &mut MutableAppContext) {
self.workspace
.update(app, |workspace, ctx| workspace.open_paths(paths, ctx));
pub fn open_paths(&self, paths: &[PathBuf], ctx: &mut ViewContext<Self>) {
let entries = self
.workspace
.update(ctx, |workspace, ctx| workspace.open_paths(paths, ctx));
for (i, entry) in entries.into_iter().enumerate() {
let path = paths[i].clone();
ctx.spawn(
ctx.background_executor()
.spawn(async move { path.is_file() }),
|me, is_file, ctx| {
if is_file {
me.open_entry(entry, ctx)
}
},
)
.detach();
}
}
pub fn toggle_modal<V, F>(&mut self, ctx: &mut ViewContext<Self>, add_view: F)
@@ -382,6 +396,7 @@ mod tests {
use crate::{settings, test::temp_tree, workspace::WorkspaceHandle as _};
use gpui::App;
use serde_json::json;
use std::collections::HashSet;
#[test]
fn test_open_entry() {
@@ -444,6 +459,67 @@ mod tests {
});
}
#[test]
fn test_open_paths() {
App::test_async((), |mut app| async move {
let dir1 = temp_tree(json!({
"a.txt": "",
}));
let dir2 = temp_tree(json!({
"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));
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| {
view.open_paths(&[dir1.path().join("a.txt")], ctx);
});
});
workspace_view
.condition(&app, |view, ctx| {
view.active_pane()
.read(ctx)
.active_item()
.map_or(false, |item| item.title(&ctx) == "a.txt")
})
.await;
// Open a file outside of any existing worktree.
app.update(|ctx| {
workspace_view.update(ctx, |view, ctx| {
view.open_paths(&[dir2.path().join("b.txt")], ctx);
});
let worktree_roots = workspace
.read(ctx)
.worktrees()
.iter()
.map(|w| w.read(ctx).abs_path())
.collect::<HashSet<_>>();
assert_eq!(
worktree_roots,
vec![dir1.path(), &dir2.path().join("b.txt")]
.into_iter()
.collect(),
);
});
workspace_view
.condition(&app, |view, ctx| {
view.active_pane()
.read(ctx)
.active_item()
.map_or(false, |item| item.title(&ctx) == "b.txt")
})
.await;
});
}
#[test]
fn test_pane_actions() {
App::test_async((), |mut app| async move {