Compare commits

...

2 Commits

Author SHA1 Message Date
7307387f64 Optimize line iterator 2025-12-19 20:46:52 +00:00
8002012705 Update todo list 2025-12-19 15:00:16 +00:00
8 changed files with 176 additions and 141 deletions

View File

@@ -6,28 +6,55 @@ A TUI IDE.
# TODO # TODO
- [ ] Add tab/untab with [,<] and [.>]
- [ ] Add something that works like `p` but doesnt move cursor.
- [ ] Fix the fact that hooks arent updated when a line is deleted/added.
- [ ] also use this sorta rules apart from previous lines indentation for the indentor.
- if previous line ends with { or similar → indent +1
- (i.e. just check the last line that is not string or comment or empty)
- language-specific rules later if you want
- ignore comments that dedent etc. - anything else should be handled by LSP.
- [ ] Fix bug where clicking outside eof sets to top.
- [ ] Add this thing where select at end of screen scrolls down. (and vice versa)
- [ ] Add a virtual text support (text that is rendered in the editor but not in the actual text)
- Add a whitespace highlighter (nerd font). for spaces and tabs at start/end of line.
- [ ] Add support for LSP & autocomplete / snippets.
- First research
- `textDocument/documentHighlight` - for highlighting stuff (probably tree-sitter is enough)
- `textDocument/selectionRange` //
- `textDocument/completion` - Obviously
- `textDocument/onTypeFormatting` - seems promising for auto formatting (indentation etc)
- `textDocument/inlayHint` & `textDocument/inlineHint` & `textDocument/codeLens`
- `textDocument/formatting` & `textDocument/rangeFormatting`
- `textDocument/semanticTokens/*` (probably tree-sitter is enough)
- `textDocument/linkedEditingRange` - probably useful
- `textDocument/foldingRange` - i will never use this for folding but it might be useful for other things.
- `textDocument/rename` & `textDocument/prepareRename` - probably useful
- And a lot more (just go through each for `clangd` and then expand to say `solargraph`).
- Make a universal plug for lsp. So focus more on making a general purpose solid communication interface. Instead of something specific.
- With a 4ish pass system. (more like each returned value from the lsp is used in 4 ways)
1. One for stuff like jump to x position. or rename symbol x to y. (stuff that explicitly requires user request to do something)
- Maybe even hover goes here
2. One for stuff that only affects highlighting and styles . like symbol highlighting etc.
3. One for Warnings/errors and inlay hints etc. (stuff that adds virtual text to the editor)
4. One for fromatting and stuff like that. (stuff that edits the buffer text)
- [ ] Add snippets from wherever i get them. (like luasnip or vsnip)
- [ ] Add support for virtual cursor where edits apply at all the places. - [ ] Add support for virtual cursor where edits apply at all the places.
- [ ] Add alt + click to set multiple cursors. - [ ] Add alt + click to set multiple cursors.
- [ ] Add search / replace along with search / virtual cursors are searched pos. - [ ] Add search / replace along with search / virtual cursors are searched pos.
- [ ] Add support for undo/redo. - [ ] Add support for undo/redo.
- [ ] Add `.scm` files for all the supported languages. (2/14) Done. - [ ] Add `.scm` files for all the supported languages. (2/14) Done.
- [ ] Add splash screen / minigame jumping. - [ ] Add splash screen / minigame jumping.
- [ ] Add support for LSP & autocomplete / snippets.
- [ ] Add codeium/copilot support. - [ ] Add codeium/copilot support.
- [ ] Normalize / validate unicode on file open. - [ ] Normalize / validate unicode on file open.
- [ ] Add git stuff. - [ ] Add git stuff.
- [ ] Add SQL support. (viewer and basic editor)
- [ ] Add color picker/palette for hex or other css colors.
- [ ] Fix bug where alt+up at eof adds extra line. - [ ] Fix bug where alt+up at eof adds extra line.
- [ ] Think about how i would keep fold states sensical if i added code prettying. - [ ] Think about how i would keep fold states sensical if i added code prettying/formatting.
- [ ] Use tree-sitter to get the node path of the current node under cursor and add an indicator bar.
- [ ] Retry get proper blocks from tree-sitter. - (possibly with a picker to jump to any node)
- And use it for full block selection (including inline ones). - [ ] Add the highlight of block edges when cursor is on a bracket (or in). (prolly from lsp)
- And for indenting.
- And highlighting block edges etc.
- [ ] Add feature where doing enter uses tree-sitter to add newline with indentation.
- it should also put stuff like `}` on the next line.
- [ ] Add the highlight of block edges when cursor is on a bracket (or in).
- [ ] Add this thing where selection double click on a bracket selects whole block. - [ ] Add this thing where selection double click on a bracket selects whole block.
- (only on the first time) and sets mode to `WORD`. - (only on the first time) and sets mode to `WORD`.
- [ ] Redo folding system and its relation to move_line_* functions. - [ ] Redo folding system and its relation to move_line_* functions. (Currently its a mess)
- [ ] Also try regex based indentation.
- [ ] And indentation based blocks

View File

@@ -27,6 +27,8 @@ typedef struct LineIterator {
uint8_t top; uint8_t top;
uint32_t offset; uint32_t offset;
Knot *stack[64]; Knot *stack[64];
char *buffer;
size_t capacity;
} LineIterator; } LineIterator;
typedef struct LeafIterator { typedef struct LeafIterator {

View File

@@ -92,12 +92,12 @@ void render_editor(Editor *editor) {
case LINE: case LINE:
LineIterator *it = begin_l_iter(editor->root, editor->selection.row); LineIterator *it = begin_l_iter(editor->root, editor->selection.row);
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
free(it);
if (!line) if (!line)
return; return;
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
line_len--; line_len--;
free(line); free(it->buffer);
free(it);
end = {editor->selection.row, line_len}; end = {editor->selection.row, line_len};
break; break;
} }
@@ -148,7 +148,6 @@ void render_editor(Editor *editor) {
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
global_byte_offset--; global_byte_offset--;
global_byte_offset++; global_byte_offset++;
free(line);
line_index++; line_index++;
} }
continue; continue;
@@ -179,7 +178,8 @@ void render_editor(Editor *editor) {
update(editor->position.row + rendered_rows, editor->position.col, hook, update(editor->position.row + rendered_rows, editor->position.col, hook,
0xAAAAAA, 0, 0); 0xAAAAAA, 0, 0);
char buf[16]; char buf[16];
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 3, line_index + 1); int len =
snprintf(buf, sizeof(buf), "%*u ", numlen - 3, line_index + 1);
uint32_t num_color = uint32_t num_color =
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555; editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
@@ -298,7 +298,6 @@ void render_editor(Editor *editor) {
} }
global_byte_offset += line_len + 1; global_byte_offset += line_len + 1;
line_index++; line_index++;
free(line);
} }
if (cursor.row != UINT32_MAX && cursor.col != UINT32_MAX) { if (cursor.row != UINT32_MAX && cursor.col != UINT32_MAX) {
int type = 0; int type = 0;
@@ -322,5 +321,6 @@ void render_editor(Editor *editor) {
" ", 0xFFFFFF, 0, 0); " ", 0xFFFFFF, 0, 0);
rendered_rows++; rendered_rows++;
} }
free(it->buffer);
free(it); free(it);
} }

View File

@@ -48,7 +48,6 @@ void word_boundaries_exclusive(Editor *editor, Coord coord, uint32_t *prev_col,
return; return;
uint32_t line_len; uint32_t line_len;
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
free(it);
if (!line) if (!line)
return; return;
if (line_len && line[line_len - 1] == '\n') if (line_len && line[line_len - 1] == '\n')
@@ -62,7 +61,8 @@ void word_boundaries_exclusive(Editor *editor, Coord coord, uint32_t *prev_col,
*prev_col = left; *prev_col = left;
if (next_col) if (next_col)
*next_col = right; *next_col = right;
free(line); free(it->buffer);
free(it);
} }
uint32_t word_jump_right(const char *line, size_t len, uint32_t pos) { uint32_t word_jump_right(const char *line, size_t len, uint32_t pos) {
@@ -99,7 +99,6 @@ void word_boundaries(Editor *editor, Coord coord, uint32_t *prev_col,
return; return;
uint32_t line_len; uint32_t line_len;
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
free(it);
if (!line) if (!line)
return; return;
if (line_len && line[line_len - 1] == '\n') if (line_len && line[line_len - 1] == '\n')
@@ -117,7 +116,8 @@ void word_boundaries(Editor *editor, Coord coord, uint32_t *prev_col,
*prev_clusters = count_clusters(line, line_len, left, col); *prev_clusters = count_clusters(line, line_len, left, col);
if (next_clusters) if (next_clusters)
*next_clusters = count_clusters(line, line_len, col, right); *next_clusters = count_clusters(line, line_len, col, right);
free(line); free(it->buffer);
free(it);
} }
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) { Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
@@ -140,6 +140,7 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
const Fold *fold = fold_for_line(editor->folds, line_index); const Fold *fold = fold_for_line(editor->folds, line_index);
if (fold) { if (fold) {
if (visual_row == target_visual_row) { if (visual_row == target_visual_row) {
free(it->buffer);
free(it); free(it);
if (is_gutter_click) { if (is_gutter_click) {
remove_fold(editor, fold->start); remove_fold(editor, fold->start);
@@ -152,7 +153,6 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
char *l = next_line(it, nullptr); char *l = next_line(it, nullptr);
if (!l) if (!l)
break; break;
free(l);
line_index++; line_index++;
} }
continue; continue;
@@ -177,7 +177,7 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
if (col + w > render_width) if (col + w > render_width)
break; break;
if (visual_row == target_visual_row && x < col + w) { if (visual_row == target_visual_row && x < col + w) {
free(line); free(it->buffer);
free(it); free(it);
return {line_index, offset + advance}; return {line_index, offset + advance};
} }
@@ -187,7 +187,7 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
col += w; col += w;
} }
if (visual_row == target_visual_row) { if (visual_row == target_visual_row) {
free(line); free(it->buffer);
free(it); free(it);
return {line_index, last_good_offset}; return {line_index, last_good_offset};
} }
@@ -200,9 +200,9 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
if (line_len == 0) if (line_len == 0)
break; break;
} }
free(line);
line_index++; line_index++;
} }
free(it->buffer);
free(it); free(it);
return editor->scroll; return editor->scroll;
} }
@@ -215,16 +215,18 @@ Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
uint32_t col = result.col; uint32_t col = result.col;
uint32_t line_len = 0; uint32_t line_len = 0;
LineIterator *it = begin_l_iter(editor->root, row); LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &line_len); if (!it)
free(it);
if (!line)
return result; return result;
char *line = next_line(it, &line_len);
if (!line) {
free(it->buffer);
free(it);
return result;
}
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
--line_len; --line_len;
while (number > 0) { while (number > 0) {
if (col >= line_len) { if (col >= line_len) {
free(line);
line = nullptr;
uint32_t next_row = row + 1; uint32_t next_row = row + 1;
if (next_row >= editor->root->line_count) { if (next_row >= editor->root->line_count) {
col = line_len; col = line_len;
@@ -232,9 +234,7 @@ Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
} }
row = next_row; row = next_row;
col = 0; col = 0;
it = begin_l_iter(editor->root, row);
line = next_line(it, &line_len); line = next_line(it, &line_len);
free(it);
if (!line) if (!line)
break; break;
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
@@ -248,8 +248,8 @@ Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
} }
number--; number--;
} }
if (line) free(it->buffer);
free(line); free(it);
result.row = row; result.row = row;
result.col = col; result.col = col;
return result; return result;
@@ -265,6 +265,7 @@ Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number) {
LineIterator *it = begin_l_iter(editor->root, row); LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &len); char *line = next_line(it, &len);
if (!line) { if (!line) {
free(it->buffer);
free(it); free(it);
return result; return result;
} }
@@ -276,10 +277,9 @@ Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number) {
if (row == 0) if (row == 0)
break; break;
if (iterator_ahead) { if (iterator_ahead) {
free(prev_line(it, nullptr)); prev_line(it, nullptr);
iterator_ahead = false; iterator_ahead = false;
} }
free(line);
line = nullptr; line = nullptr;
row--; row--;
line = prev_line(it, &len); line = prev_line(it, &len);
@@ -301,8 +301,7 @@ Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number) {
} }
number--; number--;
} }
if (line) free(it->buffer);
free(line);
free(it); free(it);
result.row = row; result.row = row;
result.col = col; result.col = col;
@@ -317,41 +316,58 @@ Coord move_right(Editor *editor, Coord cursor, uint32_t number) {
uint32_t col = result.col; uint32_t col = result.col;
uint32_t line_len = 0; uint32_t line_len = 0;
LineIterator *it = begin_l_iter(editor->root, row); LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &line_len); if (!it)
free(it);
if (!line)
return result; return result;
uint32_t target_row = next_unfolded_row(editor, row);
while (row < target_row) {
if (!next_line(it, &line_len)) {
free(it->buffer);
free(it);
return result;
}
++row;
}
char *line = next_line(it, &line_len);
if (!line) {
free(it->buffer);
free(it);
return result;
}
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
--line_len; --line_len;
while (number > 0) { while (number > 0) {
if (col >= line_len) { if (col >= line_len) {
free(line);
line = nullptr;
uint32_t next_row = next_unfolded_row(editor, row + 1); uint32_t next_row = next_unfolded_row(editor, row + 1);
if (next_row >= editor->root->line_count) { if (next_row >= editor->root->line_count) {
col = line_len; col = line_len;
break; break;
} }
row = next_row; while (row < next_row) {
col = 0;
it = begin_l_iter(editor->root, row);
line = next_line(it, &line_len); line = next_line(it, &line_len);
if (!line) {
free(it->buffer);
free(it); free(it);
if (!line) result.row = row;
break; result.col = col;
return result;
}
++row;
}
col = 0;
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
--line_len; --line_len;
continue;
} else { } else {
uint32_t inc = uint32_t inc =
grapheme_next_character_break_utf8(line + col, line_len - col); grapheme_next_character_break_utf8(line + col, line_len - col);
if (inc == 0) if (inc == 0)
break; break;
col += inc; col += inc;
--number;
} }
number--;
} }
if (line) free(it->buffer);
free(line); free(it);
result.row = row; result.row = row;
result.col = col; result.col = col;
return result; return result;
@@ -367,6 +383,7 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
LineIterator *it = begin_l_iter(editor->root, row); LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &len); char *line = next_line(it, &len);
if (!line) { if (!line) {
free(it->buffer);
free(it); free(it);
return result; return result;
} }
@@ -378,10 +395,9 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
if (row == 0) if (row == 0)
break; break;
if (iterator_ahead) { if (iterator_ahead) {
free(prev_line(it, nullptr)); prev_line(it, nullptr);
iterator_ahead = false; iterator_ahead = false;
} }
free(line);
line = nullptr; line = nullptr;
while (row > 0) { while (row > 0) {
row--; row--;
@@ -391,14 +407,10 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
const Fold *fold = fold_for_line(editor->folds, row); const Fold *fold = fold_for_line(editor->folds, row);
if (fold) { if (fold) {
while (line && row > fold->start) { while (line && row > fold->start) {
free(line);
line = prev_line(it, &len); line = prev_line(it, &len);
row--; row--;
} }
if (line) {
free(line);
line = nullptr; line = nullptr;
}
continue; continue;
} }
break; break;
@@ -421,8 +433,7 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
} }
number--; number--;
} }
if (line) free(it->buffer);
free(line);
free(it); free(it);
result.row = row; result.row = row;
result.col = col; result.col = col;
@@ -435,14 +446,14 @@ void cursor_down(Editor *editor, uint32_t number) {
uint32_t len; uint32_t len;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row); LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line_content = next_line(it, &len); char *line_content = next_line(it, &len);
free(it);
if (line_content == nullptr) if (line_content == nullptr)
return; return;
if (editor->cursor_preffered == UINT32_MAX) if (editor->cursor_preffered == UINT32_MAX)
editor->cursor_preffered = editor->cursor_preffered =
get_visual_col_from_bytes(line_content, len, editor->cursor.col); get_visual_col_from_bytes(line_content, len, editor->cursor.col);
uint32_t visual_col = editor->cursor_preffered; uint32_t visual_col = editor->cursor_preffered;
free(line_content); free(it->buffer);
free(it);
uint32_t target_row = editor->cursor.row; uint32_t target_row = editor->cursor.row;
while (number > 0 && target_row < editor->root->line_count - 1) { while (number > 0 && target_row < editor->root->line_count - 1) {
target_row = next_unfolded_row(editor, target_row + 1); target_row = next_unfolded_row(editor, target_row + 1);
@@ -454,14 +465,14 @@ void cursor_down(Editor *editor, uint32_t number) {
} }
it = begin_l_iter(editor->root, target_row); it = begin_l_iter(editor->root, target_row);
line_content = next_line(it, &len); line_content = next_line(it, &len);
free(it);
if (!line_content) if (!line_content)
return; return;
if (len > 0 && line_content[len - 1] == '\n') if (len > 0 && line_content[len - 1] == '\n')
--len; --len;
editor->cursor.row = target_row; editor->cursor.row = target_row;
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col); editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
free(line_content); free(it->buffer);
free(it);
} }
void cursor_up(Editor *editor, uint32_t number) { void cursor_up(Editor *editor, uint32_t number) {
@@ -470,14 +481,14 @@ void cursor_up(Editor *editor, uint32_t number) {
uint32_t len; uint32_t len;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row); LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line_content = next_line(it, &len); char *line_content = next_line(it, &len);
free(it);
if (!line_content) if (!line_content)
return; return;
if (editor->cursor_preffered == UINT32_MAX) if (editor->cursor_preffered == UINT32_MAX)
editor->cursor_preffered = editor->cursor_preffered =
get_visual_col_from_bytes(line_content, len, editor->cursor.col); get_visual_col_from_bytes(line_content, len, editor->cursor.col);
uint32_t visual_col = editor->cursor_preffered; uint32_t visual_col = editor->cursor_preffered;
free(line_content); free(it->buffer);
free(it);
uint32_t target_row = editor->cursor.row; uint32_t target_row = editor->cursor.row;
while (number > 0 && target_row > 0) { while (number > 0 && target_row > 0) {
target_row = prev_unfolded_row(editor, target_row - 1); target_row = prev_unfolded_row(editor, target_row - 1);
@@ -489,18 +500,18 @@ void cursor_up(Editor *editor, uint32_t number) {
} }
it = begin_l_iter(editor->root, target_row); it = begin_l_iter(editor->root, target_row);
line_content = next_line(it, &len); line_content = next_line(it, &len);
free(it);
if (line_content) { if (line_content) {
if (len > 0 && line_content[len - 1] == '\n') if (len > 0 && line_content[len - 1] == '\n')
--len; --len;
editor->cursor.row = target_row; editor->cursor.row = target_row;
editor->cursor.col = editor->cursor.col =
get_bytes_from_visual_col(line_content, len, visual_col); get_bytes_from_visual_col(line_content, len, visual_col);
free(line_content);
} else { } else {
editor->cursor.row = 0; editor->cursor.row = 0;
editor->cursor.col = 0; editor->cursor.col = 0;
} }
free(it->buffer);
free(it);
} }
void cursor_right(Editor *editor, uint32_t number) { void cursor_right(Editor *editor, uint32_t number) {
@@ -525,7 +536,6 @@ void move_line_up(Editor *editor) {
std::shared_lock lock(editor->knot_mtx); std::shared_lock lock(editor->knot_mtx);
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row); LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
free(it);
if (!line) { if (!line) {
lock.unlock(); lock.unlock();
return; return;
@@ -543,7 +553,8 @@ void move_line_up(Editor *editor) {
edit_erase(editor, {cursor.row, 0}, -1); edit_erase(editor, {cursor.row, 0}, -1);
edit_insert(editor, {cursor.row - up_by, 0}, (char *)"\n", 1); edit_insert(editor, {cursor.row - up_by, 0}, (char *)"\n", 1);
edit_insert(editor, {cursor.row - up_by, 0}, line, line_len); edit_insert(editor, {cursor.row - up_by, 0}, line, line_len);
free(line); free(it->buffer);
free(it);
editor->cursor = {cursor.row - up_by, cursor.col}; editor->cursor = {cursor.row - up_by, cursor.col};
} else if (mode == SELECT) { } else if (mode == SELECT) {
uint32_t start_row = MIN(editor->cursor.row, editor->selection.row); uint32_t start_row = MIN(editor->cursor.row, editor->selection.row);
@@ -576,7 +587,6 @@ void move_line_down(Editor *editor) {
std::shared_lock lock(editor->knot_mtx); std::shared_lock lock(editor->knot_mtx);
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row); LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
free(it);
if (!line) { if (!line) {
lock.unlock(); lock.unlock();
return; return;
@@ -601,7 +611,8 @@ void move_line_down(Editor *editor) {
edit_erase(editor, {cursor.row, 0}, -1); edit_erase(editor, {cursor.row, 0}, -1);
edit_insert(editor, {cursor.row + down_by, 0}, (char *)"\n", 1); edit_insert(editor, {cursor.row + down_by, 0}, (char *)"\n", 1);
edit_insert(editor, {cursor.row + down_by, 0}, line, line_len); edit_insert(editor, {cursor.row + down_by, 0}, line, line_len);
free(line); free(it->buffer);
free(it);
editor->cursor = {cursor.row + down_by, cursor.col}; editor->cursor = {cursor.row + down_by, cursor.col};
} else if (mode == SELECT) { } else if (mode == SELECT) {
if (editor->cursor.row >= editor->root->line_count - 1 || if (editor->cursor.row >= editor->root->line_count - 1 ||
@@ -811,12 +822,13 @@ char *get_selection(Editor *editor, uint32_t *out_len) {
case LINE: case LINE:
LineIterator *it = begin_l_iter(editor->root, editor->selection.row); LineIterator *it = begin_l_iter(editor->root, editor->selection.row);
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
free(it);
if (!line) if (!line)
return nullptr; return nullptr;
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
line_len--; line_len--;
end = {editor->selection.row, line_len}; end = {editor->selection.row, line_len};
free(it->buffer);
free(it);
break; break;
} }
} }
@@ -877,13 +889,11 @@ bool add_fold(Editor *editor, uint32_t start, uint32_t end) {
end = std::min(end, editor->root->line_count - 1); end = std::min(end, editor->root->line_count - 1);
if (start == end) if (start == end)
return false; return false;
Fold new_fold{start, end}; Fold new_fold{start, end};
auto &folds = editor->folds; auto &folds = editor->folds;
auto it = std::lower_bound( auto it = std::lower_bound(
folds.begin(), folds.end(), new_fold.start, folds.begin(), folds.end(), new_fold.start,
[](const Fold &fold, uint32_t value) { return fold.start < value; }); [](const Fold &fold, uint32_t value) { return fold.start < value; });
if (it != folds.begin()) { if (it != folds.begin()) {
auto prev = std::prev(it); auto prev = std::prev(it);
if (prev->end + 1 >= new_fold.start) { if (prev->end + 1 >= new_fold.start) {

View File

@@ -70,12 +70,12 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
uint32_t line_len; uint32_t line_len;
LineIterator *it = begin_l_iter(editor->root, p.row); LineIterator *it = begin_l_iter(editor->root, p.row);
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
free(it);
if (!line) if (!line)
return; return;
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
line_len--; line_len--;
free(line); free(it->buffer);
free(it);
editor->cursor = {p.row, line_len}; editor->cursor = {p.row, line_len};
} }
editor->cursor_preffered = UINT32_MAX; editor->cursor_preffered = UINT32_MAX;
@@ -115,12 +115,12 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
} else { } else {
LineIterator *it = begin_l_iter(editor->root, p.row); LineIterator *it = begin_l_iter(editor->root, p.row);
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
free(it);
if (!line) if (!line)
return; return;
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
line_len--; line_len--;
free(line); free(it->buffer);
free(it);
editor->cursor = {p.row, line_len}; editor->cursor = {p.row, line_len};
} }
break; break;
@@ -216,9 +216,9 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
break; break;
if (line_len > 0 && line[line_len - 1] == '\n') if (line_len > 0 && line[line_len - 1] == '\n')
line_len--; line_len--;
free(it);
line_len = count_clusters(line, line_len, 0, line_len); line_len = count_clusters(line, line_len, 0, line_len);
free(line); free(it->buffer);
free(it);
editor->cursor.col = line_len; editor->cursor.col = line_len;
editor->cursor_preffered = UINT32_MAX; editor->cursor_preffered = UINT32_MAX;
mode = SELECT; mode = SELECT;
@@ -305,7 +305,6 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
uint32_t line_len = 0; uint32_t line_len = 0;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row); LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line = next_line(it, &line_len); char *line = next_line(it, &line_len);
free(it);
bool closing = false; bool closing = false;
if (line && line_len > 0 && line[line_len - 1] == '\n') if (line && line_len > 0 && line[line_len - 1] == '\n')
line_len--; line_len--;
@@ -314,8 +313,9 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
if (indent == 0) if (indent == 0)
indent = leading_indent(line, line_len); indent = leading_indent(line, line_len);
closing = closing_after_cursor(line, line_len, editor->cursor.col); closing = closing_after_cursor(line, line_len, editor->cursor.col);
free(line);
} }
free(it->buffer);
free(it);
uint32_t closing_indent = uint32_t closing_indent =
indent >= INDENT_WIDTH ? indent - INDENT_WIDTH : 0; indent >= INDENT_WIDTH ? indent - INDENT_WIDTH : 0;
std::string insert_text("\n"); std::string insert_text("\n");
@@ -366,10 +366,10 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
if (!it) if (!it)
return; return;
char *line = next_line(it, nullptr); char *line = next_line(it, nullptr);
free(it);
char prev_char = line[prev_pos.col]; char prev_char = line[prev_pos.col];
char next_char = line[editor->cursor.col]; char next_char = line[editor->cursor.col];
free(line); free(it->buffer);
free(it);
bool is_pair = (prev_char == '{' && next_char == '}') || bool is_pair = (prev_char == '{' && next_char == '}') ||
(prev_char == '(' && next_char == ')') || (prev_char == '(' && next_char == ')') ||
(prev_char == '[' && next_char == ']') || (prev_char == '[' && next_char == ']') ||

View File

@@ -23,10 +23,10 @@ uint32_t get_indent(Editor *editor, Coord cursor) {
if (line_len == 0) if (line_len == 0)
continue; continue;
uint32_t indent = leading_indent(line, line_len); uint32_t indent = leading_indent(line, line_len);
free(line);
free(it);
return indent; return indent;
} }
free(it->buffer);
free(it);
return 0; return 0;
} }

View File

@@ -18,6 +18,7 @@ void scroll_up(Editor *editor, int32_t number) {
uint32_t len; uint32_t len;
char *line = next_line(it, &len); char *line = next_line(it, &len);
if (!line) { if (!line) {
free(it->buffer);
free(it); free(it);
return; return;
} }
@@ -45,24 +46,24 @@ void scroll_up(Editor *editor, int32_t number) {
++it_seg) { ++it_seg) {
if (--number == 0) { if (--number == 0) {
editor->scroll = {line_index, *it_seg}; editor->scroll = {line_index, *it_seg};
free(line); free(it->buffer);
free(it); free(it);
return; return;
} }
} }
free(line);
line = prev_line(it, &len); line = prev_line(it, &len);
if (!line) { if (!line) {
editor->scroll = {0, 0}; editor->scroll = {0, 0};
free(it->buffer);
free(it); free(it);
return; return;
} }
free(line);
do { do {
line_index--; line_index--;
line = prev_line(it, &len); line = prev_line(it, &len);
if (!line) { if (!line) {
editor->scroll = {0, 0}; editor->scroll = {0, 0};
free(it->buffer);
free(it); free(it);
return; return;
} }
@@ -74,19 +75,20 @@ void scroll_up(Editor *editor, int32_t number) {
line_index--; line_index--;
if (!line) { if (!line) {
editor->scroll = {0, 0}; editor->scroll = {0, 0};
free(it->buffer);
free(it); free(it);
return; return;
} }
} }
if (--number == 0) { if (--number == 0) {
editor->scroll = {fold->start, 0}; editor->scroll = {fold->start, 0};
free(line); free(it->buffer);
free(it); free(it);
return; return;
} }
free(line);
if (fold->start == 0) { if (fold->start == 0) {
editor->scroll = {0, 0}; editor->scroll = {0, 0};
free(it->buffer);
free(it); free(it);
return; return;
} }
@@ -94,6 +96,7 @@ void scroll_up(Editor *editor, int32_t number) {
line = prev_line(it, &len); line = prev_line(it, &len);
if (!line) { if (!line) {
editor->scroll = {0, 0}; editor->scroll = {0, 0};
free(it->buffer);
free(it); free(it);
return; return;
} }
@@ -121,13 +124,13 @@ void scroll_up(Editor *editor, int32_t number) {
++it_seg) { ++it_seg) {
if (--number == 0) { if (--number == 0) {
editor->scroll = {line_index, *it_seg}; editor->scroll = {line_index, *it_seg};
free(line); free(it->buffer);
free(it); free(it);
return; return;
} }
} }
free(line);
} while (number > 0); } while (number > 0);
free(it->buffer);
free(it); free(it);
} }
@@ -168,10 +171,10 @@ void scroll_down(Editor *editor, uint32_t number) {
char *line = next_line(it, nullptr); char *line = next_line(it, nullptr);
if (!line) { if (!line) {
free(scroll_queue); free(scroll_queue);
free(it->buffer);
free(it); free(it);
return; return;
} }
free(line);
line_index++; line_index++;
} }
continue; continue;
@@ -200,8 +203,8 @@ void scroll_down(Editor *editor, uint32_t number) {
visual_seen++; visual_seen++;
if (visual_seen >= number + max_visual_lines) { if (visual_seen >= number + max_visual_lines) {
editor->scroll = scroll_queue[q_head]; editor->scroll = scroll_queue[q_head];
free(line);
free(scroll_queue); free(scroll_queue);
free(it->buffer);
free(it); free(it);
return; return;
} }
@@ -223,13 +226,13 @@ void scroll_down(Editor *editor, uint32_t number) {
if (line_len == 0) if (line_len == 0)
break; break;
} }
free(line);
line_index++; line_index++;
} }
if (q_size > 0) { if (q_size > 0) {
uint32_t advance = (q_size > number) ? number : (q_size - 1); uint32_t advance = (q_size > number) ? number : (q_size - 1);
editor->scroll = scroll_queue[(q_head + advance) % max_visual_lines]; editor->scroll = scroll_queue[(q_head + advance) % max_visual_lines];
} }
free(it->buffer);
free(it); free(it);
free(scroll_queue); free(scroll_queue);
} }
@@ -267,7 +270,6 @@ void ensure_cursor(Editor *editor) {
char *line = next_line(it, nullptr); char *line = next_line(it, nullptr);
if (!line) if (!line)
break; break;
free(line);
line_index++; line_index++;
} }
continue; continue;
@@ -302,7 +304,7 @@ void ensure_cursor(Editor *editor) {
if (line_index == editor->cursor.row) { if (line_index == editor->cursor.row) {
if (editor->cursor.col >= offset && if (editor->cursor.col >= offset &&
editor->cursor.col <= offset + advance) { editor->cursor.col <= offset + advance) {
free(line); free(it->buffer);
free(it); free(it);
return; return;
} }
@@ -313,7 +315,6 @@ void ensure_cursor(Editor *editor) {
if (line_len == 0) if (line_len == 0)
break; break;
} }
free(line);
line_index++; line_index++;
} }
uint32_t last_real_row = last_visible.row; uint32_t last_real_row = last_visible.row;
@@ -325,6 +326,7 @@ void ensure_cursor(Editor *editor) {
editor->cursor.row = last_visible.row; editor->cursor.row = last_visible.row;
editor->cursor.col = last_visible.row == last_real_row ? last_visible.col : 0; editor->cursor.col = last_visible.row == last_real_row ? last_visible.col : 0;
editor->cursor_preffered = UINT32_MAX; editor->cursor_preffered = UINT32_MAX;
free(it->buffer);
free(it); free(it);
} }
@@ -340,6 +342,7 @@ void ensure_scroll(Editor *editor) {
uint32_t len; uint32_t len;
char *line = next_line(it, &len); char *line = next_line(it, &len);
if (!line) { if (!line) {
free(it->buffer);
free(it); free(it);
return; return;
} }
@@ -359,7 +362,7 @@ void ensure_scroll(Editor *editor) {
if (editor->cursor.col > old_offset && editor->cursor.col <= offset) { if (editor->cursor.col > old_offset && editor->cursor.col <= offset) {
editor->scroll.row = editor->cursor.row; editor->scroll.row = editor->cursor.row;
editor->scroll.col = old_offset; editor->scroll.col = old_offset;
free(line); free(it->buffer);
free(it); free(it);
return; return;
} }
@@ -368,7 +371,7 @@ void ensure_scroll(Editor *editor) {
cols += width; cols += width;
offset += inc; offset += inc;
} }
free(line); free(it->buffer);
free(it); free(it);
editor->scroll.row = editor->cursor.row; editor->scroll.row = editor->cursor.row;
editor->scroll.col = (editor->cursor.col == 0) ? 0 : old_offset; editor->scroll.col = (editor->cursor.col == 0) ? 0 : old_offset;
@@ -403,7 +406,6 @@ void ensure_scroll(Editor *editor) {
char *line = next_line(it, nullptr); char *line = next_line(it, nullptr);
if (!line) if (!line)
break; break;
free(line);
line_index++; line_index++;
} }
continue; continue;
@@ -453,8 +455,8 @@ void ensure_scroll(Editor *editor) {
cursor_found = true; cursor_found = true;
if (cursor_found) { if (cursor_found) {
editor->scroll = scroll_queue[q_head]; editor->scroll = scroll_queue[q_head];
free(line);
free(scroll_queue); free(scroll_queue);
free(it->buffer);
free(it); free(it);
return; return;
} }
@@ -464,9 +466,9 @@ void ensure_scroll(Editor *editor) {
break; break;
} }
line_index++; line_index++;
free(line);
} }
free(scroll_queue); free(scroll_queue);
free(it->buffer);
free(it); free(it);
} }
} }

View File

@@ -471,6 +471,12 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line) {
return nullptr; return nullptr;
it->top = 0; it->top = 0;
it->node = nullptr; it->node = nullptr;
it->capacity = 128;
it->buffer = (char *)malloc(it->capacity);
if (!it->buffer) {
free(it);
return nullptr;
}
if (start_line == 0) { if (start_line == 0) {
it->offset = 0; it->offset = 0;
while (root->left) { while (root->left) {
@@ -499,6 +505,7 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line) {
} }
} }
if (!it->node) { if (!it->node) {
free(it->buffer);
free(it); free(it);
return nullptr; return nullptr;
} }
@@ -516,7 +523,7 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line) {
} }
} }
} }
free(next_line(it, nullptr)); next_line(it, nullptr);
return it; return it;
} }
@@ -559,11 +566,7 @@ static void str_reverse(char *begin, char *end) {
char *prev_line(LineIterator *it, uint32_t *out_len) { char *prev_line(LineIterator *it, uint32_t *out_len) {
if (!it || !it->node) if (!it || !it->node)
return nullptr; return nullptr;
size_t capacity = 128;
size_t len = 0; size_t len = 0;
char *buffer = (char *)malloc(capacity);
if (!buffer)
return nullptr;
while (it->node) { while (it->node) {
if (it->offset == 0) { if (it->offset == 0) {
iter_retreat_leaf(it); iter_retreat_leaf(it);
@@ -578,25 +581,22 @@ char *prev_line(LineIterator *it, uint32_t *out_len) {
break; break;
} }
} }
if (len + 1 >= capacity) { if (len + 1 >= it->capacity) {
capacity *= 2; it->capacity *= 2;
char *new_buf = (char *)realloc(buffer, capacity); char *new_buf = (char *)realloc(it->buffer, it->capacity);
if (!new_buf) { if (!new_buf)
free(buffer);
return nullptr; return nullptr;
it->buffer = new_buf;
} }
buffer = new_buf; it->buffer[len++] = c;
}
buffer[len++] = c;
} }
if (len > 0) { if (len > 0) {
buffer[len] = '\0'; it->buffer[len] = '\0';
str_reverse(buffer, buffer + len - 1); str_reverse(it->buffer, it->buffer + len - 1);
if (out_len) if (out_len)
*out_len = len; *out_len = len;
return buffer; return it->buffer;
} }
free(buffer);
return nullptr; return nullptr;
} }
@@ -630,11 +630,7 @@ static inline void iter_advance_leaf(LineIterator *it) {
char *next_line(LineIterator *it, uint32_t *out_len) { char *next_line(LineIterator *it, uint32_t *out_len) {
if (!it || !it->node) if (!it || !it->node)
return nullptr; return nullptr;
size_t capacity = 128;
size_t len = 0; size_t len = 0;
char *buffer = (char *)malloc(capacity);
if (!buffer)
return nullptr;
while (it->node) { while (it->node) {
if (it->offset >= it->node->char_count) { if (it->offset >= it->node->char_count) {
iter_advance_leaf(it); iter_advance_leaf(it);
@@ -652,32 +648,30 @@ char *next_line(LineIterator *it, uint32_t *out_len) {
} else { } else {
chunk_len = end - start; chunk_len = end - start;
} }
if (len + chunk_len + 1 > capacity) { if (len + chunk_len + 1 > it->capacity) {
capacity = (capacity * 2) + chunk_len; it->capacity = (it->capacity * 2) + chunk_len;
char *new_buf = (char *)realloc(buffer, capacity); char *new_buf = (char *)realloc(it->buffer, it->capacity);
if (!new_buf) { if (!new_buf) {
free(buffer);
return nullptr; return nullptr;
} }
buffer = new_buf; it->buffer = new_buf;
} }
memcpy(buffer + len, start, chunk_len); memcpy(it->buffer + len, start, chunk_len);
len += chunk_len; len += chunk_len;
it->offset += chunk_len; it->offset += chunk_len;
if (found_newline) { if (found_newline) {
buffer[len] = '\0'; it->buffer[len] = '\0';
if (out_len) if (out_len)
*out_len = len; *out_len = len;
return buffer; return it->buffer;
} }
} }
if (len > 0) { if (len > 0) {
buffer[len] = '\0'; it->buffer[len] = '\0';
if (out_len) if (out_len)
*out_len = len; *out_len = len;
return buffer; return it->buffer;
} }
free(buffer);
return nullptr; return nullptr;
} }