Update buffer's saved mtime when file is reloaded after on-disk change

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-10-21 12:05:44 +02:00
parent eea0f35d38
commit eb9d7c8660
2 changed files with 35 additions and 72 deletions

View file

@ -300,45 +300,34 @@ impl Buffer {
cx.emit(Event::Saved);
}
pub fn file_renamed(&self, cx: &mut ModelContext<Self>) {
cx.emit(Event::FileHandleChanged);
}
pub fn file_updated(
&mut self,
path: Arc<Path>,
mtime: SystemTime,
new_text: Option<String>,
new_text: String,
cx: &mut ModelContext<Self>,
) {
let file = self.file.as_mut().unwrap();
let mut changed = false;
if path != *file.path() {
file.set_path(path);
changed = true;
}
if mtime != file.mtime() {
file.set_mtime(mtime);
changed = true;
if let Some(new_text) = new_text {
if self.version == self.saved_version {
cx.spawn(|this, mut cx| async move {
let diff = this
.read_with(&cx, |this, cx| this.diff(new_text.into(), cx))
.await;
this.update(&mut cx, |this, cx| {
if this.apply_diff(diff, cx) {
this.saved_version = this.version.clone();
this.saved_mtime = mtime;
cx.emit(Event::Reloaded);
}
});
})
.detach();
}
) -> Option<Task<()>> {
if let Some(file) = self.file.as_ref() {
cx.emit(Event::FileHandleChanged);
let mtime = file.mtime();
if self.version == self.saved_version {
return Some(cx.spawn(|this, mut cx| async move {
let diff = this
.read_with(&cx, |this, cx| this.diff(new_text.into(), cx))
.await;
this.update(&mut cx, |this, cx| {
if this.apply_diff(diff, cx) {
this.saved_version = this.version.clone();
this.saved_mtime = mtime;
cx.emit(Event::Reloaded);
}
});
}));
}
}
if changed {
cx.emit(Event::FileHandleChanged);
}
None
}
pub fn file_deleted(&mut self, cx: &mut ModelContext<Self>) {
@ -740,20 +729,6 @@ impl Buffer {
})
}
pub fn set_text_from_disk(&self, new_text: Arc<str>, cx: &mut ModelContext<Self>) -> Task<()> {
cx.spawn(|this, mut cx| async move {
let diff = this
.read_with(&cx, |this, cx| this.diff(new_text, cx))
.await;
this.update(&mut cx, |this, cx| {
if this.apply_diff(diff, cx) {
this.saved_version = this.version.clone();
}
});
})
}
fn apply_diff(&mut self, diff: Diff, cx: &mut ModelContext<Self>) -> bool {
if self.version == diff.base_version {
self.start_transaction(None).unwrap();