Skip to content

Commit 562fe01

Browse files
authored
feat(events): add SelectPre and SelectPost events (#172)
1 parent 1a6e16e commit 562fe01

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ jobs:
1717
fail-fast: false
1818
matrix:
1919
include:
20-
- os: ubuntu-20.04
20+
- os: ubuntu-latest
2121
url: https://github.com/neovim/neovim/releases/download/nightly/nvim-linux-x86_64.tar.gz
22-
- os: ubuntu-20.04
22+
- os: ubuntu-latest
2323
url: https://github.com/neovim/neovim/releases/download/v0.10.0/nvim-linux64.tar.gz
24-
- os: ubuntu-20.04
24+
- os: ubuntu-latest
2525
url: https://github.com/neovim/neovim/releases/download/v0.9.0/nvim-linux64.tar.gz
26-
- os: ubuntu-20.04
26+
- os: ubuntu-latest
2727
url: https://github.com/neovim/neovim/releases/download/v0.8.0/nvim-linux64.tar.gz
2828

2929
steps:

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,19 @@ In this setup, `~/.config` and `~/.local/nvim` are still going to behave in thei
326326

327327
The plugin fires events at various points during its lifecycle:
328328

329+
- `PersistedDeletePre` - For _before_ a session is deleted
330+
- `PersistedDeletePost` - For _after_ a session is deleted
329331
- `PersistedLoadPre` - For _before_ a session is loaded
330332
- `PersistedLoadPost` - For _after_ a session is loaded
331-
- `PersistedTelescopeLoadPre` - For _before_ a session is loaded via Telescope
332-
- `PersistedTelescopeLoadPost` - For _after_ a session is loaded via Telescope
333333
- `PersistedSavePre` - For _before_ a session is saved
334334
- `PersistedSavePost` - For _after_ a session is saved
335-
- `PersistedDeletePre` - For _before_ a session is deleted
336-
- `PersistedDeletePost` - For _after_ a session is deleted
335+
- `PersistedSelectPre` - For _before_ a session is selected (via `:SessionSelect`)
336+
- `PersistedSelectPost` - For _after_ a session is selected
337337
- `PersistedStart` - For when a session has _started_
338338
- `PersistedStop` - For when a session has _stopped_
339339
- `PersistedToggle` - For when a session is toggled
340+
- `PersistedTelescopeLoadPre` - For _before_ a session is loaded via Telescope
341+
- `PersistedTelescopeLoadPost` - For _after_ a session is loaded via Telescope
340342

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

@@ -346,7 +348,7 @@ A commonly requested example is to use the Telescope extension to load a session
346348
vim.api.nvim_create_autocmd("User", {
347349
pattern = "PersistedTelescopeLoadPre",
348350
callback = function(session)
349-
-- Save the currently loaded session using the global variable
351+
-- Save the currently loaded session passing in the path to the current session
350352
require("persisted").save({ session = vim.g.persisted_loaded_session })
351353

352354
-- Delete all of the open buffers

lua/persisted/init.lua

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local uv = vim.uv or vim.loop
1010

1111
---Fire an event
1212
---@param event string
13+
---@return nil
1314
function M.fire(event)
1415
vim.api.nvim_exec_autocmds("User", { pattern = "Persisted" .. event })
1516
end
@@ -34,6 +35,7 @@ end
3435

3536
---Automatically load the session for the current dir
3637
---@param opts? { force?: boolean }
38+
---@return nil
3739
function M.autoload(opts)
3840
opts = opts or {}
3941

@@ -48,6 +50,7 @@ end
4850

4951
---Load a session
5052
---@param opts? { last?: boolean, autoload?: boolean, session?: string }
53+
---@return nil
5154
function M.load(opts)
5255
opts = opts or {}
5356

@@ -80,6 +83,7 @@ function M.load(opts)
8083
end
8184

8285
---Start a session
86+
---@return nil
8387
function M.start()
8488
vim.api.nvim_create_autocmd("VimLeavePre", {
8589
group = vim.api.nvim_create_augroup("Persisted", { clear = true }),
@@ -93,6 +97,7 @@ function M.start()
9397
end
9498

9599
---Stop a session
100+
---@return nil
96101
function M.stop()
97102
vim.g.persisting = false
98103
pcall(vim.api.nvim_del_augroup_by_name, "Persisted")
@@ -101,6 +106,7 @@ end
101106

102107
---Save the session
103108
---@param opts? { force?: boolean, session?: string }
109+
---@return nil
104110
function M.save(opts)
105111
opts = opts or {}
106112

@@ -115,11 +121,12 @@ function M.save(opts)
115121
M.fire("SavePost")
116122
end
117123

118-
---Delete the current session
119-
---@param opts? { session?: string }
124+
---Delete a session
125+
---@param opts? { path?: string }
126+
---@return nil
120127
function M.delete(opts)
121128
opts = opts or {}
122-
local session = opts.session or M.current()
129+
local session = opts.path or M.current()
123130

124131
if session and uv.fs_stat(session) ~= 0 then
125132
M.fire("DeletePre")
@@ -141,9 +148,9 @@ function M.branch()
141148
end
142149

143150
---Select a session to load
151+
---@return nil
144152
function M.select()
145-
---@type { session: string, dir: string, branch?: string }[]
146-
local items = {}
153+
local items = {} ---@type { session: string, dir: string, branch?: string }[]
147154
local found = {} ---@type table<string, boolean>
148155
for _, session in ipairs(M.list()) do
149156
if uv.fs_stat(session) then
@@ -160,7 +167,7 @@ function M.select()
160167
end
161168
end
162169
vim.ui.select(items, {
163-
prompt = "Select a session: ",
170+
prompt = "Load a session: ",
164171
format_item = function(item)
165172
local name = vim.fn.fnamemodify(item.dir, ":p:~")
166173
if item.branch then
@@ -170,13 +177,16 @@ function M.select()
170177
end,
171178
}, function(item)
172179
if item then
180+
M.fire("SelectPre")
173181
vim.fn.chdir(item.dir)
174182
M.load()
183+
M.fire("SelectPost")
175184
end
176185
end)
177186
end
178187

179188
---Determines whether to load, start or stop a session
189+
---@return nil
180190
function M.toggle()
181191
M.fire("Toggle")
182192
if vim.g.persisting == nil then
@@ -219,6 +229,7 @@ end
219229

220230
---Setup the plugin
221231
---@param opts? table
232+
---@return nil
222233
function M.setup(opts)
223234
config.setup(opts)
224235

0 commit comments

Comments
 (0)