Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Replace callbacks with autocommand events #25

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions lua/auto-save/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
30 changes: 11 additions & 19 deletions lua/auto-save/init.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
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
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 = {}

Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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, {
Expand All @@ -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, {
Expand All @@ -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",
})

Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lua/auto-save/utils/autocommands.lua
Original file line number Diff line number Diff line change
@@ -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
8 changes: 0 additions & 8 deletions lua/auto-save/utils/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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