Skip to content

Commit 22e17b0

Browse files
authored
refactor!: #145 restore simplicity to the plugin
Announcement in #145
1 parent 0446fd0 commit 22e17b0

File tree

14 files changed

+552
-1100
lines changed

14 files changed

+552
-1100
lines changed

README.md

+107-104
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
## :sparkles: Features
2424

2525
- :evergreen_tree: Supports sessions across multiple git branches
26-
- :telescope: Telescope extension to work with saved sessions
27-
- :tickets: Custom events which users can hook into for tighter integration
26+
- :telescope: Telescope extension to manage sessions
27+
- :tickets: Custom events which users can hook into for tighter integrations
2828
- :memo: Simple API to save/stop/restore/delete/list the current session(s)
2929
- :open_file_folder: Supports autosaving and autoloading of sessions with allowed/ignored directories
3030
- :floppy_disk: Automatically saves the active session under `.local/share/nvim/sessions` on exiting Neovim
@@ -35,7 +35,7 @@
3535

3636
## :package: Installation
3737

38-
Install the plugin with your preferred package manager:
38+
Install and configure the plugin with your preferred package manager:
3939

4040
**[Lazy.nvim](https://github.com/folke/lazy.nvim)**
4141

@@ -110,7 +110,7 @@ require('telescope').setup({
110110
The plugin comes with a number of commands:
111111

112112
- `:SessionToggle` - Determines whether to load, start or stop a session
113-
- `:SessionStart` - Start recording a session. Useful if `autosave = false`
113+
- `:SessionStart` - Start recording a session. Useful if `autostart = false`
114114
- `:SessionStop` - Stop recording a session
115115
- `:SessionSave` - Save the current session
116116
- `:SessionLoad` - Load the session for the current directory and current branch (if `git_use_branch = true`)
@@ -123,12 +123,12 @@ The plugin comes with a number of commands:
123123
<!-- panvimdoc-ignore-start -->
124124

125125
<p align="center">
126-
<img src="https://github.com/olimorris/persisted.nvim/assets/9512444/5bfd6f94-ff70-4f2b-9193-53cdf7140d75" alt="Telescope">
126+
<img src="https://github.com/user-attachments/assets/3ff91790-b61a-4089-b87d-432e8b4969c2" alt="Telescope">
127127
</p>
128128

129129
<!-- panvimdoc-ignore-end -->
130130

131-
The Telescope extension may be opened via `:Telescope persisted`. The available actions are:
131+
The Telescope extension may be opened via `:Telescope persisted`. The default actions are:
132132

133133
- `<CR>` - Open/source the session file
134134
- `<C-b>` - Add/update the git branch for the session file
@@ -140,8 +140,8 @@ The Telescope extension may be opened via `:Telescope persisted`. The available
140140
The plugin sets a number of global variables throughout its lifecycle:
141141

142142
- `vim.g.persisting` - (bool) Determines if the plugin is active for the current session
143-
- `vim.g.persisted_exists` - (bool) Determines if a session exists for the current working directory
144-
- `vim.g.persisted_loaded_session` - (string) The file path to the current session
143+
- `vim.g.persisting_session` - (string) The file path to the current session (if `follow_cwd` is false)
144+
- `vim.g.persisted_loaded_session` - (string) The file path to the last loaded session
145145

146146
## :wrench: Configuration
147147

@@ -150,42 +150,49 @@ The plugin sets a number of global variables throughout its lifecycle:
150150
The plugin comes with the following defaults:
151151

152152
```lua
153-
require("persisted").setup({
154-
log_level = "ERROR", -- One of "TRACE", "DEBUG", "ERROR"
155-
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
156-
silent = false, -- silent nvim message when sourcing session file
157-
use_git_branch = false, -- create session files based on the branch of a git enabled repository
158-
default_branch = "main", -- the branch to load if a session file is not found for the current branch
159-
autosave = true, -- automatically save session files when exiting Neovim
160-
should_autosave = nil, -- function to determine if a session should be autosaved
161-
autoload = false, -- automatically load the session for the cwd on Neovim startup
162-
on_autoload_no_session = nil, -- function to run when `autoload = true` but there is no session to load
163-
follow_cwd = true, -- change session file name to match current working directory if it changes
164-
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
165-
ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading
166-
ignored_branches = nil, -- table of branch patterns that are ignored for auto-saving and auto-loading
153+
{
154+
autostart = true, -- Automatically start the plugin on load?
155+
156+
-- Function to determine if a session should be saved
157+
---@type fun(): boolean
158+
should_save = function()
159+
return true
160+
end,
161+
162+
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- Directory where session files are saved
163+
164+
follow_cwd = true, -- Change the session file to match any change in the cwd?
165+
use_git_branch = false, -- Include the git branch in the session file name?
166+
autoload = false, -- Automatically load the session for the cwd on Neovim startup?
167+
168+
-- Function to run when `autoload = true` but there is no session to load
169+
---@type fun(): any
170+
on_autoload_no_session = function() end,
171+
172+
allowed_dirs = {}, -- Table of dirs that the plugin will start and autoload from
173+
ignored_dirs = {}, -- Table of dirs that are ignored for starting and autoloading
174+
167175
telescope = {
168-
reset_prompt = true, -- Reset the Telescope prompt after an action?
169-
mappings = { -- table of mappings for the Telescope extension
170-
change_branch = "<c-b>",
171-
copy_session = "<c-c>",
172-
delete_session = "<c-d>",
176+
mappings = { -- Mappings for managing sessions in Telescope
177+
copy_session = "<C-c>",
178+
change_branch = "<C-b>",
179+
delete_session = "<C-d>",
173180
},
174-
icons = { -- icons displayed in the picker, set to nil to disable entirely
181+
icons = { -- icons displayed in the Telescope picker
182+
selected = "",
183+
dir = "",
175184
branch = "",
176-
dir = "",
177-
selected = "",
178185
},
179186
},
180-
})
187+
}
181188
```
182189

183190
**What is saved in the session?**
184191

185192
As the plugin uses Vim's `:mksession` command then you may change the `vim.o.sessionoptions` value to determine what to write into the session. Please see `:h sessionoptions` for more information.
186193

187194
> [!NOTE]
188-
> The author uses: `vim.o.sessionoptions = "buffers,curdir,folds,tabpages,winpos,winsize"`
195+
> The author uses: `vim.o.sessionoptions = "buffers,curdir,folds,globals,tabpages,winpos,winsize"`
189196
190197
**Session save location**
191198

@@ -210,24 +217,27 @@ require("persisted").setup({
210217
})
211218
```
212219

213-
**Autosaving**
220+
**Autostart**
214221

215-
By default, the plugin will automatically save a Neovim session to disk when the `VimLeavePre` event is triggered. Autosaving can be turned off by:
222+
By default, the plugin will automatically start when the setup function is called. This results in a Neovim session being saved to disk when the `VimLeavePre` event is triggered. This can be disabled by:
216223

217224
```lua
218225
require("persisted").setup({
219-
autosave = false,
226+
autostart = false,
220227
})
221228
```
222229

223-
Autosaving can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
230+
Autostarting can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
231+
232+
**`should_save`**
224233

225-
There may be occasions when you do not wish to autosave; perhaps when a dashboard or a certain buftype is present. To control this, a callback function, `should_autosave`, may be used which should return a boolean value.
234+
There may be occasions when you do not wish to save the session; perhaps when a dashboard or a certain filetype is present. To handle this, the `should_save` function may be used which should return a boolean value.
226235

227236
```lua
228237
require("persisted").setup({
229-
should_autosave = function()
230-
-- do not autosave if the alpha dashboard is the current filetype
238+
---@return bool
239+
should_save = function()
240+
-- Do not save if the alpha dashboard is the current filetype
231241
if vim.bo.filetype == "alpha" then
232242
return false
233243
end
@@ -236,10 +246,7 @@ require("persisted").setup({
236246
})
237247
```
238248

239-
Of course, if you wish to manually save the session when autosaving is disabled, the `:SessionSave` command can be used.
240-
241-
> [!NOTE]
242-
> If `autosave = false` then the `should_autosave` callback will not be executed.
249+
Of course, if you wish to manually save the session the `:SessionSave` command can be used.
243250

244251
**Autoloading**
245252

@@ -251,7 +258,7 @@ require("persisted").setup({
251258
})
252259
```
253260

254-
You can also provide a function to run when `autoload = true` but there is no session to be loaded:
261+
You can also provide a function to run when `autoload = true` and there is no session to load:
255262

256263
```lua
257264
require("persisted").setup({
@@ -262,29 +269,14 @@ require("persisted").setup({
262269
})
263270
```
264271

265-
Autoloading can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
266-
267-
> [!NOTE]
268-
> Autoloading will not occur if the plugin is lazy loaded or a user opens Neovim with arguments other than a single directory argument. For example: `nvim some_file.rb` will not result in autoloading but `nvim some/existing/path` or `nvim .` will.
269-
270-
**Following current working directory**
271-
272-
There may be a need to change the working directory to quickly access files in other directories without changing the current session's name on save. This behavior can be configured with `follow_cwd = false`.
273-
274-
By default, the session name will match the current working directory:
275-
276-
```lua
277-
require("persisted").setup({
278-
follow_cwd = true,
279-
})
280-
```
272+
Autoloading can be further controlled for directories in the `allowed_dirs` and `ignored_dirs` config tables.
281273

282-
> [!NOTE]
283-
> If `follow_cwd = false` the session name is stored upon loading under the global variable `vim.g.persisting_session`. This variable can be manually adjusted if changes to the session name are needed. Alternatively, if `follow_cwd = true` then `vim.g.persisting_session = nil`.
274+
> [!IMPORTANT]
275+
> By design, the plugin will not autoload a session when any arguments are passed to Neovim such as `nvim my_file.py`
284276
285277
**Allowed directories**
286278

287-
You may specify a table of directories for which the plugin will autosave and/or autoload from. For example:
279+
You may specify a table of directories for which the plugin will start and/or autoload from. For example:
288280

289281
```lua
290282
require("persisted").setup({
@@ -295,14 +287,14 @@ require("persisted").setup({
295287
})
296288
```
297289

298-
Specifying `~/Code` will autosave and autoload from that directory as well as all its sub-directories.
290+
Specifying `~/Code` will start and autoload from that directory as well as all its sub-directories.
299291

300292
> [!NOTE]
301-
> If `allowed_dirs` is left at its default value and `autosave` and/or `autoload` are set to `true`, then the plugin will autoload/autosave from _any_ directory
293+
> If `allowed_dirs` is left at its default value and `autostart` and/or `autoload` are set to `true`, then the plugin will start and autoload from _any_ directory
302294
303295
**Ignored directories**
304296

305-
You may specify a table of directories for which the plugin will **never** autosave and autoload from. For example:
297+
You may specify a table of directories for which the plugin will **never** start and autoload from. For example:
306298

307299
```lua
308300
require("persisted").setup({
@@ -330,19 +322,6 @@ require("persisted").setup({
330322

331323
In this setup, `~/.config` and `~/.local/nvim` are still going to behave in their default setting (ignoring all listed directory and its children), however `/` and `/tmp` will only ignore those directories exactly.
332324

333-
**Ignored branches**
334-
335-
You may specify a table of patterns that match against branches for which the plugin will **never** autosave and autoload from:
336-
337-
```lua
338-
require("persisted").setup({
339-
ignored_branches = {
340-
"^master",
341-
"feature/%u"
342-
},
343-
})
344-
```
345-
346325
**Events / Callbacks**
347326

348327
The plugin fires events at various points during its lifecycle:
@@ -355,21 +334,19 @@ The plugin fires events at various points during its lifecycle:
355334
- `PersistedSavePost` - For _after_ a session is saved
356335
- `PersistedDeletePre` - For _before_ a session is deleted
357336
- `PersistedDeletePost` - For _after_ a session is deleted
358-
- `PersistedStateChange` - For when a session is _started_ or _stopped_
359-
- `PersistedToggled` - For when a session is toggled
337+
- `PersistedStart` - For when a session has _started_
338+
- `PersistedStop` - For when a session has _stopped_
339+
- `PersistedToggle` - For when a session is toggled
360340

361341
These events can be consumed anywhere within your configuration by utilising the `vim.api.nvim_create_autocmd` function.
362342

363343
A commonly requested example is to use the Telescope extension to load a session, saving the current session before clearing all of the open buffers:
364344

365345
```lua
366-
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
367-
368-
vim.api.nvim_create_autocmd({ "User" }, {
346+
vim.api.nvim_create_autocmd("User", {
369347
pattern = "PersistedTelescopeLoadPre",
370-
group = group,
371348
callback = function(session)
372-
-- Save the currently loaded session using a global variable
349+
-- Save the currently loaded session using the global variable
373350
require("persisted").save({ session = vim.g.persisted_loaded_session })
374351

375352
-- Delete all of the open buffers
@@ -378,33 +355,59 @@ vim.api.nvim_create_autocmd({ "User" }, {
378355
})
379356
```
380357

381-
**Using callback data**
358+
**Highlights**
382359

383-
When certain events are fired, session data is made available for the user to consume, for example:
360+
The plugin also comes with pre-defined highlight groups for the Telescope implementation:
384361

385-
```lua
386-
{
387-
branch = "main",
388-
dir_path = "Code/Neovim/persisted.nvim",
389-
file_path = "/Users/Oli/.local/share/nvim/sessions/%Users%Oli%Code%Neovim%persisted.nvim@@main.vim",
390-
name = "Code/Neovim/persisted.nvim@@main",
391-
}
392-
```
362+
- `PersistedTelescopeSelected`
363+
- `PersistedTelescopeDir`
364+
- `PersistedTelescopeBranch`
393365

394-
To consume this data, use the `session.data` table in your autocmd:
366+
## :building_construction: Extending the Plugin
367+
368+
The plugin has been designed to be fully extensible. All of the functions in the [init.lua](https://github.com/olimorris/persisted.nvim/blob/main/lua/persisted/init.lua) and [utils.lua](https://github.com/olimorris/persisted.nvim/blob/main/lua/persisted/utils.lua) file are public.
369+
370+
Consider a user who wishes to autoload a session if arguments are passed to Neovim. A custom autocmd can be created which forces the autoload:
395371

396372
```lua
397-
vim.api.nvim_create_autocmd({ "User" }, {
398-
pattern = "PersistedLoadPost",
399-
group = group,
400-
callback = function(session)
401-
print(session.data.branch)
373+
local persisted = require("persisted")
374+
375+
persisted.setup({
376+
autoload = true
377+
})
378+
379+
vim.api.nvim_create_autocmd("VimEnter", {
380+
nested = true,
381+
callback = function()
382+
-- Add more complex logic here
383+
if vim.fn.argc() > 0 then
384+
-- Leverage the plugin's ability to resolve allowed_dirs and ignored_dirs
385+
require("persisted").autoload({ force = true })
386+
end
402387
end,
403388
})
404389
```
405390

406-
> [!NOTE]
407-
> This data is available for the `PersistedLoad`, `PersistedDelete` and `PersistedTelescope` events
391+
Or, a user who wishes to check whether the current branch is in a table of branches to be ignored:
392+
393+
```lua
394+
local persisted = require("persisted")
395+
local utils = require("persisted.utils")
396+
397+
persisted.setup({
398+
autostart = false,
399+
use_git_branch = true,
400+
})
401+
402+
local ignored_branches = {
403+
"feature_branch"
404+
}
405+
406+
if utils.in_table(persisted.branch(), ignored_branches) ~= nil then
407+
persisted.load()
408+
persisted.start()
409+
end
410+
```
408411

409412
## :page_with_curl: License
410413

0 commit comments

Comments
 (0)