From 6d66986b5b63e434364e648077caa8a2e0e55c37 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 7 Aug 2024 07:56:21 +0200 Subject: [PATCH] Small refactor --- src/text_editor/lua_api.cpp | 64 +++++++++++++++++---------------- src/text_editor/process.cpp | 2 +- src/text_editor/text_editor.cpp | 8 ++--- src/text_editor/title_bar.cpp | 2 +- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 03c6f28..625f3a0 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -238,37 +238,7 @@ Int LuaBufferChangeID = 0; void ReloadStyle(); extern String BaseLuaConfig; -void ReloadLuaConfig() { - // Init base config, test that it works and initialize the lua stuff - if (LuaBufferChangeID == 0) { - if (!luaL_dostring(LuaState, BaseLuaConfig.data) == LUA_OK) { - const char *error_message = lua_tostring(LuaState, -1); - ReportErrorf("Failed to load base lua config! %s", error_message); - lua_pop(LuaState, 1); - Assert(!"Invalid codepath"); - } - - // Init user config - Scratch scratch; - String lua_config_path = Format(scratch, "%.*s/init.lua", FmtString(ConfigDir)); - -#if DEBUG_BUILD - // WARNING! Delete config to make sure we are running this code more frequently - SDL_RemovePath(lua_config_path.data); -#endif - - Buffer *lua_buffer = BufferOpenFile(lua_config_path); - if (lua_buffer->len == 0) { - String16 string16 = ToString16(scratch, BaseLuaConfig); - IKnowWhatImDoing_ReplaceText(lua_buffer, {}, string16); - } - - // if we loaded from file this should force to read - lua_buffer->change_id = 2; - LuaBufferChangeID = 1; - LuaBufferID = lua_buffer->id; - } - +void UpdateLuaConfig() { if (LuaBufferID.id == -1) return; Buffer *lua_buffer = GetBuffer(LuaBufferID); @@ -299,6 +269,38 @@ void ReloadLuaConfig() { } } +void InitLuaConfig() { + // Init base config, test that it works and initialize the lua stuff + if (!luaL_dostring(LuaState, BaseLuaConfig.data) == LUA_OK) { + const char *error_message = lua_tostring(LuaState, -1); + ReportErrorf("Failed to load base lua config! %s", error_message); + lua_pop(LuaState, 1); + Assert(!"Invalid codepath"); + } + + // Init user config + Scratch scratch; + String lua_config_path = Format(scratch, "%.*s/init.lua", FmtString(ConfigDir)); + +#if DEBUG_BUILD + // WARNING! Delete config to make sure we are running this code more frequently + SDL_RemovePath(lua_config_path.data); +#endif + + Buffer *lua_buffer = BufferOpenFile(lua_config_path); + if (lua_buffer->len == 0) { + String16 string16 = ToString16(scratch, BaseLuaConfig); + IKnowWhatImDoing_ReplaceText(lua_buffer, {}, string16); + } + + // if we loaded from file this should force to read + lua_buffer->change_id = 2; + LuaBufferChangeID = 1; + LuaBufferID = lua_buffer->id; + + UpdateLuaConfig(); +} + // String16 EvalString(Allocator allocator, String16 string16) { diff --git a/src/text_editor/process.cpp b/src/text_editor/process.cpp index b528177..87d2ab7 100644 --- a/src/text_editor/process.cpp +++ b/src/text_editor/process.cpp @@ -11,7 +11,7 @@ void Exec(String16 cmd16, String working_dir) { Exec(cmd, working_dir); } -void Process_OnUpdate() { +void UpdateProcesses() { Scratch scratch; int64_t buffer_size = 4096; char *buffer = AllocArray(scratch, char, buffer_size); diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index bce0eb1..3d81042 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -165,9 +165,9 @@ void Update(Event event) { HandleEvent(event); - Process_OnUpdate(); - ReloadLuaConfig(); - ReplaceDebugData(); + UpdateProcesses(); + UpdateLuaConfig(); + UpdateDebugBuffer(); For(IterateInReverse(&order)) { Window *window = &Windows[it]; @@ -301,7 +301,7 @@ int main() InitRender(); InitLua(); ReloadFont(); - ReloadLuaConfig(); + InitLuaConfig(); InitWindows(); while (AppIsRunning) { diff --git a/src/text_editor/title_bar.cpp b/src/text_editor/title_bar.cpp index 1133905..486c6dd 100644 --- a/src/text_editor/title_bar.cpp +++ b/src/text_editor/title_bar.cpp @@ -23,7 +23,7 @@ String DebugWindowList(Allocator allocator) { return result; } -void ReplaceDebugData() { +void UpdateDebugBuffer() { Buffer *buffer = GetBuffer(DebugBufferID); Window *window = GetActiveWindow();