Debian Codegraph @

Xtree-house/bindings/vendor/src/get_changed_ranges.c

raw
1 #include "./get_changed_ranges.h"
2 #include "./subtree.h"
3 #include "./language.h"
4 #include "./error_costs.h"
5 #include "./tree_cursor.h"
6 #include "./ts_assert.h"
7
8 // #define DEBUG_GET_CHANGED_RANGES
9
10 static void ts_range_array_add(
11 TSRangeArray *self,
12 Length start,
13 Length end
14 ) {
15 if (self->size > 0) {
16 TSRange *last_range = array_back(self);
17 if (start.bytes <= last_range->end_byte) {
18 last_range->end_byte = end.bytes;
19 last_range->end_point = end.extent;
20 return;
21 }
22 }
23
24 if (start.bytes < end.bytes) {
25 TSRange range = { start.extent, end.extent, start.bytes, end.bytes };
26 array_push(self, range);
27 }
28 }
29
30 bool ts_range_array_intersects(
31 const TSRangeArray *self,
32 unsigned start_index,
33 uint32_t start_byte,
34 uint32_t end_byte
35 ) {
36 for (unsigned i = start_index; i < self->size; i++) {
37 TSRange *range = array_get(self, i);
38 if (range->end_byte > start_byte) {
39 if (range->start_byte >= end_byte) break;
40 return true;
41 }
42 }
43 return false;
44 }
45
46 void ts_range_array_get_changed_ranges(
47 const TSRange *old_ranges, unsigned old_range_count,
48 const TSRange *new_ranges, unsigned new_range_count,
49 TSRangeArray *differences
50 ) {
51 unsigned new_index = 0;
52 unsigned old_index = 0;
53 Length current_position = length_zero();
54 bool in_old_range = false;
55 bool in_new_range = false;
56
57 while (old_index < old_range_count || new_index < new_range_count) {
58 const TSRange *old_range = &old_ranges[old_index];
59 const TSRange *new_range = &new_ranges[new_index];
60
61 Length next_old_position;
62 if (in_old_range) {
63 next_old_position = (Length) {old_range->end_byte, old_range->end_point};
64 } else if (old_index < old_range_count) {
65 next_old_position = (Length) {old_range->start_byte, old_range->start_point};
66 } else {
67 next_old_position = LENGTH_MAX;
68 }
69
70 Length next_new_position;
71 if (in_new_range) {
72 next_new_position = (Length) {new_range->end_byte, new_range->end_point};
73 } else if (new_index < new_range_count) {
74 next_new_position = (Length) {new_range->start_byte, new_range->start_point};
75 } else {
76 next_new_position = LENGTH_MAX;
77 }
78
79 if (next_old_position.bytes < next_new_position.bytes) {
80 if (in_old_range != in_new_range) {
81 ts_range_array_add(differences, current_position, next_old_position);
82 }
83 if (in_old_range) old_index++;
84 current_position = next_old_position;
85 in_old_range = !in_old_range;
86 } else if (next_new_position.bytes < next_old_position.bytes) {
87 if (in_old_range != in_new_range) {
88 ts_range_array_add(differences, current_position, next_new_position);
89 }
90 if (in_new_range) new_index++;
91 current_position = next_new_position;
92 in_new_range = !in_new_range;
93 } else {
94 if (in_old_range != in_new_range) {
95 ts_range_array_add(differences, current_position, next_new_position);
96 }
97 if (in_old_range) old_index++;
98 if (in_new_range) new_index++;
99 in_old_range = !in_old_range;
100 in_new_range = !in_new_range;
101 current_position = next_new_position;
102 }
103 }
104 }
105
106 void ts_range_edit(TSRange *range, const TSInputEdit *edit) {
107 if (range->end_byte >= edit->old_end_byte) {
108 if (range->end_byte != UINT32_MAX) {
109 range->end_byte = edit->new_end_byte + (range->end_byte - edit->old_end_byte);
110 range->end_point = point_add(
111 edit->new_end_point,
112 point_sub(range->end_point, edit->old_end_point)
113 );
114 if (range->end_byte < edit->new_end_byte) {
115 range->end_byte = UINT32_MAX;
116 range->end_point = POINT_MAX;
117 }
118 }
119 } else if (range->end_byte > edit->start_byte) {
120 range->end_byte = edit->start_byte;
121 range->end_point = edit->start_point;
122 }
123
124 if (range->start_byte >= edit->old_end_byte) {
125 range->start_byte = edit->new_end_byte + (range->start_byte - edit->old_end_byte);
126 range->start_point = point_add(
127 edit->new_end_point,
128 point_sub(range->start_point, edit->old_end_point)
129 );
130 if (range->start_byte < edit->new_end_byte) {
131 range->start_byte = UINT32_MAX;
132 range->start_point = POINT_MAX;
133 }
134 } else if (range->start_byte > edit->start_byte) {
135 range->start_byte = edit->start_byte;
136 range->start_point = edit->start_point;
137 }
138 }
139
140 typedef struct {
141 TreeCursor cursor;
142 const TSLanguage *language;
143 unsigned visible_depth;
144 bool in_padding;
145 Subtree prev_external_token;
146 } Iterator;
147
148 static Iterator iterator_new(
149 TreeCursor *cursor,
150 const Subtree *tree,
151 const TSLanguage *language
152 ) {
153 array_clear(&cursor->stack);
154 array_push(&cursor->stack, ((TreeCursorEntry) {
155 .subtree = tree,
156 .position = length_zero(),
157 .child_index = 0,
158 .structural_child_index = 0,
159 }));
160 return (Iterator) {
161 .cursor = *cursor,
162 .language = language,
163 .visible_depth = 1,
164 .in_padding = false,
165 .prev_external_token = NULL_SUBTREE,
166 };
167 }
168
169 static bool iterator_done(Iterator *self) {
170 return self->cursor.stack.size == 0;
171 }
172
173 static Length iterator_start_position(Iterator *self) {
174 TreeCursorEntry entry = *array_back(&self->cursor.stack);
175 if (self->in_padding) {
176 return entry.position;
177 } else {
178 return length_add(entry.position, ts_subtree_padding(*entry.subtree));
179 }
180 }
181
182 static Length iterator_end_position(Iterator *self) {
183 TreeCursorEntry entry = *array_back(&self->cursor.stack);
184 Length result = length_add(entry.position, ts_subtree_padding(*entry.subtree));
185 if (self->in_padding) {
186 return result;
187 } else {
188 return length_add(result, ts_subtree_size(*entry.subtree));
189 }
190 }
191
192 static bool iterator_tree_is_visible(const Iterator *self) {
193 TreeCursorEntry entry = *array_back(&self->cursor.stack);
194 if (ts_subtree_visible(*entry.subtree)) return true;
195 if (self->cursor.stack.size > 1) {
196 Subtree parent = *array_get(&self->cursor.stack, self->cursor.stack.size - 2)->subtree;
197 return ts_language_alias_at(
198 self->language,
199 parent.ptr->production_id,
200 entry.structural_child_index
201 ) != 0;
202 }
203 return false;
204 }
205
206 static void iterator_get_visible_state(
207 const Iterator *self,
208 Subtree *tree,
209 TSSymbol *alias_symbol,
210 uint32_t *start_byte
211 ) {
212 uint32_t i = self->cursor.stack.size - 1;
213
214 if (self->in_padding) {
215 if (i == 0) return;
216 i--;
217 }
218
219 for (; i + 1 > 0; i--) {
220 TreeCursorEntry entry = *array_get(&self->cursor.stack, i);
221
222 if (i > 0) {
223 const Subtree *parent = array_get(&self->cursor.stack, i - 1)->subtree;
224 *alias_symbol = ts_language_alias_at(
225 self->language,
226 parent->ptr->production_id,
227 entry.structural_child_index
228 );
229 }
230
231 if (ts_subtree_visible(*entry.subtree) || *alias_symbol) {
232 *tree = *entry.subtree;
233 *start_byte = entry.position.bytes;
234 break;
235 }
236 }
237 }
238
239 static void iterator_ascend(Iterator *self) {
240 if (iterator_done(self)) return;
241 if (iterator_tree_is_visible(self) && !self->in_padding) self->visible_depth--;
242 if (array_back(&self->cursor.stack)->child_index > 0) self->in_padding = false;
243 self->cursor.stack.size--;
244 }
245
246 static bool iterator_descend(Iterator *self, uint32_t goal_position) {
247 if (self->in_padding) return false;
248
249 bool did_descend = false;
250 do {
251 did_descend = false;
252 TreeCursorEntry entry = *array_back(&self->cursor.stack);
253 Length position = entry.position;
254 uint32_t structural_child_index = 0;
255 for (uint32_t i = 0, n = ts_subtree_child_count(*entry.subtree); i < n; i++) {
256 const Subtree *child = &ts_subtree_children(*entry.subtree)[i];
257 Length child_left = length_add(position, ts_subtree_padding(*child));
258 Length child_right = length_add(child_left, ts_subtree_size(*child));
259
260 if (child_right.bytes > goal_position) {
261 array_push(&self->cursor.stack, ((TreeCursorEntry) {
262 .subtree = child,
263 .position = position,
264 .child_index = i,
265 .structural_child_index = structural_child_index,
266 }));
267
268 if (iterator_tree_is_visible(self)) {
269 if (child_left.bytes > goal_position) {
270 self->in_padding = true;
271 } else {
272 self->visible_depth++;
273 }
274 return true;
275 }
276
277 did_descend = true;
278 break;
279 }
280
281 position = child_right;
282 if (!ts_subtree_extra(*child)) structural_child_index++;
283 Subtree last_external_token = ts_subtree_last_external_token(*child);
284 if (last_external_token.ptr) {
285 self->prev_external_token = last_external_token;
286 }
287 }
288 } while (did_descend);
289
290 return false;
291 }
292
293 static void iterator_advance(Iterator *self) {
294 if (self->in_padding) {
295 self->in_padding = false;
296 if (iterator_tree_is_visible(self)) {
297 self->visible_depth++;
298 } else {
299 iterator_descend(self, 0);
300 }
301 return;
302 }
303
304 for (;;) {
305 if (iterator_tree_is_visible(self)) self->visible_depth--;
306 TreeCursorEntry entry = array_pop(&self->cursor.stack);
307 if (iterator_done(self)) return;
308
309 const Subtree *parent = array_back(&self->cursor.stack)->subtree;
310 uint32_t child_index = entry.child_index + 1;
311 Subtree last_external_token = ts_subtree_last_external_token(*entry.subtree);
312 if (last_external_token.ptr) {
313 self->prev_external_token = last_external_token;
314 }
315 if (ts_subtree_child_count(*parent) > child_index) {
316 Length position = length_add(entry.position, ts_subtree_total_size(*entry.subtree));
317 uint32_t structural_child_index = entry.structural_child_index;
318 if (!ts_subtree_extra(*entry.subtree)) structural_child_index++;
319 const Subtree *next_child = &ts_subtree_children(*parent)[child_index];
320
321 array_push(&self->cursor.stack, ((TreeCursorEntry) {
322 .subtree = next_child,
323 .position = position,
324 .child_index = child_index,
325 .structural_child_index = structural_child_index,
326 }));
327
328 if (iterator_tree_is_visible(self)) {
329 if (ts_subtree_padding(*next_child).bytes > 0) {
330 self->in_padding = true;
331 } else {
332 self->visible_depth++;
333 }
334 } else {
335 iterator_descend(self, 0);
336 }
337 break;
338 }
339 }
340 }
341
342 typedef enum {
343 IteratorDiffers,
344 IteratorMayDiffer,
345 IteratorMatches,
346 } IteratorComparison;
347
348 static IteratorComparison iterator_compare(
349 const Iterator *old_iter,
350 const Iterator *new_iter
351 ) {
352 Subtree old_tree = NULL_SUBTREE;
353 Subtree new_tree = NULL_SUBTREE;
354 uint32_t old_start = 0;
355 uint32_t new_start = 0;
356 TSSymbol old_alias_symbol = 0;
357 TSSymbol new_alias_symbol = 0;
358 iterator_get_visible_state(old_iter, &old_tree, &old_alias_symbol, &old_start);
359 iterator_get_visible_state(new_iter, &new_tree, &new_alias_symbol, &new_start);
360 TSSymbol old_symbol = ts_subtree_symbol(old_tree);
361 TSSymbol new_symbol = ts_subtree_symbol(new_tree);
362
363 if (!old_tree.ptr && !new_tree.ptr) return IteratorMatches;
364 if (!old_tree.ptr || !new_tree.ptr) return IteratorDiffers;
365 if (old_alias_symbol != new_alias_symbol || old_symbol != new_symbol) return IteratorDiffers;
366
367 uint32_t old_size = ts_subtree_size(old_tree).bytes;
368 uint32_t new_size = ts_subtree_size(new_tree).bytes;
369 TSStateId old_state = ts_subtree_parse_state(old_tree);
370 TSStateId new_state = ts_subtree_parse_state(new_tree);
371 bool old_has_external_tokens = ts_subtree_has_external_tokens(old_tree);
372 bool new_has_external_tokens = ts_subtree_has_external_tokens(new_tree);
373 uint32_t old_error_cost = ts_subtree_error_cost(old_tree);
374 uint32_t new_error_cost = ts_subtree_error_cost(new_tree);
375
376 if (
377 old_start != new_start ||
378 old_symbol == ts_builtin_sym_error ||
379 old_size != new_size ||
380 old_state == TS_TREE_STATE_NONE ||
381 new_state == TS_TREE_STATE_NONE ||
382 ((old_state == ERROR_STATE) != (new_state == ERROR_STATE)) ||
383 old_error_cost != new_error_cost ||
384 old_has_external_tokens != new_has_external_tokens ||
385 ts_subtree_has_changes(old_tree) ||
386 (
387 old_has_external_tokens &&
388 !ts_subtree_external_scanner_state_eq(old_iter->prev_external_token, new_iter->prev_external_token)
389 )
390 ) {
391 return IteratorMayDiffer;
392 }
393
394 return IteratorMatches;
395 }
396
397 #ifdef DEBUG_GET_CHANGED_RANGES
398 static inline void iterator_print_state(Iterator *self) {
399 TreeCursorEntry entry = *array_back(&self->cursor.stack);
400 TSPoint start = iterator_start_position(self).extent;
401 TSPoint end = iterator_end_position(self).extent;
402 const char *name = ts_language_symbol_name(self->language, ts_subtree_symbol(*entry.subtree));
403 printf(
404 "(%-25s %s\t depth:%u [%u, %u] - [%u, %u])",
405 name, self->in_padding ? "(p)" : " ",
406 self->visible_depth,
407 start.row, start.column,
408 end.row, end.column
409 );
410 }
411 #endif
412
413 unsigned ts_subtree_get_changed_ranges(
414 const Subtree *old_tree, const Subtree *new_tree,
415 TreeCursor *cursor1, TreeCursor *cursor2,
416 const TSLanguage *language,
417 const TSRangeArray *included_range_differences,
418 TSRange **ranges
419 ) {
420 TSRangeArray results = array_new();
421
422 Iterator old_iter = iterator_new(cursor1, old_tree, language);
423 Iterator new_iter = iterator_new(cursor2, new_tree, language);
424
425 unsigned included_range_difference_index = 0;
426
427 Length position = iterator_start_position(&old_iter);
428 Length next_position = iterator_start_position(&new_iter);
429 if (position.bytes < next_position.bytes) {
430 ts_range_array_add(&results, position, next_position);
431 position = next_position;
432 } else if (position.bytes > next_position.bytes) {
433 ts_range_array_add(&results, next_position, position);
434 next_position = position;
435 }
436
437 do {
438 #ifdef DEBUG_GET_CHANGED_RANGES
439 printf("At [%-2u, %-2u] Compare ", position.extent.row, position.extent.column);
440 iterator_print_state(&old_iter);
441 printf("\tvs\t");
442 iterator_print_state(&new_iter);
443 puts("");
444 #endif
445
446 // Compare the old and new subtrees.
447 IteratorComparison comparison = iterator_compare(&old_iter, &new_iter);
448
449 // Even if the two subtrees appear to be identical, they could differ
450 // internally if they contain a range of text that was previously
451 // excluded from the parse, and is now included, or vice-versa.
452 if (comparison == IteratorMatches && ts_range_array_intersects(
453 included_range_differences,
454 included_range_difference_index,
455 position.bytes,
456 iterator_end_position(&old_iter).bytes
457 )) {
458 comparison = IteratorMayDiffer;
459 }
460
461 bool is_changed = false;
462 switch (comparison) {
463 // If the subtrees are definitely identical, move to the end
464 // of both subtrees.
465 case IteratorMatches:
466 next_position = iterator_end_position(&old_iter);
467 break;
468
469 // If the subtrees might differ internally, descend into both
470 // subtrees, finding the first child that spans the current position.
471 case IteratorMayDiffer:
472 if (iterator_descend(&old_iter, position.bytes)) {
473 if (!iterator_descend(&new_iter, position.bytes)) {
474 is_changed = true;
475 next_position = iterator_end_position(&old_iter);
476 }
477 } else if (iterator_descend(&new_iter, position.bytes)) {
478 is_changed = true;
479 next_position = iterator_end_position(&new_iter);
480 } else {
481 next_position = length_min(
482 iterator_end_position(&old_iter),
483 iterator_end_position(&new_iter)
484 );
485 }
486 break;
487
488 // If the subtrees are different, record a change and then move
489 // to the end of both subtrees.
490 case IteratorDiffers:
491 is_changed = true;
492 next_position = length_min(
493 iterator_end_position(&old_iter),
494 iterator_end_position(&new_iter)
495 );
496 break;
497 }
498
499 // Ensure that both iterators are caught up to the current position.
500 while (
501 !iterator_done(&old_iter) &&
502 iterator_end_position(&old_iter).bytes <= next_position.bytes
503 ) iterator_advance(&old_iter);
504 while (
505 !iterator_done(&new_iter) &&
506 iterator_end_position(&new_iter).bytes <= next_position.bytes
507 ) iterator_advance(&new_iter);
508
509 // Ensure that both iterators are at the same depth in the tree.
510 while (old_iter.visible_depth > new_iter.visible_depth) {
511 iterator_ascend(&old_iter);
512 }
513 while (new_iter.visible_depth > old_iter.visible_depth) {
514 iterator_ascend(&new_iter);
515 }
516
517 if (is_changed) {
518 #ifdef DEBUG_GET_CHANGED_RANGES
519 printf(
520 " change: [[%u, %u] - [%u, %u]]\n",
521 position.extent.row + 1, position.extent.column,
522 next_position.extent.row + 1, next_position.extent.column
523 );
524 #endif
525
526 ts_range_array_add(&results, position, next_position);
527 }
528
529 position = next_position;
530
531 // Keep track of the current position in the included range differences
532 // array in order to avoid scanning the entire array on each iteration.
533 while (included_range_difference_index < included_range_differences->size) {
534 const TSRange *range = array_get(included_range_differences,
535 included_range_difference_index
536 );
537 if (range->end_byte <= position.bytes) {
538 included_range_difference_index++;
539 } else {
540 break;
541 }
542 }
543 } while (!iterator_done(&old_iter) && !iterator_done(&new_iter));
544
545 Length old_size = ts_subtree_total_size(*old_tree);
546 Length new_size = ts_subtree_total_size(*new_tree);
547 if (old_size.bytes < new_size.bytes) {
548 ts_range_array_add(&results, old_size, new_size);
549 } else if (new_size.bytes < old_size.bytes) {
550 ts_range_array_add(&results, new_size, old_size);
551 }
552
553 *cursor1 = old_iter.cursor;
554 *cursor2 = new_iter.cursor;
555 *ranges = results.contents;
556 return results.size;
557 }