From 07a41e026637c248eaf0349c7c9baa4ef51a4c09 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 27 Jul 2024 22:07:58 +0200 Subject: [PATCH] Using app data path, deleting config every time, improve loop --- src/basic/win32.cpp | 11 ++- src/text_editor/commands_window.cpp | 41 +++++++++- src/text_editor/lua_api.cpp | 39 ---------- src/text_editor/text_editor.cpp | 112 ++++++++++------------------ src/text_editor/text_editor.h | 1 + src/text_editor/todo.txt | 2 + 6 files changed, 90 insertions(+), 116 deletions(-) diff --git a/src/basic/win32.cpp b/src/basic/win32.cpp index 89cd407..8804736 100644 --- a/src/basic/win32.cpp +++ b/src/basic/win32.cpp @@ -314,6 +314,15 @@ String GetWorkingDir(Allocator arena) { } bool IsAbsolute(String path) { - bool result = path.len > 3 && IsAlphabetic(path.data[0]) && path.data[1] == ':' && path.data[2] == '/'; + bool result = path.len > 3 && IsAlphabetic(path.data[0]) && path.data[1] == ':' && (path.data[2] == '/' || path.data[2] == '\\'); + return result; +} + +bool DeleteFile(String path) { + wchar_t wpath[1024]; + CreateWidecharFromChar(wpath, Lengthof(wpath), path.data, path.len); + BOOL success = DeleteFileW(wpath); + bool result = true; + if (success == 0) result = false; return result; } diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index ffe6da8..7e27f6a 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -222,7 +222,38 @@ void MergeCarets(View *view, Range *mouse_selection_anchor) { Swap(&view->carets[first_caret_index], &view->carets[0]); } -void ReplaceDebugbarData() { +String DebugViewList() { + Scratch scratch; + Array strings = {scratch}; + For(Views) { + Buffer *buffer = GetBuffer(it.active_buffer); + String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)it.id.id, (long long)buffer->id.id, FmtString(buffer->name)); + Add(&strings, string); + } + String result = Merge(scratch, strings, "\n"); + return result; +} + +String DebugWindowList() { + Scratch scratch; + Array strings = {scratch}; + For(Windows) { + View *view = GetActiveView(&it); + Buffer *buffer = GetBuffer(view->active_buffer); + String string = Format(scratch, "window = %lld active_view = %lld buffer_name = %.*s", (long long)it.id.id, (long long)it.active_view.id, FmtString(buffer->name)); + Add(&strings, string); + ForItem(child_view_id, it.views) { + View *child_view = GetView(child_view_id); + Buffer *child_buffer = GetBuffer(child_view->active_buffer); + String child_string = Format(scratch, " view = %lld buffer = %lld name = %.*s", (long long)child_view->id.id, (long long)child_buffer->id.id, FmtString(child_buffer->name)); + Add(&strings, child_string); + } + } + String result = Merge(scratch, strings, "\n"); + return result; +} + +void ReplaceDebugData() { Buffer *buffer = GetBuffer("*debug*"); Window *window = GetActiveWindow(); @@ -234,9 +265,15 @@ void ReplaceDebugbarData() { Buffer *last_buffer = GetBuffer(last_view->active_buffer); Scratch scratch; - String s = Format(scratch, "wid: %d\nvid: %d\nbid: %d\nframe: %lld", (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id, (long long)FrameID); + String s = Format(scratch, "wid: %d\nvid: %d\nbid: %d\nframe: %lld\n", (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id, (long long)FrameID); String16 string = ToString16(scratch, s); ReplaceText(buffer, GetRange(*buffer), string); + + // String view_list = DebugViewList(); + // Append(buffer, ToString16(scratch, view_list)); + + // String window_list = DebugWindowList(); + // Append(buffer, ToString16(scratch, window_list)); } void ReplaceInfobarData() { diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 2aa8364..2d1392b 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -52,39 +52,6 @@ int LuaListOpenBuffers(lua_State *L) { return 1; } -int LuaListViews(lua_State *L) { - Scratch scratch; - Array strings = {scratch}; - For(Views) { - Buffer *buffer = GetBuffer(it.active_buffer); - String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)it.id.id, (long long)buffer->id.id, FmtString(buffer->name)); - Add(&strings, string); - } - String result = Merge(scratch, strings, "\n"); - lua_pushlstring(LuaState, result.data, result.len); - return 1; -} - -int LuaListWindows(lua_State *L) { - Scratch scratch; - Array strings = {scratch}; - For(Windows) { - View *view = GetActiveView(&it); - Buffer *buffer = GetBuffer(view->active_buffer); - String string = Format(scratch, "window = %lld active_view = %lld buffer_name = %.*s", (long long)it.id.id, (long long)it.active_view.id, FmtString(buffer->name)); - Add(&strings, string); - ForItem(child_view_id, it.views) { - View *child_view = GetView(child_view_id); - Buffer *child_buffer = GetBuffer(child_view->active_buffer); - String child_string = Format(scratch, " view = %lld buffer = %lld name = %.*s", (long long)child_view->id.id, (long long)child_buffer->id.id, FmtString(child_buffer->name)); - Add(&strings, child_string); - } - } - String result = Merge(scratch, strings, "\n"); - lua_pushlstring(LuaState, result.data, result.len); - return 1; -} - int LuaOpenBigBuffer(lua_State *L) { Window *window = GetWindow(GetLastActiveWindow()); @@ -110,12 +77,6 @@ void InitLua() { lua_pushcfunction(LuaState, LuaListOpenBuffers); lua_setglobal(LuaState, "list_buffers"); - lua_pushcfunction(LuaState, LuaListViews); - lua_setglobal(LuaState, "list_views"); - - lua_pushcfunction(LuaState, LuaListWindows); - lua_setglobal(LuaState, "list_windows"); - lua_pushcfunction(LuaState, LuaOpenBigBuffer); lua_setglobal(LuaState, "open_big_buffer"); } diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 5cca413..e75bda0 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -103,63 +103,6 @@ void ProcessSDLEvent(SDL_Event *input_event) { HandleEvent(event); } -uint64_t MapCharToNumber(char c) { - switch (c) { - case '0': return 0; break; - case '1': return 1; break; - case '2': return 2; break; - case '3': return 3; break; - case '4': return 4; break; - case '5': return 5; break; - case '6': return 6; break; - case '7': return 7; break; - case '8': return 8; break; - case '9': return 9; break; - case 'a': - case 'A': return 10; break; - case 'b': - case 'B': return 11; break; - case 'c': - case 'C': return 12; break; - case 'd': - case 'D': return 13; break; - case 'e': - case 'E': return 14; break; - case 'f': - case 'F': return 15; break; - default: return 255; - } -} - -uint64_t ParseInt(char *string, uint64_t len, uint64_t base) { - Assert(base >= 2 && base <= 16); - uint64_t acc = 0; - for (uint64_t i = 0; i < len; i++) { - uint64_t num = MapCharToNumber(string[i]); - if (num >= base) { - // @todo: error - return 0; - } - acc *= base; - acc += num; - } - return acc; -} - -Color ParseColor(String string) { - Color result = {0xFF, 0xFF, 0, 0xFF}; - String hex = CutPrefix(&string, 1); - String red = CutPrefix(&string, 2); - result.r = (uint8_t)ParseInt(red.data, red.len, 16); - String green = CutPrefix(&string, 2); - result.g = (uint8_t)ParseInt(green.data, green.len, 16); - String blue = CutPrefix(&string, 2); - result.b = (uint8_t)ParseInt(blue.data, blue.len, 16); - String alpha = CutPrefix(&string, 2); - result.a = (uint8_t)ParseInt(alpha.data, alpha.len, 16); - return result; -} - #if _WIN32 int WinMain(void *hInstance, void *hPrevInstance, const char *lpCmdLine, int nShowCmd) #else @@ -172,6 +115,14 @@ int main() InitArena(&Perm); WorkingDir = GetWorkingDir(Perm); ExeDir = GetExeDir(Perm); + { + String sdl_config_path = SDL_GetPrefPath("krzosa", "text_editor"); + if (sdl_config_path.len && sdl_config_path.data[sdl_config_path.len - 1] == '/') { + sdl_config_path = Chop(sdl_config_path, 1); // chop '/' + } + ConfigDir = NormalizePath(Perm, sdl_config_path); + SDL_free(sdl_config_path.data); + } if (SDL_Init(SDL_INIT_VIDEO) == -1) { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL); @@ -227,24 +178,32 @@ int main() Buffer *lua_buffer = NULL; Int lua_buffer_change_id = 0; { - - // Init base config - if (luaL_dostring(LuaState, BaseLuaConfig.data) == LUA_OK) { - } else { + // Init base config, test that it works and initialize the lua stuff + if (!luaL_dostring(LuaState, BaseLuaConfig.data) == LUA_OK) { Assert(0); } // Init user config Scratch scratch; - String lua_config_path = Format(scratch, "%.*s/init.lua", FmtString(ExeDir)); - lua_buffer = BufferOpenFile(lua_config_path); - String16 string16 = GetString(*lua_buffer); - String string = ToString(scratch, string16); - if (luaL_dostring(LuaState, string.data) == LUA_OK) { - ReloadColors(); + 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 + + lua_buffer = BufferOpenFile(lua_config_path); + if (lua_buffer->len) { + String16 string16 = GetString(*lua_buffer); + String string = ToString(scratch, string16); + if (luaL_dostring(LuaState, string.data) == LUA_OK) { + ReloadColors(); + } else { + } } else { - __debugbreak(); + ReplaceText(lua_buffer, {}, ToString16(scratch, BaseLuaConfig)); } + lua_buffer_change_id = lua_buffer->change_id; } @@ -252,6 +211,14 @@ int main() while (AppIsRunning) { FrameID += 1; + + // We are waiting here because we don't want to miss the + // resize event which we don't handle but which we will use + // in the loop to figure out window size and window layout + SDL_Event event = {}; + if (WaitForEvents) SDL_WaitEvent(&event); + WaitForEvents = true; + int window_x, window_y; SDL_GetWindowSize(window, &window_x, &window_y); WindowSize = {(float)window_x, (float)window_y}; @@ -269,12 +236,9 @@ int main() window->mouse_in_scrollbar = false; } - SDL_Event event; - if (WaitForEvents) { - SDL_WaitEvent(&event); + if (event.type != SDL_EVENT_FIRST) { ProcessSDLEvent(&event); } - WaitForEvents = true; while (SDL_PollEvent(&event)) { ProcessSDLEvent(&event); } @@ -290,7 +254,7 @@ int main() } ReplaceInfobarData(); - ReplaceDebugbarData(); + ReplaceDebugData(); if (lua_buffer->dirty == false && lua_buffer->change_id != lua_buffer_change_id) { Scratch scratch; @@ -352,7 +316,7 @@ int main() { ProfileScope(clip_scroll); Int last_line = LastLine(*buffer); - view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line)*FontLineSpacing)); + view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing)); // @note: // GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index e46e8e5..f92090a 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -91,6 +91,7 @@ struct Scroller { Int FrameID; String WorkingDir; +String ConfigDir; String ExeDir; Arena Perm; diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 9bc710d..4de46c8 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -9,8 +9,10 @@ - open "asd/asd/asd/asd" - console buffer with output and errors - some popup for errors + - good error reporting for user - word completion - Colored strings +- open project files in folder and only show open views in Ctrl+P - font cache and on demand unicode loads