@@ -550,6 +550,10 @@ impl Document {
550
550
}
551
551
552
552
pub async fn await_save ( & mut self ) -> Option < DocumentSaveEventResult > {
553
+ self . await_save_impl ( true ) . await
554
+ }
555
+
556
+ async fn await_save_impl ( & mut self , block : bool ) -> Option < DocumentSaveEventResult > {
553
557
let mut current_save = self . current_save . lock ( ) . await ;
554
558
if let Some ( ref mut save) = * current_save {
555
559
let result = save. await ;
@@ -561,7 +565,15 @@ impl Document {
561
565
// return early if the receiver is closed
562
566
self . save_receiver . as_ref ( ) ?;
563
567
564
- let save = match self . save_receiver . as_mut ( ) . unwrap ( ) . recv ( ) . await {
568
+ let rx = self . save_receiver . as_mut ( ) . unwrap ( ) ;
569
+
570
+ let save_req = if block {
571
+ rx. recv ( ) . await
572
+ } else {
573
+ rx. try_recv ( ) . ok ( )
574
+ } ;
575
+
576
+ let save = match save_req {
565
577
Some ( save) => save,
566
578
None => {
567
579
self . save_receiver = None ;
@@ -582,19 +594,24 @@ impl Document {
582
594
Some ( result)
583
595
}
584
596
585
- /// Prepares the Document for being closed by stopping any new writes
586
- /// and flushing through the queue of pending writes. If any fail,
587
- /// it stops early before emptying the rest of the queue. Callers
588
- /// should keep calling until it returns None.
589
- pub async fn close ( & mut self ) -> Option < DocumentSaveEventResult > {
590
- if self . save_sender . is_some ( ) {
591
- self . save_sender = None ;
592
- }
597
+ /// Flushes the queue of pending writes. If any fail,
598
+ /// it stops early before emptying the rest of the queue.
599
+ pub async fn try_flush_saves ( & mut self ) -> Option < DocumentSaveEventResult > {
600
+ self . flush_saves_impl ( false ) . await
601
+ }
593
602
603
+ async fn flush_saves_impl ( & mut self , block : bool ) -> Option < DocumentSaveEventResult > {
594
604
let mut final_result = None ;
595
605
596
- while let Some ( save_event) = self . await_save ( ) . await {
597
- let is_err = save_event. is_err ( ) ;
606
+ while let Some ( save_event) = self . await_save_impl ( block) . await {
607
+ let is_err = match & save_event {
608
+ Ok ( event) => {
609
+ self . set_last_saved_revision ( event. revision ) ;
610
+ false
611
+ }
612
+ Err ( _) => true ,
613
+ } ;
614
+
598
615
final_result = Some ( save_event) ;
599
616
600
617
if is_err {
@@ -605,6 +622,17 @@ impl Document {
605
622
final_result
606
623
}
607
624
625
+ /// Prepares the Document for being closed by stopping any new writes
626
+ /// and flushing through the queue of pending writes. If any fail,
627
+ /// it stops early before emptying the rest of the queue.
628
+ pub async fn close ( & mut self ) -> Option < DocumentSaveEventResult > {
629
+ if self . save_sender . is_some ( ) {
630
+ self . save_sender = None ;
631
+ }
632
+
633
+ self . flush_saves_impl ( true ) . await
634
+ }
635
+
608
636
/// Detect the programming language based on the file type.
609
637
pub fn detect_language ( & mut self , config_loader : Arc < syntax:: Loader > ) {
610
638
if let Some ( path) = & self . path {
@@ -955,6 +983,11 @@ impl Document {
955
983
let history = self . history . take ( ) ;
956
984
let current_revision = history. current_revision ( ) ;
957
985
self . history . set ( history) ;
986
+ log:: debug!(
987
+ "modified - last saved: {}, current: {}" ,
988
+ self . last_saved_revision,
989
+ current_revision
990
+ ) ;
958
991
current_revision != self . last_saved_revision || !self . changes . is_empty ( )
959
992
}
960
993
@@ -968,9 +1001,20 @@ impl Document {
968
1001
969
1002
/// Set the document's latest saved revision to the given one.
970
1003
pub fn set_last_saved_revision ( & mut self , rev : usize ) {
1004
+ log:: debug!(
1005
+ "doc {} revision updated {} -> {}" ,
1006
+ self . id,
1007
+ self . last_saved_revision,
1008
+ rev
1009
+ ) ;
971
1010
self . last_saved_revision = rev;
972
1011
}
973
1012
1013
+ /// Get the document's latest saved revision.
1014
+ pub fn get_last_saved_revision ( & mut self ) -> usize {
1015
+ self . last_saved_revision
1016
+ }
1017
+
974
1018
/// Get the current revision number
975
1019
pub fn get_current_revision ( & mut self ) -> usize {
976
1020
let history = self . history . take ( ) ;
0 commit comments