Skip to content

Commit

Permalink
feat(plugin):delete buffers before loading a session
Browse files Browse the repository at this point in the history
  • Loading branch information
wasden committed May 22, 2022
1 parent 16b436d commit 2e6f07a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ require('possession').setup {
},
nvim_tree = true,
tabby = true,
delete_buffers = true,
},
}
```
Expand Down
1 change: 1 addition & 0 deletions lua/possession/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ local function defaults()
},
nvim_tree = true,
tabby = true,
delete_buffers = true,
},
}
end
Expand Down
18 changes: 18 additions & 0 deletions lua/possession/plugins/delete_buffers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local M = {}

function M.before_load(_, _, plugin_data)
-- Deleting the current buffer before deleting other buffers will cause autocmd "BufEnter" to be triggered.
-- Lspconfig will use the invalid buffer handler in vim.schedule.
-- So make sure the current buffer is the last one to delete.
local current_buffer = vim.api.nvim_get_current_buf()
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buffer) and current_buffer ~= buffer then
vim.api.nvim_buf_delete(buffer, { force = true })
end
end
vim.api.nvim_buf_delete(current_buffer, { force = true })
vim.lsp.stop_client(vim.lsp.get_active_clients())
return plugin_data
end

return M
1 change: 1 addition & 0 deletions lua/possession/plugins/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local plugins = {
'delete_hidden_buffers',
'nvim_tree',
'tabby',
'delete_buffers',
}

local function req(plugin)
Expand Down
33 changes: 16 additions & 17 deletions lua/possession/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,22 @@ function M.save(name, opts)
end

function M.autosave()
if config.autosave == false then
return
end
if M.session_name then
M.save(M.session_name, { no_confirm = true })
elseif config.tmp_session then -- Save as tmp when session is not loaded

-- Skip scratch buffer e.g. startscreen
local unscratch_buffers = vim.tbl_filter(function (buf)
return "nofile" ~= vim.api.nvim_buf_get_option(buf, "buftype")
end, vim.api.nvim_list_bufs())
if not unscratch_buffers or not next(unscratch_buffers) then
return
end

M.save("tmp", { no_confirm = true })
end
if config.autosave == false then
return
end
if M.session_name then
M.save(M.session_name, { no_confirm = true })
elseif config.tmp_session then -- Save as tmp when session is not loaded
-- Skip scratch buffer e.g. startscreen
local unscratch_buffers = vim.tbl_filter(function(buf)
return 'nofile' ~= vim.api.nvim_buf_get_option(buf, 'buftype')
end, vim.api.nvim_list_bufs())
if not unscratch_buffers or not next(unscratch_buffers) then
return
end

M.save('tmp', { no_confirm = true })
end
end

-- Load session by name (or from raw data)
Expand Down
4 changes: 2 additions & 2 deletions plugin/possession.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ local session = require('possession.session')
local possession_group = vim.api.nvim_create_augroup('Possession', {})

vim.api.nvim_create_autocmd({ 'VimLeavePre' }, {
group = possession_group,
callback = session.autosave,
group = possession_group,
callback = session.autosave,
})

0 comments on commit 2e6f07a

Please sign in to comment.