Skip to content

Commit 322d82f

Browse files
authored
Merge pull request #346 from cameronr/main
2 parents afa735f + e276caa commit 322d82f

File tree

4 files changed

+83
-45
lines changed

4 files changed

+83
-45
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ EOF
103103

104104
```lua
105105
require("auto-session").setup {
106-
bypass_session_save_file_types = nil, -- table: Bypass auto save when only buffer open is one of these file types
106+
bypass_session_save_file_types = nil, -- table: Bypass auto save when only buffer open is one of these file types, useful to ignore dashboards
107107
close_unsupported_windows = true, -- boolean: Close windows that aren't backed by normal file
108108
cwd_change_handling = { -- table: Config for handling the DirChangePre and DirChanged autocmds, can be set to nil to disable altogether
109109
restore_upcoming_session = false, -- boolean: restore session for upcoming cwd on cwd change
@@ -391,6 +391,11 @@ You can use Telescope to see, load, and delete your sessions. It's enabled by de
391391
load_on_setup = true,
392392
theme_conf = { border = true },
393393
previewer = false,
394+
mappings = {
395+
-- Mode can be a string or a table, e.g. {"i", "n"} for both insert and normal mode
396+
delete_session = { "i", "<C-D>" },
397+
alternate_session = { "i", "<C-S>" },
398+
},
394399
},
395400
})
396401
end,
@@ -434,6 +439,17 @@ require('lualine').setup{
434439

435440
<img width="1904" alt="Screen Shot 2021-10-30 at 3 58 57 PM" src="https://user-images.githubusercontent.com/2881382/139559478-8edefdb8-8254-42e7-a0f3-babd3dfd6ff2.png">
436441

442+
## Dashboards
443+
444+
If you use a dashboard, you probably don't want to try and save a session when just the dashboard is open. To avoid that, add your dashboard filetype to the bypass list as follows:
445+
446+
```lua
447+
require('auto-session').setup({
448+
bypass_session_save_file_types = { 'alpha', 'dashboard' } -- or whatever dashboard you use
449+
})
450+
451+
```
452+
437453
## Disabling the plugin
438454

439455
You might run into issues with Firenvim or another plugin and want to disable `auto_session` altogether based on some condition.

doc/auto-session.txt

+23-14
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ luaOnlyConf *luaOnlyConf*
2020

2121
Fields: ~
2222
{cwd_change_handling?} (boolean|CwdChangeHandling)
23-
{bypass_session_save_file_types?} (table) List of file types to bypass auto save when the only buffer open is one of the file types listed
23+
{bypass_session_save_file_types?} (table) List of file types to bypass auto save when the only buffer open is one of the file types listed, useful to ignore dashboards
2424
{close_unsupported_windows?} (boolean) Whether to close windows that aren't backed by a real file
2525
{silent_restore?} (boolean) Suppress extraneous messages and source the whole session, even if there's an error. Set to false to get the line number of a restore error
2626
{log_level?} (string|integer) "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
@@ -39,6 +39,20 @@ CwdChangeHandling *CwdChangeHandling*
3939
{post_cwd_changed_hook?} (boolean) {true} This is called after auto_session code runs for the DirChanged autocmd
4040

4141

42+
session_lens_config *session_lens_config*
43+
Session Lens Config
44+
45+
Fields: ~
46+
{load_on_setup?} (boolean)
47+
{shorten_path?} (boolean) Deprecated, pass { 'shorten' } to path_display
48+
{path_display?} (table) An array that specifies how to handle paths. Read :h telescope.defaults.path_display
49+
{theme_conf?} (table)
50+
{buftypes_to_ignore?} (table) Deprecated, if you're using this please report your usage on github
51+
{previewer?} (boolean)
52+
{session_control?} (session_control)
53+
{mappings?} (session_lens_mapping)
54+
55+
4256
session_control *session_control*
4357
Session Control Config
4458

@@ -47,6 +61,14 @@ session_control *session_control*
4761
{control_filename} (string)
4862

4963

64+
session_lens_mapping *session_lens_mapping*
65+
Session Lens Mapping
66+
67+
Fields: ~
68+
{delete_session} (table) mode and key for deleting a session from the picker
69+
{alternate_session} (table) mode and key for swapping to alertnate session from the picker
70+
71+
5072
AutoSession.setup({config}) *AutoSession.setup*
5173
Setup function for AutoSession
5274

@@ -187,19 +209,6 @@ AutoSession.DisableAutoSave({enable?}) *AutoSession.DisableAutoSave*
187209
(boolean) autosaving is enabled or not
188210

189211

190-
session_lens_config *session_lens_config*
191-
Session Lens Config
192-
193-
Fields: ~
194-
{shorten_path?} (boolean) Deprecated, pass { 'shorten' } to path_display
195-
{path_display?} (table) An array that specifies how to handle paths. Read :h telescope.defaults.path_display
196-
{theme_conf?} (table)
197-
{buftypes_to_ignore?} (table) Deprecated, if you're using this please report your usage on github
198-
{previewer?} (boolean)
199-
{session_control?} (session_control)
200-
{load_on_setup?} (boolean)
201-
202-
203212
SessionLens.setup() *SessionLens.setup*
204213

205214

lua/auto-session/init.lua

+36-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ local defaultConf = {
7272
---Lua Only Configs for Auto Session
7373
---@class luaOnlyConf
7474
---@field cwd_change_handling? boolean|CwdChangeHandling
75-
---@field bypass_session_save_file_types? table List of file types to bypass auto save when the only buffer open is one of the file types listed
75+
---@field bypass_session_save_file_types? table List of file types to bypass auto save when the only buffer open is one of the file types listed, useful to ignore dashboards
7676
---@field close_unsupported_windows? boolean Whether to close windows that aren't backed by a real file
7777
---@field silent_restore? boolean Suppress extraneous messages and source the whole session, even if there's an error. Set to false to get the line number of a restore error
7878
---@field log_level? string|integer "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
@@ -82,7 +82,7 @@ local defaultConf = {
8282
---@field session_lens? session_lens_config Session lens configuration options
8383

8484
local luaOnlyConf = {
85-
bypass_session_save_file_types = nil, -- Bypass auto save when only buffer open is one of these file types
85+
bypass_session_save_file_types = nil, -- Bypass auto save when only buffer open is one of these file types, useful to ignore dashboards
8686
close_unsupported_windows = true, -- Close windows that aren't backed by normal file
8787
args_allow_single_directory = true, -- Allow single directory arguments by default
8888
args_allow_files_auto_save = false, -- Don't save session for file args by default
@@ -100,19 +100,41 @@ local luaOnlyConf = {
100100
--- post_cwd_changed_hook = nil, -- lua function hook. This is called after auto_session code runs for the `DirChanged` autocmd
101101
--- }
102102
cwd_change_handling = false,
103+
104+
---Session Lens Config
105+
---@class session_lens_config
106+
---@field load_on_setup? boolean
107+
---@field shorten_path? boolean Deprecated, pass { 'shorten' } to path_display
108+
---@field path_display? table An array that specifies how to handle paths. Read :h telescope.defaults.path_display
109+
---@field theme_conf? table
110+
---@field buftypes_to_ignore? table Deprecated, if you're using this please report your usage on github
111+
---@field previewer? boolean
112+
---@field session_control? session_control
113+
---@field mappings? session_lens_mapping
114+
103115
---Session Control Config
104116
---@class session_control
105117
---@field control_dir string
106118
---@field control_filename string
107119

120+
---Session Lens Mapping
121+
---@class session_lens_mapping
122+
---@field delete_session table mode and key for deleting a session from the picker
123+
---@field alternate_session table mode and key for swapping to alertnate session from the picker
124+
108125
---@type session_lens_config
109126
session_lens = {
110-
buftypes_to_ignore = {}, -- list of bufftypes to ignore when switching between sessions
111127
load_on_setup = true,
128+
buftypes_to_ignore = {},
112129
session_control = {
113130
control_dir = vim.fn.stdpath "data" .. "/auto_session/", -- Auto session control dir, for control files, like alternating between two sessions with session-lens
114131
control_filename = "session_control.json", -- File name of the session control file
115132
},
133+
mappings = {
134+
-- Mode can be a string or a table, e.g. {"i", "n"} for both insert and normal mode
135+
delete_session = { "i", "<C-D>" },
136+
alternate_session = { "i", "<C-S>" },
137+
},
116138
},
117139
silent_restore = true, -- Suppress extraneous messages and source the whole session, even if there's an error. Set to false to get the line number of a restore error
118140
}
@@ -487,7 +509,7 @@ function AutoSession.session_exists_for_cwd()
487509
end
488510

489511
-- Check legacy sessions
490-
local session_file = get_session_file_name(vim.fn.getcwd(), true)
512+
session_file = get_session_file_name(vim.fn.getcwd(), true)
491513
return vim.fn.filereadable(AutoSession.get_root_dir() .. session_file) ~= 0
492514
end
493515

@@ -648,11 +670,19 @@ end
648670

649671
---@private
650672
---Handler for when a session is picked from the UI, either via Telescope or via AutoSession.select_session
651-
---Save the current session (if autosave allows) and restore the selected session
673+
---Save the current session if the session we're loading isn't also for the cwd (if autosave allows)
674+
---and then restore the selected session
652675
---@param session_name string The session name to restore
653676
---@return boolean Was the session restored successfully
654677
function AutoSession.autosave_and_restore(session_name)
655-
AutoSession.AutoSaveSession()
678+
local cwd_session_name = Lib.escaped_session_name_to_session_name(get_session_file_name())
679+
if cwd_session_name ~= session_name then
680+
Lib.logger.debug("Autosaving before restoring", { cwd = cwd_session_name, session_name = session_name })
681+
AutoSession.AutoSaveSession()
682+
else
683+
Lib.logger.debug("Not autosaving, cwd == session_name for: ", session_name)
684+
end
685+
656686
return AutoSession.RestoreSession(session_name)
657687
end
658688

lua/auto-session/session-lens/init.lua

+7-24
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,8 @@ local SessionLens = {
77
conf = {},
88
}
99

10-
---Session Lens Config
11-
---@class session_lens_config
12-
---@field shorten_path? boolean Deprecated, pass { 'shorten' } to path_display
13-
---@field path_display? table An array that specifies how to handle paths. Read :h telescope.defaults.path_display
14-
---@field theme_conf? table
15-
---@field buftypes_to_ignore? table Deprecated, if you're using this please report your usage on github
16-
---@field previewer? boolean
17-
---@field session_control? session_control
18-
---@field load_on_setup? boolean
19-
20-
---@type session_lens_config
21-
local defaultConf = {
22-
theme_conf = {},
23-
previewer = false,
24-
buftypes_to_ignore = {},
25-
}
26-
27-
-- Set default config on plugin load
28-
SessionLens.conf = defaultConf
29-
3010
function SessionLens.setup()
31-
SessionLens.conf = vim.tbl_deep_extend("force", SessionLens.conf, AutoSession.conf.session_lens)
11+
SessionLens.conf = AutoSession.conf.session_lens
3212

3313
if SessionLens.conf.buftypes_to_ignore ~= nil and not vim.tbl_isempty(SessionLens.conf.buftypes_to_ignore) then
3414
Lib.logger.warn "buftypes_to_ignore is deprecated. If you think you need this option, please file a bug on GitHub. If not, please remove it from your config"
@@ -131,9 +111,12 @@ SessionLens.search_session = function(custom_opts)
131111
cwd = session_root_dir,
132112
attach_mappings = function(_, map)
133113
telescope_actions.select_default:replace(Actions.source_session)
134-
map("i", "<c-d>", Actions.delete_session)
135-
map("i", "<c-s>", Actions.alternate_session)
136-
map("i", "<c-a>", Actions.alternate_session)
114+
115+
local mappings = AutoSession.conf.session_lens.mappings
116+
if mappings then
117+
map(mappings.delete_session[1], mappings.delete_session[2], Actions.delete_session)
118+
map(mappings.alternate_session[1], mappings.alternate_session[2], Actions.alternate_session)
119+
end
137120
return true
138121
end,
139122
}

0 commit comments

Comments
 (0)