Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: delete current dir session #107

Merged
merged 2 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,7 +1,7 @@
local config = require('session_manager.config')
local AutoloadMode = require('session_manager.config').AutoloadMode
local utils = require('session_manager.utils')
local Path = require('plenary.path')

Check warning on line 4 in lua/session_manager/init.lua

View workflow job for this annotation

GitHub Actions / Lint

unused variable Path
local session_manager = {}

--- Apply user settings.
Expand Down Expand Up @@ -69,16 +69,23 @@
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
Loading