mirror of
https://github.com/zoriya/auto-save.nvim.git
synced 2025-12-06 06:36:11 +00:00
refactor!: Assert condition before setting timers (#16)
This solves #12 See #16 for more details.
This commit is contained in:
@@ -11,27 +11,19 @@ Config = {
|
||||
},
|
||||
trigger_events = { -- See :h events
|
||||
immediate_save = { "BufLeave", "FocusLost" }, -- vim events that trigger an immediate save
|
||||
defer_save = { "InsertLeave", "TextChanged" }, -- vim events that trigger a deferred save (saves after `debounce_delay`)
|
||||
cancel_defered_save = { "InsertEnter" }, -- vim events that cancel a pending deferred save
|
||||
defer_save = { "InsertLeave", "TextChanged" }, -- vim events that trigger a deferred save (saves after `debounce_delay`)
|
||||
cancel_defered_save = { "InsertEnter" }, -- vim events that cancel a pending deferred save
|
||||
},
|
||||
-- function that determines whether to save the current buffer or not
|
||||
-- function that takes the buffer handle and determines whether to save the current buffer or not
|
||||
-- return true: if buffer is ok to be saved
|
||||
-- return false: if it's not ok to be saved
|
||||
condition = function(buf)
|
||||
local fn = vim.fn
|
||||
local utils = require("auto-save.utils.data")
|
||||
|
||||
if fn.getbufvar(buf, "&modifiable") == 1 and utils.not_in(fn.getbufvar(buf, "&filetype"), {}) then
|
||||
return true -- met condition(s), can save
|
||||
end
|
||||
return false -- can't save
|
||||
end,
|
||||
-- if set to `nil` then no specific condition is applied
|
||||
condition = nil,
|
||||
write_all_buffers = false, -- write all buffers when the current one meets `condition`
|
||||
debounce_delay = 1000, -- delay after which a pending save is executed
|
||||
callbacks = { -- functions to be executed at different intervals
|
||||
enabling = nil, -- ran when enabling auto-save
|
||||
disabling = nil, -- ran when disabling auto-save
|
||||
before_asserting_save = nil, -- ran before checking `condition`
|
||||
before_saving = nil, -- ran before doing the actual save
|
||||
after_saving = nil, -- ran after doing the actual save
|
||||
},
|
||||
|
||||
@@ -52,13 +52,22 @@ local function echo_execution_message()
|
||||
end
|
||||
end
|
||||
|
||||
local function save(buf)
|
||||
callback("before_asserting_save")
|
||||
|
||||
if cnf.opts.condition(buf) == false then
|
||||
return
|
||||
--- Determines if the given buffer is modifiable and if the condition from the config yields true for it
|
||||
--- @param buf number
|
||||
--- @return boolean
|
||||
local function should_be_saved(buf)
|
||||
if fn.getbufvar(buf, "&modifiable") ~= 1 then
|
||||
return false
|
||||
end
|
||||
|
||||
if cnf.opts.condition ~= nil then
|
||||
return cnf.opts.condition(buf)
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local function save(buf)
|
||||
if not api.nvim_buf_get_option(buf, "modified") then
|
||||
return
|
||||
end
|
||||
@@ -86,19 +95,13 @@ local function save(buf)
|
||||
end
|
||||
end
|
||||
|
||||
function M.immediate_save(buf)
|
||||
buf = buf or api.nvim_get_current_buf()
|
||||
cancel_timer(buf)
|
||||
save(buf)
|
||||
local function immediate_save(buf)
|
||||
cancel_timer(buf)
|
||||
save(buf)
|
||||
end
|
||||
|
||||
|
||||
local save_func = nil
|
||||
local function defer_save(buf)
|
||||
-- why is this needed? auto_save_abort is never set to true anyways?
|
||||
-- TODO: remove?
|
||||
g.auto_save_abort = false
|
||||
|
||||
-- is it really needed to cache this function
|
||||
-- TODO: remove?
|
||||
if save_func == nil then
|
||||
@@ -109,25 +112,31 @@ end
|
||||
|
||||
function M.on()
|
||||
api.nvim_create_autocmd(cnf.opts.trigger_events.immediate_save, {
|
||||
callback = function (opts)
|
||||
M.immediate_save(opts.buf)
|
||||
callback = function(opts)
|
||||
if should_be_saved(opts.buf) then
|
||||
immediate_save(opts.buf)
|
||||
end
|
||||
end,
|
||||
group = "AutoSave",
|
||||
desc = "Immediately save a buffer"
|
||||
desc = "Immediately save a buffer",
|
||||
})
|
||||
api.nvim_create_autocmd(cnf.opts.trigger_events.defer_save, {
|
||||
callback = function(opts)
|
||||
defer_save(opts.buf)
|
||||
if should_be_saved(opts.buf) then
|
||||
defer_save(opts.buf)
|
||||
end
|
||||
end,
|
||||
group = "AutoSave",
|
||||
desc = "Save a buffer after the `debounce_delay`"
|
||||
desc = "Save a buffer after the `debounce_delay`",
|
||||
})
|
||||
api.nvim_create_autocmd(cnf.opts.trigger_events.cancel_defered_save, {
|
||||
callback = function (opts)
|
||||
cancel_timer(opts.buf)
|
||||
callback = function(opts)
|
||||
if should_be_saved(opts.buf) then
|
||||
cancel_timer(opts.buf)
|
||||
end
|
||||
end,
|
||||
group = "AutoSave",
|
||||
desc = "Cancel a pending save timer for a buffer"
|
||||
desc = "Cancel a pending save timer for a buffer",
|
||||
})
|
||||
|
||||
api.nvim_create_autocmd({ "VimEnter", "ColorScheme", "UIEnter" }, {
|
||||
|
||||
Reference in New Issue
Block a user