From 166f06d1fb99c414418f3917af49fb9d7f0b37c1 Mon Sep 17 00:00:00 2001 From: krzosa Date: Thu, 21 Aug 2025 09:16:31 +0200 Subject: [PATCH] Trace buffer vs Debug buffer, print Buffer info, Add SaveAll --- data/init.lua | 12 +++++++++++- src/metaprogram/metaprogram.cpp | 1 + src/text_editor/commands.cpp | 16 ++++++++++++--- src/text_editor/generated_config.cpp | 12 +++++++++++- src/text_editor/lua_api.cpp | 5 ++--- src/text_editor/lua_api_generated.cpp | 1 + src/text_editor/management.cpp | 4 ++++ src/text_editor/text_editor.cpp | 1 + src/text_editor/title_bar.cpp | 28 +++++++++++++++++++++++++-- src/text_editor/todo.txt | 2 ++ 10 files changed, 72 insertions(+), 10 deletions(-) diff --git a/data/init.lua b/data/init.lua index 99908b6..47892d6 100644 --- a/data/init.lua +++ b/data/init.lua @@ -391,9 +391,19 @@ function KeybindsBasic(e) return false end -FKey = {"build.bat", "", "", "", "", "", "", "", "", "", "", ""} +function BasicBuild() + SaveAll() + if OS_VALUE == OS_WINDOWS then + return "build.bat" + else + return "./build.sh" + end +end + +FKey = {BasicBuild, "", "", "", "", "", "", "", "", "", "", ""} FKeySDLK = {SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_F9, SDLK_F10, SDLK_F11, SDLK_F12} + function KeybindsFKeys(e) for i = #FKey,1,-1 do if FKey[i] ~= "" then diff --git a/src/metaprogram/metaprogram.cpp b/src/metaprogram/metaprogram.cpp index 203b283..86d2236 100644 --- a/src/metaprogram/metaprogram.cpp +++ b/src/metaprogram/metaprogram.cpp @@ -14,4 +14,5 @@ int main() { Add(&array, String{"\n)==\";\n"}); String result = Merge(scratch, array, ""); WriteFile("../src/text_editor/generated_config.cpp", result); + } \ No newline at end of file diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index cacc5c9..bf96cf1 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -226,9 +226,7 @@ void ReportWarningf(const char *fmt, ...) { void ReportDebugf(const char *fmt, ...) { Scratch scratch; STRING_FORMAT(scratch, fmt, string); - Buffer *buffer = GetBuffer(DebugBufferID); - ReplaceWithoutMovingCarets(buffer, GetEndAsRange(buffer), ToString16(scratch, string)); - ReplaceWithoutMovingCarets(buffer, GetEndAsRange(buffer), ToString16(scratch, "\n")); + Command_Appendf(TraceView, "%.*s\n", FmtString(string)); } void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) { @@ -696,6 +694,18 @@ int Lua_Save(lua_State *L) { return 0; } +void Command_SaveAll() { + for (Buffer *it = FirstBuffer; it; it = it->next) { + if (it->file_mod_time) { + SaveBuffer(it); + } + } +} +int Lua_SaveAll(lua_State *L) { + Command_SaveAll(); + return 0; +} + void Command_KillSelectedLines(View *view) { Scratch scratch; Buffer *buffer = GetBuffer(view->active_buffer); diff --git a/src/text_editor/generated_config.cpp b/src/text_editor/generated_config.cpp index 438fa88..5b4f922 100644 --- a/src/text_editor/generated_config.cpp +++ b/src/text_editor/generated_config.cpp @@ -392,9 +392,19 @@ function KeybindsBasic(e) return false end -FKey = {"build.bat", "", "", "", "", "", "", "", "", "", "", ""} +function BasicBuild() + SaveAll() + if OS_VALUE == OS_WINDOWS then + return "build.bat" + else + return "./build.sh" + end +end + +FKey = {BasicBuild, "", "", "", "", "", "", "", "", "", "", ""} FKeySDLK = {SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_F9, SDLK_F10, SDLK_F11, SDLK_F12} + function KeybindsFKeys(e) for i = #FKey,1,-1 do if FKey[i] ~= "" then diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 0acc18f..fc07484 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -14,12 +14,11 @@ int Lua_print(lua_State *L) { int Lua_Print(lua_State *L) { Scratch scratch; int nargs = lua_gettop(L); - View *view = GetView(DebugViewID); for (int i = 1; i <= nargs; i += 1) { String string = lua_tostring(L, i); - Command_Appendf(view, "%.*s ", FmtString(string)); + Command_Appendf(TraceView, "%.*s ", FmtString(string)); } - Command_Appendf(view, "\n"); + Command_Appendf(TraceView, "\n"); lua_pop(L, nargs); return 0; } diff --git a/src/text_editor/lua_api_generated.cpp b/src/text_editor/lua_api_generated.cpp index de5f768..a155cfe 100644 --- a/src/text_editor/lua_api_generated.cpp +++ b/src/text_editor/lua_api_generated.cpp @@ -1,6 +1,7 @@ luaL_Reg LuaFunctions[] = { {"print", Lua_print}, {"Print", Lua_Print}, + {"SaveAll", Lua_SaveAll}, {"Kill", Lua_Kill}, {"GetLoadWord", Lua_GetLoadWord}, {"BufferExists", Lua_BufferExists}, diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 4db04f2..35e2e18 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -38,6 +38,8 @@ Buffer *LuaConfigBuffer; Buffer *GCInfoBuffer; Buffer *EventBuffer; Buffer *ScratchBuffer; +Buffer *TraceBuffer; +View *TraceView; String WorkDir; RandomSeed UniqueBufferNameSeed = {}; @@ -74,6 +76,8 @@ void InitScratchBuffer() { View *null_view = CreateView(null_buffer->id); Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID); + TraceBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "trace")); + TraceView = CreateView(TraceBuffer->id); GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "gc")); EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "events")); ScratchBuffer = BufferOpenFile(GetUniqueBufferName(WorkDir, "scratch")); diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 08761f1..fa83398 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -239,6 +239,7 @@ void Update(Event event) { UpdateCo(&event); ReloadLuaConfigs(); CallLuaOnUpdate(&event); + UpdateDebugBuffer(); GarbageCollect(); For(IterateInReverse(&order)) { diff --git a/src/text_editor/title_bar.cpp b/src/text_editor/title_bar.cpp index 53e1086..d218518 100644 --- a/src/text_editor/title_bar.cpp +++ b/src/text_editor/title_bar.cpp @@ -15,8 +15,32 @@ void UpdateDebugBuffer() { float xmouse, ymouse; SDL_GetMouseState(&xmouse, &ymouse); RawAppendf(buffer, "mouse: [%f, %f]\n", xmouse, ymouse); - RawAppendf(buffer, "C:/Work/text_editor/src/text_editor/text_editor.cpp\n"); - RawAppendf(buffer, "config dir: %.*s\n", FmtString(ConfigDir)); + + RawAppendf(buffer, "BufferID id = %d\n", main.buffer->id.id); + RawAppendf(buffer, "Buffer *next = %zu\n", main.buffer->next); + RawAppendf(buffer, "Buffer *prev = %zu\n", main.buffer->prev); + RawAppendf(buffer, "String name = %.*s\n", FmtString(main.buffer->name)); + RawAppendf(buffer, "Int change_id = %lld\n", (long long)main.buffer->change_id); + RawAppendf(buffer, "Int user_change_id = %lld\n", (long long)main.buffer->user_change_id); + RawAppendf(buffer, "Int file_mod_time = %lld\n", (long long)main.buffer->file_mod_time); + RawAppendf(buffer, "\n"); + + RawAppendf(buffer, "U16 *data = %zu\n", main.buffer->data); + RawAppendf(buffer, "Int len = %lld\n", (long long)main.buffer->len); + RawAppendf(buffer, "Int cap = %lld\n", (long long)main.buffer->cap); + RawAppendf(buffer, "Array line_starts = {len = %lld, cap = %lld, data = %zu}\n", (long long)main.buffer->line_starts.len, (long long)main.buffer->line_starts.cap, main.buffer->line_starts.data); + RawAppendf(buffer, "\n"); + + RawAppendf(buffer, "Array undo_stack = {len = %lld, cap = %lld, data = %zu}\n", (long long)main.buffer->undo_stack.len, (long long)main.buffer->undo_stack.cap, main.buffer->undo_stack.data); + RawAppendf(buffer, "Array redo_stack = {len = %lld, cap = %lld, data = %zu}\n", (long long)main.buffer->redo_stack.len, (long long)main.buffer->redo_stack.cap, main.buffer->redo_stack.data); + RawAppendf(buffer, "int edit_phase = %d", main.buffer->edit_phase); + RawAppendf(buffer, "\n"); + + RawAppendf(buffer, "int no_history = %d\n", main.buffer->no_history); + RawAppendf(buffer, "int no_line_starts = %d\n", main.buffer->no_line_starts); + RawAppendf(buffer, "int dirty = %d\n", main.buffer->dirty); + RawAppendf(buffer, "int changed_on_disk = %d\n", main.buffer->changed_on_disk); + RawAppendf(buffer, "int garbage = %d\n", main.buffer->garbage); } void ReplaceTitleBarData(Window *window) { diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index cc3a66e..6b44a19 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -12,6 +12,8 @@ FEATURE Select all searched occurences DESIGN Indicate maybe on the console border that a process is running in the console! also maybe exit code when exits PLATFORM Fix windows build +ISSUE What to do / how should Reopen work after we delete the file on disk in some other program? + - Changing window properties by changing the window name? - commands for scrolling: center, cursor_at_bottom_of_screen, cursor_at_top - Add metadata to Lua bindings so that we would get a better listing (function args?, what else?)