diff --git a/include/editor.h b/include/editor.h index ad80229..ce2a80a 100644 --- a/include/editor.h +++ b/include/editor.h @@ -29,7 +29,7 @@ struct SpanCursor { SpanCursor(const std::vector &s) : spans(s) {} 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) active.erase(active.begin() + i); while (index < spans.size() && spans[index].start <= byte_offset) { @@ -69,28 +69,27 @@ struct SpanCursor { }; struct Editor { - const char *filename; // Filename of the editor - Knot *root; // A rope - std::shared_mutex knot_mtx; // A mutex - std::shared_mutex span_mtx; // A mutex - Coord cursor; // position of the cursor - uint32_t cursor_preffered; // preffered visual column - Coord selection; // position of the selection - bool selection_active; // true if there is a selection - Coord position; // Position of the editor - Coord size; // Size of the editor - Coord scroll; // Position of the scroll - TSTree *tree; // Tree-sitter tree - TSParser *parser; // Tree-sitter parser - TSQuery *query; // Tree-sitter query - const TSLanguage *language; // Tree-sitter language - Queue edit_queue; // Tree-sitter edit queue - std::vector query_map; // Tree-sitter query map - int *folded; // folded lines indexed by line number - cached form of - // folded_node - std::vector spans; + const char *filename; // Filename of the editor + Knot *root; // A rope + std::shared_mutex knot_mtx; // A mutex + std::shared_mutex span_mtx; // A mutex + Coord cursor; // position of the cursor + uint32_t cursor_preffered; // preffered visual column + Coord selection; // position of the selection + bool selection_active; // true if there is a selection + Coord position; // Position of the editor + Coord size; // Size of the editor + Coord scroll; // Position of the scroll + TSTree *tree; // Tree-sitter tree + TSParser *parser; // Tree-sitter parser + TSQuery *query; // Tree-sitter query + const TSLanguage *language; // Tree-sitter language + Queue edit_queue; // Tree-sitter edit queue + std::vector query_map; // Tree-sitter query map + std::vector folded; // folded lines indexed by line number + std::vector spans; // Highlighted spans std::map folded_node; // maps content hash to fold state - // - built by tree-sitter + // - built by tree-sitter helpers }; typedef struct TSLoad { diff --git a/src/editor.cc b/src/editor.cc index 138f567..ecfb30c 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -44,7 +44,7 @@ Editor *new_editor(const char *filename, Coord position, Coord size) { editor->selection = {0, 0}; editor->scroll = {0, 0}; 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"; if (!(len > (1024 * 1024))) { editor->parser = ts_parser_new(); @@ -57,8 +57,6 @@ Editor *new_editor(const char *filename, Coord position, Coord size) { } void free_editor(Editor *editor) { - if (editor->folded) - free(editor->folded); ts_parser_delete(editor->parser); if (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; break; }; - if (editor->folded && editor->folded[editor->cursor.row] != 0) + if (editor->folded[editor->cursor.row] != 0) number++; } while (--number > 0); free(it); @@ -219,7 +217,7 @@ void cursor_up(Editor *editor, uint32_t number) { free(line_content); while (number > 0 && editor->cursor.row > 0) { editor->cursor.row--; - if (editor->folded && editor->folded[editor->cursor.row] != 0) + if (editor->folded[editor->cursor.row] != 0) continue; number--; } @@ -251,7 +249,7 @@ void cursor_right(Editor *editor, uint32_t number) { free(line); line = nullptr; 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) next_row++; if (next_row >= editor->root->line_count) { @@ -299,7 +297,7 @@ void cursor_left(Editor *editor, uint32_t number) { if (editor->cursor.row == 0) break; 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--; if (prev_row < 0) break; @@ -363,11 +361,6 @@ void ensure_scroll(Editor *editor) { void fold(Editor *editor, uint32_t start_line, uint32_t end_line) { if (!editor) 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++) editor->folded[i] = 1; 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); span_cursor.sync(global_byte_offset); while (rendered_rows < screen_rows) { - if (editor->folded && editor->folded[line_index]) { + if (editor->folded[line_index]) { if (editor->folded[line_index] == 2) { update_render_fold_marker(rendered_rows, screen_cols); rendered_rows++; diff --git a/src/main.cc b/src/main.cc index fa8c224..8b22ce0 100644 --- a/src/main.cc +++ b/src/main.cc @@ -105,7 +105,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) { lock_1.unlock(); std::unique_lock lock_2(editor->knot_mtx); 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(); if (editor->tree) { TSInputEdit edit = {