Move the save and save_as code paths close together
This commit is contained in:
parent
de6eb00e2b
commit
3a7cfc3901
7 changed files with 125 additions and 169 deletions
|
@ -724,18 +724,55 @@ impl LocalWorktree {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn save_buffer(
|
||||
&self,
|
||||
buffer_handle: ModelHandle<Buffer>,
|
||||
path: Arc<Path>,
|
||||
cx: &mut ModelContext<Worktree>,
|
||||
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
|
||||
let buffer = buffer_handle.read(cx);
|
||||
|
||||
let rpc = self.client.clone();
|
||||
let buffer_id = buffer.remote_id();
|
||||
let project_id = self.share.as_ref().map(|share| share.project_id);
|
||||
|
||||
let text = buffer.as_rope().clone();
|
||||
let fingerprint = text.fingerprint();
|
||||
let version = buffer.version();
|
||||
let save = self.write_file(path, text, buffer.line_ending(), cx);
|
||||
|
||||
cx.as_mut().spawn(|mut cx| async move {
|
||||
let mtime = save.await?.mtime;
|
||||
if let Some(project_id) = project_id {
|
||||
rpc.send(proto::BufferSaved {
|
||||
project_id,
|
||||
buffer_id,
|
||||
version: serialize_version(&version),
|
||||
mtime: Some(mtime.into()),
|
||||
fingerprint: serialize_fingerprint(fingerprint),
|
||||
})?;
|
||||
}
|
||||
buffer_handle.update(&mut cx, |buffer, cx| {
|
||||
buffer.did_save(version.clone(), fingerprint, mtime, None, cx);
|
||||
});
|
||||
anyhow::Ok((version, fingerprint, mtime))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn save_buffer_as(
|
||||
&self,
|
||||
buffer_handle: ModelHandle<Buffer>,
|
||||
path: impl Into<Arc<Path>>,
|
||||
cx: &mut ModelContext<Worktree>,
|
||||
) -> Task<Result<()>> {
|
||||
let handle = cx.handle();
|
||||
let buffer = buffer_handle.read(cx);
|
||||
|
||||
let text = buffer.as_rope().clone();
|
||||
let fingerprint = text.fingerprint();
|
||||
let version = buffer.version();
|
||||
let save = self.write_file(path, text, buffer.line_ending(), cx);
|
||||
let handle = cx.handle();
|
||||
|
||||
cx.as_mut().spawn(|mut cx| async move {
|
||||
let entry = save.await?;
|
||||
let file = File {
|
||||
|
@ -1085,6 +1122,39 @@ impl RemoteWorktree {
|
|||
self.disconnected = true;
|
||||
}
|
||||
|
||||
pub fn save_buffer(
|
||||
&self,
|
||||
buffer_handle: ModelHandle<Buffer>,
|
||||
cx: &mut ModelContext<Worktree>,
|
||||
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
|
||||
let buffer = buffer_handle.read(cx);
|
||||
let buffer_id = buffer.remote_id();
|
||||
let version = buffer.version();
|
||||
let rpc = self.client.clone();
|
||||
let project_id = self.project_id;
|
||||
cx.as_mut().spawn(|mut cx| async move {
|
||||
let response = rpc
|
||||
.request(proto::SaveBuffer {
|
||||
project_id,
|
||||
buffer_id,
|
||||
version: serialize_version(&version),
|
||||
})
|
||||
.await?;
|
||||
let version = deserialize_version(response.version);
|
||||
let fingerprint = deserialize_fingerprint(&response.fingerprint)?;
|
||||
let mtime = response
|
||||
.mtime
|
||||
.ok_or_else(|| anyhow!("missing mtime"))?
|
||||
.into();
|
||||
|
||||
buffer_handle.update(&mut cx, |buffer, cx| {
|
||||
buffer.did_save(version.clone(), fingerprint, mtime, None, cx);
|
||||
});
|
||||
|
||||
Ok((version, fingerprint, mtime))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update_from_remote(&mut self, update: proto::UpdateWorktree) {
|
||||
if let Some(updates_tx) = &self.updates_tx {
|
||||
updates_tx
|
||||
|
@ -1859,57 +1929,6 @@ impl language::File for File {
|
|||
self.is_deleted
|
||||
}
|
||||
|
||||
fn save(
|
||||
&self,
|
||||
buffer_id: u64,
|
||||
text: Rope,
|
||||
version: clock::Global,
|
||||
line_ending: LineEnding,
|
||||
cx: &mut MutableAppContext,
|
||||
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> {
|
||||
self.worktree.update(cx, |worktree, cx| match worktree {
|
||||
Worktree::Local(worktree) => {
|
||||
let rpc = worktree.client.clone();
|
||||
let project_id = worktree.share.as_ref().map(|share| share.project_id);
|
||||
let fingerprint = text.fingerprint();
|
||||
let save = worktree.write_file(self.path.clone(), text, line_ending, cx);
|
||||
cx.background().spawn(async move {
|
||||
let entry = save.await?;
|
||||
if let Some(project_id) = project_id {
|
||||
rpc.send(proto::BufferSaved {
|
||||
project_id,
|
||||
buffer_id,
|
||||
version: serialize_version(&version),
|
||||
mtime: Some(entry.mtime.into()),
|
||||
fingerprint: serialize_fingerprint(fingerprint),
|
||||
})?;
|
||||
}
|
||||
Ok((version, fingerprint, entry.mtime))
|
||||
})
|
||||
}
|
||||
Worktree::Remote(worktree) => {
|
||||
let rpc = worktree.client.clone();
|
||||
let project_id = worktree.project_id;
|
||||
cx.foreground().spawn(async move {
|
||||
let response = rpc
|
||||
.request(proto::SaveBuffer {
|
||||
project_id,
|
||||
buffer_id,
|
||||
version: serialize_version(&version),
|
||||
})
|
||||
.await?;
|
||||
let version = deserialize_version(response.version);
|
||||
let fingerprint = deserialize_fingerprint(&response.fingerprint)?;
|
||||
let mtime = response
|
||||
.mtime
|
||||
.ok_or_else(|| anyhow!("missing mtime"))?
|
||||
.into();
|
||||
Ok((version, fingerprint, mtime))
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue