|
1 | 1 | local config = require('session_manager.config')
|
2 | 2 | local scandir = require('plenary.scandir')
|
3 | 3 | local Path = require('plenary.path')
|
4 |
| -local utils = {} |
| 4 | +local utils = { active_session_filename = nil } |
5 | 5 |
|
6 | 6 | --- A small wrapper around `vim.notify` that adds plugin title.
|
7 | 7 | ---@param msg string
|
@@ -56,6 +56,9 @@ function utils.load_session(filename, discard_current)
|
56 | 56 | end
|
57 | 57 | vim.api.nvim_buf_delete(current_buffer, { force = true })
|
58 | 58 |
|
| 59 | + -- Set the active session filename. |
| 60 | + utils.active_session_filename = filename |
| 61 | + |
59 | 62 | local swapfile = vim.o.swapfile
|
60 | 63 | vim.o.swapfile = false
|
61 | 64 | vim.api.nvim_exec_autocmds('User', { pattern = 'SessionLoadPre' })
|
@@ -83,37 +86,41 @@ function utils.save_session(filename)
|
83 | 86 | vim.api.nvim_command('%argdel')
|
84 | 87 | end
|
85 | 88 |
|
| 89 | + -- Set the active session filename. |
| 90 | + utils.active_session_filename = filename |
| 91 | + |
86 | 92 | vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePre' })
|
87 | 93 | vim.api.nvim_command('mksession! ' .. filename)
|
88 | 94 | vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePost' })
|
89 | 95 | end
|
90 | 96 |
|
91 | 97 | ---@param filename string
|
92 |
| -function utils.delete_session(filename) Path:new(filename):rm() end |
| 98 | +function utils.delete_session(filename) |
| 99 | + Path:new(filename):rm() |
| 100 | + |
| 101 | + -- Clear the active session filename if deleted. |
| 102 | + if filename == utils.active_session_filename then |
| 103 | + utils.active_session_filename = nil |
| 104 | + end |
| 105 | +end |
93 | 106 |
|
94 | 107 | ---@param opts table?: Additional arguments. Currently only `silent` is supported.
|
95 | 108 | ---@return table
|
96 | 109 | function utils.get_sessions(opts)
|
97 | 110 | local sessions = {}
|
98 | 111 | for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir), opts)) do
|
99 |
| - local dir = config.session_filename_to_dir(session_filename) |
100 |
| - if dir:is_dir() then |
101 |
| - table.insert(sessions, { timestamp = vim.fn.getftime(session_filename), filename = session_filename, dir = dir }) |
102 |
| - else |
103 |
| - Path:new(session_filename):rm() |
| 112 | + -- Add all but the active session to the list. |
| 113 | + if session_filename ~= utils.active_session_filename then |
| 114 | + local dir = config.session_filename_to_dir(session_filename) |
| 115 | + if dir:is_dir() then |
| 116 | + table.insert(sessions, { timestamp = vim.fn.getftime(session_filename), filename = session_filename, dir = dir }) |
| 117 | + else |
| 118 | + Path:new(session_filename):rm() |
| 119 | + end |
104 | 120 | end
|
105 | 121 | end
|
106 | 122 | table.sort(sessions, function(a, b) return a.timestamp > b.timestamp end)
|
107 | 123 |
|
108 |
| - -- If we are in a session already, don't list the current session. |
109 |
| - if utils.is_exist_in_session() then |
110 |
| - local cwd = vim.uv.cwd() |
111 |
| - local is_current_session = cwd and config.dir_to_session_filename(cwd).filename == sessions[1].filename |
112 |
| - if is_current_session then |
113 |
| - table.remove(sessions, 1) |
114 |
| - end |
115 |
| - end |
116 |
| - |
117 | 124 | -- If no sessions to list, send a notification.
|
118 | 125 | if not (opts and opts.silent) and #sessions == 0 then
|
119 | 126 | vim.notify('The only available session is your current session. Nothing to select from.', vim.log.levels.INFO)
|
@@ -164,14 +171,9 @@ function utils.is_restorable_buffer_present()
|
164 | 171 | end
|
165 | 172 |
|
166 | 173 | ---@return boolean
|
167 |
| -function utils.is_exist_in_session() |
| 174 | +function utils.exists_in_session() |
168 | 175 | local cwd = vim.uv.cwd()
|
169 |
| - for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir))) do |
170 |
| - if config.dir_to_session_filename(cwd).filename == session_filename then |
171 |
| - return true |
172 |
| - end |
173 |
| - end |
174 |
| - return false |
| 176 | + return config.dir_to_session_filename(cwd).filename == utils.active_session_filename |
175 | 177 | end
|
176 | 178 |
|
177 | 179 | ---@return boolean
|
|
0 commit comments