From 315cd1a8a501ca8e0c1d55f0c245b9cc0e1ffa01 Mon Sep 17 00:00:00 2001 From: Rafael Bodill Date: Fri, 11 Aug 2023 16:07:50 +0300 Subject: [PATCH] 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 --- lua/persisted/init.lua | 59 ++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index add28f3..0a027cb 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -135,46 +135,43 @@ 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 {} + + 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 + end + + -- Do not save the session if autosave is turned off...unless it's overriden + if not config.options.autosave and not opt.override then + return + end + + -- Do not save the session if the callback returns false + 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()) - - if opt.session then - write(session) - return - end - - -- 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 - end - - -- Do not save the session if autosave is turned off...unless it's overriden - if not config.options.autosave and not opt.override then - return - end - - -- Do not save the session if the callback returns false - if type(config.options.should_autosave) == "function" and not config.options.should_autosave() then - return - end - 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()