Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c06115fa81
|
||
|
|
b84acda8a4
|
No files matched your search
@@ -5,6 +5,30 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [0.1.2] - 2026-04-12
|
||||
|
||||
### 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.
|
||||
|
||||
## [0.1.1] - 2026-04-09
|
||||
|
||||
### Fixed
|
||||
|
||||
- Save after normal mode changes (undo, redo, paste, delete) by adding
|
||||
`TextChanged` to default events
|
||||
|
||||
## [0.1.0] - 2026-04-09
|
||||
|
||||
### Added
|
||||
@@ -14,4 +38,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Configurable trigger events
|
||||
- Zero-config auto-loading via `plugin/` directory
|
||||
|
||||
[0.1.2]: https://github.com/swaits/tiny-autosave.nvim/releases/tag/v0.1.2
|
||||
[0.1.1]: https://github.com/swaits/tiny-autosave.nvim/releases/tag/v0.1.1
|
||||
[0.1.0]: https://github.com/swaits/tiny-autosave.nvim/releases/tag/v0.1.0
|
||||
@@ -7,7 +7,7 @@ Zero config. ~15 lines of Lua.
|
||||
|
||||
## Features
|
||||
|
||||
- Auto-saves on leaving insert mode and losing focus
|
||||
- Auto-saves on leaving insert mode, normal mode changes (undo, paste, etc.), and losing focus
|
||||
- Only saves normal file buffers (skips special buffers)
|
||||
- Configurable trigger events
|
||||
- Works out of the box — no setup call required
|
||||
@@ -32,7 +32,7 @@ All options are optional. Call `setup()` only if you want to customize:
|
||||
|
||||
```lua
|
||||
require("tiny-autosave").setup({
|
||||
events = { "InsertLeave", "FocusLost" }, -- events that trigger save
|
||||
events = { "InsertLeave", "TextChanged", "FocusLost" }, -- events that trigger save
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
*tiny-autosave.txt* Auto-save buffers on InsertLeave and FocusLost
|
||||
*tiny-autosave.txt* Auto-save buffers on text changes and focus loss
|
||||
|
||||
Author: Stephen Waits <https://github.com/swaits>
|
||||
License: MIT
|
||||
@@ -15,7 +15,8 @@ CONTENTS *tiny-autosave-contents*
|
||||
1. INTRODUCTION *tiny-autosave-introduction*
|
||||
|
||||
tiny-autosave.nvim automatically saves modified buffers when leaving insert
|
||||
mode or when Neovim loses focus. Works out of the box with zero configuration.
|
||||
mode, after normal mode changes (undo, paste, etc.), and when Neovim loses
|
||||
focus. Works out of the box with zero configuration.
|
||||
|
||||
==============================================================================
|
||||
2. REQUIREMENTS *tiny-autosave-requirements*
|
||||
@@ -32,7 +33,7 @@ Add to your |vim.pack.add()| call: >lua
|
||||
That's it. The plugin auto-loads with defaults. To customize, call setup: >lua
|
||||
|
||||
require("tiny-autosave").setup({
|
||||
events = { "InsertLeave", "FocusLost" },
|
||||
events = { "InsertLeave", "TextChanged", "FocusLost" },
|
||||
})
|
||||
<
|
||||
==============================================================================
|
||||
@@ -47,7 +48,7 @@ setup({opts})
|
||||
All options shown with defaults: >lua
|
||||
|
||||
require("tiny-autosave").setup({
|
||||
events = { "InsertLeave", "FocusLost" },
|
||||
events = { "InsertLeave", "TextChanged", "FocusLost" },
|
||||
})
|
||||
<
|
||||
Parameters: ~
|
||||
@@ -55,7 +56,7 @@ setup({opts})
|
||||
|
||||
Fields: ~
|
||||
{events} (table) List of events that trigger auto-save.
|
||||
Default: { "InsertLeave", "FocusLost" }
|
||||
Default: { "InsertLeave", "TextChanged", "FocusLost" }
|
||||
|
||||
==============================================================================
|
||||
vim:tw=78:ts=8:ft=help:norl:
|
||||
@@ -2,13 +2,29 @@ local M = {}
|
||||
|
||||
function M.setup(opts)
|
||||
opts = opts or {}
|
||||
local events = opts.events or { "InsertLeave", "FocusLost" }
|
||||
local events = opts.events or { "InsertLeave", "TextChanged", "FocusLost" }
|
||||
|
||||
vim.opt.autowrite = true
|
||||
vim.opt.autowriteall = true
|
||||
|
||||
vim.api.nvim_create_autocmd(events, {
|
||||
group = vim.api.nvim_create_augroup("tiny-autosave", { clear = true }),
|
||||
callback = function()
|
||||
if vim.bo.buftype == "" and vim.bo.modified then
|
||||
vim.cmd("silent! wall")
|
||||
nested = true,
|
||||
callback = function(args)
|
||||
local buf = args.buf
|
||||
if vim.bo[buf].buftype ~= "" or not vim.bo[buf].modified then
|
||||
return
|
||||
end
|
||||
local name = vim.api.nvim_buf_get_name(buf)
|
||||
if name == "" then
|
||||
return
|
||||
end
|
||||
if vim.fn.filereadable(name) == 0 then
|
||||
vim.fn.mkdir(vim.fn.fnamemodify(name, ":p:h"), "p")
|
||||
end
|
||||
vim.api.nvim_buf_call(buf, function()
|
||||
vim.cmd("silent keepalt keepjumps write")
|
||||
end)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
@@ -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