From f0c3c6115d278aa80a596abe60df004c85bea28a Mon Sep 17 00:00:00 2001 From: Elias Aebi Date: Wed, 4 Oct 2023 18:29:22 +0200 Subject: [PATCH] remove the tree-sitter wrapper --- atom/src/syntax-scope-map.h | 4 +- atom/src/tree-sitter-grammar.cc | 14 +-- atom/src/tree-sitter-language-mode.cc | 120 +++++++++++++++----------- atom/src/tree-sitter-language-mode.h | 13 ++- tree-sitter/CMakeLists.txt | 2 - tree-sitter/meson.build | 2 - tree-sitter/src/parser.cc | 7 +- tree-sitter/src/parser.h | 5 +- tree-sitter/src/tree-cursor.cc | 66 -------------- tree-sitter/src/tree-cursor.h | 28 ------ tree-sitter/src/tree-sitter.cc | 16 ++++ tree-sitter/src/tree-sitter.h | 4 + tree-sitter/src/tree.cc | 52 ----------- tree-sitter/src/tree.h | 25 ------ 14 files changed, 114 insertions(+), 244 deletions(-) delete mode 100644 tree-sitter/src/tree-cursor.cc delete mode 100644 tree-sitter/src/tree-cursor.h delete mode 100644 tree-sitter/src/tree.cc delete mode 100644 tree-sitter/src/tree.h diff --git a/atom/src/syntax-scope-map.h b/atom/src/syntax-scope-map.h index 5b1112e..a891c35 100644 --- a/atom/src/syntax-scope-map.h +++ b/atom/src/syntax-scope-map.h @@ -2,18 +2,18 @@ #define SYNTAX_SCOPE_MAP_H_ #include +#include #include #include #include #include struct TextBuffer; -struct TreeCursor; struct SyntaxScopeMap { struct Result { virtual ~Result(); - virtual optional applyLeafRules(TextBuffer *, const TreeCursor &) = 0; + virtual optional applyLeafRules(TextBuffer *, TSTreeCursor *) = 0; }; struct Table { std::unordered_map> indices; diff --git a/atom/src/tree-sitter-grammar.cc b/atom/src/tree-sitter-grammar.cc index 65fe109..a263ed7 100644 --- a/atom/src/tree-sitter-grammar.cc +++ b/atom/src/tree-sitter-grammar.cc @@ -2,7 +2,7 @@ #include "syntax-scope-map.h" #include "tree-sitter-language-mode.h" #include -#include +#include #include TreeSitterGrammar::TreeSitterGrammar(const char *name, const char *scopeName, const TSLanguage *languageModule) : Grammar(name, scopeName) { @@ -35,7 +35,7 @@ std::shared_ptr TreeSitterGrammar::preprocessScopes(cons struct StringResult final : SyntaxScopeMap::Result { std::string rules; StringResult(const char *value) : rules(value) {} - optional applyLeafRules(TextBuffer *, const TreeCursor &) override { + optional applyLeafRules(TextBuffer *, TSTreeCursor *) override { return rules; } }; @@ -47,8 +47,8 @@ std::shared_ptr TreeSitterGrammar::preprocessScopesExact std::u16string exact; std::shared_ptr scopes; ExactResult(const char16_t *exact, std::shared_ptr &&scopes) : exact(exact), scopes(std::move(scopes)) {} - optional applyLeafRules(TextBuffer *buffer, const TreeCursor &cursor) override { - const std::u16string nodeText = buffer->buffer->text_in_range({cursor.startPosition(), cursor.endPosition()}); + optional applyLeafRules(TextBuffer *buffer, TSTreeCursor *cursor) override { + const std::u16string nodeText = buffer->buffer->text_in_range({startPosition(cursor), endPosition(cursor)}); return nodeText == exact ? scopes->applyLeafRules(buffer, cursor) : optional(); @@ -62,8 +62,8 @@ std::shared_ptr TreeSitterGrammar::preprocessScopesMatch Regex match; std::shared_ptr scopes; MatchResult(const char16_t *match, std::shared_ptr &&scopes) : match(match), scopes(std::move(scopes)) {} - optional applyLeafRules(TextBuffer *buffer, const TreeCursor &cursor) override { - const std::u16string nodeText = buffer->buffer->text_in_range({cursor.startPosition(), cursor.endPosition()}); + optional applyLeafRules(TextBuffer *buffer, TSTreeCursor *cursor) override { + const std::u16string nodeText = buffer->buffer->text_in_range({startPosition(cursor), endPosition(cursor)}); return match.match(nodeText) ? scopes->applyLeafRules(buffer, cursor) : optional(); @@ -76,7 +76,7 @@ std::shared_ptr TreeSitterGrammar::preprocessScopes(std: struct ArrayResult final : SyntaxScopeMap::Result { std::vector> rules; ArrayResult(std::vector> &&value) : rules(std::move(value)) {} - optional applyLeafRules(TextBuffer *buffer, const TreeCursor &cursor) override { + optional applyLeafRules(TextBuffer *buffer, TSTreeCursor *cursor) override { for (size_t i = 0, length = rules.size(); i != length; ++i) { const auto result = rules[i]->applyLeafRules(buffer, cursor); if (result) return result; diff --git a/atom/src/tree-sitter-language-mode.cc b/atom/src/tree-sitter-language-mode.cc index f1e0383..4e2cdda 100644 --- a/atom/src/tree-sitter-language-mode.cc +++ b/atom/src/tree-sitter-language-mode.cc @@ -9,6 +9,7 @@ static void insertContainingTag(int32_t, double, std::vector &, std::ve template static Range rangeForNode(T); static bool nodeContainsIndices(TSNode, double, double); static bool nodeIsSmaller(TSNode, TSNode); +static optional gotoFirstChildForIndex(TSTreeCursor *, double); template static T *last(const std::vector> &); template static const T &last(const std::vector &); @@ -72,10 +73,10 @@ void TreeSitterLanguageMode::bufferDidFinishTransaction() { this->rootLanguageLayer->update(nullptr); } -Tree TreeSitterLanguageMode::parse(const TSLanguage *language, const Tree &oldTree, const std::vector &ranges) { +TSTree *TreeSitterLanguageMode::parse(const TSLanguage *language, TSTree *oldTree, const std::vector &ranges) { Parser *parser = /* PARSER_POOL.pop() || */ new Parser(); parser->setLanguage(language); - const Tree result = parser->parseTextBufferSync(this->buffer->buffer, oldTree, + TSTree *result = parser->parseTextBufferSync(this->buffer->buffer, oldTree, //syncTimeoutMicros: this.syncTimeoutMicros, ranges ); @@ -236,7 +237,7 @@ double TreeSitterLanguageMode::indentLevelForLine(const std::u16string &line, do Section - Folding */ -void TreeSitterLanguageMode::forEachTreeWithRange_(Range range, std::function callback) { +void TreeSitterLanguageMode::forEachTreeWithRange_(Range range, std::function callback) { if (this->rootLanguageLayer->tree) { callback(this->rootLanguageLayer->tree, this->rootLanguageLayer->grammar); } @@ -247,7 +248,7 @@ void TreeSitterLanguageMode::forEachTreeWithRange_(Range range, std::functionlanguageLayersByMarker[injectionMarker]; - const Tree &tree = languageLayer->tree; + TSTree *tree = languageLayer->tree; TreeSitterGrammar *grammar = languageLayer->grammar; if (tree) callback(tree, grammar); } @@ -268,8 +269,8 @@ std::pair TreeSitterLanguageMode::getSyntaxNodeAndG TSNode smallestNode = {}; TreeSitterGrammar *smallestNodeGrammar = this->grammar; - this->forEachTreeWithRange_(range, [&](const Tree &tree, TreeSitterGrammar *grammar) { - TSNode node = ts_node_descendant_for_byte_range(tree.rootNode(), startIndex * 2, searchEndIndex * 2); + this->forEachTreeWithRange_(range, [&](TSTree *tree, TreeSitterGrammar *grammar) { + TSNode node = ts_node_descendant_for_byte_range(ts_tree_root_node(tree), startIndex * 2, searchEndIndex * 2); while (!ts_node_is_null(node)) { if ( nodeContainsIndices(node, startIndex, endIndex) && @@ -338,20 +339,27 @@ TreeSitterLanguageMode::LanguageLayer::LanguageLayer(Marker *marker, TreeSitterL this->marker = marker; this->languageMode = languageMode; this->grammar = grammar; + this->tree = nullptr; this->depth = depth; } -TreeSitterLanguageMode::LanguageLayer::~LanguageLayer() {} +TreeSitterLanguageMode::LanguageLayer::~LanguageLayer() { + ts_tree_delete(this->tree); +} std::unique_ptr TreeSitterLanguageMode::LanguageLayer::buildHighlightIterator() { - return std::unique_ptr(new LayerHighlightIterator(this, this->tree.walk())); + return std::unique_ptr(new LayerHighlightIterator(this, ts_tree_cursor_new(ts_tree_root_node(this->tree)))); +} + +static void edit(TSTree *tree, const TSInputEdit &edit) { + ts_tree_edit(tree, &edit); } void TreeSitterLanguageMode::LanguageLayer::handleTextChange(const TreeEdit &edit, const std::u16string &oldText, const std::u16string &newText) { const Point startPosition = edit.startPosition, oldEndPosition = edit.oldEndPosition, newEndPosition = edit.newEndPosition; if (this->tree) { - this->tree.edit(edit); + ::edit(this->tree, edit); if (this->editedRange) { if (startPosition.isLessThan(this->editedRange->start)) { this->editedRange->start = startPosition; @@ -380,7 +388,8 @@ void TreeSitterLanguageMode::LanguageLayer::handleTextChange(const TreeEdit &edi } void TreeSitterLanguageMode::LanguageLayer::destroy() { - this->tree = Tree(); + ts_tree_delete(this->tree); + this->tree = nullptr; this->marker->destroy(); for (Marker *marker : this->languageMode->injectionsMarkerLayer->getMarkers()) { if (this->languageMode->parentLanguageLayersByMarker[marker] == this) { @@ -421,7 +430,7 @@ void TreeSitterLanguageMode::LanguageLayer::performUpdate_(NodeRangeSet *nodeRan this->editedRange = optional(); //this.patchSinceCurrentParseStarted = new Patch(); - auto tree = this->languageMode->parse( + TSTree *tree = this->languageMode->parse( this->grammar->languageModule, this->tree, includedRanges @@ -454,17 +463,20 @@ void TreeSitterLanguageMode::LanguageLayer::performUpdate_(NodeRangeSet *nodeRan }*/ if (this->tree) { - const auto rangesWithSyntaxChanges = this->tree.getChangedRanges(tree); + uint32_t length; + TSRange *rangesWithSyntaxChanges = ts_tree_get_changed_ranges(this->tree, tree, &length); + ts_tree_delete(this->tree); this->tree = tree; - if (rangesWithSyntaxChanges.size() > 0) { - for (const auto range : rangesWithSyntaxChanges) { + if (length > 0) { + for (uint32_t i = 0; i < length; i++) { + const TSRange range = rangesWithSyntaxChanges[i]; this->languageMode->emitRangeUpdate(rangeForNode(range)); } const Range combinedRangeWithSyntaxChange = Range( startPosition(rangesWithSyntaxChanges[0]), - endPosition(last(rangesWithSyntaxChanges)) + endPosition(rangesWithSyntaxChanges[length - 1]) ); if (affectedRange) { @@ -474,9 +486,10 @@ void TreeSitterLanguageMode::LanguageLayer::performUpdate_(NodeRangeSet *nodeRan affectedRange = combinedRangeWithSyntaxChange; } } + free(rangesWithSyntaxChanges); } else { this->tree = tree; - this->languageMode->emitRangeUpdate(rangeForNode(tree.rootNode())); + this->languageMode->emitRangeUpdate(rangeForNode(ts_tree_root_node(tree))); if (includedRanges.size()) { affectedRange = Range( startPosition(includedRanges[0]), @@ -516,7 +529,7 @@ void TreeSitterLanguageMode::LanguageLayer::populateInjections_(Range range, Nod } std::unordered_map markersToUpdate; - const auto nodes = descendantsOfType(this->tree.rootNode(), + const auto nodes = descendantsOfType(ts_tree_root_node(this->tree), keys(this->grammar->injectionPointsByType), range.start, range.end @@ -737,7 +750,7 @@ std::vector TreeSitterLanguageMode::HighlightIterator::getOpenScopeIds( LayerHighlightIterator */ -TreeSitterLanguageMode::LayerHighlightIterator::LayerHighlightIterator(LanguageLayer *languageLayer, TreeCursor treeCursor): treeCursor{treeCursor} { +TreeSitterLanguageMode::LayerHighlightIterator::LayerHighlightIterator(LanguageLayer *languageLayer, TSTreeCursor treeCursor): treeCursor{treeCursor} { this->languageLayer = languageLayer; this->depth = this->languageLayer->depth; @@ -761,10 +774,12 @@ TreeSitterLanguageMode::LayerHighlightIterator::LayerHighlightIterator(LanguageL //this->openTags = {}; } -TreeSitterLanguageMode::LayerHighlightIterator::~LayerHighlightIterator() {} +TreeSitterLanguageMode::LayerHighlightIterator::~LayerHighlightIterator() { + ts_tree_cursor_delete(&this->treeCursor); +} bool TreeSitterLanguageMode::LayerHighlightIterator::seek(double targetIndex, std::vector &containingTags, std::vector &containingTagStartIndices) { - while (this->treeCursor.gotoParent()) {} + while (ts_tree_cursor_goto_parent(&this->treeCursor)) {} this->atEnd = true; this->closeTags.clear(); @@ -775,26 +790,26 @@ bool TreeSitterLanguageMode::LayerHighlightIterator::seek(double targetIndex, st std::vector containingTagEndIndices; - if (targetIndex >= this->treeCursor.endIndex()) { + if (targetIndex >= endIndex(&this->treeCursor)) { return false; } optional childIndex = -1; for (;;) { - this->containingNodeTypes.push_back(this->treeCursor.nodeType()); + this->containingNodeTypes.push_back(ts_node_type(ts_tree_cursor_current_node(&this->treeCursor))); this->containingNodeChildIndices.push_back(*childIndex); - this->containingNodeEndIndices.push_back(this->treeCursor.endIndex()); + this->containingNodeEndIndices.push_back(endIndex(&this->treeCursor)); const auto scopeId = this->currentScopeId_(); if (scopeId) { - if (this->treeCursor.startIndex() < targetIndex) { + if (startIndex(&this->treeCursor) < targetIndex) { insertContainingTag( *scopeId, - this->treeCursor.startIndex(), + startIndex(&this->treeCursor), containingTags, containingTagStartIndices ); - containingTagEndIndices.push_back(this->treeCursor.endIndex()); + containingTagEndIndices.push_back(endIndex(&this->treeCursor)); } else { this->atEnd = false; this->openTags.push_back(*scopeId); @@ -803,20 +818,20 @@ bool TreeSitterLanguageMode::LayerHighlightIterator::seek(double targetIndex, st } } - childIndex = this->treeCursor.gotoFirstChildForIndex(targetIndex); + childIndex = gotoFirstChildForIndex(&this->treeCursor, targetIndex); if (!childIndex) break; - if (this->treeCursor.startIndex() >= targetIndex) this->atEnd = false; + if (startIndex(&this->treeCursor) >= targetIndex) this->atEnd = false; } if (this->atEnd) { - this->offset = this->treeCursor.endIndex(); + this->offset = endIndex(&this->treeCursor); for (size_t i = 0, length = containingTags.size(); i < length; i++) { if (i < containingTagEndIndices.size() && containingTagEndIndices[i] == this->offset) { this->closeTags.push_back(containingTags[i]); } } } else { - this->offset = this->treeCursor.startIndex(); + this->offset = startIndex(&this->treeCursor); } return true; @@ -847,9 +862,9 @@ bool TreeSitterLanguageMode::LayerHighlightIterator::moveToSuccessor() { } if (this->atEnd) { - this->offset = this->treeCursor.endIndex(); + this->offset = endIndex(&this->treeCursor); } else { - this->offset = this->treeCursor.startIndex(); + this->offset = startIndex(&this->treeCursor); } return true; @@ -857,9 +872,9 @@ bool TreeSitterLanguageMode::LayerHighlightIterator::moveToSuccessor() { Point TreeSitterLanguageMode::LayerHighlightIterator::getPosition() { if (this->atEnd) { - return this->treeCursor.endPosition(); + return endPosition(&this->treeCursor); } else { - return this->treeCursor.startPosition(); + return startPosition(&this->treeCursor); } } @@ -885,7 +900,7 @@ bool TreeSitterLanguageMode::LayerHighlightIterator::isAtInjectionBoundary() { bool TreeSitterLanguageMode::LayerHighlightIterator::moveUp_(bool atLastChild) { bool result = false; - const double endIndex = this->treeCursor.endIndex(); + const double endIndex = ::endIndex(&this->treeCursor); size_t depth = this->containingNodeEndIndices.size(); // The iterator should not move up until it has visited all of the children of this node. @@ -895,7 +910,7 @@ bool TreeSitterLanguageMode::LayerHighlightIterator::moveUp_(bool atLastChild) { ) { atLastChild = false; result = true; - this->treeCursor.gotoParent(); + ts_tree_cursor_goto_parent(&this->treeCursor); this->containingNodeTypes.pop_back(); this->containingNodeChildIndices.pop_back(); this->containingNodeEndIndices.pop_back(); @@ -908,24 +923,24 @@ bool TreeSitterLanguageMode::LayerHighlightIterator::moveUp_(bool atLastChild) { bool TreeSitterLanguageMode::LayerHighlightIterator::moveDown_() { bool result = false; - const double startIndex = this->treeCursor.startIndex(); + const double startIndex = ::startIndex(&this->treeCursor); // Once the iterator has found a scope boundary, it needs to stay at the same // position, so it should not move down if the first child node starts later than the // current node. - while (this->treeCursor.gotoFirstChild()) { + while (ts_tree_cursor_goto_first_child(&this->treeCursor)) { if ( (this->closeTags.size() || this->openTags.size()) && - this->treeCursor.startIndex() > startIndex + ::startIndex(&this->treeCursor) > startIndex ) { - this->treeCursor.gotoParent(); + ts_tree_cursor_goto_parent(&this->treeCursor); break; } result = true; - this->containingNodeTypes.push_back(this->treeCursor.nodeType()); + this->containingNodeTypes.push_back(ts_node_type(ts_tree_cursor_current_node(&this->treeCursor))); this->containingNodeChildIndices.push_back(0); - this->containingNodeEndIndices.push_back(this->treeCursor.endIndex()); + this->containingNodeEndIndices.push_back(endIndex(&this->treeCursor)); const auto scopeId = this->currentScopeId_(); if (scopeId) this->openTags.push_back(*scopeId); @@ -935,11 +950,11 @@ bool TreeSitterLanguageMode::LayerHighlightIterator::moveDown_() { } bool TreeSitterLanguageMode::LayerHighlightIterator::moveRight_() { - if (this->treeCursor.gotoNextSibling()) { + if (ts_tree_cursor_goto_next_sibling(&this->treeCursor)) { const size_t depth = this->containingNodeTypes.size(); - this->containingNodeTypes[depth - 1] = this->treeCursor.nodeType(); + this->containingNodeTypes[depth - 1] = ts_node_type(ts_tree_cursor_current_node(&this->treeCursor)); this->containingNodeChildIndices[depth - 1]++; - this->containingNodeEndIndices[depth - 1] = this->treeCursor.endIndex(); + this->containingNodeEndIndices[depth - 1] = endIndex(&this->treeCursor); return true; } return false; @@ -949,11 +964,11 @@ optional TreeSitterLanguageMode::LayerHighlightIterator::currentScopeId SyntaxScopeMap::Result *value = this->languageLayer->grammar->scopeMap->get( this->containingNodeTypes, this->containingNodeChildIndices, - this->treeCursor.nodeIsNamed() + ts_node_is_named(ts_tree_cursor_current_node(&this->treeCursor)) ); TextBuffer *buffer = this->languageLayer->languageMode->buffer; - const auto scopeName = value ? value->applyLeafRules(buffer, this->treeCursor) : optional(); - const TSNode node = this->treeCursor.currentNode(); + const auto scopeName = value ? value->applyLeafRules(buffer, &this->treeCursor) : optional(); + const TSNode node = ts_tree_cursor_current_node(&this->treeCursor); if (!ts_node_child_count(node)) { return this->languageLayer->languageMode->grammar->idForScope(scopeName); } else if (scopeName) { @@ -1093,6 +1108,15 @@ static bool nodeIsSmaller(TSNode left, TSNode right) { return endIndex(left) - startIndex(left) < endIndex(right) - startIndex(right); } +static optional gotoFirstChildForIndex(TSTreeCursor *treeCursor, double index) { + const int64_t child_index = ts_tree_cursor_goto_first_child_for_byte(treeCursor, index * 2); + if (child_index < 0) { + return optional(); + } else { + return optional(child_index); + } +} + template static T *last(const std::vector> &array) { if (array.size() > 0) { return array[array.size() - 1].get(); diff --git a/atom/src/tree-sitter-language-mode.h b/atom/src/tree-sitter-language-mode.h index 97f2fff..bb77cf9 100644 --- a/atom/src/tree-sitter-language-mode.h +++ b/atom/src/tree-sitter-language-mode.h @@ -2,8 +2,7 @@ #define TREE_SITTER_LANGUAGE_MODE_H_ #include -#include -#include +#include #include #include @@ -40,7 +39,7 @@ struct TreeSitterLanguageMode final : LanguageMode { Marker *marker; TreeSitterLanguageMode *languageMode; TreeSitterGrammar *grammar; - Tree tree; + TSTree *tree; double depth; optional editedRange; LanguageLayer(Marker *, TreeSitterLanguageMode *, TreeSitterGrammar *, double); @@ -58,14 +57,14 @@ struct TreeSitterLanguageMode final : LanguageMode { LanguageLayer *languageLayer; double depth; bool atEnd; - TreeCursor treeCursor; + TSTreeCursor treeCursor; double offset; std::vector containingNodeTypes; std::vector containingNodeChildIndices; std::vector containingNodeEndIndices; std::vector closeTags; std::vector openTags; - LayerHighlightIterator(LanguageLayer *, TreeCursor); + LayerHighlightIterator(LanguageLayer *, TSTreeCursor); ~LayerHighlightIterator(); bool seek(double, std::vector &, std::vector &); bool moveToSuccessor(); @@ -108,7 +107,7 @@ struct TreeSitterLanguageMode final : LanguageMode { void bufferDidChange(Range, Range, const std::u16string &, const std::u16string &) override; void bufferDidFinishTransaction() override; - Tree parse(const TSLanguage *, const Tree &, const std::vector &); + TSTree *parse(const TSLanguage *, TSTree *, const std::vector &); std::unique_ptr buildHighlightIterator() override; void onDidChangeHighlighting(std::function) override; std::string classNameForScopeId(int32_t) override; @@ -118,7 +117,7 @@ struct TreeSitterLanguageMode final : LanguageMode { optional suggestedIndentForEditedBufferRow(double, double) override; double suggestedIndentForLineWithScopeAtBufferRow_(double, const std::u16string &, double, bool = true); double indentLevelForLine(const std::u16string &, double); - void forEachTreeWithRange_(Range, std::function); + void forEachTreeWithRange_(Range, std::function); TSNode getSyntaxNodeContainingRange(Range, std::function = [](TSNode, TreeSitterGrammar *) { return true; }); std::pair getSyntaxNodeAndGrammarContainingRange(Range, std::function = [](TSNode, TreeSitterGrammar *) { return true; }); optional getRangeForSyntaxNodeContainingRange(Range) override; diff --git a/tree-sitter/CMakeLists.txt b/tree-sitter/CMakeLists.txt index c0f0794..e6158d2 100644 --- a/tree-sitter/CMakeLists.txt +++ b/tree-sitter/CMakeLists.txt @@ -18,9 +18,7 @@ add_library(tree-sitter STATIC) target_link_libraries(tree-sitter tree-sitter-lib superstring) target_sources(tree-sitter PRIVATE src/parser.cc - src/tree-cursor.cc src/tree-sitter.cc - src/tree.cc ) target_include_directories(tree-sitter INTERFACE src diff --git a/tree-sitter/meson.build b/tree-sitter/meson.build index 79fe552..f0f6bb0 100644 --- a/tree-sitter/meson.build +++ b/tree-sitter/meson.build @@ -3,9 +3,7 @@ tree_sitter = declare_dependency( 'tree-sitter', files( 'src/parser.cc', - 'src/tree-cursor.cc', 'src/tree-sitter.cc', - 'src/tree.cc', ), include_directories: include_directories( 'vendor/tree-sitter/lib/include', diff --git a/tree-sitter/src/parser.cc b/tree-sitter/src/parser.cc index 95f17e8..a479bf3 100644 --- a/tree-sitter/src/parser.cc +++ b/tree-sitter/src/parser.cc @@ -79,12 +79,13 @@ void Parser::setLanguage(const TSLanguage *language) { ts_parser_set_language(parser, language); } -Tree Parser::parseTextBufferSync(NativeTextBuffer *text_buffer, const Tree &old_tree, const std::vector &included_ranges) { +TSTree *Parser::parseTextBufferSync(NativeTextBuffer *text_buffer, TSTree *old_tree, const std::vector &included_ranges) { ts_parser_set_included_ranges(parser, included_ranges.data(), included_ranges.size()); NativeTextBuffer::Snapshot *snapshot = text_buffer->create_snapshot(); auto slices = snapshot->primitive_chunks(); TextBufferInput input(&slices); - TSTree *result = ts_parser_parse(parser, old_tree.tree, input.input()); + TSTree *result = ts_parser_parse(parser, old_tree, input.input()); + delete snapshot; - return Tree(result); + return result; } diff --git a/tree-sitter/src/parser.h b/tree-sitter/src/parser.h index 0720d2a..2ceb8f6 100644 --- a/tree-sitter/src/parser.h +++ b/tree-sitter/src/parser.h @@ -1,7 +1,8 @@ #ifndef PARSER_H_ #define PARSER_H_ -#include "tree.h" +#include +#include struct TSParser; struct TSLanguage; @@ -15,7 +16,7 @@ class Parser { ~Parser(); void setLanguage(const TSLanguage *); - Tree parseTextBufferSync(NativeTextBuffer *, const Tree &, const std::vector &); + TSTree *parseTextBufferSync(NativeTextBuffer *, TSTree *, const std::vector &); }; #endif // PARSER_H_ diff --git a/tree-sitter/src/tree-cursor.cc b/tree-sitter/src/tree-cursor.cc deleted file mode 100644 index 556fa89..0000000 --- a/tree-sitter/src/tree-cursor.cc +++ /dev/null @@ -1,66 +0,0 @@ -#include "tree-cursor.h" - -TreeCursor::TreeCursor(TSNode node) : tree_cursor{ts_tree_cursor_new(node)} {} - -TreeCursor::TreeCursor(const TreeCursor &tree_cursor) : tree_cursor{ts_tree_cursor_copy(&tree_cursor.tree_cursor)} {} - -TreeCursor::~TreeCursor() { - ts_tree_cursor_delete(&this->tree_cursor); -} - -TreeCursor &TreeCursor::operator=(const TreeCursor &tree_cursor) { - ts_tree_cursor_delete(&this->tree_cursor); - this->tree_cursor = ts_tree_cursor_copy(&tree_cursor.tree_cursor); - return *this; -} - -TSNode TreeCursor::currentNode() const { - return ts_tree_cursor_current_node(&this->tree_cursor); -} - -bool TreeCursor::gotoParent() { - return ts_tree_cursor_goto_parent(&this->tree_cursor); -} - -bool TreeCursor::gotoNextSibling() { - return ts_tree_cursor_goto_next_sibling(&this->tree_cursor); -} - -bool TreeCursor::gotoFirstChild() { - return ts_tree_cursor_goto_first_child(&this->tree_cursor); -} - -optional TreeCursor::gotoFirstChildForIndex(double index) { - const int64_t child_index = ts_tree_cursor_goto_first_child_for_byte(&this->tree_cursor, index * 2); - if (child_index < 0) { - return optional(); - } else { - return optional(child_index); - } -} - -double TreeCursor::startIndex() const { - return ts_node_start_byte(this->currentNode()) / 2; -} - -double TreeCursor::endIndex() const { - return ts_node_end_byte(this->currentNode()) / 2; -} - -NativePoint TreeCursor::startPosition() const { - const TSPoint point = ts_node_start_point(this->currentNode()); - return NativePoint(point.row, point.column / 2); -} - -NativePoint TreeCursor::endPosition() const { - const TSPoint point = ts_node_end_point(this->currentNode()); - return NativePoint(point.row, point.column / 2); -} - -const char *TreeCursor::nodeType() const { - return ts_node_type(this->currentNode()); -} - -bool TreeCursor::nodeIsNamed() const { - return ts_node_is_named(this->currentNode()); -} diff --git a/tree-sitter/src/tree-cursor.h b/tree-sitter/src/tree-cursor.h deleted file mode 100644 index 5080e29..0000000 --- a/tree-sitter/src/tree-cursor.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef TREE_CURSOR_H_ -#define TREE_CURSOR_H_ - -#include -#include -#include - -class TreeCursor { - TSTreeCursor tree_cursor; -public: - TreeCursor(TSNode); - TreeCursor(const TreeCursor &); - ~TreeCursor(); - TreeCursor &operator=(const TreeCursor &); - TSNode currentNode() const; - bool gotoParent(); - bool gotoNextSibling(); - bool gotoFirstChild(); - optional gotoFirstChildForIndex(double); - double startIndex() const; - double endIndex() const; - NativePoint startPosition() const; - NativePoint endPosition() const; - const char *nodeType() const; - bool nodeIsNamed() const; -}; - -#endif // TREE_CURSOR_H_ diff --git a/tree-sitter/src/tree-sitter.cc b/tree-sitter/src/tree-sitter.cc index b031d02..4676d13 100644 --- a/tree-sitter/src/tree-sitter.cc +++ b/tree-sitter/src/tree-sitter.cc @@ -34,6 +34,14 @@ unsigned endIndex(TSRange range) { return range.end_byte / BYTES_PER_CHARACTER; } +unsigned startIndex(TSTreeCursor *tree_cursor) { + return startIndex(ts_tree_cursor_current_node(tree_cursor)); +} + +unsigned endIndex(TSTreeCursor *tree_cursor) { + return endIndex(ts_tree_cursor_current_node(tree_cursor)); +} + NativePoint startPosition(TSNode node) { return PointToJS(ts_node_start_point(node)); } @@ -50,6 +58,14 @@ NativePoint endPosition(TSRange range) { return PointToJS(range.end_point); } +NativePoint startPosition(TSTreeCursor *tree_cursor) { + return startPosition(ts_tree_cursor_current_node(tree_cursor)); +} + +NativePoint endPosition(TSTreeCursor *tree_cursor) { + return endPosition(ts_tree_cursor_current_node(tree_cursor)); +} + std::vector children(TSNode node) { std::vector result; ts_tree_cursor_reset(&scratch_cursor, node); diff --git a/tree-sitter/src/tree-sitter.h b/tree-sitter/src/tree-sitter.h index a2e19b4..f91306e 100644 --- a/tree-sitter/src/tree-sitter.h +++ b/tree-sitter/src/tree-sitter.h @@ -8,10 +8,14 @@ unsigned startIndex(TSNode); unsigned endIndex(TSNode); unsigned startIndex(TSRange); unsigned endIndex(TSRange); +unsigned startIndex(TSTreeCursor *); +unsigned endIndex(TSTreeCursor *); NativePoint startPosition(TSNode); NativePoint endPosition(TSNode); NativePoint startPosition(TSRange); NativePoint endPosition(TSRange); +NativePoint startPosition(TSTreeCursor *); +NativePoint endPosition(TSTreeCursor *); std::vector children(TSNode); std::vector descendantsOfType(TSNode, const std::vector &, const NativePoint &, const NativePoint &); diff --git a/tree-sitter/src/tree.cc b/tree-sitter/src/tree.cc deleted file mode 100644 index 9024447..0000000 --- a/tree-sitter/src/tree.cc +++ /dev/null @@ -1,52 +0,0 @@ -#include "tree.h" -#include - -static TSTree *copy(const TSTree *tree) { - if (tree) { - return ts_tree_copy(tree); - } else { - return nullptr; - } -} - -Tree::Tree(TSTree *tree) : tree{tree} {} - -Tree::Tree() : tree{nullptr} {} - -Tree::Tree(const Tree &tree) : tree{copy(tree.tree)} {} - -Tree::~Tree() { - if (tree) { - ts_tree_delete(this->tree); - } -} - -Tree &Tree::operator=(const Tree &tree) { - ts_tree_delete(this->tree); - this->tree = tree.tree ? ts_tree_copy(tree.tree) : nullptr; - return *this; -} - -Tree::operator bool() const { - return this->tree != nullptr; -} - -void Tree::edit(const TSInputEdit &edit) { - ts_tree_edit(tree, &edit); -} - -TSNode Tree::rootNode() const { - return ts_tree_root_node(tree); -} - -std::vector Tree::getChangedRanges(const Tree &newTree) { - uint32_t length; - TSRange *ranges = ts_tree_get_changed_ranges(tree, newTree.tree, &length); - std::vector result(ranges, ranges + length); - free(ranges); - return result; -} - -TreeCursor Tree::walk() { - return TreeCursor(this->rootNode()); -} diff --git a/tree-sitter/src/tree.h b/tree-sitter/src/tree.h deleted file mode 100644 index c7386d7..0000000 --- a/tree-sitter/src/tree.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef TREE_H_ -#define TREE_H_ - -#include "tree-cursor.h" - -class Tree { - TSTree *tree; - - Tree(TSTree *); -public: - Tree(); - Tree(const Tree &); - ~Tree(); - Tree &operator=(const Tree &); - operator bool() const; - - void edit(const TSInputEdit &); - TSNode rootNode() const; - std::vector getChangedRanges(const Tree &); - TreeCursor walk(); - - friend class Parser; -}; - -#endif // TREE_H_