Skip to content

Commit

Permalink
simplify the tree-sitter usage
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Sep 19, 2023
1 parent bb00b9e commit 18ac53c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 21 deletions.
3 changes: 2 additions & 1 deletion atom/src/syntax-scope-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
#include <unordered_map>
#include <memory>

struct TextBuffer;
struct TreeCursor;

struct SyntaxScopeMap {
struct Result {
virtual ~Result();
virtual optional<std::string> applyLeafRules(const TreeCursor &) = 0;
virtual optional<std::string> applyLeafRules(TextBuffer *, const TreeCursor &) = 0;
};
struct Table {
std::unordered_map<double, std::unique_ptr<Table>> indices;
Expand Down
21 changes: 12 additions & 9 deletions atom/src/tree-sitter-grammar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "tree-sitter-language-mode.h"
#include <regex.h>
#include <tree-cursor.h>
#include <text-buffer.h>

TreeSitterGrammar::TreeSitterGrammar(const char *name, const char *scopeName, const TSLanguage *languageModule) : Grammar(name, scopeName) {
this->scopeMap = new SyntaxScopeMap();
Expand Down Expand Up @@ -34,7 +35,7 @@ std::shared_ptr<SyntaxScopeMap::Result> TreeSitterGrammar::preprocessScopes(cons
struct StringResult final : SyntaxScopeMap::Result {
std::string rules;
StringResult(const char *value) : rules(value) {}
optional<std::string> applyLeafRules(const TreeCursor &) override {
optional<std::string> applyLeafRules(TextBuffer *, const TreeCursor &) override {
return rules;
}
};
Expand All @@ -46,9 +47,10 @@ std::shared_ptr<SyntaxScopeMap::Result> TreeSitterGrammar::preprocessScopesExact
std::u16string exact;
std::shared_ptr<SyntaxScopeMap::Result> scopes;
ExactResult(const char16_t *exact, std::shared_ptr<SyntaxScopeMap::Result> &&scopes) : exact(exact), scopes(std::move(scopes)) {}
optional<std::string> applyLeafRules(const TreeCursor &cursor) override {
return cursor.nodeText() == exact
? scopes->applyLeafRules(cursor)
optional<std::string> applyLeafRules(TextBuffer *buffer, const TreeCursor &cursor) override {
const std::u16string nodeText = buffer->buffer->text_in_range({cursor.startPosition(), cursor.endPosition()});
return nodeText == exact
? scopes->applyLeafRules(buffer, cursor)
: optional<std::string>();
}
};
Expand All @@ -60,9 +62,10 @@ std::shared_ptr<SyntaxScopeMap::Result> TreeSitterGrammar::preprocessScopesMatch
Regex match;
std::shared_ptr<SyntaxScopeMap::Result> scopes;
MatchResult(const char16_t *match, std::shared_ptr<SyntaxScopeMap::Result> &&scopes) : match(match), scopes(std::move(scopes)) {}
optional<std::string> applyLeafRules(const TreeCursor &cursor) override {
return match.match(cursor.nodeText())
? scopes->applyLeafRules(cursor)
optional<std::string> applyLeafRules(TextBuffer *buffer, const TreeCursor &cursor) override {
const std::u16string nodeText = buffer->buffer->text_in_range({cursor.startPosition(), cursor.endPosition()});
return match.match(nodeText)
? scopes->applyLeafRules(buffer, cursor)
: optional<std::string>();
}
};
Expand All @@ -73,9 +76,9 @@ std::shared_ptr<SyntaxScopeMap::Result> TreeSitterGrammar::preprocessScopes(std:
struct ArrayResult final : SyntaxScopeMap::Result {
std::vector<std::shared_ptr<SyntaxScopeMap::Result>> rules;
ArrayResult(std::vector<std::shared_ptr<SyntaxScopeMap::Result>> &&value) : rules(std::move(value)) {}
optional<std::string> applyLeafRules(const TreeCursor &cursor) override {
optional<std::string> applyLeafRules(TextBuffer *buffer, const TreeCursor &cursor) override {
for (size_t i = 0, length = rules.size(); i != length; ++i) {
const auto result = rules[i]->applyLeafRules(cursor);
const auto result = rules[i]->applyLeafRules(buffer, cursor);
if (result) return result;
}
return optional<std::string>();
Expand Down
4 changes: 3 additions & 1 deletion atom/src/tree-sitter-language-mode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,9 @@ optional<int32_t> TreeSitterLanguageMode::LayerHighlightIterator::currentScopeId
this->containingNodeChildIndices,
this->treeCursor.nodeIsNamed()
);
const auto scopeName = value ? value->applyLeafRules(this->treeCursor) : optional<std::string>();
TextBuffer *buffer = this->languageLayer->languageMode->buffer;

const auto scopeName = value ? value->applyLeafRules(buffer, this->treeCursor) : optional<std::string>();
const TSNode node = this->treeCursor.currentNode();
if (!ts_node_child_count(node)) {
return this->languageLayer->languageMode->grammar->idForScope(scopeName);
Expand Down
4 changes: 0 additions & 4 deletions tree-sitter/src/tree-cursor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,3 @@ const char *TreeCursor::nodeType() const {
bool TreeCursor::nodeIsNamed() const {
return ts_node_is_named(this->currentNode());
}

std::u16string TreeCursor::nodeText() const {
return this->tree->getText(*this);
}
1 change: 0 additions & 1 deletion tree-sitter/src/tree-cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class TreeCursor {
NativePoint endPosition() const;
const char *nodeType() const;
bool nodeIsNamed() const;
std::u16string nodeText() const;
};

#endif // TREE_CURSOR_H_
4 changes: 0 additions & 4 deletions tree-sitter/src/tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,3 @@ std::vector<TSRange> Tree::getChangedRanges(const Tree &newTree) {
TreeCursor Tree::walk() {
return TreeCursor(this->rootNode(), this);
}

std::u16string Tree::getText(const TreeCursor &cursor) {
return this->input->text_in_range({cursor.startPosition(), cursor.endPosition()});
}
1 change: 0 additions & 1 deletion tree-sitter/src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class Tree {
TSNode rootNode() const;
std::vector<TSRange> getChangedRanges(const Tree &);
TreeCursor walk();
std::u16string getText(const TreeCursor &);

friend class Parser;
};
Expand Down

0 comments on commit 18ac53c

Please sign in to comment.