WIP: Load remote history

Antonio Scandurra created

Change summary

zed-rpc/proto/zed.proto |  9 ++----
zed-rpc/src/peer.rs     | 14 +--------
zed-rpc/src/proto.rs    |  6 ---
zed/src/workspace.rs    |  4 --
zed/src/worktree.rs     | 62 ++++++++++++++++++++----------------------
5 files changed, 35 insertions(+), 60 deletions(-)

Detailed changes

zed-rpc/proto/zed.proto 🔗

@@ -71,8 +71,7 @@ message CloseFile {
 }
 
 message OpenBuffer {
-    uint64 worktree_id = 1;
-    string path = 2;
+    uint64 id = 1;
 }
 
 message OpenBufferResponse {
@@ -99,10 +98,8 @@ message Entry {
 }
 
 message Buffer {
-    uint64 id = 1;
-    string path = 2;
-    string content = 3;
-    repeated Operation history = 4;
+    string content = 1;
+    repeated Operation history = 2;
 }
 
 message Operation {

zed-rpc/src/peer.rs 🔗

@@ -408,26 +408,16 @@ mod tests {
             let response2 = proto::AuthResponse {
                 credentials_valid: false,
             };
-            let request3 = proto::OpenBuffer {
-                worktree_id: 102,
-                path: "path/two".to_string(),
-            };
+            let request3 = proto::OpenBuffer { id: 2 };
             let response3 = proto::OpenBufferResponse {
                 buffer: Some(proto::Buffer {
-                    id: 1001,
-                    path: "path/two".to_string(),
                     content: "path/two content".to_string(),
                     history: vec![],
                 }),
             };
-            let request4 = proto::OpenBuffer {
-                worktree_id: 101,
-                path: "path/one".to_string(),
-            };
+            let request4 = proto::OpenBuffer { id: 1 };
             let response4 = proto::OpenBufferResponse {
                 buffer: Some(proto::Buffer {
-                    id: 1002,
-                    path: "path/one".to_string(),
                     content: "path/one content".to_string(),
                     history: vec![],
                 }),

zed-rpc/src/proto.rs 🔗

@@ -145,11 +145,7 @@ mod tests {
             }
             .into_envelope(3, None, None);
 
-            let message2 = OpenBuffer {
-                worktree_id: 1,
-                path: "path".to_string(),
-            }
-            .into_envelope(5, None, None);
+            let message2 = OpenBuffer { id: 1 }.into_envelope(5, None, None);
 
             let mut message_stream = MessageStream::new(byte_stream);
             message_stream.write_message(&message1).await.unwrap();

zed/src/workspace.rs 🔗

@@ -176,15 +176,11 @@ mod remote {
         rpc: &rpc::Client,
         cx: &mut AsyncAppContext,
     ) -> anyhow::Result<()> {
-        let payload = &request.payload;
-        dbg!(&payload.path);
         rpc.respond(
             request.receipt(),
             proto::OpenBufferResponse { buffer: None },
         )
         .await?;
-
-        dbg!(cx.read(|app| app.root_view_id(1)));
         Ok(())
     }
 }

zed/src/worktree.rs 🔗

@@ -10,7 +10,7 @@ use crate::{
     util::Bias,
 };
 use ::ignore::gitignore::Gitignore;
-use anyhow::{Context, Result};
+use anyhow::{anyhow, Context, Result};
 pub use fuzzy::{match_paths, PathMatch};
 use gpui::{scoped_pool, AppContext, Entity, ModelContext, ModelHandle, MutableAppContext, Task};
 use lazy_static::lazy_static;
@@ -96,17 +96,6 @@ impl Worktree {
         }
     }
 
-    pub fn load_history(
-        &self,
-        path: &Path,
-        cx: &AppContext,
-    ) -> impl Future<Output = Result<History>> {
-        match self {
-            Worktree::Local(worktree) => worktree.load_history(path, cx),
-            Worktree::Remote(worktree) => todo!(),
-        }
-    }
-
     pub fn save(
         &self,
         path: &Path,
@@ -300,21 +289,6 @@ impl LocalWorktree {
         }
     }
 
-    pub fn load_history(
-        &self,
-        path: &Path,
-        cx: &AppContext,
-    ) -> impl Future<Output = Result<History>> {
-        let path = path.to_path_buf();
-        let abs_path = self.absolutize(&path);
-        cx.background_executor().spawn(async move {
-            let mut file = fs::File::open(&abs_path)?;
-            let mut base_text = String::new();
-            file.read_to_string(&mut base_text)?;
-            Ok(History::new(Arc::from(base_text)))
-        })
-    }
-
     pub fn save(&self, path: &Path, content: Rope, cx: &AppContext) -> Task<Result<()>> {
         let handles = self.handles.clone();
         let path = path.to_path_buf();
@@ -669,8 +643,32 @@ impl FileHandle {
         !self.is_deleted()
     }
 
-    pub fn load_history(&self, cx: &AppContext) -> impl Future<Output = Result<History>> {
-        self.worktree.read(cx).load_history(&self.path(), cx)
+    pub fn load_history(&self, cx: &AppContext) -> Task<Result<History>> {
+        match self.worktree.read(cx) {
+            Worktree::Local(worktree) => {
+                let path = self.state.lock().path.to_path_buf();
+                let abs_path = worktree.absolutize(&path);
+                cx.background_executor().spawn(async move {
+                    let mut file = fs::File::open(&abs_path)?;
+                    let mut base_text = String::new();
+                    file.read_to_string(&mut base_text)?;
+                    Ok(History::new(Arc::from(base_text)))
+                })
+            }
+            Worktree::Remote(worktree) => {
+                let state = self.state.lock();
+                let id = state.id;
+                let (connection_id, rpc) = state.rpc.clone().unwrap();
+                cx.background_executor().spawn(async move {
+                    let response = rpc.request(connection_id, proto::OpenBuffer { id }).await?;
+                    let buffer = response
+                        .buffer
+                        .ok_or_else(|| anyhow!("buffer must be present"))?;
+                    let mut history = History::new(buffer.content.into());
+                    Ok(history)
+                })
+            }
+        }
     }
 
     pub fn save(&self, content: Rope, cx: &AppContext) -> impl Future<Output = Result<()>> {
@@ -1786,10 +1784,8 @@ mod tests {
             path
         });
 
-        let history = cx
-            .read(|cx| tree.read(cx).load_history(&path, cx))
-            .await
-            .unwrap();
+        let file = cx.update(|cx| tree.file(&path, cx)).await.unwrap();
+        let history = cx.read(|cx| file.load_history(cx)).await.unwrap();
         cx.read(|cx| {
             assert_eq!(history.base_text.as_ref(), buffer.read(cx).text());
         });