Move `remote` module from `workspace` to `worktree`

Antonio Scandurra created

Change summary

zed/src/main.rs      |  5 +-
zed/src/rpc.rs       |  5 +-
zed/src/workspace.rs | 86 ++-------------------------------------------
zed/src/worktree.rs  | 80 ++++++++++++++++++++++++++++++++++++++++++
4 files changed, 89 insertions(+), 87 deletions(-)

Detailed changes

zed/src/main.rs 🔗

@@ -8,7 +8,7 @@ use std::{fs, path::PathBuf, sync::Arc};
 use zed::{
     self, assets, editor, file_finder, language, menus, rpc, settings,
     workspace::{self, OpenParams},
-    AppState,
+    worktree, AppState,
 };
 
 fn main() {
@@ -29,7 +29,8 @@ fn main() {
     app.run(move |cx| {
         cx.set_menus(menus::menus(app_state.clone()));
         zed::init(cx);
-        workspace::init(cx, app_state.rpc.clone());
+        workspace::init(cx);
+        worktree::init(cx, app_state.rpc.clone());
         editor::init(cx);
         file_finder::init(cx);
 

zed/src/rpc.rs 🔗

@@ -10,13 +10,12 @@ use std::collections::HashMap;
 use std::time::Duration;
 use std::{convert::TryFrom, future::Future, sync::Arc};
 use surf::Url;
+pub use zed_rpc::{proto, ConnectionId, PeerId, TypedEnvelope};
 use zed_rpc::{
     proto::{EnvelopedMessage, RequestMessage},
-    rest, Peer, Receipt, TypedEnvelope,
+    rest, Peer, Receipt,
 };
 
-pub use zed_rpc::{proto, ConnectionId, PeerId};
-
 lazy_static! {
     static ref ZED_SERVER_URL: String =
         std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev".to_string());

zed/src/workspace.rs 🔗

@@ -12,8 +12,8 @@ use crate::{
 use anyhow::{anyhow, Result};
 use gpui::{
     color::rgbu, elements::*, json::to_string_pretty, keymap::Binding, AnyViewHandle, AppContext,
-    AsyncAppContext, ClipboardItem, Entity, ModelHandle, MutableAppContext, PathPromptOptions,
-    PromptLevel, Task, View, ViewContext, ViewHandle, WeakModelHandle,
+    ClipboardItem, Entity, ModelHandle, MutableAppContext, PathPromptOptions, PromptLevel, Task,
+    View, ViewContext, ViewHandle, WeakModelHandle,
 };
 use log::error;
 pub use pane::*;
@@ -26,9 +26,8 @@ use std::{
     path::{Path, PathBuf},
     sync::Arc,
 };
-use zed_rpc::{proto, TypedEnvelope};
 
-pub fn init(cx: &mut MutableAppContext, rpc: rpc::Client) {
+pub fn init(cx: &mut MutableAppContext) {
     cx.add_global_action("workspace:open", open);
     cx.add_global_action("workspace:open_paths", open_paths);
     cx.add_action("workspace:save", Workspace::save_active_item);
@@ -41,9 +40,6 @@ pub fn init(cx: &mut MutableAppContext, rpc: rpc::Client) {
         Binding::new("cmd-alt-i", "workspace:debug_elements", None),
     ]);
     pane::init(cx);
-
-    rpc.on_message(remote::open_buffer, cx);
-    rpc.on_message(remote::close_buffer, cx);
 }
 
 pub struct OpenParams {
@@ -104,80 +100,6 @@ fn open_paths(params: &OpenParams, cx: &mut MutableAppContext) {
     });
 }
 
-mod remote {
-    use super::*;
-
-    pub async fn open_buffer(
-        request: TypedEnvelope<proto::OpenBuffer>,
-        rpc: &rpc::Client,
-        cx: &mut AsyncAppContext,
-    ) -> anyhow::Result<()> {
-        let message = &request.payload;
-        let peer_id = request
-            .original_sender_id
-            .ok_or_else(|| anyhow!("missing original sender id"))?;
-
-        let mut state = rpc.state.lock().await;
-        let worktree = state
-            .shared_worktrees
-            .get(&message.worktree_id)
-            .ok_or_else(|| anyhow!("worktree {} not found", message.worktree_id))?
-            .clone();
-
-        let buffer = worktree
-            .update(cx, |worktree, cx| {
-                worktree.open_buffer(
-                    Path::new(&message.path),
-                    state.language_registry.clone(),
-                    cx,
-                )
-            })
-            .await?;
-        state
-            .shared_buffers
-            .entry(peer_id)
-            .or_default()
-            .insert(buffer.id(), buffer.clone());
-
-        rpc.respond(
-            request.receipt(),
-            proto::OpenBufferResponse {
-                buffer_id: buffer.id() as u64,
-                buffer: Some(buffer.read_with(cx, |buf, _| buf.to_proto())),
-            },
-        )
-        .await?;
-
-        Ok(())
-    }
-
-    pub async fn close_buffer(
-        _request: TypedEnvelope<proto::CloseBuffer>,
-        _rpc: &rpc::Client,
-        _cx: &mut AsyncAppContext,
-    ) -> anyhow::Result<()> {
-        // let message = &request.payload;
-        // let peer_id = request
-        //     .original_sender_id
-        //     .ok_or_else(|| anyhow!("missing original sender id"))?;
-        // let mut state = rpc.state.lock().await;
-        // if let Some((_, ref_counts)) = state
-        //     .shared_files
-        //     .iter_mut()
-        //     .find(|(file, _)| file.id() == message.id)
-        // {
-        //     if let Some(count) = ref_counts.get_mut(&peer_id) {
-        //         *count -= 1;
-        //         if *count == 0 {
-        //             ref_counts.remove(&peer_id);
-        //         }
-        //     }
-        // }
-
-        Ok(())
-    }
-}
-
 pub trait Item: Entity + Sized {
     type View: ItemView;
 
@@ -921,7 +843,7 @@ mod tests {
     fn test_open_paths_action(cx: &mut gpui::MutableAppContext) {
         let app_state = build_app_state(cx.as_ref());
 
-        init(cx, app_state.rpc.clone());
+        init(cx);
 
         let dir = temp_tree(json!({
             "a": {

zed/src/worktree.rs 🔗

@@ -43,6 +43,11 @@ lazy_static! {
     static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore");
 }
 
+pub fn init(cx: &mut MutableAppContext, rpc: rpc::Client) {
+    rpc.on_message(remote::open_buffer, cx);
+    rpc.on_message(remote::close_buffer, cx);
+}
+
 #[derive(Clone, Debug)]
 enum ScanState {
     Idle(Option<Diff>),
@@ -1725,6 +1730,81 @@ impl<'a> Iterator for ChildEntriesIter<'a> {
     }
 }
 
+mod remote {
+    use super::*;
+    use crate::rpc::TypedEnvelope;
+
+    pub async fn open_buffer(
+        request: TypedEnvelope<proto::OpenBuffer>,
+        rpc: &rpc::Client,
+        cx: &mut AsyncAppContext,
+    ) -> anyhow::Result<()> {
+        let message = &request.payload;
+        let peer_id = request
+            .original_sender_id
+            .ok_or_else(|| anyhow!("missing original sender id"))?;
+
+        let mut state = rpc.state.lock().await;
+        let worktree = state
+            .shared_worktrees
+            .get(&message.worktree_id)
+            .ok_or_else(|| anyhow!("worktree {} not found", message.worktree_id))?
+            .clone();
+
+        let buffer = worktree
+            .update(cx, |worktree, cx| {
+                worktree.open_buffer(
+                    Path::new(&message.path),
+                    state.language_registry.clone(),
+                    cx,
+                )
+            })
+            .await?;
+        state
+            .shared_buffers
+            .entry(peer_id)
+            .or_default()
+            .insert(buffer.id(), buffer.clone());
+
+        rpc.respond(
+            request.receipt(),
+            proto::OpenBufferResponse {
+                buffer_id: buffer.id() as u64,
+                buffer: Some(buffer.read_with(cx, |buf, _| buf.to_proto())),
+            },
+        )
+        .await?;
+
+        Ok(())
+    }
+
+    pub async fn close_buffer(
+        _request: TypedEnvelope<proto::CloseBuffer>,
+        _rpc: &rpc::Client,
+        _cx: &mut AsyncAppContext,
+    ) -> anyhow::Result<()> {
+        // let message = &request.payload;
+        // let peer_id = request
+        //     .original_sender_id
+        //     .ok_or_else(|| anyhow!("missing original sender id"))?;
+        // let mut state = rpc.state.lock().await;
+        // if let Some((_, ref_counts)) = state
+        //     .shared_files
+        //     .iter_mut()
+        //     .find(|(file, _)| file.id() == message.id)
+        // {
+        //     if let Some(count) = ref_counts.get_mut(&peer_id) {
+        //         *count -= 1;
+        //         if *count == 0 {
+        //             ref_counts.remove(&peer_id);
+        //         }
+        //     }
+        // }
+
+        Ok(())
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;