Maintain remote buffers via UpdateBufferFile messages sent by host

This commit is contained in:
Antonio Scandurra 2022-01-24 09:32:40 +01:00
parent da13d028a3
commit 4372fe1ed0
4 changed files with 115 additions and 23 deletions

View file

@ -298,6 +298,7 @@ impl Project {
Self::handle_disk_based_diagnostics_updated,
),
client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer),
client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer_file),
client.subscribe_to_entity(remote_id, cx, Self::handle_buffer_saved),
],
client,
@ -1136,10 +1137,12 @@ impl Project {
fn add_worktree(&mut self, worktree: &ModelHandle<Worktree>, cx: &mut ModelContext<Self>) {
cx.observe(&worktree, |_, _, cx| cx.notify()).detach();
cx.subscribe(&worktree, |this, worktree, _, cx| {
this.update_open_buffers(worktree, cx)
})
.detach();
if worktree.read(cx).is_local() {
cx.subscribe(&worktree, |this, worktree, _, cx| {
this.update_local_worktree_buffers(worktree, cx);
})
.detach();
}
let push_weak_handle = {
let worktree = worktree.read(cx);
@ -1161,12 +1164,11 @@ impl Project {
cx.notify();
}
fn update_open_buffers(
fn update_local_worktree_buffers(
&mut self,
worktree_handle: ModelHandle<Worktree>,
cx: &mut ModelContext<Self>,
) {
let local = worktree_handle.read(cx).is_local();
let snapshot = worktree_handle.read(cx).snapshot();
let mut buffers_to_delete = Vec::new();
for (buffer_id, buffer) in &self.open_buffers {
@ -1183,7 +1185,7 @@ impl Project {
.and_then(|entry_id| snapshot.entry_for_id(entry_id))
{
File {
is_local: local,
is_local: true,
entry_id: Some(entry.id),
mtime: entry.mtime,
path: entry.path.clone(),
@ -1193,7 +1195,7 @@ impl Project {
snapshot.entry_for_path(old_file.path().as_ref())
{
File {
is_local: local,
is_local: true,
entry_id: Some(entry.id),
mtime: entry.mtime,
path: entry.path.clone(),
@ -1201,7 +1203,7 @@ impl Project {
}
} else {
File {
is_local: local,
is_local: true,
entry_id: None,
path: old_file.path().clone(),
mtime: old_file.mtime(),
@ -1209,9 +1211,18 @@ impl Project {
}
};
if let Some(task) = buffer.file_updated(Box::new(new_file), cx) {
task.detach();
if let Some(project_id) = self.remote_id() {
let client = self.client.clone();
let message = proto::UpdateBufferFile {
project_id,
buffer_id: *buffer_id as u64,
file: Some(new_file.to_proto()),
};
cx.foreground()
.spawn(async move { client.send(message).await })
.detach_and_log_err(cx);
}
buffer.file_updated(Box::new(new_file), cx).detach();
}
});
} else {
@ -1492,6 +1503,31 @@ impl Project {
Ok(())
}
pub fn handle_update_buffer_file(
&mut self,
envelope: TypedEnvelope<proto::UpdateBufferFile>,
_: Arc<Client>,
cx: &mut ModelContext<Self>,
) -> Result<()> {
let payload = envelope.payload.clone();
let buffer_id = payload.buffer_id as usize;
let file = payload.file.ok_or_else(|| anyhow!("invalid file"))?;
let worktree = self
.worktree_for_id(WorktreeId::from_proto(file.worktree_id), cx)
.ok_or_else(|| anyhow!("no such worktree"))?;
let file = File::from_proto(file, worktree.clone(), cx)?;
let buffer = self
.open_buffers
.get_mut(&buffer_id)
.and_then(|b| b.upgrade(cx))
.ok_or_else(|| anyhow!("no such buffer"))?;
buffer.update(cx, |buffer, cx| {
buffer.file_updated(Box::new(file), cx).detach();
});
Ok(())
}
pub fn handle_save_buffer(
&mut self,
envelope: TypedEnvelope<proto::SaveBuffer>,
@ -2181,7 +2217,12 @@ mod tests {
cx.update(|cx| {
let target_buffer = definition.target_buffer.read(cx);
assert_eq!(
target_buffer.file().unwrap().as_local().unwrap().abs_path(cx),
target_buffer
.file()
.unwrap()
.as_local()
.unwrap()
.abs_path(cx),
dir.path().join("a.rs")
);
assert_eq!(definition.target_range.to_offset(target_buffer), 9..10);