27 Commits

Author SHA1 Message Date
c45ff862b5 chore(main): release 3.1.0 (#66)
🤖 I have created a release *beep* *boop*
---


##
[3.1.0](https://github.com/folke/persistence.nvim/compare/v3.0.1...v3.1.0)
(2024-07-07)


### Features

* **load:** fallback to regular session when branch session does not
exist (yet)
([a93748a](a93748acdb))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2024-07-07 21:24:56 +02:00
a93748acdb feat(load): fallback to regular session when branch session does not exist (yet) 2024-07-07 19:01:19 +02:00
fc8273bac0 chore(main): release 3.0.1 (#63)
🤖 I have created a release *beep* *boop*
---


##
[3.0.1](https://github.com/folke/persistence.nvim/compare/v3.0.0...v3.0.1)
(2024-07-07)


### Bug Fixes

* branch detection. Closes
[#62](https://github.com/folke/persistence.nvim/issues/62)
([a1d37fa](a1d37fa32e))
* **branch:** escape branch name. Fixes
[#65](https://github.com/folke/persistence.nvim/issues/65)
([1565ca0](1565ca0af2))
* windows
([2d83b1a](2d83b1a5c3))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2024-07-07 09:24:55 +02:00
2d83b1a5c3 fix: windows 2024-07-07 05:36:38 +02:00
f18d0131ed chore(build): auto-generate docs 2024-07-07 05:03:13 +00:00
1565ca0af2 fix(branch): escape branch name. Fixes #65 2024-07-07 07:02:32 +02:00
2c7d3eb6b8 ci: update 2024-07-06 23:45:36 +02:00
a1d37fa32e fix: branch detection. Closes #62 2024-07-06 23:21:20 +02:00
34f337e383 ci: update 2024-07-06 23:19:54 +02:00
8b83876213 ci: update 2024-07-06 23:18:35 +02:00
7f1e8ce997 chore(main): release 3.0.0 (#61)
🤖 I have created a release *beep* *boop*
---


##
[3.0.0](https://github.com/folke/persistence.nvim/compare/v2.0.0...v3.0.0)
(2024-07-06)


### ⚠ BREAKING CHANGES

* opts.need specifieds how many buffers should be open for saving.
Replaces save_empty. Closes #19
* removed load_pre/load_post/save_pre/save_post in favor of events.
Check the readme
* removed `opts.options`. Use `vim.o.sessionoptions` instead.

### Features

* added `require("persistence").select()` to select a session to load
([5346b53](5346b5346a))
* opts.need specifieds how many buffers should be open for saving.
Replaces save_empty. Closes
[#19](https://github.com/folke/persistence.nvim/issues/19)
([7bb5755](7bb575517c))
* persistence.active. Check if a session saving is active
([f0ac0e9](f0ac0e981e))
* **persistence:** add `pre-` and `post-` load hooks
([#24](https://github.com/folke/persistence.nvim/issues/24))
([3d443bd](3d443bd0a7))
* removed `opts.options`. Use `vim.o.sessionoptions` instead.
([eb5622e](eb5622edae))
* removed load_pre/load_post/save_pre/save_post in favor of events.
Check the readme
([f58a838](f58a838282))
* sessions per branch. Closes
[#9](https://github.com/folke/persistence.nvim/issues/9)
([cd0054e](cd0054e6a4))


### Bug Fixes

* don't save `gitrebase` session
([#44](https://github.com/folke/persistence.nvim/issues/44))
([9dbe264](9dbe2648c6))
* opts.need
([9c0e522](9c0e5227fa))
* remove expand() as stdpath() expands itself
([#50](https://github.com/folke/persistence.nvim/issues/50))
([a2fd3d9](a2fd3d9965))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2024-07-06 22:01:55 +02:00
e1e2bf917f chore(build): auto-generate docs 2024-07-06 20:00:45 +00:00
cd0054e6a4 feat: sessions per branch. Closes #9 2024-07-06 22:00:20 +02:00
9c0e5227fa fix: opts.need 2024-07-06 21:54:41 +02:00
7bb575517c feat!: opts.need specifieds how many buffers should be open for saving. Replaces save_empty. Closes #19 2024-07-06 21:41:58 +02:00
f0ac0e981e feat: persistence.active. Check if a session saving is active 2024-07-06 21:31:22 +02:00
06e8ef93c8 chore(build): auto-generate docs 2024-07-06 19:24:52 +00:00
5346b5346a feat: added require("persistence").select() to select a session to load 2024-07-06 21:24:22 +02:00
f58a838282 feat!: removed load_pre/load_post/save_pre/save_post in favor of events. Check the readme 2024-07-06 21:23:56 +02:00
eb5622edae feat!: removed opts.options. Use vim.o.sessionoptions instead. 2024-07-06 21:22:40 +02:00
08af8fee77 chore(update): update repository (#60)
Automated changes by
[create-pull-request](https://github.com/peter-evans/create-pull-request)
GitHub action

Co-authored-by: folke <292349+folke@users.noreply.github.com>
2024-07-06 18:06:06 +02:00
4452f973c3 chore(update): update repository (#59)
Automated changes by
[create-pull-request](https://github.com/peter-evans/create-pull-request)
GitHub action

Co-authored-by: folke <292349+folke@users.noreply.github.com>
2024-07-06 14:59:44 +02:00
b915cbd3d1 chore(build): auto-generate docs 2024-07-06 09:48:09 +00:00
f11c573cb2 ci: update 2024-07-06 11:45:26 +02:00
57953891f8 ci: update 2024-07-05 19:00:53 +02:00
04599f36b5 chore(build): auto-generate docs 2024-07-05 13:46:37 +00:00
6a45b4fe2c ci: update 2024-07-05 15:46:10 +02:00
18 changed files with 311 additions and 157 deletions

View File

@ -75,7 +75,7 @@ body:
-- install plugins -- install plugins
local plugins = { local plugins = {
"folke/tokyonight.nvim", "folke/tokyonight.nvim",
"folke/persistence.nvim", { "folke/persistence.nvim", opts = {} },
-- add any other plugins here -- add any other plugins here
} }
require("lazy").setup(plugins, { require("lazy").setup(plugins, {

5
.github/ISSUE_TEMPLATE/config.yml vendored Normal file
View File

@ -0,0 +1,5 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/folke/persistence.nvim/discussions
about: Use Github discussions instead

16
.github/PULL_REQUEST_TEMPLATE.md vendored Normal file
View File

@ -0,0 +1,16 @@
## Description
<!-- Describe the big picture of your changes to communicate to the maintainers
why we should accept this pull request. -->
## Related Issue(s)
<!--
If this PR fixes any issues, please link to the issue here.
- Fixes #<issue_number>
-->
## Screenshots
<!-- Add screenshots of the changes if applicable. -->

6
.github/dependabot.yml vendored Normal file
View File

@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"

View File

@ -1,72 +1,14 @@
name: CI name: CI
on: on:
push: push:
branches: [main, master]
pull_request: pull_request:
jobs: jobs:
tests: ci:
strategy: uses: folke/github/.github/workflows/ci.yml@main
matrix: secrets: inherit
# os: [ubuntu-latest, windows-latest]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Install Neovim
shell: bash
run: |
mkdir -p /tmp/nvim
wget -q https://github.com/neovim/neovim/releases/download/nightly/nvim.appimage -O /tmp/nvim/nvim.appimage
cd /tmp/nvim
chmod a+x ./nvim.appimage
./nvim.appimage --appimage-extract
echo "/tmp/nvim/squashfs-root/usr/bin/" >> $GITHUB_PATH
- name: Run Tests
run: |
nvim --version
[ ! -d tests ] && exit 0
nvim --headless -u tests/init.lua -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/init.lua', sequential = true}"
docs:
runs-on: ubuntu-latest
needs: tests
if: ${{ github.ref == 'refs/heads/main' }}
steps:
- uses: actions/checkout@v3
- name: panvimdoc
uses: kdheepak/panvimdoc@main
with: with:
vimdoc: persistence.nvim plugin: persistence.nvim
version: "Neovim >= 0.8.0" repo: folke/persistence.nvim
demojify: true
treesitter: true
- name: Push changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "chore(build): auto-generate vimdoc"
commit_user_name: "github-actions[bot]"
commit_user_email: "github-actions[bot]@users.noreply.github.com"
commit_author: "github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
release:
name: release
if: ${{ github.ref == 'refs/heads/main' }}
needs:
- docs
- tests
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v3
id: release
with:
release-type: simple
package-name: persistence.nvim
- uses: actions/checkout@v3
- name: tag stable versions
if: ${{ steps.release.outputs.release_created }}
run: |
git config user.name github-actions[bot]
git config user.email github-actions[bot]@users.noreply.github.com
git remote add gh-token "https://${{ secrets.GITHUB_TOKEN }}@github.com/google-github-actions/release-please-action.git"
git tag -d stable || true
git push origin :stable || true
git tag -a stable -m "Last Stable Release"
git push origin stable

8
.github/workflows/labeler.yml vendored Normal file
View File

@ -0,0 +1,8 @@
name: "PR Labeler"
on:
- pull_request_target
jobs:
labeler:
uses: folke/github/.github/workflows/labeler.yml@main
secrets: inherit

18
.github/workflows/pr.yml vendored Normal file
View File

@ -0,0 +1,18 @@
name: PR Title
on:
pull_request_target:
types:
- opened
- edited
- synchronize
- reopened
- ready_for_review
permissions:
pull-requests: read
jobs:
pr-title:
uses: folke/github/.github/workflows/pr.yml@main
secrets: inherit

10
.github/workflows/stale.yml vendored Normal file
View File

@ -0,0 +1,10 @@
name: Stale Issues & PRs
on:
schedule:
- cron: "30 1 * * *"
jobs:
ci:
uses: folke/github/.github/workflows/stale.yml@main
secrets: inherit

12
.github/workflows/update.yml vendored Normal file
View File

@ -0,0 +1,12 @@
name: Update Repo
on:
workflow_dispatch:
schedule:
# Run every hour
- cron: "0 * * * *"
jobs:
ci:
uses: folke/github/.github/workflows/update.yml@main
secrets: inherit

15
.gitignore vendored
View File

@ -1,8 +1,9 @@
tt.*
.tests
doc/tags
debug
.repro
foo.*
*.log *.log
data /.repro
/.tests
/build
/debug
/doc/tags
foo.*
node_modules
tt.*

View File

@ -1,5 +1,47 @@
# Changelog # Changelog
## [3.1.0](https://github.com/folke/persistence.nvim/compare/v3.0.1...v3.1.0) (2024-07-07)
### Features
* **load:** fallback to regular session when branch session does not exist (yet) ([a93748a](https://github.com/folke/persistence.nvim/commit/a93748acdb2e7bc4389b3738b4c787b764c3b2a6))
## [3.0.1](https://github.com/folke/persistence.nvim/compare/v3.0.0...v3.0.1) (2024-07-07)
### Bug Fixes
* branch detection. Closes [#62](https://github.com/folke/persistence.nvim/issues/62) ([a1d37fa](https://github.com/folke/persistence.nvim/commit/a1d37fa32ef9431f6a57c217ba5c456d20834679))
* **branch:** escape branch name. Fixes [#65](https://github.com/folke/persistence.nvim/issues/65) ([1565ca0](https://github.com/folke/persistence.nvim/commit/1565ca0af2d93ee94335c2950d92bc133c90aa82))
* windows ([2d83b1a](https://github.com/folke/persistence.nvim/commit/2d83b1a5c3fe5b2251866f5263fb9607db8d64c0))
## [3.0.0](https://github.com/folke/persistence.nvim/compare/v2.0.0...v3.0.0) (2024-07-06)
### ⚠ BREAKING CHANGES
* opts.need specifieds how many buffers should be open for saving. Replaces save_empty. Closes #19
* removed load_pre/load_post/save_pre/save_post in favor of events. Check the readme
* removed `opts.options`. Use `vim.o.sessionoptions` instead.
### Features
* added `require("persistence").select()` to select a session to load ([5346b53](https://github.com/folke/persistence.nvim/commit/5346b5346a2dd1732ae84f05251ecb704f35df87))
* opts.need specifieds how many buffers should be open for saving. Replaces save_empty. Closes [#19](https://github.com/folke/persistence.nvim/issues/19) ([7bb5755](https://github.com/folke/persistence.nvim/commit/7bb575517cebbc2b172caa04581dc5d91be90136))
* persistence.active. Check if a session saving is active ([f0ac0e9](https://github.com/folke/persistence.nvim/commit/f0ac0e981e4c864df11e613636a23c5bad09376d))
* **persistence:** add `pre-` and `post-` load hooks ([#24](https://github.com/folke/persistence.nvim/issues/24)) ([3d443bd](https://github.com/folke/persistence.nvim/commit/3d443bd0a7e1d9eebfa37321fc8118d8d538af13))
* removed `opts.options`. Use `vim.o.sessionoptions` instead. ([eb5622e](https://github.com/folke/persistence.nvim/commit/eb5622edae69ec65f6f83fcdd0eb5a70ce48ece7))
* removed load_pre/load_post/save_pre/save_post in favor of events. Check the readme ([f58a838](https://github.com/folke/persistence.nvim/commit/f58a838282dac1ed33165a5fd03829b036584df2))
* sessions per branch. Closes [#9](https://github.com/folke/persistence.nvim/issues/9) ([cd0054e](https://github.com/folke/persistence.nvim/commit/cd0054e6a4c17e4068a3e69a030013d268e569f9))
### Bug Fixes
* don't save `gitrebase` session ([#44](https://github.com/folke/persistence.nvim/issues/44)) ([9dbe264](https://github.com/folke/persistence.nvim/commit/9dbe2648c67b678bf7fe688f03b57a2514e03e6f))
* opts.need ([9c0e522](https://github.com/folke/persistence.nvim/commit/9c0e5227fa7b36208a2db0d812008965c1aac889))
* remove expand() as stdpath() expands itself ([#50](https://github.com/folke/persistence.nvim/issues/50)) ([a2fd3d9](https://github.com/folke/persistence.nvim/commit/a2fd3d99656ac496e56233aa4a40dd045a16fdc4))
## [2.0.0](https://github.com/folke/persistence.nvim/compare/v1.2.1...v2.0.0) (2023-10-15) ## [2.0.0](https://github.com/folke/persistence.nvim/compare/v1.2.1...v2.0.0) (2023-10-15)

View File

@ -35,27 +35,38 @@ Persistence comes with the following defaults:
```lua ```lua
{ {
dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving -- minimum number of file buffers that need to be open to save
pre_save = nil, -- a function to call before saving the session -- Set to 0 to always save
post_save = nil, -- a function to call after saving the session need = 1,
save_empty = false, -- don't save if there are no open file buffers branch = true, -- use git branch to save session
pre_load = nil, -- a function to call before loading the session
post_load = nil, -- a function to call after loading the session
} }
``` ```
> [!TIP]
> To configure what should be saved in your session, check [:h 'sessionoptions'](https://neovim.io/doc/user/options.html#'sessionoptions')
## 🚀 Usage ## 🚀 Usage
**Persistence** works well with plugins like `startify` or `dashboard`. It will never restore a session automatically, **Persistence** works well with plugins like `startify` or `dashboard`. It will never restore a session automatically,
but you can of course write an autocmd that does exactly that if you want. but you can of course write an autocmd that does exactly that if you want.
```lua ```lua
-- restore the session for the current directory -- load the session for the current directory
vim.api.nvim_set_keymap("n", "<leader>qs", [[<cmd>lua require("persistence").load()<cr>]], {}) vim.keymap.set("n", "<leader>qs", function() require("persistence").load() end)
-- restore the last session -- select a session to load
vim.api.nvim_set_keymap("n", "<leader>ql", [[<cmd>lua require("persistence").load({ last = true })<cr>]], {}) vim.keymap.set("n", "<leader>qS", function() require("persistence").select() end)
-- load the last session
vim.keymap.set("n", "<leader>ql", function() require("persistence").load({ last = true }) end)
-- stop Persistence => session won't be saved on exit -- stop Persistence => session won't be saved on exit
vim.api.nvim_set_keymap("n", "<leader>qd", [[<cmd>lua require("persistence").stop()<cr>]], {}) vim.keymap.set("n", "<leader>qd", function() require("persistence").stop() end)
``` ```
## 📅 Events
- **PersistenceLoadPre**: before loading a session
- **PersistenceLoadPost**: after loading a session
- **PersistenceSavePre**: before saving a session
- **PersistenceSavePost**: after saving a session

View File

@ -1,4 +1,4 @@
*persistence.nvim.txt* For Neovim >= 0.8.0 Last change: 2024 June 12 *persistence.nvim.txt* For Neovim Last change: 2024 July 07
============================================================================== ==============================================================================
Table of Contents *persistence.nvim-table-of-contents* Table of Contents *persistence.nvim-table-of-contents*
@ -9,6 +9,7 @@ Table of Contents *persistence.nvim-table-of-contents*
- Installation |persistence.nvim-persistence-installation| - Installation |persistence.nvim-persistence-installation|
- Configuration |persistence.nvim-persistence-configuration| - Configuration |persistence.nvim-persistence-configuration|
- Usage |persistence.nvim-persistence-usage| - Usage |persistence.nvim-persistence-usage|
- Events |persistence.nvim-persistence-events|
============================================================================== ==============================================================================
1. Persistence *persistence.nvim-persistence* 1. Persistence *persistence.nvim-persistence*
@ -53,16 +54,17 @@ Persistence comes with the following defaults:
>lua >lua
{ {
dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving -- minimum number of file buffers that need to be open to save
pre_save = nil, -- a function to call before saving the session -- Set to 0 to always save
post_save = nil, -- a function to call after saving the session need = 1,
save_empty = false, -- don't save if there are no open file buffers branch = true, -- use git branch to save session
pre_load = nil, -- a function to call before loading the session
post_load = nil, -- a function to call after loading the session
} }
< <
[!TIP] To configure what should be saved in your session, check |:h
'sessionoptions'|
USAGE *persistence.nvim-persistence-usage* USAGE *persistence.nvim-persistence-usage*
**Persistence** works well with plugins like `startify` or `dashboard`. It will **Persistence** works well with plugins like `startify` or `dashboard`. It will
@ -70,16 +72,27 @@ never restore a session automatically, but you can of course write an autocmd
that does exactly that if you want. that does exactly that if you want.
>lua >lua
-- restore the session for the current directory -- load the session for the current directory
vim.api.nvim_set_keymap("n", "<leader>qs", [[<cmd>lua require("persistence").load()<cr>]], {}) vim.keymap.set("n", "<leader>qs", function() require("persistence").load() end)
-- restore the last session -- select a session to load
vim.api.nvim_set_keymap("n", "<leader>ql", [[<cmd>lua require("persistence").load({ last = true })<cr>]], {}) vim.keymap.set("n", "<leader>qS", function() require("persistence").select() end)
-- load the last session
vim.keymap.set("n", "<leader>ql", function() require("persistence").load({ last = true }) end)
-- stop Persistence => session won't be saved on exit -- stop Persistence => session won't be saved on exit
vim.api.nvim_set_keymap("n", "<leader>qd", [[<cmd>lua require("persistence").stop()<cr>]], {}) vim.keymap.set("n", "<leader>qd", function() require("persistence").stop() end)
< <
EVENTS *persistence.nvim-persistence-events*
- **PersistenceLoadPre**before loading a session
- **PersistenceLoadPost**after loading a session
- **PersistenceSavePre**before saving a session
- **PersistenceSavePost**after saving a session
Generated by panvimdoc <https://github.com/kdheepak/panvimdoc> Generated by panvimdoc <https://github.com/kdheepak/panvimdoc>
vim:tw=78:ts=8:noet:ft=help:norl: vim:tw=78:ts=8:noet:ft=help:norl:

View File

@ -1,17 +1,15 @@
local M = {} local M = {}
---@class PersistenceOptions ---@class Persistence.Config
---@field pre_save? fun()
---@field post_save? fun()
---@field pre_load? fun()
---@field post_load? fun()
local defaults = { local defaults = {
dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize", "skiprtp" }, -- sessionoptions used for saving -- minimum number of file buffers that need to be open to save
save_empty = false, -- don't save if there are no open file buffers -- Set to 0 to always save
need = 1,
branch = true, -- use git branch to save session
} }
---@type PersistenceOptions ---@type Persistence.Config
M.options = {} M.options = {}
function M.setup(opts) function M.setup(opts)

View File

@ -1,99 +1,146 @@
local Config = require("persistence.config") local Config = require("persistence.config")
local uv = vim.uv or vim.loop
local M = {} local M = {}
---@type string? M._active = false
M.current = nil
local e = vim.fn.fnameescape local e = vim.fn.fnameescape
function M.get_current() ---@param opts? {branch?: boolean}
local pattern = "/" function M.current(opts)
if vim.fn.has("win32") == 1 then opts = opts or {}
pattern = "[\\:]" local name = vim.fn.getcwd():gsub("[\\/:]+", "%%")
if Config.options.branch and opts.branch ~= false then
local branch = M.branch()
if branch and branch ~= "main" and branch ~= "master" then
name = name .. "%%" .. branch:gsub("[\\/:]+", "%%")
end
end end
local name = vim.fn.getcwd():gsub(pattern, "%%")
return Config.options.dir .. name .. ".vim" return Config.options.dir .. name .. ".vim"
end end
function M.get_last()
local sessions = M.list()
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
function M.setup(opts) function M.setup(opts)
Config.setup(opts) Config.setup(opts)
M.start() M.start()
end end
function M.fire(event)
vim.api.nvim_exec_autocmds("User", {
pattern = "Persistence" .. event,
})
end
-- Check if a session is active
function M.active()
return M._active
end
function M.start() function M.start()
M.current = M.get_current() M._active = true
vim.api.nvim_create_autocmd("VimLeavePre", { vim.api.nvim_create_autocmd("VimLeavePre", {
group = vim.api.nvim_create_augroup("persistence", { clear = true }), group = vim.api.nvim_create_augroup("persistence", { clear = true }),
callback = function() callback = function()
if Config.options.pre_save then M.fire("SavePre")
Config.options.pre_save()
end
if not Config.options.save_empty then if Config.options.need > 0 then
local bufs = vim.tbl_filter(function(b) local bufs = vim.tbl_filter(function(b)
if vim.bo[b].buftype ~= "" then if vim.bo[b].buftype ~= "" or vim.bo[b].filetype == "gitcommit" or vim.bo[b].filetype == "gitrebase" then
return false
end
if vim.bo[b].filetype == "gitcommit" then
return false
end
if vim.bo[b].filetype == "gitrebase" then
return false return false
end end
return vim.api.nvim_buf_get_name(b) ~= "" return vim.api.nvim_buf_get_name(b) ~= ""
end, vim.api.nvim_list_bufs()) end, vim.api.nvim_list_bufs())
if #bufs == 0 then if #bufs < Config.options.need then
return return
end end
end end
M.save() M.save()
M.fire("SavePost")
if type(Config.options.post_save) == "function" then
Config.options.post_save()
end
end, end,
}) })
end end
function M.stop() function M.stop()
M.current = nil M._active = false
pcall(vim.api.nvim_del_augroup_by_name, "persistence") pcall(vim.api.nvim_del_augroup_by_name, "persistence")
end end
function M.save() function M.save()
local tmp = vim.o.sessionoptions vim.cmd("mks! " .. e(M.current()))
vim.o.sessionoptions = table.concat(Config.options.options, ",")
vim.cmd("mks! " .. e(M.current or M.get_current()))
vim.o.sessionoptions = tmp
end end
function M.load(opt) ---@param opts? { last?: boolean }
opt = opt or {} function M.load(opts)
local sfile = opt.last and M.get_last() or M.get_current() opts = opts or {}
if sfile and vim.fn.filereadable(sfile) ~= 0 then ---@type string
if type(Config.options.pre_load) == "function" then local file
Config.options.pre_load() if opts.last then
file = M.last()
else
file = M.current()
if vim.fn.filereadable(file) == 0 then
file = M.current({ branch = false })
end end
vim.cmd("silent! source " .. e(sfile))
if type(Config.options.post_load) == "function" then
Config.options.post_load()
end end
if file and vim.fn.filereadable(file) ~= 0 then
M.fire("LoadPre")
vim.cmd("silent! source " .. e(file))
M.fire("LoadPost")
end end
end end
---@return string[]
function M.list() function M.list()
return vim.fn.glob(Config.options.dir .. "*.vim", true, true) local sessions = vim.fn.glob(Config.options.dir .. "*.vim", true, true)
table.sort(sessions, function(a, b)
return uv.fs_stat(a).mtime.sec > uv.fs_stat(b).mtime.sec
end)
return sessions
end
function M.last()
return M.list()[1]
end
function M.select()
---@type { session: string, dir: string, branch?: string }[]
local items = {}
local have = {} ---@type table<string, boolean>
for _, session in ipairs(M.list()) do
if uv.fs_stat(session) then
local file = session:sub(#Config.options.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 have[dir] then
have[dir] = 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)
return vim.fn.fnamemodify(item.dir, ":p:~")
end,
}, function(item)
if item then
vim.fn.chdir(item.dir)
M.load()
end
end)
end
--- get current branch name
---@return string?
function M.branch()
if uv.fs_stat(".git") then
local ret = vim.fn.systemlist("git branch --show-current")[1]
return vim.v.shell_error == 0 and ret or nil
end
end end
return M return M

View File

@ -1 +1,4 @@
std="lua51+vim" std="vim"
[lints]
mixed_table="allow"

View File

@ -1,3 +1,6 @@
indent_type = "Spaces" indent_type = "Spaces"
indent_width = 2 indent_width = 2
column_width = 120 column_width = 120
[sort_requires]
enabled = true

View File

@ -1,2 +1,21 @@
[selene]
base = "lua51"
name = "vim"
[vim] [vim]
any = true any = true
[jit]
any = true
[assert]
any = true
[describe]
any = true
[it]
any = true
[before_each.args]
any = true