From 546ce14cb973ecd3eaafc6cc73b191b3130be021 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 15 Jun 2021 16:14:08 +0200 Subject: [PATCH] Start sharing paths with the server via RPC --- zed/src/workspace.rs | 17 ++++++++--------- zed/src/worktree.rs | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 14810c08b243a209d785b3c56ea38d5be959d467..16b3323a09eb4e75b1e06144fad2cd8c88477011 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -647,7 +647,7 @@ impl Workspace { let zed_url = std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev".to_string()); let executor = cx.background_executor().clone(); - let task = cx.spawn::<_, _, surf::Result<()>>(|_this, cx| async move { + let task = cx.spawn::<_, _, surf::Result<()>>(|this, mut cx| async move { let (user_id, access_token) = login(zed_url.clone(), cx.platform(), cx.background_executor()).await?; @@ -682,15 +682,14 @@ impl Workspace { Err(anyhow!("failed to authenticate with RPC server"))?; } - let share_response = rpc_client - .request(proto::from_client::ShareWorktree { - worktree_id: worktree_id as u64, - files: Vec::new(), - }) - .await?; - - log::info!("sharing worktree {:?}", share_response); + let share_task = this.update(&mut cx, |this, cx| { + let worktree = this.worktrees.iter().next()?; + Some(worktree.update(cx, |worktree, cx| worktree.share(rpc_client, cx))) + }); + if let Some(share_task) = share_task { + share_task.await?; + } Ok(()) }); diff --git a/zed/src/worktree.rs b/zed/src/worktree.rs index a43856d7f8ec9bc21d357930c6a0d4133dc30434..16c7a8c696fdbe9d508509244b853641ecb4a278 100644 --- a/zed/src/worktree.rs +++ b/zed/src/worktree.rs @@ -4,6 +4,7 @@ mod ignore; use crate::{ editor::{History, Rope}, + rpc_client::RpcClient, sum_tree::{self, Cursor, Edit, SumTree}, util::Bias, }; @@ -31,6 +32,7 @@ use std::{ sync::{Arc, Weak}, time::{Duration, SystemTime, UNIX_EPOCH}, }; +use zed_rpc::proto::{self, from_client::PathAndDigest}; use self::{char_bag::CharBag, ignore::IgnoreStack}; @@ -52,6 +54,7 @@ pub struct Worktree { scan_state: (watch::Sender, watch::Receiver), _event_stream_handle: fsevent::Handle, poll_scheduled: bool, + rpc_client: Option>, } #[derive(Clone, Debug)] @@ -93,6 +96,7 @@ impl Worktree { scan_state: watch::channel_with(ScanState::Scanning), _event_stream_handle: event_stream_handle, poll_scheduled: false, + rpc_client: None, }; std::thread::spawn(move || { @@ -221,6 +225,39 @@ impl Worktree { Ok(()) }) } + + pub fn share( + &mut self, + client: Arc, + cx: &mut ModelContext, + ) -> Task> { + self.rpc_client = Some(client.clone()); + let snapshot = self.snapshot(); + cx.spawn(|_this, cx| async move { + let files = cx + .background_executor() + .spawn(async move { + snapshot + .paths() + .map(|path| PathAndDigest { + path: path.as_os_str().as_bytes().to_vec(), + digest: Default::default(), + }) + .collect() + }) + .await; + + let share_response = client + .request(proto::from_client::ShareWorktree { + worktree_id: 0, + files, + }) + .await?; + + log::info!("sharing worktree {:?}", share_response); + Ok(()) + }) + } } impl Entity for Worktree { @@ -264,7 +301,6 @@ impl Snapshot { FileIter::all(self, start) } - #[cfg(test)] pub fn paths(&self) -> impl Iterator> { self.entries .cursor::<(), ()>()