From 7f5d7b23b5adf994c4747d7d308e0e5a6badca41 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 3 Oct 2024 00:13:10 +0200 Subject: [PATCH] fix: clear cursor, buffers, workspace and mappings i think this is everything? --- lua/codemp/buffers.lua | 1 + lua/codemp/workspace.lua | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lua/codemp/buffers.lua b/lua/codemp/buffers.lua index f015c8c..180376d 100644 --- a/lua/codemp/buffers.lua +++ b/lua/codemp/buffers.lua @@ -14,6 +14,7 @@ local function detach(name) local buffer = buffer_id_map[name] id_buffer_map[buffer] = nil buffer_id_map[name] = nil + CODEMP.workspace:get_buffer(name):clear_callback() if not CODEMP.workspace:detach(name) then collectgarbage("collect") -- clear dangling references end diff --git a/lua/codemp/workspace.lua b/lua/codemp/workspace.lua index dc582f7..98894f8 100644 --- a/lua/codemp/workspace.lua +++ b/lua/codemp/workspace.lua @@ -33,13 +33,15 @@ local function fetch_workspaces_list() end local last_jump = { 0, 0 } +local workspace_callback_group = nil ---@param controller CursorController ---@param name string local function register_cursor_callback(controller, name) local once = true + workspace_callback_group = vim.api.nvim_create_augroup("codemp-workspace-" .. name, { clear = true }) vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, { - group = vim.api.nvim_create_augroup("codemp-workspace-" .. name, { clear = true }), + group = workspace_callback_group, callback = function (_ev) if CODEMP.ignore_following_action then CODEMP.ignore_following_action = false @@ -219,13 +221,23 @@ local function join(workspace) end local function leave() - local name = CODEMP.workspace.name + local ws_name = CODEMP.workspace.name + CODEMP.workspace.cursor:clear_callback() + vim.api.nvim_clear_autocmds({ group = workspace_callback_group }) + for id, name in pairs(buffers.map) do + CODEMP.workspace:get_buffer(name):clear_callback() + buffers.map[id] = nil + buffers.map_rev[name] = nil + end + for user, _buf in pairs(buffers.users) do + buffers.users[user] = nil + end CODEMP.workspace = nil if events_poller ~= nil then events_poller:stop() events_poller = nil end - if not CODEMP.client:leave_workspace(name) then + if not CODEMP.client:leave_workspace(ws_name) then collectgarbage("collect") -- TODO codemp disconnects when all references to its objects are dropped. since it -- hands out Arc<> of things, all references still not garbage collected in Lua will @@ -233,7 +245,7 @@ local function leave() -- only happens when manually requested, and it's not like the extra garbage collection -- is an effort for nothing... still it would be more elegant to not need this!! end - print(" -- left workspace " .. name) + print(" -- left workspace " .. ws_name) require('codemp.window').update() end