Add static function for stringifying nodes including hidden ones

This commit is contained in:
Max Brunsfeld 2016-04-03 23:45:43 -07:00
parent 849ecf0a13
commit eb47edb446

View file

@ -242,14 +242,13 @@ static size_t write_lookahead_to_string(char *string, size_t limit,
static size_t ts_tree__write_to_string(const TSTree *self,
const TSLanguage *language, char *string,
size_t limit, bool is_root,
bool include_anonymous) {
bool include_all) {
if (!self)
return snprintf(string, limit, "(NULL)");
char *cursor = string;
char **writer = (limit > 0) ? &cursor : &string;
bool visible =
is_root || (self->visible && (include_anonymous || self->named));
bool visible = include_all || is_root || (self->visible && self->named);
if (visible && !is_root)
cursor += snprintf(*writer, limit, " ");
@ -267,7 +266,7 @@ static size_t ts_tree__write_to_string(const TSTree *self,
for (size_t i = 0; i < self->child_count; i++) {
TSTree *child = self->children[i];
cursor += ts_tree__write_to_string(child, language, *writer, limit, false,
include_anonymous);
include_all);
}
if (visible)
@ -276,17 +275,21 @@ static size_t ts_tree__write_to_string(const TSTree *self,
return cursor - string;
}
char *ts_node_string(TSNode self, const TSDocument *document) {
static char *ts_node__string(TSNode self, const TSDocument *document, bool include_all) {
static char SCRATCH[1];
const TSTree *tree = ts_node__tree(self);
const TSLanguage *language = document->parser.language;
size_t size =
ts_tree__write_to_string(tree, language, SCRATCH, 0, true, false) + 1;
ts_tree__write_to_string(tree, language, SCRATCH, 0, true, include_all) + 1;
char *result = ts_malloc(size * sizeof(char));
ts_tree__write_to_string(tree, language, result, size, true, false);
ts_tree__write_to_string(tree, language, result , size, true, include_all);
return result;
}
char *ts_node_string(TSNode self, const TSDocument *document) {
return ts_node__string(self, document, false);
}
bool ts_node_eq(TSNode self, TSNode other) {
return ts_tree_eq(ts_node__tree(self), ts_node__tree(other)) &&
self.offset[0] == other.offset[0] &&