Skip to content

Commit 270e235

Browse files
f-leeskeShatur
andauthored
Add option to include the current session in load_session UI (#145)
Co-authored-by: Hennadii Chernyshchyk <[email protected]>
1 parent ce43f2e commit 270e235

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ The plugin saves the sessions in the specified folder (see [configuration](#conf
1313
Use the command `:SessionManager[!]` with one of the following arguments:
1414

1515
| Argument | Description |
16-
| -----------------------------| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
17-
| `load_session` | Select and load session. (Your current session won't appear on the list). |
16+
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
17+
| `load_session` | Select and load session. (Your current session won't appear on the list by default, see configuration below). |
1818
| `load_last_session` | Removes all buffers and tries to `:source` the last saved session. Returns `true` if the session was restored and `false` otherwise. |
1919
| `load_current_dir_session` | Removes all buffers and tries to `:source` the last saved session of the current directory. Returns `true` if the session was restored and `false` otherwise. |
2020
| `load_git_session` | When in a git repo, removes all buffers and tries to `:source` the last saved session of the git repo root directory. Returns `true` if the session was restored and `false` otherwise. |
@@ -48,6 +48,7 @@ require('session_manager').setup({
4848
autosave_ignore_buftypes = {}, -- All buffers of these bufer types will be closed before the session is saved.
4949
autosave_only_in_session = false, -- Always autosaves session. If true, only autosaves after a session is active.
5050
max_path_length = 80, -- Shorten the display path if length exceeds this threshold. Use 0 if don't want to shorten the path at all.
51+
load_include_current = false, -- The currently loaded session appears in the load_session UI.
5152
})
5253
```
5354

@@ -71,7 +72,6 @@ autoload_mode = { config.AutoloadMode.CurrentDir, config.AutoloadMode.LastSessio
7172

7273
Would attempt to load the current directory session and then fallback to the last session.
7374

74-
7575
## Autocommands
7676

7777
You can specify commands to be executed automatically after saving or loading a session using the following events:

lua/session_manager/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ config.defaults = {
4949
autosave_ignore_buftypes = {},
5050
autosave_only_in_session = false,
5151
max_path_length = 80,
52+
load_include_current = false,
5253
}
5354

5455
setmetatable(config, { __index = config.defaults })

lua/session_manager/init.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function session_manager.available_commands()
2626
end)
2727
end
2828

29-
--- Selects a session a loads it.
29+
--- Selects a session and loads it.
3030
---@param discard_current boolean: If `true`, do not check for unsaved buffers.
3131
function session_manager.load_session(discard_current)
3232
local sessions = utils.get_sessions()
@@ -35,7 +35,10 @@ function session_manager.load_session(discard_current)
3535
format_item = function(item) return utils.shorten_path(item.dir) end,
3636
}, function(item)
3737
if item then
38-
session_manager.autosave_session()
38+
-- If re-loading the current session, do not save it before.
39+
if item.filename ~= utils.active_session_filename then
40+
session_manager.autosave_session()
41+
end
3942
utils.load_session(item.filename, discard_current)
4043
end
4144
end)

lua/session_manager/utils.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function utils.get_sessions(opts)
110110
local sessions = {}
111111
for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir), opts)) do
112112
-- Add all but the active session to the list.
113-
if session_filename ~= utils.active_session_filename then
113+
if config.load_include_current or session_filename ~= utils.active_session_filename then
114114
local dir = config.session_filename_to_dir(session_filename)
115115
if dir:is_dir() then
116116
table.insert(sessions, { timestamp = vim.fn.getftime(session_filename), filename = session_filename, dir = dir })

0 commit comments

Comments
 (0)