Make syntax highlighting smoother

This commit is contained in:
2025-12-30 10:56:31 +00:00
parent 26e0b06e24
commit dc507dfc23
13 changed files with 216 additions and 230 deletions

View File

@@ -51,7 +51,6 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
apply_hook_deletion(editor, start_row + 1, end_row);
std::unique_lock lock_2(editor->knot_mtx);
editor->root = erase(editor->root, start, byte_pos - start);
lock_2.unlock();
if (editor->ts.tree) {
TSInputEdit edit = {
.start_byte = start,
@@ -63,6 +62,15 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
};
editor->edit_queue.push(edit);
}
std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, start, start - byte_pos);
if (editor->spans.mid_parse)
editor->spans.edits.push({start, start - byte_pos});
lock_3.unlock();
lock_2.unlock();
std::unique_lock lock_4(editor->hex_color_spans.mtx);
apply_edit(editor->hex_color_spans.spans, byte_pos, start - byte_pos);
lock_4.unlock();
if (do_lsp) {
if (editor->lsp->incremental_sync) {
json message = {
@@ -88,12 +96,6 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
lsp_send(editor->lsp, message, nullptr);
}
}
std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, start, start - byte_pos);
if (editor->spans.mid_parse)
editor->spans.edits.push({start, start - byte_pos});
std::unique_lock lock_4(editor->def_spans.mtx);
apply_edit(editor->def_spans.spans, byte_pos, start - byte_pos);
} else {
std::shared_lock lock_1(editor->knot_mtx);
uint32_t cursor_original =
@@ -140,7 +142,6 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
apply_hook_deletion(editor, start_row + 1, end_row);
std::unique_lock lock_2(editor->knot_mtx);
editor->root = erase(editor->root, byte_pos, end - byte_pos);
lock_2.unlock();
if (editor->ts.tree) {
TSInputEdit edit = {
.start_byte = byte_pos,
@@ -152,6 +153,15 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
};
editor->edit_queue.push(edit);
}
std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, byte_pos, byte_pos - end);
if (editor->spans.mid_parse)
editor->spans.edits.push({byte_pos, byte_pos - end});
lock_3.unlock();
lock_2.unlock();
std::unique_lock lock_4(editor->hex_color_spans.mtx);
apply_edit(editor->hex_color_spans.spans, byte_pos, byte_pos - end);
lock_4.unlock();
if (do_lsp) {
if (editor->lsp->incremental_sync) {
json message = {
@@ -177,12 +187,6 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
lsp_send(editor->lsp, message, nullptr);
}
}
std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, byte_pos, byte_pos - end);
if (editor->spans.mid_parse)
editor->spans.edits.push({byte_pos, byte_pos - end});
std::unique_lock lock_4(editor->def_spans.mtx);
apply_edit(editor->def_spans.spans, byte_pos, byte_pos - end);
}
}
@@ -202,7 +206,6 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
lock_1.unlock();
std::unique_lock lock_2(editor->knot_mtx);
editor->root = insert(editor->root, byte_pos, data, len);
lock_2.unlock();
uint32_t cols = 0;
uint32_t rows = 0;
for (uint32_t i = 0; i < len; i++) {
@@ -227,6 +230,15 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
};
editor->edit_queue.push(edit);
}
std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, byte_pos, len);
if (editor->spans.mid_parse)
editor->spans.edits.push({byte_pos, len});
lock_3.unlock();
lock_2.unlock();
std::unique_lock lock_4(editor->hex_color_spans.mtx);
apply_edit(editor->hex_color_spans.spans, byte_pos, len);
lock_4.unlock();
if (editor->lsp) {
if (editor->lsp->incremental_sync) {
lock_1.lock();
@@ -265,10 +277,4 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
lsp_send(editor->lsp, message, nullptr);
}
}
std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, byte_pos, len);
if (editor->spans.mid_parse)
editor->spans.edits.push({byte_pos, len});
std::unique_lock lock_4(editor->def_spans.mtx);
apply_edit(editor->def_spans.spans, byte_pos, len);
}

View File

@@ -33,8 +33,9 @@ Editor *new_editor(const char *filename_arg, Coord position, Coord size) {
ts_parser_set_language(editor->ts.parser, editor->ts.language);
editor->ts.query_file =
get_exe_dir() + "/../grammar/" + language.name + ".scm";
request_add_to_lsp(language, editor);
}
if (len <= (1024 * 28))
request_add_to_lsp(language, editor);
return editor;
}

View File

@@ -1,6 +1,7 @@
#include "editor/editor.h"
#include "editor/folds.h"
#include "main.h"
#include "ts/decl.h"
void render_editor(Editor *editor) {
uint32_t sel_start = 0, sel_end = 0;
@@ -73,14 +74,16 @@ void render_editor(Editor *editor) {
Coord cursor = {UINT32_MAX, UINT32_MAX};
uint32_t line_index = editor->scroll.row;
SpanCursor span_cursor(editor->spans);
SpanCursor def_span_cursor(editor->def_spans);
SpanCursor word_span_cursor(editor->word_spans);
SpanCursor hex_span_cursor(editor->hex_color_spans);
LineIterator *it = begin_l_iter(editor->root, line_index);
if (!it)
return;
uint32_t rendered_rows = 0;
uint32_t global_byte_offset = line_to_byte(editor->root, line_index, nullptr);
span_cursor.sync(global_byte_offset);
def_span_cursor.sync(global_byte_offset);
word_span_cursor.sync(global_byte_offset);
hex_span_cursor.sync(global_byte_offset);
while (rendered_rows < editor->size.row) {
const Fold *fold = fold_for_line(editor->folds, line_index);
if (fold) {
@@ -180,16 +183,23 @@ void render_editor(Editor *editor) {
uint32_t absolute_byte_pos =
global_byte_offset + current_byte_offset + local_render_offset;
Highlight *hl = span_cursor.get_highlight(absolute_byte_pos);
Highlight *def_hl = def_span_cursor.get_highlight(absolute_byte_pos);
Highlight *word_hl = word_span_cursor.get_highlight(absolute_byte_pos);
Highlight *hex_hl = hex_span_cursor.get_highlight(absolute_byte_pos);
uint32_t fg = hl ? hl->fg : 0xFFFFFF;
uint32_t bg = hl ? hl->bg : 0;
uint8_t fl = hl ? hl->flags : 0;
if (def_hl) {
if (def_hl->fg != 0)
fg = def_hl->fg;
if (def_hl->bg != 0)
bg = def_hl->bg;
fl |= def_hl->flags;
if (hex_hl) {
if (hex_hl->fg != 0)
fg = hex_hl->fg;
if (hex_hl->bg != 0)
bg = hex_hl->bg;
fl |= hex_hl->flags;
} else if (word_hl) {
if (word_hl->fg != 0)
fg |= word_hl->fg;
if (word_hl->bg != 0)
bg |= word_hl->bg;
fl |= word_hl->flags;
}
if (editor->selection_active && absolute_byte_pos >= sel_start &&
absolute_byte_pos < sel_end)

View File

@@ -30,16 +30,19 @@ void hover_diagnostic(Editor *editor) {
void editor_worker(Editor *editor) {
if (!editor || !editor->root)
return;
if (editor->root->char_count > (1024 * 200))
if (editor->root->char_count > (1024 * 128))
return;
if (editor->ts.query_file != "" && !editor->ts.query)
editor->ts.query = load_query(editor->ts.query_file.c_str(), &editor->ts);
if (editor->ts.parser && editor->ts.query)
ts_collect_spans(editor);
if (editor->root->char_count > (1024 * 32))
return;
uint32_t prev_col, next_col;
word_boundaries_exclusive(editor, editor->cursor, &prev_col, &next_col);
std::unique_lock lock(editor->def_spans.mtx);
editor->def_spans.spans.clear();
std::unique_lock lock(editor->word_spans.mtx);
editor->word_spans.spans.clear();
lock.unlock();
if (next_col - prev_col > 0 && next_col - prev_col < 256 - 4) {
std::shared_lock lockk(editor->knot_mtx);
uint32_t offset = line_to_byte(editor->root, editor->cursor.row, nullptr);
@@ -48,43 +51,56 @@ void editor_worker(Editor *editor) {
if (word) {
char buf[256];
snprintf(buf, sizeof(buf), "\\b%s\\b", word);
std::shared_lock lockk(editor->knot_mtx);
std::vector<std::pair<size_t, size_t>> results =
search_rope(editor->root, buf);
search_rope_dfa(editor->root, buf);
lockk.unlock();
std::unique_lock lock2(editor->word_spans.mtx);
editor->word_spans.spans.reserve(results.size());
for (const auto &match : results) {
Span s;
s.start = match.first;
s.end = match.first + match.second;
s.hl = &HL_UNDERLINE;
editor->def_spans.spans.push_back(s);
editor->word_spans.spans.push_back(s);
}
free(word);
lock2.unlock();
}
}
uint8_t top = 0;
static Highlight *hl_s = (Highlight *)calloc(200, sizeof(Highlight));
static uint16_t limit = 150;
static Highlight *hl_s = (Highlight *)calloc(limit, sizeof(Highlight));
if (!hl_s)
exit(ENOMEM);
std::shared_lock lockk(editor->knot_mtx);
std::vector<std::pair<size_t, size_t>> results =
search_rope(editor->root, "(0x|#)[0-9a-fA-F]{6}");
for (int i = 0; i < results.size() && top < 200; i++) {
std::vector<Match> results =
search_rope(editor->root, "(?:0x|#)[0-9a-fA-F]{6}");
if (results.size() > limit) {
limit = results.size() + 50;
free(hl_s);
hl_s = (Highlight *)calloc(limit, sizeof(Highlight));
if (!hl_s)
exit(ENOMEM);
}
lockk.unlock();
std::unique_lock lock2(editor->hex_color_spans.mtx);
editor->hex_color_spans.spans.clear();
editor->hex_color_spans.spans.reserve(results.size());
for (size_t i = 0; i < results.size(); ++i) {
Span s;
s.start = results[i].first;
s.end = results[i].first + results[i].second;
char *buf = read(editor->root, s.start, s.end - s.start);
int x = buf[0] == '#' ? 1 : 2;
uint32_t bg = HEX(buf + x);
free(buf);
uint8_t r = bg >> 16, g = (bg >> 8) & 0xFF, b = bg & 0xFF;
s.start = results[i].start;
s.end = results[i].end;
int x = results[i].text[0] == '#' ? 1 : 2;
uint32_t bg = HEX(results[i].text.substr(x));
uint8_t r = bg >> 16;
uint8_t g = (bg >> 8) & 0xFF;
uint8_t b = bg & 0xFF;
double luminance = 0.299 * r + 0.587 * g + 0.114 * b;
uint32_t fg = (luminance > 128) ? 0x010101 : 0xFEFEFE;
hl_s[top] = {fg, bg, CF_BOLD, UINT8_MAX};
s.hl = &hl_s[top];
editor->def_spans.spans.push_back(s);
top++;
hl_s[i] = {fg, bg, CF_BOLD, UINT8_MAX};
s.hl = &hl_s[i];
editor->hex_color_spans.spans.push_back(s);
}
std::sort(editor->def_spans.spans.begin(), editor->def_spans.spans.end());
lock.unlock();
lockk.unlock();
lock2.unlock();
hover_diagnostic(editor);
}