folke / persistence.nvim

💾 Simple session management for Neovim
Apache License 2.0
591 stars 26 forks source link

feat(persistence): `pre_save` option to call before saving #22

Closed Iron-E closed 1 year ago

Iron-E commented 1 year ago

About

This PR adds the pre_save option, which is a fun()|nil that will be called (if present) before saving the current session.

Use cases include filtering buffers, executing autocmds, etc. Example config using lazy.nvim, for integration with barbar.nvim:

{'folke/persistence.nvim', opts = {
  options = {'buffers', 'curdir', 'globals', 'tabpages', 'winsize'},
  pre_save = function() vim.api.nvim_exec_autocmds('User', {pattern = 'SessionSavePre'}) end,
}},

Motivation

barbar.nvim's is a &tabline plugin which has builtin integration with session files. Our implementation differs from bufferline.nvim's in that we do not keep a global variable synced with the state of the plugin, until it comes time to export it to a session.

Normally, it is advised to create a wrapper function around the :mksession (or plugin equivalent) command which runs :doautocmd with our User event pattern. However, this plugin (extremely conveniently) saves sessions without requiring user intervention. Thus, in order to ensure that our event runs first, a user of our plugin would have to clear the persistence augroup and recreate it, adding only one function call.

That solution seemed a bit hacky to me, so I wanted to see if creating a pre_save option would be a viable alternative. This implementation allows not just integration with barbar.nvim and user configurations, but it also allows new plugins to wait to sync global variables relevant to a session until necessary, which increases performance.


Feel free to request that the variable name or implementation details be changed. Also feel free to close the PR if this isn't something that aligns with the goals of the plugin.

folke commented 1 year ago

Thank you!