@@ -13,10 +13,6 @@ use std::future::Future;
13use std::path::{Path, PathBuf};
13use std::path::{Path, PathBuf};
14use std::str::FromStr;
14use std::str::FromStr;
15use std::sync::Arc;
15use std::sync::Arc;
16−use tokio::sync::mpsc::error::TryRecvError;
17−use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
19−use tokio::sync::Mutex;
21use helix_core::{
17use helix_core::{
@@ -134,9 +130,6 @@ pub struct Document {
134 last_saved_revision: usize,
130 last_saved_revision: usize,
135 version: i32, // should be usize?
131 version: i32, // should be usize?
136 pub(crate) modified_since_accessed: bool,
132 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>>>,
141 diagnostics: Vec<Diagnostic>,
134 diagnostics: Vec<Diagnostic>,
142 language_server: Option<Arc<helix_lsp::Client>>,
135 language_server: Option<Arc<helix_lsp::Client>>,
@@ -357,7 +350,6 @@ impl Document {
357 let encoding = encoding.unwrap_or(encoding::UTF_8);
350 let encoding = encoding.unwrap_or(encoding::UTF_8);
358 let changes = ChangeSet::new(&text);
351 let changes = ChangeSet::new(&text);
359 let old_state = None;
352 let old_state = None;
360− let (save_sender, save_receiver) = tokio::sync::mpsc::unbounded_channel();
363 id: DocumentId::default(),
355 id: DocumentId::default(),
@@ -378,9 +370,6 @@ impl Document {
378 savepoint: None,
370 savepoint: None,
379 last_saved_revision: 0,
371 last_saved_revision: 0,
380 modified_since_accessed: false,
372 modified_since_accessed: false,
381− save_sender: Some(save_sender),
382− save_receiver: Some(save_receiver),
383− current_save: Arc::new(Mutex::new(None)),
384 language_server: None,
373 language_server: None,
@@ -519,21 +508,26 @@ impl Document {
519 &mut self,
508 &mut self,
520 path: Option<P>,
509 path: Option<P>,
521 force: bool,
510 force: bool,
522− ) -> Result<(), anyhow::Error> {
511+ ) -> Result<
512+ impl Future<Output = Result<DocumentSavedEvent, anyhow::Error>> + 'static + Send,
515+ let path = path.map(|path| path.into());
516+ self.save_impl(path, force)
523− self.save_impl::<futures_util::future::Ready<_>, _>(path, force)
518+ // futures_util::future::Ready<_>,
526 /// The `Document`'s text is encoded according to its encoding and written to the file located
521 /// The `Document`'s text is encoded according to its encoding and written to the file located
527 /// at its `path()`.
522 /// at its `path()`.
528− fn save_impl<F, P>(&mut self, path: Option<P>, force: bool) -> Result<(), anyhow::Error>
523+ fn save_impl(
529− where
524+ &mut self,
525+ path: Option<PathBuf>,
530− F: Future<Output = Result<Transaction, FormatterError>> + 'static + Send,
528+ impl Future<Output = Result<DocumentSavedEvent, anyhow::Error>> + 'static + Send,
531− P: Into<PathBuf>,
529+ anyhow::Error,
533− if self.save_sender.is_none() {
534− bail!("saves are closed for this document!");
537 log::debug!(
531 log::debug!(
538 "submitting save of doc '{:?}'",
532 "submitting save of doc '{:?}'",
539 self.path().map(|path| path.to_string_lossy())
533 self.path().map(|path| path.to_string_lossy())
@@ -544,7 +538,7 @@ impl Document {
544 let text = self.text().clone();
538 let text = self.text().clone();
546 let path = match path {
540 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)?,
548 None => {
542 None => {
549 if self.path.is_none() {
543 if self.path.is_none() {
550 bail!("Can't save with no path set!");
544 bail!("Can't save with no path set!");
@@ -564,7 +558,7 @@ impl Document {
564 let encoding = self.encoding;
558 let encoding = self.encoding;
566 // We encode the file according to the `Document`'s encoding.
560 // We encode the file according to the `Document`'s encoding.
567− let save_event = async move {
561+ let future = async move {
568 use tokio::fs::File;
562 use tokio::fs::File;
569 if let Some(parent) = path.parent() {
563 if let Some(parent) = path.parent() {
570 // TODO: display a prompt asking the user if the directories should be created
564 // TODO: display a prompt asking the user if the directories should be created
@@ -604,107 +598,15 @@ impl Document {
604 Ok(event)
598 Ok(event)
607− self.save_sender
601+ Ok(future)
610− .send(Box::pin(save_event))
611− .map_err(|err| anyhow!("failed to send save event: {}", err))
614− pub async fn await_save(&mut self) -> Option<DocumentSavedEventResult> {
615− self.await_save_impl(true).await
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);
628− // return early if the receiver is closed
629− let rx = self.save_receiver.as_mut()?;
631− let save_req = if block {
634− let msg = rx.try_recv();
636− if let Err(err) = msg {
638− TryRecvError::Empty => return None,
639− TryRecvError::Disconnected => None,
646− let save = match save_req {
649− self.save_receiver = None;
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());
659− let result = (*current_save).as_mut().unwrap().await;
660− *current_save = None;
662− log::trace!("save of '{:?}' result: {:?}", self.path(), result);
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
673− async fn flush_saves_impl(&mut self, block: bool) -> Option<DocumentSavedEventResult> {
674− let mut final_result = None;
676− while let Some(save_event) = self.await_save_impl(block).await {
677− let is_err = match &save_event {
679− self.set_last_saved_revision(event.revision);
684− "error saving document {:?}: {}",
685− self.path().map(|path| path.to_string_lossy()),
692− final_result = Some(save_event);
702 /// Prepares the Document for being closed by stopping any new writes
604 /// Prepares the Document for being closed by stopping any new writes
703 /// and flushing through the queue of pending writes. If any fail,
605 /// and flushing through the queue of pending writes. If any fail,
704 /// it stops early before emptying the rest of the queue.
606 /// it stops early before emptying the rest of the queue.
705 pub async fn close(&mut self) -> Option<DocumentSavedEventResult> {
607 pub async fn close(&mut self) -> Option<DocumentSavedEventResult> {
706− self.save_sender.take();
608+ // TODO
707− self.flush_saves_impl(true).await
609+ None
710 /// Detect the programming language based on the file type.
612 /// Detect the programming language based on the file type.