Skip to content

Commit

Permalink
Merge pull request #319 from cameronr/create_enabled_function
Browse files Browse the repository at this point in the history
Fix #234 auto_session_create_enabled take a func
  • Loading branch information
rmagatti authored Jul 8, 2024
2 parents 15586c4 + 1cd7b24 commit df99506
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
47 changes: 34 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,19 @@ EOF

### Options

| Config | Options | Default | Description |
| -------------------------------- | ------------------------ | ------------------------------------ | --------------------------------------------------------------- |
| log_level | 'debug', 'info', 'error' | 'info' | Sets the log level of the plugin |
| auto_session_enable_last_session | false, true | false | Loads the last loaded session if session for cwd does not exist |
| auto_session_root_dir | "/some/path/you/want" | vim.fn.stdpath('data').."/sessions/" | Changes the root dir for sessions |
| auto_session_enabled | false, true | true | Enables/disables the plugin's auto save _and_ restore features |
| auto_session_create_enabled | false, true | true | Enables/disables the plugin's session auto creation |
| auto_save_enabled | false, true, nil | nil | Enables/disables auto saving |
| auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring |
| auto_restore_lazy_delay_enabled | false, true, nil | true | Enables/disables delaying auto-restore if Lazy.nvim is used |
| auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs |
| auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs |
| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name |
| Config | Options | Default | Description |
| -------------------------------- | ------------------------ | ------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| log_level | 'debug', 'info', 'error' | 'info' | Sets the log level of the plugin |
| auto_session_enable_last_session | false, true | false | Loads the last loaded session if session for cwd does not exist |
| auto_session_root_dir | "/some/path/you/want" | vim.fn.stdpath('data').."/sessions/" | Changes the root dir for sessions |
| auto_session_enabled | false, true | true | Enables/disables the plugin's auto save _and_ restore features |
| 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 |
| auto_save_enabled | false, true, nil | nil | Enables/disables auto saving |
| auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring |
| auto_restore_lazy_delay_enabled | false, true, nil | true | Enables/disables delaying auto-restore if Lazy.nvim is used |
| auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs |
| auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs |
| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name |

#### Notes

Expand Down Expand Up @@ -266,6 +266,27 @@ require('auto-session').setup {
}
```

## Conditionally creating a session

`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
use this to only create sessions for git projects:

```lua

config = function()
require('auto-session').setup({
auto_save_enabled = true,
auto_restore_enabled = true,

auto_session_create_enabled = function()
local cmd = 'git rev-parse --is-inside-work-tree'
return vim.fn.system(cmd) == 'true\n'
end,
})
end

```

## Argument Handling

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:
Expand Down
33 changes: 28 additions & 5 deletions lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end
---@field auto_session_enable_last_session? boolean
---@field auto_session_root_dir? string root directory for session files, by default is `vim.fn.stdpath('data')/sessions/`
---@field auto_session_enabled? boolean enable auto session
---@field auto_session_create_enabled boolean|nil Enables/disables auto creating new sessions
---@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
---@field auto_save_enabled? boolean Enables/disables auto saving session
---@field auto_restore_enabled? boolean Enables/disables auto restoring session
---@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
Expand All @@ -53,7 +53,7 @@ local defaultConf = {
auto_session_enable_last_session = vim.g.auto_session_enable_last_session or false, -- Enables/disables the "last session" feature
auto_session_root_dir = vim.fn.stdpath "data" .. "/sessions/", -- Root dir where sessions will be stored
auto_session_enabled = true, -- Enables/disables auto creating, saving and restoring
auto_session_create_enabled = nil, -- Enables/disables auto creating new sessions
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
auto_save_enabled = nil, -- Enables/disables auto save feature
auto_restore_enabled = nil, -- Enables/disables auto restore feature
auto_restore_lazy_delay_enabled = true, -- Enables/disables Lazy delay feature
Expand Down Expand Up @@ -175,9 +175,31 @@ end

local function is_auto_create_enabled()
if vim.g.auto_session_create_enabled ~= nil then
return vim.g.auto_session_create_enabled == Lib._VIM_TRUE
elseif AutoSession.conf.auto_session_create_enabled ~= nil then
return AutoSession.conf.auto_session_create_enabled
if type(vim.g.auto_session_create_enabled) == 'function' then
if (vim.g.auto_session_create_enabled()) then
Lib.logger.debug('vim.g.auto_session_create_enabled returned true, allowing creation')
return true
else
Lib.logger.debug('vim.g.auto_session_create_enabled returned false, not allowing creation')
return false
end
else
return vim.g.auto_session_create_enabled == Lib._VIM_TRUE
end
end

if AutoSession.conf.auto_session_create_enabled ~= nil then
if type(AutoSession.conf.auto_session_create_enabled) == 'function' then
if AutoSession.conf.auto_session_create_enabled() then
Lib.logger.debug('AutoSession.conf.auto_session_create_enabled returned true, allowing creation')
return true
else
Lib.logger.debug('AutoSession.conf.auto_session_create_enabled returned false, not allowing creation')
return false
end
else
return AutoSession.conf.auto_session_create_enabled
end
end

return true
Expand Down Expand Up @@ -449,6 +471,7 @@ function AutoSession.AutoSaveSession(sessions_dir)
if not is_auto_create_enabled() then
local session_file_name = get_session_file_name(sessions_dir)
if not Lib.is_readable(session_file_name) then
Lib.logger.debug('Create not enabled and no existing session, not creating session')
return
end
end
Expand Down

0 comments on commit df99506

Please sign in to comment.