From 8cd754c92399ac5950a6e2db3958673d960fd1cc Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 14 Aug 2024 13:05:38 +0200 Subject: [PATCH] Lua project config --- build_file.cpp | 1 - src/platform/render_opengl.cpp | 4 +- src/text_editor/commands.cpp | 11 +++-- src/text_editor/generated.cpp | 3 +- src/text_editor/lua_api.cpp | 87 +++++++++++++++++---------------- src/text_editor/management.cpp | 3 +- src/text_editor/text_editor.cpp | 21 ++++++-- src/text_editor/text_editor.h | 1 + 8 files changed, 74 insertions(+), 57 deletions(-) diff --git a/build_file.cpp b/build_file.cpp index 4d66ee8..1fe91c1 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -331,7 +331,6 @@ function AddCo(f) return Coroutines[i] end - function OnUpdate() local new_co_list = {} for key, co in pairs(Coroutines) do diff --git a/src/platform/render_opengl.cpp b/src/platform/render_opengl.cpp index 1ac7422..2efc031 100644 --- a/src/platform/render_opengl.cpp +++ b/src/platform/render_opengl.cpp @@ -372,9 +372,7 @@ void DrawCircle(Vec2 pos, float radius, Color color) { } } -Int GetStyleInt(String name, Int default_int); -String GetStyleString(String name, String default_string); -void ReloadFont() { +void ReloadFont() { Int size = StyleFontSize; size = ClampBottom((Int)2, size); if (MainFont.texture_id) { diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index ca09ed0..c3e80b8 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -199,11 +199,12 @@ void ReportWarningf(const char *fmt, ...) { View *null_view = GetView(NullViewID); Command_Append(null_view, string, true); - Window *window = GetWindowWithView(null_view->id); - if (!window) window = GetActiveMainSet().window; - CheckpointBeforeGoto(window); - window->active_view = null_view->id; - ActiveWindow = window->id; + // @todo: proper warning + // Window *window = GetWindowWithView(null_view->id); + // if (!window) window = GetActiveMainSet().window; + // CheckpointBeforeGoto(window); + // window->active_view = null_view->id; + // ActiveWindow = window->id; } void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) { diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index fdf8b24..2d11a34 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -217,10 +217,9 @@ Coroutines = {} function AddCo(f) local i = #Coroutines + 1 Coroutines[i] = coroutine.create(f) - return i + return Coroutines[i] end - function OnUpdate() local new_co_list = {} for key, co in pairs(Coroutines) do diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 3dcc160..c85bc22 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -236,17 +236,6 @@ static void HookLuaForceExit(lua_State *L, lua_Debug *debug) { extern luaL_Reg LuaFunctions[]; -void InitLua() { - LuaState = luaL_newstate(); - luaL_openlibs(LuaState); - lua_sethook(LuaState, HookLuaForceExit, LUA_MASKCOUNT, 100000000); - - for (int i = 0; LuaFunctions[i].name; i += 1) { - lua_pushcfunction(LuaState, LuaFunctions[i].func); - lua_setglobal(LuaState, LuaFunctions[i].name); - } -} - Int GetStyleInt(String name, Int default_int) { Int result = default_int; lua_getglobal(LuaState, "Style"); @@ -355,38 +344,44 @@ int Lua_Play(lua_State *L) { return 0; } -Int LuaBufferChangeID = 0; void ReloadStyle(); extern String BaseLuaConfig; -void UpdateLua() { - if (LuaBufferID.id == -1) return; +int LoadLuaBuffer(Buffer *lua_buffer) { + if (!lua_buffer || lua_buffer->dirty || lua_buffer->change_id == lua_buffer->user_change_id) return false; + ReportConsolef("reloading config: %.*s", FmtString(lua_buffer->name)); - Buffer *lua_buffer = GetBuffer(LuaBufferID); - if (lua_buffer->dirty == false && lua_buffer->change_id != LuaBufferChangeID) { - Scratch scratch; - String string = AllocCharString(scratch, lua_buffer); - if (luaL_dostring(LuaState, string.data) == LUA_OK) { - if (lua_isstring(LuaState, -1)) { - const char *text = lua_tostring(LuaState, -1); - ReportConsolef(text); - lua_pop(LuaState, 1); - } - ReloadStyle(); - // @todo: maybe don't reload font if nothing changed - ReloadFont(); - For(Windows) { - Window *window = it.o; - if (!window->visible || window->absolute_position || window->is_title_bar) continue; - window->draw_scrollbar = StyleDrawScrollbar; - window->draw_line_numbers = StyleDrawLineNumbers; - } - } else { - const char *error_message = lua_tostring(LuaState, -1); - ReportWarningf("Failed to load user config! %s", error_message); + Scratch scratch; + String string = AllocCharString(scratch, lua_buffer); + if (luaL_dostring(LuaState, string.data) == LUA_OK) { + if (lua_isstring(LuaState, -1)) { + const char *text = lua_tostring(LuaState, -1); + ReportConsolef(text); lua_pop(LuaState, 1); } - LuaBufferChangeID = lua_buffer->change_id; + } else { + const char *error_message = lua_tostring(LuaState, -1); + ReportWarningf("Failed to load user config! %s", error_message); + lua_pop(LuaState, 1); + } + lua_buffer->user_change_id = lua_buffer->change_id; + return true; +} + +void UpdateLua() { + int base_config_reload = LoadLuaBuffer(LuaConfigBuffer); + int project_config_reload = LoadLuaBuffer(LuaProjectBuffer); + + if (base_config_reload || project_config_reload) { + ReportConsolef("reloading style variables because config changed"); + ReloadStyle(); + ReloadFont(); // @todo: maybe don't reload font if nothing changed + For(Windows) { + Window *window = it.o; + if (!window->visible || window->absolute_position || window->is_title_bar) continue; + window->draw_scrollbar = StyleDrawScrollbar; + window->draw_line_numbers = StyleDrawLineNumbers; + } } lua_getglobal(LuaState, "OnUpdate"); @@ -399,7 +394,17 @@ void UpdateLua() { } void InitLuaConfig() { + LuaState = luaL_newstate(); + luaL_openlibs(LuaState); + lua_sethook(LuaState, HookLuaForceExit, LUA_MASKCOUNT, 100000000); + + for (int i = 0; LuaFunctions[i].name; i += 1) { + lua_pushcfunction(LuaState, LuaFunctions[i].func); + lua_setglobal(LuaState, LuaFunctions[i].name); + } + // Init base config, test that it works and initialize the lua stuff + ReportConsolef("load base lua config"); 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); @@ -414,18 +419,18 @@ void InitLuaConfig() { #if DEBUG_BUILD // WARNING! Delete config to make sure we are running this code more frequently SDL_RemovePath(lua_config_path.data); + ReportConsolef("deleting config for debug purposes!"); #endif Buffer *lua_buffer = BufferOpenFile(lua_config_path); if (lua_buffer->len == 0) { String16 string16 = ToString16(scratch, BaseLuaConfig); IKnowWhatImDoing_ReplaceText(lua_buffer, {}, string16); + ReportConsolef("no config at: %.*s - creating config buffer", FmtString(lua_config_path)); } - // if we loaded from file this should force to read - lua_buffer->change_id = 2; - LuaBufferChangeID = 1; - LuaBufferID = lua_buffer->id; + lua_buffer->user_change_id = -1; // if we loaded from file this should force to read + LuaConfigBuffer = lua_buffer; UpdateLua(); } diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 3b6c572..d86b079 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -11,7 +11,6 @@ ViewID NullViewID; WindowID DebugWindowID; // +debug BufferID DebugBufferID; -BufferID LuaBufferID = {-1}; WindowID ActiveWindow; @@ -19,6 +18,8 @@ WindowID ScrollbarSelected = {-1}; WindowID DocumentSelected = {-1}; Range DocumentRangeAnchor; +Buffer *LuaProjectBuffer; +Buffer *LuaConfigBuffer; Buffer *GCInfoBuffer; Buffer *EventBuffer; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 94c55da..1a61259 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -288,15 +288,28 @@ int main(int argc, char **argv) InitScratchBuffer(); InitRender(); - InitLua(); ReloadFont(); + + Array files_to_load = {}; + for (int i = 1; i < argc; i += 1) { + String it = argv[i]; + if (!FileExists(it)) continue; + + if (EndsWith(it, ".project.lua")) { + if (!LuaProjectBuffer) { + LuaProjectBuffer = BufferOpenFile(it); + LuaProjectBuffer->user_change_id = -1; + } + } else { + Add(&files_to_load, it); + } + } + InitLuaConfig(); InitWindows(); InitOS(ReportWarningf); - for (int i = 1; i < argc; i += 1) { - String it = argv[i]; - if (!FileExists(it)) continue; + For(files_to_load) { Window *window = GetWindow({0}); WindowOpenBufferView(window, it); } diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 4b94802..fed63d6 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -25,6 +25,7 @@ struct Buffer { BufferID id; String name; Int change_id; + Int user_change_id; Int file_mod_time; union {