diff --git a/lua/auto-session-autocmds.lua b/lua/auto-session-autocmds.lua index 9a30ce8..f73265b 100644 --- a/lua/auto-session-autocmds.lua +++ b/lua/auto-session-autocmds.lua @@ -16,7 +16,17 @@ M.setup_autocmds = function(config, AutoSession) vim.api.nvim_create_autocmd("DirChangedPre", { callback = function() Lib.logger.debug "DirChangedPre" - Lib.logger.debug("cwd: " .. vim.fn.getcwd()) + Lib.logger.debug(" cwd: " .. vim.fn.getcwd()) + Lib.logger.debug(" target: " .. vim.v.event.directory) + Lib.logger.debug(" changed window: " .. tostring(vim.v.event.changed_window)) + Lib.logger.debug(" scope: " .. vim.v.event.scope) + + -- Don't want to save session if dir change was triggered + -- by a window change. This will corrupt the session data, + -- mixing the two different directory sessions + if vim.v.event.changed_window then + return + end AutoSession.AutoSaveSession() @@ -32,26 +42,37 @@ M.setup_autocmds = function(config, AutoSession) }) if conf.restore_upcoming_session then - vim.api.nvim_create_autocmd("DirChanged", { - callback = function() - Lib.logger.debug "DirChanged" - Lib.logger.debug("cwd: " .. vim.fn.getcwd()) + vim.api.nvim_create_autocmd("DirChanged", { + callback = function() + Lib.logger.debug "DirChanged" + Lib.logger.debug(" cwd: " .. vim.fn.getcwd() ) + Lib.logger.debug(" changed window: " .. tostring(vim.v.event.changed_window)) + Lib.logger.debug(" scope: " .. vim.v.event.scope) + + -- see above + if vim.v.event.changed_window then + return + end + + -- all buffers should've been deleted in `DirChangedPre`, something probably went wrong + if Lib.has_open_buffers() then + Lib.logger.debug("Cancelling session restore") + return + end - -- Deferring to avoid otherwise there are tresitter highlighting issues - vim.defer_fn(function() local success = AutoSession.AutoRestoreSession() if not success then Lib.logger.info("Could not load session. A session file is likely missing for this cwd." .. vim.fn.getcwd()) + return end if type(conf.post_cwd_changed_hook) == "function" then conf.post_cwd_changed_hook() end - end, 50) - end, - pattern = "global", - }) + end, + pattern = "global", + }) end end diff --git a/lua/auto-session-library.lua b/lua/auto-session-library.lua index e6f6f63..98bc53c 100644 --- a/lua/auto-session-library.lua +++ b/lua/auto-session-library.lua @@ -158,6 +158,26 @@ function Lib.expand(file_or_dir) return ret end +function Lib.has_open_buffers() + local result = false + for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do + if vim.fn.bufloaded(bufnr) then + local bufname = vim.api.nvim_buf_get_name(bufnr) + if bufname ~= "" then + if vim.fn.bufwinnr(bufnr) ~= -1 then + if result then + result = true + Lib.logger.debug("There are buffer(s) present: ") + end + Lib.logger.debug(" " .. bufname) + end + end + end + end + return result +end + + function Lib.logger.debug(...) if Lib.conf.log_level == "debug" then vim.notify(vim.fn.join({ "debug: ", tostring(...) }, " "), vim.log.levels.DEBUG) diff --git a/lua/auto-session.lua b/lua/auto-session.lua index 75559c7..cc37821 100644 --- a/lua/auto-session.lua +++ b/lua/auto-session.lua @@ -480,6 +480,19 @@ function AutoSession.RestoreSessionFromFile(session_file) AutoSession.RestoreSession(string.format(AutoSession.get_root_dir() .. "%s.vim", session_file:gsub("/", "%%"))) end +-- +-- Refresh syntax highlighting and file trees +local function post_restore_refresh() + -- refresh sytax highlighting + for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do + if vim.api.nvim_buf_is_loaded(bufnr) then + vim.api.nvim_buf_call(bufnr, function() + vim.cmd 'filetype detect' + end) + end + end +end + -- TODO: make this more readable! ---Restores the session by sourcing the session file if it exists/is readable. ---This function is intended to be called by the user but it is also called by `AutoRestoreSession` @@ -513,6 +526,8 @@ function AutoSession.RestoreSession(sessions_dir_or_file) local post_cmds = AutoSession.get_cmds "post_restore" run_hook_cmds(post_cmds, "post-restore") + + vim.defer_fn(post_restore_refresh, 0) end -- I still don't like reading this chunk, please cleanup