Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File tree integration with nvim-tree and NERDTree #179

Merged
merged 4 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 76 additions & 32 deletions lua/auto-session-autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ local Lib = require "auto-session-library"

local M = {}

-- don't autorestore if there are open buffers (indicating auto save session failed)
local function 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

---Setup autocmds for DirChangedPre and DirChanged
---@param config table auto session config
---@param AutoSession table auto session instance
Expand All @@ -12,46 +32,70 @@ M.setup_autocmds = function(config, AutoSession)
end

local conf = config.cwd_change_handling
local scopes = { "global", "tabpage"}

vim.api.nvim_create_autocmd("DirChangedPre", {
callback = function()
Lib.logger.debug "DirChangedPre"
Lib.logger.debug("cwd: " .. vim.fn.getcwd())
for _, pattern in ipairs(scopes) do
vim.api.nvim_create_autocmd("DirChangedPre", {
callback = function()
Lib.logger.debug "DirChangedPre"
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)

AutoSession.AutoSaveSession()
-- 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

-- Clear all buffers and jumps after session save so session doesn't blead over to next session.
vim.cmd "%bd!"
vim.cmd "clearjumps"
AutoSession.AutoSaveSession()

if type(conf.pre_cwd_changed_hook) == "function" then
conf.pre_cwd_changed_hook()
end
end,
pattern = "global",
})
-- Clear all buffers and jumps after session save so session doesn't blead over to next session.
vim.cmd "%bd!"
vim.cmd "clearjumps"

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())
if type(conf.pre_cwd_changed_hook) == "function" then
conf.pre_cwd_changed_hook()
end
end,
pattern = pattern,
})

-- Deferring to avoid otherwise there are tresitter highlighting issues
vim.defer_fn(function()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach was causing an infinite loop. From the docs:

DirChanged After the current-directory was changed.
...
Non-recursive (event cannot trigger itself).

By triggering Autosession.AutoRestoreSession asynchronously, it caused an infinite loop where the opened buffer could change the global directory (somehow) after opening. So, I moved the asynchronous part to after the session was restored. It then runs filetype detect on all the open buffers which fixes the syntax highlighting error.

local success = AutoSession.AutoRestoreSession()
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() )
Lib.logger.debug(" changed window: " .. tostring(vim.v.event.changed_window))
Lib.logger.debug(" scope: " .. vim.v.event.scope)

if not success then
Lib.logger.info("Could not load session. A session file is likely missing for this cwd." .. vim.fn.getcwd())
end
-- see above
if vim.v.event.changed_window then
return
end

if type(conf.post_cwd_changed_hook) == "function" then
conf.post_cwd_changed_hook()
end
end, 50)
end,
pattern = "global",
})
-- all buffers should've been deleted in `DirChangedPre`, something probably went wrong
if has_open_buffers() then
Lib.logger.debug("Cancelling session restore")
return
end

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,
pattern = pattern,
})
end
end
end

Expand Down
15 changes: 15 additions & 0 deletions lua/auto-session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what fixes the syntax highlighting. I moved it to after the restore session, so that it's always triggered. Although, I'm not sure if the problem exists other than in during the autocmd callbacks? So, it might be better to move it back there if not.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I hadn't noticed problems with this. But this should be fine anyway 👍

-- 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`
Expand Down Expand Up @@ -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
Expand Down