@@ -61,7 +61,13 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
61
61
. zip ( changes. iter ( ) . skip ( 1 ) )
62
62
. filter ( |( l, r) | {
63
63
// 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
+ )
65
71
} )
66
72
. all ( |( l, r) | {
67
73
get_node_depth ( l. target_parent ( ) ) != get_node_depth ( r. target_parent ( ) )
@@ -97,6 +103,7 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
97
103
// Pop off any ancestors that aren't applicable
98
104
changed_ancestors. drain ( ( index + 1 ) ..) ;
99
105
106
+ // FIXME: Resolve changes that depend on a range of elements
100
107
let ancestor = & changed_ancestors[ index] ;
101
108
102
109
dependent_changes. push ( DependentChange {
@@ -115,9 +122,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
115
122
// Add to changed ancestors, if applicable
116
123
match change {
117
124
Change :: Insert ( _, _) | Change :: InsertAll ( _, _) => { }
118
- Change :: Replace ( target, _) => {
125
+ Change :: Replace ( target, _) | Change :: ReplaceWithMany ( target , _ ) => {
119
126
changed_ancestors. push_back ( ChangedAncestor :: single ( target, change_index) )
120
127
}
128
+ Change :: ReplaceAll ( range, _) => {
129
+ changed_ancestors. push_back ( ChangedAncestor :: multiple ( range, change_index) )
130
+ }
121
131
}
122
132
}
123
133
@@ -137,9 +147,15 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
137
147
}
138
148
} ;
139
149
}
140
- Change :: Replace ( target, _) => {
150
+ Change :: Replace ( target, _) | Change :: ReplaceWithMany ( target , _ ) => {
141
151
* target = tree_mutator. make_element_mut ( target) ;
142
152
}
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
+ }
143
159
}
144
160
145
161
// Collect changed elements
@@ -148,6 +164,10 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
148
164
Change :: InsertAll ( _, elements) => changed_elements. extend ( elements. iter ( ) . cloned ( ) ) ,
149
165
Change :: Replace ( _, Some ( element) ) => changed_elements. push ( element. clone ( ) ) ,
150
166
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 ( ) ) ,
151
171
}
152
172
}
153
173
@@ -160,6 +180,9 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
160
180
}
161
181
// Silently drop outdated change
162
182
Change :: Replace ( _, None ) => continue ,
183
+ Change :: ReplaceAll ( _, _) | Change :: ReplaceWithMany ( _, _) => {
184
+ unimplemented ! ( "cannot resolve changes that depend on replacing many elements" )
185
+ }
163
186
} ;
164
187
165
188
let upmap_target_node = |target : & SyntaxNode | {
@@ -185,9 +208,12 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
185
208
* child = upmap_target ( child) ;
186
209
}
187
210
} ,
188
- Change :: Replace ( target, _) => {
211
+ Change :: Replace ( target, _) | Change :: ReplaceWithMany ( target , _ ) => {
189
212
* target = upmap_target ( & target) ;
190
213
}
214
+ Change :: ReplaceAll ( range, _) => {
215
+ * range = upmap_target ( range. start ( ) ) ..=upmap_target ( range. end ( ) ) ;
216
+ }
191
217
}
192
218
}
193
219
@@ -214,6 +240,16 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
214
240
let parent = target. parent ( ) . unwrap ( ) ;
215
241
parent. splice_children ( target. index ( ) ..target. index ( ) + 1 , vec ! [ new_target] ) ;
216
242
}
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
+ }
217
253
}
218
254
}
219
255
@@ -252,7 +288,7 @@ struct ChangedAncestor {
252
288
253
289
enum ChangedAncestorKind {
254
290
Single { node : SyntaxNode } ,
255
- Range { changed_nodes : RangeInclusive < SyntaxNode > , in_parent : SyntaxNode } ,
291
+ Range { _changed_elements : RangeInclusive < SyntaxElement > , in_parent : SyntaxNode } ,
256
292
}
257
293
258
294
impl ChangedAncestor {
@@ -267,13 +303,25 @@ impl ChangedAncestor {
267
303
Self { kind, change_index }
268
304
}
269
305
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
+
270
316
fn affected_range ( & self ) -> TextRange {
271
317
match & self . kind {
272
318
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
+ }
277
325
}
278
326
}
279
327
}
0 commit comments