From 217659256bac971d73df21ee22d49e9a4723cc6b Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 31 Jan 2026 09:49:44 +0100 Subject: [PATCH] Begin design of comment evaluation and {{variables}} --- src/basic/basic_os.cpp | 22 ++++++++++++++++++++++ src/text_editor/buffer.cpp | 2 +- src/text_editor/commands.cpp | 1 + src/text_editor/globals.cpp | 5 +++++ src/text_editor/plugin_config.cpp | 21 ++++++++++++--------- src/text_editor/text_editor.cpp | 2 +- src/text_editor/ui.cpp | 14 ++++++++++++-- 7 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/basic/basic_os.cpp b/src/basic/basic_os.cpp index a031d9e..d2bf5c2 100644 --- a/src/basic/basic_os.cpp +++ b/src/basic/basic_os.cpp @@ -1172,3 +1172,25 @@ API void CloseStdin(Process *process) { API double GetTimeSeconds() { return GetTimeMicros() / 1000000.0; } + +API String WriteTempFile(String data) { + Scratch scratch; + +#if OS_WINDOWS + int buffer_len = MAX_PATH+1; + char16_t *buffer = AllocArray(scratch, char16_t, buffer_len); + Assert(sizeof(char16_t) == sizeof(wchar_t)); + DWORD result = GetTempPath2W(buffer_len, (LPWSTR)buffer); + Assert(result != 0); + String16 temp16 = {buffer, result}; + NormalizePathInPlace(temp16); + String temp_directory = ToString(scratch, temp16); +#else + String temp_directory = "/tmp"; +#endif + + String temp_filename = Format(scratch, "%S/temp%llu", temp_directory, GetTimeNanos()); + bool done = WriteFile(temp_filename, data); + Assert(done); + return temp_filename; +} diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index ae9bb6a..2e793b4 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -310,7 +310,7 @@ API Int GetWordEnd(Buffer *buffer, Int pos) { } API bool IsLoadWord(char16_t w) { - bool result = w == u'-' || w == u'/' || w == u'\\' || w == u':' || w == u'$' || w == u'_' || w == u'.' || w == u'!' || w == u'@'; + bool result = w == u'-' || w == u'/' || w == u'\\' || w == u':' || w == u'$' || w == u'_' || w == u'.' || w == u'!' || w == u'@' || w == '{' || w == '}'; if (!result) { result = !(IsSymbol(w) || IsWhitespace(w)); } diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 46204bb..5faa360 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -68,6 +68,7 @@ void Appendf(View *view, const char *fmt, ...) { } void ReportErrorf(const char *fmt, ...) { + ErrorCount += 1; Scratch scratch; STRING_FORMAT(scratch, fmt, string); if (BreakOnError) { diff --git a/src/text_editor/globals.cpp b/src/text_editor/globals.cpp index 4ad3f33..2ae1b19 100644 --- a/src/text_editor/globals.cpp +++ b/src/text_editor/globals.cpp @@ -14,6 +14,11 @@ Int ErrorCount; String16 InitialScratchContent = uR"==(:OpenProject C:/text_editor/src/text_editor/text_editor.cpp :Set FontSize 70 +/* +!python {{TEMP}} +for it in i: + memes +*/ 0 1 2 diff --git a/src/text_editor/plugin_config.cpp b/src/text_editor/plugin_config.cpp index d15027f..88e6df8 100644 --- a/src/text_editor/plugin_config.cpp +++ b/src/text_editor/plugin_config.cpp @@ -56,6 +56,17 @@ 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); @@ -69,7 +80,6 @@ BufferID LoadConfig(String config_path) { return buffer->id; } - #define ExpectP(x, ...) \ if (!(x)) { \ ReportErrorf("Failed to parse '" __FUNCTION__ "' command, " __VA_ARGS__); \ @@ -80,14 +90,7 @@ void Set(String string) { String name = SkipIdent(&string); ExpectP(name.len != 0, "expected a variable name, instead got '%S'", string); - Variable *var = NULL; - For (Variables) { - if (name == it.name) { - var = ⁢ - break; - } - } - + Variable *var = GetVariable(name); if (var) { SkipWhitespace(&string); if (var->type == VariableType_String) { diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 0bc9059..ce6bc08 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -854,7 +854,7 @@ int main(int argc, char **argv) { InitScratch(); InitOS((OSErrorReport *)printf); -#if _WIN32 +#if OS_WINDOWS int argc = __argc; char **argv = __argv; AttachConsole(ATTACH_PARENT_PROCESS); diff --git a/src/text_editor/ui.cpp b/src/text_editor/ui.cpp index eaa8bcc..5152a72 100644 --- a/src/text_editor/ui.cpp +++ b/src/text_editor/ui.cpp @@ -214,7 +214,7 @@ void MouseLoadWord(Event event, ResolveOpenMeta meta = ResolveOpenMeta_Normal) { } bool IsOpenBoundary(char c) { - bool result = c == 0 || IsBrace(c) || c == ':' || c == '\t' || c == '\n' || c == '"' || c == '\''; + bool result = c == 0 || c == ':' || c == '\t' || c == '\n' || c == '"' || c == '\''; return result; } @@ -224,6 +224,8 @@ ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpen bool exec = !(ResolveOpenMeta_DontExec & meta); #if PLUGIN_CONFIG + // @todo: variable substitution {{ProjectDirectory}}/build/te.exe + if (exec && result.kind == OpenKind_Invalid && StartsWith(path, ":Set ")) { result.kind = OpenKind_Set; result.path = Skip(path, 5); @@ -253,6 +255,15 @@ ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpen if (exec && result.kind == OpenKind_Invalid && StartsWith(path, "!")) { result.kind = OpenKind_Exec; result.path = Skip(path, 1); + + Int idx = 0; + String needle = "{{TEMP}}"; + if (Seek(result.path, needle, &idx, SeekFlag_None)) { + String rest = Skip(result.path, idx + needle.len); + String begin = GetPrefix(result.path, idx); + String temp_filename = WriteTempFile(rest); + result.path = Format(alo, "%S%S", begin, temp_filename); + } } // https://web @@ -291,7 +302,6 @@ ResolvedOpen ResolveOpen(Allocator alo, Window *window, String path, ResolveOpen } path = {pstart.data, (Int)(p.data - pstart.data)}; - // @todo: verify! // For (LINE:COLUMN): error: - we can either backtrack at the end since we are including // the parenthesis and whitespace or alternatively we can look for patterns on every // character move in the loop... For now let's do backtracking. This doesn't handle all paths