diff --git a/src/basic/basic.h b/src/basic/basic.h index 6b54338..d37c2f1 100644 --- a/src/basic/basic.h +++ b/src/basic/basic.h @@ -199,29 +199,6 @@ For(arr.reverse_iter()) { defer{ arr.unordered_remove(it); }; } -// -// When double looping and deleting -// - -Array deleted_cursors = {scratch}; -ForItem(cursor, window->cursors) { - For(deleted_cursors) if (it == &cursor) goto end_of_cursor_loop; - - For(window->cursors) { - if (&it == &cursor) break; - bool a = cursor.range.max >= it.range.min && cursor.range.max <= it.range.max; - bool b = cursor.range.min >= it.range.min && cursor.range.min <= it.range.max; - if (a || b) { - deleted_cursors.add(&it); - cursor.range.max = Max(cursor.range.max, it.range.max); - cursor.range.min = Min(cursor.range.min, it.range.min); - break; - } - } -end_of_cursor_loop:; -} - -For(deleted_cursors) window->cursors.ordered_remove(*it); */ @@ -258,6 +235,21 @@ struct Slice { // @copy_paste array slice + bool contains(T item) { + for (int64_t i = 0; i < len; i += 1) { + if (data[i] == item) return true; + } + return false; + } + + T get_or_default(int64_t i, T default_value = {}) { + T result = default_value; + if (i >= 0 && i < len) { + result = data[i]; + } + return result; + } + T pop() { Assert(len > 0); return data[--len]; @@ -531,6 +523,13 @@ struct Array { // @copy_paste array slice + bool contains(T item) { + for (int64_t i = 0; i < len; i += 1) { + if (data[i] == item) return true; + } + return false; + } + T get_or_default(int64_t i, T default_value = {}) { T result = default_value; if (i >= 0 && i < len) { diff --git a/src/text_editor/layout.cpp b/src/text_editor/layout.cpp index 94d94ce..833af44 100644 --- a/src/text_editor/layout.cpp +++ b/src/text_editor/layout.cpp @@ -250,16 +250,17 @@ void BeforeEdit(Window *window) { Scratch scratch; // Merge cursors that overlap, this needs to be handled before any edits to // make sure overlapping edits won't happen. - // @optimize: could probably reverse the inner loop and simply unordered remove + + // @optimize @refactor: this is retarded, I hit so many array removal bugs here, without allocation please Array deleted_cursors = {scratch}; ForItem(cursor, window->cursors) { - For(deleted_cursors) if (it == &cursor) goto end_of_cursor_loop; + if (deleted_cursors.contains(&cursor)) goto end_of_cursor_loop; For(window->cursors) { - if (&it == &cursor) break; + if (&it == &cursor) continue; bool a = cursor.range.max >= it.range.min && cursor.range.max <= it.range.max; bool b = cursor.range.min >= it.range.min && cursor.range.min <= it.range.max; - if (a || b) { + if ((a || b) && !deleted_cursors.contains(&it)) { deleted_cursors.add(&it); cursor.range.max = Max(cursor.range.max, it.range.max); cursor.range.min = Min(cursor.range.min, it.range.min); @@ -269,7 +270,14 @@ void BeforeEdit(Window *window) { end_of_cursor_loop:; } - For(deleted_cursors) window->cursors.ordered_remove(*it); + Array new_cursors = {window->cursors.allocator}; + For(window->cursors) { + if (deleted_cursors.contains(&it) == false) { + new_cursors.add(it); + } + } + window->cursors.dealloc(); + window->cursors = new_cursors; } void AfterEdit(Window *window, Array edits) { diff --git a/src/text_editor/main.cpp b/src/text_editor/main.cpp index ac2a6ab..97ecab4 100644 --- a/src/text_editor/main.cpp +++ b/src/text_editor/main.cpp @@ -5,6 +5,7 @@ // @todo: add history (undo, redo) // @todo: add clipboard history? +// @todo: mouse double click #include "rect2.cpp" #include "buffer.cpp" #include "layout.cpp" @@ -75,6 +76,7 @@ int main() { } } + window.cursors.allocator = GetSystemAllocator(); window.cursors.add({}); AfterEdit(&window, {}); @@ -164,9 +166,8 @@ int main() { front = MoveDown(focused_window->buffer, front); cursors_to_add.add({front, front}); } - For(cursors_to_add) { - focused_window->cursors.add(it); - } + For(cursors_to_add) focused_window->cursors.add(it); + BeforeEdit(focused_window); // Merge cursors } else if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyDown(KEY_LEFT_ALT)) { // Default in VSCode seems to be Shift + Alt + down BeforeEdit(focused_window); Array edits = {FrameArena}; @@ -203,6 +204,7 @@ int main() { cursors_to_add.add({front, front}); } For(cursors_to_add) focused_window->cursors.add(it); + BeforeEdit(focused_window); // Merge cursors } else if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyDown(KEY_LEFT_ALT)) { // Default in VSCode seems to be Shift + Alt + up BeforeEdit(focused_window); Array edits = {FrameArena}; @@ -238,6 +240,7 @@ int main() { if (Seek(to_seek, needle, &found_index, SeekFlag_IgnoreCase)) { found_index += cursor.range.max; focused_window->cursors.add(MakeCursor(found_index + needle.len, found_index)); + BeforeEdit(focused_window); // Merge cursors } } @@ -359,6 +362,14 @@ int main() { AfterEdit(focused_window, edits); } + if (IsKeyPressed(KEY_TAB) || IsKeyPressedRepeat(KEY_TAB)) { + BeforeEdit(focused_window); + Array edits = {FrameArena}; + For(focused_window->cursors) AddEdit(&edits, it.range, " "); + ApplyEdits(&focused_window->buffer, edits); + AfterEdit(focused_window, edits); + } + if (IsKeyPressed(KEY_ENTER) || IsKeyPressedRepeat(KEY_ENTER)) { if (IsKeyDown(KEY_LEFT_CONTROL)) { For(focused_window->cursors) { @@ -446,6 +457,7 @@ int main() { window.cursors.clear(); } window.cursors.add({col->pos, col->pos}); + BeforeEdit(&window); // Merge cursors window.mouse_selecting = true; }