Switch folds from c-style array to std::vector

This commit is contained in:
2025-12-07 18:18:54 +00:00
parent 7c94df1040
commit 2c3f8f2e46
3 changed files with 28 additions and 36 deletions

View File

@@ -29,7 +29,7 @@ struct SpanCursor {
SpanCursor(const std::vector<Span> &s) : spans(s) {} SpanCursor(const std::vector<Span> &s) : spans(s) {}
Highlight *get_highlight(uint32_t byte_offset) { Highlight *get_highlight(uint32_t byte_offset) {
for (int i = active.size() - 1; i >= 0; i--) for (int i = (int)active.size() - 1; i >= 0; i--)
if (active[i]->end <= byte_offset) if (active[i]->end <= byte_offset)
active.erase(active.begin() + i); active.erase(active.begin() + i);
while (index < spans.size() && spans[index].start <= byte_offset) { while (index < spans.size() && spans[index].start <= byte_offset) {
@@ -69,28 +69,27 @@ struct SpanCursor {
}; };
struct Editor { struct Editor {
const char *filename; // Filename of the editor const char *filename; // Filename of the editor
Knot *root; // A rope Knot *root; // A rope
std::shared_mutex knot_mtx; // A mutex std::shared_mutex knot_mtx; // A mutex
std::shared_mutex span_mtx; // A mutex std::shared_mutex span_mtx; // A mutex
Coord cursor; // position of the cursor Coord cursor; // position of the cursor
uint32_t cursor_preffered; // preffered visual column uint32_t cursor_preffered; // preffered visual column
Coord selection; // position of the selection Coord selection; // position of the selection
bool selection_active; // true if there is a selection bool selection_active; // true if there is a selection
Coord position; // Position of the editor Coord position; // Position of the editor
Coord size; // Size of the editor Coord size; // Size of the editor
Coord scroll; // Position of the scroll Coord scroll; // Position of the scroll
TSTree *tree; // Tree-sitter tree TSTree *tree; // Tree-sitter tree
TSParser *parser; // Tree-sitter parser TSParser *parser; // Tree-sitter parser
TSQuery *query; // Tree-sitter query TSQuery *query; // Tree-sitter query
const TSLanguage *language; // Tree-sitter language const TSLanguage *language; // Tree-sitter language
Queue<TSInputEdit> edit_queue; // Tree-sitter edit queue Queue<TSInputEdit> edit_queue; // Tree-sitter edit queue
std::vector<Highlight> query_map; // Tree-sitter query map std::vector<Highlight> query_map; // Tree-sitter query map
int *folded; // folded lines indexed by line number - cached form of std::vector<int8_t> folded; // folded lines indexed by line number
// folded_node std::vector<Span> spans; // Highlighted spans
std::vector<Span> spans;
std::map<uint32_t, bool> folded_node; // maps content hash to fold state std::map<uint32_t, bool> folded_node; // maps content hash to fold state
// - built by tree-sitter // - built by tree-sitter helpers
}; };
typedef struct TSLoad { typedef struct TSLoad {

View File

@@ -44,7 +44,7 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
editor->selection = {0, 0}; editor->selection = {0, 0};
editor->scroll = {0, 0}; editor->scroll = {0, 0};
editor->root = load(str, len, optimal_chunk_size(len)); editor->root = load(str, len, optimal_chunk_size(len));
editor->folded = (int *)calloc(editor->root->line_count + 2, sizeof(int)); editor->folded.resize(editor->root->line_count + 2);
std::string query = get_exe_dir() + "/../grammar/ruby.scm"; std::string query = get_exe_dir() + "/../grammar/ruby.scm";
if (!(len > (1024 * 1024))) { if (!(len > (1024 * 1024))) {
editor->parser = ts_parser_new(); editor->parser = ts_parser_new();
@@ -57,8 +57,6 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
} }
void free_editor(Editor *editor) { void free_editor(Editor *editor) {
if (editor->folded)
free(editor->folded);
ts_parser_delete(editor->parser); ts_parser_delete(editor->parser);
if (editor->tree) if (editor->tree)
ts_tree_delete(editor->tree); ts_tree_delete(editor->tree);
@@ -195,7 +193,7 @@ void cursor_down(Editor *editor, uint32_t number) {
editor->cursor.row = editor->root->line_count - 1; editor->cursor.row = editor->root->line_count - 1;
break; break;
}; };
if (editor->folded && editor->folded[editor->cursor.row] != 0) if (editor->folded[editor->cursor.row] != 0)
number++; number++;
} while (--number > 0); } while (--number > 0);
free(it); free(it);
@@ -219,7 +217,7 @@ void cursor_up(Editor *editor, uint32_t number) {
free(line_content); free(line_content);
while (number > 0 && editor->cursor.row > 0) { while (number > 0 && editor->cursor.row > 0) {
editor->cursor.row--; editor->cursor.row--;
if (editor->folded && editor->folded[editor->cursor.row] != 0) if (editor->folded[editor->cursor.row] != 0)
continue; continue;
number--; number--;
} }
@@ -251,7 +249,7 @@ void cursor_right(Editor *editor, uint32_t number) {
free(line); free(line);
line = nullptr; line = nullptr;
uint32_t next_row = editor->cursor.row + 1; uint32_t next_row = editor->cursor.row + 1;
while (editor->folded && next_row < editor->root->line_count && while (next_row < editor->root->line_count &&
editor->folded[next_row] != 0) editor->folded[next_row] != 0)
next_row++; next_row++;
if (next_row >= editor->root->line_count) { if (next_row >= editor->root->line_count) {
@@ -299,7 +297,7 @@ void cursor_left(Editor *editor, uint32_t number) {
if (editor->cursor.row == 0) if (editor->cursor.row == 0)
break; break;
int32_t prev_row = editor->cursor.row - 1; int32_t prev_row = editor->cursor.row - 1;
while (editor->folded && prev_row >= 0 && editor->folded[prev_row] != 0) while (prev_row >= 0 && editor->folded[prev_row] != 0)
prev_row--; prev_row--;
if (prev_row < 0) if (prev_row < 0)
break; break;
@@ -363,11 +361,6 @@ void ensure_scroll(Editor *editor) {
void fold(Editor *editor, uint32_t start_line, uint32_t end_line) { void fold(Editor *editor, uint32_t start_line, uint32_t end_line) {
if (!editor) if (!editor)
return; return;
if (!editor->folded) {
editor->folded = (int *)calloc(editor->root->line_count + 2, sizeof(int));
if (!editor->folded)
return;
}
for (uint32_t i = start_line; i <= end_line && i < editor->size.row; i++) for (uint32_t i = start_line; i <= end_line && i < editor->size.row; i++)
editor->folded[i] = 1; editor->folded[i] = 1;
editor->folded[start_line] = 2; editor->folded[start_line] = 2;
@@ -397,7 +390,7 @@ void render_editor(Editor *editor) {
uint32_t global_byte_offset = line_to_byte(editor->root, line_index, nullptr); uint32_t global_byte_offset = line_to_byte(editor->root, line_index, nullptr);
span_cursor.sync(global_byte_offset); span_cursor.sync(global_byte_offset);
while (rendered_rows < screen_rows) { while (rendered_rows < screen_rows) {
if (editor->folded && editor->folded[line_index]) { if (editor->folded[line_index]) {
if (editor->folded[line_index] == 2) { if (editor->folded[line_index] == 2) {
update_render_fold_marker(rendered_rows, screen_cols); update_render_fold_marker(rendered_rows, screen_cols);
rendered_rows++; rendered_rows++;

View File

@@ -105,7 +105,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
lock_1.unlock(); lock_1.unlock();
std::unique_lock lock_2(editor->knot_mtx); std::unique_lock lock_2(editor->knot_mtx);
editor->root = insert(editor->root, pos, (char *)"\n", 1); editor->root = insert(editor->root, pos, (char *)"\n", 1);
editor->folded = (int *)calloc(editor->root->line_count + 2, sizeof(int)); editor->folded.resize(editor->root->line_count + 2);
lock_2.unlock(); lock_2.unlock();
if (editor->tree) { if (editor->tree) {
TSInputEdit edit = { TSInputEdit edit = {