Skip to content

Commit

Permalink
remove the tree-sitter wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Oct 7, 2023
1 parent 09ccde6 commit f0c3c61
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 244 deletions.
4 changes: 2 additions & 2 deletions atom/src/syntax-scope-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#define SYNTAX_SCOPE_MAP_H_

#include <optional.h>
#include <tree_sitter/api.h>
#include <vector>
#include <string>
#include <unordered_map>
#include <memory>

struct TextBuffer;
struct TreeCursor;

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

TreeSitterGrammar::TreeSitterGrammar(const char *name, const char *scopeName, const TSLanguage *languageModule) : Grammar(name, scopeName) {
Expand Down Expand Up @@ -35,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(TextBuffer *, const TreeCursor &) override {
optional<std::string> applyLeafRules(TextBuffer *, TSTreeCursor *) override {
return rules;
}
};
Expand All @@ -47,8 +47,8 @@ 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(TextBuffer *buffer, const TreeCursor &cursor) override {
const std::u16string nodeText = buffer->buffer->text_in_range({cursor.startPosition(), cursor.endPosition()});
optional<std::string> 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<std::string>();
Expand All @@ -62,8 +62,8 @@ 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(TextBuffer *buffer, const TreeCursor &cursor) override {
const std::u16string nodeText = buffer->buffer->text_in_range({cursor.startPosition(), cursor.endPosition()});
optional<std::string> 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<std::string>();
Expand All @@ -76,7 +76,7 @@ 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(TextBuffer *buffer, const TreeCursor &cursor) override {
optional<std::string> 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;
Expand Down
Loading

0 comments on commit f0c3c61

Please sign in to comment.