Allow dynamic theming and improve ruby parser

This commit is contained in:
2026-01-18 13:00:41 +00:00
parent 1fda5bf246
commit d0e811904c
18 changed files with 1029 additions and 496 deletions

View File

@@ -73,19 +73,76 @@ struct Highlight {
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}},
enum struct TokenKind : uint8_t {
#define ADD(name) name,
#include "syntax/tokens.def"
#undef ADD
Count
};
constexpr size_t TOKEN_KIND_COUNT = static_cast<size_t>(TokenKind::Count);
const std::unordered_map<std::string, TokenKind> kind_map = {
#define ADD(name) {#name, TokenKind::name},
#include "syntax/tokens.def"
#undef ADD
};
extern std::array<Highlight, TOKEN_KIND_COUNT> highlights;
inline void load_theme(std::string filename) {
uint32_t len = 0;
char *raw = load_file(filename.c_str(), &len);
if (!raw)
return;
std::string data(raw, len);
free(raw);
json j = json::parse(data);
Highlight default_hl = {0xFFFFFF, 0, 0};
if (j.contains("Default")) {
auto def = j["Default"];
if (def.contains("fg") && def["fg"].is_string())
default_hl.fg = HEX(def["fg"]);
if (def.contains("bg") && def["bg"].is_string())
default_hl.bg = HEX(def["bg"]);
if (def.contains("italic") && def["italic"].get<bool>())
default_hl.flags |= CF_ITALIC;
if (def.contains("bold") && def["bold"].get<bool>())
default_hl.flags |= CF_BOLD;
if (def.contains("underline") && def["underline"].get<bool>())
default_hl.flags |= CF_UNDERLINE;
if (def.contains("strikethrough") && def["strikethrough"].get<bool>())
default_hl.flags |= CF_STRIKETHROUGH;
}
for (auto &hl : highlights)
hl = default_hl;
for (auto &[key, value] : j.items()) {
if (key == "Default")
continue;
auto it = kind_map.find(key);
if (it == kind_map.end())
continue;
Highlight hl = {0xFFFFFF, 0, 0};
if (value.contains("fg") && value["fg"].is_string())
hl.fg = HEX(value["fg"]);
if (value.contains("bg") && value["bg"].is_string())
hl.bg = HEX(value["bg"]);
if (value.contains("italic") && value["italic"].get<bool>())
hl.flags |= CF_ITALIC;
if (value.contains("bold") && value["bold"].get<bool>())
hl.flags |= CF_BOLD;
if (value.contains("underline") && value["underline"].get<bool>())
hl.flags |= CF_UNDERLINE;
if (value.contains("strikethrough") && value["strikethrough"].get<bool>())
hl.flags |= CF_STRIKETHROUGH;
highlights[static_cast<uint8_t>(it->second)] = hl;
}
}
struct Token {
uint32_t start;
uint32_t end;
uint8_t type;
TokenKind type;
};
struct LineData {

View File

@@ -10,9 +10,27 @@
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}
#define LANG_A(name) \
{ \
#name, { name##_parse, name##_state_match } \
}
template <typename T>
inline std::shared_ptr<T> ensure_state(std::shared_ptr<T> state) {
using U = typename T::full_state_type;
if (!state)
state = std::make_shared<T>();
if (!state.unique())
state = std::make_shared<T>(*state);
if (!state->full_state)
state->full_state = std::make_shared<U>();
else if (!state->full_state.unique())
state->full_state = std::make_shared<U>(*state->full_state);
return state;
}
DEF_LANG(ruby);
DEF_LANG(bash);
inline static const std::unordered_map<
std::string,
@@ -22,7 +40,8 @@ inline static const std::unordered_map<
bool (*)(std::shared_ptr<void> state_1,
std::shared_ptr<void> state_2)>>
parsers = {
{"ruby", LANG_A(ruby)},
LANG_A(ruby),
LANG_A(bash),
};
#endif

View File

@@ -1,212 +1,233 @@
// #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; }
// };
#ifndef LINE_TREE_H
#define LINE_TREE_H
#include "syntax/decl.h"
struct LineTree {
void clear() {
std::unique_lock lock(mtx);
clear_node(root);
root = nullptr;
stack_size = 0;
}
void build(uint32_t x) {
std::unique_lock lock(mtx);
root = build_node(x);
}
LineData *at(uint32_t x) {
std::shared_lock lock(mtx);
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) {
std::shared_lock lock(mtx);
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() {
std::shared_lock lock(mtx);
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) {
std::unique_lock lock(mtx);
root = insert_node(root, x, y);
}
void erase(uint32_t x, uint32_t y) {
std::unique_lock lock(mtx);
root = erase_node(root, x, y);
}
uint32_t count() {
std::shared_lock lock(mtx);
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];
std::atomic<uint8_t> stack_size = 0;
std::shared_mutex mtx;
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; }
};
#endif

View File

@@ -1,4 +1,8 @@
#ifndef SYNTAX_PARSER_H
#define SYNTAX_PARSER_H
#include "syntax/decl.h"
#include "syntax/line_tree.h"
struct Parser {
Knot *root;
@@ -12,7 +16,7 @@ struct Parser {
std::atomic<uint32_t> scroll_max{UINT32_MAX - 2048};
std::mutex mutex;
std::mutex data_mutex;
std::vector<LineData> line_data;
LineTree line_tree;
std::set<uint32_t> dirty_lines;
Parser(Knot *n_root, std::shared_mutex *n_knot_mutex, std::string n_lang,
@@ -21,13 +25,6 @@ struct Parser {
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;
}
};
#endif

51
include/syntax/tokens.def Normal file
View File

@@ -0,0 +1,51 @@
ADD(Data)
ADD(Comment)
ADD(String)
ADD(Escape)
ADD(Interpolation)
ADD(Regexp)
ADD(Number)
ADD(True)
ADD(False)
ADD(Char)
ADD(Keyword)
ADD(KeywordOperator)
ADD(Operator)
ADD(Function)
ADD(Type)
ADD(Constant)
ADD(VariableInstance)
ADD(VariableGlobal)
ADD(Annotation)
ADD(Directive)
ADD(Label)
ADD(Brace1)
ADD(Brace2)
ADD(Brace3)
ADD(Brace4)
ADD(Brace5)
ADD(Heading1)
ADD(Heading2)
ADD(Heading3)
ADD(Heading4)
ADD(Heading5)
ADD(Heading6)
ADD(Blockquote)
ADD(List)
ADD(ListItem)
ADD(Code)
ADD(LanguageName)
ADD(LinkLabel)
ADD(ImageLabel)
ADD(Link)
ADD(Table)
ADD(TableHeader)
ADD(Italic)
ADD(Bold)
ADD(Underline)
ADD(Strikethrough)
ADD(HorixontalRule)
ADD(Tag)
ADD(Attribute)
ADD(CheckDone)
ADD(CheckNotDone)