Skip to content

Commit

Permalink
Merge pull request #305 from cameronr/main
Browse files Browse the repository at this point in the history
Fix #223, where session is loaded into Lazy window
  • Loading branch information
rmagatti authored Jun 21, 2024
2 parents 92744f5 + ced9c18 commit 7d7eeac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ require('lualine').setup{
| auto_session_create_enabled | false, true | true | Enables/disables the plugin's session auto creation |
| auto_save_enabled | false, true, nil | nil | Enables/disables auto saving |
| auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring |
| auto_restore_lazy_delay_enabled | false, true, nil | true | Enables/disables delaying auto-restore if Lazy.nvim is used |
| auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs |
| auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs |
| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name |
Expand Down
60 changes: 59 additions & 1 deletion lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ end
---@field auto_session_create_enabled boolean|nil Enables/disables auto creating new sessions
---@field auto_save_enabled? boolean Enables/disables auto saving session
---@field auto_restore_enabled? boolean Enables/disables auto restoring session
---@field auto_restore_lazy_delay_enabled? boolean Automatically detect if Lazy.nvim is being used and wait until Lazy is done to make sure session is restored correctly. Does nothing if Lazy isn't being used. Can be disabled if a problem is suspected or for debugging
---@field auto_session_suppress_dirs? table Suppress auto session for directories
---@field auto_session_allowed_dirs? table Allow auto session for directories, if empty then all directories are allowed except for suppressed ones
---@field auto_session_use_git_branch? boolean Include git branch name in session name to differentiate between sessions for different git branches
Expand All @@ -55,6 +56,7 @@ local defaultConf = {
auto_session_create_enabled = nil, -- Enables/disables auto creating new sessions
auto_save_enabled = nil, -- Enables/disables auto save feature
auto_restore_enabled = nil, -- Enables/disables auto restore feature
auto_restore_lazy_delay_enabled = true, -- Enables/disables Lazy delay feature
auto_session_suppress_dirs = nil, -- Suppress session restore/create in certain directories
auto_session_allowed_dirs = nil, -- Allow session restore/create in certain directories
auto_session_use_git_branch = vim.g.auto_session_use_git_branch or false, -- Include git branch name in session name
Expand Down Expand Up @@ -940,14 +942,42 @@ function SetupAutocmds()
end,
})

-- Used to track the Lazy window if we're delaying loading until it's dismissed
local lazy_view_win = nil
vim.api.nvim_create_autocmd({ "VimEnter" }, {
group = group,
pattern = "*",
nested = true,
callback = function()
if not vim.g.in_pager_mode then
if vim.g.in_pager_mode then
-- Don't auto restore session in pager mode
return
end

if not AutoSession.conf.auto_restore_lazy_delay_enabled then
-- If auto_restore_lazy_delay_enabled is false, just restore the session as normal
AutoSession.AutoRestoreSession()
return
end

-- Not in pager mode, auto_restore_lazy_delay_enabled is true, check for Lazy
local ok, lazy_view = pcall(require, "lazy.view")
if not ok then
-- No Lazy, load as usual
AutoSession.AutoRestoreSession()
return
end

if not lazy_view.visible() then
-- Lazy isn't visible, load as usual
Lib.logger.debug "Lazy is loaded, but not visible, restore session!"
AutoSession.AutoRestoreSession()
return
end

-- If the Lazy window is visibile, hold onto it for later
lazy_view_win = lazy_view.view.win
Lib.logger.debug "Lazy window is still visible, waiting for it to close"
end,
})

Expand All @@ -963,6 +993,34 @@ function SetupAutocmds()

-- Set a flag to indicate that the plugin has been loaded
vim.g.loaded_auto_session = true

if AutoSession.conf.auto_restore_lazy_delay_enabled then
-- Helper to delay loading the session if the Lazy.nvim window is open
vim.api.nvim_create_autocmd("WinClosed", {
callback = function(event)
-- If we we're in pager mode or we have no Lazy window, bail out
if vim.g.in_pager_mode or not lazy_view_win then
return
end

if event.match ~= tostring(lazy_view_win) then
-- A window was closed, but it wasn't Lazy's window so keep waiting
Lib.logger.debug "A window was closed but it was not Lazy, keep waiting"
return
end

Lib.logger.debug "Lazy window was closed, restore the session!"

-- Clear lazy_view_win so we stop processing future WinClosed events
lazy_view_win = nil
-- Schedule restoration for the next pass in the event loop to time for the window to close
-- Not doing this could create a blank buffer in the restored session
vim.schedule(function()
AutoSession.AutoRestoreSession()
end)
end,
})
end
end

return AutoSession

0 comments on commit 7d7eeac

Please sign in to comment.