tree-sitter/src/runtime/document.c

143 lines
3.6 KiB
C
Raw Normal View History

2016-01-15 15:08:42 -08:00
#include "runtime/alloc.h"
2014-07-17 23:29:11 -07:00
#include "runtime/node.h"
2015-08-22 10:48:34 -07:00
#include "runtime/tree.h"
#include "runtime/parser.h"
2014-08-01 12:43:14 -07:00
#include "runtime/string_input.h"
#include "runtime/document.h"
#include "runtime/tree_path.h"
2014-01-07 21:50:32 -08:00
TSDocument *ts_document_new() {
TSDocument *self = ts_calloc(1, sizeof(TSDocument));
if (!self)
goto error;
2016-08-29 12:08:58 -07:00
if (!parser_init(&self->parser))
goto error;
return self;
error:
if (self)
ts_free(self);
return NULL;
}
2014-01-07 21:50:32 -08:00
2015-10-14 21:52:13 -07:00
void ts_document_free(TSDocument *self) {
2016-08-29 12:08:58 -07:00
parser_destroy(&self->parser);
2015-10-14 21:52:13 -07:00
if (self->tree)
ts_tree_release(self->tree);
2016-02-17 20:41:29 -08:00
ts_document_set_input(self,
(TSInput){ NULL, NULL, NULL, TSInputEncodingUTF8 });
2016-01-15 15:08:42 -08:00
ts_free(self);
2014-02-20 18:38:31 -08:00
}
2015-10-14 21:52:13 -07:00
const TSLanguage *ts_document_language(TSDocument *self) {
return self->parser.language;
}
2015-10-14 21:52:13 -07:00
void ts_document_set_language(TSDocument *self, const TSLanguage *language) {
ts_document_invalidate(self);
parser_set_language(&self->parser, language);
if (self->tree) {
ts_tree_release(self->tree);
self->tree = NULL;
}
}
TSLogger ts_document_logger(const TSDocument *self) {
return self->parser.lexer.logger;
}
void ts_document_set_logger(TSDocument *self, TSLogger logger) {
self->parser.lexer.logger = logger;
2014-09-06 17:56:00 -07:00
}
void ts_document_print_debugging_graphs(TSDocument *self, bool should_print) {
self->parser.print_debugging_graphs = should_print;
}
2015-10-14 21:52:13 -07:00
TSInput ts_document_input(TSDocument *self) {
return self->input;
}
2015-10-14 21:52:13 -07:00
void ts_document_set_input(TSDocument *self, TSInput input) {
2016-01-29 16:40:38 -08:00
if (self->owns_input)
ts_free(self->input.payload);
2015-10-14 21:52:13 -07:00
self->input = input;
2016-01-29 16:40:38 -08:00
self->owns_input = false;
}
2015-10-14 21:52:13 -07:00
void ts_document_set_input_string(TSDocument *self, const char *text) {
ts_document_invalidate(self);
TSInput input = ts_string_input_make(text);
ts_document_set_input(self, input);
if (input.payload) {
self->owns_input = true;
}
}
2015-10-14 21:52:13 -07:00
void ts_document_edit(TSDocument *self, TSInputEdit edit) {
if (!self->tree)
2015-09-20 23:41:40 -07:00
return;
uint32_t max_bytes = ts_tree_total_bytes(self->tree);
2016-09-13 13:08:52 -07:00
if (edit.start_byte > max_bytes)
return;
2016-09-13 13:08:52 -07:00
if (edit.bytes_removed > max_bytes - edit.start_byte)
edit.bytes_removed = max_bytes - edit.start_byte;
2016-09-13 13:08:52 -07:00
ts_tree_edit(self->tree, &edit);
}
2016-11-04 09:18:38 -07:00
void ts_document_parse_and_get_changed_ranges(TSDocument *self, TSRange **ranges,
uint32_t *range_count) {
if (ranges) *ranges = NULL;
if (range_count) *range_count = 0;
if (!self->input.read || !self->parser.language)
2016-11-04 09:18:38 -07:00
return;
Tree *reusable_tree = self->valid ? self->tree : NULL;
2015-12-22 14:20:58 -08:00
if (reusable_tree && !reusable_tree->has_changes)
2016-11-04 09:18:38 -07:00
return;
Tree *tree = parser_parse(&self->parser, self->input, reusable_tree);
if (self->tree) {
Tree *old_tree = self->tree;
self->tree = tree;
if (ranges && range_count) {
tree_path_init(&self->parser.tree_path1, old_tree);
tree_path_init(&self->parser.tree_path2, tree);
2016-11-04 09:18:38 -07:00
tree_path_get_changes(&self->parser.tree_path1, &self->parser.tree_path2,
ranges, range_count);
}
ts_tree_release(old_tree);
}
self->tree = tree;
self->parse_count++;
self->valid = true;
}
2016-11-04 09:18:38 -07:00
void ts_document_parse(TSDocument *self) {
ts_document_parse_and_get_changed_ranges(self, NULL, NULL);
}
void ts_document_invalidate(TSDocument *self) {
self->valid = false;
2014-01-07 21:50:32 -08:00
}
2014-07-17 23:29:11 -07:00
2015-10-14 21:52:13 -07:00
TSNode ts_document_root_node(const TSDocument *self) {
TSNode result = ts_node_make(self->tree, 0, 0, 0);
while (result.data && !((Tree *)result.data)->visible)
result = ts_node_named_child(result, 0);
return result;
2014-07-17 23:29:11 -07:00
}
uint32_t ts_document_parse_count(const TSDocument *self) {
2015-10-14 21:52:13 -07:00
return self->parse_count;
}