Skip to content

Commit df99506

Browse files
authored
Merge pull request #319 from cameronr/create_enabled_function
Fix #234 auto_session_create_enabled take a func
2 parents 15586c4 + 1cd7b24 commit df99506

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

README.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,19 @@ EOF
114114

115115
### Options
116116

117-
| Config | Options | Default | Description |
118-
| -------------------------------- | ------------------------ | ------------------------------------ | --------------------------------------------------------------- |
119-
| log_level | 'debug', 'info', 'error' | 'info' | Sets the log level of the plugin |
120-
| auto_session_enable_last_session | false, true | false | Loads the last loaded session if session for cwd does not exist |
121-
| auto_session_root_dir | "/some/path/you/want" | vim.fn.stdpath('data').."/sessions/" | Changes the root dir for sessions |
122-
| auto_session_enabled | false, true | true | Enables/disables the plugin's auto save _and_ restore features |
123-
| auto_session_create_enabled | false, true | true | Enables/disables the plugin's session auto creation |
124-
| auto_save_enabled | false, true, nil | nil | Enables/disables auto saving |
125-
| auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring |
126-
| auto_restore_lazy_delay_enabled | false, true, nil | true | Enables/disables delaying auto-restore if Lazy.nvim is used |
127-
| auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs |
128-
| auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs |
129-
| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name |
117+
| Config | Options | Default | Description |
118+
| -------------------------------- | ------------------------ | ------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
119+
| log_level | 'debug', 'info', 'error' | 'info' | Sets the log level of the plugin |
120+
| auto_session_enable_last_session | false, true | false | Loads the last loaded session if session for cwd does not exist |
121+
| auto_session_root_dir | "/some/path/you/want" | vim.fn.stdpath('data').."/sessions/" | Changes the root dir for sessions |
122+
| auto_session_enabled | false, true | true | Enables/disables the plugin's auto save _and_ restore features |
123+
| auto_session_create_enabled | false, true, function | true | Enables/disables the plugin's session auto creation. Can also be a Lua function that returns true if a session should be created and false otherwise |
124+
| auto_save_enabled | false, true, nil | nil | Enables/disables auto saving |
125+
| auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring |
126+
| auto_restore_lazy_delay_enabled | false, true, nil | true | Enables/disables delaying auto-restore if Lazy.nvim is used |
127+
| auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs |
128+
| auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs |
129+
| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name |
130130

131131
#### Notes
132132

@@ -266,6 +266,27 @@ require('auto-session').setup {
266266
}
267267
```
268268

269+
## Conditionally creating a session
270+
271+
`auto_session_create_enabled` can take a function that returns if a session should be created or not as part of auto saving. As one example, you could
272+
use this to only create sessions for git projects:
273+
274+
```lua
275+
276+
config = function()
277+
require('auto-session').setup({
278+
auto_save_enabled = true,
279+
auto_restore_enabled = true,
280+
281+
auto_session_create_enabled = function()
282+
local cmd = 'git rev-parse --is-inside-work-tree'
283+
return vim.fn.system(cmd) == 'true\n'
284+
end,
285+
})
286+
end
287+
288+
```
289+
269290
## Argument Handling
270291

271292
By default, when `nvim` is run with a single directory argument, auto-session will try to restore the session for that directory. If `nvim` is run with multiple directories or any file arguments, auto-session won't try to restore a session and won't auto-save a session on exit (if enabled). Those behaviors can be changed with these config parameters:

lua/auto-session/init.lua

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ end
3838
---@field auto_session_enable_last_session? boolean
3939
---@field auto_session_root_dir? string root directory for session files, by default is `vim.fn.stdpath('data')/sessions/`
4040
---@field auto_session_enabled? boolean enable auto session
41-
---@field auto_session_create_enabled boolean|nil Enables/disables auto creating new sessions
41+
---@field auto_session_create_enabled boolean|function|nil Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not
4242
---@field auto_save_enabled? boolean Enables/disables auto saving session
4343
---@field auto_restore_enabled? boolean Enables/disables auto restoring session
4444
---@field auto_restore_lazy_delay_enabled? boolean Automatically detect if Lazy.nvim is being used and wait until Lazy is done to make sure session is restored correctly. Does nothing if Lazy isn't being used. Can be disabled if a problem is suspected or for debugging
@@ -53,7 +53,7 @@ local defaultConf = {
5353
auto_session_enable_last_session = vim.g.auto_session_enable_last_session or false, -- Enables/disables the "last session" feature
5454
auto_session_root_dir = vim.fn.stdpath "data" .. "/sessions/", -- Root dir where sessions will be stored
5555
auto_session_enabled = true, -- Enables/disables auto creating, saving and restoring
56-
auto_session_create_enabled = nil, -- Enables/disables auto creating new sessions
56+
auto_session_create_enabled = nil, -- Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not
5757
auto_save_enabled = nil, -- Enables/disables auto save feature
5858
auto_restore_enabled = nil, -- Enables/disables auto restore feature
5959
auto_restore_lazy_delay_enabled = true, -- Enables/disables Lazy delay feature
@@ -175,9 +175,31 @@ end
175175

176176
local function is_auto_create_enabled()
177177
if vim.g.auto_session_create_enabled ~= nil then
178-
return vim.g.auto_session_create_enabled == Lib._VIM_TRUE
179-
elseif AutoSession.conf.auto_session_create_enabled ~= nil then
180-
return AutoSession.conf.auto_session_create_enabled
178+
if type(vim.g.auto_session_create_enabled) == 'function' then
179+
if (vim.g.auto_session_create_enabled()) then
180+
Lib.logger.debug('vim.g.auto_session_create_enabled returned true, allowing creation')
181+
return true
182+
else
183+
Lib.logger.debug('vim.g.auto_session_create_enabled returned false, not allowing creation')
184+
return false
185+
end
186+
else
187+
return vim.g.auto_session_create_enabled == Lib._VIM_TRUE
188+
end
189+
end
190+
191+
if AutoSession.conf.auto_session_create_enabled ~= nil then
192+
if type(AutoSession.conf.auto_session_create_enabled) == 'function' then
193+
if AutoSession.conf.auto_session_create_enabled() then
194+
Lib.logger.debug('AutoSession.conf.auto_session_create_enabled returned true, allowing creation')
195+
return true
196+
else
197+
Lib.logger.debug('AutoSession.conf.auto_session_create_enabled returned false, not allowing creation')
198+
return false
199+
end
200+
else
201+
return AutoSession.conf.auto_session_create_enabled
202+
end
181203
end
182204

183205
return true
@@ -449,6 +471,7 @@ function AutoSession.AutoSaveSession(sessions_dir)
449471
if not is_auto_create_enabled() then
450472
local session_file_name = get_session_file_name(sessions_dir)
451473
if not Lib.is_readable(session_file_name) then
474+
Lib.logger.debug('Create not enabled and no existing session, not creating session')
452475
return
453476
end
454477
end

0 commit comments

Comments
 (0)