Move Buffer::save_as to Editor

This removes buffer's dependency on Worktree, preparing the path for us to pull it into its own crate.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo 2021-10-01 17:32:22 -06:00
parent 74a47a1384
commit fcf6a9d58a
3 changed files with 36 additions and 42 deletions

View file

@ -2656,8 +2656,26 @@ impl workspace::ItemView for Editor {
path: &Path, path: &Path,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Result<()>> { ) -> Task<Result<()>> {
self.buffer self.buffer.update(cx, |buffer, cx| {
.update(cx, |b, cx| b.save_as(worktree, path, cx)) let handle = cx.handle();
let text = buffer.as_rope().clone();
let version = buffer.version();
let save_as = worktree.update(cx, |worktree, cx| {
worktree
.as_local_mut()
.unwrap()
.save_buffer_as(handle, path, text, cx)
});
cx.spawn(|buffer, mut cx| async move {
save_as.await.map(|new_file| {
buffer.update(&mut cx, |buffer, cx| {
buffer.did_save(version, new_file.mtime, Some(new_file), cx);
});
})
})
})
} }
fn is_dirty(&self, cx: &AppContext) -> bool { fn is_dirty(&self, cx: &AppContext) -> bool {

View file

@ -9,14 +9,11 @@ use crate::{
settings::{HighlightId, HighlightMap}, settings::{HighlightId, HighlightMap},
time::{self, ReplicaId}, time::{self, ReplicaId},
util::Bias, util::Bias,
worktree::{File, Worktree}, worktree::File,
}; };
pub use anchor::*; pub use anchor::*;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use sum_tree::{self, FilterCursor, SumTree}; use gpui::{AppContext, Entity, ModelContext, Task};
use gpui::{
AppContext, Entity, ModelContext, ModelHandle, Task,
};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use operation_queue::OperationQueue; use operation_queue::OperationQueue;
use parking_lot::Mutex; use parking_lot::Mutex;
@ -37,6 +34,7 @@ use std::{
sync::Arc, sync::Arc,
time::{Duration, Instant, SystemTime, UNIX_EPOCH}, time::{Duration, Instant, SystemTime, UNIX_EPOCH},
}; };
use sum_tree::{self, FilterCursor, SumTree};
use tree_sitter::{InputEdit, Parser, QueryCursor}; use tree_sitter::{InputEdit, Parser, QueryCursor};
use zrpc::proto; use zrpc::proto;
@ -702,54 +700,32 @@ impl Buffer {
Ok(cx.spawn(|this, mut cx| async move { Ok(cx.spawn(|this, mut cx| async move {
let (version, mtime) = save.await?; let (version, mtime) = save.await?;
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
this.did_save(version.clone(), mtime, cx); this.did_save(version.clone(), mtime, None, cx);
}); });
Ok((version, mtime)) Ok((version, mtime))
})) }))
} }
pub fn save_as( pub fn as_rope(&self) -> &Rope {
&mut self, &self.visible_text
worktree: &ModelHandle<Worktree>,
path: impl Into<Arc<Path>>,
cx: &mut ModelContext<Self>,
) -> Task<Result<()>> {
let path = path.into();
let handle = cx.handle();
let text = self.visible_text.clone();
let version = self.version.clone();
let save_as = worktree.update(cx, |worktree, cx| {
worktree
.as_local_mut()
.unwrap()
.save_buffer_as(handle, path, text, cx)
});
cx.spawn(|this, mut cx| async move {
save_as.await.map(|new_file| {
this.update(&mut cx, |this, cx| {
if let Some(language) = new_file.select_language(cx) {
this.language = Some(language);
this.reparse(cx);
}
let mtime = new_file.mtime;
this.file = Some(new_file);
this.did_save(version, mtime, cx);
});
})
})
} }
pub fn did_save( pub fn did_save(
&mut self, &mut self,
version: time::Global, version: time::Global,
mtime: SystemTime, mtime: SystemTime,
new_file: Option<File>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) { ) {
self.saved_mtime = mtime; self.saved_mtime = mtime;
self.saved_version = version; self.saved_version = version;
if let Some(new_file) = new_file {
if let Some(language) = new_file.select_language(cx) {
self.language = Some(language);
self.reparse(cx);
}
self.file = Some(new_file);
}
cx.emit(Event::Saved); cx.emit(Event::Saved);
} }
@ -3438,7 +3414,7 @@ mod tests {
assert!(buffer.is_dirty()); assert!(buffer.is_dirty());
assert_eq!(*events.borrow(), &[Event::Edited, Event::Dirtied]); assert_eq!(*events.borrow(), &[Event::Edited, Event::Dirtied]);
events.borrow_mut().clear(); events.borrow_mut().clear();
buffer.did_save(buffer.version(), buffer.file().unwrap().mtime, cx); buffer.did_save(buffer.version(), buffer.file().unwrap().mtime, None, cx);
}); });
// after saving, the buffer is not dirty, and emits a saved event. // after saving, the buffer is not dirty, and emits a saved event.

View file

@ -518,7 +518,7 @@ impl Worktree {
.mtime .mtime
.ok_or_else(|| anyhow!("missing mtime"))? .ok_or_else(|| anyhow!("missing mtime"))?
.into(); .into();
buffer.did_save(version, mtime, cx); buffer.did_save(version, mtime, None, cx);
Result::<_, anyhow::Error>::Ok(()) Result::<_, anyhow::Error>::Ok(())
})?; })?;
} }