From c0d69521fe6f9394c4b52853de7a0d17d632c206 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 23 Jun 2024 07:53:30 +0200 Subject: [PATCH] More proper offseting of cursors during char insert --- src/text_editor/main.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/text_editor/main.cpp b/src/text_editor/main.cpp index dd6e070..9c006ad 100644 --- a/src/text_editor/main.cpp +++ b/src/text_editor/main.cpp @@ -209,11 +209,16 @@ int main() { AddEdit(&edits, it, string); } ApplyEdits(&focused_window->buffer, edits); - For(focused_window->cursors) { - // Need to update current cursor and all cursors after it. + + ForItem(edit, edits) { + int64_t remove_size = GetRangeSize(edit.range); + int64_t add_size = string.len; + int64_t offset = add_size - remove_size; + ForItem(cursor, focused_window->cursors) { - if (cursor.min >= it.min) { - cursor.min = cursor.max = MoveRight(focused_window->buffer, cursor.min); + if (edit.range.min <= cursor.min) { + cursor.min += offset; + cursor.max = cursor.min; } } }