fix undo tracking

This commit is contained in:
Jonathan Apodaca 2024-10-22 08:08:29 -06:00
parent 3c948ac985
commit 6d49d1a0e4

View File

@ -12,6 +12,11 @@ function M.set(cmd)
if cmd ~= nil then vim.b.tt_repeatcmd = cmd end if cmd ~= nil then vim.b.tt_repeatcmd = cmd end
end end
local function tt_was_last_repeatable()
local ts, tt_ts, tt_cmd = vim.b.changedtick, vim.b.tt_changedtick, vim.b.tt_repeatcmd
return tt_ts ~= nil and ts <= tt_ts
end
---@generic T ---@generic T
---@param cmd string|fun():T ---@param cmd string|fun():T
---@return T ---@return T
@ -24,14 +29,7 @@ end
function M.do_repeat() function M.do_repeat()
local ts, tt_ts, tt_cmd = vim.b.changedtick, vim.b.tt_changedtick, vim.b.tt_repeatcmd local ts, tt_ts, tt_cmd = vim.b.changedtick, vim.b.tt_changedtick, vim.b.tt_repeatcmd
if if not tt_was_last_repeatable() or (type(tt_cmd) ~= 'function' and type(tt_cmd) ~= 'string') then
-- (force formatter break)
tt_ts == nil
or tt_cmd == nil
-- has the buffer been modified after we last modified it?
or ts > tt_ts
or (type(tt_cmd) ~= 'function' and type(tt_cmd) ~= 'string')
then
return M.native_repeat() return M.native_repeat()
end end
@ -49,10 +47,13 @@ function M.do_repeat()
end end
function M.undo() function M.undo()
local tt_was_last_repeatable_before_undo = tt_was_last_repeatable()
M.native_undo() M.native_undo()
-- Update the current TS on the next event tick, if tt_was_last_repeatable_before_undo then
-- to make sure we get the latest -- Update the current TS on the next event tick,
vim.schedule(M.set) -- to make sure we get the latest
M.set()
end
end end
function M.setup() function M.setup()