Skip to content

Commit

Permalink
fix: better support for handling swapfiles on load
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc-stripe committed May 2, 2023
1 parent f5e489e commit 8c553c7
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lua/resession/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,12 @@ M.load = function(name, opts)
else
open_clean_tab()
end
-- Don't trigger autocmds during session load
local eventignore = vim.o.eventignore
vim.o.eventignore = "all"
-- Ignore all messages (including swapfile messages) during session load
local shortmess = vim.o.shortmess
vim.o.shortmess = "aAF"
if not data.tab_scoped then
-- Set the options immediately
util.restore_global_options(data.global.options)
Expand All @@ -440,12 +444,17 @@ M.load = function(name, opts)
local bufnr = vim.fn.bufadd(buf.name)
if buf.loaded then
vim.fn.bufload(bufnr)
vim.api.nvim_create_autocmd("BufWinEnter", {
vim.api.nvim_create_autocmd("BufEnter", {
desc = "Resession: complete setup of restored buffer",
callback = function(args)
pcall(vim.api.nvim_win_set_cursor, 0, buf.last_pos)
-- After showing the buffer in a window, manually set the filetype to trigger syntax highlighting
vim.api.nvim_buf_set_option(bufnr, "filetype", vim.bo[bufnr].filetype)
-- We have to schedule :edit otherwise it recurses
vim.schedule(function()
if vim.api.nvim_get_current_buf() == args.buf then
-- This triggers the autocmds that set filetype, syntax highlighting, and checks the swapfile
vim.cmd.edit({ mods = { emsg_silent = true } })
end
end)
end,
buffer = bufnr,
once = true,
Expand Down Expand Up @@ -510,8 +519,12 @@ M.load = function(name, opts)
}
end
vim.o.eventignore = eventignore
vim.o.shortmess = shortmess
_is_loading = false
dispatch("post_load", name, opts)

-- In case the current buffer has a swapfile, make sure we trigger all the necessary autocmds
vim.cmd.edit({ mods = { emsg_silent = true } })
end

---Add a callback that runs at a specific time
Expand Down

0 comments on commit 8c553c7

Please sign in to comment.