diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 70f1f3eb1f..7ae502ad0c 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -301,7 +301,7 @@ impl Project { language_servers: Default::default(), }; for worktree in worktrees { - this.add_worktree(&worktree, false, cx); + this.add_worktree(&worktree, cx); } this })) @@ -902,7 +902,7 @@ impl Project { Worktree::open_local(client.clone(), user_store, path, weak, fs, &mut cx).await?; let (remote_project_id, is_shared) = project.update(&mut cx, |project, cx| { - project.add_worktree(&worktree, weak, cx); + project.add_worktree(&worktree, cx); (project.remote_id(), project.is_shared()) }); @@ -934,14 +934,20 @@ impl Project { cx.notify(); } - fn add_worktree( - &mut self, - worktree: &ModelHandle, - weak: bool, - cx: &mut ModelContext, - ) { + fn add_worktree(&mut self, worktree: &ModelHandle, cx: &mut ModelContext) { cx.observe(&worktree, |_, _, cx| cx.notify()).detach(); - if weak { + + let push_weak_handle = { + let worktree = worktree.read(cx); + worktree.is_local() && worktree.is_weak() + }; + if push_weak_handle { + cx.observe_release(&worktree, |this, cx| { + this.worktrees + .retain(|worktree| worktree.upgrade(cx).is_some()); + cx.notify(); + }) + .detach(); self.worktrees .push(WorktreeHandle::Weak(worktree.downgrade())); } else { @@ -1112,7 +1118,7 @@ impl Project { let worktree = Worktree::remote(remote_id, replica_id, worktree, client, user_store, &mut cx) .await?; - this.update(&mut cx, |this, cx| this.add_worktree(&worktree, false, cx)); + this.update(&mut cx, |this, cx| this.add_worktree(&worktree, cx)); Ok(()) } .log_err() diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index eb42b95bc5..9b9a161b1a 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -267,6 +267,10 @@ impl Worktree { } } + pub fn is_local(&self) -> bool { + matches!(self, Worktree::Local(_)) + } + pub fn snapshot(&self) -> Snapshot { match self { Worktree::Local(worktree) => worktree.snapshot(),