Lua project config

This commit is contained in:
Krzosa Karol
2024-08-14 13:05:38 +02:00
parent 225d1ffc06
commit 8cd754c923
8 changed files with 74 additions and 57 deletions

View File

@@ -331,7 +331,6 @@ function AddCo(f)
return Coroutines[i] return Coroutines[i]
end end
function OnUpdate() function OnUpdate()
local new_co_list = {} local new_co_list = {}
for key, co in pairs(Coroutines) do for key, co in pairs(Coroutines) do

View File

@@ -372,8 +372,6 @@ 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; Int size = StyleFontSize;
size = ClampBottom((Int)2, size); size = ClampBottom((Int)2, size);

View File

@@ -199,11 +199,12 @@ void ReportWarningf(const char *fmt, ...) {
View *null_view = GetView(NullViewID); View *null_view = GetView(NullViewID);
Command_Append(null_view, string, true); Command_Append(null_view, string, true);
Window *window = GetWindowWithView(null_view->id); // @todo: proper warning
if (!window) window = GetActiveMainSet().window; // Window *window = GetWindowWithView(null_view->id);
CheckpointBeforeGoto(window); // if (!window) window = GetActiveMainSet().window;
window->active_view = null_view->id; // CheckpointBeforeGoto(window);
ActiveWindow = window->id; // window->active_view = null_view->id;
// ActiveWindow = window->id;
} }
void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) { void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) {

View File

@@ -217,10 +217,9 @@ Coroutines = {}
function AddCo(f) function AddCo(f)
local i = #Coroutines + 1 local i = #Coroutines + 1
Coroutines[i] = coroutine.create(f) Coroutines[i] = coroutine.create(f)
return i return Coroutines[i]
end end
function OnUpdate() function OnUpdate()
local new_co_list = {} local new_co_list = {}
for key, co in pairs(Coroutines) do for key, co in pairs(Coroutines) do

View File

@@ -236,17 +236,6 @@ static void HookLuaForceExit(lua_State *L, lua_Debug *debug) {
extern luaL_Reg LuaFunctions[]; 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 GetStyleInt(String name, Int default_int) {
Int result = default_int; Int result = default_int;
lua_getglobal(LuaState, "Style"); lua_getglobal(LuaState, "Style");
@@ -355,15 +344,13 @@ int Lua_Play(lua_State *L) {
return 0; return 0;
} }
Int LuaBufferChangeID = 0;
void ReloadStyle(); void ReloadStyle();
extern String BaseLuaConfig; extern String BaseLuaConfig;
void UpdateLua() { int LoadLuaBuffer(Buffer *lua_buffer) {
if (LuaBufferID.id == -1) return; 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; Scratch scratch;
String string = AllocCharString(scratch, lua_buffer); String string = AllocCharString(scratch, lua_buffer);
if (luaL_dostring(LuaState, string.data) == LUA_OK) { if (luaL_dostring(LuaState, string.data) == LUA_OK) {
@@ -372,21 +359,29 @@ void UpdateLua() {
ReportConsolef(text); ReportConsolef(text);
lua_pop(LuaState, 1); lua_pop(LuaState, 1);
} }
} 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(); ReloadStyle();
// @todo: maybe don't reload font if nothing changed ReloadFont(); // @todo: maybe don't reload font if nothing changed
ReloadFont();
For(Windows) { For(Windows) {
Window *window = it.o; Window *window = it.o;
if (!window->visible || window->absolute_position || window->is_title_bar) continue; if (!window->visible || window->absolute_position || window->is_title_bar) continue;
window->draw_scrollbar = StyleDrawScrollbar; window->draw_scrollbar = StyleDrawScrollbar;
window->draw_line_numbers = StyleDrawLineNumbers; window->draw_line_numbers = StyleDrawLineNumbers;
} }
} else {
const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Failed to load user config! %s", error_message);
lua_pop(LuaState, 1);
}
LuaBufferChangeID = lua_buffer->change_id;
} }
lua_getglobal(LuaState, "OnUpdate"); lua_getglobal(LuaState, "OnUpdate");
@@ -399,7 +394,17 @@ void UpdateLua() {
} }
void InitLuaConfig() { 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 // 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) { if (!luaL_dostring(LuaState, BaseLuaConfig.data) == LUA_OK) {
const char *error_message = lua_tostring(LuaState, -1); const char *error_message = lua_tostring(LuaState, -1);
ReportErrorf("Failed to load base lua config! %s", error_message); ReportErrorf("Failed to load base lua config! %s", error_message);
@@ -414,18 +419,18 @@ void InitLuaConfig() {
#if DEBUG_BUILD #if DEBUG_BUILD
// WARNING! Delete config to make sure we are running this code more frequently // WARNING! Delete config to make sure we are running this code more frequently
SDL_RemovePath(lua_config_path.data); SDL_RemovePath(lua_config_path.data);
ReportConsolef("deleting config for debug purposes!");
#endif #endif
Buffer *lua_buffer = BufferOpenFile(lua_config_path); Buffer *lua_buffer = BufferOpenFile(lua_config_path);
if (lua_buffer->len == 0) { if (lua_buffer->len == 0) {
String16 string16 = ToString16(scratch, BaseLuaConfig); String16 string16 = ToString16(scratch, BaseLuaConfig);
IKnowWhatImDoing_ReplaceText(lua_buffer, {}, string16); 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->user_change_id = -1; // if we loaded from file this should force to read
lua_buffer->change_id = 2; LuaConfigBuffer = lua_buffer;
LuaBufferChangeID = 1;
LuaBufferID = lua_buffer->id;
UpdateLua(); UpdateLua();
} }

View File

@@ -11,7 +11,6 @@ ViewID NullViewID;
WindowID DebugWindowID; // +debug WindowID DebugWindowID; // +debug
BufferID DebugBufferID; BufferID DebugBufferID;
BufferID LuaBufferID = {-1};
WindowID ActiveWindow; WindowID ActiveWindow;
@@ -19,6 +18,8 @@ WindowID ScrollbarSelected = {-1};
WindowID DocumentSelected = {-1}; WindowID DocumentSelected = {-1};
Range DocumentRangeAnchor; Range DocumentRangeAnchor;
Buffer *LuaProjectBuffer;
Buffer *LuaConfigBuffer;
Buffer *GCInfoBuffer; Buffer *GCInfoBuffer;
Buffer *EventBuffer; Buffer *EventBuffer;

View File

@@ -288,15 +288,28 @@ int main(int argc, char **argv)
InitScratchBuffer(); InitScratchBuffer();
InitRender(); InitRender();
InitLua();
ReloadFont(); ReloadFont();
Array<String> 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(); InitLuaConfig();
InitWindows(); InitWindows();
InitOS(ReportWarningf); InitOS(ReportWarningf);
for (int i = 1; i < argc; i += 1) { For(files_to_load) {
String it = argv[i];
if (!FileExists(it)) continue;
Window *window = GetWindow({0}); Window *window = GetWindow({0});
WindowOpenBufferView(window, it); WindowOpenBufferView(window, it);
} }

View File

@@ -25,6 +25,7 @@ struct Buffer {
BufferID id; BufferID id;
String name; String name;
Int change_id; Int change_id;
Int user_change_id;
Int file_mod_time; Int file_mod_time;
union { union {