Port Seek word, add ctrl+left

This commit is contained in:
Krzosa Karol
2024-06-23 09:17:54 +02:00
parent c0d69521fe
commit 10afbd3e4f
2 changed files with 86 additions and 16 deletions

View File

@@ -105,6 +105,21 @@ int64_t MoveUp(Buffer &buffer, int64_t pos) {
return new_pos;
}
void UpdateCursorsAfterEdit(Window *window, Array<Edit> 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<Edit> 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);
}
}