Skip to content

Commit

Permalink
Merge pull request #179 from zwhitchcox/file-tree-integration
Browse files Browse the repository at this point in the history
File tree integration with nvim-tree and NERDTree
  • Loading branch information
rmagatti authored Nov 8, 2022
2 parents 39319bf + 1ee6574 commit 4af51e2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
43 changes: 32 additions & 11 deletions lua/auto-session-autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

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

0 comments on commit 4af51e2

Please sign in to comment.