From 930620a49efaf79af5520739d6c6db823f01fa44 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 3 Feb 2026 21:10:33 +0100 Subject: [PATCH] Init variables, ReportErrorf now doesn't pop a message cause it doesn't do the queuing --- src/render/opengl.cpp | 4 +- src/text_editor/buffer.cpp | 2 +- src/text_editor/commands.cpp | 23 ++---- src/text_editor/config.cpp | 7 +- src/text_editor/coroutines.cpp | 2 +- src/text_editor/globals.cpp | 1 + src/text_editor/plugin_config.cpp | 11 --- src/text_editor/plugin_config.h | 1 + src/text_editor/plugin_remedybg.cpp | 2 +- src/text_editor/plugin_word_complete.cpp | 1 + src/text_editor/text_editor.cpp | 31 ++++---- src/text_editor/ui.cpp | 95 ++++++++++++++++++++++-- 12 files changed, 124 insertions(+), 56 deletions(-) diff --git a/src/render/opengl.cpp b/src/render/opengl.cpp index 806f2e7..4b2c516 100644 --- a/src/render/opengl.cpp +++ b/src/render/opengl.cpp @@ -73,10 +73,10 @@ static const char *glsl_fshader_es3 = R"==(#version 300 es } )=="; -void ReportWarningf(const char *fmt, ...); +void ReportErrorf(const char *fmt, ...); void GLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *user) { Unused(source); Unused(type); Unused(id); Unused(length); Unused(user); - ReportWarningf("OpenGL message: %s", message); + ReportErrorf("OpenGL message: %s", message); if (severity == GL_DEBUG_SEVERITY_HIGH || severity == GL_DEBUG_SEVERITY_MEDIUM) { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OpenGL error", message, NULL); } diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index a4c7800..26832ee 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -1557,7 +1557,7 @@ void SaveBuffer(Buffer *buffer) { buffer->dirty = false; buffer->temp = false; } else { - ReportWarningf("Failed to save file with name: %S", buffer->name); + ReportErrorf("Failed to save file with name: %S", buffer->name); } } diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 75980ae..83dbf21 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -67,6 +67,12 @@ void Appendf(View *view, const char *fmt, ...) { Append(view, string, true); } +void ReportConsolef(const char *fmt, ...) { + Scratch scratch; + STRING_FORMAT(scratch, fmt, string); + Appendf(LogView, "%S\n", string); +} + void ReportErrorf(const char *fmt, ...) { ErrorCount += 1; Scratch scratch; @@ -77,28 +83,11 @@ void ReportErrorf(const char *fmt, ...) { if (LogView) { Appendf(LogView, "%S\n", string); - ShowUIMessagef("%S", string); } else { printf("%.*s\n", (int)string.len, string.data); } } -void ReportConsolef(const char *fmt, ...) { - Scratch scratch; - STRING_FORMAT(scratch, fmt, string); - Appendf(LogView, "%S\n", string); -} - -void ReportWarningf(const char *fmt, ...) { - ErrorCount += 1; - Scratch scratch; - STRING_FORMAT(scratch, fmt, string); - if (BreakOnError) { - BREAK(); - } - Appendf(LogView, "%S\n", string); -} - void CMD_CenterView() { CenterView(PrimaryWindowID); } RegisterCommand(CMD_CenterView, "", ""); diff --git a/src/text_editor/config.cpp b/src/text_editor/config.cpp index 9187236..833abc0 100644 --- a/src/text_editor/config.cpp +++ b/src/text_editor/config.cpp @@ -4,7 +4,7 @@ struct Lexer { char *start; char *end; char *name; - int line, column; + Int line, column; }; enum TriggerKind { @@ -34,6 +34,11 @@ struct CachedTrigger { }; Array CachedTriggers; +Lexer MakeLexer(Allocator allocator, String string, char *file, Int line, Int column) { + Lexer lexer = {allocator, string.data, string.data, string.data + string.len, file, line, column}; + return lexer; +} + void Advance(Lexer *lex) { if (lex->at < lex->end) { if (lex->at[0] == '\n') { diff --git a/src/text_editor/coroutines.cpp b/src/text_editor/coroutines.cpp index 172d87e..f1a25fb 100644 --- a/src/text_editor/coroutines.cpp +++ b/src/text_editor/coroutines.cpp @@ -61,7 +61,7 @@ void UpdateCoroutines(Event *event) { _CoroutineContext = ⁢ mco_result ok = mco_resume(it.co); if (ok != MCO_SUCCESS) { - ReportWarningf("failed to resume coroutine %d", ok); + ReportErrorf("failed to resume coroutine %d", ok); DestroyCoroutine(&it); remove_item = true; } diff --git a/src/text_editor/globals.cpp b/src/text_editor/globals.cpp index e83f88d..baae7ad 100644 --- a/src/text_editor/globals.cpp +++ b/src/text_editor/globals.cpp @@ -176,6 +176,7 @@ RegisterVariable(String, OpenCodePatterns, ".c .h .cpp .hpp .cc .cxx .rs .go .zi RegisterVariable(String, OpenCodeExcludePatterns, ""); RegisterVariable(Int, TrimTrailingWhitespace, 1); + // PROJECT_MANAGEMENT // Set at the beginning of the program to current directory RegisterVariable(String, ProjectDirectory, ""); diff --git a/src/text_editor/plugin_config.cpp b/src/text_editor/plugin_config.cpp index 2f424fd..45d1acb 100644 --- a/src/text_editor/plugin_config.cpp +++ b/src/text_editor/plugin_config.cpp @@ -56,17 +56,6 @@ void CMD_EvalCommandsLineByLine() { EvalCommandsLineByLine(set); } RegisterCommand(CMD_EvalCommandsLineByLine, "", "Goes line by line over a buffer and evaluates every line as a command, ignores empty or lines starting with '//'"); -Variable *GetVariable(String name) { - Variable *var = NULL; - For (Variables) { - if (name == it.name) { - var = ⁢ - break; - } - } - return var; -} - BufferID LoadConfig(String config_path) { ReportConsolef("Loading config %S...", config_path); Window *window = GetWindow(NullWindowID); diff --git a/src/text_editor/plugin_config.h b/src/text_editor/plugin_config.h index 3f89921..c9fb318 100644 --- a/src/text_editor/plugin_config.h +++ b/src/text_editor/plugin_config.h @@ -1,3 +1,4 @@ #if PLUGIN_CONFIG void Set(String string); +String InsertVariables(Allocator allocator, String string); #endif \ No newline at end of file diff --git a/src/text_editor/plugin_remedybg.cpp b/src/text_editor/plugin_remedybg.cpp index f8c5aaf..66798ae 100644 --- a/src/text_editor/plugin_remedybg.cpp +++ b/src/text_editor/plugin_remedybg.cpp @@ -2133,7 +2133,7 @@ bool RDBG_InitConnection(mco_coro *co, bool create_session = true) { } if (file.len == 0) { - ReportWarningf("Couldn't find neither .rdbg file, nor use the BinaryUnderDebug variable to locate the binary"); + ReportErrorf("Couldn't find neither .rdbg file, nor use the BinaryUnderDebug variable to locate the binary"); return false; } } diff --git a/src/text_editor/plugin_word_complete.cpp b/src/text_editor/plugin_word_complete.cpp index 313eb49..b1c7be7 100644 --- a/src/text_editor/plugin_word_complete.cpp +++ b/src/text_editor/plugin_word_complete.cpp @@ -1,5 +1,6 @@ // MAAAAAAAAAAAAN I DONT LIKE THIS CODE, BUT HOPE IT WORKS +// @todo: potentially bad that we are not checking against end! lexer->at[0] == 0 check is not enough struct Lexer2 { char16_t *at; }; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 7e4d7ba..579d41c 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -854,25 +854,15 @@ extern char **environ; int main(int argc, char **argv) #endif { - InitScratch(); - InitOS(ReportErrorf); #if OS_WINDOWS int argc = __argc; char **argv = __argv; AttachConsole(ATTACH_PARENT_PROCESS); #endif - - if (1) { - RunArenaTest(); - For (TestFunctions) { - it.function(); - } - - // ReportErrorf("Testing DONE\n"); - // return 0; - } - -#if OS_WINDOWS + InitScratch(); + InitOS(ReportErrorf); + ProjectDirectory = GetWorkingDir(Perm); + #if OS_WINDOWS { wchar_t *p = GetEnvironmentStringsW(); for (;p && p[0];) { @@ -889,7 +879,16 @@ int main(int argc, char **argv) } #endif - ProjectDirectory = GetWorkingDir(Perm); + if (1) { + RunArenaTest(); + For (TestFunctions) { + it.function(); + } + + // ReportErrorf("Testing DONE\n"); + // return 0; + } + { String sdl_config_path = SDL_GetPrefPath("krzosa", "text_editor"); if (sdl_config_path.len && sdl_config_path.data[sdl_config_path.len - 1] == '\\') { @@ -1009,7 +1008,7 @@ int main(int argc, char **argv) ReloadFont(PathToFont, (U32)FontSize); CreateWind(); ReopenBuffer(GetBuffer(NullBufferID)); - InitOS(ReportWarningf); + InitOS(ReportErrorf); For (GlobalCommands) { if (it.binding.len != 0) { diff --git a/src/text_editor/ui.cpp b/src/text_editor/ui.cpp index 7e548ed..251166f 100644 --- a/src/text_editor/ui.cpp +++ b/src/text_editor/ui.cpp @@ -217,11 +217,88 @@ bool IsOpenBoundary(char c) { return result; } -/* +Variable *GetVariable(String name) { + Variable *var = NULL; + For (Variables) { + if (name == it.name) { + var = ⁢ + break; + } + } + return var; +} -Variables that control the default shell for '!' commands - DefaultShellWindows - "cmd" - DefaultShellUnix - "bash | sh" +String InsertVariables(Allocator allocator, String string) { + Scratch scratch(allocator); + Array parts = {scratch}; + String it = string; + for (;;) { + int64_t idx = 0; + bool found = Seek(it, "@", &idx, SeekFlag_None); + if (!found) { + if (it.len > 0) { + Add(&parts, it); + } + break; + } + + String prev = GetPrefix(it, idx); + if (prev.len > 0) { + Add(&parts, prev); + } + + it = Skip(it, idx + 1); + char c = At(it, 0); + String name = {}; + if (c == '@') { + Add(&parts, String{"@", 1}); + it = Skip(it, 1); + continue; + } else if (c == '(') { + char *start = it.data + 1; + while (At(it, 0) && At(it, 0) != ')') { + it = Skip(it, 1); + } + Int len = it.data - start; + name = {start, len}; + it = Skip(it, 1); // skip ')' + } else { + char *start = it.data; + while (IsAlphanumeric(At(it, 0))) { + it = Skip(it, 1); + } + Int len = it.data - start; + name = {start, len}; + } + Variable *variable = GetVariable(name); + if (!variable) { + ReportErrorf("Variable: %S, not found", name); + return string; + } + if (variable->type != VariableType_String) { + // @todo: this will not report - open will override + ReportErrorf("Variable: %S, not of type String", variable->type); + return string; + } + + Add(&parts, *variable->string); + } + + String result = Merge(allocator, parts, ""); + return result; +} + +void TestInsertVariable() { + Scratch scratch; + + String a = "Thing/@(ProjectDirectory)/Another"; + String b = "Thing/@ProjectDirectory/Another"; + Assert(InsertVariables(scratch, a) == InsertVariables(scratch, b)); + int c = 10; + +} RegisterFunction(&TestFunctions, TestInsertVariable); + +/* Variables: @ProjectDirectory/build/te @@ -249,7 +326,13 @@ Otherwise it does filepath parsing: TODO: need to add '~', but where? -TODO: on linux find shell on first command and set as default + +USECASE: Wouldn't it be cool to just select a part of codebase pipe that into a script + and get a result in a clipboard or capture the output and change the selection. +TODO: Data desc language +!{bash,Out:Sel} SCRIPT +!{bash,Out:Clip} SCRIPT +Use variables for injecting selection: @Sel */ ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpenMeta meta) { @@ -258,7 +341,7 @@ ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpen bool exec = !(ResolveOpenMeta_DontExec & meta); #if PLUGIN_CONFIG - // @todo: variable substitution {{ProjectDirectory}}/build/te.exe + path = InsertVariables(alo, path); if (exec && result.kind == OpenKind_Invalid && StartsWith(path, ":Set ")) { result.kind = OpenKind_Set;