From 10afbd3e4f230b064a130d4272bf8c46ff925a97 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 23 Jun 2024 09:17:54 +0200 Subject: [PATCH] Port Seek word, add ctrl+left --- src/text_editor/buffer.cpp | 49 +++++++++++++++++++++++++++++++++++ src/text_editor/main.cpp | 53 ++++++++++++++++++++++++++------------ 2 files changed, 86 insertions(+), 16 deletions(-) diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 9302102..25fb421 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -369,6 +369,55 @@ int64_t FindPos(Buffer &buffer, int64_t line_number, int64_t column) { return result; } +int64_t Seek(Buffer &buffer, int64_t pos, int64_t direction = ITERATE_FORWARD) { + Assert(direction == ITERATE_FORWARD || direction == ITERATE_BACKWARD); + // ( - inclusive + // < - non-inclusive + + int64_t min = 0; + int64_t max = 0; + char c = 0; + if (direction == ITERATE_FORWARD) { + // (pos + 1, end> + min = AdjustUTF8Pos(buffer, pos + 1, ITERATE_FORWARD); + max = buffer.len; + c = GetChar(buffer, min); + } else { + // (0, pos> + max = pos; + min = 0; + + int64_t next = AdjustUTF8Pos(buffer, max - 1, ITERATE_BACKWARD); + c = GetChar(buffer, next); + } + + bool standing_on_whitespace = IsWhitespace(c); + bool seek_whitespace = standing_on_whitespace == false; + bool seek_word = standing_on_whitespace; + + int64_t result = max; + BufferIter iter = Iterate(buffer, {min, max}, direction); + int64_t prev_pos = iter.pos; + for (; IsValid(iter); Advance(&iter)) { + bool char_is_whitespace = iter.item < 255 && IsWhitespace(iter.item); + + if (seek_word && char_is_whitespace == false) { + result = prev_pos; + break; + } + if (seek_whitespace && char_is_whitespace) { + if (direction == ITERATE_FORWARD) { + result = iter.pos; + } else { + result = prev_pos; + } + break; + } + prev_pos = iter.pos; + } + return result; +} + void RunBufferTests() { Scratch scratch; { diff --git a/src/text_editor/main.cpp b/src/text_editor/main.cpp index 9c006ad..1e4484e 100644 --- a/src/text_editor/main.cpp +++ b/src/text_editor/main.cpp @@ -105,6 +105,21 @@ int64_t MoveUp(Buffer &buffer, int64_t pos) { return new_pos; } +void UpdateCursorsAfterEdit(Window *window, Array edits) { + ForItem(edit, edits) { + int64_t remove_size = GetRangeSize(edit.range); + int64_t insert_size = edit.string.len; + int64_t offset = insert_size - remove_size; + + ForItem(cursor, window->cursors) { + if (edit.range.min <= cursor.min) { + cursor.min += offset; + cursor.max = cursor.min; + } + } + } +} + int main() { InitScratch(); RunBufferTests(); @@ -168,7 +183,11 @@ int main() { } if (IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) { For(focused_window->cursors) { - it.max = it.min = MoveRight(focused_window->buffer, it.min); + if (IsKeyDown(KEY_LEFT_CONTROL)) { + it.max = it.min = Seek(focused_window->buffer, it.min, ITERATE_FORWARD); + } else { + it.max = it.min = MoveRight(focused_window->buffer, it.min); + } } } if (IsKeyPressed(KEY_DOWN) || IsKeyPressedRepeat(KEY_DOWN)) { @@ -196,8 +215,22 @@ int main() { } } - // Handle user input chars - for (int c = GetCharPressed(); c; c = GetCharPressed()) { + if (IsKeyPressed(KEY_BACKSPACE) || IsKeyPressedRepeat(KEY_BACKSPACE)) { + Array edits = {FrameArena}; + String string = {}; + For(focused_window->cursors) { + int64_t pos = MoveLeft(focused_window->buffer, it.min); + AddEdit(&edits, {pos, it.min}, string); + } + ApplyEdits(&focused_window->buffer, edits); + UpdateCursorsAfterEdit(focused_window, edits); + } + + // Handle user input + for (;;) { + int c = GetCharPressed(); + if (!c) break; + String string = "?"; UTF8Result utf8 = UTF32ToUTF8((uint32_t)c); if (utf8.error == 0) { @@ -209,19 +242,7 @@ int main() { AddEdit(&edits, it, string); } ApplyEdits(&focused_window->buffer, edits); - - 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 (edit.range.min <= cursor.min) { - cursor.min += offset; - cursor.max = cursor.min; - } - } - } + UpdateCursorsAfterEdit(focused_window, edits); } }