@@ -616,6 +616,10 @@ impl Document {
618 pub async fn await_save(&mut self) -> Option<DocumentSaveEventResult> {
618 pub async fn await_save(&mut self) -> Option<DocumentSaveEventResult> {
619+ self.await_save_impl(true).await
622+ async fn await_save_impl(&mut self, block: bool) -> Option<DocumentSaveEventResult> {
619 let mut current_save = self.current_save.lock().await;
623 let mut current_save = self.current_save.lock().await;
620 if let Some(ref mut save) = *current_save {
624 if let Some(ref mut save) = *current_save {
621 let result = save.await;
625 let result = save.await;
@@ -627,7 +631,15 @@ impl Document {
627 // return early if the receiver is closed
631 // return early if the receiver is closed
628 self.save_receiver.as_ref()?;
632 self.save_receiver.as_ref()?;
630− let save = match self.save_receiver.as_mut().unwrap().recv().await {
634+ let rx = self.save_receiver.as_mut().unwrap();
636+ let save_req = if block {
642+ let save = match save_req {
631 Some(save) => save,
643 Some(save) => save,
632 None => {
644 None => {
633 self.save_receiver = None;
645 self.save_receiver = None;
@@ -648,19 +660,24 @@ impl Document {
648 Some(result)
660 Some(result)
651− /// Prepares the Document for being closed by stopping any new writes
652− /// and flushing through the queue of pending writes. If any fail,
663+ /// Flushes the queue of pending writes. If any fail,
653− /// it stops early before emptying the rest of the queue. Callers
664+ /// it stops early before emptying the rest of the queue.
654− /// should keep calling until it returns None.
655− pub async fn close(&mut self) -> Option<DocumentSaveEventResult> {
665+ pub async fn try_flush_saves(&mut self) -> Option<DocumentSaveEventResult> {
656− if self.save_sender.is_some() {
666+ self.flush_saves_impl(false).await
657− self.save_sender = None;
669+ async fn flush_saves_impl(&mut self, block: bool) -> Option<DocumentSaveEventResult> {
660 let mut final_result = None;
670 let mut final_result = None;
662− while let Some(save_event) = self.await_save().await {
672+ while let Some(save_event) = self.await_save_impl(block).await {
663− let is_err = save_event.is_err();
673+ let is_err = match &save_event {
675+ self.set_last_saved_revision(event.revision);
664 final_result = Some(save_event);
681 final_result = Some(save_event);
666 if is_err {
683 if is_err {
@@ -671,6 +688,17 @@ impl Document {
671 final_result
688 final_result
691+ /// Prepares the Document for being closed by stopping any new writes
692+ /// and flushing through the queue of pending writes. If any fail,
693+ /// it stops early before emptying the rest of the queue.
694+ pub async fn close(&mut self) -> Option<DocumentSaveEventResult> {
695+ if self.save_sender.is_some() {
696+ self.save_sender = None;
699+ self.flush_saves_impl(true).await
674 /// Detect the programming language based on the file type.
702 /// Detect the programming language based on the file type.
675 pub fn detect_language(&mut self, config_loader: Arc<syntax::Loader>) {
703 pub fn detect_language(&mut self, config_loader: Arc<syntax::Loader>) {
676 if let Some(path) = &self.path {
704 if let Some(path) = &self.path {
@@ -1023,6 +1051,11 @@ impl Document {
1023 let history = self.history.take();
1051 let history = self.history.take();
1024 let current_revision = history.current_revision();
1052 let current_revision = history.current_revision();
1025 self.history.set(history);
1053 self.history.set(history);
1055+ "modified - last saved: {}, current: {}",
1056+ self.last_saved_revision,
1026 current_revision != self.last_saved_revision || !self.changes.is_empty()
1059 current_revision != self.last_saved_revision || !self.changes.is_empty()
@@ -1036,9 +1069,20 @@ impl Document {
1037 /// Set the document's latest saved revision to the given one.
1070 /// Set the document's latest saved revision to the given one.
1038 pub fn set_last_saved_revision(&mut self, rev: usize) {
1071 pub fn set_last_saved_revision(&mut self, rev: usize) {
1073+ "doc {} revision updated {} -> {}",
1075+ self.last_saved_revision,
1039 self.last_saved_revision = rev;
1078 self.last_saved_revision = rev;
1081+ /// Get the document's latest saved revision.
1082+ pub fn get_last_saved_revision(&mut self) -> usize {
1083+ self.last_saved_revision
1042 /// Get the current revision number
1086 /// Get the current revision number
1043 pub fn get_current_revision(&mut self) -> usize {
1087 pub fn get_current_revision(&mut self) -> usize {
1044 let history = self.history.take();
1088 let history = self.history.take();