Skip to content

Commit

Permalink
injections
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Sep 18, 2023
1 parent ef67f18 commit ac60f89
Show file tree
Hide file tree
Showing 29 changed files with 678 additions and 175 deletions.
26 changes: 3 additions & 23 deletions atom/src/bracket-matcher-view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "text-editor.h"
#include "selection.h"
#include "tree-sitter-language-mode.h"
#include <tree-sitter.h>
#include <display-marker.h>
#include <cstring>

Expand Down Expand Up @@ -99,27 +100,6 @@ optional<Point> BracketMatcherView::findMatchingStartBracket(Point endBracketPos
}
}

static Point PointToJS(TSPoint point) {
return Point(point.row, point.column / 2);
}
static Point startPosition(TSNode node) {
return PointToJS(ts_node_start_point(node));
}
static Point endPosition(TSNode node) {
return PointToJS(ts_node_end_point(node));
}
static std::vector<TSNode> children(TSNode node) {
static TSTreeCursor scratch_cursor = {nullptr, nullptr, {0, 0}};
std::vector<TSNode> result;
ts_tree_cursor_reset(&scratch_cursor, node);
if (ts_tree_cursor_goto_first_child(&scratch_cursor)) {
do {
TSNode child = ts_tree_cursor_current_node(&scratch_cursor);
result.push_back(child);
} while (ts_tree_cursor_goto_next_sibling(&scratch_cursor));
}
return result;
}
static TSNode ts_node_first_child(TSNode node) {
return ts_node_child(node, 0);
}
Expand All @@ -142,7 +122,7 @@ optional<Point> BracketMatcherView::findMatchingEndBracketWithSyntaxTree(Point b
auto matchNode = std::find_if(children.begin(), children.end(), [&](TSNode child) {
return bracketEndPosition.isLessThanOrEqual(startPosition(child)) && ts_node_type(child)[0] == endBracket;
});
if (matchNode != children.end()) result = startPosition(*matchNode);
if (matchNode != children.end()) result = Point(startPosition(*matchNode));
return true;
}
return false;
Expand All @@ -162,7 +142,7 @@ optional<Point> BracketMatcherView::findMatchingStartBracketWithSyntaxTree(Point
auto matchNode = std::find_if(children.begin(), children.end(), [&](TSNode child) {
return bracketPosition.isGreaterThanOrEqual(endPosition(child)) && ts_node_type(child)[0] == startBracket;
});
if (matchNode != children.end()) result = startPosition(*matchNode);
if (matchNode != children.end()) result = Point(startPosition(*matchNode));
return true;
}
return false;
Expand Down
21 changes: 20 additions & 1 deletion atom/src/grammar-registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void GrammarRegistry::autoAssignLanguageMode(TextBuffer *buffer) {
}

LanguageMode *GrammarRegistry::languageModeForGrammarAndBuffer(Grammar *grammar, TextBuffer *buffer) {
return grammar->getLanguageMode(buffer);
return grammar->getLanguageMode(buffer, this);
}

/*selectGrammar(filePath, fileContents) {
Expand Down Expand Up @@ -199,6 +199,25 @@ std::vector<Grammar *> GrammarRegistry::getGrammars() {
return grammars;
}

TreeSitterGrammar *GrammarRegistry::treeSitterGrammarForLanguageString(const std::u16string &languageString) {
double longestMatchLength = 0;
TreeSitterGrammar *grammarWithLongestMatch = nullptr;
for (const auto &entry : this->treeSitterGrammarsById) {
TreeSitterGrammar *grammar = entry.second;
if (grammar->injectionRegex) {
const auto match = grammar->injectionRegex.match(languageString);
if (match) {
const double length = match.end_offset - match.start_offset;
if (length > longestMatchLength) {
grammarWithLongestMatch = grammar;
longestMatchLength = length;
}
}
}
}
return grammarWithLongestMatch;
}

static std::u16string getGrammarSelectionContent(TextBuffer *buffer) {
return buffer->getTextInRange(
Range(Point(0, 0), buffer->positionForCharacterIndex(1024))
Expand Down
1 change: 1 addition & 0 deletions atom/src/grammar-registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct GrammarRegistry {
double getGrammarPathScore(Grammar *, const optional<std::string> &);
void addGrammar(TreeSitterGrammar *);
std::vector<Grammar *> getGrammars();
TreeSitterGrammar *treeSitterGrammarForLanguageString(const std::u16string &);
};

#endif // GRAMMAR_REGISTRY_H_
3 changes: 2 additions & 1 deletion atom/src/grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

struct LanguageMode;
struct TextBuffer;
struct GrammarRegistry;

struct Grammar {
const char *name;
Expand All @@ -23,7 +24,7 @@ struct Grammar {
template <typename... T> void setFileTypes(T&&... t) {
addFileTypes(std::forward<T>(t)...);
}
virtual LanguageMode *getLanguageMode(TextBuffer *) = 0;
virtual LanguageMode *getLanguageMode(TextBuffer *, GrammarRegistry *) = 0;
};

#endif // GRAMMAR_H_
2 changes: 1 addition & 1 deletion atom/src/null-grammar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ NullGrammar::NullGrammar() : Grammar("Null Grammar", "text.plain.null-grammar")

NullGrammar::~NullGrammar() {}

LanguageMode *NullGrammar::getLanguageMode(TextBuffer *) {
LanguageMode *NullGrammar::getLanguageMode(TextBuffer *, GrammarRegistry *) {
return new TextMateLanguageMode(this);
}
2 changes: 1 addition & 1 deletion atom/src/null-grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct NullGrammar final : Grammar {
NullGrammar();
~NullGrammar();

LanguageMode *getLanguageMode(TextBuffer *) override;
LanguageMode *getLanguageMode(TextBuffer *, GrammarRegistry *) override;
};

#endif // NULL_GRAMMAR_H_
13 changes: 11 additions & 2 deletions atom/src/tree-sitter-grammar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ TreeSitterGrammar::~TreeSitterGrammar() {
delete this->scopeMap;
}

void TreeSitterGrammar::setInjectionRegex(const char16_t *pattern) {
this->injectionRegex = Regex(pattern);
}

void TreeSitterGrammar::setIncreaseIndentPattern(const char16_t *pattern) {
this->increaseIndentRegex = Regex(pattern);
}
Expand Down Expand Up @@ -105,6 +109,11 @@ std::string TreeSitterGrammar::classNameForScopeId(int32_t id) {
return this->classNamesById[id];
}

LanguageMode *TreeSitterGrammar::getLanguageMode(TextBuffer *buffer) {
return new TreeSitterLanguageMode(buffer, this);
void TreeSitterGrammar::addInjectionPoint(const InjectionPoint &injectionPoint) {
std::vector<InjectionPoint> &injectionPoints = this->injectionPointsByType[injectionPoint.type];
injectionPoints.push_back(injectionPoint);
}

LanguageMode *TreeSitterGrammar::getLanguageMode(TextBuffer *buffer, GrammarRegistry *grammars) {
return new TreeSitterLanguageMode(buffer, this, grammars);
}
13 changes: 12 additions & 1 deletion atom/src/tree-sitter-grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "grammar.h"
#include <regex.h>
#include <optional.h>
#include <tree_sitter/api.h>
#include <unordered_map>

struct SyntaxScopeMap;
Expand Down Expand Up @@ -53,6 +54,13 @@ template <typename T> constexpr Match<T> match(const char16_t *match, T scopes)
}

struct TreeSitterGrammar final : Grammar {
struct InjectionPoint {
std::string type;
std::u16string (*language)(TSNode);
std::vector<TSNode> (*content)(TSNode);
};

Regex injectionRegex;
SyntaxScopeMap *scopeMap;
const TSLanguage *languageModule;
std::unordered_map<int32_t, std::string> classNamesById;
Expand All @@ -62,10 +70,12 @@ struct TreeSitterGrammar final : Grammar {
Regex increaseIndentRegex;
Regex decreaseIndentRegex;
Regex decreaseNextIndentRegex;
std::unordered_map<std::string, std::vector<InjectionPoint>> injectionPointsByType;

TreeSitterGrammar(const char *, const char *, const TSLanguage *);
~TreeSitterGrammar();

void setInjectionRegex(const char16_t *);
void setIncreaseIndentPattern(const char16_t *);
void setDecreaseIndentPattern(const char16_t *);
void setDecreaseNextIndentPattern(const char16_t *);
Expand Down Expand Up @@ -114,7 +124,8 @@ struct TreeSitterGrammar final : Grammar {
}
optional<int32_t> idForScope(const optional<std::string> &);
std::string classNameForScopeId(int32_t);
LanguageMode *getLanguageMode(TextBuffer *) override;
void addInjectionPoint(const InjectionPoint &);
LanguageMode *getLanguageMode(TextBuffer *, GrammarRegistry *) override;
};

#endif // TREE_SITTER_GRAMMAR_H_
Loading

0 comments on commit ac60f89

Please sign in to comment.