@@ -636,6 +636,7 @@ impl Document {
636
636
. into_std ( )
637
637
. await ;
638
638
tokio:: task:: spawn_blocking ( move || -> anyhow:: Result < ( ) > {
639
+ // Truncate the file if it's not a valid undofile.
639
640
if History :: deserialize ( & mut std:: fs:: File :: open ( & undofile_path) ?, & path)
640
641
. is_ok ( )
641
642
{
@@ -1386,7 +1387,8 @@ impl Display for FormatterError {
1386
1387
1387
1388
#[ cfg( test) ]
1388
1389
mod test {
1389
- use arc_swap:: ArcSwap ;
1390
+ use arc_swap:: { access:: Map , ArcSwap } ;
1391
+ use quickcheck:: Gen ;
1390
1392
1391
1393
use super :: * ;
1392
1394
@@ -1556,6 +1558,98 @@ mod test {
1556
1558
) ;
1557
1559
}
1558
1560
1561
+ #[ tokio:: test( flavor = "multi_thread" ) ]
1562
+ async fn reload_history ( ) {
1563
+ let test_fn: fn ( Vec < String > ) -> bool = |changes| -> bool {
1564
+ let len = changes. len ( ) / 3 ;
1565
+ let mut original = Rope :: new ( ) ;
1566
+ let mut iter = changes. into_iter ( ) ;
1567
+
1568
+ let changes_a: Vec < _ > = iter
1569
+ . by_ref ( )
1570
+ . take ( len)
1571
+ . map ( |c| {
1572
+ let c = Rope :: from ( c) ;
1573
+ let transaction = helix_core:: diff:: compare_ropes ( & original, & c) ;
1574
+ original = c;
1575
+ transaction
1576
+ } )
1577
+ . collect ( ) ;
1578
+ let mut original_concurrent = original. clone ( ) ;
1579
+
1580
+ let changes_b: Vec < _ > = iter
1581
+ . by_ref ( )
1582
+ . take ( len)
1583
+ . map ( |c| {
1584
+ let c = Rope :: from ( c) ;
1585
+ let transaction = helix_core:: diff:: compare_ropes ( & original, & c) ;
1586
+ original = c;
1587
+ transaction
1588
+ } )
1589
+ . collect ( ) ;
1590
+ let changes_c: Vec < _ > = iter
1591
+ . take ( len)
1592
+ . map ( |c| {
1593
+ let c = Rope :: from ( c) ;
1594
+ let transaction = helix_core:: diff:: compare_ropes ( & original_concurrent, & c) ;
1595
+ original_concurrent = c;
1596
+ transaction
1597
+ } )
1598
+ . collect ( ) ;
1599
+
1600
+ let file = tempfile:: NamedTempFile :: new ( ) . unwrap ( ) ;
1601
+ let mut config = Config :: default ( ) ;
1602
+ config. persistent_undo = true ;
1603
+
1604
+ let view_id = ViewId :: default ( ) ;
1605
+ let config = Arc :: new ( ArcSwap :: new ( Arc :: new ( config) ) ) ;
1606
+ let mut doc_1 = Document :: open ( file. path ( ) , None , None , config. clone ( ) ) . unwrap ( ) ;
1607
+ doc_1. ensure_view_init ( view_id) ;
1608
+
1609
+ // Make changes & save document A
1610
+ for c in changes_a {
1611
+ doc_1. apply ( & c, view_id) ;
1612
+ }
1613
+ helix_lsp:: block_on ( doc_1. save :: < PathBuf > ( None , true ) . unwrap ( ) ) . unwrap ( ) ;
1614
+
1615
+ let mut doc_2 = Document :: open ( file. path ( ) , None , None , config. clone ( ) ) . unwrap ( ) ;
1616
+ let mut doc_3 = Document :: open ( file. path ( ) , None , None , config. clone ( ) ) . unwrap ( ) ;
1617
+ doc_2. ensure_view_init ( view_id) ;
1618
+ doc_3. ensure_view_init ( view_id) ;
1619
+
1620
+ // Make changes in A and B at the same time.
1621
+ for c in changes_b {
1622
+ doc_1. apply ( & c, view_id) ;
1623
+ }
1624
+
1625
+ for c in changes_c {
1626
+ doc_2. apply ( & c, view_id) ;
1627
+ }
1628
+ helix_lsp:: block_on ( doc_2. save :: < PathBuf > ( None , true ) . unwrap ( ) ) . unwrap ( ) ;
1629
+
1630
+ doc_1. load_history ( ) . unwrap ( ) ;
1631
+ doc_3. load_history ( ) . unwrap ( ) ;
1632
+
1633
+ assert_eq ! ( doc_2. history. get_mut( ) , doc_3. history. get_mut( ) ) ;
1634
+
1635
+ helix_lsp:: block_on ( doc_1. save :: < PathBuf > ( None , true ) . unwrap ( ) ) . unwrap ( ) ;
1636
+ doc_2. load_history ( ) . unwrap ( ) ;
1637
+ doc_3. load_history ( ) . unwrap ( ) ;
1638
+ doc_1. history . get_mut ( ) == doc_2. history . get_mut ( )
1639
+ && doc_1. history . get_mut ( ) == doc_3. history . get_mut ( )
1640
+ } ;
1641
+ let handles: Vec < _ > = ( 0 ..100 )
1642
+ . map ( |_| {
1643
+ tokio:: task:: spawn_blocking ( move || {
1644
+ quickcheck:: QuickCheck :: new ( )
1645
+ . max_tests ( 1 )
1646
+ . quickcheck ( test_fn) ;
1647
+ } )
1648
+ } )
1649
+ . collect ( ) ;
1650
+ futures_util:: future:: try_join_all ( handles) . await . unwrap ( ) ;
1651
+ }
1652
+
1559
1653
macro_rules! decode {
1560
1654
( $name: ident, $label: expr, $label_override: expr) => {
1561
1655
#[ test]
0 commit comments