Remove redundant functions for Documents
There's no need for a `string` function since one already exists for Nodes. Now the root node is always stored on the document. This means callers of `ts_document_root_node` don't need to release its return value.
This commit is contained in:
parent
7ba3953f7e
commit
8da9219c3a
6 changed files with 23 additions and 43 deletions
|
|
@ -1,22 +1,17 @@
|
|||
#include "tree_sitter/runtime.h"
|
||||
#include "tree_sitter/parser.h"
|
||||
#include "runtime/tree.h"
|
||||
#include "runtime/node.h"
|
||||
#include "runtime/parser.h"
|
||||
#include "runtime/string_input.h"
|
||||
|
||||
struct TSDocument {
|
||||
TSParser parser;
|
||||
const TSTree *tree;
|
||||
TSInput input;
|
||||
TSNode *node;
|
||||
};
|
||||
|
||||
TSDocument *ts_document_make() {
|
||||
TSDocument *document = malloc(sizeof(TSDocument));
|
||||
*document = (TSDocument) {
|
||||
.input = (TSInput) { .data = NULL, .read_fn = NULL, .seek_fn = NULL },
|
||||
.tree = NULL
|
||||
};
|
||||
*document = (TSDocument) {};
|
||||
return document;
|
||||
}
|
||||
|
||||
|
|
@ -24,34 +19,34 @@ void ts_document_free(TSDocument *document) {
|
|||
ts_parser_destroy(&document->parser);
|
||||
if (document->input.release_fn)
|
||||
document->input.release_fn(document->input.data);
|
||||
if (document->node)
|
||||
ts_node_release(document->node);
|
||||
free(document);
|
||||
}
|
||||
|
||||
static void reparse(TSDocument *document, TSInputEdit *edit) {
|
||||
if (document->input.read_fn && document->parser.language) {
|
||||
const TSTree *tree = ts_parser_parse(&document->parser, document->input, edit);
|
||||
if (document->node)
|
||||
ts_node_release(document->node);
|
||||
document->node = ts_node_make_root(tree, document->parser.language->symbol_names);
|
||||
}
|
||||
}
|
||||
|
||||
void ts_document_set_language(TSDocument *document,
|
||||
const TSLanguage *language) {
|
||||
ts_parser_destroy(&document->parser);
|
||||
document->parser = ts_parser_make(language);
|
||||
if (document->input.read_fn)
|
||||
document->tree = ts_parser_parse(&document->parser, document->input, NULL);
|
||||
}
|
||||
|
||||
const TSTree *ts_document_tree(const TSDocument *document) {
|
||||
return document->tree;
|
||||
}
|
||||
|
||||
const char *ts_document_string(const TSDocument *document) {
|
||||
return ts_tree_string(document->tree,
|
||||
document->parser.language->symbol_names);
|
||||
reparse(document, NULL);
|
||||
}
|
||||
|
||||
void ts_document_set_input(TSDocument *document, TSInput input) {
|
||||
document->input = input;
|
||||
if (document->parser.language)
|
||||
document->tree = ts_parser_parse(&document->parser, document->input, NULL);
|
||||
reparse(document, NULL);
|
||||
}
|
||||
|
||||
void ts_document_edit(TSDocument *document, TSInputEdit edit) {
|
||||
document->tree = ts_parser_parse(&document->parser, document->input, &edit);
|
||||
reparse(document, &edit);
|
||||
}
|
||||
|
||||
const char *ts_document_symbol_name(const TSDocument *document,
|
||||
|
|
@ -64,16 +59,5 @@ void ts_document_set_input_string(TSDocument *document, const char *text) {
|
|||
}
|
||||
|
||||
TSNode *ts_document_root_node(const TSDocument *document) {
|
||||
if (document->tree)
|
||||
return ts_node_make_root(document->tree,
|
||||
document->parser.language->symbol_names);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TSNode *ts_document_get_node(const TSDocument *document, size_t pos) {
|
||||
TSNode *root = ts_document_root_node(document);
|
||||
TSNode *result = ts_node_leaf_at_pos(root, pos);
|
||||
ts_node_release(root);
|
||||
return result;
|
||||
return document->node;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue