2017-08-04 11:03:14 -07:00
|
|
|
#include "runtime/get_changed_ranges.h"
|
2016-10-16 20:42:55 -07:00
|
|
|
#include "runtime/tree.h"
|
2017-07-17 17:07:56 -07:00
|
|
|
#include "runtime/language.h"
|
2016-10-16 21:10:25 -07:00
|
|
|
#include "runtime/error_costs.h"
|
2016-10-16 20:42:55 -07:00
|
|
|
|
|
|
|
|
typedef Array(TSRange) RangeArray;
|
|
|
|
|
|
2016-11-04 09:18:38 -07:00
|
|
|
static void range_array_add(RangeArray *results, TSPoint start, TSPoint end) {
|
2016-10-16 20:42:55 -07:00
|
|
|
if (results->size > 0) {
|
|
|
|
|
TSRange *last_range = array_back(results);
|
2016-11-09 20:59:05 -08:00
|
|
|
if (point_lte(start, last_range->end)) {
|
2016-10-16 20:42:55 -07:00
|
|
|
last_range->end = end;
|
2016-11-04 09:18:38 -07:00
|
|
|
return;
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
if (point_lt(start, end)) {
|
2016-10-16 20:42:55 -07:00
|
|
|
TSRange range = { start, end };
|
2016-11-04 09:18:38 -07:00
|
|
|
array_push(results, range);
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:50:50 -07:00
|
|
|
typedef struct {
|
|
|
|
|
TreePath path;
|
|
|
|
|
const TSLanguage *language;
|
|
|
|
|
unsigned visible_depth;
|
|
|
|
|
} Iterator;
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
static bool iterator_tree_is_visible(const Iterator *self) {
|
|
|
|
|
TreePathEntry entry = *array_back(&self->path);
|
|
|
|
|
if (entry.tree->visible) return true;
|
|
|
|
|
if (self->path.size > 1) {
|
|
|
|
|
Tree *parent = self->path.contents[self->path.size - 2].tree;
|
|
|
|
|
const TSSymbol *alias_sequence = ts_language_alias_sequence(self->language, parent->alias_sequence_id);
|
|
|
|
|
return alias_sequence && alias_sequence[entry.structural_child_index] != 0;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-08-04 11:03:14 -07:00
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
static bool iterator_done(Iterator *self) {
|
|
|
|
|
return self->path.size == 0;
|
|
|
|
|
}
|
2017-08-04 11:03:14 -07:00
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
static void iterator_ascend(Iterator *self) {
|
|
|
|
|
bool did_ascend = false;
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (iterator_done(self)) break;
|
|
|
|
|
if (iterator_tree_is_visible(self)) {
|
|
|
|
|
if (did_ascend) break;
|
|
|
|
|
did_ascend = true;
|
|
|
|
|
self->visible_depth--;
|
2017-08-04 11:03:14 -07:00
|
|
|
}
|
2017-08-04 14:50:50 -07:00
|
|
|
self->path.size--;
|
2017-08-04 11:03:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
static bool iterator_descend(Iterator *self, Length position) {
|
|
|
|
|
uint32_t original_size = self->path.size;
|
2017-01-24 12:48:47 -08:00
|
|
|
|
2016-10-16 20:42:55 -07:00
|
|
|
bool did_descend;
|
|
|
|
|
do {
|
|
|
|
|
did_descend = false;
|
2017-08-04 14:03:41 -07:00
|
|
|
TreePathEntry entry = *array_back(&self->path);
|
2017-01-24 12:48:47 -08:00
|
|
|
Length child_left = entry.position;
|
2017-08-04 11:03:14 -07:00
|
|
|
uint32_t structural_child_index = 0;
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t i = 0; i < entry.tree->child_count; i++) {
|
2016-11-09 20:59:05 -08:00
|
|
|
Tree *child = entry.tree->children[i];
|
2017-01-24 12:48:47 -08:00
|
|
|
Length child_right = length_add(child_left, ts_tree_total_size(child));
|
2017-08-04 14:03:41 -07:00
|
|
|
|
|
|
|
|
if (child_right.bytes > position.bytes) {
|
|
|
|
|
array_push(&self->path, ((TreePathEntry){
|
2017-08-04 11:03:14 -07:00
|
|
|
.tree = child,
|
|
|
|
|
.position = child_left,
|
|
|
|
|
.child_index = i,
|
|
|
|
|
.structural_child_index = structural_child_index,
|
2017-08-04 14:03:41 -07:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
if (iterator_tree_is_visible(self)) {
|
|
|
|
|
self->visible_depth++;
|
2016-10-16 20:42:55 -07:00
|
|
|
return true;
|
2017-08-04 11:03:14 -07:00
|
|
|
}
|
2017-08-04 14:03:41 -07:00
|
|
|
|
2017-08-04 11:03:14 -07:00
|
|
|
if (child->visible_child_count > 0) {
|
2016-10-16 20:42:55 -07:00
|
|
|
did_descend = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-08-04 14:03:41 -07:00
|
|
|
|
2017-08-04 14:50:50 -07:00
|
|
|
self->path.size--;
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
2017-08-04 14:03:41 -07:00
|
|
|
|
2017-01-24 12:48:47 -08:00
|
|
|
child_left = child_right;
|
2017-08-04 11:03:14 -07:00
|
|
|
if (!child->extra) structural_child_index++;
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
|
|
|
|
} while (did_descend);
|
2017-01-24 12:48:47 -08:00
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
self->path.size = original_size;
|
2016-10-16 20:42:55 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:50:50 -07:00
|
|
|
static bool iterator_advance(Iterator *self) {
|
|
|
|
|
if (iterator_done(self)) return false;
|
2017-01-24 12:48:47 -08:00
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
for (;;) {
|
|
|
|
|
if (iterator_tree_is_visible(self)) self->visible_depth--;
|
|
|
|
|
TreePathEntry entry = array_pop(&self->path);
|
2017-08-04 14:50:50 -07:00
|
|
|
if (iterator_done(self)) return false;
|
2017-01-24 12:48:47 -08:00
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
Tree *parent = array_back(&self->path)->tree;
|
2017-01-24 12:48:47 -08:00
|
|
|
Length position = length_add(entry.position, ts_tree_total_size(entry.tree));
|
2017-08-04 11:03:14 -07:00
|
|
|
uint32_t structural_child_index = entry.structural_child_index;
|
|
|
|
|
if (!entry.tree->extra) structural_child_index++;
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
for (uint32_t i = entry.child_index + 1; i < parent->child_count; i++) {
|
|
|
|
|
Tree *next_child = parent->children[i];
|
|
|
|
|
array_push(&self->path, ((TreePathEntry){
|
|
|
|
|
.tree = next_child,
|
|
|
|
|
.position = position,
|
|
|
|
|
.child_index = i,
|
|
|
|
|
.structural_child_index = structural_child_index,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
if (iterator_tree_is_visible(self)) {
|
|
|
|
|
self->visible_depth++;
|
2017-08-04 14:50:50 -07:00
|
|
|
return true;
|
2017-08-04 14:03:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (next_child->visible_child_count > 0) {
|
|
|
|
|
iterator_descend(self, length_zero());
|
2017-08-04 14:50:50 -07:00
|
|
|
return true;
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
2017-08-04 14:03:41 -07:00
|
|
|
|
|
|
|
|
if (next_child->child_count == 0) {
|
2017-08-04 14:50:50 -07:00
|
|
|
return true;
|
2017-08-04 14:03:41 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:50:50 -07:00
|
|
|
self->path.size--;
|
2016-11-09 20:59:05 -08:00
|
|
|
position = length_add(position, ts_tree_total_size(next_child));
|
2017-08-04 11:03:14 -07:00
|
|
|
if (!next_child->extra) structural_child_index++;
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
static Iterator iterator_new(TreePath *path, Tree *tree, const TSLanguage *language) {
|
2016-10-16 20:42:55 -07:00
|
|
|
array_clear(path);
|
2016-11-09 20:19:02 -08:00
|
|
|
array_push(path, ((TreePathEntry){
|
|
|
|
|
.tree = tree,
|
|
|
|
|
.position = { 0, 0, { 0, 0 } },
|
|
|
|
|
.child_index = 0,
|
2017-08-04 11:03:14 -07:00
|
|
|
.structural_child_index = 0,
|
2016-11-09 20:19:02 -08:00
|
|
|
}));
|
2017-08-04 15:21:53 -07:00
|
|
|
return (Iterator) {
|
2017-08-04 14:03:41 -07:00
|
|
|
.path = *path,
|
|
|
|
|
.language = language,
|
2017-08-04 15:21:53 -07:00
|
|
|
.visible_depth = 1,
|
2017-08-04 14:03:41 -07:00
|
|
|
};
|
2017-01-24 12:48:47 -08:00
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
Length iterator_start_position(Iterator *self) {
|
|
|
|
|
TreePathEntry entry = *array_back(&self->path);
|
2017-01-24 12:48:47 -08:00
|
|
|
return length_add(entry.position, entry.tree->padding);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
Length iterator_end_position(Iterator *self) {
|
|
|
|
|
TreePathEntry entry = *array_back(&self->path);
|
2017-01-24 12:48:47 -08:00
|
|
|
return length_add(length_add(entry.position, entry.tree->padding), entry.tree->size);
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-17 17:07:56 -07:00
|
|
|
typedef enum {
|
2017-08-04 14:03:41 -07:00
|
|
|
IteratorDiffers,
|
|
|
|
|
IteratorMayDiffer,
|
|
|
|
|
IteratorMatches,
|
|
|
|
|
} IteratorComparison;
|
|
|
|
|
|
|
|
|
|
IteratorComparison iterator_compare(const Iterator *self, const Iterator *other) {
|
2017-07-17 17:07:56 -07:00
|
|
|
Tree *old_tree = NULL;
|
2017-07-31 11:45:24 -07:00
|
|
|
TSSymbol old_alias_symbol = 0;
|
2017-07-17 17:07:56 -07:00
|
|
|
Length old_start = length_zero();
|
2017-08-04 14:03:41 -07:00
|
|
|
for (uint32_t i = self->path.size - 1; i + 1 > 0; i--) {
|
|
|
|
|
old_tree = self->path.contents[i].tree;
|
|
|
|
|
old_start = self->path.contents[i].position;
|
2017-08-01 13:28:18 -07:00
|
|
|
if (i > 0) {
|
2017-08-04 14:03:41 -07:00
|
|
|
Tree *parent = self->path.contents[i - 1].tree;
|
|
|
|
|
const TSSymbol *alias_sequence = ts_language_alias_sequence(self->language, parent->alias_sequence_id);
|
2017-08-01 13:28:18 -07:00
|
|
|
if (alias_sequence) {
|
2017-08-04 14:03:41 -07:00
|
|
|
old_alias_symbol = alias_sequence[self->path.contents[i].structural_child_index];
|
2017-07-17 17:07:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
2017-08-01 13:28:18 -07:00
|
|
|
|
|
|
|
|
if (old_tree->visible || old_alias_symbol) break;
|
2017-07-17 17:07:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tree *new_tree = NULL;
|
2017-07-31 11:45:24 -07:00
|
|
|
TSSymbol new_alias_symbol = 0;
|
2017-07-17 17:07:56 -07:00
|
|
|
Length new_start = length_zero();
|
2017-08-04 14:03:41 -07:00
|
|
|
for (uint32_t i = other->path.size - 1; i + 1 > 0; i--) {
|
|
|
|
|
new_tree = other->path.contents[i].tree;
|
|
|
|
|
new_start = other->path.contents[i].position;
|
2017-08-01 13:28:18 -07:00
|
|
|
if (i > 0) {
|
2017-08-04 14:03:41 -07:00
|
|
|
Tree *parent = other->path.contents[i - 1].tree;
|
|
|
|
|
const TSSymbol *alias_sequence = ts_language_alias_sequence(self->language, parent->alias_sequence_id);
|
2017-08-01 13:28:18 -07:00
|
|
|
if (alias_sequence) {
|
2017-08-04 14:03:41 -07:00
|
|
|
new_alias_symbol = alias_sequence[other->path.contents[i].structural_child_index];
|
2017-07-17 17:07:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
2017-08-01 13:28:18 -07:00
|
|
|
|
|
|
|
|
if (new_tree->visible || new_alias_symbol) break;
|
2017-07-17 17:07:56 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-31 11:45:24 -07:00
|
|
|
if (old_alias_symbol == new_alias_symbol) {
|
2017-07-17 17:07:56 -07:00
|
|
|
if (old_start.bytes == new_start.bytes) {
|
|
|
|
|
if (!old_tree->has_changes &&
|
|
|
|
|
old_tree->symbol == new_tree->symbol &&
|
|
|
|
|
old_tree->symbol != ts_builtin_sym_error &&
|
|
|
|
|
old_tree->size.bytes == new_tree->size.bytes &&
|
|
|
|
|
old_tree->parse_state != TS_TREE_STATE_NONE &&
|
|
|
|
|
new_tree->parse_state != TS_TREE_STATE_NONE &&
|
|
|
|
|
(old_tree->parse_state == ERROR_STATE) == (new_tree->parse_state == ERROR_STATE)) {
|
2017-08-04 14:03:41 -07:00
|
|
|
return IteratorMatches;
|
2017-07-17 17:07:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (old_tree->symbol == new_tree->symbol) {
|
2017-08-04 14:03:41 -07:00
|
|
|
return IteratorMayDiffer;
|
2017-07-17 17:07:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
return IteratorDiffers;
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:50:50 -07:00
|
|
|
static void iterator_print_state(Iterator *self) {
|
|
|
|
|
TreePathEntry entry = *array_back(&self->path);
|
|
|
|
|
TSPoint start = point_add(entry.position.extent, entry.tree->padding.extent);
|
|
|
|
|
TSPoint end = point_add(start, entry.tree->size.extent);
|
|
|
|
|
const char *name = ts_language_symbol_name(self->language, entry.tree->symbol);
|
|
|
|
|
printf("(%-25s\t depth:%u [%u, %u] - [%u, %u])", name, self->visible_depth, start.row, start.column, end.row, end.column);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
unsigned ts_tree_get_changed_ranges(Tree *old_tree, Tree *new_tree,
|
|
|
|
|
TreePath *path1, TreePath *path2,
|
|
|
|
|
const TSLanguage *language, TSRange **ranges) {
|
|
|
|
|
Iterator old_iter = iterator_new(path1, old_tree, language);
|
|
|
|
|
Iterator new_iter = iterator_new(path2, new_tree, language);
|
2016-10-16 20:42:55 -07:00
|
|
|
RangeArray results = array_new();
|
2017-08-04 14:50:50 -07:00
|
|
|
Length position = length_zero();
|
2016-10-16 20:42:55 -07:00
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
while (!iterator_done(&old_iter) && !iterator_done(&new_iter)) {
|
2016-10-16 20:42:55 -07:00
|
|
|
bool is_changed = false;
|
2017-01-24 12:48:47 -08:00
|
|
|
Length next_position = position;
|
2017-08-04 14:03:41 -07:00
|
|
|
Length old_start = iterator_start_position(&old_iter);
|
|
|
|
|
Length new_start = iterator_start_position(&new_iter);
|
|
|
|
|
Length old_end = iterator_end_position(&old_iter);
|
|
|
|
|
Length new_end = iterator_end_position(&new_iter);
|
2016-10-16 20:42:55 -07:00
|
|
|
|
2017-01-24 12:48:47 -08:00
|
|
|
if (position.bytes < old_start.bytes) {
|
|
|
|
|
if (position.bytes < new_start.bytes) {
|
|
|
|
|
next_position = length_min(old_start, new_start);
|
2016-10-16 20:42:55 -07:00
|
|
|
} else {
|
|
|
|
|
is_changed = true;
|
2017-01-24 12:48:47 -08:00
|
|
|
next_position = old_start;
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
2017-01-24 12:48:47 -08:00
|
|
|
} else if (position.bytes < new_start.bytes) {
|
2016-10-16 20:42:55 -07:00
|
|
|
is_changed = true;
|
2017-01-24 12:48:47 -08:00
|
|
|
next_position = new_start;
|
2017-07-17 17:07:56 -07:00
|
|
|
} else {
|
2017-08-04 14:50:50 -07:00
|
|
|
// printf("At [%-2u, %-2u] Compare ", position.extent.row, position.extent.column);
|
|
|
|
|
// iterator_print_state(&old_iter);
|
|
|
|
|
// printf("\tvs\t");
|
|
|
|
|
// iterator_print_state(&new_iter);
|
|
|
|
|
// printf("\n");
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
switch (iterator_compare(&old_iter, &new_iter)) {
|
|
|
|
|
case IteratorMatches:
|
2017-07-17 17:07:56 -07:00
|
|
|
next_position = old_end;
|
|
|
|
|
break;
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
case IteratorMayDiffer:
|
|
|
|
|
next_position = length_min(old_end, new_end);
|
|
|
|
|
if (iterator_descend(&old_iter, position)) {
|
|
|
|
|
if (iterator_descend(&new_iter, position)) {
|
|
|
|
|
next_position = position;
|
|
|
|
|
} else {
|
|
|
|
|
iterator_ascend(&old_iter);
|
2017-07-17 17:07:56 -07:00
|
|
|
is_changed = true;
|
|
|
|
|
}
|
2017-08-04 14:03:41 -07:00
|
|
|
} else if (iterator_descend(&new_iter, position)) {
|
|
|
|
|
iterator_ascend(&new_iter);
|
2017-07-17 17:07:56 -07:00
|
|
|
is_changed = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
case IteratorDiffers:
|
2017-07-17 17:07:56 -07:00
|
|
|
next_position = length_min(old_end, new_end);
|
2017-08-04 14:50:50 -07:00
|
|
|
is_changed = true;
|
2017-07-17 17:07:56 -07:00
|
|
|
break;
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:50:50 -07:00
|
|
|
while (old_end.bytes <= next_position.bytes && iterator_advance(&old_iter)) {
|
|
|
|
|
old_end = iterator_end_position(&old_iter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (new_end.bytes <= next_position.bytes && iterator_advance(&new_iter)) {
|
|
|
|
|
new_end = iterator_end_position(&new_iter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (old_iter.visible_depth > new_iter.visible_depth) {
|
|
|
|
|
iterator_ascend(&old_iter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (new_iter.visible_depth > old_iter.visible_depth) {
|
|
|
|
|
iterator_ascend(&new_iter);
|
|
|
|
|
}
|
2016-10-16 20:42:55 -07:00
|
|
|
|
2017-08-04 11:03:14 -07:00
|
|
|
if (is_changed) {
|
2017-08-04 14:03:41 -07:00
|
|
|
// printf(
|
2017-08-04 14:50:50 -07:00
|
|
|
// " changed range: [[%u, %u] - [%u, %u]]\n",
|
2017-08-04 14:03:41 -07:00
|
|
|
// position.extent.row, position.extent.column,
|
|
|
|
|
// next_position.extent.row, next_position.extent.column
|
|
|
|
|
// );
|
2017-08-04 14:50:50 -07:00
|
|
|
|
2017-08-04 11:03:14 -07:00
|
|
|
range_array_add(&results, position.extent, next_position.extent);
|
|
|
|
|
}
|
2017-08-04 14:03:41 -07:00
|
|
|
|
2016-10-16 20:42:55 -07:00
|
|
|
position = next_position;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 14:03:41 -07:00
|
|
|
*path1 = old_iter.path;
|
|
|
|
|
*path2 = new_iter.path;
|
2016-10-16 20:42:55 -07:00
|
|
|
*ranges = results.contents;
|
2017-08-04 11:03:14 -07:00
|
|
|
return results.size;
|
2016-10-16 20:42:55 -07:00
|
|
|
}
|