Skip to content

Commit

Permalink
feat: save all buffers upon suspending neovim
Browse files Browse the repository at this point in the history
vim's `autowrite` option is no longer needed for this
  • Loading branch information
tmillr committed May 7, 2023
1 parent a1e03dd commit 3e84652
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ require("sos").setup {
-- open an older version of a file in another application/Neovim instance,
-- although in that case you're probably better off disabling autosaving
-- altogether (or keep it enabled but utilize a VCS to get the version you
-- need - that is, if you commit frequently enough).
-- need - that is, if you commit frequently enough). This option also enables
-- saving on suspend.
save_on_focuslost = true,

-- Predicate fn which receives a buf number and should return true if it
Expand Down
11 changes: 11 additions & 0 deletions lua/sos/_test/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ function M.start_nvim(opts)
vim.fn.jobstop(jobid)
end

function self:exec_lua(f, args)
if type(f) == "string" then
return self:req("nvim_exec_lua", f, args or {})
end
return self:req(
"nvim_exec_lua",
("return assert(loadstring(%q))(...)"):format(string.dump(f)),
args or {}
)
end

setmetatable(self, {
__index = function(_, key)
return M[key]
Expand Down
11 changes: 11 additions & 0 deletions lua/sos/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ function M.refresh(cfg)
group = augroup,
pattern = "*",
desc = "Save all buffers when Neovim loses focus",
nested = true,
callback = function(_info)
cfg.on_timer()
end,
})

api.nvim_create_autocmd("VimSuspend", {
group = augroup,
pattern = "*",
desc = "Save all buffers when Neovim is suspended",
nested = true,
callback = function(_info)
cfg.on_timer()
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/sos/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
---@field autowrite boolean | "all" | nil # Set and manage Vim's 'autowrite' option.
---@field save_on_cmd "all" | "some" | table<string, true> | false | nil # Save all buffers before executing a command on cmdline
---@field save_on_bufleave boolean | nil # Save current buffer on `BufLeave` (see `:h BufLeave`)
---@field save_on_focuslost boolean | nil # Save all bufs when Neovim loses focus.
---@field save_on_focuslost boolean | nil # Save all bufs when Neovim loses focus or is suspended.
---@field should_observe_buf nil | fun(buf: integer): boolean # Return true to observe/attach to buf.
---@field on_timer function # The function to call when the timer fires.
local defaults = {
Expand Down
27 changes: 27 additions & 0 deletions tests/suspend_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,31 @@ describe("sos.nvim", function()
nvim:set_current_tabpage(tab) -- trigger sos to check file times (which triggers autoread)
assert.are.same({ "new new new" }, nvim:buf_get_lines(0, 0, -1, true))
end)

it("should save all bufs on suspend", function()
util.with_nvim({
xargs = {
"-u",
"tests/min_init.lua",
},
}, function(nvim)
local tmp_a = util.tmpfile()
local tmp_b = util.tmpfile()
nvim:set_option("awa", false)
nvim:set_option("aw", false)
nvim:set_option("hidden", true)
nvim:silent_edit(tmp_a)
nvim:buf_set_lines(0, 0, -1, true, { "changes" })
nvim:silent_edit(tmp_b)
nvim:buf_set_lines(0, 0, -1, true, { "changes" })
nvim:exec_lua(function()
return require("sos").setup { enabled = true, timeout = 9e6 }
end)
sleep(200)
nvim:suspend()
sleep(200)
assert.is.True(util.file_exists(tmp_a))
assert.is.True(util.file_exists(tmp_b))
end)
end)
end)

0 comments on commit 3e84652

Please sign in to comment.