@@ -91,6 +91,7 @@ impl Serialize for Mode {
91pub struct DocumentSaveEvent {
91pub struct DocumentSaveEvent {
92 pub revision: usize,
92 pub revision: usize,
93 pub doc_id: DocumentId,
93 pub doc_id: DocumentId,
96pub type DocumentSaveEventResult = Result<DocumentSaveEvent, anyhow::Error>;
97pub type DocumentSaveEventResult = Result<DocumentSaveEvent, anyhow::Error>;
@@ -512,41 +513,61 @@ impl Document {
512 Some(fut.boxed())
513 Some(fut.boxed())
516+ pub fn save<P: Into<PathBuf>>(
515− pub fn save(&mut self, force: bool) -> Result<(), anyhow::Error> {
520+ ) -> Result<(), anyhow::Error> {
516− self.save_impl::<futures_util::future::Ready<_>>(None, force)
521+ self.save_impl::<futures_util::future::Ready<_>, _>(None, path, force)
519− pub fn format_and_save(
524+ pub fn format_and_save<F, P>(
520 &mut self,
525 &mut self,
521− formatting: Option<
526+ formatting: Option<F>,
522− impl Future<Output = Result<Transaction, FormatterError>> + 'static + Send,
527+ path: Option<P>,
524 force: bool,
528 force: bool,
525− ) -> anyhow::Result<()> {
529+ ) -> anyhow::Result<()>
531+ F: Future<Output = Result<Transaction, FormatterError>> + 'static + Send,
526− self.save_impl(formatting, force)
534+ self.save_impl(formatting, path, force)
529− // TODO: impl Drop to handle ensuring writes when closed
530 /// The `Document`'s text is encoded according to its encoding and written to the file located
537 /// The `Document`'s text is encoded according to its encoding and written to the file located
531 /// at its `path()`.
538 /// at its `path()`.
533 /// If `formatting` is present, it supplies some changes that we apply to the text before saving.
540 /// If `formatting` is present, it supplies some changes that we apply to the text before saving.
534− fn save_impl<F: Future<Output = Result<Transaction, FormatterError>> + 'static + Send>(
541+ fn save_impl<F, P>(
535 &mut self,
542 &mut self,
536 formatting: Option<F>,
543 formatting: Option<F>,
537 force: bool,
545 force: bool,
538− ) -> Result<(), anyhow::Error> {
546+ ) -> Result<(), anyhow::Error>
548+ F: Future<Output = Result<Transaction, FormatterError>> + 'static + Send,
539 if self.save_sender.is_none() {
551 if self.save_sender.is_none() {
540 bail!("saves are closed for this document!");
552 bail!("saves are closed for this document!");
543 // we clone and move text + path into the future so that we asynchronously save the current
555 // we clone and move text + path into the future so that we asynchronously save the current
544 // state without blocking any further edits.
556 // state without blocking any further edits.
546 let mut text = self.text().clone();
557 let mut text = self.text().clone();
547− let path = self.path.clone().expect("Can't save with no path set!");
548− let identifier = self.identifier();
559+ let path = match path {
560+ Some(path) => helix_core::path::get_canonicalized_path(&path.into())?,
562+ if self.path.is_none() {
563+ bail!("Can't save with no path set!");
566+ self.path.as_ref().unwrap().clone()
570+ let identifier = self.identifier();
550 let language_server = self.language_server.clone();
571 let language_server = self.language_server.clone();
552 // mark changes up to now as saved
573 // mark changes up to now as saved
@@ -586,12 +607,13 @@ impl Document {
589− let mut file = File::create(path).await?;
610+ let mut file = File::create(&path).await?;
590 to_writer(&mut file, encoding, &text).await?;
611 to_writer(&mut file, encoding, &text).await?;
592 let event = DocumentSaveEvent {
613 let event = DocumentSaveEvent {
593 revision: current_rev,
614 revision: current_rev,
597 if let Some(language_server) = language_server {
619 if let Some(language_server) = language_server {