Forward events from remote worktrees to their projects

Max Brunsfeld created

Change summary

crates/project/src/project.rs | 66 +++++++++++++++++++++---------------
crates/server/src/rpc.rs      | 16 +++++---
2 files changed, 48 insertions(+), 34 deletions(-)

Detailed changes

crates/project/src/project.rs 🔗

@@ -229,35 +229,45 @@ impl Project {
             collaborators.insert(collaborator.peer_id, collaborator);
         }
 
-        Ok(cx.add_model(|cx| Self {
-            worktrees,
-            active_entry: None,
-            collaborators,
-            languages,
-            user_store,
-            fs,
-            subscriptions: vec![
-                client.subscribe_to_entity(remote_id, cx, Self::handle_unshare_project),
-                client.subscribe_to_entity(remote_id, cx, Self::handle_add_collaborator),
-                client.subscribe_to_entity(remote_id, cx, Self::handle_remove_collaborator),
-                client.subscribe_to_entity(remote_id, cx, Self::handle_share_worktree),
-                client.subscribe_to_entity(remote_id, cx, Self::handle_unregister_worktree),
-                client.subscribe_to_entity(remote_id, cx, Self::handle_update_worktree),
-                client.subscribe_to_entity(remote_id, cx, Self::handle_update_diagnostic_summary),
-                client.subscribe_to_entity(
+        Ok(cx.add_model(|cx| {
+            let mut this = Self {
+                worktrees: Vec::new(),
+                active_entry: None,
+                collaborators,
+                languages,
+                user_store,
+                fs,
+                subscriptions: vec![
+                    client.subscribe_to_entity(remote_id, cx, Self::handle_unshare_project),
+                    client.subscribe_to_entity(remote_id, cx, Self::handle_add_collaborator),
+                    client.subscribe_to_entity(remote_id, cx, Self::handle_remove_collaborator),
+                    client.subscribe_to_entity(remote_id, cx, Self::handle_share_worktree),
+                    client.subscribe_to_entity(remote_id, cx, Self::handle_unregister_worktree),
+                    client.subscribe_to_entity(remote_id, cx, Self::handle_update_worktree),
+                    client.subscribe_to_entity(
+                        remote_id,
+                        cx,
+                        Self::handle_update_diagnostic_summary,
+                    ),
+                    client.subscribe_to_entity(
+                        remote_id,
+                        cx,
+                        Self::handle_disk_based_diagnostics_updated,
+                    ),
+                    client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer),
+                    client.subscribe_to_entity(remote_id, cx, Self::handle_buffer_saved),
+                ],
+                client,
+                client_state: ProjectClientState::Remote {
+                    sharing_has_stopped: false,
                     remote_id,
-                    cx,
-                    Self::handle_disk_based_diagnostics_updated,
-                ),
-                client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer),
-                client.subscribe_to_entity(remote_id, cx, Self::handle_buffer_saved),
-            ],
-            client,
-            client_state: ProjectClientState::Remote {
-                sharing_has_stopped: false,
-                remote_id,
-                replica_id,
-            },
+                    replica_id,
+                },
+            };
+            for worktree in worktrees {
+                this.add_worktree(worktree, cx);
+            }
+            this
         }))
     }
 

crates/server/src/rpc.rs 🔗

@@ -1060,7 +1060,7 @@ mod tests {
             LanguageRegistry, LanguageServerConfig, Point,
         },
         lsp,
-        project::{DiagnosticSummary, Project},
+        project::{DiagnosticSummary, Project, ProjectPath},
     };
 
     #[gpui::test]
@@ -1801,6 +1801,7 @@ mod tests {
         let project_id = project_a
             .update(&mut cx_a, |project, _| project.next_remote_id())
             .await;
+        let worktree_id = worktree_a.read_with(&cx_a, |tree, _| tree.id());
         project_a
             .update(&mut cx_a, |project, cx| project.share(cx))
             .await
@@ -1826,7 +1827,6 @@ mod tests {
         )
         .await
         .unwrap();
-        let worktree_b = project_b.update(&mut cx_b, |p, _| p.worktrees()[0].clone());
 
         // Simulate a language server reporting errors for a file.
         fake_language_server
@@ -1853,11 +1853,14 @@ mod tests {
             })
             .await;
 
-        worktree_b
-            .condition(&cx_b, |worktree, _| {
-                worktree.diagnostic_summaries().collect::<Vec<_>>()
+        project_b
+            .condition(&cx_b, |project, cx| {
+                project.diagnostic_summaries(cx).collect::<Vec<_>>()
                     == &[(
-                        Arc::from(Path::new("a.rs")),
+                        ProjectPath {
+                            worktree_id,
+                            path: Arc::from(Path::new("a.rs")),
+                        },
                         DiagnosticSummary {
                             error_count: 1,
                             warning_count: 1,
@@ -1868,6 +1871,7 @@ mod tests {
             .await;
 
         // Open the file with the errors.
+        let worktree_b = project_b.update(&mut cx_b, |p, _| p.worktrees()[0].clone());
         let buffer_b = cx_b
             .background()
             .spawn(worktree_b.update(&mut cx_b, |worktree, cx| worktree.open_buffer("a.rs", cx)))