From 2905ce3ba760756597386095433a59ddf9c60daa Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 21 Jun 2021 12:15:58 +0200 Subject: [PATCH] WIP: Load remote history --- 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(-) diff --git a/zed-rpc/proto/zed.proto b/zed-rpc/proto/zed.proto index efbf8a02ef0ebd9859d6d1be71ceb9fccc29281d..a2e2613419a7ba8ae8f46fe8ab36866777d07912 100644 --- a/zed-rpc/proto/zed.proto +++ b/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 { diff --git a/zed-rpc/src/peer.rs b/zed-rpc/src/peer.rs index 48111c2b9232853d05575e2073caed48d7ca2812..dbd72bdec32f0d32e25ebbc30d779539bb9009d8 100644 --- a/zed-rpc/src/peer.rs +++ b/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![], }), diff --git a/zed-rpc/src/proto.rs b/zed-rpc/src/proto.rs index 196ddeddc8d6337b934896c06566b82f32b5a4de..baa0280270da81200cb83e3b6fa83487f8e63089 100644 --- a/zed-rpc/src/proto.rs +++ b/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(); diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 914441e01bb0412ddb1074ae586624ed34dfff2d..599581a81a3bc80b18fbff4bc0ea7c875cf87080 100644 --- a/zed/src/workspace.rs +++ b/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(()) } } diff --git a/zed/src/worktree.rs b/zed/src/worktree.rs index 3e90db2838433d0449903e4d0d740d678d302d9f..a1f54520dab25190601529805ed6d20f9e316307 100644 --- a/zed/src/worktree.rs +++ b/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> { - 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> { - 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> { 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> { - self.worktree.read(cx).load_history(&self.path(), cx) + pub fn load_history(&self, cx: &AppContext) -> Task> { + 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> { @@ -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()); });