From a001a96abf2c6de7f6af5deb6aad24bc9bf52baa Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 27 Sep 2024 20:52:56 +0200 Subject: [PATCH] feat: ask what to do when out of sync --- lua/codemp/buffers.lua | 52 +++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/lua/codemp/buffers.lua b/lua/codemp/buffers.lua index 922ed28..93ed36c 100644 --- a/lua/codemp/buffers.lua +++ b/lua/codemp/buffers.lua @@ -8,6 +8,21 @@ local buffer_id_map = {} local user_buffer_name = {} local ticks = {} +---@param name string +--TODO this should happen at the level above (Workspace) but accesses tables on this level, badly designed! +local function detach(name) + local buffer = buffer_id_map[name] + id_buffer_map[buffer] = nil + buffer_id_map[name] = nil + if not CODEMP.workspace:detach(name) then + collectgarbage("collect") -- clear dangling references + end + + print(" -- detached from buffer " .. name) + + require('codemp.window').update() +end + ---@class AttachOptions ---@field content? string if provided, set this as content after attaching ---@field nowait? boolean skip waiting for initial content sync @@ -127,10 +142,24 @@ local function attach(name, opts) utils.buffer.set_content(buffer, event.content, event.start, event.finish) if event.hash ~= nil then if CODEMP.native.hash(utils.buffer.get_content(buffer)) ~= event.hash then - -- OUT OF SYNC! - -- TODO this may be destructive! we should probably prompt the user before doing this - print(" /!\\ out of sync, resynching...") - utils.buffer.set_content(buffer, controller:content():await()) + if CODEMP.config.auto_sync then + print(" /!\\ out of sync, resynching...") + utils.buffer.set_content(buffer, controller:content():await()) + else + vim.ui.select( + { "sync", "detach" }, + { prompt = "out of sync! force resync or detach?" }, + function (choice) + if not choice then return end + if choice == "sync" then + utils.buffer.set_content(buffer, controller:content():await()) + end + if choice == "detach" then + detach(name) + end + end + ) + end return end end @@ -162,21 +191,6 @@ local function attach(name, opts) end) end ----@param name string ---TODO this should happen at the level above (Workspace) but accesses tables on this level, badly designed! -local function detach(name) - local buffer = buffer_id_map[name] - id_buffer_map[buffer] = nil - buffer_id_map[name] = nil - if not CODEMP.workspace:detach(name) then - collectgarbage("collect") -- clear dangling references - end - - print(" -- detached from buffer " .. name) - - require('codemp.window').update() -end - ---@param buffer? integer if provided, sync given buffer id, otherwise sync current buf local function sync(buffer) if buffer == nil then