@@ -13,8 +13,9 @@ use slotmap::{DefaultKey as LayerId, HopSlotMap};
13
13
use std:: {
14
14
borrow:: Cow ,
15
15
cell:: RefCell ,
16
- collections:: { HashMap , HashSet , VecDeque } ,
16
+ collections:: { HashMap , VecDeque } ,
17
17
fmt,
18
+ mem:: replace,
18
19
path:: Path ,
19
20
str:: FromStr ,
20
21
sync:: Arc ,
@@ -594,6 +595,7 @@ impl Syntax {
594
595
tree : None ,
595
596
config,
596
597
depth : 0 ,
598
+ flags : 0 ,
597
599
ranges : vec ! [ Range {
598
600
start_byte: 0 ,
599
601
end_byte: usize :: MAX ,
@@ -656,9 +658,10 @@ impl Syntax {
656
658
}
657
659
}
658
660
659
- for layer in & mut self . layers . values_mut ( ) {
661
+ for layer in self . layers . values_mut ( ) {
660
662
// The root layer always covers the whole range (0..usize::MAX)
661
663
if layer. depth == 0 {
664
+ layer. flags = LAYER_MODIFIED ;
662
665
continue ;
663
666
}
664
667
@@ -689,6 +692,8 @@ impl Syntax {
689
692
edit. new_end_position ,
690
693
point_sub ( range. end_point , edit. old_end_position ) ,
691
694
) ;
695
+
696
+ layer. flags |= LAYER_MOVED ;
692
697
}
693
698
// if the edit starts in the space before and extends into the range
694
699
else if edit. start_byte < range. start_byte {
@@ -703,11 +708,13 @@ impl Syntax {
703
708
edit. new_end_position ,
704
709
point_sub ( range. end_point , edit. old_end_position ) ,
705
710
) ;
711
+ layer. flags = LAYER_MODIFIED ;
706
712
}
707
713
// If the edit is an insertion at the start of the tree, shift
708
714
else if edit. start_byte == range. start_byte && is_pure_insertion {
709
715
range. start_byte = edit. new_end_byte ;
710
716
range. start_point = edit. new_end_position ;
717
+ layer. flags |= LAYER_MOVED ;
711
718
} else {
712
719
range. end_byte = range
713
720
. end_byte
@@ -717,6 +724,7 @@ impl Syntax {
717
724
edit. new_end_position ,
718
725
point_sub ( range. end_point , edit. old_end_position ) ,
719
726
) ;
727
+ layer. flags = LAYER_MODIFIED ;
720
728
}
721
729
}
722
730
}
@@ -731,27 +739,30 @@ impl Syntax {
731
739
732
740
let source_slice = source. slice ( ..) ;
733
741
734
- let mut touched = HashSet :: new ( ) ;
735
-
736
- // TODO: we should be able to avoid editing & parsing layers with ranges earlier in the document before the edit
737
-
738
742
while let Some ( layer_id) = queue. pop_front ( ) {
739
- // Mark the layer as touched
740
- touched. insert ( layer_id) ;
741
-
742
743
let layer = & mut self . layers [ layer_id] ;
743
744
745
+ // Mark the layer as touched
746
+ layer. flags |= LAYER_TOUCHED ;
747
+
744
748
// If a tree already exists, notify it of changes.
745
749
if let Some ( tree) = & mut layer. tree {
746
- for edit in edits. iter ( ) . rev ( ) {
747
- // Apply the edits in reverse.
748
- // If we applied them in order then edit 1 would disrupt the positioning of edit 2.
749
- tree. edit ( edit) ;
750
+ if layer. flags & ( LAYER_MODIFIED | LAYER_MOVED ) != 0 {
751
+ for edit in edits. iter ( ) . rev ( ) {
752
+ // Apply the edits in reverse.
753
+ // If we applied them in order then edit 1 would disrupt the positioning of edit 2.
754
+ tree. edit ( edit) ;
755
+ }
750
756
}
751
- }
752
757
753
- // Re-parse the tree.
754
- layer. parse ( & mut ts_parser. parser , source) ?;
758
+ if layer. flags & LAYER_MODIFIED != 0 {
759
+ // Re-parse the tree.
760
+ layer. parse ( & mut ts_parser. parser , source) ?;
761
+ }
762
+ } else {
763
+ // always parse if this layer has never been parsed before
764
+ layer. parse ( & mut ts_parser. parser , source) ?;
765
+ }
755
766
756
767
// Switch to an immutable borrow.
757
768
let layer = & self . layers [ layer_id] ;
@@ -855,6 +866,8 @@ impl Syntax {
855
866
config,
856
867
depth,
857
868
ranges,
869
+ // set the modified flag to ensure the layer is parsed
870
+ flags : 0 ,
858
871
} )
859
872
} ) ;
860
873
@@ -869,7 +882,8 @@ impl Syntax {
869
882
ts_parser. cursors . push ( cursor) ;
870
883
871
884
// Remove all untouched layers
872
- self . layers . retain ( |id, _| touched. contains ( & id) ) ;
885
+ self . layers
886
+ . retain ( |_, layer| replace ( & mut layer. flags , 0 ) & LAYER_TOUCHED != 0 ) ;
873
887
874
888
Ok ( ( ) )
875
889
} )
@@ -968,14 +982,19 @@ impl Syntax {
968
982
// TODO: Folding
969
983
}
970
984
985
+ const LAYER_MODIFIED : u32 = 0b001 ;
986
+ const LAYER_MOVED : u32 = 0b010 ;
987
+ const LAYER_TOUCHED : u32 = 0b100 ;
988
+
971
989
#[ derive( Debug ) ]
972
990
pub struct LanguageLayer {
973
991
// mode
974
992
// grammar
975
993
pub config : Arc < HighlightConfiguration > ,
976
994
pub ( crate ) tree : Option < Tree > ,
977
995
pub ranges : Vec < Range > ,
978
- pub depth : usize ,
996
+ pub depth : u32 ,
997
+ pub flags : u32 ,
979
998
}
980
999
981
1000
impl LanguageLayer {
@@ -1191,7 +1210,7 @@ struct HighlightIter<'a> {
1191
1210
layers : Vec < HighlightIterLayer < ' a > > ,
1192
1211
iter_count : usize ,
1193
1212
next_event : Option < HighlightEvent > ,
1194
- last_highlight_range : Option < ( usize , usize , usize ) > ,
1213
+ last_highlight_range : Option < ( usize , usize , u32 ) > ,
1195
1214
}
1196
1215
1197
1216
// Adapter to convert rope chunks to bytes
@@ -1224,7 +1243,7 @@ struct HighlightIterLayer<'a> {
1224
1243
config : & ' a HighlightConfiguration ,
1225
1244
highlight_end_stack : Vec < usize > ,
1226
1245
scope_stack : Vec < LocalScope < ' a > > ,
1227
- depth : usize ,
1246
+ depth : u32 ,
1228
1247
ranges : & ' a [ Range ] ,
1229
1248
}
1230
1249
0 commit comments