From 38b36fc5fd7fb42ece47165f8f0d9d28be7e429d Mon Sep 17 00:00:00 2001 From: Connor Robertson Date: Fri, 14 Oct 2022 05:08:12 -0400 Subject: [PATCH] feat: :sparkles: 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 --- README.md | 17 +++++++- lua/persisted/config.lua | 1 + lua/persisted/init.lua | 13 +++++- tests/follow_cwd/follow_cwd_spec.lua | 65 ++++++++++++++++++++++++++++ tests/minimal.vim | 1 + 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 tests/follow_cwd/follow_cwd_spec.lua diff --git a/README.md b/README.md index 2976ff8..097927d 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Forked from 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: diff --git a/lua/persisted/config.lua b/lua/persisted/config.lua index c751ed7..21deddd 100644 --- a/lua/persisted/config.lua +++ b/lua/persisted/config.lua @@ -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 diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index 501c0ef..9c447ee 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -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 - vim.cmd("mks! " .. e(get_current())) + 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 diff --git a/tests/follow_cwd/follow_cwd_spec.lua b/tests/follow_cwd/follow_cwd_spec.lua new file mode 100644 index 0000000..85a705a --- /dev/null +++ b/tests/follow_cwd/follow_cwd_spec.lua @@ -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) diff --git a/tests/minimal.vim b/tests/minimal.vim index 0d6b241..91b5722 100644 --- a/tests/minimal.vim +++ b/tests/minimal.vim @@ -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