From edc941bf5350925c9c050aad0dbe8aa2bb0b6805 Mon Sep 17 00:00:00 2001 From: krzosa Date: Tue, 23 Dec 2025 08:14:22 +0100 Subject: [PATCH] Make OpenCode into coroutine --- src/text_editor/commands.cpp | 53 ++++++++++++++++++++---------- src/text_editor/coroutines.cpp | 13 +++++++- src/text_editor/text_editor.cpp | 2 +- src/text_editor/text_editor.h | 1 + src/text_editor/window_command.cpp | 3 +- 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index ac514e9..9f24302 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -646,34 +646,51 @@ void Command_SetWorkDir() { } RegisterCommand(Command_SetWorkDir, ""); String CodeEndings[] = { ".c", ".cpp", ".h", ".hpp", ".py", ".lua", ".cxx", ".hxx", }; +String Coro_OpenCodeDir; -void OpenCodeRecursive(String dir) { - ProfileFunction(); - Scratch scratch; - for (FileIter it = IterateFiles(scratch, dir); IsValid(it); Advance(&it)) { - if (it.filename == ".git") { - continue; - } +void Coro_OpenCode(mco_coro *co) { + BlockArena arena = {}; + Array dirs = {arena}; + Add(&dirs, Coro_OpenCodeDir); + int i = 0; + for (int diri = 0; diri < dirs.len; diri += 1) { + for (FileIter it = IterateFiles(arena, dirs[diri]); IsValid(it); Advance(&it)) { + if (it.filename == ".git") { + continue; + } - if (it.is_directory) { - OpenCodeRecursive(it.absolute_path); - } else { - bool match = false; - ForItem (ending, CodeEndings) { - if (EndsWith(it.absolute_path, ending)) { - match = true; - break; + if (it.is_directory) { + Add(&dirs, it.absolute_path); + } else { + bool match = false; + ForItem (ending, CodeEndings) { + if (EndsWith(it.absolute_path, ending)) { + match = true; + break; + } + } + if (match) { + BufferOpenFile(it.absolute_path); } } - if (match) { - BufferOpenFile(it.absolute_path); + + + if ((i%16) == 0) { + CoYield(co); } + i += 1; } } + Release(&arena); +} + +void OpenCode(String dir) { + Coro_OpenCodeDir = dir; + CoAdd(Coro_OpenCode); } void Command_OpenCode() { - OpenCodeRecursive(WorkDir); + OpenCode(WorkDir); } RegisterCommand(Command_OpenCode, ""); void Command_KillProcess() { diff --git a/src/text_editor/coroutines.cpp b/src/text_editor/coroutines.cpp index d854551..d72f14a 100644 --- a/src/text_editor/coroutines.cpp +++ b/src/text_editor/coroutines.cpp @@ -1,4 +1,4 @@ -typedef void CoroutineProc(mco_coro *co); +typedef void CoroutineProc(mco_coro *co); Array ActiveCoroutines; mco_coro *CoAdd(CoroutineProc *proc) { @@ -18,6 +18,7 @@ mco_coro *CoAdd(CoroutineProc *proc) { void CoUpdate(Event *event) { ProfileFunction(); + double start = GetTimeSeconds(); IterRemove(ActiveCoroutines) { IterRemovePrepare(ActiveCoroutines); @@ -34,6 +35,11 @@ void CoUpdate(Event *event) { remove_item = true; } } + + double took = GetTimeSeconds() - start; + if (took > 0.01) { + break; + } } } @@ -47,3 +53,8 @@ Event *CoYield(mco_coro *co) { return event; } + +void CoRemove(mco_coro *co) { + UnorderedRemove(&ActiveCoroutines, co); + mco_destroy(co); +} diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index c2c020f..5a325e1 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -23,6 +23,7 @@ #include "text_editor.h" #include "globals.cpp" +#include "coroutines.cpp" #include "buffer.cpp" #include "view.cpp" #include "window.cpp" @@ -39,7 +40,6 @@ #include "commands_clipboard.cpp" #include "draw.cpp" -#include "coroutines.cpp" #include "test/tests.cpp" diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 8818c04..db458e0 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -186,3 +186,4 @@ struct Register_Variable { #define RegisterVariable(type, name, ...) \ type name = __VA_ARGS__; \ Register_Variable var_##name(&Variables, VariableType_##type, #name, &name) + diff --git a/src/text_editor/window_command.cpp b/src/text_editor/window_command.cpp index d3e19bc..d4b60aa 100644 --- a/src/text_editor/window_command.cpp +++ b/src/text_editor/window_command.cpp @@ -24,10 +24,9 @@ void CommandWindowLayout(Rect2I *rect, Int wx, Int wy) { rect = ©_rect; } Int barsize = Clamp((Int)n->font->line_spacing*10, (Int)0, (Int)wx - 100); - n->document_rect = n->total_rect = CutTop(rect, barsize); + n->document_rect = n->total_rect = CutBottom(rect, barsize); } - const Int FuzzyCloserWordBegin = 5; const Int FuzzyConsecutiveMultiplier = 3; Int FuzzyRate(String16 string, String16 with) {