diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index be84f85c..2e3cb806 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -74,6 +74,7 @@ void ts_document_edit(TSDocument *, TSInputEdit); TSDebugger ts_document_debugger(const TSDocument *); void ts_document_set_debugger(TSDocument *, TSDebugger); TSNode ts_document_root_node(const TSDocument *); +size_t ts_document_parse_count(const TSDocument *); #define ts_builtin_sym_error 0 #define ts_builtin_sym_end 1 diff --git a/spec/runtime/parser_spec.cc b/spec/runtime/parser_spec.cc index f015545a..3e3c5074 100644 --- a/spec/runtime/parser_spec.cc +++ b/spec/runtime/parser_spec.cc @@ -394,6 +394,16 @@ describe("Parser", [&]() { "(identifier) (member_access (identifier) (identifier))))))")); }); }); + + it("updates the document's parse-count", [&]() { + AssertThat(ts_document_parse_count(doc), Equals(0)); + + set_text("{ x: (b.c) };"); + AssertThat(ts_document_parse_count(doc), Equals(1)); + + insert_text(strlen("{ x"), "yz"); + AssertThat(ts_document_parse_count(doc), Equals(2)); + }); }); describe("lexing", [&]() { diff --git a/src/runtime/document.c b/src/runtime/document.c index bacc5b01..98f5ead1 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -26,6 +26,7 @@ static void ts_document__reparse(TSDocument *document, TSInputEdit *edit) { ts_tree_release(document->tree); document->tree = tree; ts_tree_retain(tree); + document->parse_count++; } } @@ -74,3 +75,7 @@ TSNode ts_document_root_node(const TSDocument *document) { result = ts_node_named_child(result, 0); return result; } + +size_t ts_document_parse_count(const TSDocument *document) { + return document->parse_count; +} diff --git a/src/runtime/document.h b/src/runtime/document.h index b137d076..e0ca51c1 100644 --- a/src/runtime/document.h +++ b/src/runtime/document.h @@ -9,6 +9,7 @@ struct TSDocument { TSParser parser; TSInput input; TSTree *tree; + size_t parse_count; }; #endif