Skip to content

Commit

Permalink
Delete current dir session (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianord authored Jan 20, 2024
1 parent 68dde35 commit 5bc6aa5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ 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` | Will remove all buffers and `:source` the last saved session. |
| `load_current_dir_session` | Will remove all buffers and `:source` the last saved session file of the current dirtectory. |
| `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. |
| `delete_session` | Select and delete session. |
| Argument | Description |
| -----------------------------| -------------------------------------------------------------------------------------------- |
| `load_session` | Select and load session. (Your current session won't appear on the list). |
| `load_last_session` | Will remove all buffers and `:source` the last saved session. |
| `load_current_dir_session` | Will remove all buffers and `:source` the last saved session file of the current dirtectory. |
| `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_sesssion`| Deletes the session associated with the current directory. |

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

Expand Down
17 changes: 12 additions & 5 deletions lua/session_manager/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,23 @@ function session_manager.delete_session()
format_item = function(item) return utils.shorten_path(item.dir) end,
}, function(item)
if item then
Path:new(item.filename):rm()
local cwd = vim.loop.cwd()
if utils.is_session and cwd and item.filename == config.dir_to_session_filename(cwd).filename then
utils.is_session = false
end
utils.delete_session(item.filename)
session_manager.delete_session()
end
end)
end

--- Deletes the session for the current working directory.
function session_manager.delete_current_dir_session()
local cwd = vim.loop.cwd()
if cwd then
local session = config.dir_to_session_filename(cwd)
if session:exists() then
utils.delete_session(session)
end
end
end

--- Saves a session based on settings. Executed before exiting the editor.
function session_manager.autosave_session()
if not config.autosave_last_session then
Expand Down
9 changes: 9 additions & 0 deletions lua/session_manager/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ function utils.save_session(filename)
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePost' })
end

---@param filename string
function utils.delete_session(filename)
Path:new(filename):rm()
local cwd = vim.loop.cwd()
if utils.is_session and cwd and filename == config.dir_to_session_filename(cwd).filename then
utils.is_session = false
end
end

---@return table
function utils.get_sessions()
local sessions = {}
Expand Down

0 comments on commit 5bc6aa5

Please sign in to comment.