mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-07 20:39:04 +03:00
Extract a BufferStore object from Project (#14037)
This is a ~small~ pure refactor that's a step toward SSH remoting. I've extracted the Project's buffer state management into a smaller, separate struct called `BufferStore`, currently in the same crate. I did this as a separate PR to reduce conflicts between main and `remoting-over-ssh`. The idea is to make use of this struct (and other smaller structs that make up `Project`) in a dedicated, simpler `HeadlessProject` type that we will use in the SSH server to model the remote end of a project. With this approach, as we develop the headless project, we can avoid adding more conditional logic to `Project` itself (which is already very complex), and actually make `Project` a bit smaller by extracting out helper objects. Release Notes: - N/A
This commit is contained in:
parent
21c5ce2bbd
commit
489077befc
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -8199,6 +8199,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"collections",
|
||||
"futures 0.3.28",
|
||||
"prost",
|
||||
"prost-build",
|
||||
"serde",
|
||||
|
@ -13,8 +13,9 @@ use async_tungstenite::tungstenite::{
|
||||
use clock::SystemClock;
|
||||
use collections::HashMap;
|
||||
use futures::{
|
||||
channel::oneshot, future::LocalBoxFuture, AsyncReadExt, FutureExt, SinkExt, Stream, StreamExt,
|
||||
TryFutureExt as _, TryStreamExt,
|
||||
channel::oneshot,
|
||||
future::{BoxFuture, LocalBoxFuture},
|
||||
AsyncReadExt, FutureExt, SinkExt, Stream, StreamExt, TryFutureExt as _, TryStreamExt,
|
||||
};
|
||||
use gpui::{
|
||||
actions, AnyModel, AnyWeakModel, AppContext, AsyncAppContext, Global, Model, Task, WeakModel,
|
||||
@ -23,6 +24,7 @@ use http::{HttpClient, HttpClientWithUrl};
|
||||
use lazy_static::lazy_static;
|
||||
use parking_lot::RwLock;
|
||||
use postage::watch;
|
||||
use proto::ProtoClient;
|
||||
use rand::prelude::*;
|
||||
use release_channel::{AppVersion, ReleaseChannel};
|
||||
use rpc::proto::{AnyTypedEnvelope, EntityMessage, EnvelopedMessage, PeerId, RequestMessage};
|
||||
@ -1408,6 +1410,11 @@ impl Client {
|
||||
self.peer.send(self.connection_id()?, message)
|
||||
}
|
||||
|
||||
fn send_dynamic(&self, envelope: proto::Envelope) -> Result<()> {
|
||||
let connection_id = self.connection_id()?;
|
||||
self.peer.send_dynamic(connection_id, envelope)
|
||||
}
|
||||
|
||||
pub fn request<T: RequestMessage>(
|
||||
&self,
|
||||
request: T,
|
||||
@ -1606,6 +1613,20 @@ impl Client {
|
||||
}
|
||||
}
|
||||
|
||||
impl ProtoClient for Client {
|
||||
fn request(
|
||||
&self,
|
||||
envelope: proto::Envelope,
|
||||
request_type: &'static str,
|
||||
) -> BoxFuture<'static, Result<proto::Envelope>> {
|
||||
self.request_dynamic(envelope, request_type).boxed()
|
||||
}
|
||||
|
||||
fn send(&self, envelope: proto::Envelope) -> Result<()> {
|
||||
self.send_dynamic(envelope)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct DevelopmentCredentials {
|
||||
user_id: u64,
|
||||
|
@ -3327,7 +3327,7 @@ async fn test_local_settings(
|
||||
let store = cx.global::<SettingsStore>();
|
||||
assert_eq!(
|
||||
store
|
||||
.local_settings(worktree_b.read(cx).id().to_usize())
|
||||
.local_settings(worktree_b.entity_id().as_u64() as _)
|
||||
.collect::<Vec<_>>(),
|
||||
&[
|
||||
(Path::new("").into(), r#"{"tab_size":2}"#.to_string()),
|
||||
@ -3346,7 +3346,7 @@ async fn test_local_settings(
|
||||
let store = cx.global::<SettingsStore>();
|
||||
assert_eq!(
|
||||
store
|
||||
.local_settings(worktree_b.read(cx).id().to_usize())
|
||||
.local_settings(worktree_b.entity_id().as_u64() as _)
|
||||
.collect::<Vec<_>>(),
|
||||
&[
|
||||
(Path::new("").into(), r#"{}"#.to_string()),
|
||||
@ -3375,7 +3375,7 @@ async fn test_local_settings(
|
||||
let store = cx.global::<SettingsStore>();
|
||||
assert_eq!(
|
||||
store
|
||||
.local_settings(worktree_b.read(cx).id().to_usize())
|
||||
.local_settings(worktree_b.entity_id().as_u64() as _)
|
||||
.collect::<Vec<_>>(),
|
||||
&[
|
||||
(Path::new("a").into(), r#"{"tab_size":8}"#.to_string()),
|
||||
@ -3407,7 +3407,7 @@ async fn test_local_settings(
|
||||
let store = cx.global::<SettingsStore>();
|
||||
assert_eq!(
|
||||
store
|
||||
.local_settings(worktree_b.read(cx).id().to_usize())
|
||||
.local_settings(worktree_b.entity_id().as_u64() as _)
|
||||
.collect::<Vec<_>>(),
|
||||
&[(Path::new("a").into(), r#"{"hard_tabs":true}"#.to_string()),]
|
||||
)
|
||||
|
@ -1237,7 +1237,7 @@ impl RandomizedTest for ProjectCollaborationTest {
|
||||
}
|
||||
}
|
||||
|
||||
for buffer in guest_project.opened_buffers() {
|
||||
for buffer in guest_project.opened_buffers(cx) {
|
||||
let buffer = buffer.read(cx);
|
||||
assert_eq!(
|
||||
buffer.deferred_ops_len(),
|
||||
@ -1287,8 +1287,8 @@ impl RandomizedTest for ProjectCollaborationTest {
|
||||
for guest_buffer in guest_buffers {
|
||||
let buffer_id =
|
||||
guest_buffer.read_with(client_cx, |buffer, _| buffer.remote_id());
|
||||
let host_buffer = host_project.read_with(host_cx, |project, _| {
|
||||
project.buffer_for_id(buffer_id).unwrap_or_else(|| {
|
||||
let host_buffer = host_project.read_with(host_cx, |project, cx| {
|
||||
project.buffer_for_id(buffer_id, cx).unwrap_or_else(|| {
|
||||
panic!(
|
||||
"host does not have buffer for guest:{}, peer:{:?}, id:{}",
|
||||
client.username,
|
||||
|
@ -8529,7 +8529,7 @@ impl Editor {
|
||||
) -> Vec<(TaskSourceKind, TaskTemplate)> {
|
||||
let (inventory, worktree_id, file) = project.read_with(cx, |project, cx| {
|
||||
let (worktree_id, file) = project
|
||||
.buffer_for_id(runnable.buffer)
|
||||
.buffer_for_id(runnable.buffer, cx)
|
||||
.and_then(|buffer| buffer.read(cx).file())
|
||||
.map(|file| (WorktreeId::from_usize(file.worktree_id()), file.clone()))
|
||||
.unzip();
|
||||
|
@ -371,7 +371,7 @@ async fn update_editor_from_message(
|
||||
continue;
|
||||
};
|
||||
let buffer_id = BufferId::new(excerpt.buffer_id)?;
|
||||
let Some(buffer) = project.read(cx).buffer_for_id(buffer_id) else {
|
||||
let Some(buffer) = project.read(cx).buffer_for_id(buffer_id, cx) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
|
@ -1129,7 +1129,7 @@ impl OutlinePanel {
|
||||
EntryOwned::Entry(FsEntry::File(worktree_id, _, buffer_id, _)) => {
|
||||
let project = self.project.read(cx);
|
||||
let entry_id = project
|
||||
.buffer_for_id(buffer_id)
|
||||
.buffer_for_id(buffer_id, cx)
|
||||
.and_then(|buffer| buffer.read(cx).entry_id(cx));
|
||||
project
|
||||
.worktree_for_id(worktree_id, cx)
|
||||
@ -1147,7 +1147,7 @@ impl OutlinePanel {
|
||||
.remove(&CollapsedEntry::Excerpt(buffer_id, excerpt_id));
|
||||
let project = self.project.read(cx);
|
||||
let entry_id = project
|
||||
.buffer_for_id(buffer_id)
|
||||
.buffer_for_id(buffer_id, cx)
|
||||
.and_then(|buffer| buffer.read(cx).entry_id(cx));
|
||||
|
||||
entry_id.and_then(|entry_id| {
|
||||
|
928
crates/project/src/buffer_store.rs
Normal file
928
crates/project/src/buffer_store.rs
Normal file
@ -0,0 +1,928 @@
|
||||
use crate::ProjectPath;
|
||||
use anyhow::{anyhow, Context as _, Result};
|
||||
use collections::{hash_map, HashMap};
|
||||
use futures::{channel::oneshot, StreamExt as _};
|
||||
use gpui::{
|
||||
AppContext, AsyncAppContext, Context as _, EventEmitter, Model, ModelContext, Task, WeakModel,
|
||||
};
|
||||
use language::{
|
||||
proto::{deserialize_version, serialize_version, split_operations},
|
||||
Buffer, Capability, Language, Operation,
|
||||
};
|
||||
use rpc::{
|
||||
proto::{self, AnyProtoClient, PeerId},
|
||||
ErrorExt as _, TypedEnvelope,
|
||||
};
|
||||
use std::{io, path::Path, sync::Arc};
|
||||
use text::BufferId;
|
||||
use util::{debug_panic, maybe, ResultExt as _};
|
||||
use worktree::{File, ProjectEntryId, RemoteWorktree, Worktree};
|
||||
|
||||
/// A set of open buffers.
|
||||
pub struct BufferStore {
|
||||
retain_buffers: bool,
|
||||
opened_buffers: HashMap<BufferId, OpenBuffer>,
|
||||
local_buffer_ids_by_path: HashMap<ProjectPath, BufferId>,
|
||||
local_buffer_ids_by_entry_id: HashMap<ProjectEntryId, BufferId>,
|
||||
#[allow(clippy::type_complexity)]
|
||||
loading_buffers_by_path: HashMap<
|
||||
ProjectPath,
|
||||
postage::watch::Receiver<Option<Result<Model<Buffer>, Arc<anyhow::Error>>>>,
|
||||
>,
|
||||
loading_remote_buffers_by_id: HashMap<BufferId, Model<Buffer>>,
|
||||
remote_buffer_listeners:
|
||||
HashMap<BufferId, Vec<oneshot::Sender<Result<Model<Buffer>, anyhow::Error>>>>,
|
||||
}
|
||||
|
||||
enum OpenBuffer {
|
||||
Strong(Model<Buffer>),
|
||||
Weak(WeakModel<Buffer>),
|
||||
Operations(Vec<Operation>),
|
||||
}
|
||||
|
||||
pub enum BufferStoreEvent {
|
||||
BufferAdded(Model<Buffer>),
|
||||
BufferChangedFilePath {
|
||||
buffer: Model<Buffer>,
|
||||
old_file: Option<Arc<File>>,
|
||||
},
|
||||
BufferSaved {
|
||||
buffer: Model<Buffer>,
|
||||
has_changed_file: bool,
|
||||
saved_version: clock::Global,
|
||||
},
|
||||
}
|
||||
|
||||
impl EventEmitter<BufferStoreEvent> for BufferStore {}
|
||||
|
||||
impl BufferStore {
|
||||
/// Creates a buffer store, optionally retaining its buffers.
|
||||
///
|
||||
/// If `retain_buffers` is `true`, then buffers are owned by the buffer store
|
||||
/// and won't be released unless they are explicitly removed, or `retain_buffers`
|
||||
/// is set to `false` via `set_retain_buffers`. Otherwise, buffers are stored as
|
||||
/// weak handles.
|
||||
pub fn new(retain_buffers: bool) -> Self {
|
||||
Self {
|
||||
retain_buffers,
|
||||
opened_buffers: Default::default(),
|
||||
remote_buffer_listeners: Default::default(),
|
||||
loading_remote_buffers_by_id: Default::default(),
|
||||
local_buffer_ids_by_path: Default::default(),
|
||||
local_buffer_ids_by_entry_id: Default::default(),
|
||||
loading_buffers_by_path: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_buffer(
|
||||
&mut self,
|
||||
project_path: ProjectPath,
|
||||
worktree: Model<Worktree>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<Model<Buffer>>> {
|
||||
let existing_buffer = self.get_by_path(&project_path, cx);
|
||||
if let Some(existing_buffer) = existing_buffer {
|
||||
return Task::ready(Ok(existing_buffer));
|
||||
}
|
||||
|
||||
let loading_watch = match self.loading_buffers_by_path.entry(project_path.clone()) {
|
||||
// If the given path is already being loaded, then wait for that existing
|
||||
// task to complete and return the same buffer.
|
||||
hash_map::Entry::Occupied(e) => e.get().clone(),
|
||||
|
||||
// Otherwise, record the fact that this path is now being loaded.
|
||||
hash_map::Entry::Vacant(entry) => {
|
||||
let (mut tx, rx) = postage::watch::channel();
|
||||
entry.insert(rx.clone());
|
||||
|
||||
let project_path = project_path.clone();
|
||||
let load_buffer = match worktree.read(cx) {
|
||||
Worktree::Local(_) => {
|
||||
self.open_local_buffer_internal(project_path.path.clone(), worktree, cx)
|
||||
}
|
||||
Worktree::Remote(tree) => {
|
||||
self.open_remote_buffer_internal(&project_path.path, tree, cx)
|
||||
}
|
||||
};
|
||||
|
||||
cx.spawn(move |this, mut cx| async move {
|
||||
let load_result = load_buffer.await;
|
||||
*tx.borrow_mut() = Some(this.update(&mut cx, |this, _| {
|
||||
// Record the fact that the buffer is no longer loading.
|
||||
this.loading_buffers_by_path.remove(&project_path);
|
||||
let buffer = load_result.map_err(Arc::new)?;
|
||||
Ok(buffer)
|
||||
})?);
|
||||
anyhow::Ok(())
|
||||
})
|
||||
.detach();
|
||||
rx
|
||||
}
|
||||
};
|
||||
|
||||
cx.background_executor().spawn(async move {
|
||||
Self::wait_for_loading_buffer(loading_watch)
|
||||
.await
|
||||
.map_err(|e| e.cloned())
|
||||
})
|
||||
}
|
||||
|
||||
fn open_local_buffer_internal(
|
||||
&mut self,
|
||||
path: Arc<Path>,
|
||||
worktree: Model<Worktree>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<Model<Buffer>>> {
|
||||
let load_buffer = worktree.update(cx, |worktree, cx| {
|
||||
let load_file = worktree.load_file(path.as_ref(), cx);
|
||||
let reservation = cx.reserve_model();
|
||||
let buffer_id = BufferId::from(reservation.entity_id().as_non_zero_u64());
|
||||
cx.spawn(move |_, mut cx| async move {
|
||||
let loaded = load_file.await?;
|
||||
let text_buffer = cx
|
||||
.background_executor()
|
||||
.spawn(async move { text::Buffer::new(0, buffer_id, loaded.text) })
|
||||
.await;
|
||||
cx.insert_model(reservation, |_| {
|
||||
Buffer::build(
|
||||
text_buffer,
|
||||
loaded.diff_base,
|
||||
Some(loaded.file),
|
||||
Capability::ReadWrite,
|
||||
)
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
cx.spawn(move |this, mut cx| async move {
|
||||
let buffer = match load_buffer.await {
|
||||
Ok(buffer) => Ok(buffer),
|
||||
Err(error) if is_not_found_error(&error) => cx.new_model(|cx| {
|
||||
let buffer_id = BufferId::from(cx.entity_id().as_non_zero_u64());
|
||||
let text_buffer = text::Buffer::new(0, buffer_id, "".into());
|
||||
Buffer::build(
|
||||
text_buffer,
|
||||
None,
|
||||
Some(Arc::new(File {
|
||||
worktree,
|
||||
path,
|
||||
mtime: None,
|
||||
entry_id: None,
|
||||
is_local: true,
|
||||
is_deleted: false,
|
||||
is_private: false,
|
||||
})),
|
||||
Capability::ReadWrite,
|
||||
)
|
||||
}),
|
||||
Err(e) => Err(e),
|
||||
}?;
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.add_buffer(buffer.clone(), cx).log_err();
|
||||
})?;
|
||||
Ok(buffer)
|
||||
})
|
||||
}
|
||||
|
||||
fn open_remote_buffer_internal(
|
||||
&self,
|
||||
path: &Arc<Path>,
|
||||
worktree: &RemoteWorktree,
|
||||
cx: &ModelContext<Self>,
|
||||
) -> Task<Result<Model<Buffer>>> {
|
||||
let worktree_id = worktree.id().to_proto();
|
||||
let project_id = worktree.project_id();
|
||||
let client = worktree.client();
|
||||
let path_string = path.clone().to_string_lossy().to_string();
|
||||
cx.spawn(move |this, mut cx| async move {
|
||||
let response = client
|
||||
.request(proto::OpenBufferByPath {
|
||||
project_id,
|
||||
worktree_id,
|
||||
path: path_string,
|
||||
})
|
||||
.await?;
|
||||
let buffer_id = BufferId::new(response.buffer_id)?;
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.wait_for_remote_buffer(buffer_id, cx)
|
||||
})?
|
||||
.await
|
||||
})
|
||||
}
|
||||
|
||||
pub fn create_buffer(
|
||||
&mut self,
|
||||
remote_client: Option<(AnyProtoClient, u64)>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<Model<Buffer>>> {
|
||||
if let Some((remote_client, project_id)) = remote_client {
|
||||
let create = remote_client.request(proto::OpenNewBuffer { project_id });
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
let response = create.await?;
|
||||
let buffer_id = BufferId::new(response.buffer_id)?;
|
||||
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.wait_for_remote_buffer(buffer_id, cx)
|
||||
})?
|
||||
.await
|
||||
})
|
||||
} else {
|
||||
Task::ready(Ok(self.create_local_buffer("", None, cx)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_local_buffer(
|
||||
&mut self,
|
||||
text: &str,
|
||||
language: Option<Arc<Language>>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Model<Buffer> {
|
||||
let buffer = cx.new_model(|cx| {
|
||||
Buffer::local(text, cx)
|
||||
.with_language(language.unwrap_or_else(|| language::PLAIN_TEXT.clone()), cx)
|
||||
});
|
||||
self.add_buffer(buffer.clone(), cx).log_err();
|
||||
buffer
|
||||
}
|
||||
|
||||
pub fn save_buffer(
|
||||
&mut self,
|
||||
buffer: Model<Buffer>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
let Some(file) = File::from_dyn(buffer.read(cx).file()) else {
|
||||
return Task::ready(Err(anyhow!("buffer doesn't have a file")));
|
||||
};
|
||||
match file.worktree.read(cx) {
|
||||
Worktree::Local(_) => {
|
||||
self.save_local_buffer(file.worktree.clone(), buffer, file.path.clone(), false, cx)
|
||||
}
|
||||
Worktree::Remote(tree) => self.save_remote_buffer(buffer, None, tree, cx),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_buffer_as(
|
||||
&mut self,
|
||||
buffer: Model<Buffer>,
|
||||
path: ProjectPath,
|
||||
worktree: Model<Worktree>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
let old_file = File::from_dyn(buffer.read(cx).file())
|
||||
.cloned()
|
||||
.map(Arc::new);
|
||||
|
||||
let task = match worktree.read(cx) {
|
||||
Worktree::Local(_) => {
|
||||
self.save_local_buffer(worktree, buffer.clone(), path.path, true, cx)
|
||||
}
|
||||
Worktree::Remote(tree) => {
|
||||
self.save_remote_buffer(buffer.clone(), Some(path.to_proto()), tree, cx)
|
||||
}
|
||||
};
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
task.await?;
|
||||
this.update(&mut cx, |_, cx| {
|
||||
cx.emit(BufferStoreEvent::BufferChangedFilePath { buffer, old_file });
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn save_local_buffer(
|
||||
&self,
|
||||
worktree: Model<Worktree>,
|
||||
buffer_handle: Model<Buffer>,
|
||||
path: Arc<Path>,
|
||||
mut has_changed_file: bool,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
let buffer = buffer_handle.read(cx);
|
||||
let text = buffer.as_rope().clone();
|
||||
let line_ending = buffer.line_ending();
|
||||
let version = buffer.version();
|
||||
if buffer.file().is_some_and(|file| !file.is_created()) {
|
||||
has_changed_file = true;
|
||||
}
|
||||
|
||||
let save = worktree.update(cx, |worktree, cx| {
|
||||
worktree.write_file(path.as_ref(), text, line_ending, cx)
|
||||
});
|
||||
|
||||
cx.spawn(move |this, mut cx| async move {
|
||||
let new_file = save.await?;
|
||||
let mtime = new_file.mtime;
|
||||
buffer_handle.update(&mut cx, |buffer, cx| {
|
||||
if has_changed_file {
|
||||
buffer.file_updated(new_file, cx);
|
||||
}
|
||||
buffer.did_save(version.clone(), mtime, cx);
|
||||
})?;
|
||||
this.update(&mut cx, |_, cx| {
|
||||
cx.emit(BufferStoreEvent::BufferSaved {
|
||||
buffer: buffer_handle,
|
||||
has_changed_file,
|
||||
saved_version: version,
|
||||
})
|
||||
})?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn save_remote_buffer(
|
||||
&self,
|
||||
buffer_handle: Model<Buffer>,
|
||||
new_path: Option<proto::ProjectPath>,
|
||||
tree: &RemoteWorktree,
|
||||
cx: &ModelContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
let buffer = buffer_handle.read(cx);
|
||||
let buffer_id = buffer.remote_id().into();
|
||||
let version = buffer.version();
|
||||
let rpc = tree.client();
|
||||
let project_id = tree.project_id();
|
||||
cx.spawn(move |_, mut cx| async move {
|
||||
let response = rpc
|
||||
.request(proto::SaveBuffer {
|
||||
project_id,
|
||||
buffer_id,
|
||||
new_path,
|
||||
version: serialize_version(&version),
|
||||
})
|
||||
.await?;
|
||||
let version = deserialize_version(&response.version);
|
||||
let mtime = response.mtime.map(|mtime| mtime.into());
|
||||
|
||||
buffer_handle.update(&mut cx, |buffer, cx| {
|
||||
buffer.did_save(version.clone(), mtime, cx);
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn add_buffer(&mut self, buffer: Model<Buffer>, cx: &mut ModelContext<Self>) -> Result<()> {
|
||||
let remote_id = buffer.read(cx).remote_id();
|
||||
let is_remote = buffer.read(cx).replica_id() != 0;
|
||||
let open_buffer = if self.retain_buffers {
|
||||
OpenBuffer::Strong(buffer.clone())
|
||||
} else {
|
||||
OpenBuffer::Weak(buffer.downgrade())
|
||||
};
|
||||
|
||||
match self.opened_buffers.entry(remote_id) {
|
||||
hash_map::Entry::Vacant(entry) => {
|
||||
entry.insert(open_buffer);
|
||||
}
|
||||
hash_map::Entry::Occupied(mut entry) => {
|
||||
if let OpenBuffer::Operations(operations) = entry.get_mut() {
|
||||
buffer.update(cx, |b, cx| b.apply_ops(operations.drain(..), cx))?;
|
||||
} else if entry.get().upgrade().is_some() {
|
||||
if is_remote {
|
||||
return Ok(());
|
||||
} else {
|
||||
debug_panic!("buffer {} was already registered", remote_id);
|
||||
Err(anyhow!("buffer {} was already registered", remote_id))?;
|
||||
}
|
||||
}
|
||||
entry.insert(open_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(senders) = self.remote_buffer_listeners.remove(&remote_id) {
|
||||
for sender in senders {
|
||||
sender.send(Ok(buffer.clone())).ok();
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
|
||||
if file.is_local {
|
||||
self.local_buffer_ids_by_path.insert(
|
||||
ProjectPath {
|
||||
worktree_id: file.worktree_id(cx),
|
||||
path: file.path.clone(),
|
||||
},
|
||||
remote_id,
|
||||
);
|
||||
|
||||
if let Some(entry_id) = file.entry_id {
|
||||
self.local_buffer_ids_by_entry_id
|
||||
.insert(entry_id, remote_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cx.emit(BufferStoreEvent::BufferAdded(buffer));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn buffers(&self) -> impl '_ + Iterator<Item = Model<Buffer>> {
|
||||
self.opened_buffers
|
||||
.values()
|
||||
.filter_map(|buffer| buffer.upgrade())
|
||||
}
|
||||
|
||||
pub fn loading_buffers(
|
||||
&self,
|
||||
) -> impl Iterator<
|
||||
Item = (
|
||||
&ProjectPath,
|
||||
postage::watch::Receiver<Option<Result<Model<Buffer>, Arc<anyhow::Error>>>>,
|
||||
),
|
||||
> {
|
||||
self.loading_buffers_by_path
|
||||
.iter()
|
||||
.map(|(path, rx)| (path, rx.clone()))
|
||||
}
|
||||
|
||||
pub fn get_by_path(&self, path: &ProjectPath, cx: &AppContext) -> Option<Model<Buffer>> {
|
||||
self.buffers().find_map(|buffer| {
|
||||
let file = File::from_dyn(buffer.read(cx).file())?;
|
||||
if file.worktree_id(cx) == path.worktree_id && &file.path == &path.path {
|
||||
Some(buffer)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get(&self, buffer_id: BufferId) -> Option<Model<Buffer>> {
|
||||
self.opened_buffers
|
||||
.get(&buffer_id)
|
||||
.and_then(|buffer| buffer.upgrade())
|
||||
}
|
||||
|
||||
pub fn get_existing(&self, buffer_id: BufferId) -> Result<Model<Buffer>> {
|
||||
self.get(buffer_id)
|
||||
.ok_or_else(|| anyhow!("unknown buffer id {}", buffer_id))
|
||||
}
|
||||
|
||||
pub fn get_possibly_incomplete(&self, buffer_id: BufferId) -> Option<Model<Buffer>> {
|
||||
self.get(buffer_id)
|
||||
.or_else(|| self.loading_remote_buffers_by_id.get(&buffer_id).cloned())
|
||||
}
|
||||
|
||||
fn get_or_remove_by_path(
|
||||
&mut self,
|
||||
entry_id: ProjectEntryId,
|
||||
project_path: &ProjectPath,
|
||||
) -> Option<(BufferId, Model<Buffer>)> {
|
||||
let buffer_id = match self.local_buffer_ids_by_entry_id.get(&entry_id) {
|
||||
Some(&buffer_id) => buffer_id,
|
||||
None => match self.local_buffer_ids_by_path.get(project_path) {
|
||||
Some(&buffer_id) => buffer_id,
|
||||
None => {
|
||||
return None;
|
||||
}
|
||||
},
|
||||
};
|
||||
let buffer = if let Some(buffer) = self.get(buffer_id) {
|
||||
buffer
|
||||
} else {
|
||||
self.opened_buffers.remove(&buffer_id);
|
||||
self.local_buffer_ids_by_path.remove(project_path);
|
||||
self.local_buffer_ids_by_entry_id.remove(&entry_id);
|
||||
return None;
|
||||
};
|
||||
Some((buffer_id, buffer))
|
||||
}
|
||||
|
||||
pub fn wait_for_remote_buffer(
|
||||
&mut self,
|
||||
id: BufferId,
|
||||
cx: &mut AppContext,
|
||||
) -> Task<Result<Model<Buffer>>> {
|
||||
let buffer = self.get(id);
|
||||
if let Some(buffer) = buffer {
|
||||
return Task::ready(Ok(buffer));
|
||||
}
|
||||
let (tx, rx) = oneshot::channel();
|
||||
self.remote_buffer_listeners.entry(id).or_default().push(tx);
|
||||
cx.background_executor().spawn(async move { rx.await? })
|
||||
}
|
||||
|
||||
pub fn buffer_version_info(
|
||||
&self,
|
||||
cx: &AppContext,
|
||||
) -> (Vec<proto::BufferVersion>, Vec<BufferId>) {
|
||||
let buffers = self
|
||||
.buffers()
|
||||
.map(|buffer| {
|
||||
let buffer = buffer.read(cx);
|
||||
proto::BufferVersion {
|
||||
id: buffer.remote_id().into(),
|
||||
version: language::proto::serialize_version(&buffer.version),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let incomplete_buffer_ids = self
|
||||
.loading_remote_buffers_by_id
|
||||
.keys()
|
||||
.copied()
|
||||
.collect::<Vec<_>>();
|
||||
(buffers, incomplete_buffer_ids)
|
||||
}
|
||||
|
||||
pub fn disconnected_from_host(&mut self, cx: &mut AppContext) {
|
||||
self.set_retain_buffers(false, cx);
|
||||
|
||||
for buffer in self.buffers() {
|
||||
buffer.update(cx, |buffer, cx| {
|
||||
buffer.set_capability(Capability::ReadOnly, cx)
|
||||
});
|
||||
}
|
||||
|
||||
// Wake up all futures currently waiting on a buffer to get opened,
|
||||
// to give them a chance to fail now that we've disconnected.
|
||||
self.remote_buffer_listeners.clear();
|
||||
}
|
||||
|
||||
pub fn set_retain_buffers(&mut self, retain_buffers: bool, cx: &mut AppContext) {
|
||||
self.retain_buffers = retain_buffers;
|
||||
for open_buffer in self.opened_buffers.values_mut() {
|
||||
if retain_buffers {
|
||||
if let OpenBuffer::Weak(buffer) = open_buffer {
|
||||
if let Some(buffer) = buffer.upgrade() {
|
||||
*open_buffer = OpenBuffer::Strong(buffer);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if let Some(buffer) = open_buffer.upgrade() {
|
||||
buffer.update(cx, |buffer, _| buffer.give_up_waiting());
|
||||
}
|
||||
if let OpenBuffer::Strong(buffer) = open_buffer {
|
||||
*open_buffer = OpenBuffer::Weak(buffer.downgrade());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn discard_incomplete(&mut self) {
|
||||
self.opened_buffers
|
||||
.retain(|_, buffer| !matches!(buffer, OpenBuffer::Operations(_)));
|
||||
}
|
||||
|
||||
pub fn file_changed(
|
||||
&mut self,
|
||||
path: Arc<Path>,
|
||||
entry_id: ProjectEntryId,
|
||||
worktree_handle: &Model<worktree::Worktree>,
|
||||
snapshot: &worktree::Snapshot,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Option<(Model<Buffer>, Arc<File>, Arc<File>)> {
|
||||
let (buffer_id, buffer) = self.get_or_remove_by_path(
|
||||
entry_id,
|
||||
&ProjectPath {
|
||||
worktree_id: snapshot.id(),
|
||||
path,
|
||||
},
|
||||
)?;
|
||||
|
||||
let result = buffer.update(cx, |buffer, cx| {
|
||||
let old_file = File::from_dyn(buffer.file())?;
|
||||
if old_file.worktree != *worktree_handle {
|
||||
return None;
|
||||
}
|
||||
|
||||
let new_file = if let Some(entry) = old_file
|
||||
.entry_id
|
||||
.and_then(|entry_id| snapshot.entry_for_id(entry_id))
|
||||
{
|
||||
File {
|
||||
is_local: true,
|
||||
entry_id: Some(entry.id),
|
||||
mtime: entry.mtime,
|
||||
path: entry.path.clone(),
|
||||
worktree: worktree_handle.clone(),
|
||||
is_deleted: false,
|
||||
is_private: entry.is_private,
|
||||
}
|
||||
} else if let Some(entry) = snapshot.entry_for_path(old_file.path.as_ref()) {
|
||||
File {
|
||||
is_local: true,
|
||||
entry_id: Some(entry.id),
|
||||
mtime: entry.mtime,
|
||||
path: entry.path.clone(),
|
||||
worktree: worktree_handle.clone(),
|
||||
is_deleted: false,
|
||||
is_private: entry.is_private,
|
||||
}
|
||||
} else {
|
||||
File {
|
||||
is_local: true,
|
||||
entry_id: old_file.entry_id,
|
||||
path: old_file.path.clone(),
|
||||
mtime: old_file.mtime,
|
||||
worktree: worktree_handle.clone(),
|
||||
is_deleted: true,
|
||||
is_private: old_file.is_private,
|
||||
}
|
||||
};
|
||||
|
||||
if new_file == *old_file {
|
||||
return None;
|
||||
}
|
||||
|
||||
let old_file = Arc::new(old_file.clone());
|
||||
let new_file = Arc::new(new_file);
|
||||
buffer.file_updated(new_file.clone(), cx);
|
||||
Some((cx.handle(), old_file, new_file))
|
||||
});
|
||||
|
||||
if let Some((buffer, old_file, new_file)) = &result {
|
||||
if new_file.path != old_file.path {
|
||||
self.local_buffer_ids_by_path.remove(&ProjectPath {
|
||||
path: old_file.path.clone(),
|
||||
worktree_id: old_file.worktree_id(cx),
|
||||
});
|
||||
self.local_buffer_ids_by_path.insert(
|
||||
ProjectPath {
|
||||
worktree_id: new_file.worktree_id(cx),
|
||||
path: new_file.path.clone(),
|
||||
},
|
||||
buffer_id,
|
||||
);
|
||||
cx.emit(BufferStoreEvent::BufferChangedFilePath {
|
||||
buffer: buffer.clone(),
|
||||
old_file: Some(old_file.clone()),
|
||||
});
|
||||
}
|
||||
|
||||
if new_file.entry_id != old_file.entry_id {
|
||||
if let Some(entry_id) = old_file.entry_id {
|
||||
self.local_buffer_ids_by_entry_id.remove(&entry_id);
|
||||
}
|
||||
if let Some(entry_id) = new_file.entry_id {
|
||||
self.local_buffer_ids_by_entry_id
|
||||
.insert(entry_id, buffer_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn buffer_changed_file(
|
||||
&mut self,
|
||||
buffer: Model<Buffer>,
|
||||
cx: &mut AppContext,
|
||||
) -> Option<()> {
|
||||
let file = File::from_dyn(buffer.read(cx).file())?;
|
||||
|
||||
let remote_id = buffer.read(cx).remote_id();
|
||||
if let Some(entry_id) = file.entry_id {
|
||||
match self.local_buffer_ids_by_entry_id.get(&entry_id) {
|
||||
Some(_) => {
|
||||
return None;
|
||||
}
|
||||
None => {
|
||||
self.local_buffer_ids_by_entry_id
|
||||
.insert(entry_id, remote_id);
|
||||
}
|
||||
}
|
||||
};
|
||||
self.local_buffer_ids_by_path.insert(
|
||||
ProjectPath {
|
||||
worktree_id: file.worktree_id(cx),
|
||||
path: file.path.clone(),
|
||||
},
|
||||
remote_id,
|
||||
);
|
||||
|
||||
Some(())
|
||||
}
|
||||
|
||||
pub async fn create_buffer_for_peer(
|
||||
this: Model<Self>,
|
||||
peer_id: PeerId,
|
||||
buffer_id: BufferId,
|
||||
project_id: u64,
|
||||
client: AnyProtoClient,
|
||||
cx: &mut AsyncAppContext,
|
||||
) -> Result<()> {
|
||||
let Some(buffer) = this.update(cx, |this, _| this.get(buffer_id))? else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let operations = buffer.update(cx, |b, cx| b.serialize_ops(None, cx))?;
|
||||
let operations = operations.await;
|
||||
let state = buffer.update(cx, |buffer, _| buffer.to_proto())?;
|
||||
|
||||
let initial_state = proto::CreateBufferForPeer {
|
||||
project_id,
|
||||
peer_id: Some(peer_id),
|
||||
variant: Some(proto::create_buffer_for_peer::Variant::State(state)),
|
||||
};
|
||||
|
||||
if client.send(initial_state).log_err().is_some() {
|
||||
let client = client.clone();
|
||||
cx.background_executor()
|
||||
.spawn(async move {
|
||||
let mut chunks = split_operations(operations).peekable();
|
||||
while let Some(chunk) = chunks.next() {
|
||||
let is_last = chunks.peek().is_none();
|
||||
client.send(proto::CreateBufferForPeer {
|
||||
project_id,
|
||||
peer_id: Some(peer_id),
|
||||
variant: Some(proto::create_buffer_for_peer::Variant::Chunk(
|
||||
proto::BufferChunk {
|
||||
buffer_id: buffer_id.into(),
|
||||
operations: chunk,
|
||||
is_last,
|
||||
},
|
||||
)),
|
||||
})?;
|
||||
}
|
||||
anyhow::Ok(())
|
||||
})
|
||||
.await
|
||||
.log_err();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn handle_update_buffer(
|
||||
&mut self,
|
||||
envelope: TypedEnvelope<proto::UpdateBuffer>,
|
||||
is_remote: bool,
|
||||
cx: &mut AppContext,
|
||||
) -> Result<proto::Ack> {
|
||||
let payload = envelope.payload.clone();
|
||||
let buffer_id = BufferId::new(payload.buffer_id)?;
|
||||
let ops = payload
|
||||
.operations
|
||||
.into_iter()
|
||||
.map(language::proto::deserialize_operation)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
match self.opened_buffers.entry(buffer_id) {
|
||||
hash_map::Entry::Occupied(mut e) => match e.get_mut() {
|
||||
OpenBuffer::Strong(buffer) => {
|
||||
buffer.update(cx, |buffer, cx| buffer.apply_ops(ops, cx))?;
|
||||
}
|
||||
OpenBuffer::Operations(operations) => operations.extend_from_slice(&ops),
|
||||
OpenBuffer::Weak(_) => {}
|
||||
},
|
||||
hash_map::Entry::Vacant(e) => {
|
||||
if !is_remote {
|
||||
debug_panic!(
|
||||
"received buffer update from {:?}",
|
||||
envelope.original_sender_id
|
||||
);
|
||||
return Err(anyhow!("received buffer update for non-remote project"));
|
||||
}
|
||||
e.insert(OpenBuffer::Operations(ops));
|
||||
}
|
||||
}
|
||||
Ok(proto::Ack {})
|
||||
}
|
||||
|
||||
pub fn handle_create_buffer_for_peer(
|
||||
&mut self,
|
||||
envelope: TypedEnvelope<proto::CreateBufferForPeer>,
|
||||
mut worktrees: impl Iterator<Item = Model<Worktree>>,
|
||||
replica_id: u16,
|
||||
capability: Capability,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Result<()> {
|
||||
match envelope
|
||||
.payload
|
||||
.variant
|
||||
.ok_or_else(|| anyhow!("missing variant"))?
|
||||
{
|
||||
proto::create_buffer_for_peer::Variant::State(mut state) => {
|
||||
let buffer_id = BufferId::new(state.id)?;
|
||||
|
||||
let buffer_result = maybe!({
|
||||
let mut buffer_file = None;
|
||||
if let Some(file) = state.file.take() {
|
||||
let worktree_id = worktree::WorktreeId::from_proto(file.worktree_id);
|
||||
let worktree = worktrees
|
||||
.find(|worktree| worktree.read(cx).id() == worktree_id)
|
||||
.ok_or_else(|| {
|
||||
anyhow!("no worktree found for id {}", file.worktree_id)
|
||||
})?;
|
||||
buffer_file = Some(Arc::new(File::from_proto(file, worktree.clone(), cx)?)
|
||||
as Arc<dyn language::File>);
|
||||
}
|
||||
Buffer::from_proto(replica_id, capability, state, buffer_file)
|
||||
});
|
||||
|
||||
match buffer_result {
|
||||
Ok(buffer) => {
|
||||
let buffer = cx.new_model(|_| buffer);
|
||||
self.loading_remote_buffers_by_id.insert(buffer_id, buffer);
|
||||
}
|
||||
Err(error) => {
|
||||
if let Some(listeners) = self.remote_buffer_listeners.remove(&buffer_id) {
|
||||
for listener in listeners {
|
||||
listener.send(Err(anyhow!(error.cloned()))).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
proto::create_buffer_for_peer::Variant::Chunk(chunk) => {
|
||||
let buffer_id = BufferId::new(chunk.buffer_id)?;
|
||||
let buffer = self
|
||||
.loading_remote_buffers_by_id
|
||||
.get(&buffer_id)
|
||||
.cloned()
|
||||
.ok_or_else(|| {
|
||||
anyhow!(
|
||||
"received chunk for buffer {} without initial state",
|
||||
chunk.buffer_id
|
||||
)
|
||||
})?;
|
||||
|
||||
let result = maybe!({
|
||||
let operations = chunk
|
||||
.operations
|
||||
.into_iter()
|
||||
.map(language::proto::deserialize_operation)
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
buffer.update(cx, |buffer, cx| buffer.apply_ops(operations, cx))
|
||||
});
|
||||
|
||||
if let Err(error) = result {
|
||||
self.loading_remote_buffers_by_id.remove(&buffer_id);
|
||||
if let Some(listeners) = self.remote_buffer_listeners.remove(&buffer_id) {
|
||||
for listener in listeners {
|
||||
listener.send(Err(error.cloned())).ok();
|
||||
}
|
||||
}
|
||||
} else if chunk.is_last {
|
||||
self.loading_remote_buffers_by_id.remove(&buffer_id);
|
||||
self.add_buffer(buffer, cx)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn handle_save_buffer(
|
||||
this: Model<Self>,
|
||||
project_id: u64,
|
||||
worktree: Option<Model<Worktree>>,
|
||||
envelope: TypedEnvelope<proto::SaveBuffer>,
|
||||
mut cx: AsyncAppContext,
|
||||
) -> Result<proto::BufferSaved> {
|
||||
let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
|
||||
let buffer = this.update(&mut cx, |this, _| this.get_existing(buffer_id))??;
|
||||
buffer
|
||||
.update(&mut cx, |buffer, _| {
|
||||
buffer.wait_for_version(deserialize_version(&envelope.payload.version))
|
||||
})?
|
||||
.await?;
|
||||
let buffer_id = buffer.update(&mut cx, |buffer, _| buffer.remote_id())?;
|
||||
|
||||
if let Some(new_path) = envelope.payload.new_path {
|
||||
let worktree = worktree.context("no such worktree")?;
|
||||
let new_path = ProjectPath::from_proto(new_path);
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.save_buffer_as(buffer.clone(), new_path, worktree, cx)
|
||||
})?
|
||||
.await?;
|
||||
} else {
|
||||
this.update(&mut cx, |this, cx| this.save_buffer(buffer.clone(), cx))?
|
||||
.await?;
|
||||
}
|
||||
|
||||
buffer.update(&mut cx, |buffer, _| proto::BufferSaved {
|
||||
project_id,
|
||||
buffer_id: buffer_id.into(),
|
||||
version: serialize_version(buffer.saved_version()),
|
||||
mtime: buffer.saved_mtime().map(|time| time.into()),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn wait_for_loading_buffer(
|
||||
mut receiver: postage::watch::Receiver<Option<Result<Model<Buffer>, Arc<anyhow::Error>>>>,
|
||||
) -> Result<Model<Buffer>, Arc<anyhow::Error>> {
|
||||
loop {
|
||||
if let Some(result) = receiver.borrow().as_ref() {
|
||||
match result {
|
||||
Ok(buffer) => return Ok(buffer.to_owned()),
|
||||
Err(e) => return Err(e.to_owned()),
|
||||
}
|
||||
}
|
||||
receiver.next().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenBuffer {
|
||||
fn upgrade(&self) -> Option<Model<Buffer>> {
|
||||
match self {
|
||||
OpenBuffer::Strong(handle) => Some(handle.clone()),
|
||||
OpenBuffer::Weak(handle) => handle.upgrade(),
|
||||
OpenBuffer::Operations(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_not_found_error(error: &anyhow::Error) -> bool {
|
||||
error
|
||||
.root_cause()
|
||||
.downcast_ref::<io::Error>()
|
||||
.is_some_and(|err| err.kind() == io::ErrorKind::NotFound)
|
||||
}
|
@ -410,16 +410,18 @@ impl LspCommand for PerformRename {
|
||||
message: proto::PerformRenameResponse,
|
||||
project: Model<Project>,
|
||||
_: Model<Buffer>,
|
||||
mut cx: AsyncAppContext,
|
||||
cx: AsyncAppContext,
|
||||
) -> Result<ProjectTransaction> {
|
||||
let message = message
|
||||
.transaction
|
||||
.ok_or_else(|| anyhow!("missing transaction"))?;
|
||||
project
|
||||
.update(&mut cx, |project, cx| {
|
||||
project.deserialize_project_transaction(message, self.push_to_history, cx)
|
||||
})?
|
||||
.await
|
||||
Project::deserialize_project_transaction(
|
||||
project.downgrade(),
|
||||
message,
|
||||
self.push_to_history,
|
||||
cx,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
fn buffer_id_from_proto(message: &proto::PerformRename) -> Result<BufferId> {
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -3056,15 +3056,8 @@ async fn test_rescan_and_remote_updates(cx: &mut gpui::TestAppContext) {
|
||||
});
|
||||
});
|
||||
|
||||
let remote = cx.update(|cx| {
|
||||
Worktree::remote(
|
||||
0,
|
||||
1,
|
||||
metadata,
|
||||
Box::new(CollabRemoteWorktreeClient(project.read(cx).client())),
|
||||
cx,
|
||||
)
|
||||
});
|
||||
let remote =
|
||||
cx.update(|cx| Worktree::remote(0, 1, metadata, project.read(cx).client().into(), cx));
|
||||
|
||||
cx.executor().run_until_parked();
|
||||
|
||||
|
@ -19,6 +19,7 @@ doctest = false
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
collections.workspace = true
|
||||
futures.workspace = true
|
||||
prost.workspace = true
|
||||
serde.workspace = true
|
||||
|
||||
|
@ -7,18 +7,19 @@ mod typed_envelope;
|
||||
pub use error::*;
|
||||
pub use typed_envelope::*;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use collections::HashMap;
|
||||
use futures::{future::BoxFuture, Future};
|
||||
pub use prost::{DecodeError, Message};
|
||||
use serde::Serialize;
|
||||
use std::any::{Any, TypeId};
|
||||
use std::time::Instant;
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
cmp,
|
||||
fmt::Debug,
|
||||
iter,
|
||||
time::{Duration, SystemTime, UNIX_EPOCH},
|
||||
fmt::{self, Debug},
|
||||
iter, mem,
|
||||
sync::Arc,
|
||||
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
use std::{fmt, mem};
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/zed.messages.rs"));
|
||||
|
||||
@ -59,6 +60,51 @@ pub enum MessagePriority {
|
||||
Background,
|
||||
}
|
||||
|
||||
pub trait ProtoClient: Send + Sync {
|
||||
fn request(
|
||||
&self,
|
||||
envelope: Envelope,
|
||||
request_type: &'static str,
|
||||
) -> BoxFuture<'static, anyhow::Result<Envelope>>;
|
||||
|
||||
fn send(&self, envelope: Envelope) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AnyProtoClient(Arc<dyn ProtoClient>);
|
||||
|
||||
impl<T> From<Arc<T>> for AnyProtoClient
|
||||
where
|
||||
T: ProtoClient + 'static,
|
||||
{
|
||||
fn from(client: Arc<T>) -> Self {
|
||||
Self(client)
|
||||
}
|
||||
}
|
||||
|
||||
impl AnyProtoClient {
|
||||
pub fn new<T: ProtoClient + 'static>(client: Arc<T>) -> Self {
|
||||
Self(client)
|
||||
}
|
||||
|
||||
pub fn request<T: RequestMessage>(
|
||||
&self,
|
||||
request: T,
|
||||
) -> impl Future<Output = anyhow::Result<T::Response>> {
|
||||
let envelope = request.into_envelope(0, None, None);
|
||||
let response = self.0.request(envelope, T::NAME);
|
||||
async move {
|
||||
T::Response::from_envelope(response.await?)
|
||||
.ok_or_else(|| anyhow!("received response of the wrong type"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send<T: EnvelopedMessage>(&self, request: T) -> anyhow::Result<()> {
|
||||
let envelope = request.into_envelope(0, None, None);
|
||||
self.0.send(envelope)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EnvelopedMessage> AnyTypedEnvelope for TypedEnvelope<T> {
|
||||
fn payload_type_id(&self) -> TypeId {
|
||||
TypeId::of::<T>()
|
||||
|
@ -557,6 +557,14 @@ impl Peer {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn send_dynamic(&self, receiver_id: ConnectionId, message: proto::Envelope) -> Result<()> {
|
||||
let connection = self.connection_state(receiver_id)?;
|
||||
connection
|
||||
.outgoing_tx
|
||||
.unbounded_send(proto::Message::Envelope(message))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn forward_send<T: EnvelopedMessage>(
|
||||
&self,
|
||||
sender_id: ConnectionId,
|
||||
|
@ -37,7 +37,7 @@ use postage::{
|
||||
prelude::{Sink as _, Stream as _},
|
||||
watch,
|
||||
};
|
||||
use rpc::proto::{self, EnvelopedMessage as _, RequestMessage};
|
||||
use rpc::proto::{self, AnyProtoClient};
|
||||
use settings::{Settings, SettingsLocation, SettingsStore};
|
||||
use smol::channel::{self, Sender};
|
||||
use std::{
|
||||
@ -131,7 +131,7 @@ pub struct RemoteWorktree {
|
||||
snapshot: Snapshot,
|
||||
background_snapshot: Arc<Mutex<Snapshot>>,
|
||||
project_id: u64,
|
||||
client: Box<dyn RemoteWorktreeClient>,
|
||||
client: AnyProtoClient,
|
||||
updates_tx: Option<UnboundedSender<proto::UpdateWorktree>>,
|
||||
update_observer: Arc<
|
||||
Mutex<Option<Box<dyn Send + FnMut(proto::UpdateWorktree) -> BoxFuture<'static, bool>>>>,
|
||||
@ -142,14 +142,6 @@ pub struct RemoteWorktree {
|
||||
disconnected: bool,
|
||||
}
|
||||
|
||||
pub trait RemoteWorktreeClient {
|
||||
fn request(
|
||||
&self,
|
||||
envelope: proto::Envelope,
|
||||
request_type: &'static str,
|
||||
) -> BoxFuture<'static, Result<proto::Envelope>>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Snapshot {
|
||||
id: WorktreeId,
|
||||
@ -461,7 +453,7 @@ impl Worktree {
|
||||
project_id: u64,
|
||||
replica_id: ReplicaId,
|
||||
worktree: proto::WorktreeMetadata,
|
||||
client: Box<dyn RemoteWorktreeClient>,
|
||||
client: AnyProtoClient,
|
||||
cx: &mut AppContext,
|
||||
) -> Model<Self> {
|
||||
cx.new_model(|cx: &mut ModelContext<Self>| {
|
||||
@ -706,7 +698,7 @@ impl Worktree {
|
||||
Worktree::Local(this) => this.create_entry(path, is_directory, cx),
|
||||
Worktree::Remote(this) => {
|
||||
let project_id = this.project_id;
|
||||
let request = this.rpc_request(proto::CreateProjectEntry {
|
||||
let request = this.client.request(proto::CreateProjectEntry {
|
||||
worktree_id: worktree_id.to_proto(),
|
||||
project_id,
|
||||
path: path.to_string_lossy().into(),
|
||||
@ -748,7 +740,7 @@ impl Worktree {
|
||||
match self {
|
||||
Worktree::Local(this) => this.delete_entry(entry_id, trash, cx),
|
||||
Worktree::Remote(this) => {
|
||||
let response = this.rpc_request(proto::DeleteProjectEntry {
|
||||
let response = this.client.request(proto::DeleteProjectEntry {
|
||||
project_id: this.project_id,
|
||||
entry_id: entry_id.to_proto(),
|
||||
use_trash: trash,
|
||||
@ -778,7 +770,7 @@ impl Worktree {
|
||||
match self {
|
||||
Worktree::Local(this) => this.rename_entry(entry_id, new_path, cx),
|
||||
Worktree::Remote(this) => {
|
||||
let response = this.rpc_request(proto::RenameProjectEntry {
|
||||
let response = this.client.request(proto::RenameProjectEntry {
|
||||
project_id: this.project_id,
|
||||
entry_id: entry_id.to_proto(),
|
||||
new_path: new_path.to_string_lossy().into(),
|
||||
@ -820,7 +812,7 @@ impl Worktree {
|
||||
match self {
|
||||
Worktree::Local(this) => this.copy_entry(entry_id, new_path, cx),
|
||||
Worktree::Remote(this) => {
|
||||
let response = this.rpc_request(proto::CopyProjectEntry {
|
||||
let response = this.client.request(proto::CopyProjectEntry {
|
||||
project_id: this.project_id,
|
||||
entry_id: entry_id.to_proto(),
|
||||
new_path: new_path.to_string_lossy().into(),
|
||||
@ -870,7 +862,7 @@ impl Worktree {
|
||||
match self {
|
||||
Worktree::Local(this) => this.expand_entry(entry_id, cx),
|
||||
Worktree::Remote(this) => {
|
||||
let response = this.rpc_request(proto::ExpandProjectEntry {
|
||||
let response = this.client.request(proto::ExpandProjectEntry {
|
||||
project_id: this.project_id,
|
||||
entry_id: entry_id.to_proto(),
|
||||
});
|
||||
@ -1811,24 +1803,20 @@ impl LocalWorktree {
|
||||
}
|
||||
|
||||
impl RemoteWorktree {
|
||||
pub fn project_id(&self) -> u64 {
|
||||
self.project_id
|
||||
}
|
||||
|
||||
pub fn client(&self) -> AnyProtoClient {
|
||||
self.client.clone()
|
||||
}
|
||||
|
||||
pub fn disconnected_from_host(&mut self) {
|
||||
self.updates_tx.take();
|
||||
self.snapshot_subscriptions.clear();
|
||||
self.disconnected = true;
|
||||
}
|
||||
|
||||
fn rpc_request<T: RequestMessage>(
|
||||
&self,
|
||||
request: T,
|
||||
) -> impl Future<Output = Result<T::Response>> {
|
||||
let envelope = request.into_envelope(0, None, None);
|
||||
let response = self.client.request(envelope, T::NAME);
|
||||
async move {
|
||||
T::Response::from_envelope(response.await?)
|
||||
.ok_or_else(|| anyhow!("received response of the wrong type"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_from_remote(&mut self, update: proto::UpdateWorktree) {
|
||||
if let Some(updates_tx) = &self.updates_tx {
|
||||
updates_tx
|
||||
|
Loading…
Reference in New Issue
Block a user