diff --git a/zed/src/main.rs b/zed/src/main.rs index 0f0007c78890f624b3e7d9bd35db8d6ca05b008b..54a55ce86115c857241934ff59b1fe61536b8702 100644 --- a/zed/src/main.rs +++ b/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); diff --git a/zed/src/rpc.rs b/zed/src/rpc.rs index 0388869d6669771a8b9c6c36b706c44c1c148a18..7a4c51e8e2167bf6b0492fdf76f603e226297b12 100644 --- a/zed/src/rpc.rs +++ b/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()); diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 9d4483610c2f45eba6a3786ace96a40faab4a3a8..389d84689ff0098ecbf3e5b963bedbcc17d92587 100644 --- a/zed/src/workspace.rs +++ b/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, - 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, - _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": { diff --git a/zed/src/worktree.rs b/zed/src/worktree.rs index f441c6824d40adef6203b1c4e7d50e2beb90545a..9de83010bdd8972901988e055573bf5907788188 100644 --- a/zed/src/worktree.rs +++ b/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), @@ -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, + 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, + _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::*;