From fd70a40a49394cc4b54394742410ed22731957c7 Mon Sep 17 00:00:00 2001 From: Syed Daanish Date: Fri, 12 Dec 2025 20:47:28 +0000 Subject: [PATCH] Update preffered cursor column for edits properly --- README.md | 22 +++++++++++----------- src/editor.cc | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 052dd88..07ab92c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/editor.cc b/src/editor.cc index b0e497b..1e8f8e0 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -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);