Major code cleanup.

This commit is contained in:
2025-12-10 22:51:19 +00:00
parent 6c57521833
commit 24c5b70af1
13 changed files with 103 additions and 106 deletions

View File

@@ -1,10 +1,9 @@
#include "../include/ts.h"
#include "../include/editor.h"
#include "../include/rope.h"
#include "../include/knot.h"
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <regex>
#include <string>
#include <unordered_map>
@@ -30,9 +29,6 @@ pcre2_code *get_re(const std::string &pattern) {
return re;
}
static const std::regex scm_regex(
R"((@[A-Za-z0-9_.]+)|(;; \#[0-9a-fA-F]{6} \#[0-9a-fA-F]{6} [01] [01] [01] \d+))");
TSQuery *load_query(const char *query_path, Editor *editor) {
const TSLanguage *lang = editor->language;
std::ifstream file(query_path, std::ios::in | std::ios::binary);
@@ -40,17 +36,31 @@ TSQuery *load_query(const char *query_path, Editor *editor) {
return nullptr;
std::string highlight_query((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
std::smatch match;
int errornumber = 0;
PCRE2_SIZE erroroffset = 0;
pcre2_code *re = pcre2_compile(
(PCRE2_SPTR) R"((@[A-Za-z0-9_.]+)|(;; \#[0-9a-fA-F]{6} \#[0-9a-fA-F]{6} [01] [01] [01] \d+))",
PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroroffset, nullptr);
if (!re)
return nullptr;
pcre2_match_data *match_data =
pcre2_match_data_create_from_pattern(re, nullptr);
std::map<std::string, int> capture_name_cache;
Highlight *c_hl = nullptr;
int i = 0;
int limit = 20;
editor->query_map.resize(limit);
std::string::const_iterator searchStart(highlight_query.cbegin());
while (std::regex_search(searchStart, highlight_query.cend(), match,
scm_regex)) {
std::string mct = match.str();
if (mct.substr(0, 1) == "@") {
PCRE2_SIZE offset = 0;
PCRE2_SIZE subject_length = highlight_query.size();
while (offset < subject_length) {
int rc = pcre2_match(re, (PCRE2_SPTR)highlight_query.c_str(),
subject_length, offset, 0, match_data, nullptr);
if (rc <= 0)
break;
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
std::string mct =
highlight_query.substr(ovector[0], ovector[1] - ovector[0]);
if (!mct.empty() && mct[0] == '@') {
std::string capture_name = mct;
if (!capture_name_cache.count(capture_name)) {
if (c_hl) {
@@ -65,7 +75,7 @@ TSQuery *load_query(const char *query_path, Editor *editor) {
capture_name_cache[capture_name] = i;
i++;
}
} else if (mct.substr(0, 2) == ";;") {
} else if (mct.size() >= 2 && mct[0] == ';' && mct[1] == ';') {
if (c_hl)
delete c_hl;
c_hl = new Highlight();
@@ -78,10 +88,12 @@ TSQuery *load_query(const char *query_path, Editor *editor) {
c_hl->flags = (bold ? CF_BOLD : 0) | (italic ? CF_ITALIC : 0) |
(underline ? CF_UNDERLINE : 0);
}
searchStart = match.suffix().first;
offset = ovector[1];
}
if (c_hl)
delete c_hl;
pcre2_match_data_free(match_data);
pcre2_code_free(re);
uint32_t error_offset = 0;
TSQueryError error_type = (TSQueryError)0;
TSQuery *q = ts_query_new(lang, highlight_query.c_str(),
@@ -202,10 +214,7 @@ void ts_collect_spans(Editor *editor) {
}
parse_counter = 0;
editor->spans.mid_parse = true;
// TODO: Remove this lock and replace with an index
// modifier based on edits made in the `read_ts` function.
std::shared_lock lock(editor->knot_mtx);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tree = ts_parser_parse(editor->parser, copy, tsinput);
lock.unlock();
if (copy)