From f134f34512d9957120e87dab47783913a120afda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20M=C3=BCller?= Date: Mon, 3 Jul 2023 16:29:54 +0200 Subject: [PATCH] feat!: Replace callbacks with autocommand events (#25) --- README.md | 33 ++++++++++++++++++++++++---- lua/auto-save/config.lua | 3 --- lua/auto-save/init.lua | 30 ++++++++++--------------- lua/auto-save/utils/autocommands.lua | 18 +++++++++++++++ lua/auto-save/utils/data.lua | 8 ------- 5 files changed, 58 insertions(+), 34 deletions(-) create mode 100644 lua/auto-save/utils/autocommands.lua diff --git a/README.md b/README.md index adecb04..c9a06a2 100644 --- a/README.md +++ b/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. diff --git a/lua/auto-save/config.lua b/lua/auto-save/config.lua index ce95514..65178b4 100644 --- a/lua/auto-save/config.lua +++ b/lua/auto-save/config.lua @@ -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 }, diff --git a/lua/auto-save/init.lua b/lua/auto-save/init.lua index d0cd0a1..24947a5 100644 --- a/lua/auto-save/init.lua +++ b/lua/auto-save/init.lua @@ -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 diff --git a/lua/auto-save/utils/autocommands.lua b/lua/auto-save/utils/autocommands.lua new file mode 100644 index 0000000..8224d85 --- /dev/null +++ b/lua/auto-save/utils/autocommands.lua @@ -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 diff --git a/lua/auto-save/utils/data.lua b/lua/auto-save/utils/data.lua index 68579bc..8057565 100644 --- a/lua/auto-save/utils/data.lua +++ b/lua/auto-save/utils/data.lua @@ -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