(repeat) refactor slightly

This commit is contained in:
Jonathan Apodaca 2024-10-23 16:50:58 -06:00
parent 6d49d1a0e4
commit f3e352ceda

View File

@ -5,15 +5,16 @@ local function _normal(cmd) vim.cmd.normal { cmd = 'normal', args = { cmd }, ban
M.native_repeat = function() _normal '.' end M.native_repeat = function() _normal '.' end
M.native_undo = function() _normal 'u' end M.native_undo = function() _normal 'u' end
local function update_ts() vim.b.tt_changedtick = vim.b.changedtick end
---@param cmd? string|fun():unknown ---@param cmd? string|fun():unknown
function M.set(cmd) function M.set(cmd)
local ts = vim.b.changedtick update_ts()
vim.b.tt_changedtick = ts
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 function tt_was_last_repeatable()
local ts, tt_ts, tt_cmd = vim.b.changedtick, vim.b.tt_changedtick, vim.b.tt_repeatcmd local ts, tt_ts = vim.b.changedtick, vim.b.tt_changedtick
return tt_ts ~= nil and ts <= tt_ts return tt_ts ~= nil and ts <= tt_ts
end end
@ -23,12 +24,12 @@ end
function M.run(cmd) function M.run(cmd)
M.set(cmd) M.set(cmd)
local result = cmd() local result = cmd()
M.set() update_ts()
return result return result
end 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 tt_cmd = vim.b.tt_repeatcmd
if not tt_was_last_repeatable() or (type(tt_cmd) ~= 'function' and type(tt_cmd) ~= 'string') then if not tt_was_last_repeatable() or (type(tt_cmd) ~= 'function' and type(tt_cmd) ~= 'string') then
return M.native_repeat() return M.native_repeat()
end end
@ -49,11 +50,7 @@ end
function M.undo() function M.undo()
local tt_was_last_repeatable_before_undo = tt_was_last_repeatable() local tt_was_last_repeatable_before_undo = tt_was_last_repeatable()
M.native_undo() M.native_undo()
if tt_was_last_repeatable_before_undo then if tt_was_last_repeatable_before_undo then update_ts() end
-- Update the current TS on the next event tick,
-- to make sure we get the latest
M.set()
end
end end
function M.setup() function M.setup()