fix: unnecessary shell commands when autosave off (#85)

* fix: unnecessary shell commands when autosave off

Problem:    Shell programs aren't able to detect nvim exit.

Solution:   Avoid running shell-commands unnecessarily on exit.

Programs that spawn nvim in-order to edit a file are unable to detect
nvim exit cleanly because of a race-condition that happens in
`VimLeavePre` and `get_current()` which runs shell commands during exit
even when `autosave` is off.

For example, using nvim nightly, run persisted.nvim with:

```lua
opts.should_autosave = function()
	-- Do not autosave if git commit/rebase session.
	return vim.env.GIT_EXEC_PATH == nil
end
```

And run `EDITOR=nvim git commit` from shell; git will fail waiting for
nvim to exit cleanly.

* fix: global variable in lowercase initial
main
Rafael Bodill 2023-08-11 16:07:50 +03:00 committed by GitHub
parent 2de1fe69e7
commit 315cd1a8a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 31 deletions

View File

@ -135,18 +135,23 @@ function M.stop()
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedStateChange", data = { action = "stop" } })
end
---Write the session to disk
---@param session string
---@return nil
local function write(session)
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePre" })
vim.cmd("mks! " .. e(session))
vim.g.persisting = true
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePost" })
end
---Save the session
---@param opt? table
---@return nil
function M.save(opt)
opt = opt or {}
local session = opt.session or (vim.g.persisted_branch_session or vim.g.persisting_session or get_current())
if opt.session then
write(session)
return
end
if not opt.session then
-- Do not save the session if the user has manually stopped it, but if there's an override, then do it
if (vim.g.persisting == false or vim.g.persisting == nil) and not opt.override then
return
@ -161,20 +166,12 @@ function M.save(opt)
if type(config.options.should_autosave) == "function" and not config.options.should_autosave() then
return
end
end
local session = opt.session or (vim.g.persisted_branch_session or vim.g.persisting_session or get_current())
write(session)
end
---Write the session to disk
---@param session string
---@return nil
function write(session)
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePre" })
vim.cmd("mks! " .. e(session))
vim.g.persisting = true
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePost" })
end
---Delete the current session
---@return nil
function M.delete()