diff --git a/zed/src/lib.rs b/zed/src/lib.rs index b1cd86c04c870f0c773eb0e46549931c7a431b86..35391b06915d863c56f5e433ea625842b62ba1ad 100644 --- a/zed/src/lib.rs +++ b/zed/src/lib.rs @@ -6,6 +6,7 @@ pub mod file_finder; pub mod language; pub mod menus; mod operation_queue; +mod rpc; pub mod settings; mod sum_tree; #[cfg(test)] diff --git a/zed/src/rpc.rs b/zed/src/rpc.rs new file mode 100644 index 0000000000000000000000000000000000000000..6e2dbefe5b8686e9c873627e624398a35574f2ea --- /dev/null +++ b/zed/src/rpc.rs @@ -0,0 +1,58 @@ +use postage::prelude::Stream; +use std::{future::Future, sync::Arc}; +use zed_rpc::{proto, Peer, TypedEnvelope}; + +pub trait MessageHandler<'a, M: proto::EnvelopedMessage> { + type Output: 'a + Future>; + + fn handle( + &self, + message: TypedEnvelope, + rpc: &'a Arc, + cx: &'a mut gpui::AsyncAppContext, + ) -> Self::Output; +} + +impl<'a, M, F, Fut> MessageHandler<'a, M> for F +where + M: proto::EnvelopedMessage, + F: Fn(TypedEnvelope, &'a Arc, &'a mut gpui::AsyncAppContext) -> Fut, + Fut: 'a + Future>, +{ + type Output = Fut; + + fn handle( + &self, + message: TypedEnvelope, + rpc: &'a Arc, + cx: &'a mut gpui::AsyncAppContext, + ) -> Self::Output { + (self)(message, rpc, cx) + } +} + +pub trait PeerExt { + fn handle_messages(&self, handler: H, cx: &mut gpui::MutableAppContext) + where + H: 'static + for<'a> MessageHandler<'a, M>, + M: proto::EnvelopedMessage; +} + +impl PeerExt for Arc { + fn handle_messages(&self, handler: H, cx: &mut gpui::MutableAppContext) + where + H: 'static + for<'a> MessageHandler<'a, M>, + M: proto::EnvelopedMessage, + { + let rpc = self.clone(); + let mut messages = smol::block_on(self.add_message_handler::()); + cx.spawn(|mut cx| async move { + while let Some(message) = messages.recv().await { + if let Err(err) = handler.handle(message, &rpc, &mut cx).await { + log::error!("error handling message: {:?}", err); + } + } + }) + .detach(); + } +} diff --git a/zed/src/util.rs b/zed/src/util.rs index ad3279845350be08b67f8258d3caacdb100834ff..0fa1081d66704270fd00207b1ad813d0ed99e405 100644 --- a/zed/src/util.rs +++ b/zed/src/util.rs @@ -1,7 +1,5 @@ -use postage::prelude::Stream; use rand::prelude::*; -use std::{cmp::Ordering, future::Future, sync::Arc}; -use zed_rpc::{proto, Peer, TypedEnvelope}; +use std::cmp::Ordering; #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] pub enum Bias { @@ -55,52 +53,6 @@ where } } -pub trait MessageHandler<'a, M: proto::EnvelopedMessage> { - type Output: 'a + Future>; - - fn handle( - &self, - message: TypedEnvelope, - rpc: Arc, - cx: &'a mut gpui::AsyncAppContext, - ) -> Self::Output; -} - -impl<'a, M, F, Fut> MessageHandler<'a, M> for F -where - M: proto::EnvelopedMessage, - F: Fn(TypedEnvelope, Arc, &'a mut gpui::AsyncAppContext) -> Fut, - Fut: 'a + Future>, -{ - type Output = Fut; - - fn handle( - &self, - message: TypedEnvelope, - rpc: Arc, - cx: &'a mut gpui::AsyncAppContext, - ) -> Self::Output { - (self)(message, rpc, cx) - } -} - -pub fn handle_messages(handler: H, rpc: &Arc, cx: &mut gpui::MutableAppContext) -where - H: 'static + for<'a> MessageHandler<'a, M>, - M: proto::EnvelopedMessage, -{ - let rpc = rpc.clone(); - let mut messages = smol::block_on(rpc.add_message_handler::()); - cx.spawn(|mut cx| async move { - while let Some(message) = messages.recv().await { - if let Err(err) = handler.handle(message, rpc.clone(), &mut cx).await { - log::error!("error handling message: {:?}", err); - } - } - }) - .detach(); -} - pub struct RandomCharIter(T); impl RandomCharIter { diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index d9dc1c313f632434386bc4c38c9faacdcf52290b..98136c8cd5ffe52777a7c32056f3c7da87978008 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -4,9 +4,10 @@ pub mod pane_group; use crate::{ editor::{Buffer, Editor}, language::LanguageRegistry, + rpc::PeerExt as _, settings::Settings, time::ReplicaId, - util::{self, SurfResultExt as _}, + util::SurfResultExt as _, worktree::{FileHandle, Worktree, WorktreeHandle}, AppState, }; @@ -45,7 +46,7 @@ pub fn init(cx: &mut MutableAppContext, rpc: Arc) { ]); pane::init(cx); - util::handle_messages(handle_open_buffer, &rpc, cx); + rpc.handle_messages(handle_open_buffer, cx); } pub struct OpenParams { @@ -109,7 +110,7 @@ fn open_paths(params: &OpenParams, cx: &mut MutableAppContext) { async fn handle_open_buffer( request: TypedEnvelope, - rpc: Arc, + rpc: &Arc, cx: &mut AsyncAppContext, ) -> anyhow::Result<()> { let payload = request.payload();