Update generated neovim config
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
name: Bugs or configuration issues
|
||||
description: If you've already asked for help with a problem and confirmed something is broken with the plugin itself, create a bug report.
|
||||
title: "[Bug]: "
|
||||
title: "[Bug]: TITLE DESCRIBING THE ISSUE"
|
||||
labels: ["bug"]
|
||||
assignees: olimorris
|
||||
|
||||
@ -8,12 +8,13 @@ body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thanks for taking the time to fill out this bug report! This form may seem onerous but it makes a resolution much quicker for you and I.
|
||||
Thanks for taking the time to fill out this bug report! This form may seem onerous but it makes a resolution much quicker if I can recreate the exact problem you're facing.
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
## Test with a `minimal.lua` file
|
||||
I know the temptation is to skip this step but please don't. With the quantity of plugins you have in your Neovim config, it's like searching for a needle in a haystack.
|
||||
> [!IMPORTANT]
|
||||
> This allows me to _recreate_ your issue on my machine. Please don't skip this step and please provide the full `minimal.lua` file
|
||||
|
||||
Firstly, test and run Neovim with the minimal config below. Be sure to add your Persisted.nvim config, saving to a `minimal.lua` file and adding any additional plugins you may need.
|
||||
```lua
|
||||
@ -74,7 +75,7 @@ body:
|
||||
id: persisted-config
|
||||
attributes:
|
||||
label: Your `minimal.lua` config
|
||||
description: Please paste your minimal.lua config here
|
||||
description: Please paste your minimal.lua config from above, here
|
||||
placeholder: |
|
||||
```lua
|
||||
-- Your minimal.lua config here
|
||||
@ -98,7 +99,7 @@ body:
|
||||
id: bug
|
||||
attributes:
|
||||
label: Describe the bug
|
||||
description: Please describe the bug and include any screenshots
|
||||
description: Please describe what you expect to happen and what is actually happening. Screenshots may be useful
|
||||
placeholder: |
|
||||
### What I expect to happen
|
||||
[Your text here]
|
||||
@ -112,7 +113,7 @@ body:
|
||||
id: reproduce
|
||||
attributes:
|
||||
label: Reproduce the bug
|
||||
description: Please include the steps to reproduce the bug
|
||||
description: Please include the steps so I can reproduce the bug
|
||||
placeholder: |
|
||||
Steps to reproduce:
|
||||
1.
|
||||
|
||||
@ -2,11 +2,10 @@ name: Continuous Integration
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
branches:
|
||||
- main
|
||||
pull_request: ~
|
||||
|
||||
# Cancel any in-progress CI runs for a PR if it is updated
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
|
||||
cancel-in-progress: true
|
||||
@ -52,3 +51,17 @@ jobs:
|
||||
export VIM="${PWD}/_neovim/share/nvim/runtime"
|
||||
nvim --version
|
||||
make test
|
||||
|
||||
release:
|
||||
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- tests
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
steps:
|
||||
- uses: googleapis/release-please-action@v4
|
||||
with:
|
||||
token: ${{ secrets.RELEASE_PLEASE_TOKEN }}
|
||||
release-type: simple
|
||||
|
||||
16
config/neovim/store/lazy-plugins/persisted.nvim/CHANGELOG.md
Normal file
16
config/neovim/store/lazy-plugins/persisted.nvim/CHANGELOG.md
Normal file
@ -0,0 +1,16 @@
|
||||
# Changelog
|
||||
|
||||
## [1.1.1](https://github.com/olimorris/persisted.nvim/compare/v1.1.0...v1.1.1) (2024-09-03)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* [#146](https://github.com/olimorris/persisted.nvim/issues/146) better handling for empty tables ([c9c11ee](https://github.com/olimorris/persisted.nvim/commit/c9c11ee71feeecfa86bc7650eda4c4d0c5505f6d))
|
||||
* SessionDelete now uses native vim cmds ([#156](https://github.com/olimorris/persisted.nvim/issues/156)) ([6e9c992](https://github.com/olimorris/persisted.nvim/commit/6e9c992381765a57d29ca7dc0b912683216d59c4))
|
||||
|
||||
## [1.1.0](https://github.com/olimorris/persisted.nvim/compare/v1.0.0...v1.1.0) (2024-08-22)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add `SessionSelect` command ([999b691](https://github.com/olimorris/persisted.nvim/commit/999b6918b403e3b4ffa6b60d735557ace230ad83))
|
||||
@ -36,6 +36,7 @@ test: $(PLENARY_DIR)
|
||||
nvim --headless --noplugin -u tests/minimal.vim +TestGitBranching
|
||||
nvim --headless --noplugin -u tests/minimal.vim +TestFollowCwd
|
||||
nvim --headless --noplugin -u tests/minimal.vim +TestDefaults
|
||||
nvim --headless --noplugin -u tests/minimal.vim +TestDirs
|
||||
nvim --headless --noplugin -u tests/minimal.vim +TearDown
|
||||
|
||||
$(PLENARY_DIR):
|
||||
|
||||
@ -8,9 +8,8 @@
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/olimorris/persisted.nvim/stargazers"><img src="https://img.shields.io/github/stars/olimorris/persisted.nvim?color=c678dd&logoColor=e06c75&style=for-the-badge"></a>
|
||||
<a href="https://github.com/olimorris/persisted.nvim/issues"><img src="https://img.shields.io/github/issues/olimorris/persisted.nvim?color=%23d19a66&style=for-the-badge"></a>
|
||||
<a href="https://github.com/olimorris/persisted.nvim/blob/main/LICENSE"><img src="https://img.shields.io/github/license/olimorris/persisted.nvim?style=for-the-badge"></a>
|
||||
<a href="https://github.com/olimorris/persisted.nvim/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/olimorris/persisted.nvim/ci.yml?branch=main&label=tests&style=for-the-badge"></a>
|
||||
<a href="https://github.com/olimorris/persisted.nvim/releases"><img src="https://img.shields.io/github/v/release/olimorris/persisted.nvim?style=for-the-badge"></a>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
@ -23,8 +22,8 @@
|
||||
## :sparkles: Features
|
||||
|
||||
- :evergreen_tree: Supports sessions across multiple git branches
|
||||
- :telescope: Telescope extension to work with saved sessions
|
||||
- :tickets: Custom events which users can hook into for tighter integration
|
||||
- :telescope: Telescope extension to manage sessions
|
||||
- :tickets: Custom events which users can hook into for tighter integrations
|
||||
- :memo: Simple API to save/stop/restore/delete/list the current session(s)
|
||||
- :open_file_folder: Supports autosaving and autoloading of sessions with allowed/ignored directories
|
||||
- :floppy_disk: Automatically saves the active session under `.local/share/nvim/sessions` on exiting Neovim
|
||||
@ -35,7 +34,7 @@
|
||||
|
||||
## :package: Installation
|
||||
|
||||
Install the plugin with your preferred package manager:
|
||||
Install and configure the plugin with your preferred package manager:
|
||||
|
||||
**[Lazy.nvim](https://github.com/folke/lazy.nvim)**
|
||||
|
||||
@ -110,9 +109,10 @@ require('telescope').setup({
|
||||
The plugin comes with a number of commands:
|
||||
|
||||
- `:SessionToggle` - Determines whether to load, start or stop a session
|
||||
- `:SessionStart` - Start recording a session. Useful if `autosave = false`
|
||||
- `:SessionStart` - Start recording a session. Useful if `autostart = false`
|
||||
- `:SessionStop` - Stop recording a session
|
||||
- `:SessionSave` - Save the current session
|
||||
- `:SessionSelect` - Load a session from the list (useful if you don't wish to use the Telescope extension)
|
||||
- `:SessionLoad` - Load the session for the current directory and current branch (if `git_use_branch = true`)
|
||||
- `:SessionLoadLast` - Load the most recent session
|
||||
- `:SessionLoadFromFile` - Load a session from a given path
|
||||
@ -123,12 +123,12 @@ The plugin comes with a number of commands:
|
||||
<!-- panvimdoc-ignore-start -->
|
||||
|
||||
<p align="center">
|
||||
<img src="https://github.com/olimorris/persisted.nvim/assets/9512444/5bfd6f94-ff70-4f2b-9193-53cdf7140d75" alt="Telescope">
|
||||
<img src="https://github.com/user-attachments/assets/3ff91790-b61a-4089-b87d-432e8b4969c2" alt="Telescope">
|
||||
</p>
|
||||
|
||||
<!-- panvimdoc-ignore-end -->
|
||||
|
||||
The Telescope extension may be opened via `:Telescope persisted`. The available actions are:
|
||||
The Telescope extension may be opened via `:Telescope persisted`. The default actions are:
|
||||
|
||||
- `<CR>` - Open/source the session file
|
||||
- `<C-b>` - Add/update the git branch for the session file
|
||||
@ -140,8 +140,8 @@ The Telescope extension may be opened via `:Telescope persisted`. The available
|
||||
The plugin sets a number of global variables throughout its lifecycle:
|
||||
|
||||
- `vim.g.persisting` - (bool) Determines if the plugin is active for the current session
|
||||
- `vim.g.persisted_exists` - (bool) Determines if a session exists for the current working directory
|
||||
- `vim.g.persisted_loaded_session` - (string) The file path to the current session
|
||||
- `vim.g.persisting_session` - (string) The file path to the current session (if `follow_cwd` is false)
|
||||
- `vim.g.persisted_loaded_session` - (string) The file path to the last loaded session
|
||||
|
||||
## :wrench: Configuration
|
||||
|
||||
@ -150,34 +150,41 @@ The plugin sets a number of global variables throughout its lifecycle:
|
||||
The plugin comes with the following defaults:
|
||||
|
||||
```lua
|
||||
require("persisted").setup({
|
||||
log_level = "ERROR", -- One of "TRACE", "DEBUG", "ERROR"
|
||||
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
|
||||
silent = false, -- silent nvim message when sourcing session file
|
||||
use_git_branch = false, -- create session files based on the branch of a git enabled repository
|
||||
default_branch = "main", -- the branch to load if a session file is not found for the current branch
|
||||
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
|
||||
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
|
||||
ignored_branches = nil, -- table of branch patterns that are ignored for auto-saving and auto-loading
|
||||
{
|
||||
autostart = true, -- Automatically start the plugin on load?
|
||||
|
||||
-- Function to determine if a session should be saved
|
||||
---@type fun(): boolean
|
||||
should_save = function()
|
||||
return true
|
||||
end,
|
||||
|
||||
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- Directory where session files are saved
|
||||
|
||||
follow_cwd = true, -- Change the session file to match any change in the cwd?
|
||||
use_git_branch = false, -- Include the git branch in the session file name?
|
||||
autoload = false, -- Automatically load the session for the cwd on Neovim startup?
|
||||
|
||||
-- Function to run when `autoload = true` but there is no session to load
|
||||
---@type fun(): any
|
||||
on_autoload_no_session = function() end,
|
||||
|
||||
allowed_dirs = {}, -- Table of dirs that the plugin will start and autoload from
|
||||
ignored_dirs = {}, -- Table of dirs that are ignored for starting and autoloading
|
||||
|
||||
telescope = {
|
||||
reset_prompt = true, -- Reset the Telescope prompt after an action?
|
||||
mappings = { -- table of mappings for the Telescope extension
|
||||
change_branch = "<c-b>",
|
||||
copy_session = "<c-c>",
|
||||
delete_session = "<c-d>",
|
||||
mappings = { -- Mappings for managing sessions in Telescope
|
||||
copy_session = "<C-c>",
|
||||
change_branch = "<C-b>",
|
||||
delete_session = "<C-d>",
|
||||
},
|
||||
icons = { -- icons displayed in the picker, set to nil to disable entirely
|
||||
icons = { -- icons displayed in the Telescope picker
|
||||
selected = " ",
|
||||
dir = " ",
|
||||
branch = " ",
|
||||
dir = " ",
|
||||
selected = " ",
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
**What is saved in the session?**
|
||||
@ -185,7 +192,7 @@ require("persisted").setup({
|
||||
As the plugin uses Vim's `:mksession` command then you may change the `vim.o.sessionoptions` value to determine what to write into the session. Please see `:h sessionoptions` for more information.
|
||||
|
||||
> [!NOTE]
|
||||
> The author uses: `vim.o.sessionoptions = "buffers,curdir,folds,tabpages,winpos,winsize"`
|
||||
> The author uses: `vim.o.sessionoptions = "buffers,curdir,folds,globals,tabpages,winpos,winsize"`
|
||||
|
||||
**Session save location**
|
||||
|
||||
@ -210,24 +217,27 @@ require("persisted").setup({
|
||||
})
|
||||
```
|
||||
|
||||
**Autosaving**
|
||||
**Autostart**
|
||||
|
||||
By default, the plugin will automatically save a Neovim session to disk when the `VimLeavePre` event is triggered. Autosaving can be turned off by:
|
||||
By default, the plugin will automatically start when the setup function is called. This results in a Neovim session being saved to disk when the `VimLeavePre` event is triggered. This can be disabled by:
|
||||
|
||||
```lua
|
||||
require("persisted").setup({
|
||||
autosave = false,
|
||||
autostart = false,
|
||||
})
|
||||
```
|
||||
|
||||
Autosaving can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
|
||||
Autostarting can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
|
||||
|
||||
There may be occasions when you do not wish to autosave; perhaps when a dashboard or a certain buftype is present. To control this, a callback function, `should_autosave`, may be used which should return a boolean value.
|
||||
**`should_save`**
|
||||
|
||||
There may be occasions when you do not wish to save the session; perhaps when a dashboard or a certain filetype is present. To handle this, the `should_save` function may be used which should return a boolean value.
|
||||
|
||||
```lua
|
||||
require("persisted").setup({
|
||||
should_autosave = function()
|
||||
-- do not autosave if the alpha dashboard is the current filetype
|
||||
---@return bool
|
||||
should_save = function()
|
||||
-- Do not save if the alpha dashboard is the current filetype
|
||||
if vim.bo.filetype == "alpha" then
|
||||
return false
|
||||
end
|
||||
@ -236,10 +246,7 @@ require("persisted").setup({
|
||||
})
|
||||
```
|
||||
|
||||
Of course, if you wish to manually save the session when autosaving is disabled, the `:SessionSave` command can be used.
|
||||
|
||||
> [!NOTE]
|
||||
> If `autosave = false` then the `should_autosave` callback will not be executed.
|
||||
Of course, if you wish to manually save the session the `:SessionSave` command can be used.
|
||||
|
||||
**Autoloading**
|
||||
|
||||
@ -251,7 +258,7 @@ require("persisted").setup({
|
||||
})
|
||||
```
|
||||
|
||||
You can also provide a function to run when `autoload = true` but there is no session to be loaded:
|
||||
You can also provide a function to run when `autoload = true` and there is no session to load:
|
||||
|
||||
```lua
|
||||
require("persisted").setup({
|
||||
@ -262,29 +269,14 @@ require("persisted").setup({
|
||||
})
|
||||
```
|
||||
|
||||
Autoloading can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
|
||||
Autoloading can be further controlled for directories in the `allowed_dirs` and `ignored_dirs` config tables.
|
||||
|
||||
> [!NOTE]
|
||||
> Autoloading will not occur if the plugin is lazy loaded or a user opens Neovim with arguments other than a single directory argument. For example: `nvim some_file.rb` will not result in autoloading but `nvim some/existing/path` or `nvim .` will.
|
||||
|
||||
**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`.
|
||||
> [!IMPORTANT]
|
||||
> By design, the plugin will not autoload a session when any arguments are passed to Neovim such as `nvim my_file.py`
|
||||
|
||||
**Allowed directories**
|
||||
|
||||
You may specify a table of directories for which the plugin will autosave and/or autoload from. For example:
|
||||
You may specify a table of directories for which the plugin will start and/or autoload from. For example:
|
||||
|
||||
```lua
|
||||
require("persisted").setup({
|
||||
@ -295,14 +287,14 @@ require("persisted").setup({
|
||||
})
|
||||
```
|
||||
|
||||
Specifying `~/Code` will autosave and autoload from that directory as well as all its sub-directories.
|
||||
Specifying `~/Code` will start and autoload from that directory as well as all its sub-directories.
|
||||
|
||||
> [!NOTE]
|
||||
> If `allowed_dirs` is left at its default value and `autosave` and/or `autoload` are set to `true`, then the plugin will autoload/autosave from _any_ directory
|
||||
> If `allowed_dirs` is left at its default value and `autostart` and/or `autoload` are set to `true`, then the plugin will start and autoload from _any_ directory
|
||||
|
||||
**Ignored directories**
|
||||
|
||||
You may specify a table of directories for which the plugin will **never** autosave and autoload from. For example:
|
||||
You may specify a table of directories for which the plugin will **never** start and autoload from. For example:
|
||||
|
||||
```lua
|
||||
require("persisted").setup({
|
||||
@ -330,19 +322,6 @@ require("persisted").setup({
|
||||
|
||||
In this setup, `~/.config` and `~/.local/nvim` are still going to behave in their default setting (ignoring all listed directory and its children), however `/` and `/tmp` will only ignore those directories exactly.
|
||||
|
||||
**Ignored branches**
|
||||
|
||||
You may specify a table of patterns that match against branches for which the plugin will **never** autosave and autoload from:
|
||||
|
||||
```lua
|
||||
require("persisted").setup({
|
||||
ignored_branches = {
|
||||
"^master",
|
||||
"feature/%u"
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
**Events / Callbacks**
|
||||
|
||||
The plugin fires events at various points during its lifecycle:
|
||||
@ -355,21 +334,19 @@ The plugin fires events at various points during its lifecycle:
|
||||
- `PersistedSavePost` - For _after_ a session is saved
|
||||
- `PersistedDeletePre` - For _before_ a session is deleted
|
||||
- `PersistedDeletePost` - For _after_ a session is deleted
|
||||
- `PersistedStateChange` - For when a session is _started_ or _stopped_
|
||||
- `PersistedToggled` - For when a session is toggled
|
||||
- `PersistedStart` - For when a session has _started_
|
||||
- `PersistedStop` - For when a session has _stopped_
|
||||
- `PersistedToggle` - For when a session is toggled
|
||||
|
||||
These events can be consumed anywhere within your configuration by utilising the `vim.api.nvim_create_autocmd` function.
|
||||
|
||||
A commonly requested example is to use the Telescope extension to load a session, saving the current session before clearing all of the open buffers:
|
||||
|
||||
```lua
|
||||
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
|
||||
|
||||
vim.api.nvim_create_autocmd({ "User" }, {
|
||||
vim.api.nvim_create_autocmd("User", {
|
||||
pattern = "PersistedTelescopeLoadPre",
|
||||
group = group,
|
||||
callback = function(session)
|
||||
-- Save the currently loaded session using a global variable
|
||||
-- Save the currently loaded session using the global variable
|
||||
require("persisted").save({ session = vim.g.persisted_loaded_session })
|
||||
|
||||
-- Delete all of the open buffers
|
||||
@ -378,34 +355,107 @@ vim.api.nvim_create_autocmd({ "User" }, {
|
||||
})
|
||||
```
|
||||
|
||||
**Using callback data**
|
||||
**Highlights**
|
||||
|
||||
When certain events are fired, session data is made available for the user to consume, for example:
|
||||
The plugin also comes with pre-defined highlight groups for the Telescope implementation:
|
||||
|
||||
- `PersistedTelescopeSelected`
|
||||
- `PersistedTelescopeDir`
|
||||
- `PersistedTelescopeBranch`
|
||||
|
||||
## :building_construction: Extending the Plugin
|
||||
|
||||
The plugin has been designed to be fully extensible. All of the functions in the [init.lua](https://github.com/olimorris/persisted.nvim/blob/main/lua/persisted/init.lua) and [utils.lua](https://github.com/olimorris/persisted.nvim/blob/main/lua/persisted/utils.lua) file are public.
|
||||
|
||||
**Custom Autoloading** by [neandrake](https://github.com/neandrake)
|
||||
|
||||
Autoloading a session if arguments are passed to Neovim:
|
||||
|
||||
```lua
|
||||
{
|
||||
branch = "main",
|
||||
dir_path = "Code/Neovim/persisted.nvim",
|
||||
file_path = "/Users/Oli/.local/share/nvim/sessions/%Users%Oli%Code%Neovim%persisted.nvim@@main.vim",
|
||||
name = "Code/Neovim/persisted.nvim@@main",
|
||||
"olimorris/persisted.nvim",
|
||||
lazy = false,
|
||||
opts = {
|
||||
autoload = true,
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
To consume this data, use the `session.data` table in your autocmd:
|
||||
-- Somewhere in your config
|
||||
local persisted = require("persisted")
|
||||
vim.api.nvim_create_autocmd("VimEnter", {
|
||||
nested = true,
|
||||
callback = function()
|
||||
if vim.g.started_with_stdin then
|
||||
return
|
||||
end
|
||||
|
||||
```lua
|
||||
vim.api.nvim_create_autocmd({ "User" }, {
|
||||
pattern = "PersistedLoadPost",
|
||||
group = group,
|
||||
callback = function(session)
|
||||
print(session.data.branch)
|
||||
local forceload = false
|
||||
if vim.fn.argc() == 0 then
|
||||
forceload = true
|
||||
elseif vim.fn.argc() == 1 then
|
||||
local dir = vim.fn.expand(vim.fn.argv(0))
|
||||
if dir == '.' then
|
||||
dir = vim.fn.getcwd()
|
||||
end
|
||||
|
||||
if vim.fn.isdirectory(dir) ~= 0 then
|
||||
forceload = true
|
||||
end
|
||||
end
|
||||
|
||||
persisted.autoload({ force = forceload })
|
||||
end,
|
||||
})
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> This data is available for the `PersistedLoad`, `PersistedDelete` and `PersistedTelescope` events
|
||||
**Git Branching from directories that are not the CWD** by [mrloop](https://github.com/mrloop)
|
||||
|
||||
## :page_with_curl: License
|
||||
As per [#149](https://github.com/olimorris/persisted.nvim/discussions/149), if you invoke Neovim from a sub-directory then the git branch will not be detected. The code below amends for this:
|
||||
|
||||
[MIT](https://github.com/olimorris/persisted.nvim/blob/main/LICENSE)
|
||||
```lua
|
||||
{
|
||||
"olimorris/persisted.nvim",
|
||||
lazy = false,
|
||||
opts = {
|
||||
autoload = true,
|
||||
autosave = true,
|
||||
use_git_branch = true,
|
||||
},
|
||||
config = function(_, opts)
|
||||
local persisted = require("persisted")
|
||||
persisted.branch = function()
|
||||
local branch = vim.fn.systemlist("git branch --show-current")[1]
|
||||
return vim.v.shell_error == 0 and branch or nil
|
||||
end
|
||||
persisted.setup(opts)
|
||||
end,
|
||||
}
|
||||
```
|
||||
|
||||
**Ignore Branches**
|
||||
|
||||
If you'd like to ignore certain branches from being saved as a session:
|
||||
|
||||
```lua
|
||||
{
|
||||
"olimorris/persisted.nvim",
|
||||
lazy = false,
|
||||
opts = {
|
||||
autostart = true,
|
||||
use_git_branch = true,
|
||||
},
|
||||
config = function(_, opts)
|
||||
local persisted = require("persisted")
|
||||
local utils = require("persisted.utils")
|
||||
local ignored_branches = {
|
||||
"feature_branch"
|
||||
}
|
||||
|
||||
persisted.setup(opts)
|
||||
if not utils.in_table(persisted.branch(), ignored_branches) then
|
||||
persisted.load()
|
||||
persisted.start()
|
||||
end
|
||||
end
|
||||
}
|
||||
```
|
||||
|
||||
@ -8,14 +8,13 @@ Table of Contents *persisted.nvim-table-of-contents*
|
||||
- Installation |persisted.nvim-installation|
|
||||
- Usage |persisted.nvim-usage|
|
||||
- Configuration |persisted.nvim-configuration|
|
||||
- License |persisted.nvim-license|
|
||||
- Extending the Plugin |persisted.nvim-extending-the-plugin|
|
||||
|
||||
FEATURES *persisted.nvim-features*
|
||||
|
||||
|
||||
- Supports sessions across multiple git branches
|
||||
- Telescope extension to work with saved sessions
|
||||
- Custom events which users can hook into for tighter integration
|
||||
- Telescope extension to manage sessions
|
||||
- Custom events which users can hook into for tighter integrations
|
||||
- Simple API to save/stop/restore/delete/list the current session(s)
|
||||
- Supports autosaving and autoloading of sessions with allowed/ignored directories
|
||||
- Automatically saves the active session under `.local/share/nvim/sessions` on exiting Neovim
|
||||
@ -23,13 +22,12 @@ FEATURES *persisted.nvim-features*
|
||||
|
||||
REQUIREMENTS *persisted.nvim-requirements*
|
||||
|
||||
|
||||
- Neovim >= 0.8.0
|
||||
|
||||
|
||||
INSTALLATION *persisted.nvim-installation*
|
||||
|
||||
Install the plugin with your preferred package manager:
|
||||
Install and configure the plugin with your preferred package manager:
|
||||
|
||||
**Lazy.nvim**
|
||||
|
||||
@ -106,11 +104,11 @@ USAGE *persisted.nvim-usage*
|
||||
|
||||
The plugin comes with a number of commands:
|
||||
|
||||
|
||||
- `:SessionToggle` - Determines whether to load, start or stop a session
|
||||
- `:SessionStart` - Start recording a session. Useful if `autosave = false`
|
||||
- `:SessionStart` - Start recording a session. Useful if `autostart = false`
|
||||
- `:SessionStop` - Stop recording a session
|
||||
- `:SessionSave` - Save the current session
|
||||
- `:SessionSelect` - Load a session from the list (useful if you don’t wish to use the Telescope extension)
|
||||
- `:SessionLoad` - Load the session for the current directory and current branch (if `git_use_branch = true`)
|
||||
- `:SessionLoadLast` - Load the most recent session
|
||||
- `:SessionLoadFromFile` - Load a session from a given path
|
||||
@ -118,10 +116,9 @@ The plugin comes with a number of commands:
|
||||
|
||||
**Telescope extension**
|
||||
|
||||
The Telescope extension may be opened via `:Telescope persisted`. The available
|
||||
The Telescope extension may be opened via `:Telescope persisted`. The default
|
||||
actions are:
|
||||
|
||||
|
||||
- `<CR>` - Open/source the session file
|
||||
- `<C-b>` - Add/update the git branch for the session file
|
||||
- `<C-c>` - Copy the session file
|
||||
@ -131,10 +128,9 @@ actions are:
|
||||
|
||||
The plugin sets a number of global variables throughout its lifecycle:
|
||||
|
||||
|
||||
- `vim.g.persisting` - (bool) Determines if the plugin is active for the current session
|
||||
- `vim.g.persisted_exists` - (bool) Determines if a session exists for the current working directory
|
||||
- `vim.g.persisted_loaded_session` - (string) The file path to the current session
|
||||
- `vim.g.persisting_session` - (string) The file path to the current session (if `follow_cwd` is false)
|
||||
- `vim.g.persisted_loaded_session` - (string) The file path to the last loaded session
|
||||
|
||||
|
||||
CONFIGURATION *persisted.nvim-configuration*
|
||||
@ -144,45 +140,52 @@ CONFIGURATION *persisted.nvim-configuration*
|
||||
The plugin comes with the following defaults:
|
||||
|
||||
>lua
|
||||
require("persisted").setup({
|
||||
log_level = "ERROR", -- One of "TRACE", "DEBUG", "ERROR"
|
||||
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
|
||||
silent = false, -- silent nvim message when sourcing session file
|
||||
use_git_branch = false, -- create session files based on the branch of a git enabled repository
|
||||
default_branch = "main", -- the branch to load if a session file is not found for the current branch
|
||||
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
|
||||
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
|
||||
ignored_branches = nil, -- table of branch patterns that are ignored for auto-saving and auto-loading
|
||||
{
|
||||
autostart = true, -- Automatically start the plugin on load?
|
||||
|
||||
-- Function to determine if a session should be saved
|
||||
---@type fun(): boolean
|
||||
should_save = function()
|
||||
return true
|
||||
end,
|
||||
|
||||
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- Directory where session files are saved
|
||||
|
||||
follow_cwd = true, -- Change the session file to match any change in the cwd?
|
||||
use_git_branch = false, -- Include the git branch in the session file name?
|
||||
autoload = false, -- Automatically load the session for the cwd on Neovim startup?
|
||||
|
||||
-- Function to run when `autoload = true` but there is no session to load
|
||||
---@type fun(): any
|
||||
on_autoload_no_session = function() end,
|
||||
|
||||
allowed_dirs = {}, -- Table of dirs that the plugin will start and autoload from
|
||||
ignored_dirs = {}, -- Table of dirs that are ignored for starting and autoloading
|
||||
|
||||
telescope = {
|
||||
reset_prompt = true, -- Reset the Telescope prompt after an action?
|
||||
mappings = { -- table of mappings for the Telescope extension
|
||||
change_branch = "<c-b>",
|
||||
copy_session = "<c-c>",
|
||||
delete_session = "<c-d>",
|
||||
mappings = { -- Mappings for managing sessions in Telescope
|
||||
copy_session = "<C-c>",
|
||||
change_branch = "<C-b>",
|
||||
delete_session = "<C-d>",
|
||||
},
|
||||
icons = { -- icons displayed in the picker, set to nil to disable entirely
|
||||
branch = " ",
|
||||
dir = " ",
|
||||
selected = " ",
|
||||
icons = { -- icons displayed in the Telescope picker
|
||||
selected = " ",
|
||||
dir = " ",
|
||||
branch = " ",
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
<
|
||||
|
||||
**What is saved in the session?**
|
||||
|
||||
As the plugin uses Vim’s `:mksession` command then you may change the
|
||||
`vim.o.sessionoptions` value to determine what to write into the session.
|
||||
Please see `:h sessionoptions` for more information.
|
||||
Please see |sessionoptions| for more information.
|
||||
|
||||
|
||||
[!NOTE] The author uses: `vim.o.sessionoptions =
|
||||
"buffers,curdir,folds,tabpages,winpos,winsize"`
|
||||
"buffers,curdir,folds,globals,tabpages,winpos,winsize"`
|
||||
**Session save location**
|
||||
|
||||
The location of the session files may be changed by altering the `save_dir`
|
||||
@ -208,28 +211,32 @@ files for a given project, by using git branches. To enable git branching:
|
||||
})
|
||||
<
|
||||
|
||||
**Autosaving**
|
||||
**Autostart**
|
||||
|
||||
By default, the plugin will automatically save a Neovim session to disk when
|
||||
the `VimLeavePre` event is triggered. Autosaving can be turned off by:
|
||||
By default, the plugin will automatically start when the setup function is
|
||||
called. This results in a Neovim session being saved to disk when the
|
||||
`VimLeavePre` event is triggered. This can be disabled by:
|
||||
|
||||
>lua
|
||||
require("persisted").setup({
|
||||
autosave = false,
|
||||
autostart = false,
|
||||
})
|
||||
<
|
||||
|
||||
Autosaving can be further controlled for certain directories by specifying
|
||||
Autostarting can be further controlled for certain directories by specifying
|
||||
`allowed_dirs` and `ignored_dirs`.
|
||||
|
||||
There may be occasions when you do not wish to autosave; perhaps when a
|
||||
dashboard or a certain buftype is present. To control this, a callback
|
||||
function, `should_autosave`, may be used which should return a boolean value.
|
||||
**should_save**
|
||||
|
||||
There may be occasions when you do not wish to save the session; perhaps when a
|
||||
dashboard or a certain filetype is present. To handle this, the `should_save`
|
||||
function may be used which should return a boolean value.
|
||||
|
||||
>lua
|
||||
require("persisted").setup({
|
||||
should_autosave = function()
|
||||
-- do not autosave if the alpha dashboard is the current filetype
|
||||
---@return bool
|
||||
should_save = function()
|
||||
-- Do not save if the alpha dashboard is the current filetype
|
||||
if vim.bo.filetype == "alpha" then
|
||||
return false
|
||||
end
|
||||
@ -238,12 +245,9 @@ function, `should_autosave`, may be used which should return a boolean value.
|
||||
})
|
||||
<
|
||||
|
||||
Of course, if you wish to manually save the session when autosaving is
|
||||
disabled, the `:SessionSave` command can be used.
|
||||
Of course, if you wish to manually save the session the `:SessionSave` command
|
||||
can be used.
|
||||
|
||||
|
||||
[!NOTE] If `autosave = false` then the `should_autosave` callback will not be
|
||||
executed.
|
||||
**Autoloading**
|
||||
|
||||
The plugin can be enabled to automatically load sessions when Neovim is
|
||||
@ -255,8 +259,8 @@ started. Whilst off by default, this can be turned on by:
|
||||
})
|
||||
<
|
||||
|
||||
You can also provide a function to run when `autoload = true` but there is no
|
||||
session to be loaded:
|
||||
You can also provide a function to run when `autoload = true` and there is no
|
||||
session to load:
|
||||
|
||||
>lua
|
||||
require("persisted").setup({
|
||||
@ -267,37 +271,16 @@ session to be loaded:
|
||||
})
|
||||
<
|
||||
|
||||
Autoloading can be further controlled for certain directories by specifying
|
||||
`allowed_dirs` and `ignored_dirs`.
|
||||
Autoloading can be further controlled for directories in the `allowed_dirs` and
|
||||
`ignored_dirs` config tables.
|
||||
|
||||
|
||||
[!NOTE] Autoloading will not occur if the plugin is lazy loaded or a user opens
|
||||
Neovim with arguments other than a single directory argument. For example:
|
||||
`nvim some_file.rb` will not result in autoloading but `nvim
|
||||
some/existing/path` or `nvim .` will.
|
||||
**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`.
|
||||
[!IMPORTANT] By design, the plugin will not autoload a session when any
|
||||
arguments are passed to Neovim such as `nvim my_file.py`
|
||||
**Allowed directories**
|
||||
|
||||
You may specify a table of directories for which the plugin will autosave
|
||||
and/or autoload from. For example:
|
||||
You may specify a table of directories for which the plugin will start and/or
|
||||
autoload from. For example:
|
||||
|
||||
>lua
|
||||
require("persisted").setup({
|
||||
@ -308,17 +291,17 @@ and/or autoload from. For example:
|
||||
})
|
||||
<
|
||||
|
||||
Specifying `~/Code` will autosave and autoload from that directory as well as
|
||||
all its sub-directories.
|
||||
Specifying `~/Code` will start and autoload from that directory as well as all
|
||||
its sub-directories.
|
||||
|
||||
|
||||
[!NOTE] If `allowed_dirs` is left at its default value and `autosave` and/or
|
||||
`autoload` are set to `true`, then the plugin will autoload/autosave from _any_
|
||||
directory
|
||||
[!NOTE] If `allowed_dirs` is left at its default value and `autostart` and/or
|
||||
`autoload` are set to `true`, then the plugin will start and autoload from
|
||||
_any_ directory
|
||||
**Ignored directories**
|
||||
|
||||
You may specify a table of directories for which the plugin will **never**
|
||||
autosave and autoload from. For example:
|
||||
start and autoload from. For example:
|
||||
|
||||
>lua
|
||||
require("persisted").setup({
|
||||
@ -351,25 +334,10 @@ In this setup, `~/.config` and `~/.local/nvim` are still going to behave in
|
||||
their default setting (ignoring all listed directory and its children), however
|
||||
`/` and `/tmp` will only ignore those directories exactly.
|
||||
|
||||
**Ignored branches**
|
||||
|
||||
You may specify a table of patterns that match against branches for which the
|
||||
plugin will **never** autosave and autoload from:
|
||||
|
||||
>lua
|
||||
require("persisted").setup({
|
||||
ignored_branches = {
|
||||
"^master",
|
||||
"feature/%u"
|
||||
},
|
||||
})
|
||||
<
|
||||
|
||||
**Events / Callbacks**
|
||||
|
||||
The plugin fires events at various points during its lifecycle:
|
||||
|
||||
|
||||
- `PersistedLoadPre` - For _before_ a session is loaded
|
||||
- `PersistedLoadPost` - For _after_ a session is loaded
|
||||
- `PersistedTelescopeLoadPre` - For _before_ a session is loaded via Telescope
|
||||
@ -378,8 +346,9 @@ The plugin fires events at various points during its lifecycle:
|
||||
- `PersistedSavePost` - For _after_ a session is saved
|
||||
- `PersistedDeletePre` - For _before_ a session is deleted
|
||||
- `PersistedDeletePost` - For _after_ a session is deleted
|
||||
- `PersistedStateChange` - For when a session is _started_ or _stopped_
|
||||
- `PersistedToggled` - For when a session is toggled
|
||||
- `PersistedStart` - For when a session has _started_
|
||||
- `PersistedStop` - For when a session has _stopped_
|
||||
- `PersistedToggle` - For when a session is toggled
|
||||
|
||||
These events can be consumed anywhere within your configuration by utilising
|
||||
the `vim.api.nvim_create_autocmd` function.
|
||||
@ -388,13 +357,10 @@ A commonly requested example is to use the Telescope extension to load a
|
||||
session, saving the current session before clearing all of the open buffers:
|
||||
|
||||
>lua
|
||||
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
|
||||
|
||||
vim.api.nvim_create_autocmd({ "User" }, {
|
||||
vim.api.nvim_create_autocmd("User", {
|
||||
pattern = "PersistedTelescopeLoadPre",
|
||||
group = group,
|
||||
callback = function(session)
|
||||
-- Save the currently loaded session using a global variable
|
||||
-- Save the currently loaded session using the global variable
|
||||
require("persisted").save({ session = vim.g.persisted_loaded_session })
|
||||
|
||||
-- Delete all of the open buffers
|
||||
@ -403,39 +369,120 @@ session, saving the current session before clearing all of the open buffers:
|
||||
})
|
||||
<
|
||||
|
||||
**Using callback data**
|
||||
**Highlights**
|
||||
|
||||
When certain events are fired, session data is made available for the user to
|
||||
consume, for example:
|
||||
The plugin also comes with pre-defined highlight groups for the Telescope
|
||||
implementation:
|
||||
|
||||
- `PersistedTelescopeSelected`
|
||||
- `PersistedTelescopeDir`
|
||||
- `PersistedTelescopeBranch`
|
||||
|
||||
|
||||
EXTENDING THE PLUGIN *persisted.nvim-extending-the-plugin*
|
||||
|
||||
The plugin has been designed to be fully extensible. All of the functions in
|
||||
the init.lua
|
||||
<https://github.com/olimorris/persisted.nvim/blob/main/lua/persisted/init.lua>
|
||||
and utils.lua
|
||||
<https://github.com/olimorris/persisted.nvim/blob/main/lua/persisted/utils.lua>
|
||||
file are public.
|
||||
|
||||
**Custom Autoloading** by neandrake <https://github.com/neandrake>
|
||||
|
||||
Autoloading a session if arguments are passed to Neovim:
|
||||
|
||||
>lua
|
||||
{
|
||||
branch = "main",
|
||||
dir_path = "Code/Neovim/persisted.nvim",
|
||||
file_path = "/Users/Oli/.local/share/nvim/sessions/%Users%Oli%Code%Neovim%persisted.nvim@@main.vim",
|
||||
name = "Code/Neovim/persisted.nvim@@main",
|
||||
"olimorris/persisted.nvim",
|
||||
lazy = false,
|
||||
opts = {
|
||||
autoload = true,
|
||||
},
|
||||
}
|
||||
<
|
||||
|
||||
To consume this data, use the `session.data` table in your autocmd:
|
||||
|
||||
>lua
|
||||
vim.api.nvim_create_autocmd({ "User" }, {
|
||||
pattern = "PersistedLoadPost",
|
||||
group = group,
|
||||
callback = function(session)
|
||||
print(session.data.branch)
|
||||
|
||||
-- Somewhere in your config
|
||||
local persisted = require("persisted")
|
||||
vim.api.nvim_create_autocmd("VimEnter", {
|
||||
nested = true,
|
||||
callback = function()
|
||||
if vim.g.started_with_stdin then
|
||||
return
|
||||
end
|
||||
|
||||
local forceload = false
|
||||
if vim.fn.argc() == 0 then
|
||||
forceload = true
|
||||
elseif vim.fn.argc() == 1 then
|
||||
local dir = vim.fn.expand(vim.fn.argv(0))
|
||||
if dir == '.' then
|
||||
dir = vim.fn.getcwd()
|
||||
end
|
||||
|
||||
if vim.fn.isdirectory(dir) ~= 0 then
|
||||
forceload = true
|
||||
end
|
||||
end
|
||||
|
||||
persisted.autoload({ force = forceload })
|
||||
end,
|
||||
})
|
||||
<
|
||||
|
||||
**Git Branching from directories that are not the CWD** by mrloop
|
||||
<https://github.com/mrloop>
|
||||
|
||||
[!NOTE] This data is available for the `PersistedLoad`, `PersistedDelete` and
|
||||
`PersistedTelescope` events
|
||||
As per #149 <https://github.com/olimorris/persisted.nvim/discussions/149>, if
|
||||
you invoke Neovim from a sub-directory then the git branch will not be
|
||||
detected. The code below amends for this:
|
||||
|
||||
LICENSE *persisted.nvim-license*
|
||||
>lua
|
||||
{
|
||||
"olimorris/persisted.nvim",
|
||||
lazy = false,
|
||||
opts = {
|
||||
autoload = true,
|
||||
autosave = true,
|
||||
use_git_branch = true,
|
||||
},
|
||||
config = function(_, opts)
|
||||
local persisted = require("persisted")
|
||||
persisted.branch = function()
|
||||
local branch = vim.fn.systemlist("git branch --show-current")[1]
|
||||
return vim.v.shell_error == 0 and branch or nil
|
||||
end
|
||||
persisted.setup(opts)
|
||||
end,
|
||||
}
|
||||
<
|
||||
|
||||
MIT <https://github.com/olimorris/persisted.nvim/blob/main/LICENSE>
|
||||
**Ignore Branches**
|
||||
|
||||
If you’d like to ignore certain branches from being saved as a session:
|
||||
|
||||
>lua
|
||||
{
|
||||
"olimorris/persisted.nvim",
|
||||
lazy = false,
|
||||
opts = {
|
||||
autostart = true,
|
||||
use_git_branch = true,
|
||||
},
|
||||
config = function(_, opts)
|
||||
local persisted = require("persisted")
|
||||
local utils = require("persisted.utils")
|
||||
local ignored_branches = {
|
||||
"feature_branch"
|
||||
}
|
||||
|
||||
persisted.setup(opts)
|
||||
if not utils.in_table(persisted.branch(), ignored_branches) then
|
||||
persisted.load()
|
||||
persisted.start()
|
||||
end
|
||||
end
|
||||
}
|
||||
<
|
||||
|
||||
Generated by panvimdoc <https://github.com/kdheepak/panvimdoc>
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
persisted.nvim-configuration persisted.nvim.txt /*persisted.nvim-configuration*
|
||||
persisted.nvim-extending-the-plugin persisted.nvim.txt /*persisted.nvim-extending-the-plugin*
|
||||
persisted.nvim-features persisted.nvim.txt /*persisted.nvim-features*
|
||||
persisted.nvim-installation persisted.nvim.txt /*persisted.nvim-installation*
|
||||
persisted.nvim-license persisted.nvim.txt /*persisted.nvim-license*
|
||||
persisted.nvim-requirements persisted.nvim.txt /*persisted.nvim-requirements*
|
||||
persisted.nvim-table-of-contents persisted.nvim.txt /*persisted.nvim-table-of-contents*
|
||||
persisted.nvim-usage persisted.nvim.txt /*persisted.nvim-usage*
|
||||
|
||||
@ -1,46 +1,35 @@
|
||||
local M = {}
|
||||
return {
|
||||
autostart = true, -- Automatically start the plugin on load?
|
||||
|
||||
local defaults = {
|
||||
log_level = "ERROR", -- One of "TRACE", "DEBUG", "ERROR"
|
||||
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
|
||||
silent = false, -- silent nvim message when sourcing session file
|
||||
-- Function to determine if a session should be saved
|
||||
---@type fun(): boolean
|
||||
should_save = function()
|
||||
return true
|
||||
end,
|
||||
|
||||
use_git_branch = false, -- create session files based on the branch of a git enabled repository
|
||||
branch_separator = "@@", -- string used to separate session directory name from branch name
|
||||
default_branch = "main", -- the branch to load if a session file is not found for the current branch
|
||||
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- Directory where session files are saved
|
||||
|
||||
autosave = true, -- automatically save session files when exiting Neovim
|
||||
should_autosave = nil, -- function to determine if a session should be autosaved (resolve to a boolean)
|
||||
follow_cwd = true, -- Change the session file to match any change in the cwd?
|
||||
use_git_branch = false, -- Include the git branch in the session file name?
|
||||
autoload = false, -- Automatically load the session for the cwd on Neovim startup?
|
||||
|
||||
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
|
||||
-- Function to run when `autoload = true` but there is no session to load
|
||||
---@type fun(): any
|
||||
on_autoload_no_session = function() end,
|
||||
|
||||
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
|
||||
ignored_branches = nil, -- table of branch patterns that are ignored for auto-saving and auto-loading
|
||||
allowed_dirs = {}, -- Table of dirs that the plugin will start and autoload from
|
||||
ignored_dirs = {}, -- Table of dirs that are ignored for starting and autoloading
|
||||
|
||||
telescope = {
|
||||
reset_prompt = true, -- Reset prompt after a telescope action?
|
||||
--TODO: We should add a deprecation notice for the old API here
|
||||
mappings = {
|
||||
change_branch = "<c-b>",
|
||||
copy_session = "<c-c>",
|
||||
delete_session = "<c-d>",
|
||||
mappings = { -- Mappings for managing sessions in Telescope
|
||||
copy_session = "<C-c>",
|
||||
change_branch = "<C-b>",
|
||||
delete_session = "<C-d>",
|
||||
},
|
||||
icons = { -- icons displayed in the picker
|
||||
icons = { -- icons displayed in the Telescope picker
|
||||
selected = " ",
|
||||
dir = " ",
|
||||
branch = " ",
|
||||
dir = " ",
|
||||
selected = " ",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
M.options = {}
|
||||
|
||||
function M.setup(opts)
|
||||
M.options = vim.tbl_deep_extend("force", defaults, opts or {})
|
||||
vim.fn.mkdir(M.options.save_dir, "p")
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@ -1,38 +0,0 @@
|
||||
---[[
|
||||
--Courtesy of the awesome work in Nightfox.nvim
|
||||
--https://github.com/EdenEast/nightfox.nvim/blob/main/lua/nightfox/lib/deprecation.lua
|
||||
--]
|
||||
local M = {
|
||||
_list = { { "[Persisted.nvim]\n", "Question" }, { "The following have been " }, { "deprecated:\n", "WarningMsg" } },
|
||||
_has_registered = false,
|
||||
}
|
||||
|
||||
function M.write(...)
|
||||
for _, e in ipairs({ ... }) do
|
||||
table.insert(M._list, type(e) == "string" and { e } or e)
|
||||
end
|
||||
|
||||
M._list[#M._list][1] = M._list[#M._list][1] .. "\n"
|
||||
|
||||
if not M._has_registered then
|
||||
vim.cmd([[
|
||||
augroup PersistedDeprecations
|
||||
au!
|
||||
autocmd VimEnter * ++once lua require("persisted.deprecate").flush()
|
||||
augroup END
|
||||
]])
|
||||
M._has_registered = true
|
||||
end
|
||||
end
|
||||
|
||||
function M.flush()
|
||||
M.write(
|
||||
"----------\n",
|
||||
"See ",
|
||||
{ "https://github.com/olimorris/persisted.nvim/issues/51", "Title" },
|
||||
" for more information."
|
||||
)
|
||||
vim.api.nvim_echo(M._list, true, {})
|
||||
end
|
||||
|
||||
return M
|
||||
@ -1,413 +1,245 @@
|
||||
local config = require("persisted.config")
|
||||
local log = require("persisted.log")
|
||||
local utils = require("persisted.utils")
|
||||
|
||||
local M = {}
|
||||
|
||||
local config
|
||||
local e = vim.fn.fnameescape
|
||||
local uv = vim.uv or vim.loop
|
||||
|
||||
---Escapes special characters before performing string substitution
|
||||
---@param str string
|
||||
---@param pattern string
|
||||
---@param replace string
|
||||
---@param n? integer
|
||||
---@return string
|
||||
---@return integer
|
||||
local function escape_pattern(str, pattern, replace, n)
|
||||
pattern = string.gsub(pattern, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") -- escape pattern
|
||||
replace = string.gsub(replace, "[%%]", "%%%%") -- escape replacement
|
||||
|
||||
return string.gsub(str, pattern, replace, n)
|
||||
end
|
||||
|
||||
---Gets the directory from the file/path argument passed to Neovim if there's
|
||||
---exactly one and it resolves to a valid directory
|
||||
---@return string|nil
|
||||
local function args_path()
|
||||
if vim.fn.argc() ~= 1 then
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Use expand() to resolve '~' and use fs_realpath to resolve both '.' and
|
||||
-- relative paths passed as arguments. Note that argv() will only ever return
|
||||
-- paths/files passed as arguments and does not include other
|
||||
-- parameters/arguments. fs_realpath() returns nil if the path doesn't exist.
|
||||
-- Use isdirectory to validate it's a directory and not a file.
|
||||
local dir = vim.loop.fs_realpath(vim.fn.expand(vim.fn.argv(0)))
|
||||
if dir ~= nil and vim.fn.isdirectory(dir) ~= 0 then
|
||||
return dir
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
---Check any arguments passed to Neovim and verify if they're a directory
|
||||
---@return boolean
|
||||
local function args_check()
|
||||
-- Args are valid if a single directory was resolved or if no args were used.
|
||||
return args_path() ~= nil or vim.fn.argc() == 0
|
||||
end
|
||||
|
||||
---Get the directory to be used for the session
|
||||
---@return string
|
||||
local function session_dir()
|
||||
-- Use specified directory from arguments or the working directory otherwise.
|
||||
return args_path() or vim.fn.getcwd()
|
||||
end
|
||||
|
||||
---Does the current working directory allow for the auto-saving and loading?
|
||||
---@param dir string Directory to be used for the session
|
||||
---@return boolean
|
||||
local function allow_dir(dir)
|
||||
local allowed_dirs = config.options.allowed_dirs
|
||||
|
||||
if allowed_dirs == nil then
|
||||
return true
|
||||
end
|
||||
|
||||
return utils.dirs_match(dir, allowed_dirs)
|
||||
end
|
||||
|
||||
---Is the current working directory ignored for auto-saving and loading?
|
||||
---@param dir string Directory to be used for the session
|
||||
---@return boolean
|
||||
local function ignore_dir(dir)
|
||||
local ignored_dirs = config.options.ignored_dirs
|
||||
|
||||
if ignored_dirs == nil then
|
||||
return false
|
||||
end
|
||||
|
||||
return utils.dirs_match(dir, ignored_dirs)
|
||||
end
|
||||
|
||||
---Is the current branch ignored for auto-saving and loading?
|
||||
---@param branch? string Branch to be used for the session
|
||||
---@return boolean
|
||||
local function ignore_branch(branch)
|
||||
local ignored_branches = config.options.ignored_branches
|
||||
|
||||
if not branch or ignored_branches == nil then
|
||||
return false
|
||||
end
|
||||
|
||||
return utils.table_match(branch, ignored_branches) ~= nil
|
||||
end
|
||||
|
||||
---Get the session that was saved last
|
||||
---@return string
|
||||
local function get_last()
|
||||
local sessions = vim.fn.glob(config.options.save_dir .. "*.vim", true, true)
|
||||
|
||||
table.sort(sessions, function(a, b)
|
||||
return vim.loop.fs_stat(a).mtime.sec > vim.loop.fs_stat(b).mtime.sec
|
||||
end)
|
||||
|
||||
return sessions[1]
|
||||
end
|
||||
|
||||
---Get the current Git branch name, untouched
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return string|nil
|
||||
local function get_branchname(dir)
|
||||
dir = dir or session_dir()
|
||||
vim.fn.system('git -C "' .. dir .. '" rev-parse 2>/dev/null')
|
||||
|
||||
local git_enabled = (vim.v.shell_error == 0)
|
||||
|
||||
if git_enabled then
|
||||
local git_branch = vim.fn.systemlist('git -C "' .. dir .. '" rev-parse --abbrev-ref HEAD 2>/dev/null')
|
||||
return git_branch[1]
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
---Get the current Git branch
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return string|nil
|
||||
function M.get_branch(dir)
|
||||
dir = dir or session_dir()
|
||||
|
||||
if config.options.use_git_branch then
|
||||
vim.fn.system('git -C "' .. dir .. '" rev-parse 2>/dev/null')
|
||||
|
||||
local git_enabled = (vim.v.shell_error == 0)
|
||||
|
||||
if git_enabled then
|
||||
local git_branch = vim.fn.systemlist('git -C "' .. dir .. '" rev-parse --abbrev-ref HEAD 2>/dev/null')
|
||||
|
||||
if vim.v.shell_error == 0 then
|
||||
local branch = config.options.branch_separator .. git_branch[1]:gsub("/", "%%")
|
||||
local branch_session = config.options.save_dir .. dir:gsub(utils.get_dir_pattern(), "%%") .. branch .. ".vim"
|
||||
|
||||
-- Try to load the session for the current branch
|
||||
if vim.fn.filereadable(branch_session) ~= 0 then
|
||||
return branch
|
||||
else
|
||||
log:debug("Trying to load a session for branch: %s", config.options.default_branch)
|
||||
log:error("Could not load a session for branch: %s", git_branch[1])
|
||||
vim.g.persisted_branch_session = branch_session
|
||||
return config.options.branch_separator .. config.options.default_branch
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
---Fire an event
|
||||
---@param event string
|
||||
function M.fire(event)
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "Persisted" .. event })
|
||||
end
|
||||
|
||||
---Get the current session for the current working directory and git branch
|
||||
---@param dir string Directory to be used for the session
|
||||
---@param opts? {branch?: boolean}
|
||||
---@return string
|
||||
local function get_current(dir)
|
||||
local name = dir:gsub(utils.get_dir_pattern(), "%%")
|
||||
local branch = M.get_branch(dir)
|
||||
function M.current(opts)
|
||||
opts = opts or {}
|
||||
local name = utils.make_fs_safe(vim.fn.getcwd())
|
||||
|
||||
return config.options.save_dir .. name .. (branch or "") .. ".vim"
|
||||
if config.use_git_branch and opts.branch ~= false then
|
||||
local branch = M.branch()
|
||||
if branch then
|
||||
branch = utils.make_fs_safe(branch)
|
||||
name = name .. "@@" .. branch
|
||||
end
|
||||
end
|
||||
|
||||
return config.save_dir .. name .. ".vim"
|
||||
end
|
||||
|
||||
---Determine if a session for the current wording directory, exists
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return boolean
|
||||
function M.session_exists(dir)
|
||||
dir = dir or session_dir()
|
||||
---Automatically load the session for the current dir
|
||||
---@param opts? { force?: boolean }
|
||||
function M.autoload(opts)
|
||||
opts = opts or {}
|
||||
|
||||
return vim.fn.filereadable(get_current(dir)) ~= 0
|
||||
end
|
||||
if not opts.force and (vim.fn.argc() > 0 or vim.g.started_with_stdin) then
|
||||
return
|
||||
end
|
||||
|
||||
---Setup the plugin
|
||||
---@param opts? table
|
||||
---@return nil
|
||||
function M.setup(opts)
|
||||
config.setup(opts)
|
||||
|
||||
log.set_root(log.new({
|
||||
handlers = {
|
||||
{
|
||||
type = "echo",
|
||||
level = vim.log.levels.WARN,
|
||||
},
|
||||
{
|
||||
type = "file",
|
||||
filename = "persisted.log",
|
||||
level = vim.log.levels[config.options.log_level],
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
local dir = session_dir()
|
||||
local branch = get_branchname()
|
||||
|
||||
if
|
||||
config.options.autosave
|
||||
and (allow_dir(dir) and not ignore_dir(dir) and vim.g.persisting == nil)
|
||||
and not ignore_branch(branch)
|
||||
and args_check()
|
||||
then
|
||||
log:trace("Starting session")
|
||||
M.start()
|
||||
if config.autoload and M.allowed_dir() then
|
||||
M.load({ autoload = true })
|
||||
end
|
||||
end
|
||||
|
||||
---Load a session
|
||||
---@param opt? table
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return nil
|
||||
function M.load(opt, dir)
|
||||
opt = opt or {}
|
||||
dir = dir or session_dir()
|
||||
---@param opts? { last?: boolean, autoload?: boolean, session?: string }
|
||||
function M.load(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local branch
|
||||
local session
|
||||
if opt.session then
|
||||
session = opt.session
|
||||
local session_data = utils.make_session_data(session)
|
||||
branch = session_data and session_data.branch or ""
|
||||
log:trace("Session branch %s", branch)
|
||||
log:trace("Session data %s", session_data)
|
||||
if not branch then
|
||||
vim.notify(string.format("[Persisted.nvim]: Invalid session file %s", session), vim.log.levels.WARN)
|
||||
end
|
||||
|
||||
if opts.last then
|
||||
session = M.last()
|
||||
elseif opts.session then
|
||||
session = opts.session
|
||||
else
|
||||
branch = get_branchname()
|
||||
session = opt.last and get_last() or get_current(dir)
|
||||
log:trace("Session branch: %s", branch)
|
||||
end
|
||||
|
||||
if session then
|
||||
if vim.fn.filereadable(session) ~= 0 then
|
||||
vim.g.persisting_session = not config.options.follow_cwd and session or nil
|
||||
log:trace("Session load: %s", session)
|
||||
utils.load_session(session, config.options.silent)
|
||||
elseif type(config.options.on_autoload_no_session) == "function" then
|
||||
log:trace("No session to load")
|
||||
config.options.on_autoload_no_session()
|
||||
session = M.current()
|
||||
if vim.fn.filereadable(session) == 0 then
|
||||
session = M.current({ branch = false })
|
||||
end
|
||||
end
|
||||
|
||||
dir = session_dir()
|
||||
if config.options.autosave and (allow_dir(dir) and not ignore_dir(dir)) and not ignore_branch(branch) then
|
||||
if session and vim.fn.filereadable(session) ~= 0 then
|
||||
vim.g.persisting_session = not config.follow_cwd and session or nil
|
||||
vim.g.persisted_loaded_session = session
|
||||
M.fire("LoadPre")
|
||||
vim.cmd("silent! source " .. e(session))
|
||||
M.fire("LoadPost")
|
||||
elseif opts.autoload and type(config.on_autoload_no_session) == "function" then
|
||||
config.on_autoload_no_session()
|
||||
end
|
||||
|
||||
if config.autostart and M.allowed_dir() then
|
||||
M.start()
|
||||
end
|
||||
end
|
||||
|
||||
---Automatically load the session for the current dir
|
||||
---@return nil
|
||||
function M.autoload()
|
||||
local dir = session_dir()
|
||||
local branch = get_branchname()
|
||||
|
||||
if config.options.autoload and args_check() then
|
||||
if allow_dir(dir) and not ignore_dir(dir) and not ignore_branch(branch) then
|
||||
log:trace("Autoloading from %s", dir)
|
||||
M.load({}, dir)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Start recording a session
|
||||
---@return nil
|
||||
---Start a session
|
||||
function M.start()
|
||||
vim.api.nvim_create_autocmd("VimLeavePre", {
|
||||
group = vim.api.nvim_create_augroup("Persisted", { clear = true }),
|
||||
callback = function()
|
||||
M.save()
|
||||
end,
|
||||
})
|
||||
|
||||
vim.g.persisting = true
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedStateChange", data = { action = "start" } })
|
||||
M.fire("Start")
|
||||
end
|
||||
|
||||
---Stop recording a session
|
||||
---@return nil
|
||||
---Stop a session
|
||||
function M.stop()
|
||||
vim.g.persisting = false
|
||||
vim.g.persisting_session = nil
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedStateChange", data = { action = "stop" } })
|
||||
end
|
||||
|
||||
---Write the session to disk
|
||||
---@param session string
|
||||
---@return nil
|
||||
local function write(session)
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePre" })
|
||||
vim.cmd("mks! " .. e(session))
|
||||
vim.g.persisting = true
|
||||
log:trace("Session saved")
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePost" })
|
||||
pcall(vim.api.nvim_del_augroup_by_name, "Persisted")
|
||||
M.fire("Stop")
|
||||
end
|
||||
|
||||
---Save the session
|
||||
---@param opt? table
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return nil
|
||||
function M.save(opt, dir)
|
||||
opt = opt or {}
|
||||
dir = dir or session_dir()
|
||||
---@param opts? { force?: boolean, session?: string }
|
||||
function M.save(opts)
|
||||
opts = opts or {}
|
||||
|
||||
if not opt.session then
|
||||
-- Do not save the session if the user has manually stopped it...unless it's forced
|
||||
if (vim.g.persisting == false or vim.g.persisting == nil) and not opt.force then
|
||||
return
|
||||
end
|
||||
|
||||
-- Do not save the session if autosave is turned off...unless it's forced
|
||||
if not config.options.autosave and not opt.force then
|
||||
return
|
||||
end
|
||||
|
||||
-- Do not save the session if the callback returns false...unless it's forced
|
||||
if
|
||||
not opt.force
|
||||
and type(config.options.should_autosave) == "function"
|
||||
and not config.options.should_autosave()
|
||||
then
|
||||
return
|
||||
end
|
||||
-- Do not save the session if should_save evals to false...unless it's forced
|
||||
if type(config.should_save) == "function" and not config.should_save() and not opts.force then
|
||||
return
|
||||
end
|
||||
|
||||
local session = opt.session or (vim.g.persisted_branch_session or vim.g.persisting_session or get_current(dir))
|
||||
write(session)
|
||||
M.fire("SavePre")
|
||||
vim.cmd("mks! " .. e(opts.session or vim.g.persisting_session or M.current()))
|
||||
vim.cmd("sleep 10m")
|
||||
M.fire("SavePost")
|
||||
end
|
||||
|
||||
---Delete the current session
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return nil
|
||||
function M.delete(dir)
|
||||
dir = dir or session_dir()
|
||||
local session = get_current(dir)
|
||||
|
||||
if session and vim.loop.fs_stat(session) ~= 0 then
|
||||
local session_data = utils.make_session_data(session)
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedDeletePre", data = session_data })
|
||||
---@param opts? { session?: string }
|
||||
function M.delete(opts)
|
||||
opts = opts or {}
|
||||
local session = opts.session or M.current()
|
||||
|
||||
if session and uv.fs_stat(session) ~= 0 then
|
||||
M.fire("DeletePre")
|
||||
vim.schedule(function()
|
||||
log:trace("Deleting session %s", session)
|
||||
M.stop()
|
||||
vim.fn.system("rm " .. e(session))
|
||||
vim.fn.delete(vim.fn.expand(session))
|
||||
end)
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedDeletePost", data = session_data })
|
||||
M.fire("DeletePost")
|
||||
end
|
||||
end
|
||||
|
||||
---Get the current Git branch
|
||||
---@return string?
|
||||
function M.branch()
|
||||
if uv.fs_stat(".git") then
|
||||
local branch = vim.fn.systemlist("git branch --show-current")[1]
|
||||
return vim.v.shell_error == 0 and branch or nil
|
||||
end
|
||||
end
|
||||
|
||||
---Select a session to load
|
||||
function M.select()
|
||||
---@type { session: string, dir: string, branch?: string }[]
|
||||
local items = {}
|
||||
local found = {} ---@type table<string, boolean>
|
||||
for _, session in ipairs(M.list()) do
|
||||
if uv.fs_stat(session) then
|
||||
local file = session:sub(#M.config.save_dir + 1, -5)
|
||||
local dir, branch = unpack(vim.split(file, "@@", { plain = true }))
|
||||
dir = dir:gsub("%%", "/")
|
||||
if jit.os:find("Windows") then
|
||||
dir = dir:gsub("^(%w)/", "%1:/")
|
||||
end
|
||||
if not found[dir .. (branch or "")] then
|
||||
found[dir .. (branch or "")] = true
|
||||
items[#items + 1] = { session = session, dir = dir, branch = branch }
|
||||
end
|
||||
end
|
||||
end
|
||||
vim.ui.select(items, {
|
||||
prompt = "Select a session: ",
|
||||
format_item = function(item)
|
||||
local name = vim.fn.fnamemodify(item.dir, ":p:~")
|
||||
if item.branch then
|
||||
name = name .. " (" .. item.branch .. ")"
|
||||
end
|
||||
return name
|
||||
end,
|
||||
}, function(item)
|
||||
if item then
|
||||
vim.fn.chdir(item.dir)
|
||||
M.load()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---Determines whether to load, start or stop a session
|
||||
---@param dir? string The directory whose associated session saving should be toggled. If not set, the current working directory is used.
|
||||
---@return nil
|
||||
function M.toggle(dir)
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedToggled" })
|
||||
|
||||
dir = dir or session_dir()
|
||||
|
||||
function M.toggle()
|
||||
M.fire("Toggle")
|
||||
if vim.g.persisting == nil then
|
||||
log:trace("Toggling load")
|
||||
return M.load({}, dir)
|
||||
return M.load()
|
||||
end
|
||||
|
||||
if vim.g.persisting then
|
||||
log:trace("Toggling stop")
|
||||
return M.stop()
|
||||
end
|
||||
|
||||
log:trace("Toggling start")
|
||||
return M.start()
|
||||
end
|
||||
|
||||
---List all of the sessions
|
||||
---@return table
|
||||
---Allow autosaving and autoloading for the given dir?
|
||||
---@param opts? {dir?: string}
|
||||
---@return boolean
|
||||
function M.allowed_dir(opts)
|
||||
opts = opts or {}
|
||||
local dir = opts.dir or vim.fn.getcwd()
|
||||
|
||||
return (next(config.allowed_dirs) and utils.dirs_match(dir, config.allowed_dirs) or true)
|
||||
and not (next(config.ignored_dirs) and utils.dirs_match(dir, config.ignored_dirs) or false)
|
||||
end
|
||||
|
||||
---Get an ordered list of sessions, sorted by modified time
|
||||
---@return string[]
|
||||
function M.list()
|
||||
local save_dir = config.options.save_dir
|
||||
local session_files = vim.fn.glob(save_dir .. "*.vim", true, true)
|
||||
local branch_separator = config.options.branch_separator
|
||||
local dir_separator = utils.get_dir_pattern()
|
||||
local sessions = vim.fn.glob(config.save_dir .. "*.vim", true, true)
|
||||
|
||||
local sessions = {}
|
||||
for _, session in pairs(session_files) do
|
||||
local session_name = escape_pattern(session, save_dir, "")
|
||||
:gsub("%%", dir_separator)
|
||||
:gsub(vim.fn.expand("~"), dir_separator)
|
||||
:gsub("//", "")
|
||||
:sub(1, -5)
|
||||
|
||||
if vim.fn.has("win32") == 1 then
|
||||
-- format drive letter (no trailing separator)
|
||||
session_name = escape_pattern(session_name, dir_separator, ":", 1)
|
||||
-- format remaining filepath separator(s)
|
||||
session_name = escape_pattern(session_name, dir_separator, "\\")
|
||||
end
|
||||
|
||||
local branch, dir_path
|
||||
|
||||
if string.find(session_name, branch_separator, 1, true) then
|
||||
local splits = vim.split(session_name, branch_separator, { plain = true })
|
||||
branch = table.remove(splits, #splits)
|
||||
dir_path = vim.fn.join(splits, branch_separator)
|
||||
else
|
||||
dir_path = session_name
|
||||
end
|
||||
|
||||
table.insert(sessions, {
|
||||
["name"] = session_name,
|
||||
["file_path"] = session,
|
||||
["branch"] = branch,
|
||||
["dir_path"] = dir_path,
|
||||
})
|
||||
end
|
||||
table.sort(sessions, function(a, b)
|
||||
return uv.fs_stat(a).mtime.sec > uv.fs_stat(b).mtime.sec
|
||||
end)
|
||||
|
||||
return sessions
|
||||
end
|
||||
|
||||
---Get the last session that was saved
|
||||
---@return string
|
||||
function M.last()
|
||||
return M.list()[1]
|
||||
end
|
||||
|
||||
---Setup the plugin
|
||||
---@param opts? table
|
||||
function M.setup(opts)
|
||||
-- Account for old config options
|
||||
if opts and opts.autosave then
|
||||
opts.autostart = opts.autosave
|
||||
end
|
||||
if opts and opts.should_autosave then
|
||||
opts.should_save = opts.should_autosave
|
||||
end
|
||||
if opts and opts.allowed_dirs == nil then
|
||||
opts.allowed_dirs = {}
|
||||
end
|
||||
if opts and opts.ignored_dirs == nil then
|
||||
opts.ignored_dirs = {}
|
||||
end
|
||||
|
||||
config = vim.tbl_deep_extend("force", require("persisted.config"), opts or {})
|
||||
M.config = config
|
||||
|
||||
vim.fn.mkdir(config.save_dir, "p")
|
||||
|
||||
if config.autostart and M.allowed_dir() and vim.g.persisting == nil then
|
||||
M.start()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@ -1,280 +0,0 @@
|
||||
---@type boolean
|
||||
local is_windows = vim.loop.os_uname().version:match("Windows")
|
||||
|
||||
---@type string
|
||||
local sep = is_windows and "\\" or "/"
|
||||
|
||||
---@return string
|
||||
local join = function(...)
|
||||
local joined = table.concat({ ... }, sep)
|
||||
if is_windows then
|
||||
joined = joined:gsub("\\\\+", "\\")
|
||||
else
|
||||
joined = joined:gsub("//+", "/")
|
||||
end
|
||||
return joined
|
||||
end
|
||||
|
||||
---@class LogHandler
|
||||
---@field type string
|
||||
---@field level integer
|
||||
---@field formatter? fun(level: integer, msg: string, ...: any)
|
||||
---@field handle? fun(level: integer, text: string)
|
||||
local LogHandler = {}
|
||||
|
||||
local levels_reverse = {}
|
||||
for k, v in pairs(vim.log.levels) do
|
||||
levels_reverse[v] = k
|
||||
end
|
||||
|
||||
function LogHandler.new(opts)
|
||||
vim.validate({
|
||||
type = { opts.type, "s" },
|
||||
handle = { opts.handle, "f" },
|
||||
formatter = { opts.formatter, "f" },
|
||||
level = { opts.level, "n", true },
|
||||
})
|
||||
return setmetatable({
|
||||
type = opts.type,
|
||||
handle = opts.handle,
|
||||
formatter = opts.formatter,
|
||||
level = opts.level or vim.log.levels.INFO,
|
||||
}, { __index = LogHandler })
|
||||
end
|
||||
|
||||
function LogHandler:log(level, msg, ...)
|
||||
if self.level <= level then
|
||||
local text = self.formatter(level, msg, ...)
|
||||
self.handle(level, text)
|
||||
end
|
||||
end
|
||||
|
||||
local function default_formatter(level, msg, ...)
|
||||
local args = vim.F.pack_len(...)
|
||||
for i = 1, args.n do
|
||||
local v = args[i]
|
||||
if type(v) == "table" then
|
||||
args[i] = vim.inspect(v)
|
||||
elseif v == nil then
|
||||
args[i] = "nil"
|
||||
end
|
||||
end
|
||||
local ok, text = pcall(string.format, msg, vim.F.unpack_len(args))
|
||||
if ok then
|
||||
local str_level = levels_reverse[level]
|
||||
return string.format("[%s] %s\n%s", str_level, os.date("%Y-%m-%d %H:%M:%S"), text)
|
||||
else
|
||||
return string.format("[ERROR] error formatting log line: '%s' args %s", msg, vim.inspect(args))
|
||||
end
|
||||
end
|
||||
|
||||
---@param opts table
|
||||
---@return LogHandler
|
||||
local function create_file_handler(opts)
|
||||
vim.validate({
|
||||
filename = { opts.filename, "s" },
|
||||
})
|
||||
local ok, stdpath = pcall(vim.fn.stdpath, "log")
|
||||
if not ok then
|
||||
stdpath = vim.fn.stdpath("cache")
|
||||
end
|
||||
local filepath = join(stdpath, opts.filename)
|
||||
local logfile, openerr = io.open(filepath, "a+")
|
||||
if not logfile then
|
||||
local err_msg = string.format("Failed to open the CodeCompanion log file: %s", openerr)
|
||||
vim.notify(err_msg, vim.log.levels.ERROR)
|
||||
opts.handle = function() end
|
||||
else
|
||||
opts.handle = function(level, text)
|
||||
logfile:write(text)
|
||||
logfile:write("\n")
|
||||
logfile:flush()
|
||||
end
|
||||
end
|
||||
return LogHandler.new(opts)
|
||||
end
|
||||
|
||||
---@param opts table
|
||||
---@return LogHandler
|
||||
local function create_notify_handler(opts)
|
||||
opts.handle = function(level, text)
|
||||
vim.notify(text, level)
|
||||
end
|
||||
return LogHandler.new(opts)
|
||||
end
|
||||
|
||||
---@param opts table
|
||||
---@return LogHandler
|
||||
local function create_echo_handler(opts)
|
||||
opts.handle = function(level, text)
|
||||
local hl = "Normal"
|
||||
if level == vim.log.levels.ERROR then
|
||||
hl = "DiagnosticError"
|
||||
elseif level == vim.log.levels.WARN then
|
||||
hl = "DiagnosticWarn"
|
||||
end
|
||||
vim.api.nvim_echo({ { text, hl } }, true, {})
|
||||
end
|
||||
return LogHandler.new(opts)
|
||||
end
|
||||
|
||||
---@return LogHandler
|
||||
local function create_null_handler()
|
||||
return LogHandler.new({
|
||||
formatter = function() end,
|
||||
handle = function() end,
|
||||
})
|
||||
end
|
||||
|
||||
---@param opts table
|
||||
---@return LogHandler
|
||||
local function create_handler(opts)
|
||||
vim.validate({
|
||||
type = { opts.type, "s" },
|
||||
})
|
||||
if not opts.formatter then
|
||||
opts.formatter = default_formatter
|
||||
end
|
||||
if opts.type == "file" then
|
||||
return create_file_handler(opts)
|
||||
elseif opts.type == "notify" then
|
||||
return create_notify_handler(opts)
|
||||
elseif opts.type == "echo" then
|
||||
return create_echo_handler(opts)
|
||||
else
|
||||
vim.notify(string.format("Unknown log handler %s", opts.type), vim.log.levels.ERROR)
|
||||
return create_null_handler()
|
||||
end
|
||||
end
|
||||
|
||||
---@class Logger
|
||||
---@field handlers LogHandler[]
|
||||
local Logger = {}
|
||||
|
||||
---@class LoggerArgs
|
||||
---@field handlers LogHandler[]
|
||||
---@field level nil|integer
|
||||
|
||||
---@param opts LoggerArgs
|
||||
function Logger.new(opts)
|
||||
vim.validate({
|
||||
handlers = { opts.handlers, "t" },
|
||||
level = { opts.level, "n", true },
|
||||
})
|
||||
local handlers = {}
|
||||
for _, defn in ipairs(opts.handlers) do
|
||||
table.insert(handlers, create_handler(defn))
|
||||
end
|
||||
local log = setmetatable({
|
||||
handlers = handlers,
|
||||
}, { __index = Logger })
|
||||
if opts.level then
|
||||
log:set_level(opts.level)
|
||||
end
|
||||
return log
|
||||
end
|
||||
|
||||
---@param level integer
|
||||
function Logger:set_level(level)
|
||||
for _, handler in ipairs(self.handlers) do
|
||||
handler.level = level
|
||||
end
|
||||
end
|
||||
|
||||
---@return LogHandler[]
|
||||
function Logger:get_handlers()
|
||||
return self.handlers
|
||||
end
|
||||
|
||||
---@param level integer
|
||||
---@param msg string
|
||||
---@param ... any[]
|
||||
function Logger:log(level, msg, ...)
|
||||
for _, handler in ipairs(self.handlers) do
|
||||
handler:log(level, msg, ...)
|
||||
end
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:trace(msg, ...)
|
||||
self:log(vim.log.levels.TRACE, msg, ...)
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:debug(msg, ...)
|
||||
self:log(vim.log.levels.DEBUG, msg, ...)
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:info(msg, ...)
|
||||
self:log(vim.log.levels.INFO, msg, ...)
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:warn(msg, ...)
|
||||
self:log(vim.log.levels.WARN, msg, ...)
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:error(msg, ...)
|
||||
self:log(vim.log.levels.ERROR, msg, ...)
|
||||
end
|
||||
|
||||
---@generic T : any
|
||||
---@param cb T
|
||||
---@param message nil|string
|
||||
---@return T
|
||||
function Logger:wrap_cb(cb, message)
|
||||
return function(err, ...)
|
||||
if err then
|
||||
self:error(message or "Error: %s", err)
|
||||
end
|
||||
return cb(err, ...)
|
||||
end
|
||||
end
|
||||
|
||||
local root = Logger.new({
|
||||
handlers = {
|
||||
{
|
||||
type = "echo",
|
||||
level = vim.log.levels.WARN,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
---@class Logger
|
||||
local M = {}
|
||||
|
||||
M.new = Logger.new
|
||||
|
||||
M.get_logfile = function()
|
||||
local ok, stdpath = pcall(vim.fn.stdpath, "log")
|
||||
if not ok then
|
||||
stdpath = vim.fn.stdpath("cache")
|
||||
end
|
||||
|
||||
return join(stdpath, "persisted.log")
|
||||
end
|
||||
|
||||
---@param logger Logger
|
||||
M.set_root = function(logger)
|
||||
root = logger
|
||||
end
|
||||
|
||||
---@return Logger
|
||||
M.get_root = function()
|
||||
return root
|
||||
end
|
||||
|
||||
setmetatable(M, {
|
||||
__index = function(_, key)
|
||||
return root[key]
|
||||
end,
|
||||
})
|
||||
|
||||
return M
|
||||
@ -1,118 +1,15 @@
|
||||
local M = {}
|
||||
local e = vim.fn.fnameescape
|
||||
local fp_sep = vim.loop.os_uname().sysname:lower():match("windows") and "\\" or "/" -- \ for windows, mac and linux both use \
|
||||
|
||||
---Print an error message
|
||||
--@param msg string
|
||||
--@param error string
|
||||
--@return string
|
||||
local function echoerr(msg, error)
|
||||
vim.api.nvim_echo({
|
||||
{ "[Persisted.nvim]: ", "ErrorMsg" },
|
||||
{ msg, "WarningMsg" },
|
||||
{ error, "Normal" },
|
||||
}, true, {})
|
||||
end
|
||||
|
||||
--- Escape special pattern matching characters in a string
|
||||
---@param input string
|
||||
---@return string
|
||||
local function escape_pattern(input)
|
||||
local magic_chars = { "%", "(", ")", ".", "+", "-", "*", "?", "[", "^", "$" }
|
||||
|
||||
for _, char in ipairs(magic_chars) do
|
||||
input = input:gsub("%" .. char, "%%" .. char)
|
||||
end
|
||||
|
||||
return input
|
||||
end
|
||||
|
||||
---Form a table of session data
|
||||
---@param session string
|
||||
---@return table|nil
|
||||
function M.make_session_data(session)
|
||||
local config = require("persisted.config").options
|
||||
|
||||
local home = os.getenv("HOME") or os.getenv("USERPROFILE") or ""
|
||||
|
||||
-- Split the session string into path and branch parts
|
||||
local separator_index = session:find(config.branch_separator)
|
||||
local branch = ""
|
||||
if separator_index then
|
||||
branch = session:sub(separator_index + 2):gsub("%.vim$", ""):gsub("%%", "/")
|
||||
end
|
||||
|
||||
-- Removing the home directory from the path and cleaning leading `/`
|
||||
local name = session:gsub(config.save_dir, ""):gsub("%%", "/"):gsub(home, "")
|
||||
-- Remove the .vim extension
|
||||
name = name:sub(1, #name - 4)
|
||||
if name:sub(1, 1) == "/" then
|
||||
name = name:sub(2)
|
||||
end
|
||||
|
||||
local dir_path = name:gsub(branch, ""):gsub(config.branch_separator, ""):gsub(home, "")
|
||||
|
||||
return {
|
||||
name = name,
|
||||
dir_path = dir_path,
|
||||
file_path = session,
|
||||
branch = branch,
|
||||
}
|
||||
end
|
||||
|
||||
--- Get the last element in a table
|
||||
---@param table table
|
||||
---@return string
|
||||
function M.get_last_item(table)
|
||||
local last
|
||||
for _, _ in pairs(table) do
|
||||
last = #table - 0
|
||||
end
|
||||
return table[last]
|
||||
end
|
||||
|
||||
---Check if a target directory exists in a given table
|
||||
---@param dir string
|
||||
---@param dirs_table table
|
||||
---@return boolean
|
||||
function M.dirs_match(dir, dirs_table)
|
||||
dir = vim.fn.expand(dir)
|
||||
return M.table_match(dir, dirs_table, function(pattern)
|
||||
return escape_pattern(vim.fn.expand(pattern))
|
||||
end)
|
||||
end
|
||||
|
||||
---Check if a string matches and entry in a given table
|
||||
---@param needle string
|
||||
---@param heystack table
|
||||
---@return boolean
|
||||
function M.table_match(needle, heystack, escape_fct)
|
||||
if needle == nil then
|
||||
return false
|
||||
end
|
||||
return heystack
|
||||
and next(vim.tbl_filter(function(pattern)
|
||||
if pattern.exact then
|
||||
-- The pattern is actually a table
|
||||
pattern = pattern[1]
|
||||
-- Stripping off the trailing backslash that a user might put here,
|
||||
-- but only if we aren't looking at the root directory
|
||||
if pattern:sub(-1) == fp_sep and pattern:len() > 1 then
|
||||
pattern = pattern:sub(1, -2)
|
||||
end
|
||||
return needle == pattern
|
||||
else
|
||||
if escape_fct then
|
||||
pattern = escape_fct(pattern)
|
||||
end
|
||||
return needle:match(pattern)
|
||||
end
|
||||
end, heystack))
|
||||
--- Escapes the given text to be safe for use in file-system paths/names,
|
||||
--- accounting for cross-platform use.
|
||||
---@param text string
|
||||
function M.make_fs_safe(text)
|
||||
return text:gsub("[\\/:]+", "%%")
|
||||
end
|
||||
|
||||
---Get the directory pattern based on OS
|
||||
---@return string
|
||||
function M.get_dir_pattern()
|
||||
function M.dir_pattern()
|
||||
local pattern = "/"
|
||||
if vim.fn.has("win32") == 1 then
|
||||
pattern = "[\\:]"
|
||||
@ -120,24 +17,51 @@ function M.get_dir_pattern()
|
||||
return pattern
|
||||
end
|
||||
|
||||
---Load the given session
|
||||
---@param session string
|
||||
---@param silent boolean Load the session silently?
|
||||
---@return nil|string
|
||||
function M.load_session(session, silent)
|
||||
local session_data = M.make_session_data(session)
|
||||
---Check if a directory is a subdirectory of another
|
||||
---@param parent string
|
||||
---@param child string
|
||||
---@return boolean
|
||||
function M.is_subdirectory(parent, child)
|
||||
return vim.startswith(child, parent)
|
||||
end
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPre", data = session_data })
|
||||
---Check if a directory exists in the given table of directories
|
||||
---@param dir string The directory to check
|
||||
---@param dirs table The table of directories to search in
|
||||
---@return boolean
|
||||
function M.dirs_match(dir, dirs)
|
||||
dir = M.make_fs_safe(vim.fn.expand(dir))
|
||||
|
||||
local ok, result = pcall(vim.cmd, (silent and "silent " or "") .. "source " .. e(session))
|
||||
if not ok then
|
||||
return echoerr("Error loading the session! ", result)
|
||||
for _, search in ipairs(dirs) do
|
||||
if type(search) == "string" then
|
||||
search = M.make_fs_safe(vim.fn.expand(search))
|
||||
if M.is_subdirectory(search, dir) then
|
||||
return true
|
||||
end
|
||||
elseif type(search) == "table" then
|
||||
if search.exact then
|
||||
search = M.make_fs_safe(vim.fn.expand(search[1]))
|
||||
if dir == search then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
vim.g.persisted_exists = true
|
||||
vim.g.persisted_loaded_session = session
|
||||
return false
|
||||
end
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPost", data = session_data })
|
||||
---Check if a value exists in a table
|
||||
---@param val any The value to search for
|
||||
---@param tbl table The table to search in
|
||||
---@return boolean
|
||||
function M.in_table(val, tbl)
|
||||
for _, v in pairs(tbl) do
|
||||
if v == val then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@ -6,23 +6,79 @@ local action_state = require("telescope.actions.state")
|
||||
local _actions = require("telescope._extensions.persisted.actions")
|
||||
local _finders = require("telescope._extensions.persisted.finders")
|
||||
|
||||
local persisted = require("persisted")
|
||||
local utils = require("persisted.utils")
|
||||
|
||||
local config = persisted.config
|
||||
|
||||
local telescope_opts = {}
|
||||
|
||||
---Escapes special characters before performing string substitution
|
||||
---@param str string
|
||||
---@param pattern string
|
||||
---@param replace string
|
||||
---@param n? integer
|
||||
---@return string
|
||||
---@return integer
|
||||
local function escape_pattern(str, pattern, replace, n)
|
||||
pattern = string.gsub(pattern, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") -- escape pattern
|
||||
replace = string.gsub(replace, "[%%]", "%%%%") -- escape replacement
|
||||
|
||||
return string.gsub(str, pattern, replace, n)
|
||||
end
|
||||
|
||||
---List all of the available sessions
|
||||
local function list_sessions()
|
||||
local sep = utils.dir_pattern()
|
||||
|
||||
local sessions = {}
|
||||
for _, session in pairs(persisted.list()) do
|
||||
local session_name = escape_pattern(session, config.save_dir, "")
|
||||
:gsub("%%", sep)
|
||||
:gsub(vim.fn.expand("~"), sep)
|
||||
:gsub("//", "")
|
||||
:sub(1, -5)
|
||||
|
||||
if vim.fn.has("win32") == 1 then
|
||||
session_name = escape_pattern(session_name, sep, ":", 1)
|
||||
session_name = escape_pattern(session_name, sep, "\\")
|
||||
end
|
||||
|
||||
local branch, dir_path
|
||||
|
||||
if string.find(session_name, "@@", 1, true) then
|
||||
local splits = vim.split(session_name, "@@", { plain = true })
|
||||
branch = table.remove(splits, #splits)
|
||||
dir_path = vim.fn.join(splits, "@@")
|
||||
else
|
||||
dir_path = session_name
|
||||
end
|
||||
|
||||
table.insert(sessions, {
|
||||
["name"] = session_name,
|
||||
["file_path"] = session,
|
||||
["branch"] = branch,
|
||||
["dir_path"] = dir_path,
|
||||
})
|
||||
end
|
||||
return sessions
|
||||
end
|
||||
|
||||
---Search through the Persisted sessions
|
||||
---@param opts table
|
||||
local function search_sessions(opts)
|
||||
local config = require("persisted.config").options
|
||||
opts = vim.tbl_extend("force", telescope_opts, opts or {})
|
||||
|
||||
pickers
|
||||
.new(opts, {
|
||||
prompt_title = "Sessions",
|
||||
sorter = conf.generic_sorter(opts),
|
||||
finder = _finders.session_finder(require("persisted").list()),
|
||||
finder = _finders.session_finder(list_sessions()),
|
||||
attach_mappings = function(prompt_bufnr, map)
|
||||
local refresh_sessions = function()
|
||||
local picker = action_state.get_current_picker(prompt_bufnr)
|
||||
picker:refresh(_finders.session_finder(require("persisted").list()), {
|
||||
-- INFO: Account for users who are still using the old API
|
||||
reset_prompt = config.telescope.reset_prompt or config.telescope.reset_prompt_after_deletion,
|
||||
picker:refresh(_finders.session_finder(list_sessions()), {
|
||||
reset_prompt = true,
|
||||
})
|
||||
end
|
||||
|
||||
@ -53,9 +109,9 @@ end
|
||||
|
||||
return require("telescope").register_extension({
|
||||
setup = function(topts)
|
||||
vim.api.nvim_set_hl(0, "TelescopePersistedIsCurrent", { link = "TelescopeResultsOperator" })
|
||||
vim.api.nvim_set_hl(0, "TelescopePersistedDir", { link = "Directory" })
|
||||
vim.api.nvim_set_hl(0, "TelescopePersistedBranch", { link = "TelescopeResultsIdentifier" })
|
||||
vim.api.nvim_set_hl(0, "PersistedTelescopeSelected", { link = "TelescopeResultsOperator", default = true })
|
||||
vim.api.nvim_set_hl(0, "PersistedTelescopeDir", { link = "Directory", default = true })
|
||||
vim.api.nvim_set_hl(0, "PersistedTelescopeBranch", { link = "TelescopeResultsIdentifier", default = true })
|
||||
telescope_opts = topts
|
||||
end,
|
||||
exports = {
|
||||
|
||||
@ -2,43 +2,43 @@ local actions_state = require("telescope.actions.state")
|
||||
local transform_mod = require("telescope.actions.mt").transform_mod
|
||||
|
||||
local persisted = require("persisted")
|
||||
local config = persisted.config
|
||||
|
||||
local M = {}
|
||||
|
||||
---Fire an event
|
||||
---@param event string
|
||||
local function fire(event)
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "Persisted" .. event })
|
||||
end
|
||||
|
||||
---Get the selected session from Telescope
|
||||
---@return table
|
||||
local get_selected_session = function()
|
||||
local function get_selected_session()
|
||||
return actions_state.get_selected_entry()
|
||||
end
|
||||
|
||||
---Load the selected session
|
||||
---@param session table
|
||||
---@param config table
|
||||
---@return nil
|
||||
M.load_session = function(session, config)
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedTelescopeLoadPre", data = session })
|
||||
|
||||
function M.load_session(session)
|
||||
fire("TelescopeLoadPre")
|
||||
vim.schedule(function()
|
||||
persisted.load({ session = session.file_path })
|
||||
end)
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedTelescopeLoadPost", data = session })
|
||||
fire("TelescopeLoadPost")
|
||||
end
|
||||
|
||||
---Delete the selected session from disk
|
||||
--@return nil
|
||||
M.delete_session = function()
|
||||
function M.delete_session()
|
||||
local session = get_selected_session()
|
||||
local path = session.file_path
|
||||
|
||||
if vim.fn.confirm("Delete [" .. session.name .. "]?", "&Yes\n&No") == 1 then
|
||||
vim.fn.delete(vim.fn.expand(path))
|
||||
vim.fn.delete(vim.fn.expand(session.file_path))
|
||||
end
|
||||
end
|
||||
|
||||
---Change the branch of an existing session
|
||||
---@param config table
|
||||
---@return nil
|
||||
M.change_branch = function(config)
|
||||
function M.change_branch()
|
||||
local session = get_selected_session()
|
||||
local path = session.file_path
|
||||
|
||||
@ -48,8 +48,7 @@ M.change_branch = function(config)
|
||||
local ext = path:match("^.+(%..+)$")
|
||||
|
||||
-- Check for existing branch in the filename
|
||||
local branch_separator = config.branch_separator or "@@"
|
||||
local pattern = "(.*)" .. branch_separator .. ".+" .. ext .. "$"
|
||||
local pattern = "(.*)@@.+" .. ext .. "$"
|
||||
local base = path:match(pattern) or path:sub(1, #path - #ext)
|
||||
|
||||
-- Replace or add the new branch name
|
||||
@ -57,7 +56,7 @@ M.change_branch = function(config)
|
||||
if branch == "" then
|
||||
new_path = base .. ext
|
||||
else
|
||||
new_path = base .. branch_separator .. branch .. ext
|
||||
new_path = base .. "@@" .. branch .. ext
|
||||
end
|
||||
|
||||
os.rename(path, new_path)
|
||||
@ -65,8 +64,7 @@ M.change_branch = function(config)
|
||||
end
|
||||
|
||||
---Copy an existing session
|
||||
---@return nil
|
||||
M.copy_session = function(config)
|
||||
function M.copy_session()
|
||||
local session = get_selected_session()
|
||||
local old_name = session.file_path:gsub(config.save_dir, "")
|
||||
|
||||
|
||||
@ -1,16 +1,18 @@
|
||||
local config = require("persisted.config")
|
||||
local config = require("persisted").config
|
||||
local finders = require("telescope.finders")
|
||||
|
||||
local M = {}
|
||||
|
||||
local no_icons = {
|
||||
branch = "",
|
||||
dir = "",
|
||||
selected = "",
|
||||
dir = "",
|
||||
branch = "",
|
||||
}
|
||||
|
||||
M.session_finder = function(sessions)
|
||||
local icons = vim.tbl_extend("force", no_icons, config.options.telescope.icons or {})
|
||||
---Create a finder for persisted sessions
|
||||
---@param sessions table
|
||||
function M.session_finder(sessions)
|
||||
local icons = vim.tbl_extend("force", no_icons, config.telescope.icons or {})
|
||||
|
||||
local custom_displayer = function(session)
|
||||
local final_str = ""
|
||||
@ -25,15 +27,15 @@ M.session_finder = function(sessions)
|
||||
end
|
||||
|
||||
-- is current session
|
||||
append(session.file_path == vim.v.this_session and (icons.selected .. " ") or " ", "TelescopePersistedIsCurrent")
|
||||
append(session.file_path == vim.v.this_session and (icons.selected .. " ") or " ", "PersistedTelescopeSelected")
|
||||
|
||||
-- session path
|
||||
append(icons.dir, "TelescopePersistedDir")
|
||||
append(icons.dir, "PersistedTelescopeDir")
|
||||
append(session.dir_path)
|
||||
|
||||
-- branch
|
||||
if session.branch then
|
||||
append(" " .. icons.branch .. session.branch, "TelescopePersistedBranch")
|
||||
append(" " .. icons.branch .. session.branch, "PersistedTelescopeBranch")
|
||||
end
|
||||
|
||||
return final_str, hls
|
||||
|
||||
@ -2,33 +2,20 @@ if vim.g.loaded_persisted then
|
||||
return
|
||||
end
|
||||
|
||||
local persisted = require("persisted")
|
||||
|
||||
-- Create the user commands
|
||||
vim.cmd([[command! SessionStart :lua require("persisted").start()]])
|
||||
vim.cmd([[command! SessionStop :lua require("persisted").stop()]])
|
||||
vim.cmd([[command! SessionSave :lua require("persisted").save({ force = true })]])
|
||||
vim.cmd([[command! SessionLoad :lua require("persisted").load()]])
|
||||
vim.cmd([[command! SessionLoadLast :lua require("persisted").load({ last = true })]])
|
||||
vim.cmd([[command! -nargs=1 SessionLoadFromFile :lua require("persisted").load({ session = <f-args> })]])
|
||||
vim.cmd([[command! SessionDelete :lua require("persisted").delete()]])
|
||||
vim.cmd([[command! SessionToggle :lua require("persisted").toggle()]])
|
||||
vim.cmd([[command! SessionSelect :lua require("persisted").select()]])
|
||||
|
||||
-- Create the autocmds
|
||||
local group = vim.api.nvim_create_augroup("Persisted", {})
|
||||
local persisted = require("persisted")
|
||||
|
||||
vim.api.nvim_create_autocmd({ "VimEnter" }, {
|
||||
group = group,
|
||||
vim.api.nvim_create_autocmd("VimEnter", {
|
||||
nested = true,
|
||||
callback = persisted.autoload,
|
||||
})
|
||||
vim.api.nvim_create_autocmd({ "VimLeavePre" }, {
|
||||
group = group,
|
||||
nested = true,
|
||||
callback = function()
|
||||
persisted.save()
|
||||
vim.cmd('sleep 10m')
|
||||
end
|
||||
})
|
||||
|
||||
vim.g.loaded_persisted = true
|
||||
|
||||
@ -8,7 +8,7 @@ describe("With default settings:", function()
|
||||
-- vim.fn.system("rm -rf " .. e(session_dir))
|
||||
end)
|
||||
|
||||
it("it saves a session", function()
|
||||
it("saves a session", function()
|
||||
-- Check no file exists
|
||||
assert.equals(vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", ""), "0")
|
||||
|
||||
@ -24,7 +24,7 @@ describe("With default settings:", function()
|
||||
assert.equals("1", vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", ""))
|
||||
end)
|
||||
|
||||
it("it loads a session", function()
|
||||
it("loads a session", function()
|
||||
-- Load a session
|
||||
require("persisted").load()
|
||||
|
||||
@ -35,19 +35,19 @@ describe("With default settings:", function()
|
||||
assert.equals(vim.g.persisting, true)
|
||||
end)
|
||||
|
||||
it("it stops a session", function()
|
||||
it("stops a session", function()
|
||||
require("persisted").stop()
|
||||
|
||||
assert.equals(vim.g.persisting, false)
|
||||
end)
|
||||
|
||||
it("it starts a session", function()
|
||||
it("starts a session", function()
|
||||
require("persisted").start()
|
||||
|
||||
assert.equals(vim.g.persisting, true)
|
||||
end)
|
||||
|
||||
it("it lists sessions", function()
|
||||
it("lists sessions", function()
|
||||
local sessions = require("persisted").list()
|
||||
local path = require("plenary.path"):new(sessions[1].file_path)
|
||||
|
||||
@ -59,19 +59,10 @@ local async = require("plenary.async.tests")
|
||||
local util = require("plenary.async.util")
|
||||
|
||||
async.describe("With default settings:", function()
|
||||
async.it("it deletes a session", function()
|
||||
async.it("deletes a session", function()
|
||||
require("persisted").delete()
|
||||
util.scheduler()
|
||||
|
||||
assert.equals("0", vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", ""))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("Utilities", function()
|
||||
it("can make derive the session name", function()
|
||||
local session = "%home%username%projects%front@@user%fix-analytics-export-null-values.vim"
|
||||
local data = require("persisted.utils").make_session_data(session)
|
||||
|
||||
assert.equals("home/username/projects/front@@user/fix-analytics-export-null-values", data.name)
|
||||
end)
|
||||
end)
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
local session_dir = vim.fn.getcwd() .. "/tests/default_data/"
|
||||
local utils = require("persisted.utils")
|
||||
|
||||
describe("Directory utilities:", function()
|
||||
it("can match directories", function()
|
||||
local cwd = "~/Code/Neovim/persisted.nvim"
|
||||
|
||||
local allowed_dirs = { "~/Code" }
|
||||
local match = utils.dirs_match(cwd, allowed_dirs)
|
||||
assert.equals(true, match)
|
||||
|
||||
allowed_dirs = { "~/.dotfiles" }
|
||||
match = utils.dirs_match(cwd, allowed_dirs)
|
||||
assert.equals(false, match)
|
||||
end)
|
||||
|
||||
it("can work with exact directories", function()
|
||||
local cwd = "~/Code/Neovim/persisted.nvim"
|
||||
local allowed_dirs = { { "~/Code", exact = true } }
|
||||
local match = utils.dirs_match(cwd, allowed_dirs)
|
||||
assert.equals(false, match)
|
||||
|
||||
cwd = "~/Code/Neovim/persisted.nvim"
|
||||
allowed_dirs = { { "~/Code/Neovim/persisted.nvim", exact = true } }
|
||||
match = utils.dirs_match(cwd, allowed_dirs)
|
||||
assert.equals(true, match)
|
||||
end)
|
||||
|
||||
it("can handle only ignore directories", function()
|
||||
local cwd = "~/Code/Neovim/persisted.nvim"
|
||||
local allowed_dirs = {}
|
||||
local ignored_dirs = { { "/tmp" } }
|
||||
local allowed_match = utils.dirs_match(cwd, allowed_dirs)
|
||||
local ignored_match = utils.dirs_match(cwd, ignored_dirs)
|
||||
-- This looks weird, I know. That is because we expect dirs_match to return
|
||||
-- false for allowed_dirs since allowed dirs is empty.
|
||||
-- Therefore this is actually testing to ensure we are getting false and false
|
||||
-- This test specifically addresses the change added in
|
||||
-- https://github.com/olimorris/persisted.nvim/pull/152
|
||||
assert.equals(true, not allowed_match and not ignored_match)
|
||||
end)
|
||||
end)
|
||||
@ -1,15 +1,13 @@
|
||||
pcall(vim.fn.system, "rm -rf tests/dummy_data")
|
||||
|
||||
local session_dir = vim.fn.getcwd() .. "/tests/dummy_data/"
|
||||
|
||||
-- follow_cwd = true
|
||||
pcall(vim.fn.system, "rm -rf tests/dummy_data")
|
||||
require("persisted").setup({
|
||||
save_dir = session_dir,
|
||||
follow_cwd = true,
|
||||
})
|
||||
|
||||
describe("Follow cwd change", function()
|
||||
it("creates two sessions with change in cwd", function()
|
||||
describe("Follow cwd", function()
|
||||
it("creates two sessions", function()
|
||||
vim.cmd(":e tests/stubs/test_autoload.txt")
|
||||
vim.cmd(":w")
|
||||
|
||||
@ -19,15 +17,15 @@ describe("Follow cwd change", function()
|
||||
require("persisted").save()
|
||||
vim.cmd(":bdelete")
|
||||
end)
|
||||
|
||||
it("ensures both sessions were created", function()
|
||||
require("persisted").load()
|
||||
|
||||
local pattern = "/"
|
||||
local branch1 = require("persisted").get_branch()
|
||||
local name1 = vim.fn.getcwd():gsub(pattern, "%%") .. (branch1 or "") .. ".vim"
|
||||
local name1 = vim.fn.getcwd():gsub(pattern, "%%") .. ".vim"
|
||||
|
||||
vim.cmd(":cd ../..")
|
||||
local branch2 = require("persisted").get_branch()
|
||||
local name2 = vim.fn.getcwd():gsub(pattern, "%%") .. (branch2 or "") .. ".vim"
|
||||
local name2 = vim.fn.getcwd():gsub(pattern, "%%") .. ".vim"
|
||||
|
||||
local sessions = vim.fn.readdir(session_dir)
|
||||
assert.equals(sessions[1], name1)
|
||||
@ -35,14 +33,13 @@ describe("Follow cwd change", function()
|
||||
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()
|
||||
describe("Follow a cwd change", function()
|
||||
it("creates two sessions with change in cwd", function()
|
||||
vim.cmd(":e tests/stubs/test_autoload.txt")
|
||||
vim.cmd(":w")
|
||||
@ -56,11 +53,11 @@ describe("Don't follow cwd change", function()
|
||||
it("ensures only one session was created", function()
|
||||
local pattern = "/"
|
||||
vim.cmd(":cd ../..")
|
||||
local branch = require("persisted").get_branch()
|
||||
local name = vim.fn.getcwd():gsub(pattern, "%%") .. (branch or "") .. ".vim"
|
||||
|
||||
local name = vim.fn.getcwd():gsub(pattern, "%%") .. ".vim"
|
||||
|
||||
local sessions = vim.fn.readdir(session_dir)
|
||||
assert.equals(#sessions, 1)
|
||||
assert.equals(name, sessions[1])
|
||||
assert.equals(sessions[1], name)
|
||||
end)
|
||||
end)
|
||||
|
||||
@ -21,9 +21,24 @@ describe("Git Branching", function()
|
||||
end)
|
||||
|
||||
it("ensures the session has the branch name in", function()
|
||||
if vim.fn.isdirectory(session_dir .. "/.git") == 0 then
|
||||
vim.fn.system("mkdir -p " .. session_dir)
|
||||
vim.fn.system("cd " .. session_dir .. " && git init")
|
||||
end
|
||||
|
||||
local branch_name = require("persisted").branch()
|
||||
|
||||
-- Check if branch_name is valid
|
||||
if not branch_name then
|
||||
print("Failed to get branch name.")
|
||||
branch_name = ""
|
||||
else
|
||||
branch_name = "@@" .. branch_name
|
||||
end
|
||||
|
||||
-- Workout what the name should be
|
||||
local pattern = "/"
|
||||
local name = vim.fn.getcwd():gsub(pattern, "%%") .. require("persisted").get_branch() .. ".vim"
|
||||
local name = vim.fn.getcwd():gsub(pattern, "%%") .. branch_name .. ".vim"
|
||||
local session = vim.fn.glob(session_dir .. "*.vim", true, true)[1]
|
||||
|
||||
session:gsub(session_dir .. "/", "")
|
||||
|
||||
@ -7,4 +7,5 @@ command TestAutoloading PlenaryBustedDirectory tests/autoload {minimal_init = 't
|
||||
command TestGitBranching PlenaryBustedDirectory tests/git_branching {minimal_init = 'tests/minimal.vim'}
|
||||
command TestFollowCwd PlenaryBustedDirectory tests/follow_cwd {minimal_init = 'tests/minimal.vim'}
|
||||
command TestDefaults PlenaryBustedFile tests/default_settings_spec.lua
|
||||
command TestDirs PlenaryBustedFile tests/dirs_spec.lua
|
||||
command TearDown PlenaryBustedFile tests/teardown/clean_up_dirs.lua
|
||||
|
||||
Reference in New Issue
Block a user