Compare commits
4 Commits
f347f50dbe
...
a12e2fb1c4
| Author | SHA1 | Date | |
|---|---|---|---|
|
a12e2fb1c4
|
|||
|
7307387f64
|
|||
|
8002012705
|
|||
|
d5145d88a7
|
43
README.md
43
README.md
@@ -6,21 +6,48 @@ A TUI IDE.
|
|||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
- [ ] Add feature where doing enter uses tree-sitter to add newline with indentation.
|
- [ ] Add a virtual text support (text that is rendered in the editor but not in the actual text)
|
||||||
- it should also put stuff like `}` on the next line.
|
- Add a whitespace highlighter (nerd font). for spaces and tabs at start/end of line. as a test.
|
||||||
- [ ] Add the highlight of block edges when cursor is on a bracket (or in).
|
- [ ] Add support for LSP & autocomplete / snippets.
|
||||||
- [ ] Add this thing where selection double click on a bracket selects whole block.
|
- First research
|
||||||
- (only on the first time) and sets mode to `WORD`.
|
- `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 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.
|
||||||
- [ ] Redo folding system and its relation to move_line_* functions.
|
- [ ] 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)
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
|
|
||||||
#define EXTRA_META 4
|
#define EXTRA_META 4
|
||||||
|
|
||||||
|
#define INDENT_WIDTH 2
|
||||||
|
|
||||||
struct Highlight {
|
struct Highlight {
|
||||||
uint32_t fg;
|
uint32_t fg;
|
||||||
uint32_t bg;
|
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_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);
|
Editor *new_editor(const char *filename, Coord position, Coord size);
|
||||||
void free_editor(Editor *editor);
|
void free_editor(Editor *editor);
|
||||||
void render_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_up(Editor *editor, int32_t number);
|
||||||
void scroll_down(Editor *editor, uint32_t number);
|
void scroll_down(Editor *editor, uint32_t number);
|
||||||
void ensure_cursor(Editor *editor);
|
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 ensure_scroll(Editor *editor);
|
||||||
void handle_editor_event(Editor *editor, KeyEvent event);
|
void handle_editor_event(Editor *editor, KeyEvent event);
|
||||||
void edit_erase(Editor *editor, Coord pos, int64_t len);
|
void edit_erase(Editor *editor, Coord pos, int64_t len);
|
||||||
void edit_insert(Editor *editor, Coord pos, char *data, uint32_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);
|
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 editor_worker(Editor *editor);
|
||||||
void move_line_down(Editor *editor);
|
void move_line_down(Editor *editor);
|
||||||
void move_line_up(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_insertion(Editor *editor, uint32_t line, uint32_t rows);
|
||||||
void apply_line_deletion(Editor *editor, uint32_t removal_start,
|
void apply_line_deletion(Editor *editor, uint32_t removal_start,
|
||||||
uint32_t removal_end);
|
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
|
#endif
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -15,7 +15,8 @@
|
|||||||
#define KEY_CHAR 0
|
#define KEY_CHAR 0
|
||||||
#define KEY_SPECIAL 1
|
#define KEY_SPECIAL 1
|
||||||
#define KEY_MOUSE 2
|
#define KEY_MOUSE 2
|
||||||
#define KEY_NONE 3
|
#define KEY_PASTE 3
|
||||||
|
#define KEY_NONE 4
|
||||||
|
|
||||||
#define KEY_UP 0
|
#define KEY_UP 0
|
||||||
#define KEY_DOWN 1
|
#define KEY_DOWN 1
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -122,7 +122,7 @@ void render_editor(Editor *editor) {
|
|||||||
update(editor->position.row + rendered_rows, editor->position.col, "",
|
update(editor->position.row + rendered_rows, editor->position.col, "",
|
||||||
0xAAAAAA, 0, 0);
|
0xAAAAAA, 0, 0);
|
||||||
char buf[16];
|
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 =
|
uint32_t num_color =
|
||||||
editor->cursor.row == fold->start ? 0xFFFFFF : 0x555555;
|
editor->cursor.row == fold->start ? 0xFFFFFF : 0x555555;
|
||||||
for (int i = 0; i < len; i++)
|
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')
|
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++)
|
||||||
@@ -270,7 +270,7 @@ 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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
||||||
@@ -131,6 +131,8 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
|
|||||||
uint32_t target_visual_row = y;
|
uint32_t target_visual_row = y;
|
||||||
uint32_t visual_row = 0;
|
uint32_t visual_row = 0;
|
||||||
uint32_t line_index = editor->scroll.row;
|
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;
|
bool first_visual_line = true;
|
||||||
std::shared_lock knot_lock(editor->knot_mtx);
|
std::shared_lock knot_lock(editor->knot_mtx);
|
||||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
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);
|
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,9 +155,10 @@ 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++;
|
||||||
}
|
}
|
||||||
|
last_line_index = fold->end;
|
||||||
|
last_col = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
uint32_t line_len;
|
uint32_t line_len;
|
||||||
@@ -163,6 +167,8 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
|
|||||||
break;
|
break;
|
||||||
if (line_len && line[line_len - 1] == '\n')
|
if (line_len && line[line_len - 1] == '\n')
|
||||||
line_len--;
|
line_len--;
|
||||||
|
last_line_index = line_index;
|
||||||
|
last_col = line_len;
|
||||||
uint32_t offset = first_visual_line ? editor->scroll.col : 0;
|
uint32_t offset = first_visual_line ? editor->scroll.col : 0;
|
||||||
first_visual_line = false;
|
first_visual_line = false;
|
||||||
while (offset < line_len || (line_len == 0 && offset == 0)) {
|
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)
|
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};
|
||||||
}
|
}
|
||||||
@@ -186,8 +192,9 @@ Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
|
|||||||
left -= g;
|
left -= g;
|
||||||
col += w;
|
col += w;
|
||||||
}
|
}
|
||||||
|
last_col = last_good_offset;
|
||||||
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,11 +207,11 @@ 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 {last_line_index, last_col};
|
||||||
}
|
}
|
||||||
|
|
||||||
Coord move_right_pure(Editor *editor, Coord cursor, uint32_t number) {
|
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 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 +241,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 +255,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 +272,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 +284,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 +308,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 +323,59 @@ 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;
|
||||||
|
}
|
||||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||||
--line_len;
|
--line_len;
|
||||||
|
col = 0;
|
||||||
|
--number;
|
||||||
|
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 +391,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 +403,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 +415,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 +441,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 +454,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 +473,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 +489,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 +508,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 +544,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 +561,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 +595,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 +619,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 ||
|
||||||
@@ -661,6 +680,7 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
|||||||
uint32_t start_row = point.row;
|
uint32_t start_row = point.row;
|
||||||
uint32_t end_row = pos.row;
|
uint32_t end_row = pos.row;
|
||||||
apply_line_deletion(editor, start_row + 1, end_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);
|
std::unique_lock lock_2(editor->knot_mtx);
|
||||||
editor->root = erase(editor->root, start, byte_pos - start);
|
editor->root = erase(editor->root, start, byte_pos - start);
|
||||||
lock_2.unlock();
|
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 start_row = pos.row;
|
||||||
uint32_t end_row = point.row;
|
uint32_t end_row = point.row;
|
||||||
apply_line_deletion(editor, start_row + 1, end_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);
|
std::unique_lock lock_2(editor->knot_mtx);
|
||||||
editor->root = erase(editor->root, byte_pos, end - byte_pos);
|
editor->root = erase(editor->root, byte_pos, end - byte_pos);
|
||||||
lock_2.unlock();
|
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_line_insertion(editor, pos.row, rows);
|
||||||
|
apply_hook_insertion(editor, pos.row, rows);
|
||||||
if (editor->tree) {
|
if (editor->tree) {
|
||||||
TSInputEdit edit = {
|
TSInputEdit edit = {
|
||||||
.start_byte = byte_pos,
|
.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);
|
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);
|
std::shared_lock lock(editor->knot_mtx);
|
||||||
Coord start, end;
|
Coord start, end;
|
||||||
if (editor->cursor >= editor->selection) {
|
if (editor->cursor >= editor->selection) {
|
||||||
@@ -811,15 +833,18 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (out_start)
|
||||||
|
*out_start = start;
|
||||||
uint32_t start_byte =
|
uint32_t start_byte =
|
||||||
line_to_byte(editor->root, start.row, nullptr) + start.col;
|
line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||||
uint32_t end_byte = line_to_byte(editor->root, end.row, nullptr) + end.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);
|
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) {
|
||||||
@@ -943,3 +966,16 @@ void apply_line_deletion(Editor *editor, uint32_t removal_start,
|
|||||||
}
|
}
|
||||||
editor->folds.swap(updated);
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "../include/editor.h"
|
#include "../include/editor.h"
|
||||||
#include "../include/main.h"
|
#include "../include/main.h"
|
||||||
#include "../include/ts.h"
|
#include "../include/ts.h"
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
void handle_editor_event(Editor *editor, KeyEvent event) {
|
void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||||
static std::chrono::steady_clock::time_point last_click_time =
|
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;
|
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 +116,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;
|
||||||
@@ -203,6 +204,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
|||||||
switch (mode) {
|
switch (mode) {
|
||||||
case NORMAL:
|
case NORMAL:
|
||||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||||
|
Coord start = editor->cursor;
|
||||||
switch (event.c[0]) {
|
switch (event.c[0]) {
|
||||||
case 'u':
|
case 'u':
|
||||||
if (editor->root->line_count > 0) {
|
if (editor->root->line_count > 0) {
|
||||||
@@ -216,9 +218,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;
|
||||||
@@ -230,6 +232,8 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
|||||||
case 'a':
|
case 'a':
|
||||||
mode = INSERT;
|
mode = INSERT;
|
||||||
cursor_right(editor, 1);
|
cursor_right(editor, 1);
|
||||||
|
if (start.row != editor->cursor.row)
|
||||||
|
cursor_left(editor, 1);
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
mode = INSERT;
|
mode = INSERT;
|
||||||
@@ -282,6 +286,14 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
|||||||
scroll_up(editor, 1);
|
scroll_up(editor, 1);
|
||||||
ensure_cursor(editor);
|
ensure_cursor(editor);
|
||||||
break;
|
break;
|
||||||
|
case '>':
|
||||||
|
case '.':
|
||||||
|
indent_line(editor, editor->cursor.row);
|
||||||
|
break;
|
||||||
|
case '<':
|
||||||
|
case ',':
|
||||||
|
dedent_line(editor, editor->cursor.row);
|
||||||
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
char *text = get_from_clipboard(&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);
|
edit_insert(editor, editor->cursor, (char *)" ", 2);
|
||||||
cursor_right(editor, 2);
|
cursor_right(editor, 2);
|
||||||
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
|
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
|
||||||
edit_insert(editor, editor->cursor, (char *)"\n", 1);
|
uint32_t line_len = 0;
|
||||||
cursor_right(editor, 1);
|
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')) {
|
} else if (event.c[0] == CTRL('W')) {
|
||||||
uint32_t prev_col_byte, prev_col_cluster;
|
uint32_t prev_col_byte, prev_col_cluster;
|
||||||
word_boundaries(editor, editor->cursor, &prev_col_byte, nullptr,
|
word_boundaries(editor, editor->cursor, &prev_col_byte, nullptr,
|
||||||
@@ -341,10 +378,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 == ']') ||
|
||||||
@@ -357,8 +394,11 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
|||||||
edit_erase(editor, editor->cursor, -1);
|
edit_erase(editor, editor->cursor, -1);
|
||||||
}
|
}
|
||||||
} else if (event.c[0] == 0x1B) {
|
} else if (event.c[0] == 0x1B) {
|
||||||
|
Coord prev_pos = editor->cursor;
|
||||||
mode = NORMAL;
|
mode = NORMAL;
|
||||||
cursor_left(editor, 1);
|
cursor_left(editor, 1);
|
||||||
|
if (prev_pos.row != editor->cursor.row)
|
||||||
|
cursor_right(editor, 1);
|
||||||
}
|
}
|
||||||
} else if (event.len > 1) {
|
} else if (event.len > 1) {
|
||||||
edit_insert(editor, editor->cursor, event.c, event.len);
|
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);
|
edit_erase(editor, editor->cursor, next_col_cluster);
|
||||||
break;
|
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;
|
break;
|
||||||
case SELECT:
|
case SELECT:
|
||||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
char *text;
|
char *text;
|
||||||
|
Coord start;
|
||||||
switch (event.c[0]) {
|
switch (event.c[0]) {
|
||||||
case 'f':
|
case 'f':
|
||||||
if (editor->cursor.row != editor->selection.row) {
|
if (editor->cursor.row != editor->selection.row) {
|
||||||
@@ -405,16 +453,17 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
|||||||
mode = NORMAL;
|
mode = NORMAL;
|
||||||
break;
|
break;
|
||||||
case 'y':
|
case 'y':
|
||||||
text = get_selection(editor, &len);
|
text = get_selection(editor, &len, nullptr);
|
||||||
copy_to_clipboard(text, len);
|
copy_to_clipboard(text, len);
|
||||||
free(text);
|
free(text);
|
||||||
editor->selection_active = false;
|
editor->selection_active = false;
|
||||||
mode = NORMAL;
|
mode = NORMAL;
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
text = get_selection(editor, &len);
|
text = get_selection(editor, &len, &start);
|
||||||
copy_to_clipboard(text, len);
|
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);
|
free(text);
|
||||||
editor->selection_active = false;
|
editor->selection_active = false;
|
||||||
mode = NORMAL;
|
mode = NORMAL;
|
||||||
@@ -477,7 +526,7 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ensure_scroll(editor);
|
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);
|
free(event.c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
86
src/editor_indents.cc
Normal file
86
src/editor_indents.cc
Normal 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);
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
69
src/input.cc
69
src/input.cc
@@ -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 read_key() {
|
||||||
KeyEvent ret;
|
KeyEvent ret;
|
||||||
char *buf;
|
char *buf;
|
||||||
@@ -145,10 +199,21 @@ KeyEvent read_key() {
|
|||||||
ret.key_type = KEY_NONE;
|
ret.key_type = KEY_NONE;
|
||||||
return ret;
|
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;
|
ret.key_type = KEY_MOUSE;
|
||||||
capture_mouse(buf, &ret);
|
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;
|
ret.key_type = KEY_SPECIAL;
|
||||||
int using_modifiers = buf[3] == ';';
|
int using_modifiers = buf[3] == ';';
|
||||||
int pos;
|
int pos;
|
||||||
|
|||||||
58
src/knot.cc
58
src/knot.cc
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ std::mutex screen_mutex;
|
|||||||
termios orig_termios;
|
termios orig_termios;
|
||||||
|
|
||||||
void disable_raw_mode() {
|
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());
|
write(STDOUT_FILENO, os.c_str(), os.size());
|
||||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) {
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) {
|
||||||
perror("tcsetattr");
|
perror("tcsetattr");
|
||||||
@@ -30,7 +30,7 @@ void enable_raw_mode() {
|
|||||||
raw.c_cc[VTIME] = 0;
|
raw.c_cc[VTIME] = 0;
|
||||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
|
||||||
exit(EXIT_FAILURE);
|
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());
|
write(STDOUT_FILENO, os.c_str(), os.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user