co-authored by
Max Brunsfeld
parent
bfccb173c4
commit
0deaa3a61d
+25
-12
@@ -1,14 +1,18 @@
|
||||
use crate::worktree::{FileHandle, Worktree};
|
||||
|
||||
use super::util::SurfResultExt as _;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use gpui::executor::Background;
|
||||
use gpui::{AsyncAppContext, Task};
|
||||
use gpui::{AsyncAppContext, ModelHandle, Task};
|
||||
use lazy_static::lazy_static;
|
||||
use postage::prelude::Stream;
|
||||
use smol::lock::Mutex;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::time::Duration;
|
||||
use std::{convert::TryFrom, future::Future, sync::Arc};
|
||||
use surf::Url;
|
||||
use zed_rpc::{proto::RequestMessage, rest, Peer, TypedEnvelope};
|
||||
use zed_rpc::{PeerId, Receipt};
|
||||
|
||||
pub use zed_rpc::{proto, ConnectionId};
|
||||
|
||||
@@ -20,13 +24,14 @@ lazy_static! {
|
||||
#[derive(Clone)]
|
||||
pub struct Client {
|
||||
peer: Arc<Peer>,
|
||||
state: Arc<Mutex<ClientState>>,
|
||||
pub state: Arc<Mutex<ClientState>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ClientState {
|
||||
// TODO - allow multiple connections
|
||||
pub struct ClientState {
|
||||
connection_id: Option<ConnectionId>,
|
||||
pub shared_worktrees: HashSet<ModelHandle<Worktree>>,
|
||||
pub shared_files: HashMap<FileHandle, HashMap<PeerId, usize>>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
@@ -42,11 +47,11 @@ impl Client {
|
||||
H: 'static + for<'a> MessageHandler<'a, M>,
|
||||
M: proto::EnvelopedMessage,
|
||||
{
|
||||
let peer = self.peer.clone();
|
||||
let mut messages = smol::block_on(peer.add_message_handler::<M>());
|
||||
let this = self.clone();
|
||||
let mut messages = smol::block_on(this.peer.add_message_handler::<M>());
|
||||
cx.spawn(|mut cx| async move {
|
||||
while let Some(message) = messages.recv().await {
|
||||
if let Err(err) = handler.handle(message, &peer, &mut cx).await {
|
||||
if let Err(err) = handler.handle(message, &this, &mut cx).await {
|
||||
log::error!("error handling message: {:?}", err);
|
||||
}
|
||||
}
|
||||
@@ -189,9 +194,17 @@ impl Client {
|
||||
pub fn request<T: RequestMessage>(
|
||||
&self,
|
||||
connection_id: ConnectionId,
|
||||
req: T,
|
||||
request: T,
|
||||
) -> impl Future<Output = Result<T::Response>> {
|
||||
self.peer.request(connection_id, req)
|
||||
self.peer.request(connection_id, request)
|
||||
}
|
||||
|
||||
pub fn respond<T: RequestMessage>(
|
||||
&self,
|
||||
receipt: Receipt<T>,
|
||||
response: T::Response,
|
||||
) -> impl Future<Output = Result<()>> {
|
||||
self.peer.respond(receipt, response)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,7 +214,7 @@ pub trait MessageHandler<'a, M: proto::EnvelopedMessage> {
|
||||
fn handle(
|
||||
&self,
|
||||
message: TypedEnvelope<M>,
|
||||
rpc: &'a Arc<Peer>,
|
||||
rpc: &'a Client,
|
||||
cx: &'a mut gpui::AsyncAppContext,
|
||||
) -> Self::Output;
|
||||
}
|
||||
@@ -209,7 +222,7 @@ pub trait MessageHandler<'a, M: proto::EnvelopedMessage> {
|
||||
impl<'a, M, F, Fut> MessageHandler<'a, M> for F
|
||||
where
|
||||
M: proto::EnvelopedMessage,
|
||||
F: Fn(TypedEnvelope<M>, &'a Arc<Peer>, &'a mut gpui::AsyncAppContext) -> Fut,
|
||||
F: Fn(TypedEnvelope<M>, &'a Client, &'a mut gpui::AsyncAppContext) -> Fut,
|
||||
Fut: 'a + Future<Output = anyhow::Result<()>>,
|
||||
{
|
||||
type Output = Fut;
|
||||
@@ -217,7 +230,7 @@ where
|
||||
fn handle(
|
||||
&self,
|
||||
message: TypedEnvelope<M>,
|
||||
rpc: &'a Arc<Peer>,
|
||||
rpc: &'a Client,
|
||||
cx: &'a mut gpui::AsyncAppContext,
|
||||
) -> Self::Output {
|
||||
(self)(message, rpc, cx)
|
||||
|
||||
+52
-12
@@ -28,7 +28,7 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
use zed_rpc::{proto, Peer, TypedEnvelope};
|
||||
use zed_rpc::{proto, TypedEnvelope};
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext, rpc: rpc::Client) {
|
||||
cx.add_global_action("workspace:open", open);
|
||||
@@ -44,7 +44,8 @@ pub fn init(cx: &mut MutableAppContext, rpc: rpc::Client) {
|
||||
]);
|
||||
pane::init(cx);
|
||||
|
||||
rpc.on_message(handle_open_buffer, cx);
|
||||
rpc.on_message(remote::open_file, cx);
|
||||
rpc.on_message(remote::open_buffer, cx);
|
||||
}
|
||||
|
||||
pub struct OpenParams {
|
||||
@@ -106,18 +107,57 @@ fn open_paths(params: &OpenParams, cx: &mut MutableAppContext) {
|
||||
});
|
||||
}
|
||||
|
||||
async fn handle_open_buffer(
|
||||
request: TypedEnvelope<proto::OpenBuffer>,
|
||||
rpc: &Arc<Peer>,
|
||||
cx: &mut AsyncAppContext,
|
||||
) -> anyhow::Result<()> {
|
||||
let payload = &request.payload;
|
||||
dbg!(&payload.path);
|
||||
rpc.respond(request, proto::OpenBufferResponse { buffer: None })
|
||||
mod remote {
|
||||
use super::*;
|
||||
|
||||
pub async fn open_file(
|
||||
request: TypedEnvelope<proto::OpenFile>,
|
||||
rpc: &rpc::Client,
|
||||
cx: &mut AsyncAppContext,
|
||||
) -> anyhow::Result<()> {
|
||||
let message = &request.payload;
|
||||
let mut state = rpc.state.lock().await;
|
||||
|
||||
let worktree = state
|
||||
.shared_worktrees
|
||||
.get(&(message.worktree_id as usize))
|
||||
.ok_or_else(|| anyhow!("worktree {} not found", message.worktree_id))?
|
||||
.clone();
|
||||
|
||||
let peer_id = request
|
||||
.original_sender_id
|
||||
.ok_or_else(|| anyhow!("missing original sender id"))?;
|
||||
|
||||
let file = cx.update(|cx| worktree.file(&message.path, cx)).await?;
|
||||
|
||||
let file_entry = state.shared_files.entry(file);
|
||||
if matches!(file_entry, Entry::Vacant(_)) {
|
||||
worktree.update(cx, |worktree, cx| {});
|
||||
}
|
||||
*file_entry
|
||||
.or_insert(Default::default())
|
||||
.entry(peer_id)
|
||||
.or_insert(0) += 1;
|
||||
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn open_buffer(
|
||||
request: TypedEnvelope<proto::OpenBuffer>,
|
||||
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(())
|
||||
dbg!(cx.read(|app| app.root_view_id(1)));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Item: Entity + Sized {
|
||||
|
||||
+31
-3
@@ -2,6 +2,7 @@ mod char_bag;
|
||||
mod fuzzy;
|
||||
mod ignore;
|
||||
|
||||
use self::{char_bag::CharBag, ignore::IgnoreStack};
|
||||
use crate::{
|
||||
editor::{History, Rope},
|
||||
rpc::{self, proto, ConnectionId},
|
||||
@@ -25,16 +26,18 @@ use std::{
|
||||
ffi::{CStr, OsStr, OsString},
|
||||
fmt, fs,
|
||||
future::Future,
|
||||
hash::Hash,
|
||||
io::{self, Read, Write},
|
||||
ops::Deref,
|
||||
os::unix::{ffi::OsStrExt, fs::MetadataExt},
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, Weak},
|
||||
sync::{
|
||||
atomic::{AtomicUsize, Ordering::SeqCst},
|
||||
Arc, Weak,
|
||||
},
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
use self::{char_bag::CharBag, ignore::IgnoreStack};
|
||||
|
||||
lazy_static! {
|
||||
static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore");
|
||||
}
|
||||
@@ -132,6 +135,7 @@ pub struct LocalWorktree {
|
||||
snapshot: Snapshot,
|
||||
background_snapshot: Arc<Mutex<Snapshot>>,
|
||||
handles: Arc<Mutex<HashMap<Arc<Path>, Weak<Mutex<FileHandleState>>>>>,
|
||||
next_handle_id: AtomicUsize,
|
||||
scan_state: (watch::Sender<ScanState>, watch::Receiver<ScanState>),
|
||||
_event_stream_handle: fsevent::Handle,
|
||||
poll_scheduled: bool,
|
||||
@@ -149,6 +153,7 @@ struct FileHandleState {
|
||||
path: Arc<Path>,
|
||||
is_deleted: bool,
|
||||
mtime: SystemTime,
|
||||
id: usize,
|
||||
}
|
||||
|
||||
impl LocalWorktree {
|
||||
@@ -174,6 +179,7 @@ impl LocalWorktree {
|
||||
snapshot,
|
||||
background_snapshot: background_snapshot.clone(),
|
||||
handles: handles.clone(),
|
||||
next_handle_id: Default::default(),
|
||||
scan_state: watch::channel_with(ScanState::Scanning),
|
||||
_event_stream_handle: event_stream_handle,
|
||||
poll_scheduled: false,
|
||||
@@ -326,6 +332,7 @@ impl LocalWorktree {
|
||||
self.rpc = Some(client.clone());
|
||||
let root_name = self.root_name.clone();
|
||||
let snapshot = self.snapshot();
|
||||
let handle = cx.handle();
|
||||
cx.spawn(|_this, cx| async move {
|
||||
let entries = cx
|
||||
.background_executor()
|
||||
@@ -353,6 +360,8 @@ impl LocalWorktree {
|
||||
)
|
||||
.await?;
|
||||
|
||||
client.state.lock().await.shared_worktrees.insert(handle);
|
||||
|
||||
log::info!("sharing worktree {:?}", share_response);
|
||||
Ok((share_response.worktree_id, share_response.access_token))
|
||||
})
|
||||
@@ -685,6 +694,21 @@ impl FileHandle {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for FileHandle {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.worktree == other.worktree && self.state.lock().id == other.state.lock().id
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for FileHandle {}
|
||||
|
||||
impl Hash for FileHandle {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.state.lock().id.hash(state);
|
||||
self.worktree.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Entry {
|
||||
kind: EntryKind,
|
||||
@@ -1420,17 +1444,21 @@ impl WorktreeHandle for ModelHandle<Worktree> {
|
||||
.get(&path)
|
||||
.and_then(Weak::upgrade)
|
||||
.unwrap_or_else(|| {
|
||||
let id =
|
||||
tree.as_local().unwrap().next_handle_id.fetch_add(1, SeqCst);
|
||||
let handle_state = if let Some(entry) = tree.entry_for_path(&path) {
|
||||
FileHandleState {
|
||||
path: entry.path().clone(),
|
||||
is_deleted: false,
|
||||
mtime,
|
||||
id,
|
||||
}
|
||||
} else {
|
||||
FileHandleState {
|
||||
path: path.clone(),
|
||||
is_deleted: !tree.path_is_pending(path),
|
||||
mtime,
|
||||
id,
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user