Add custom syntax highlighter and optimize

This commit is contained in:
2026-01-16 21:47:05 +00:00
parent 04cce25bf2
commit 1fda5bf246
77 changed files with 1487 additions and 1673 deletions

View File

@@ -3,7 +3,6 @@
#include "lsp/lsp.h"
#include "pch.h"
#include "ts/decl.h"
static const std::unordered_map<uint8_t, LSP> kLsps = {
{1,
@@ -168,43 +167,41 @@ static const std::unordered_map<uint8_t, LSP> kLsps = {
};
static const std::unordered_map<std::string, Language> kLanguages = {
{"bash", {"bash", LANG(bash), 4, 0x4d5a5e, ""}},
{"c", {"c", LANG(cpp), 1, 0x555555, ""}},
{"cpp", {"cpp", LANG(cpp), 1, 0x00599C, ""}},
{"h", {"h", LANG(cpp), 1, 0xA8B9CC, ""}},
{"css", {"css", LANG(css), 5, 0x36a3d9, ""}},
{"fish", {"fish", LANG(fish), 7, 0x4d5a5e, ""}},
{"go", {"go", LANG(go), 8, 0x00add8, ""}},
{"gomod", {"gomod", LANG(gomod), 8, 0x00add8, ""}},
{"haskell", {"haskell", LANG(haskell), 9, 0xa074c4, ""}},
{"html", {"html", LANG(html), 10, 0xef8a91, ""}},
{"javascript", {"javascript", LANG(javascript), 11, 0xf0df8a, ""}},
{"typescript", {"typescript", LANG(tsx), 11, 0x36a3d9, ""}},
{"json", {"json", LANG(json), 6, 0xcbcb41, "{}"}},
{"jsonc", {"jsonc", LANG(json), 6, 0xcbcb41, "{}"}},
{"erb", {"erb", LANG(embedded_template), 10, 0x6e1516, ""}},
{"ruby", {"ruby", LANG(ruby), 3, 0xff8087, "󰴭 "}},
{"lua", {"lua", LANG(lua), 12, 0x36a3d9, "󰢱 "}},
{"python", {"python", LANG(python), 13, 0x95e6cb, "󰌠 "}},
{"rust", {"rust", LANG(rust), 14, 0xdea584, "󱘗 "}},
{"php", {"php", LANG(php), 15, 0xa074c4, "󰌟 "}},
{"markdown", {"markdown", LANG(markdown), 16, 0x36a3d9, ""}},
{"markdown_inline",
{"markdown_inline", LANG(markdown_inline), 16, 0x36a3d9, " "}},
{"nginx", {"nginx", LANG(nginx), 17, 0x6d8086, " "}},
{"toml", {"toml", LANG(toml), 18, 0x36a3d9, " "}},
{"yaml", {"yaml", LANG(yaml), 19, 0x6d8086, " "}},
{"sql", {"sql", LANG(sql), 20, 0xdad8d8, " "}},
{"make", {"make", LANG(make), 21, 0x4e5c61, " "}},
{"gdscript", {"gdscript", LANG(gdscript), 0, 0x6d8086, " "}},
{"man", {"man", LANG(man), 0, 0xdad8d8, " "}},
{"diff", {"diff", LANG(diff), 0, 0xDD4C35, " "}},
{"gitattributes",
{"gitattributes", LANG(gitattributes), 0, 0xF05032, " "}},
{"gitignore", {"gitignore", LANG(gitignore), 0, 0xF05032, ""}},
{"query", {"query", LANG(query), 0, 0x7E57C2, " "}},
{"regex", {"regex", LANG(regex), 0, 0x9E9E9E, ".*"}},
{"ini", {"ini", LANG(ini), 0, 0x6d8086, ""}},
{"bash", {"bash", 4, 0x4d5a5e, ""}},
{"c", {"c", 1, 0x555555, ""}},
{"cpp", {"cpp", 1, 0x00599C, ""}},
{"h", {"h", 1, 0xA8B9CC, ""}},
{"css", {"css", 5, 0x36a3d9, ""}},
{"fish", {"fish", 7, 0x4d5a5e, ""}},
{"go", {"go", 8, 0x00add8, ""}},
{"gomod", {"gomod", 8, 0x00add8, ""}},
{"haskell", {"haskell", 9, 0xa074c4, ""}},
{"html", {"html", 10, 0xef8a91, ""}},
{"javascript", {"javascript", 11, 0xf0df8a, ""}},
{"typescript", {"typescript", 11, 0x36a3d9, ""}},
{"json", {"json", 6, 0xcbcb41, "{}"}},
{"jsonc", {"jsonc", 6, 0xcbcb41, "{}"}},
{"erb", {"erb", 10, 0x6e1516, ""}},
{"ruby", {"ruby", 3, 0xff8087, "󰴭 "}},
{"lua", {"lua", 12, 0x36a3d9, "󰢱 "}},
{"python", {"python", 13, 0x95e6cb, "󰌠 "}},
{"rust", {"rust", 14, 0xdea584, "󱘗 "}},
{"php", {"php", 15, 0xa074c4, "󰌟 "}},
{"markdown", {"markdown", 16, 0x36a3d9, ""}},
{"markdown_inline", {"markdown_inline", 16, 0x36a3d9, ""}},
{"nginx", {"nginx", 17, 0x6d8086, " "}},
{"toml", {"toml", 18, 0x36a3d9, " "}},
{"yaml", {"yaml", 19, 0x6d8086, " "}},
{"sql", {"sql", 20, 0xdad8d8, " "}},
{"make", {"make", 21, 0x4e5c61, " "}},
{"gdscript", {"gdscript", 0, 0x6d8086, " "}},
{"man", {"man", 0, 0xdad8d8, " "}},
{"diff", {"diff", 0, 0xDD4C35, " "}},
{"gitattributes", {"gitattributes", 0, 0xF05032, " "}},
{"gitignore", {"gitignore", 0, 0xF05032, ""}},
{"query", {"query", 0, 0x7E57C2, " "}},
{"regex", {"regex", 0, 0x9E9E9E, ".*"}},
{"ini", {"ini", 0, 0x6d8086, " "}},
};
static const std::unordered_map<std::string, std::string> kExtToLang = {

View File

@@ -1,7 +1,6 @@
#ifndef EDITOR_COMPLETIONS_H
#define EDITOR_COMPLETIONS_H
#include "editor/decl.h"
#include "pch.h"
#include "ui/completionbox.h"
#include "ui/hover.h"

View File

@@ -9,22 +9,6 @@ struct TextEdit {
std::string text;
};
struct Fold {
uint32_t start;
uint32_t end;
bool contains(uint32_t line) const { return line >= start && line <= end; }
bool operator<(const Fold &other) const { return start < other.start; }
};
struct Span {
uint32_t start;
uint32_t end;
Highlight *hl;
bool operator<(const Span &other) const { return start < other.start; }
};
struct VWarn {
uint32_t line;
std::string text;

View File

@@ -3,10 +3,9 @@
#include "editor/completions.h"
#include "editor/indents.h"
#include "editor/spans.h"
#include "io/knot.h"
#include "io/sysio.h"
#include "ts/decl.h"
#include "syntax/parser.h"
#include "ui/completionbox.h"
#include "ui/diagnostics.h"
#include "ui/hover.h"
@@ -35,12 +34,6 @@ struct Editor {
Coord size;
Coord scroll;
Language lang;
TSSetMain ts;
Queue<TSInputEdit> edit_queue;
std::vector<Fold> folds;
Spans spans;
Spans word_spans;
Spans hex_color_spans;
uint32_t hooks[94];
bool jumper_set;
std::shared_mutex v_mtx;
@@ -56,19 +49,17 @@ struct Editor {
std::atomic<int> lsp_version = 1;
CompletionSession completion;
IndentationEngine indents;
Parser *parser;
};
Editor *new_editor(const char *filename_arg, Coord position, Coord size);
void save_file(Editor *editor);
void free_editor(Editor *editor);
void render_editor(Editor *editor);
void fold(Editor *editor, uint32_t start_line, uint32_t end_line);
void cursor_up(Editor *editor, uint32_t number);
void cursor_down(Editor *editor, uint32_t number);
Coord move_left(Editor *editor, Coord cursor, uint32_t number);
Coord move_right(Editor *editor, Coord cursor, uint32_t number);
Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number);
Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number);
void cursor_left(Editor *editor, uint32_t number);
void cursor_right(Editor *editor, uint32_t number);
void scroll_up(Editor *editor, int32_t number);
@@ -90,9 +81,6 @@ void word_boundaries(Editor *editor, Coord coord, uint32_t *prev_col,
uint32_t *next_clusters);
void word_boundaries_exclusive(Editor *editor, Coord coord, uint32_t *prev_col,
uint32_t *next_col);
std::vector<Fold>::iterator find_fold_iter(Editor *editor, uint32_t line);
bool add_fold(Editor *editor, uint32_t start, uint32_t end);
bool remove_fold(Editor *editor, uint32_t line);
void editor_lsp_handle(Editor *editor, json msg);
void apply_lsp_edits(Editor *editor, std::vector<TextEdit> edits, bool move);
void completion_resolve_doc(Editor *editor);

View File

@@ -1,150 +0,0 @@
#ifndef EDITOR_FOLDS_H
#define EDITOR_FOLDS_H
#include "editor/editor.h"
inline std::vector<Fold>::iterator find_fold_iter(Editor *editor,
uint32_t line) {
auto &folds = editor->folds;
auto it = std::lower_bound(
folds.begin(), folds.end(), line,
[](const Fold &fold, uint32_t value) { return fold.start < value; });
if (it != folds.end() && it->start == line)
return it;
if (it != folds.begin()) {
--it;
if (it->contains(line))
return it;
}
return folds.end();
}
inline bool add_fold(Editor *editor, uint32_t start, uint32_t end) {
if (!editor || !editor->root)
return false;
if (start > end)
std::swap(start, end);
if (start >= editor->root->line_count)
return false;
end = std::min(end, editor->root->line_count - 1);
if (start == end)
return false;
Fold new_fold{start, end};
auto &folds = editor->folds;
auto it = std::lower_bound(
folds.begin(), folds.end(), new_fold.start,
[](const Fold &fold, uint32_t value) { return fold.start < value; });
if (it != folds.begin()) {
auto prev = std::prev(it);
if (prev->end + 1 >= new_fold.start) {
new_fold.start = std::min(new_fold.start, prev->start);
new_fold.end = std::max(new_fold.end, prev->end);
it = folds.erase(prev);
}
}
while (it != folds.end() && it->start <= new_fold.end + 1) {
new_fold.end = std::max(new_fold.end, it->end);
it = folds.erase(it);
}
folds.insert(it, new_fold);
return true;
}
inline bool remove_fold(Editor *editor, uint32_t line) {
auto it = find_fold_iter(editor, line);
if (it == editor->folds.end())
return false;
editor->folds.erase(it);
return true;
}
inline void apply_line_insertion(Editor *editor, uint32_t line, uint32_t rows) {
for (auto it = editor->folds.begin(); it != editor->folds.end();) {
if (line <= it->start) {
it->start += rows;
it->end += rows;
++it;
} else if (line <= it->end) {
it = editor->folds.erase(it);
} else {
++it;
}
}
}
inline void apply_line_deletion(Editor *editor, uint32_t removal_start,
uint32_t removal_end) {
if (removal_start > removal_end)
return;
uint32_t rows_removed = removal_end - removal_start + 1;
std::vector<Fold> updated;
updated.reserve(editor->folds.size());
for (auto fold : editor->folds) {
if (removal_end < fold.start) {
fold.start -= rows_removed;
fold.end -= rows_removed;
updated.push_back(fold);
continue;
}
if (removal_start > fold.end) {
updated.push_back(fold);
continue;
}
}
editor->folds.swap(updated);
}
inline const Fold *fold_for_line(const std::vector<Fold> &folds,
uint32_t line) {
auto it = std::lower_bound(
folds.begin(), folds.end(), line,
[](const Fold &fold, uint32_t value) { return fold.start < value; });
if (it != folds.end() && it->start == line)
return &(*it);
if (it != folds.begin()) {
--it;
if (it->contains(line))
return &(*it);
}
return nullptr;
}
inline Fold *fold_for_line(std::vector<Fold> &folds, uint32_t line) {
const auto *fold =
fold_for_line(static_cast<const std::vector<Fold> &>(folds), line);
return const_cast<Fold *>(fold);
}
inline bool line_is_fold_start(const std::vector<Fold> &folds, uint32_t line) {
const Fold *fold = fold_for_line(folds, line);
return fold && fold->start == line;
}
inline bool line_is_folded(const std::vector<Fold> &folds, uint32_t line) {
return fold_for_line(folds, line) != nullptr;
}
inline uint32_t next_unfolded_row(const Editor *editor, uint32_t row) {
uint32_t limit = editor && editor->root ? editor->root->line_count : 0;
while (row < limit) {
const Fold *fold = fold_for_line(editor->folds, row);
if (!fold)
return row;
row = fold->end + 1;
}
return limit;
}
inline uint32_t prev_unfolded_row(const Editor *editor, uint32_t row) {
while (row > 0) {
const Fold *fold = fold_for_line(editor->folds, row);
if (!fold)
return row;
if (fold->start == 0)
return 0;
row = fold->start - 1;
}
return 0;
}
#endif

View File

@@ -1,85 +0,0 @@
#ifndef EDITOR_SPANS_H
#define EDITOR_SPANS_H
#include "editor/decl.h"
#include "utils/utils.h"
struct Spans {
std::vector<Span> spans;
Queue<std::pair<uint32_t, int64_t>> edits;
std::atomic<bool> mid_parse = false;
std::shared_mutex mtx;
};
struct SpanCursor {
Spans &spans;
size_t index = 0;
std::vector<Span *> active;
std::shared_lock<std::shared_mutex> lock;
SpanCursor(Spans &s) : spans(s) {}
Highlight *get_highlight(uint32_t byte_offset) {
for (int i = (int)active.size() - 1; i >= 0; i--)
if (active[i]->end <= byte_offset)
active.erase(active.begin() + i);
while (index < spans.spans.size() &&
spans.spans[index].start <= byte_offset) {
if (spans.spans[index].end > byte_offset)
active.push_back(const_cast<Span *>(&spans.spans[index]));
index++;
}
Highlight *best = nullptr;
int max_prio = -1;
for (auto *s : active)
if (s->hl->priority > max_prio) {
max_prio = s->hl->priority;
best = s->hl;
}
return best;
}
void sync(uint32_t byte_offset) {
lock = std::shared_lock(spans.mtx);
active.clear();
size_t left = 0, right = spans.spans.size();
while (left < right) {
size_t mid = (left + right) / 2;
if (spans.spans[mid].start <= byte_offset)
left = mid + 1;
else
right = mid;
}
index = left;
while (left > 0) {
left--;
if (spans.spans[left].end > byte_offset)
active.push_back(const_cast<Span *>(&spans.spans[left]));
else if (byte_offset - spans.spans[left].end > 1000)
break;
}
}
};
inline void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y) {
Span key{.start = x, .end = 0, .hl = nullptr};
auto it = std::lower_bound(
spans.begin(), spans.end(), key,
[](const Span &a, const Span &b) { return a.start < b.start; });
size_t idx = std::distance(spans.begin(), it);
while (idx > 0 && spans.at(idx - 1).end >= x)
--idx;
for (size_t i = idx; i < spans.size();) {
Span &s = spans.at(i);
if (s.start < x && s.end >= x) {
s.end += y;
} else if (s.start > x) {
s.start += y;
s.end += y;
}
if (s.end <= s.start)
spans.erase(spans.begin() + i);
else
++i;
}
}
#endif

View File

@@ -91,6 +91,9 @@ Knot *erase(Knot *node, uint32_t offset, uint32_t len);
// returns a null terminated string, should be freed by the caller
char *read(Knot *root, uint32_t offset, uint32_t len);
// Used to read into an existing buffer
void read_into(Knot *node, uint32_t offset, uint32_t len, char *dest);
// Used to split the rope into left and right ropes
// node is the rope to be split (it is no longer valid after call / do not free)
// offset is the position of the split relative to the start of the rope
@@ -111,9 +114,9 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line);
// Each subsequent call returns the next line as a null terminated string
// `it` is the iterator returned from begin_l_iter
// After getting the necessary lines free the iterator (no need to go upto the
// end) returns null if there are no more lines All return strings `must` be
// freed by the caller
// After getting the necessary lines free the iterator (no need to go upto
// the end) returns null if there are no more lines All return strings
// `must` be freed by the caller
char *next_line(LineIterator *it, uint32_t *out_len);
// Returns the previous line as a null terminated string

View File

@@ -92,11 +92,6 @@ struct KeyEvent {
uint8_t mouse_modifier;
};
extern uint32_t rows, cols;
extern std::vector<ScreenCell> screen;
extern std::vector<ScreenCell> old_screen;
extern std::mutex screen_mutex;
inline bool is_empty_cell(const ScreenCell &c) {
return c.utf8.empty() || c.utf8 == " " || c.utf8 == "\x1b";
}

View File

@@ -11,7 +11,6 @@ extern "C" {
#include "libgrapheme/grapheme.h"
#include "unicode_width/unicode_width.h"
}
#include "tree-sitter/lib/include/tree_sitter/api.h"
#include <algorithm>
#include <atomic>
#include <cctype>
@@ -32,8 +31,10 @@ extern "C" {
#include <mutex>
#include <optional>
#include <queue>
#include <set>
#include <shared_mutex>
#include <signal.h>
#include <stack>
#include <string.h>
#include <string>
#include <sys/ioctl.h>

97
include/syntax/decl.h Normal file
View File

@@ -0,0 +1,97 @@
#ifndef SYNTAX_DECL_H
#define SYNTAX_DECL_H
#include "io/knot.h"
#include "io/sysio.h"
struct Trie {
struct TrieNode {
bool is_word = false;
std::array<TrieNode *, 128> children{};
TrieNode() { children.fill(nullptr); }
};
Trie() : root(new TrieNode()) {}
~Trie() { clear_trie(root); }
void build(const std::vector<std::string> &words) {
for (const auto &word : words) {
TrieNode *node = root;
for (char c : word) {
unsigned char uc = static_cast<unsigned char>(c);
if (!node->children[uc])
node->children[uc] = new TrieNode();
node = node->children[uc];
}
node->is_word = true;
}
}
uint32_t match(const char *text, uint32_t pos, uint32_t len,
bool (*is_word_char)(char c)) const {
const TrieNode *node = root;
uint32_t max_len = 0;
for (uint32_t i = pos; i < len; ++i) {
unsigned char uc = static_cast<unsigned char>(text[i]);
if (uc >= 128)
return 0;
if (!node->children[uc]) {
if (node->is_word && !is_word_char(text[i]))
return i - pos;
break;
}
node = node->children[uc];
if (node->is_word)
max_len = i - pos + 1;
}
if (max_len > 0)
if (pos + max_len < len && is_word_char(text[pos + max_len]))
return 0;
return max_len;
}
void clear() {
clear_trie(root);
root = new TrieNode();
}
private:
TrieNode *root;
void clear_trie(TrieNode *node) {
if (!node)
return;
for (auto *child : node->children)
clear_trie(child);
delete node;
}
};
struct Highlight {
uint32_t fg;
uint32_t bg;
uint8_t flags;
};
inline static const std::unordered_map<uint8_t, Highlight> highlight_map = {
{0, {0xFFFFFF, 0, 0}}, {1, {0xAAAAAA, 0, CF_ITALIC}},
{2, {0xAAD94C, 0, 0}}, {3, {0xFFFFFF, 0, CF_ITALIC}},
{4, {0xFF8F40, 0, 0}}, {5, {0xFFB454, 0, 0}},
{6, {0xD2A6FF, 0, 0}}, {7, {0x95E6CB, 0, 0}},
{8, {0xF07178, 0, 0}}, {9, {0xE6C08A, 0, 0}},
{10, {0x7dcfff, 0, 0}},
};
struct Token {
uint32_t start;
uint32_t end;
uint8_t type;
};
struct LineData {
std::shared_ptr<void> in_state{nullptr};
std::vector<Token> tokens;
std::shared_ptr<void> out_state{nullptr};
};
#endif

28
include/syntax/langs.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef SYNTAX_LANGS_H
#define SYNTAX_LANGS_H
#include "syntax/decl.h"
#define DEF_LANG(name) \
std::shared_ptr<void> name##_parse(std::vector<Token> *tokens, \
std::shared_ptr<void> in_state, \
const char *text, uint32_t len); \
bool name##_state_match(std::shared_ptr<void> state_1, \
std::shared_ptr<void> state_2);
#define LANG_A(name) {name##_parse, name##_state_match}
DEF_LANG(ruby);
inline static const std::unordered_map<
std::string,
std::tuple<std::shared_ptr<void> (*)(std::vector<Token> *tokens,
std::shared_ptr<void> in_state,
const char *text, uint32_t len),
bool (*)(std::shared_ptr<void> state_1,
std::shared_ptr<void> state_2)>>
parsers = {
{"ruby", LANG_A(ruby)},
};
#endif

212
include/syntax/line_tree.h Normal file
View File

@@ -0,0 +1,212 @@
// #include "syntax/decl.h"
//
// struct LineTree {
// void clear() {
// clear_node(root);
// root = nullptr;
// stack_size = 0;
// }
// void build(uint32_t x) { root = build_node(x); }
// LineData *at(uint32_t x) {
// LineNode *n = root;
// while (n) {
// uint32_t left_size = n->left ? n->left->size : 0;
// if (x < left_size) {
// n = n->left;
// } else if (x < left_size + n->data.size()) {
// return &n->data[x - left_size];
// } else {
// x -= left_size + n->data.size();
// n = n->right;
// }
// }
// return nullptr;
// }
// LineData *start_iter(uint32_t x) {
// stack_size = 0;
// LineNode *n = root;
// while (n) {
// uint32_t left_size = n->left ? n->left->size : 0;
// if (x < left_size) {
// push(n, 0);
// n = n->left;
// } else if (x < left_size + n->data.size()) {
// push(n, x - left_size + 1);
// return &n->data[x - left_size];
// } else {
// x -= left_size + n->data.size();
// push(n, UINT32_MAX);
// n = n->right;
// }
// }
// return nullptr;
// }
// void end_iter() { stack_size = 0; }
// LineData *next() {
// while (stack_size) {
// auto &f = stack[stack_size - 1];
// LineNode *n = f.node;
// if (f.index < n->data.size())
// return &n->data[f.index++];
// stack_size--;
// if (n->right) {
// n = n->right;
// while (n) {
// push(n, 0);
// if (!n->left)
// break;
// n = n->left;
// }
// return &stack[stack_size - 1].node->data[0];
// }
// }
// return nullptr;
// }
// void insert(uint32_t x, uint32_t y) { root = insert_node(root, x, y); }
// void erase(uint32_t x, uint32_t y) { root = erase_node(root, x, y); }
// uint32_t count() { return subtree_size(root); }
// ~LineTree() { clear(); }
//
// private:
// struct LineNode {
// LineNode *left = nullptr;
// LineNode *right = nullptr;
// uint8_t depth = 1;
// uint32_t size = 0;
// std::vector<LineData> data;
// };
// struct Frame {
// LineNode *node;
// uint32_t index;
// };
// void push(LineNode *n, uint32_t x) {
// stack[stack_size].node = n;
// stack[stack_size].index = x;
// stack_size++;
// }
// static void clear_node(LineNode *n) {
// if (!n)
// return;
// clear_node(n->left);
// clear_node(n->right);
// delete n;
// }
// LineNode *root = nullptr;
// Frame stack[32];
// uint8_t stack_size = 0;
// static constexpr uint32_t LEAF_TARGET = 256;
// LineTree::LineNode *erase_node(LineNode *n, uint32_t x, uint32_t y) {
// if (!n)
// return nullptr;
// if (!n->left && !n->right) {
// n->data.erase(n->data.begin() + x, n->data.begin() + x + y);
// fix(n);
// return n;
// }
// uint32_t left_size = subtree_size(n->left);
// if (x < left_size)
// n->left = erase_node(n->left, x, y);
// else
// n->right = erase_node(n->right, x - left_size - n->data.size(), y);
// if (n->left && n->right &&
// subtree_size(n->left) + subtree_size(n->right) < 256) {
// return merge(n->left, n->right);
// }
// return rebalance(n);
// }
// LineTree::LineNode *insert_node(LineNode *n, uint32_t x, uint32_t y) {
// if (!n) {
// auto *leaf = new LineNode();
// leaf->data.resize(y);
// leaf->size = y;
// return leaf;
// }
// if (!n->left && !n->right) {
// n->data.insert(n->data.begin() + x, y, LineData{});
// fix(n);
// if (n->data.size() > 512)
// return split_leaf(n);
// return n;
// }
// uint32_t left_size = subtree_size(n->left);
// if (x <= left_size)
// n->left = insert_node(n->left, x, y);
// else
// n->right = insert_node(n->right, x - left_size - n->data.size(), y);
// return rebalance(n);
// }
// LineNode *build_node(uint32_t count) {
// if (count <= LEAF_TARGET) {
// auto *n = new LineNode();
// n->data.resize(count);
// n->size = count;
// return n;
// }
// uint32_t left_count = count / 2;
// uint32_t right_count = count - left_count;
// auto *n = new LineNode();
// n->left = build_node(left_count);
// n->right = build_node(right_count);
// fix(n);
// return n;
// }
// static LineNode *split_leaf(LineNode *n) {
// auto *right = new LineNode();
// size_t mid = n->data.size() / 2;
// right->data.assign(n->data.begin() + mid, n->data.end());
// n->data.resize(mid);
// fix(n);
// fix(right);
// auto *parent = new LineNode();
// parent->left = n;
// parent->right = right;
// fix(parent);
// return parent;
// }
// static LineNode *merge(LineNode *a, LineNode *b) {
// a->data.insert(a->data.end(), b->data.begin(), b->data.end());
// delete b;
// fix(a);
// return a;
// }
// static void fix(LineNode *n) {
// n->depth = 1 + MAX(height(n->left), height(n->right));
// n->size = subtree_size(n->left) + n->data.size() +
// subtree_size(n->right);
// }
// static LineNode *rotate_right(LineNode *y) {
// LineNode *x = y->left;
// LineNode *T2 = x->right;
// x->right = y;
// y->left = T2;
// fix(y);
// fix(x);
// return x;
// }
// static LineNode *rotate_left(LineNode *x) {
// LineNode *y = x->right;
// LineNode *T2 = y->left;
// y->left = x;
// x->right = T2;
// fix(x);
// fix(y);
// return y;
// }
// static LineNode *rebalance(LineNode *n) {
// fix(n);
// int balance = int(height(n->left)) - int(height(n->right));
// if (balance > 1) {
// if (height(n->left->left) < height(n->left->right))
// n->left = rotate_left(n->left);
// return rotate_right(n);
// }
// if (balance < -1) {
// if (height(n->right->right) < height(n->right->left))
// n->right = rotate_right(n->right);
// return rotate_left(n);
// }
// return n;
// }
// static uint8_t height(LineNode *n) { return n ? n->depth : 0; }
// static uint32_t subtree_size(LineNode *n) { return n ? n->size : 0; }
// };

33
include/syntax/parser.h Normal file
View File

@@ -0,0 +1,33 @@
#include "syntax/decl.h"
struct Parser {
Knot *root;
std::shared_mutex *knot_mutex;
std::string lang;
std::shared_ptr<void> (*parse_func)(std::vector<Token> *tokens,
std::shared_ptr<void> in_state,
const char *text, uint32_t len);
bool (*state_match_func)(std::shared_ptr<void> state_1,
std::shared_ptr<void> state_2);
std::atomic<uint32_t> scroll_max{UINT32_MAX - 2048};
std::mutex mutex;
std::mutex data_mutex;
std::vector<LineData> line_data;
std::set<uint32_t> dirty_lines;
Parser(Knot *n_root, std::shared_mutex *n_knot_mutex, std::string n_lang,
uint32_t n_scroll_max);
void edit(Knot *n_root, uint32_t start_line, uint32_t old_end_line,
uint32_t new_end_line);
void work();
void scroll(uint32_t line);
uint8_t get_type(Coord c) {
if (c.row >= line_data.size())
return 0;
const LineData &line = line_data[c.row];
for (const Token &t : line.tokens)
if (t.start <= c.col && c.col < t.end)
return t.type;
return 0;
}
};

View File

@@ -1,76 +0,0 @@
#ifndef TS_DECL_H
#define TS_DECL_H
#include "pch.h"
#define LANG(name) tree_sitter_##name
#define TS_DEF(name) extern "C" const TSLanguage *LANG(name)()
struct Language {
std::string name = "unknown";
const TSLanguage *(*fn)() = nullptr;
uint8_t lsp_id = 0;
uint32_t color = 0xFFFFFF;
const char *symbol = "";
};
struct Highlight {
uint32_t fg;
uint32_t bg;
uint32_t flags;
uint8_t priority;
};
struct TSSetBase {
std::string lang;
TSParser *parser;
std::string query_file;
TSQuery *query;
TSTree *tree;
std::map<uint16_t, Highlight> query_map;
std::map<uint16_t, Language> injection_map;
const TSLanguage *language;
};
struct TSSet : TSSetBase {
std::vector<TSRange> ranges;
};
struct TSSetMain : TSSetBase {
std::unordered_map<std::string, TSSet> injections;
};
TS_DEF(ruby);
TS_DEF(bash);
TS_DEF(cpp);
TS_DEF(css);
TS_DEF(fish);
TS_DEF(go);
TS_DEF(haskell);
TS_DEF(html);
TS_DEF(javascript);
TS_DEF(tsx);
TS_DEF(man);
TS_DEF(json);
TS_DEF(lua);
TS_DEF(regex);
TS_DEF(query);
TS_DEF(markdown);
TS_DEF(markdown_inline);
TS_DEF(embedded_template);
TS_DEF(php);
TS_DEF(python);
TS_DEF(rust);
TS_DEF(sql);
TS_DEF(gitattributes);
TS_DEF(gitignore);
TS_DEF(gomod);
TS_DEF(nginx);
TS_DEF(toml);
TS_DEF(yaml);
TS_DEF(ini);
TS_DEF(diff);
TS_DEF(make);
TS_DEF(gdscript);
#endif

View File

@@ -1,20 +0,0 @@
#ifndef TS_H
#define TS_H
#include "editor/editor.h"
#include "pch.h"
#include "utils/utils.h"
#define HEX(s) (static_cast<uint32_t>(std::stoul(s, nullptr, 16)))
extern std::unordered_map<std::string, pcre2_code *> regex_cache;
TSQuery *load_query(const char *query_path, TSSetBase *set);
void ts_collect_spans(Editor *editor);
bool ts_predicate(
TSQuery *query, const TSQueryMatch &match,
std::function<char *(const TSNode *, uint32_t *len, bool *allocated)>
subject_fn);
void clear_regex_cache();
#endif

View File

@@ -4,7 +4,6 @@
#include "editor/decl.h"
#include "io/sysio.h"
#include "pch.h"
#include "ts/decl.h"
#include "utils/utils.h"
struct HoverBox {
@@ -13,8 +12,6 @@ struct HoverBox {
uint32_t scroll_;
std::vector<ScreenCell> cells;
Coord size;
std::vector<Highlight> highlights;
std::vector<Span> hover_spans;
void clear();
void scroll(int32_t number);

View File

@@ -2,7 +2,6 @@
#define UTILS_H
#include "pch.h"
#include "ts/decl.h"
template <typename T> struct Queue {
std::queue<T> q;
@@ -59,6 +58,13 @@ struct Match {
std::string text;
};
struct Language {
std::string name;
uint8_t lsp_id;
uint32_t color;
const char *symbol;
};
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(x) ((x) < 0 ? -(x) : (x))
@@ -80,6 +86,7 @@ uint32_t get_bytes_from_visual_col(const char *line, uint32_t len,
uint32_t target_visual_col);
uint32_t utf8_byte_offset_to_utf16(const char *s, uint32_t byte_pos);
uint32_t utf16_offset_to_utf8(const char *s, uint32_t utf16_pos);
uint8_t utf8_codepoint_width(unsigned char c);
void log(const char *fmt, ...);