From 7ae4e74f5069442077cffbd5d62d77479c713eec Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 25 Jan 2026 08:54:00 +0100 Subject: [PATCH] SLOW_BUILD, 2 additional build targets, improve word complete --- build.bat | 2 ++ src/basic/basic_array.h | 12 ++++++- src/basic/basic_head.h | 11 +++++- src/text_editor/buffer.cpp | 2 +- src/text_editor/coroutines.cpp | 2 +- src/text_editor/globals.cpp | 22 +++++++----- src/text_editor/plugin_build_window.cpp | 8 +++++ src/text_editor/plugin_word_complete.cpp | 45 +++++++++++++++--------- src/text_editor/text_editor.cpp | 4 +-- 9 files changed, 78 insertions(+), 30 deletions(-) diff --git a/build.bat b/build.bat index e91b60f..0cbdf40 100755 --- a/build.bat +++ b/build.bat @@ -6,6 +6,7 @@ for %%a in (%*) do set "%%~a=1" if not "%release%"=="1" set debug=1 if "%debug%"=="1" set release=0 && echo [debug mode] if "%release%"=="1" set debug=0 && echo [release mode] +if "%slow%"=="1" echo [slow mode] if "%package%"=="1" echo [package zip] if not exist "src\external\SDL" ( @@ -28,6 +29,7 @@ pushd build set flags=/EHsc- /MT /Zi /FC /nologo /WX /W3 /wd4200 /wd4334 /diagnostics:column if "%release%"=="1" set flags=%flags% -O2 -DDEBUG_BUILD=0 +if "%slow%"=="1" set flags=%flags% -DSLOW_BUILD=1 set libs=%sdllib%/SDL3-static.lib %sdllib%/SDL_uclibc.lib kernel32.lib gdi32.lib user32.lib Imm32.lib ole32.lib Shell32.lib OleAut32.lib Cfgmgr32.lib Setupapi.lib Advapi32.lib version.lib winmm.lib rem rc ..\data\icon.rc diff --git a/src/basic/basic_array.h b/src/basic/basic_array.h index ae56b83..84c8a94 100644 --- a/src/basic/basic_array.h +++ b/src/basic/basic_array.h @@ -165,7 +165,7 @@ Slice GetSlice(Slice &arr, int64_t first_index = 0, int64_t one_past_last_ } // Make arrays resize on every item -#define ARRAY_DEBUG 0 +#define ARRAY_DEBUG BUILD_SLOW #if ARRAY_DEBUG #define ARRAY_IF_DEBUG_ELSE(IF, ELSE) IF #else @@ -310,6 +310,16 @@ bool Contains(Array &arr, T item) { return false; } +template +int64_t FindValueIndex(Array &arr, T item) { + for (int64_t i = 0; i < arr.len; i += 1) { + if (arr.data[i] == item) { + return i; + } + } + return -1; +} + template int64_t GetIndex(Array &arr, const T &item) { ptrdiff_t index = (ptrdiff_t)(&item - arr.data); diff --git a/src/basic/basic_head.h b/src/basic/basic_head.h index 10b5bc1..46d9a1c 100644 --- a/src/basic/basic_head.h +++ b/src/basic/basic_head.h @@ -9,7 +9,6 @@ #include #include - #if defined(__APPLE__) && defined(__MACH__) #define OS_POSIX 1 #define OS_MAC 1 @@ -70,6 +69,16 @@ #define DEBUG_BUILD 1 #endif +#ifndef SLOW_BUILD + #define SLOW_BUILD 0 +#endif + +#if SLOW_BUILD + #define IF_SLOW_BUILD(x) x +#else + #define IF_SLOW_BUILD(x) +#endif + #if DEBUG_BUILD #define IF_DEBUG(x) x #else diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 693540f..6711d6a 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -1,4 +1,4 @@ -#define BUFFER_DEBUG DEBUG_BUILD +#define BUFFER_DEBUG SLOW_BUILD API bool AreEqual(XY a, XY b) { bool result = a.x == b.x && a.y == b.y; diff --git a/src/text_editor/coroutines.cpp b/src/text_editor/coroutines.cpp index c0879b8..9563687 100644 --- a/src/text_editor/coroutines.cpp +++ b/src/text_editor/coroutines.cpp @@ -62,7 +62,7 @@ void CoUpdate(Event *event) { } } -#if DEBUG_BUILD +#if SLOW_BUILD //:CoroutineLeakCheck // Make sure we don't leak scratch arenas between coroutine boundaries. // it leads to memory corruption! If you hit Assert here make sure you diff --git a/src/text_editor/globals.cpp b/src/text_editor/globals.cpp index 2273d1d..84741f8 100644 --- a/src/text_editor/globals.cpp +++ b/src/text_editor/globals.cpp @@ -10,10 +10,9 @@ bool SearchCaseSensitive = false; bool SearchWordBoundary = false; bool BreakOnError = false; Int ErrorCount; -#if 1 -String16 InitialScratchContent; -#else -String16 InitialScratchContent = uR"==(C:/text_editor/src/text_editor/text_editor.cpp +#if DEBUG_BUILD +String16 InitialScratchContent = uR"==(:OpenProject +C:/text_editor/src/text_editor/text_editor.cpp 0 1 2 @@ -21,6 +20,8 @@ String16 InitialScratchContent = uR"==(C:/text_editor/src/text_editor/text_edito 4 5 6)=="; +#else +String16 InitialScratchContent; #endif Allocator SysAllocator = {SystemAllocatorProc}; @@ -187,10 +188,15 @@ RegisterVariable(Int, TrimTrailingWhitespace, 1); RegisterVariable(String, ProjectDirectory, ""); // PLUGIN_BUILD_WINDOW -RegisterVariable(String, Build1OnWindows, "build.bat"); -RegisterVariable(String, Build1OnUnix, "sh build.sh"); -RegisterVariable(String, Build2OnWindows, "build.bat release"); -RegisterVariable(String, Build2OnUnix, "sh build.sh release"); +RegisterVariable(String, Build1OnWindows, "build.bat slow"); +RegisterVariable(String, Build1OnUnix, "sh build.sh slow"); +RegisterVariable(String, Build2OnWindows, "build.bat"); +RegisterVariable(String, Build2OnUnix, "sh build.sh"); +RegisterVariable(String, Build3OnWindows, "build.bat release"); +RegisterVariable(String, Build3OnUnix, "sh build.sh release"); +RegisterVariable(String, Build4OnWindows, "build.bat release"); +RegisterVariable(String, Build4OnUnix, "sh build.sh release"); + // PLUGIN_LOAD_VCVARS RegisterVariable(String, VCVarsPath, "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat"); diff --git a/src/text_editor/plugin_build_window.cpp b/src/text_editor/plugin_build_window.cpp index 32e3a0a..ae76170 100644 --- a/src/text_editor/plugin_build_window.cpp +++ b/src/text_editor/plugin_build_window.cpp @@ -56,6 +56,14 @@ void CMD_Build2() { ExecBuild(Build2OnWindows, Build2OnUnix); } RegisterCommand(CMD_Build2, "f2", "Run Build2OnWindows or OnUnix in working directory, output is printed in a popup console and a special build buffer"); +void CMD_Build3() { + ExecBuild(Build3OnWindows, Build3OnUnix); +} RegisterCommand(CMD_Build3, "f3", "Run Build3OnWindows or OnUnix in working directory, output is printed in a popup console and a special build buffer"); + +void CMD_Build4() { + ExecBuild(Build4OnWindows, Build4OnUnix); +} RegisterCommand(CMD_Build4, "f4", "Run Build4OnWindows or OnUnix in working directory, output is printed in a popup console and a special build buffer"); + void CMD_RunFile() { Scratch scratch; BSet primary = GetBSet(PrimaryWindowID); diff --git a/src/text_editor/plugin_word_complete.cpp b/src/text_editor/plugin_word_complete.cpp index 3ca31c1..0ca2819 100644 --- a/src/text_editor/plugin_word_complete.cpp +++ b/src/text_editor/plugin_word_complete.cpp @@ -26,11 +26,13 @@ struct StringAndDistance { Int distance; }; -bool Contains(Array *arr, String16 string) { - For (*arr) { - if (it.string == string) return true; +Int FindValueIndex(Array *arr, String16 string) { + for (int64_t i = 0; i < arr->len; i += 1) { + if (arr->data[i].string == string) { + return i; + } } - return false; + return -1; } inline bool MergeSortCompare(StringAndDistance *EntryA, StringAndDistance *EntryB) { @@ -58,10 +60,17 @@ void CWSLexIdentifiers(Array *out_idents, Buffer *buffer) { if (token.len <= 0) { break; } - if (StartsWith(token, CWS.prefix_string) && token != CWS.prefix_string && !Contains(&idents, token)) { + if (StartsWith(token, CWS.prefix_string) && token != CWS.prefix_string) { Int pos = token.data - buffer->str; Int distance = Absolute(CWS.original_caret_pos - pos); - Add(&idents, {Copy16(CWS.arena, token), distance}); + int64_t value_index = FindValueIndex(&idents, token); + if (value_index != -1) { + if (idents[value_index].distance > distance) { + idents[value_index].distance = distance; + } + } else { + Add(&idents, {Copy16(CWS.arena, token), distance}); + } } } @@ -70,11 +79,11 @@ void CWSLexIdentifiers(Array *out_idents, Buffer *buffer) { MergeSort(idents.len, idents.data, temp.data); } For (idents) { - if (!Contains(out_idents, it.string)) Add(out_idents, it); + if (FindValueIndex(out_idents, it.string) == -1) Add(out_idents, it); } } -void CompleteWord2(mco_coro *co) { +void WordComplete(mco_coro *co) { Array buffers = {CWS.arena}; { Add(&buffers, CWS.buffer->id); @@ -132,10 +141,14 @@ void CompleteWord2(mco_coro *co) { } } -void CompleteWord(View *view, Int pos) { +void WordComplete(View *view, Int pos) { Buffer *buffer = GetBuffer(view->active_buffer); Range prefix_string_range = {GetWordStart(buffer, pos), pos}; String16 prefix = GetString(buffer, prefix_string_range); + if (prefix == u"") { + return; + } + bool continue_with_previous = CWS.co && CWS.last_string == prefix && CWS.buffer == buffer; if (!continue_with_previous) { if (CWS.co) { @@ -151,7 +164,7 @@ void CompleteWord(View *view, Int pos) { CWS.original_caret_pos = pos; CWS.prefix_string = Copy16(CWS.arena, prefix); - mco_desc desc = mco_desc_init(CompleteWord2, NULL); + mco_desc desc = mco_desc_init(WordComplete, NULL); mco_result res = mco_create(&CWS.co, &desc); Assert(res == MCO_SUCCESS); } @@ -185,14 +198,14 @@ void CompletePrevWord(View *view, Int pos) { } } -void CMD_CompleteWord() { +void CMD_WordComplete() { BSet active = GetBSet(ActiveWindowID); bool ok = active.view->carets.len == 1 && GetSize(active.view->carets[0].range) == 0; if (!ok) { return; } - CompleteWord(active.view, active.view->carets[0].range.min); -} RegisterCommand(CMD_CompleteWord, "", "Completes the current word"); + WordComplete(active.view, active.view->carets[0].range.min); +} RegisterCommand(CMD_WordComplete, "", "Completes the current word"); void CMD_CompletePrevWord() { BSet active = GetBSet(ActiveWindowID); @@ -212,11 +225,11 @@ void CMD_CompletePrevWordOrDedent() { } } RegisterCommand(CMD_CompletePrevWordOrDedent, "shift-tab", "Completes the current word or it indents it, when single caret with no selection it goes for word complete"); -void CMD_CompleteWordOrIndent() { +void CMD_WordCompleteOrIndent() { BSet active = GetBSet(ActiveWindowID); if (active.view->carets.len == 1 && GetSize(active.view->carets[0].range) == 0) { - CMD_CompleteWord(); + CMD_WordComplete(); } else { IndentSelectedLines(active.view); } -} RegisterCommand(CMD_CompleteWordOrIndent, "tab", "Completes the current word or it indents it, when single caret with no selection it goes for word complete"); \ No newline at end of file +} RegisterCommand(CMD_WordCompleteOrIndent, "tab", "Completes the current word or it indents it, when single caret with no selection it goes for word complete"); \ No newline at end of file diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 57273fb..56b7125 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -459,8 +459,8 @@ void OnCommand(Event event) { } } - IF_DEBUG(AssertRanges(main.view->carets)); - IF_DEBUG(AssertRanges(active.view->carets)); + IF_SLOW_BUILD(AssertRanges(main.view->carets)); + IF_SLOW_BUILD(AssertRanges(active.view->carets)); } void EvalCommand(String command) {