diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 1bd8e0d..902ba97 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -291,7 +291,6 @@ Color GetColor(String name, Color default_color) { return result; } -BufferID LuaBufferID = {-1}; Int LuaBufferChangeID = 0; void ReloadStyle(); extern String BaseLuaConfig; diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index a380504..8ef6e9c 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -7,19 +7,14 @@ Array Views = {}; Array Windows = {}; WindowID NullWindowID; -BufferID NullBufferID; +BufferID NullBufferID; // +buffer ViewID NullViewID; -WindowID DebugWindowID; +WindowID DebugWindowID; // +debug BufferID DebugBufferID; +BufferID LuaBufferID = {-1}; -// @note: -// Remember that WindowCommand works on window handed it down from HandleEvent -// just because we don't have NextActiveWindow doesn't mean that we work on new -// window all of a sudden in that function call! -WindowID ActiveWindow; -Array WindowSwitchHistory; // @todo: probably better as a circular buffer -Int CaretChangeID; +WindowID ActiveWindow; WindowID ScrollbarSelected = {-1}; WindowID DocumentSelected = {-1}; @@ -340,3 +335,49 @@ View *WindowOpenBufferView(Window *new_parent_window, String name) { new_parent_window->active_view = result->id; return result; } + +bool ViewIsReferenced(ViewID view) { + For(Windows) { + Window *window = it.o; + if (window->active_view == view) return true; + if (window->active_goto_list == view) return true; + } + return false; +} + +bool BufferIsReferenced(BufferID buffer_id) { + if (buffer_id == NullBufferID) return true; + if (buffer_id == DebugBufferID) return true; + if (buffer_id == LuaBufferID) return true; + For(Views) { + View *view = it.o; + if (view->active_buffer == buffer_id) return true; + } + return false; +} + +void GarbageCollect() { + IterRemove(Views) { + IterRemovePrepare(Views); + View *view = it.o; + if (!ViewIsReferenced(view->id)) { + Dealloc(&view->carets); + // @todo: add view to free list + remove_item = true; + } + } + + IterRemove(Buffers) { + IterRemovePrepare(Buffers); + Buffer *buffer = it.o; + if (!BufferIsReferenced(buffer->id)) { + Dealloc(&buffer->line_starts); + Dealloc(&buffer->undo_stack); + Dealloc(&buffer->redo_stack); + Dealloc(buffer->line_starts.allocator, &buffer->data); + // @todo: add buffer to free list + remove_item = true; + ReportConsolef("collected = %.*s", FmtString(buffer->name)); + } + } +} \ No newline at end of file diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 94cecf3..9f25e35 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -156,11 +156,6 @@ Event TranslateSDLEvent(SDL_Event *input_event) { return event; } -void HandleEvent(Event event) { - GlobalCommand(event); - For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o); -} - void Update(Event event) { WindowSize = {(float)event.xwindow, (float)event.ywindow}; LayoutWindows(); @@ -175,11 +170,12 @@ void Update(Event event) { view->update_scroll = true; } - HandleEvent(event); - + GlobalCommand(event); + For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o); UpdateProcesses(); UpdateLua(); UpdateDebugBuffer(); + GarbageCollect(); For(IterateInReverse(&order)) { Window *window = Windows[it].o;