@@ -61,7 +61,13 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
6161 . zip ( changes. iter ( ) . skip ( 1 ) )
6262 . filter ( |( l, r) | {
6363 // We only care about checking for disjoint replace ranges
64- l. change_kind ( ) == ChangeKind :: Replace && r. change_kind ( ) == ChangeKind :: Replace
64+ matches ! (
65+ ( l. change_kind( ) , r. change_kind( ) ) ,
66+ (
67+ ChangeKind :: Replace | ChangeKind :: ReplaceRange ,
68+ ChangeKind :: Replace | ChangeKind :: ReplaceRange
69+ )
70+ )
6571 } )
6672 . all ( |( l, r) | {
6773 get_node_depth ( l. target_parent ( ) ) != get_node_depth ( r. target_parent ( ) )
@@ -97,6 +103,7 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
97103 // Pop off any ancestors that aren't applicable
98104 changed_ancestors. drain ( ( index + 1 ) ..) ;
99105
106+ // FIXME: Resolve changes that depend on a range of elements
100107 let ancestor = & changed_ancestors[ index] ;
101108
102109 dependent_changes. push ( DependentChange {
@@ -115,9 +122,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
115122 // Add to changed ancestors, if applicable
116123 match change {
117124 Change :: Insert ( _, _) | Change :: InsertAll ( _, _) => { }
118- Change :: Replace ( target, _) => {
125+ Change :: Replace ( target, _) | Change :: ReplaceWithMany ( target , _ ) => {
119126 changed_ancestors. push_back ( ChangedAncestor :: single ( target, change_index) )
120127 }
128+ Change :: ReplaceAll ( range, _) => {
129+ changed_ancestors. push_back ( ChangedAncestor :: multiple ( range, change_index) )
130+ }
121131 }
122132 }
123133
@@ -137,9 +147,15 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
137147 }
138148 } ;
139149 }
140- Change :: Replace ( target, _) => {
150+ Change :: Replace ( target, _) | Change :: ReplaceWithMany ( target , _ ) => {
141151 * target = tree_mutator. make_element_mut ( target) ;
142152 }
153+ Change :: ReplaceAll ( range, _) => {
154+ let start = tree_mutator. make_element_mut ( range. start ( ) ) ;
155+ let end = tree_mutator. make_element_mut ( range. end ( ) ) ;
156+
157+ * range = start..=end;
158+ }
143159 }
144160
145161 // Collect changed elements
@@ -148,6 +164,10 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
148164 Change :: InsertAll ( _, elements) => changed_elements. extend ( elements. iter ( ) . cloned ( ) ) ,
149165 Change :: Replace ( _, Some ( element) ) => changed_elements. push ( element. clone ( ) ) ,
150166 Change :: Replace ( _, None ) => { }
167+ Change :: ReplaceWithMany ( _, elements) => {
168+ changed_elements. extend ( elements. iter ( ) . cloned ( ) )
169+ }
170+ Change :: ReplaceAll ( _, elements) => changed_elements. extend ( elements. iter ( ) . cloned ( ) ) ,
151171 }
152172 }
153173
@@ -160,6 +180,9 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
160180 }
161181 // Silently drop outdated change
162182 Change :: Replace ( _, None ) => continue ,
183+ Change :: ReplaceAll ( _, _) | Change :: ReplaceWithMany ( _, _) => {
184+ unimplemented ! ( "cannot resolve changes that depend on replacing many elements" )
185+ }
163186 } ;
164187
165188 let upmap_target_node = |target : & SyntaxNode | {
@@ -185,9 +208,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
185208 * child = upmap_target ( child) ;
186209 }
187210 } ,
188- Change :: Replace ( target, _) => {
211+ Change :: Replace ( target, _) | Change :: ReplaceWithMany ( target , _ ) => {
189212 * target = upmap_target ( & target) ;
190213 }
214+ Change :: ReplaceAll ( range, _) => {
215+ * range = upmap_target ( range. start ( ) ) ..=upmap_target ( range. end ( ) ) ;
216+ }
191217 }
192218 }
193219
@@ -214,6 +240,16 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
214240 let parent = target. parent ( ) . unwrap ( ) ;
215241 parent. splice_children ( target. index ( ) ..target. index ( ) + 1 , vec ! [ new_target] ) ;
216242 }
243+ Change :: ReplaceWithMany ( target, elements) => {
244+ let parent = target. parent ( ) . unwrap ( ) ;
245+ parent. splice_children ( target. index ( ) ..target. index ( ) + 1 , elements) ;
246+ }
247+ Change :: ReplaceAll ( range, elements) => {
248+ let start = range. start ( ) . index ( ) ;
249+ let end = range. end ( ) . index ( ) ;
250+ let parent = range. start ( ) . parent ( ) . unwrap ( ) ;
251+ parent. splice_children ( start..end + 1 , elements) ;
252+ }
217253 }
218254 }
219255
@@ -252,7 +288,7 @@ struct ChangedAncestor {
252288
253289enum ChangedAncestorKind {
254290 Single { node : SyntaxNode } ,
255- Range { changed_nodes : RangeInclusive < SyntaxNode > , in_parent : SyntaxNode } ,
291+ Range { _changed_elements : RangeInclusive < SyntaxElement > , in_parent : SyntaxNode } ,
256292}
257293
258294impl ChangedAncestor {
@@ -267,13 +303,25 @@ impl ChangedAncestor {
267303 Self { kind, change_index }
268304 }
269305
306+ fn multiple ( range : & RangeInclusive < SyntaxElement > , change_index : usize ) -> Self {
307+ Self {
308+ kind : ChangedAncestorKind :: Range {
309+ _changed_elements : range. clone ( ) ,
310+ in_parent : range. start ( ) . parent ( ) . unwrap ( ) ,
311+ } ,
312+ change_index,
313+ }
314+ }
315+
270316 fn affected_range ( & self ) -> TextRange {
271317 match & self . kind {
272318 ChangedAncestorKind :: Single { node } => node. text_range ( ) ,
273- ChangedAncestorKind :: Range { changed_nodes, in_parent : _ } => TextRange :: new (
274- changed_nodes. start ( ) . text_range ( ) . start ( ) ,
275- changed_nodes. end ( ) . text_range ( ) . end ( ) ,
276- ) ,
319+ ChangedAncestorKind :: Range { _changed_elements : changed_nodes, in_parent : _ } => {
320+ TextRange :: new (
321+ changed_nodes. start ( ) . text_range ( ) . start ( ) ,
322+ changed_nodes. end ( ) . text_range ( ) . end ( ) ,
323+ )
324+ }
277325 }
278326 }
279327}
0 commit comments