Bug Fixes and optimizations
This commit is contained in:
+3
-1
@@ -90,7 +90,8 @@ void ts_collect_spans(Editor *editor) {
|
||||
ts_query_cursor_exec(cursor, q, ts_tree_root_node(item.tsset->tree));
|
||||
std::unordered_map<std::string, PendingRanges> pending_injections;
|
||||
TSQueryMatch match;
|
||||
auto subject_fn = [&](const TSNode *node, uint32_t *len) -> char * {
|
||||
auto subject_fn = [&](const TSNode *node, uint32_t *len,
|
||||
bool *allocated) -> char * {
|
||||
uint32_t start = ts_node_start_byte(*node);
|
||||
uint32_t end = ts_node_end_byte(*node);
|
||||
if (start == end || end > editor->root->char_count)
|
||||
@@ -98,6 +99,7 @@ void ts_collect_spans(Editor *editor) {
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
char *text = read(editor->root, start, end - start);
|
||||
*len = end - start;
|
||||
*allocated = true;
|
||||
return text;
|
||||
};
|
||||
while (ts_query_cursor_next_match(cursor, &match)) {
|
||||
|
||||
+6
-3
@@ -117,7 +117,8 @@ TSQuery *load_query(const char *query_path, TSSetBase *set) {
|
||||
|
||||
bool ts_predicate(
|
||||
TSQuery *query, const TSQueryMatch &match,
|
||||
std::function<char *(const TSNode *, uint32_t *len)> subject_fn) {
|
||||
std::function<char *(const TSNode *, uint32_t *len, bool *allocated)>
|
||||
subject_fn) {
|
||||
uint32_t step_count;
|
||||
const TSQueryPredicateStep *steps =
|
||||
ts_query_predicates_for_pattern(query, match.pattern_index, &step_count);
|
||||
@@ -152,13 +153,15 @@ bool ts_predicate(
|
||||
const TSNode *node = find_capture_node(match, subject_id);
|
||||
pcre2_code *re = get_re(regex_txt);
|
||||
uint32_t len;
|
||||
char *subject = subject_fn(node, &len);
|
||||
bool allocated;
|
||||
char *subject = subject_fn(node, &len, &allocated);
|
||||
if (!subject)
|
||||
return false;
|
||||
pcre2_match_data *md = pcre2_match_data_create_from_pattern(re, nullptr);
|
||||
int rc = pcre2_match(re, (PCRE2_SPTR)subject, len, 0, 0, md, nullptr);
|
||||
pcre2_match_data_free(md);
|
||||
bool ok = (rc >= 0);
|
||||
free(subject);
|
||||
if (allocated)
|
||||
free(subject);
|
||||
return (command == "match?" ? ok : !ok);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user