feat: ✨ allow session name to be fixed on load
* Add follow_cwd option to allow fixing session name on load * Update readme to reflect new follow_cwd command and explain use * Add tests for follow_cwd * Update README.md * Adjust wording in README for follow_cwd to clarify use case Co-authored-by: Oli M <olimorris@users.noreply.github.com>main
parent
510eb02caa
commit
38b36fc5fd
17
README.md
17
README.md
|
|
@ -32,6 +32,7 @@ Forked from <a href="https://github.com/folke/persistence.nvim">Persistence.nvim
|
|||
- [Git branching](#git-branching)
|
||||
- [Autosaving](#autosaving)
|
||||
- [Autoloading](#autoloading)
|
||||
- [Following current working directory](#following-current-working-directory)
|
||||
- [Allowed directories](#allowed-directories)
|
||||
- [Ignored directories](#ignored-directories)
|
||||
- [Callbacks](#callbacks)
|
||||
|
|
@ -133,6 +134,7 @@ require("persisted").setup({
|
|||
should_autosave = nil, -- function to determine if a session should be autosaved
|
||||
autoload = false, -- automatically load the session for the cwd on Neovim startup
|
||||
on_autoload_no_session = nil, -- function to run when `autoload = true` but there is no session to load
|
||||
follow_cwd = true, -- change session file name to match current working directory if it changes
|
||||
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
|
||||
ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading
|
||||
before_save = nil, -- function to run before the session is saved to disk
|
||||
|
|
@ -234,11 +236,24 @@ require("persisted").setup({
|
|||
})
|
||||
```
|
||||
|
||||
|
||||
Autoloading can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
|
||||
|
||||
> **Note:** Autoloading will not occur if a user opens Neovim with arguments. For example: `nvim some_file.rb`
|
||||
|
||||
### Following current working directory
|
||||
|
||||
There may be a need to change the working directory to quickly access files in other directories without changing the current session's name on save. This behavior can be configured with `follow_cwd = false`.
|
||||
|
||||
By default, the session name will match the current working directory:
|
||||
|
||||
```lua
|
||||
require("persisted").setup({
|
||||
follow_cwd = true,
|
||||
})
|
||||
```
|
||||
|
||||
> **Note:** If `follow_cwd = false` the session name is stored upon loading under the global variable `vim.g.persisting_session`. This variable can be manually adjusted if changes to the session name are needed. Alternatively, if `follow_cwd = true` then `vim.g.persisting_session = nil`.
|
||||
|
||||
### Allowed directories
|
||||
|
||||
You may specify a table of directories for which the plugin will autosave and/or autoload from. For example:
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ local defaults = {
|
|||
autosave = true, -- automatically save session files when exiting Neovim
|
||||
should_autosave = nil, -- function to determine if a session should be autosaved
|
||||
autoload = false, -- automatically load the session for the cwd on Neovim startup
|
||||
follow_cwd = true, -- change session file name with changes in current working directory
|
||||
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
|
||||
ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading
|
||||
before_save = nil, -- function to run before the session is saved to disk
|
||||
|
|
|
|||
|
|
@ -104,6 +104,11 @@ function M.load(opt)
|
|||
|
||||
if session then
|
||||
if vim.fn.filereadable(session) ~= 0 then
|
||||
if config.options.follow_cwd then
|
||||
vim.g.persisting_session = nil
|
||||
else
|
||||
vim.g.persisting_session = session
|
||||
end
|
||||
utils.load_session(session, config.options.before_source, config.options.after_source, config.options.silent)
|
||||
elseif type(config.options.on_autoload_no_session) == "function" then
|
||||
config.options.on_autoload_no_session()
|
||||
|
|
@ -140,6 +145,7 @@ function M.stop()
|
|||
augroup! Persisted
|
||||
]])
|
||||
vim.g.persisting = false
|
||||
vim.g.persisting_session = nil
|
||||
end
|
||||
|
||||
---Save the session to disk
|
||||
|
|
@ -156,7 +162,12 @@ function M.save()
|
|||
return
|
||||
end
|
||||
|
||||
if vim.g.persisting_session == nil then
|
||||
vim.cmd("mks! " .. e(get_current()))
|
||||
else
|
||||
vim.cmd("mks! " .. e(vim.g.persisting_session))
|
||||
end
|
||||
|
||||
vim.g.persisting = true
|
||||
|
||||
if type(config.options.after_save) == "function" then
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
pcall(vim.fn.system, "rm -rf tests/dummy_data")
|
||||
|
||||
local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/"
|
||||
|
||||
-- follow_cwd = true
|
||||
require("persisted").setup({
|
||||
save_dir = session_dir,
|
||||
follow_cwd = true,
|
||||
})
|
||||
|
||||
describe("Follow cwd change", function()
|
||||
it("creates two sessions with change in cwd", function()
|
||||
|
||||
vim.cmd(":e tests/stubs/test_autoload.txt")
|
||||
vim.cmd(":w")
|
||||
|
||||
require("persisted").save()
|
||||
vim.cmd(":cd tests/stubs")
|
||||
vim.cmd(":w")
|
||||
require("persisted").save()
|
||||
vim.cmd(":bdelete")
|
||||
|
||||
end)
|
||||
it("ensures both sessions were created", function()
|
||||
local pattern = "/"
|
||||
local name1 = vim.fn.getcwd():gsub(pattern, "%%") .. require("persisted").get_branch() .. ".vim"
|
||||
vim.cmd(":cd ../..")
|
||||
local name2 = vim.fn.getcwd():gsub(pattern, "%%") .. require("persisted").get_branch() .. ".vim"
|
||||
|
||||
local sessions = vim.fn.readdir(session_dir)
|
||||
assert.equals(sessions[1], name1)
|
||||
assert.equals(sessions[2], name2)
|
||||
end)
|
||||
end)
|
||||
|
||||
-- follow_cwd = false
|
||||
pcall(vim.fn.system, "rm -rf tests/dummy_data")
|
||||
require("persisted").setup({
|
||||
save_dir = session_dir,
|
||||
follow_cwd = false,
|
||||
})
|
||||
|
||||
describe("Don't follow cwd change", function()
|
||||
it("creates two sessions with change in cwd", function()
|
||||
|
||||
vim.cmd(":e tests/stubs/test_autoload.txt")
|
||||
vim.cmd(":w")
|
||||
require("persisted").save()
|
||||
require("persisted").load()
|
||||
vim.cmd(":cd tests/stubs")
|
||||
vim.cmd(":w")
|
||||
require("persisted").save()
|
||||
vim.cmd(":bdelete")
|
||||
|
||||
end)
|
||||
it("ensures only one session was created", function()
|
||||
local pattern = "/"
|
||||
vim.cmd(":cd ../..")
|
||||
local name = vim.fn.getcwd():gsub(pattern, "%%") .. require("persisted").get_branch() .. ".vim"
|
||||
|
||||
local sessions = vim.fn.readdir(session_dir)
|
||||
assert.equals(#sessions, 1)
|
||||
assert.equals(name, sessions[1])
|
||||
end)
|
||||
end)
|
||||
|
|
@ -11,5 +11,6 @@ runtime plugin/plenary.vim
|
|||
command Setup PlenaryBustedDirectory tests/setup {minimal_init = 'tests/minimal.vim'}
|
||||
command TestAutoloading PlenaryBustedDirectory tests/autoload {minimal_init = 'tests/minimal.vim'}
|
||||
command TestGitBranching PlenaryBustedDirectory tests/git_branching {minimal_init = 'tests/minimal.vim'}
|
||||
command TestFollowCwd PlenaryBustedDirectory tests/follow_cwd/follow_cwd.lua {minimal_init = 'tests/minimal.vim'}
|
||||
command TestDefaults PlenaryBustedFile tests/default_settings_spec.lua
|
||||
command TearDown PlenaryBustedFile tests/teardown/clean_up_dirs.lua
|
||||
|
|
|
|||
Loading…
Reference in New Issue