mirror of
https://github.com/zoriya/auto-save.nvim.git
synced 2025-12-06 06:36:11 +00:00
feat!: Replace callbacks with autocommand events (#25)
This commit is contained in:
33
README.md
33
README.md
@@ -33,7 +33,7 @@
|
||||
- execution message (it can be dimmed and personalized)
|
||||
- events that trigger auto-save
|
||||
- debounce the save with a delay
|
||||
- multiple callbacks
|
||||
- hook into the lifecycle with autocommands
|
||||
- automatically clean the message area
|
||||
|
||||
## 📚 Requirements
|
||||
@@ -113,9 +113,6 @@ EOF
|
||||
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
|
||||
before_saving = nil, -- ran before doing the actual save
|
||||
},
|
||||
-- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable
|
||||
debug = false,
|
||||
}
|
||||
@@ -185,6 +182,34 @@ or as part of the `lazy.nvim` plugin spec:
|
||||
|
||||
```
|
||||
|
||||
## 🪝 Events / Callbacks
|
||||
|
||||
The plugin fires events at various points during its lifecycle which users can hook into:
|
||||
|
||||
- `AutoSaveWritePre` Just before a buffer is getting saved
|
||||
- `AutoSaveWritePost` Just after a buffer is getting saved
|
||||
|
||||
It will always supply the current buffer in the `data.saved_buffer`
|
||||
|
||||
An example to always print the file name before a file is getting saved (use `:messages` if the execution message swallows the print):
|
||||
|
||||
```lua
|
||||
local group = vim.api.nvim_create_augroup('autosave', {})
|
||||
|
||||
vim.api.nvim_create_autocmd('User', {
|
||||
pattern = 'AutoSaveWritePre',
|
||||
group = group,
|
||||
callback = function(opts)
|
||||
if opts.data.saved_buffer ~= nil then
|
||||
local filename = vim.api.nvim_buf_get_name(opts.data.saved_buffer)
|
||||
print('We are about to save ' .. filename .. ' get ready captain!')
|
||||
end
|
||||
end,
|
||||
})
|
||||
```
|
||||
|
||||
If you want more Events, feel free to open an issue.
|
||||
|
||||
## 🤝 Contributing
|
||||
|
||||
- All pull requests are welcome.
|
||||
|
||||
@@ -21,9 +21,6 @@ Config = {
|
||||
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
|
||||
before_saving = nil, -- ran before doing the actual save
|
||||
},
|
||||
-- log debug messages to 'auto-save.log' file in neovim cache directory, set to `true` to enable
|
||||
debug = false, -- print debug messages, set to `true` to enable
|
||||
},
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
local M = {}
|
||||
|
||||
local cnf = require("auto-save.config")
|
||||
local callback = require("auto-save.utils.data").do_callback
|
||||
local colors = require("auto-save.utils.colors")
|
||||
local echo = require("auto-save.utils.echo")
|
||||
local autocmds = require("auto-save.utils.autocommands")
|
||||
local logger
|
||||
local autosave_running
|
||||
local api = vim.api
|
||||
local g = vim.g
|
||||
local fn = vim.fn
|
||||
local cmd = vim.cmd
|
||||
local o = vim.o
|
||||
@@ -15,9 +14,7 @@ local AUTO_SAVE_COLOR = "MsgArea"
|
||||
local BLACK = "#000000"
|
||||
local WHITE = "#ffffff"
|
||||
|
||||
api.nvim_create_augroup("AutoSave", {
|
||||
clear = true,
|
||||
})
|
||||
autocmds.create_augroup({ clear = true })
|
||||
|
||||
local timers_by_buffer = {}
|
||||
|
||||
@@ -81,13 +78,7 @@ local function save(buf)
|
||||
return
|
||||
end
|
||||
|
||||
callback("before_saving")
|
||||
|
||||
-- why is this needed? auto_save_abort is never set to true?
|
||||
-- TODO: remove?
|
||||
if g.auto_save_abort == true then
|
||||
return
|
||||
end
|
||||
autocmds.exec_autocmd("AutoSaveWritePre", buf)
|
||||
|
||||
if cnf.opts.write_all_buffers then
|
||||
cmd("silent! wall")
|
||||
@@ -97,6 +88,7 @@ local function save(buf)
|
||||
end)
|
||||
end
|
||||
|
||||
autocmds.exec_autocmd("AutoSaveWritePost", buf)
|
||||
logger.log(buf, "Saved buffer")
|
||||
|
||||
if cnf.opts.execution_message.enabled == true then
|
||||
@@ -120,13 +112,15 @@ local function defer_save(buf)
|
||||
end
|
||||
|
||||
function M.on()
|
||||
local augroup = autocmds.create_augroup()
|
||||
|
||||
api.nvim_create_autocmd(cnf.opts.trigger_events.immediate_save, {
|
||||
callback = function(opts)
|
||||
if should_be_saved(opts.buf) then
|
||||
immediate_save(opts.buf)
|
||||
end
|
||||
end,
|
||||
group = "AutoSave",
|
||||
group = augroup,
|
||||
desc = "Immediately save a buffer",
|
||||
})
|
||||
api.nvim_create_autocmd(cnf.opts.trigger_events.defer_save, {
|
||||
@@ -135,7 +129,7 @@ function M.on()
|
||||
defer_save(opts.buf)
|
||||
end
|
||||
end,
|
||||
group = "AutoSave",
|
||||
group = augroup,
|
||||
desc = "Save a buffer after the `debounce_delay`",
|
||||
})
|
||||
api.nvim_create_autocmd(cnf.opts.trigger_events.cancel_defered_save, {
|
||||
@@ -144,7 +138,7 @@ function M.on()
|
||||
cancel_timer(opts.buf)
|
||||
end
|
||||
end,
|
||||
group = "AutoSave",
|
||||
group = augroup,
|
||||
desc = "Cancel a pending save timer for a buffer",
|
||||
})
|
||||
|
||||
@@ -175,16 +169,14 @@ function M.on()
|
||||
end
|
||||
end)
|
||||
end,
|
||||
group = "AutoSave",
|
||||
group = augroup,
|
||||
})
|
||||
|
||||
autosave_running = true
|
||||
end
|
||||
|
||||
function M.off()
|
||||
api.nvim_create_augroup("AutoSave", {
|
||||
clear = true,
|
||||
})
|
||||
autocmds.create_augroup({ clear = true })
|
||||
|
||||
autosave_running = false
|
||||
end
|
||||
|
||||
18
lua/auto-save/utils/autocommands.lua
Normal file
18
lua/auto-save/utils/autocommands.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
local M = {}
|
||||
|
||||
local api = vim.api
|
||||
local augroup_name = "AutoSave"
|
||||
|
||||
--- @param opts? table
|
||||
M.create_augroup = function(opts)
|
||||
opts = opts or {}
|
||||
api.nvim_create_augroup(augroup_name, opts)
|
||||
end
|
||||
|
||||
--- @param pattern string
|
||||
--- @param saved_buffer number
|
||||
M.exec_autocmd = function(pattern, saved_buffer)
|
||||
api.nvim_exec_autocmds("User", { pattern = pattern, data = { saved_buffer = saved_buffer } })
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -14,12 +14,4 @@ function M.not_in(var, arr)
|
||||
end
|
||||
end
|
||||
|
||||
function M.do_callback(callback_name)
|
||||
local cnf = require("auto-save.config").opts
|
||||
|
||||
if type(cnf.callbacks[callback_name]) == "function" then
|
||||
cnf.callbacks[callback_name]()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user