feat: load session by path (#48) (#47)

* feat: load session by path

* chore: change order of commands

* tests: fix

* docs: tweak readme wording
main
Oli 2023-02-21 21:09:01 +00:00 committed by GitHub
parent 675744d835
commit dfbd039fe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 20 deletions

View File

@ -22,7 +22,7 @@ Forked from <a href="https://github.com/folke/persistence.nvim">Persistence.nvim
- Supports sessions across multiple git branches
- Supports auto saving and loading of sessions with allowed/ignored directories
- Simple API to save/stop/restore/delete/list the current session(s)
- Telescope extension to list all sessions
- Telescope extension to work with saved sessions
## :zap: Requirements
@ -32,7 +32,18 @@ Forked from <a href="https://github.com/folke/persistence.nvim">Persistence.nvim
Install the plugin with your preferred package manager:
**[packer](https://github.com/wbthomason/packer.nvim)**
**[Lazy.nvim](https://github.com/folke/lazy.nvim)**
```lua
-- Lua
use({
"olimorris/persisted.nvim"
-- lazy = true, -- For lazy loading
config = true
})
```
**[Packer](https://github.com/wbthomason/packer.nvim)**
```lua
-- Lua
@ -41,12 +52,11 @@ use({
--module = "persisted", -- For lazy loading
config = function()
require("persisted").setup()
require("telescope").load_extension("persisted") -- To load the telescope extension
end,
})
```
**[vim-plug](https://github.com/junegunn/vim-plug)**
**[Vim Plug](https://github.com/junegunn/vim-plug)**
```vim
" Vim Script
@ -61,11 +71,19 @@ lua << EOF
EOF
```
### Telescope extension
Ensure that the telescope extension is loaded with:
```lua
require("telescope").load_extension("persisted")
```
### Lazy loading
The plugin is designed to work with startup screens like [vim-startify](https://github.com/mhinz/vim-startify) or [dashboard](https://github.com/glepnir/dashboard-nvim) out of the box. It will never load a session automatically by default.
However, to lazy load the plugin add the `module = "persisted"` line to packer.
However, to lazy load the plugin add the `module = "persisted"` line to packer or `lazy = true` for Lazy.nvim.
## :rocket: Usage
@ -77,7 +95,8 @@ The plugin comes with a number of commands:
- `:SessionStop` - Stop recording a session
- `:SessionSave` - Save the current session
- `:SessionLoad` - Load the session for the current directory and current branch if `git_use_branch = true`
- `:SessionLoadLast` - Load the last session
- `:SessionLoadLast` - Load the most recent session
- `:SessionLoadFromPath` - Load a session from a given path
- `:SessionDelete` - Delete the current session
> **Note:** The author only binds `:SessionToggle` to a keymap for simplicity.
@ -310,8 +329,6 @@ The plugin allows for *before* and *after* callbacks to be used when sourcing a
require("persisted").setup({
telescope = {
before_source = function()
-- Close all open buffers
-- Thanks to https://github.com/avently
vim.api.nvim_input("<ESC>:%bd<CR>")
end,
after_source = function(session)

View File

@ -68,6 +68,12 @@ function M.setup(opts)
config.setup(opts)
if config.options.autoload and (allow_dir() and not ignore_dir()) and vim.fn.argc() == 0 then
-- vim.api.nvim_create_autocmd("VimEnter", {
-- group = group,
-- callback = function()
-- M.load()
-- end,
-- })
M.load()
end
@ -85,7 +91,7 @@ end
---@return nil
function M.load(opt)
opt = opt or {}
local session = opt.last and get_last() or get_current()
local session = opt.session or (opt.last and get_last() or get_current())
if session then
if vim.fn.filereadable(session) ~= 0 then

View File

@ -5,6 +5,7 @@ command! SessionStop :lua require("persisted").stop()
command! SessionSave :lua require("persisted").save()
command! SessionLoad :lua require("persisted").load()
command! SessionLoadLast :lua require("persisted").load({ last = true })
command! -nargs=1 SessionLoadFromFile :lua require("persisted").load({ session = <f-args> })
command! SessionDelete :lua require("persisted").delete()
command! SessionToggle :lua require("persisted").toggle()

View File

@ -1,6 +1,6 @@
local session_dir = vim.fn.getcwd() .. "/tests/default_data/"
require("persisted").setup({
save_dir = session_dir
save_dir = session_dir,
})
describe("With default settings:", function()
@ -8,7 +8,7 @@ describe("With default settings:", function()
-- vim.fn.system("rm -rf " .. e(session_dir))
end)
it("saves a session", function()
it("it saves a session", function()
-- Check no file exists
assert.equals(vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", ""), "0")
@ -21,40 +21,40 @@ describe("With default settings:", function()
-- Check that the session is written to disk
assert.equals(vim.g.persisting, true)
assert.equals(vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", ""), "1")
assert.equals("1", vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", ""))
end)
it("loads a session", function()
it("it loads a session", function()
-- Load a session
require("persisted").load()
-- Read the buffers contents
local content = vim.fn.getline(1, '$')
local content = vim.fn.getline(1, "$")
assert.equals(content[1], "This is a test file")
assert.equals("This is a test file", content[1])
assert.equals(vim.g.persisting, true)
end)
it("stops a session", function()
it("it stops a session", function()
require("persisted").stop()
assert.equals(vim.g.persisting, false)
end)
it("starts a session", function()
it("it starts a session", function()
require("persisted").start()
assert.equals(vim.g.persisting, true)
end)
it("lists sessions", function()
it("it lists sessions", function()
local sessions = require("persisted").list()
local path = require("plenary.path"):new(sessions[1])
local path = require("plenary.path"):new(sessions[1].file_path)
assert.equals(path:is_path(), true)
end)
it("deletes a session", function()
it("it deletes a session", function()
require("persisted").delete()
assert.equals(vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", ""), "0")