Update preffered cursor column for edits properly

This commit is contained in:
2025-12-12 20:47:28 +00:00
parent 39542bac33
commit fd70a40a49
2 changed files with 56 additions and 12 deletions

View File

@@ -6,23 +6,23 @@ A TUI IDE.
# TODO
- [ ] Add `hooks` in files that can be set/unset/jumped to.
- [ ] Add support for text selection (slect range / all).
- [ ] Make function to get selected text. (selection itself is done)
- [ ] Add modes for editing - insert, select, normal, etc.
- [ ] Add line numbers.
- [ ] Add bg highlight for current line.
- [ ] Add support for copy/cut/paste.
- [ ] Add support for ctrl + arrow key for moving words.
- [ ] Add support for ctrl + backspace / delete for deleting words.
- [ ] Add underline highlight for current word and all occurences.
- [ ] Add bg highlight for current line.
- [ ] Add line numbers.
- [ ] Add support for copy/cut/paste.
- [ ] Add mouse support.
- [ ] Add modes for editing - insert, select, normal, etc.
- [ ] Add `hooks` in files that can be set/unset/jumped to.
- [ ] Add folding support at tree-sitter level (basic folding is done).
- [ ] Add feature where doing enter uses tree-sitter to add newline with indentation.
1. it should also put stuff like `}` on the next line.
- it should also put stuff like `}` on the next line.
- [ ] Add support for brackets/quotes to auto-close.
- [ ] Add support for LSP.
- [ ] Add support for virtual cursor where edits apply at all the places.
- [ ] Add search / replace along with search / virtual cursors
- [ ] Add scm files for all the supported languages. (2/14) Done.
- [ ] Add codeium/copilot support.
- [ ] 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 support for LSP & autocomplete / snippets.
- [ ] Add codeium/copilot support.

View File

@@ -28,7 +28,7 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
editor->root = load(str, len, optimal_chunk_size(len));
free(str);
editor->folded.resize(editor->root->line_count + 2);
if (len < (1024 * 64)) {
if (len < (1024 * 128)) {
editor->parser = ts_parser_new();
Language language = language_for_file(filename);
editor->language = language.fn();
@@ -536,11 +536,33 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col;
if (cursor_original > start && cursor_original <= byte_pos) {
editor->cursor = point;
LineIterator *it = begin_l_iter(editor->root, point.row);
if (!it)
return;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
editor->cursor_preffered =
get_visual_col_from_bytes(line, line_len, point.col);
free(line);
} else if (cursor_original > byte_pos) {
uint32_t cursor_new = cursor_original - (byte_pos - start);
uint32_t new_col;
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
editor->cursor = {new_row, new_col};
LineIterator *it = begin_l_iter(editor->root, new_row);
if (!it)
return;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
editor->cursor_preffered =
get_visual_col_from_bytes(line, line_len, new_col);
free(line);
}
lock_1.unlock();
std::unique_lock lock_2(editor->knot_mtx);
@@ -572,11 +594,33 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col;
if (cursor_original > byte_pos && cursor_original <= end) {
editor->cursor = pos;
LineIterator *it = begin_l_iter(editor->root, pos.row);
if (!it)
return;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
editor->cursor_preffered =
get_visual_col_from_bytes(line, line_len, pos.col);
free(line);
} else if (cursor_original > end) {
uint32_t cursor_new = cursor_original - (end - byte_pos);
uint32_t new_col;
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
editor->cursor = {new_row, new_col};
LineIterator *it = begin_l_iter(editor->root, new_row);
if (!it)
return;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
editor->cursor_preffered =
get_visual_col_from_bytes(line, line_len, new_col);
free(line);
}
lock_1.unlock();
std::unique_lock lock_2(editor->knot_mtx);