From 361bb83f4382685b4d742d6d8881fadfed464a77 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 4 May 2021 14:18:12 -0700 Subject: [PATCH] Replace Workspace::open_path with simpler ::add_worktree method Co-Authored-By: Nathan Sobo --- zed/src/file_finder.rs | 6 +++--- zed/src/workspace.rs | 30 ++++++++++++++++-------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/zed/src/file_finder.rs b/zed/src/file_finder.rs index 05ff52d020..0965cdcda5 100644 --- a/zed/src/file_finder.rs +++ b/zed/src/file_finder.rs @@ -474,7 +474,7 @@ mod tests { let settings = settings::channel(&app.font_cache()).unwrap().1; let (window_id, workspace) = app.add_window(|ctx| { let mut workspace = Workspace::new(0, settings, ctx); - workspace.open_path(tmp_dir.path().into(), ctx); + workspace.add_worktree(tmp_dir.path(), ctx); workspace }); app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx)) @@ -543,7 +543,7 @@ mod tests { let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, workspace) = app.add_window(|ctx| { let mut workspace = Workspace::new(0, settings.clone(), ctx); - workspace.open_path(tmp_dir.path().into(), ctx); + workspace.add_worktree(tmp_dir.path(), ctx); workspace }); app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx)) @@ -600,7 +600,7 @@ mod tests { let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, workspace) = app.add_window(|ctx| { let mut workspace = Workspace::new(0, settings.clone(), ctx); - workspace.open_path(file_path, ctx); + workspace.add_worktree(&file_path, ctx); workspace }); app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx)) diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 0e0faa6b91..629d23beec 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -272,7 +272,15 @@ impl Workspace { let entries = paths .iter() .cloned() - .map(|path| self.open_path(path, ctx)) + .map(|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_id = self.add_worktree(&path, ctx); + (worktree_id, Path::new("").into()) + }) .collect::>(); let bg = ctx.background_executor().clone(); @@ -302,19 +310,13 @@ impl Workspace { } } - pub fn open_path(&mut self, path: PathBuf, ctx: &mut ViewContext) -> (usize, Arc) { - 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)); + pub fn add_worktree(&mut self, path: &Path, ctx: &mut ViewContext) -> usize { + let worktree = ctx.add_model(|ctx| Worktree::new(path, ctx)); let worktree_id = worktree.id(); ctx.observe_model(&worktree, |_, _, ctx| ctx.notify()); self.worktrees.insert(worktree); ctx.notify(); - (worktree_id, Path::new("").into()) + worktree_id } pub fn toggle_modal(&mut self, ctx: &mut ViewContext, add_view: F) @@ -685,7 +687,7 @@ mod tests { let (_, workspace) = app.add_window(|ctx| { let mut workspace = Workspace::new(0, settings, ctx); - workspace.open_path(dir.path().into(), ctx); + workspace.add_worktree(dir.path(), ctx); workspace }); @@ -771,7 +773,7 @@ mod tests { let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, workspace) = app.add_window(|ctx| { let mut workspace = Workspace::new(0, settings, ctx); - workspace.open_path(dir1.path().into(), ctx); + workspace.add_worktree(dir1.path(), ctx); workspace }); app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx)) @@ -845,7 +847,7 @@ mod tests { let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, workspace) = app.add_window(|ctx| { let mut workspace = Workspace::new(0, settings, ctx); - workspace.open_path(dir.into(), ctx); + workspace.add_worktree(dir, ctx); workspace }); app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx)) @@ -906,7 +908,7 @@ mod tests { let settings = settings::channel(&app.font_cache()).unwrap().1; let (window_id, workspace) = app.add_window(|ctx| { let mut workspace = Workspace::new(0, settings, ctx); - workspace.open_path(dir.path().into(), ctx); + workspace.add_worktree(dir.path(), ctx); workspace }); app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx))