Send buffer operations via the Project instead of Worktree

This commit is contained in:
Antonio Scandurra 2022-03-08 11:11:25 +01:00
parent b49951ac57
commit d6c8fdb3c4
5 changed files with 101 additions and 112 deletions

View file

@ -17,8 +17,8 @@ use gpui::{
use language::{
proto::{deserialize_anchor, deserialize_version, serialize_anchor, serialize_version},
range_from_lsp, Anchor, AnchorRangeExt, Bias, Buffer, CodeAction, CodeLabel, Completion,
Diagnostic, DiagnosticEntry, File as _, Language, LanguageRegistry, Operation, PointUtf16,
ToLspPosition, ToOffset, ToPointUtf16, Transaction,
Diagnostic, DiagnosticEntry, Event as BufferEvent, File as _, Language, LanguageRegistry,
Operation, PointUtf16, ToLspPosition, ToOffset, ToPointUtf16, Transaction,
};
use lsp::{DiagnosticSeverity, DocumentHighlightKind, LanguageServer};
use lsp_command::*;
@ -945,10 +945,37 @@ impl Project {
remote_id
))?,
}
self.assign_language_to_buffer(&buffer, worktree, cx);
cx.become_delegate(buffer, Self::on_buffer_event).detach();
self.assign_language_to_buffer(buffer, worktree, cx);
Ok(())
}
fn on_buffer_event(
&mut self,
buffer: ModelHandle<Buffer>,
event: BufferEvent,
cx: &mut ModelContext<Self>,
) {
match event {
BufferEvent::Operation(operation) => {
if let Some(project_id) = self.remote_id() {
let request = self.client.request(proto::UpdateBuffer {
project_id,
buffer_id: buffer.read(cx).remote_id(),
operations: vec![language::proto::serialize_operation(&operation)],
});
cx.foreground()
.spawn(async move {
request.await.log_err();
})
.detach();
}
}
_ => {}
}
}
fn assign_language_to_buffer(
&mut self,
buffer: &ModelHandle<Buffer>,
@ -4452,7 +4479,10 @@ mod tests {
buffer1.update(cx, |buffer, cx| {
cx.subscribe(&buffer1, {
let events = events.clone();
move |_, _, event, _| events.borrow_mut().push(event.clone())
move |_, _, event, _| match event {
BufferEvent::Operation(_) => {}
_ => events.borrow_mut().push(event.clone()),
}
})
.detach();

View file

@ -19,7 +19,7 @@ use gpui::{
};
use language::{
proto::{deserialize_version, serialize_version},
Buffer, DiagnosticEntry, Operation, PointUtf16, Rope,
Buffer, DiagnosticEntry, PointUtf16, Rope,
};
use lazy_static::lazy_static;
use parking_lot::Mutex;
@ -71,7 +71,6 @@ pub struct LocalWorktree {
share: Option<ShareState>,
diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<PointUtf16>>>,
diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
queued_operations: Vec<(u64, Operation)>,
client: Arc<Client>,
fs: Arc<dyn Fs>,
visible: bool,
@ -84,7 +83,6 @@ pub struct RemoteWorktree {
client: Arc<Client>,
updates_tx: UnboundedSender<proto::UpdateWorktree>,
replica_id: ReplicaId,
queued_operations: Vec<(u64, Operation)>,
diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
visible: bool,
}
@ -226,7 +224,6 @@ impl Worktree {
snapshot_rx: snapshot_rx.clone(),
updates_tx,
client: client.clone(),
queued_operations: Default::default(),
diagnostic_summaries: TreeMap::from_ordered_entries(
worktree.diagnostic_summaries.into_iter().map(|summary| {
(
@ -420,42 +417,6 @@ impl Worktree {
cx.notify();
}
fn send_buffer_update(
&mut self,
buffer_id: u64,
operation: Operation,
cx: &mut ModelContext<Self>,
) {
if let Some((project_id, rpc)) = match self {
Worktree::Local(worktree) => worktree
.share
.as_ref()
.map(|share| (share.project_id, worktree.client.clone())),
Worktree::Remote(worktree) => Some((worktree.project_id, worktree.client.clone())),
} {
cx.spawn(|worktree, mut cx| async move {
if let Err(error) = rpc
.request(proto::UpdateBuffer {
project_id,
buffer_id,
operations: vec![language::proto::serialize_operation(&operation)],
})
.await
{
worktree.update(&mut cx, |worktree, _| {
log::error!("error sending buffer operation: {}", error);
match worktree {
Worktree::Local(t) => &mut t.queued_operations,
Worktree::Remote(t) => &mut t.queued_operations,
}
.push((buffer_id, operation));
});
}
})
.detach();
}
}
}
impl LocalWorktree {
@ -526,7 +487,6 @@ impl LocalWorktree {
poll_task: None,
diagnostics: Default::default(),
diagnostic_summaries: Default::default(),
queued_operations: Default::default(),
client,
fs,
visible,
@ -1455,12 +1415,6 @@ impl language::File for File {
})
}
fn buffer_updated(&self, buffer_id: u64, operation: Operation, cx: &mut MutableAppContext) {
self.worktree.update(cx, |worktree, cx| {
worktree.send_buffer_update(buffer_id, operation, cx);
});
}
fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext) {
self.worktree.update(cx, |worktree, _| {
if let Worktree::Remote(worktree) = worktree {