Compare commits

...

4 Commits

Author SHA1 Message Date
a12e2fb1c4 Add better indentation and pasting 2025-12-22 14:56:48 +00:00
7307387f64 Optimize line iterator 2025-12-19 20:46:52 +00:00
8002012705 Update todo list 2025-12-19 15:00:16 +00:00
d5145d88a7 Basic indentation support 2025-12-18 18:55:08 +00:00
12 changed files with 419 additions and 147 deletions

View File

@@ -6,21 +6,48 @@ A TUI IDE.
# TODO
- [ ] 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.
- (only on the first time) and sets mode to `WORD`.
- [ ] 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. as a test.
- [ ] 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 this thing where select at end of screen scrolls down. (and vice versa)
- Can be acheived by updating `main.cc` to send drag events to the selected editor instead of just under cursor.
- Then a drag event above or below will scroll the selected editor.
- [ ] Add support for virtual cursor where edits apply at all the places.
- [ ] Add alt + click to set multiple cursors.
- [ ] Add search / replace along with search / virtual cursors are searched pos.
- [ ] Add support for undo/redo.
- [ ] Add `.scm` files for all the supported languages. (2/14) Done.
- [ ] Add splash screen / minigame jumping.
- [ ] Add support for LSP & autocomplete / snippets.
- [ ] Add codeium/copilot support.
- [ ] Normalize / validate unicode on file open.
- [ ] 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.
- [ ] Think about how i would keep fold states sensical if i added code prettying.
- [ ] Redo folding system and its relation to move_line_* functions.
- [ ] 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.
- (possibly with a picker to jump to any node)
- [ ] Add the highlight of block edges when cursor is on a bracket (or in). (prolly from lsp)
- [ ] Add this thing where selection double click on a bracket selects whole block.
- (only on the first time) and sets mode to `WORD`.
- [ ] Redo folding system and its relation to move_line_* functions. (Currently its a mess)

View File

@@ -18,6 +18,8 @@
#define EXTRA_META 4
#define INDENT_WIDTH 2
struct Highlight {
uint32_t fg;
uint32_t bg;
@@ -175,6 +177,9 @@ inline uint32_t prev_unfolded_row(const Editor *editor, uint32_t row) {
}
void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y);
void apply_hook_insertion(Editor *editor, uint32_t line, uint32_t rows);
void apply_hook_deletion(Editor *editor, uint32_t removal_start,
uint32_t removal_end);
Editor *new_editor(const char *filename, Coord position, Coord size);
void free_editor(Editor *editor);
void render_editor(Editor *editor);
@@ -188,12 +193,14 @@ void cursor_right(Editor *editor, uint32_t number);
void scroll_up(Editor *editor, int32_t number);
void scroll_down(Editor *editor, uint32_t number);
void ensure_cursor(Editor *editor);
void indent_line(Editor *editor, uint32_t row);
void dedent_line(Editor *editor, uint32_t row);
void ensure_scroll(Editor *editor);
void handle_editor_event(Editor *editor, KeyEvent event);
void edit_erase(Editor *editor, Coord pos, int64_t len);
void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len);
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y);
char *get_selection(Editor *editor, uint32_t *out_len);
char *get_selection(Editor *editor, uint32_t *out_len, Coord *out_start);
void editor_worker(Editor *editor);
void move_line_down(Editor *editor);
void move_line_up(Editor *editor);
@@ -208,5 +215,8 @@ bool remove_fold(Editor *editor, uint32_t line);
void apply_line_insertion(Editor *editor, uint32_t line, uint32_t rows);
void apply_line_deletion(Editor *editor, uint32_t removal_start,
uint32_t removal_end);
uint32_t leading_indent(const char *line, uint32_t len);
uint32_t get_indent(Editor *editor, Coord cursor);
bool closing_after_cursor(const char *line, uint32_t len, uint32_t col);
#endif

View File

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

View File

@@ -15,7 +15,8 @@
#define KEY_CHAR 0
#define KEY_SPECIAL 1
#define KEY_MOUSE 2
#define KEY_NONE 3
#define KEY_PASTE 3
#define KEY_NONE 4
#define KEY_UP 0
#define KEY_DOWN 1

View File

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

View File

@@ -48,7 +48,6 @@ void word_boundaries_exclusive(Editor *editor, Coord coord, uint32_t *prev_col,
return;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
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;
if (next_col)
*next_col = right;
free(line);
free(it->buffer);
free(it);
}
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;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
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);
if (next_clusters)
*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) {
@@ -131,6 +131,8 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
uint32_t target_visual_row = y;
uint32_t visual_row = 0;
uint32_t line_index = editor->scroll.row;
uint32_t last_line_index = editor->scroll.row;
uint32_t last_col = editor->scroll.col;
bool first_visual_line = true;
std::shared_lock knot_lock(editor->knot_mtx);
LineIterator *it = begin_l_iter(editor->root, line_index);
@@ -140,6 +142,7 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
const Fold *fold = fold_for_line(editor->folds, line_index);
if (fold) {
if (visual_row == target_visual_row) {
free(it->buffer);
free(it);
if (is_gutter_click) {
remove_fold(editor, fold->start);
@@ -152,9 +155,10 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
char *l = next_line(it, nullptr);
if (!l)
break;
free(l);
line_index++;
}
last_line_index = fold->end;
last_col = 0;
continue;
}
uint32_t line_len;
@@ -163,6 +167,8 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
break;
if (line_len && line[line_len - 1] == '\n')
line_len--;
last_line_index = line_index;
last_col = line_len;
uint32_t offset = first_visual_line ? editor->scroll.col : 0;
first_visual_line = false;
while (offset < line_len || (line_len == 0 && offset == 0)) {
@@ -177,7 +183,7 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
if (col + w > render_width)
break;
if (visual_row == target_visual_row && x < col + w) {
free(line);
free(it->buffer);
free(it);
return {line_index, offset + advance};
}
@@ -186,8 +192,9 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
left -= g;
col += w;
}
last_col = last_good_offset;
if (visual_row == target_visual_row) {
free(line);
free(it->buffer);
free(it);
return {line_index, last_good_offset};
}
@@ -200,11 +207,11 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
if (line_len == 0)
break;
}
free(line);
line_index++;
}
free(it->buffer);
free(it);
return editor->scroll;
return {last_line_index, last_col};
}
Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
@@ -215,16 +222,18 @@ Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
uint32_t col = result.col;
uint32_t line_len = 0;
LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &line_len);
free(it);
if (!line)
if (!it)
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')
--line_len;
while (number > 0) {
if (col >= line_len) {
free(line);
line = nullptr;
uint32_t next_row = row + 1;
if (next_row >= editor->root->line_count) {
col = line_len;
@@ -232,9 +241,7 @@ Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
}
row = next_row;
col = 0;
it = begin_l_iter(editor->root, row);
line = next_line(it, &line_len);
free(it);
if (!line)
break;
if (line_len > 0 && line[line_len - 1] == '\n')
@@ -248,8 +255,8 @@ Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
}
number--;
}
if (line)
free(line);
free(it->buffer);
free(it);
result.row = row;
result.col = col;
return result;
@@ -265,6 +272,7 @@ Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number) {
LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &len);
if (!line) {
free(it->buffer);
free(it);
return result;
}
@@ -276,10 +284,9 @@ Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number) {
if (row == 0)
break;
if (iterator_ahead) {
free(prev_line(it, nullptr));
prev_line(it, nullptr);
iterator_ahead = false;
}
free(line);
line = nullptr;
row--;
line = prev_line(it, &len);
@@ -301,8 +308,7 @@ Coord move_left_pure(Editor *editor, Coord cursor, uint32_t number) {
}
number--;
}
if (line)
free(line);
free(it->buffer);
free(it);
result.row = row;
result.col = col;
@@ -317,41 +323,59 @@ Coord move_right(Editor *editor, Coord cursor, uint32_t number) {
uint32_t col = result.col;
uint32_t line_len = 0;
LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &line_len);
free(it);
if (!line)
if (!it)
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')
--line_len;
while (number > 0) {
if (col >= line_len) {
free(line);
line = nullptr;
uint32_t next_row = next_unfolded_row(editor, row + 1);
if (next_row >= editor->root->line_count) {
col = line_len;
break;
}
row = next_row;
col = 0;
it = begin_l_iter(editor->root, row);
line = next_line(it, &line_len);
free(it);
if (!line)
break;
while (row < next_row) {
line = next_line(it, &line_len);
if (!line) {
free(it->buffer);
free(it);
result.row = row;
result.col = col;
return result;
}
++row;
}
if (line_len > 0 && line[line_len - 1] == '\n')
--line_len;
col = 0;
--number;
continue;
} else {
uint32_t inc =
grapheme_next_character_break_utf8(line + col, line_len - col);
if (inc == 0)
break;
col += inc;
--number;
}
number--;
}
if (line)
free(line);
free(it->buffer);
free(it);
result.row = row;
result.col = col;
return result;
@@ -367,6 +391,7 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &len);
if (!line) {
free(it->buffer);
free(it);
return result;
}
@@ -378,10 +403,9 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
if (row == 0)
break;
if (iterator_ahead) {
free(prev_line(it, nullptr));
prev_line(it, nullptr);
iterator_ahead = false;
}
free(line);
line = nullptr;
while (row > 0) {
row--;
@@ -391,14 +415,10 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
const Fold *fold = fold_for_line(editor->folds, row);
if (fold) {
while (line && row > fold->start) {
free(line);
line = prev_line(it, &len);
row--;
}
if (line) {
free(line);
line = nullptr;
}
line = nullptr;
continue;
}
break;
@@ -421,8 +441,7 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
}
number--;
}
if (line)
free(line);
free(it->buffer);
free(it);
result.row = row;
result.col = col;
@@ -435,14 +454,14 @@ void cursor_down(Editor *editor, uint32_t number) {
uint32_t len;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line_content = next_line(it, &len);
free(it);
if (line_content == nullptr)
return;
if (editor->cursor_preffered == UINT32_MAX)
editor->cursor_preffered =
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
uint32_t visual_col = editor->cursor_preffered;
free(line_content);
free(it->buffer);
free(it);
uint32_t target_row = editor->cursor.row;
while (number > 0 && target_row < editor->root->line_count - 1) {
target_row = next_unfolded_row(editor, target_row + 1);
@@ -454,14 +473,14 @@ void cursor_down(Editor *editor, uint32_t number) {
}
it = begin_l_iter(editor->root, target_row);
line_content = next_line(it, &len);
free(it);
if (!line_content)
return;
if (len > 0 && line_content[len - 1] == '\n')
--len;
editor->cursor.row = target_row;
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) {
@@ -470,14 +489,14 @@ void cursor_up(Editor *editor, uint32_t number) {
uint32_t len;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line_content = next_line(it, &len);
free(it);
if (!line_content)
return;
if (editor->cursor_preffered == UINT32_MAX)
editor->cursor_preffered =
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
uint32_t visual_col = editor->cursor_preffered;
free(line_content);
free(it->buffer);
free(it);
uint32_t target_row = editor->cursor.row;
while (number > 0 && target_row > 0) {
target_row = prev_unfolded_row(editor, target_row - 1);
@@ -489,18 +508,18 @@ void cursor_up(Editor *editor, uint32_t number) {
}
it = begin_l_iter(editor->root, target_row);
line_content = next_line(it, &len);
free(it);
if (line_content) {
if (len > 0 && line_content[len - 1] == '\n')
--len;
editor->cursor.row = target_row;
editor->cursor.col =
get_bytes_from_visual_col(line_content, len, visual_col);
free(line_content);
} else {
editor->cursor.row = 0;
editor->cursor.col = 0;
}
free(it->buffer);
free(it);
}
void cursor_right(Editor *editor, uint32_t number) {
@@ -525,7 +544,6 @@ void move_line_up(Editor *editor) {
std::shared_lock lock(editor->knot_mtx);
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line = next_line(it, &line_len);
free(it);
if (!line) {
lock.unlock();
return;
@@ -543,7 +561,8 @@ void move_line_up(Editor *editor) {
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}, line, line_len);
free(line);
free(it->buffer);
free(it);
editor->cursor = {cursor.row - up_by, cursor.col};
} else if (mode == SELECT) {
uint32_t start_row = MIN(editor->cursor.row, editor->selection.row);
@@ -576,7 +595,6 @@ void move_line_down(Editor *editor) {
std::shared_lock lock(editor->knot_mtx);
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line = next_line(it, &line_len);
free(it);
if (!line) {
lock.unlock();
return;
@@ -601,7 +619,8 @@ void move_line_down(Editor *editor) {
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}, line, line_len);
free(line);
free(it->buffer);
free(it);
editor->cursor = {cursor.row + down_by, cursor.col};
} else if (mode == SELECT) {
if (editor->cursor.row >= editor->root->line_count - 1 ||
@@ -661,6 +680,7 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
uint32_t start_row = point.row;
uint32_t end_row = pos.row;
apply_line_deletion(editor, start_row + 1, end_row);
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();
@@ -704,6 +724,7 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
uint32_t start_row = pos.row;
uint32_t end_row = point.row;
apply_line_deletion(editor, start_row + 1, end_row);
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();
@@ -755,6 +776,7 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
}
}
apply_line_insertion(editor, pos.row, rows);
apply_hook_insertion(editor, pos.row, rows);
if (editor->tree) {
TSInputEdit edit = {
.start_byte = byte_pos,
@@ -775,7 +797,7 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
apply_edit(editor->def_spans.spans, byte_pos, len);
}
char *get_selection(Editor *editor, uint32_t *out_len) {
char *get_selection(Editor *editor, uint32_t *out_len, Coord *out_start) {
std::shared_lock lock(editor->knot_mtx);
Coord start, end;
if (editor->cursor >= editor->selection) {
@@ -811,15 +833,18 @@ char *get_selection(Editor *editor, uint32_t *out_len) {
case LINE:
LineIterator *it = begin_l_iter(editor->root, editor->selection.row);
char *line = next_line(it, &line_len);
free(it);
if (!line)
return nullptr;
if (line_len > 0 && line[line_len - 1] == '\n')
line_len--;
end = {editor->selection.row, line_len};
free(it->buffer);
free(it);
break;
}
}
if (out_start)
*out_start = start;
uint32_t start_byte =
line_to_byte(editor->root, start.row, nullptr) + start.col;
uint32_t end_byte = line_to_byte(editor->root, end.row, nullptr) + end.col;
@@ -877,13 +902,11 @@ bool add_fold(Editor *editor, uint32_t start, uint32_t end) {
end = std::min(end, editor->root->line_count - 1);
if (start == end)
return false;
Fold new_fold{start, end};
auto &folds = editor->folds;
auto it = std::lower_bound(
folds.begin(), folds.end(), new_fold.start,
[](const Fold &fold, uint32_t value) { return fold.start < value; });
if (it != folds.begin()) {
auto prev = std::prev(it);
if (prev->end + 1 >= new_fold.start) {
@@ -943,3 +966,16 @@ void apply_line_deletion(Editor *editor, uint32_t removal_start,
}
editor->folds.swap(updated);
}
void apply_hook_insertion(Editor *editor, uint32_t line, uint32_t rows) {
for (auto &hook : editor->hooks)
if (hook > line)
hook += rows;
}
void apply_hook_deletion(Editor *editor, uint32_t removal_start,
uint32_t removal_end) {
for (auto &hook : editor->hooks)
if (hook > removal_start)
hook -= removal_end - removal_start + 1;
}

View File

@@ -1,6 +1,7 @@
#include "../include/editor.h"
#include "../include/main.h"
#include "../include/ts.h"
#include <cstdint>
void handle_editor_event(Editor *editor, KeyEvent event) {
static std::chrono::steady_clock::time_point last_click_time =
@@ -70,12 +71,12 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
uint32_t line_len;
LineIterator *it = begin_l_iter(editor->root, p.row);
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
if (line_len > 0 && line[line_len - 1] == '\n')
line_len--;
free(line);
free(it->buffer);
free(it);
editor->cursor = {p.row, line_len};
}
editor->cursor_preffered = UINT32_MAX;
@@ -115,12 +116,12 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
} else {
LineIterator *it = begin_l_iter(editor->root, p.row);
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
if (line_len > 0 && line[line_len - 1] == '\n')
line_len--;
free(line);
free(it->buffer);
free(it);
editor->cursor = {p.row, line_len};
}
break;
@@ -203,6 +204,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
switch (mode) {
case NORMAL:
if (event.key_type == KEY_CHAR && event.len == 1) {
Coord start = editor->cursor;
switch (event.c[0]) {
case 'u':
if (editor->root->line_count > 0) {
@@ -216,9 +218,9 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
break;
if (line_len > 0 && line[line_len - 1] == '\n')
line_len--;
free(it);
line_len = count_clusters(line, line_len, 0, line_len);
free(line);
free(it->buffer);
free(it);
editor->cursor.col = line_len;
editor->cursor_preffered = UINT32_MAX;
mode = SELECT;
@@ -230,6 +232,8 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
case 'a':
mode = INSERT;
cursor_right(editor, 1);
if (start.row != editor->cursor.row)
cursor_left(editor, 1);
break;
case 'i':
mode = INSERT;
@@ -282,6 +286,14 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
scroll_up(editor, 1);
ensure_cursor(editor);
break;
case '>':
case '.':
indent_line(editor, editor->cursor.row);
break;
case '<':
case ',':
dedent_line(editor, editor->cursor.row);
break;
case 'p':
uint32_t len;
char *text = get_from_clipboard(&len);
@@ -302,8 +314,33 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
edit_insert(editor, editor->cursor, (char *)" ", 2);
cursor_right(editor, 2);
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
edit_insert(editor, editor->cursor, (char *)"\n", 1);
cursor_right(editor, 1);
uint32_t line_len = 0;
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
char *line = next_line(it, &line_len);
bool closing = false;
if (line && line_len > 0 && line[line_len - 1] == '\n')
line_len--;
uint32_t indent = get_indent(editor, editor->cursor);
if (line) {
if (indent == 0)
indent = leading_indent(line, line_len);
closing = closing_after_cursor(line, line_len, editor->cursor.col);
}
free(it->buffer);
free(it);
uint32_t closing_indent =
indent >= INDENT_WIDTH ? indent - INDENT_WIDTH : 0;
std::string insert_text("\n");
insert_text.append(indent, ' ');
Coord new_cursor = {editor->cursor.row + 1, indent};
if (closing) {
insert_text.push_back('\n');
insert_text.append(closing_indent, ' ');
}
edit_insert(editor, editor->cursor, insert_text.data(),
insert_text.size());
editor->cursor = new_cursor;
editor->cursor_preffered = UINT32_MAX;
} else if (event.c[0] == CTRL('W')) {
uint32_t prev_col_byte, prev_col_cluster;
word_boundaries(editor, editor->cursor, &prev_col_byte, nullptr,
@@ -341,10 +378,10 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
if (!it)
return;
char *line = next_line(it, nullptr);
free(it);
char prev_char = line[prev_pos.col];
char next_char = line[editor->cursor.col];
free(line);
free(it->buffer);
free(it);
bool is_pair = (prev_char == '{' && next_char == '}') ||
(prev_char == '(' && next_char == ')') ||
(prev_char == '[' && next_char == ']') ||
@@ -357,8 +394,11 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
edit_erase(editor, editor->cursor, -1);
}
} else if (event.c[0] == 0x1B) {
Coord prev_pos = editor->cursor;
mode = NORMAL;
cursor_left(editor, 1);
if (prev_pos.row != editor->cursor.row)
cursor_right(editor, 1);
}
} else if (event.len > 1) {
edit_insert(editor, editor->cursor, event.c, event.len);
@@ -380,12 +420,20 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
edit_erase(editor, editor->cursor, next_col_cluster);
break;
}
} else if (event.key_type == KEY_PASTE) {
if (event.c) {
edit_insert(editor, editor->cursor, event.c, event.len);
uint32_t grapheme_len =
count_clusters(event.c, event.len, 0, event.len);
cursor_right(editor, grapheme_len);
}
}
break;
case SELECT:
if (event.key_type == KEY_CHAR && event.len == 1) {
uint32_t len;
char *text;
Coord start;
switch (event.c[0]) {
case 'f':
if (editor->cursor.row != editor->selection.row) {
@@ -405,16 +453,17 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
mode = NORMAL;
break;
case 'y':
text = get_selection(editor, &len);
text = get_selection(editor, &len, nullptr);
copy_to_clipboard(text, len);
free(text);
editor->selection_active = false;
mode = NORMAL;
break;
case 'x':
text = get_selection(editor, &len);
text = get_selection(editor, &len, &start);
copy_to_clipboard(text, len);
edit_erase(editor, MIN(editor->cursor, editor->selection), len);
len = count_clusters(text, len, 0, len);
edit_erase(editor, start, len);
free(text);
editor->selection_active = false;
mode = NORMAL;
@@ -477,7 +526,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
break;
}
ensure_scroll(editor);
if (event.key_type == KEY_CHAR && event.c)
if ((event.key_type == KEY_CHAR || event.key_type == KEY_PASTE) && event.c)
free(event.c);
}

86
src/editor_indents.cc Normal file
View File

@@ -0,0 +1,86 @@
#include "../include/editor.h"
uint32_t leading_indent(const char *line, uint32_t len) {
uint32_t indent = 0;
for (uint32_t i = 0; i < len; i++) {
if (line[i] == ' ')
indent++;
else if (line[i] == '\t')
indent += INDENT_WIDTH;
else
break;
}
return indent;
}
uint32_t get_indent(Editor *editor, Coord cursor) {
if (!editor)
return 0;
LineIterator *it = begin_l_iter(editor->root, cursor.row);
uint32_t line_len;
char *line;
while ((line = prev_line(it, &line_len)) != nullptr) {
if (line_len == 0)
continue;
uint32_t indent = leading_indent(line, line_len);
free(it->buffer);
free(it);
return indent;
}
free(it->buffer);
free(it);
return 0;
}
bool closing_after_cursor(const char *line, uint32_t len, uint32_t col) {
uint32_t i = col;
while (i < len && (line[i] == ' ' || line[i] == '\t'))
i++;
if (i >= len)
return false;
return line[i] == '}' || line[i] == ']' || line[i] == ')';
}
void indent_line(Editor *editor, uint32_t row) {
if (!editor)
return;
LineIterator *it = begin_l_iter(editor->root, row);
uint32_t line_len;
char *line = next_line(it, &line_len);
if (!line) {
free(it->buffer);
free(it);
return;
}
char *spaces = (char *)malloc(INDENT_WIDTH);
memset(spaces, ' ', INDENT_WIDTH);
Coord cursor = editor->cursor;
edit_insert(editor, {row, 0}, spaces, INDENT_WIDTH);
editor->cursor = cursor;
free(spaces);
free(it->buffer);
free(it);
}
void dedent_line(Editor *editor, uint32_t row) {
if (!editor)
return;
LineIterator *it = begin_l_iter(editor->root, row);
uint32_t line_len;
char *line = next_line(it, &line_len);
if (!line) {
free(it->buffer);
free(it);
return;
}
uint32_t indent = leading_indent(line, line_len);
if (indent == 0) {
free(it->buffer);
free(it);
return;
}
uint32_t remove = indent >= INDENT_WIDTH ? INDENT_WIDTH : indent;
edit_erase(editor, {row, 0}, remove);
free(it->buffer);
free(it);
}

View File

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

View File

@@ -137,6 +137,60 @@ void capture_mouse(char *buf, KeyEvent *ret) {
}
}
bool read_bracketed_paste(char **out, int *out_len) {
size_t cap = 256, len = 0;
char *buf = (char *)malloc(cap);
if (!buf)
return false;
char window[6] = {0};
size_t wlen = 0;
auto push_byte = [&](char c) {
if (len + 1 >= cap) {
cap *= 2;
buf = (char *)realloc(buf, cap);
}
buf[len++] = c;
};
while (true) {
char c;
if (!get_next_byte(&c)) {
free(buf);
return false;
}
if (wlen < 5) {
window[wlen++] = c;
} else {
memmove(window, window + 1, 4);
window[4] = c;
wlen = 5;
}
if (wlen == 5 && window[0] == '\x1b' && window[1] == '[' &&
window[2] == '2' && window[3] == '0' && window[4] == '1') {
char tilde;
if (!get_next_byte(&tilde)) {
free(buf);
return false;
}
if (tilde == '~')
break;
for (int i = 0; i < 5; i++)
push_byte(window[i]);
push_byte(tilde);
wlen = 0;
continue;
}
if (wlen == 5) {
push_byte(window[0]);
memmove(window, window + 1, 4);
wlen = 4;
}
}
buf[len] = '\0';
*out = buf;
*out_len = (int)len;
return true;
}
KeyEvent read_key() {
KeyEvent ret;
char *buf;
@@ -145,10 +199,21 @@ KeyEvent read_key() {
ret.key_type = KEY_NONE;
return ret;
}
if (buf[0] == '\x1b' && buf[1] == '[' && buf[2] == 'M') {
if (n >= 6 && buf[0] == '\x1b' && buf[1] == '[' && buf[2] == '2' &&
buf[3] == '0' && buf[4] == '0' && buf[5] == '~') {
char *pbuf = nullptr;
int plen = 0;
if (read_bracketed_paste(&pbuf, &plen)) {
free(buf);
ret.key_type = KEY_PASTE;
ret.c = pbuf;
ret.len = plen;
return ret;
}
} else if (n >= 3 && buf[0] == '\x1b' && buf[1] == '[' && buf[2] == 'M') {
ret.key_type = KEY_MOUSE;
capture_mouse(buf, &ret);
} else if (buf[0] == '\x1b' && buf[1] == '[') {
} else if (n >= 2 && buf[0] == '\x1b' && buf[1] == '[') {
ret.key_type = KEY_SPECIAL;
int using_modifiers = buf[3] == ';';
int pos;

View File

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

View File

@@ -9,7 +9,7 @@ std::mutex screen_mutex;
termios orig_termios;
void disable_raw_mode() {
std::string os = "\x1b[?1049l\x1b[2 q\x1b[?1002l\x1b[?25h";
std::string os = "\x1b[?1049l\x1b[2 q\x1b[?1002l\x1b[?25h\x1b[?2004l";
write(STDOUT_FILENO, os.c_str(), os.size());
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) {
perror("tcsetattr");
@@ -30,7 +30,7 @@ void enable_raw_mode() {
raw.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
exit(EXIT_FAILURE);
std::string os = "\x1b[?1049h\x1b[2 q\x1b[?1002h\x1b[?25l";
std::string os = "\x1b[?1049h\x1b[2 q\x1b[?1002h\x1b[?25l\x1b[?2004h";
write(STDOUT_FILENO, os.c_str(), os.size());
}