Skip to content

Commit 8c553c7

Browse files
fix: better support for handling swapfiles on load
1 parent f5e489e commit 8c553c7

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lua/resession/init.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,12 @@ M.load = function(name, opts)
425425
else
426426
open_clean_tab()
427427
end
428+
-- Don't trigger autocmds during session load
428429
local eventignore = vim.o.eventignore
429430
vim.o.eventignore = "all"
431+
-- Ignore all messages (including swapfile messages) during session load
432+
local shortmess = vim.o.shortmess
433+
vim.o.shortmess = "aAF"
430434
if not data.tab_scoped then
431435
-- Set the options immediately
432436
util.restore_global_options(data.global.options)
@@ -440,12 +444,17 @@ M.load = function(name, opts)
440444
local bufnr = vim.fn.bufadd(buf.name)
441445
if buf.loaded then
442446
vim.fn.bufload(bufnr)
443-
vim.api.nvim_create_autocmd("BufWinEnter", {
447+
vim.api.nvim_create_autocmd("BufEnter", {
444448
desc = "Resession: complete setup of restored buffer",
445449
callback = function(args)
446450
pcall(vim.api.nvim_win_set_cursor, 0, buf.last_pos)
447-
-- After showing the buffer in a window, manually set the filetype to trigger syntax highlighting
448-
vim.api.nvim_buf_set_option(bufnr, "filetype", vim.bo[bufnr].filetype)
451+
-- We have to schedule :edit otherwise it recurses
452+
vim.schedule(function()
453+
if vim.api.nvim_get_current_buf() == args.buf then
454+
-- This triggers the autocmds that set filetype, syntax highlighting, and checks the swapfile
455+
vim.cmd.edit({ mods = { emsg_silent = true } })
456+
end
457+
end)
449458
end,
450459
buffer = bufnr,
451460
once = true,
@@ -510,8 +519,12 @@ M.load = function(name, opts)
510519
}
511520
end
512521
vim.o.eventignore = eventignore
522+
vim.o.shortmess = shortmess
513523
_is_loading = false
514524
dispatch("post_load", name, opts)
525+
526+
-- In case the current buffer has a swapfile, make sure we trigger all the necessary autocmds
527+
vim.cmd.edit({ mods = { emsg_silent = true } })
515528
end
516529

517530
---Add a callback that runs at a specific time

0 commit comments

Comments
 (0)