Skip to content

Commit

Permalink
Support autoloading session for the current git repository (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuchs authored Jun 7, 2024
1 parent b3058f6 commit 29f80e9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ The plugin saves the sessions in the specified folder (see [configuration](#conf

Use the command `:SessionManager[!]` with one of the following arguments:

| Argument | Description |
| -----------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `load_session` | Select and load session. (Your current session won't appear on the list). |
| `load_last_session` | Removes all buffers and tries to `:source` the last saved session. Returns `true` if the session was restored and `false` otherwise. |
| `load_current_dir_session` | Removes all buffers and tries to `:source` the last saved session file of the current directory. Returns `true` if the session was restored and `false` otherwise. |
| `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. |
| `delete_session` | Select and delete session. |
| `delete_current_dir_session` | Deletes the session associated with the current directory. |
| Argument | Description |
| -----------------------------| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `load_session` | Select and load session. (Your current session won't appear on the list). |
| `load_last_session` | Removes all buffers and tries to `:source` the last saved session. Returns `true` if the session was restored and `false` otherwise. |
| `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. |
| `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. |
| `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. |
| `delete_session` | Select and delete session. |
| `delete_current_dir_session` | Deletes the session associated with the current directory. |

When `!` is specified, the modified buffers will not be saved.

Expand Down Expand Up @@ -54,11 +55,12 @@ require('session_manager').setup({

If Neovim is started without arguments the value of the autoload_mode option is used to determine which session to initially load. The following modes are supported:

| Mode | Description |
| ----------- | ------------------------------------------------------------ |
| Disabled | No session will be loaded. |
| CurrentDir | The session in the current working directory will be loaded. |
| LastSession | The last session will be loaded. This is the default. |
| Mode | Description |
| ----------- | --------------------------------------------------------------- |
| Disabled | No session will be loaded. |
| CurrentDir | The session in the current working directory will be loaded. |
| LastSession | The last session will be loaded. This is the default. |
| GitSession | If in a git repo the session for repository root will be loaded |

`autoload_mode` can be set to either a single mode or an array of modes, in which
case each mode will be tried until one succeeds e.g.
Expand Down
1 change: 1 addition & 0 deletions lua/session_manager/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local config = {
'Disabled',
'CurrentDir',
'LastSession',
'GitSession',
}),
}

Expand Down
21 changes: 21 additions & 0 deletions lua/session_manager/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local config = require('session_manager.config')
local AutoloadMode = require('session_manager.config').AutoloadMode
local utils = require('session_manager.utils')
local Job = require('plenary.job')
local session_manager = {}

--- Apply user settings.
Expand Down Expand Up @@ -66,6 +67,25 @@ function session_manager.load_current_dir_session(discard_current)
return false
end

--- If in a git repo, tries to load a session for the repo's root directory
---@return boolean: `true` if session was loaded, `false` otherwise.
function session_manager.load_git_session(discard_current)
local job = Job:new({
command = 'git',
args = { 'rev-parse', '--show-toplevel' },
})
job:sync()
local git_dir = job:result()[1]
if git_dir then
local session = config.dir_to_session_filename(git_dir)
if session:exists() then
utils.load_session(session.filename, discard_current)
return true
end
end
return false
end

--- Saves a session for the current working directory.
function session_manager.save_current_session()
local cwd = vim.uv.cwd()
Expand All @@ -78,6 +98,7 @@ local autoloaders = {
[AutoloadMode.Disabled] = function() return true end,
[AutoloadMode.CurrentDir] = session_manager.load_current_dir_session,
[AutoloadMode.LastSession] = session_manager.load_last_session,
[AutoloadMode.GitSession] = session_manager.load_git_session,
}

--- Loads a session based on settings. Executed after starting the editor.
Expand Down

0 comments on commit 29f80e9

Please sign in to comment.