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]
end
function OnUpdate()
local new_co_list = {}
for key, co in pairs(Coroutines) do

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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

View File

@@ -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();
}

View File

@@ -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;

View File

@@ -288,15 +288,28 @@ int main(int argc, char **argv)
InitScratchBuffer();
InitRender();
InitLua();
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();
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);
}

View File

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