chore: release 0.1.2
### Fixed - `BufWritePre` formatters (e.g. conform.nvim format-on-save) now run on autosave. The trigger autocmd now uses `nested = true`, so the `:write` invoked from inside the `InsertLeave` / `TextChanged` / `FocusLost` handler fires nested autocmds (see `:help autocmd-nested`). ### Changed - Skip saves for unmodified, unnamed, or special (`buftype`) buffers instead of always attempting a write. - Drop `:wall` — autosave only writes the buffer the event fired on. - Use `silent` (not `silent!`) so write errors surface instead of being swallowed.
This commit is contained in:
3 files changed
+81
-5
No files matched your search
@@ -21,4 +21,53 @@ T["setup"]["respects custom events"] = function()
|
||||
eq(#groups, 1)
|
||||
end
|
||||
|
||||
T["save"] = MiniTest.new_set()
|
||||
|
||||
T["save"]["fires BufWritePre once and applies formatter changes"] = function()
|
||||
require("tiny-autosave").setup()
|
||||
local tmp = vim.fn.tempname() .. ".txt"
|
||||
vim.cmd("edit " .. tmp)
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
local pre_count = 0
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
buffer = buf,
|
||||
callback = function()
|
||||
pre_count = pre_count + 1
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { "formatted" })
|
||||
end,
|
||||
})
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { "original", "", "", "" })
|
||||
vim.api.nvim_exec_autocmds("InsertLeave", { buffer = buf })
|
||||
eq(pre_count, 1)
|
||||
eq(vim.fn.readfile(tmp), { "formatted" })
|
||||
os.remove(tmp)
|
||||
end
|
||||
|
||||
T["save"]["skips unnamed buffers"] = function()
|
||||
require("tiny-autosave").setup()
|
||||
vim.cmd("enew")
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { "scratch" })
|
||||
local ok = pcall(vim.api.nvim_exec_autocmds, "InsertLeave", {})
|
||||
eq(ok, true)
|
||||
end
|
||||
|
||||
T["save"]["skips unmodified buffers"] = function()
|
||||
require("tiny-autosave").setup()
|
||||
local tmp = vim.fn.tempname() .. ".txt"
|
||||
vim.fn.writefile({ "hello" }, tmp)
|
||||
vim.cmd("edit " .. tmp)
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
local pre_count = 0
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
buffer = buf,
|
||||
callback = function()
|
||||
pre_count = pre_count + 1
|
||||
end,
|
||||
})
|
||||
vim.api.nvim_exec_autocmds("InsertLeave", { buffer = buf })
|
||||
eq(pre_count, 0)
|
||||
os.remove(tmp)
|
||||
end
|
||||
|
||||
return T
|
||||
Reference in new issue
Block a user