@@ -13,10 +13,6 @@ use std::future::Future;
1313use std::path::{Path, PathBuf};
1414use std::str::FromStr;
1515use std::sync::Arc;
16−use tokio::sync::mpsc::error::TryRecvError;
17−use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
18−
19−use tokio::sync::Mutex;
2016
2117use helix_core::{
2218 encoding,
@@ -134,9 +130,6 @@ pub struct Document {
134130 last_saved_revision: usize,
135131 version: i32, // should be usize?
136132 pub(crate) modified_since_accessed: bool,
137− save_sender: Option<UnboundedSender<DocumentSavedEventFuture>>,
138− save_receiver: Option<UnboundedReceiver<DocumentSavedEventFuture>>,
139− current_save: Arc<Mutex<Option<DocumentSavedEventFuture>>>,
140133
141134 diagnostics: Vec<Diagnostic>,
142135 language_server: Option<Arc<helix_lsp::Client>>,
@@ -357,7 +350,6 @@ impl Document {
357350 let encoding = encoding.unwrap_or(encoding::UTF_8);
358351 let changes = ChangeSet::new(&text);
359352 let old_state = None;
360− let (save_sender, save_receiver) = tokio::sync::mpsc::unbounded_channel();
361353
362354 Self {
363355 id: DocumentId::default(),
@@ -378,9 +370,6 @@ impl Document {
378370 savepoint: None,
379371 last_saved_revision: 0,
380372 modified_since_accessed: false,
381− save_sender: Some(save_sender),
382− save_receiver: Some(save_receiver),
383− current_save: Arc::new(Mutex::new(None)),
384373 language_server: None,
385374 }
386375 }
@@ -519,21 +508,26 @@ impl Document {
519508 &mut self,
520509 path: Option<P>,
521510 force: bool,
522− ) -> Result<(), anyhow::Error> {
523− self.save_impl::<futures_util::future::Ready<_>, _>(path, force)
511+ ) -> Result<
512+ impl Future<Output = Result<DocumentSavedEvent, anyhow::Error>> + 'static + Send,
513+ anyhow::Error,
514+ > {
515+ let path = path.map(|path| path.into());
516+ self.save_impl(path, force)
517+
518+ // futures_util::future::Ready<_>,
524519 }
525520
526521 /// The `Document`'s text is encoded according to its encoding and written to the file located
527522 /// at its `path()`.
528− fn save_impl<F, P>(&mut self, path: Option<P>, force: bool) -> Result<(), anyhow::Error>
529− where
530− F: Future<Output = Result<Transaction, FormatterError>> + 'static + Send,
531− P: Into<PathBuf>,
532− {
533− if self.save_sender.is_none() {
534− bail!("saves are closed for this document!");
535− }
536−
523+ fn save_impl(
524+ &mut self,
525+ path: Option<PathBuf>,
526+ force: bool,
527+ ) -> Result<
528+ impl Future<Output = Result<DocumentSavedEvent, anyhow::Error>> + 'static + Send,
529+ anyhow::Error,
530+ > {
537531 log::debug!(
538532 "submitting save of doc '{:?}'",
539533 self.path().map(|path| path.to_string_lossy())
@@ -544,7 +538,7 @@ impl Document {
544538 let text = self.text().clone();
545539
546540 let path = match path {
547− Some(path) => helix_core::path::get_canonicalized_path(&path.into())?,
541+ Some(path) => helix_core::path::get_canonicalized_path(&path)?,
548542 None => {
549543 if self.path.is_none() {
550544 bail!("Can't save with no path set!");
@@ -564,7 +558,7 @@ impl Document {
564558 let encoding = self.encoding;
565559
566560 // We encode the file according to the `Document`'s encoding.
567− let save_event = async move {
561+ let future = async move {
568562 use tokio::fs::File;
569563 if let Some(parent) = path.parent() {
570564 // TODO: display a prompt asking the user if the directories should be created
@@ -604,107 +598,15 @@ impl Document {
604598 Ok(event)
605599 };
606600
607− self.save_sender
608− .as_mut()
609− .unwrap()
610− .send(Box::pin(save_event))
611− .map_err(|err| anyhow!("failed to send save event: {}", err))
612− }
613−
614− pub async fn await_save(&mut self) -> Option<DocumentSavedEventResult> {
615− self.await_save_impl(true).await
616− }
617−
618− async fn await_save_impl(&mut self, block: bool) -> Option<DocumentSavedEventResult> {
619− let mut current_save = self.current_save.lock().await;
620− if let Some(ref mut save) = *current_save {
621− log::trace!("reawaiting save of '{:?}'", self.path());
622− let result = save.await;
623− *current_save = None;
624− log::trace!("reawait save of '{:?}' result: {:?}", self.path(), result);
625− return Some(result);
626− }
627−
628− // return early if the receiver is closed
629− let rx = self.save_receiver.as_mut()?;
630−
631− let save_req = if block {
632− rx.recv().await
633− } else {
634− let msg = rx.try_recv();
635−
636− if let Err(err) = msg {
637− match err {
638− TryRecvError::Empty => return None,
639− TryRecvError::Disconnected => None,
640− }
641− } else {
642− msg.ok()
643− }
644− };
645−
646− let save = match save_req {
647− Some(save) => save,
648− None => {
649− self.save_receiver = None;
650− return None;
651− }
652− };
653−
654− // save a handle to the future so that when a poll on this
655− // function gets cancelled, we don't lose it
656− *current_save = Some(save);
657− log::trace!("awaiting save of '{:?}'", self.path());
658−
659− let result = (*current_save).as_mut().unwrap().await;
660− *current_save = None;
661−
662− log::trace!("save of '{:?}' result: {:?}", self.path(), result);
663−
664− Some(result)
665− }
666−
667− /// Flushes the queue of pending writes. If any fail,
668− /// it stops early before emptying the rest of the queue.
669− pub async fn try_flush_saves(&mut self) -> Option<DocumentSavedEventResult> {
670− self.flush_saves_impl(false).await
671− }
672−
673− async fn flush_saves_impl(&mut self, block: bool) -> Option<DocumentSavedEventResult> {
674− let mut final_result = None;
675−
676− while let Some(save_event) = self.await_save_impl(block).await {
677− let is_err = match &save_event {
678− Ok(event) => {
679− self.set_last_saved_revision(event.revision);
680− false
681− }
682− Err(err) => {
683− log::error!(
684− "error saving document {:?}: {}",
685− self.path().map(|path| path.to_string_lossy()),
686− err
687− );
688− true
689− }
690− };
691−
692− final_result = Some(save_event);
693−
694− if is_err {
695− break;
696− }
697− }
698−
699− final_result
601+ Ok(future)
700602 }
701603
702604 /// Prepares the Document for being closed by stopping any new writes
703605 /// and flushing through the queue of pending writes. If any fail,
704606 /// it stops early before emptying the rest of the queue.
705607 pub async fn close(&mut self) -> Option<DocumentSavedEventResult> {
706− self.save_sender.take();
707− self.flush_saves_impl(true).await
608+ // TODO
609+ None
708610 }
709611
710612 /// Detect the programming language based on the file type.