From 43d3910bada1342e0a9dc429d7dc3197710f3b13 Mon Sep 17 00:00:00 2001 From: Daniel Zhang Date: Mon, 27 Jan 2025 18:08:30 +0800 Subject: [PATCH 1/8] Use fzf_exec instead of fzf.files, sort sessions by mtime, and add new session action `fzf.files` brings some needless actions (toggle .gitignore, open in split an anyothers that user config for fzf-lua files). We could use `fzf_exec` instead. Moreover, I use `ls -t` to list sessions, so they will sorted by mtime. And I add a new session action in fzf session list. --- lua/nvim-possession/init.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lua/nvim-possession/init.lua b/lua/nvim-possession/init.lua index c87f29e..ba23e89 100644 --- a/lua/nvim-possession/init.lua +++ b/lua/nvim-possession/init.lua @@ -45,6 +45,7 @@ M.setup = function(user_opts) end end end + fzf.config.set_action_helpstr(M.new, "new-session") ---update loaded session with current status M.update = function() @@ -125,7 +126,7 @@ M.setup = function(user_opts) return end - return fzf.files({ + local opts = { user_config = user_config, prompt = user_config.sessions.sessions_icon .. user_config.sessions.sessions_prompt, cwd_prompt = false, @@ -139,9 +140,13 @@ M.setup = function(user_opts) cwd = user_config.sessions.sessions_path, actions = { ["enter"] = M.load, - ["ctrl-x"] = { M.delete_selected, fzf.actions.resume }, + ["ctrl-x"] = { fn = M.delete_selected, reload = true, header = "delete session" }, + ["ctrl-n"] = { fn = M.new, header = "new session" }, }, - }) + } + opts = require("fzf-lua.config").normalize_opts(opts, {}) + opts = require("fzf-lua.core").set_header(opts, { "actions" }) + fzf.fzf_exec("ls -t", opts) end if user_config.autoload and vim.fn.argc() == 0 then From cd01e7360d2d6085daa97a339e70bb245dbe0980 Mon Sep 17 00:00:00 2001 From: Daniel Zhang Date: Wed, 29 Jan 2025 13:58:51 +0800 Subject: [PATCH 2/8] list sessions in lua, sorted by mtime --- lua/nvim-possession/init.lua | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lua/nvim-possession/init.lua b/lua/nvim-possession/init.lua index ba23e89..b4f2ecd 100644 --- a/lua/nvim-possession/init.lua +++ b/lua/nvim-possession/init.lua @@ -126,6 +126,31 @@ M.setup = function(user_opts) return end + local function list_sessions(fzf_cb) + local sessions = {} + for name, type in vim.fs.dir(user_config.sessions.sessions_path, { depth = math.huge }) do + if type == "file" then + local stat = vim.uv.fs_stat(user_config.sessions.sessions_path .. name) + if stat then + table.insert(sessions, { name = name, mtime = stat.mtime }) + end + end + end + table.sort(sessions, function(a, b) + if a.mtime.sec ~= b.mtime.sec then + return a.mtime.sec > b.mtime.sec + end + if a.mtime.nsec ~= b.mtime.nsec then + return a.mtime.nsec > b.mtime.nsec + end + return a.name < b.name + end) + for _, sess in ipairs(sessions) do + fzf_cb(sess.name) + end + fzf_cb() + end + local opts = { user_config = user_config, prompt = user_config.sessions.sessions_icon .. user_config.sessions.sessions_prompt, @@ -140,13 +165,13 @@ M.setup = function(user_opts) cwd = user_config.sessions.sessions_path, actions = { ["enter"] = M.load, - ["ctrl-x"] = { fn = M.delete_selected, reload = true, header = "delete session" }, + ["ctrl-x"] = { M.delete_selected, fzf.actions.resume, header = "delete session" }, ["ctrl-n"] = { fn = M.new, header = "new session" }, }, } opts = require("fzf-lua.config").normalize_opts(opts, {}) opts = require("fzf-lua.core").set_header(opts, { "actions" }) - fzf.fzf_exec("ls -t", opts) + fzf.fzf_exec(list_sessions, opts) end if user_config.autoload and vim.fn.argc() == 0 then From 0f7d9644666c65d222e6e147436765547b3e205d Mon Sep 17 00:00:00 2001 From: Gennaro Tedesco Date: Thu, 30 Jan 2025 19:52:16 +0100 Subject: [PATCH 3/8] feat: add sorting functions to list sessions --- README.md | 24 +++++++++--------------- lua/nvim-possession/config.lua | 4 ++++ lua/nvim-possession/init.lua | 15 +++++---------- lua/nvim-possession/sorting.lua | 24 ++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 lua/nvim-possession/sorting.lua diff --git a/README.md b/README.md index 4b8fc39..3452494 100644 --- a/README.md +++ b/README.md @@ -36,21 +36,12 @@ Install `nvim-possession` with your favourite plugin manager (`fzf-lua` is requi "ibhagwan/fzf-lua", }, config = true, - init = function() - local possession = require("nvim-possession") - vim.keymap.set("n", "sl", function() - possession.list() - end) - vim.keymap.set("n", "sn", function() - possession.new() - end) - vim.keymap.set("n", "su", function() - possession.update() - end) - vim.keymap.set("n", "sd", function() - possession.delete() - end) - end, + keys = { + { "sl", function() require("nvim-possession").list() end, desc = "📌list sessions", }, + { "sn", function() require("nvim-possession").new() end, desc = "📌create new session", }, + { "su", function() require("nvim-possession").update() end, desc = "📌update current session", }, + { "sd", function() require("nvim-possession").delete() end, desc = "📌delete selected session"}, + }, } ``` @@ -110,6 +101,9 @@ require("nvim-possession").setup({ vertical = "right:30%" } } + sort = require("nvim-possession.sorting").alpha_sort -- callback, sorting function to list sessions + -- require("nvim-possession.sorting").time_sort + -- to sort by last updated instead }) ``` diff --git a/lua/nvim-possession/config.lua b/lua/nvim-possession/config.lua index b8fcbfe..86dc185 100644 --- a/lua/nvim-possession/config.lua +++ b/lua/nvim-possession/config.lua @@ -1,3 +1,5 @@ +local sort = require("nvim-possession.sorting") + local M = {} M.sessions = { @@ -46,4 +48,6 @@ M.fzf_winopts = { }, } +M.sort = sort.alpha_sort + return M diff --git a/lua/nvim-possession/init.lua b/lua/nvim-possession/init.lua index b4f2ecd..cfd3f74 100644 --- a/lua/nvim-possession/init.lua +++ b/lua/nvim-possession/init.lua @@ -115,12 +115,12 @@ M.setup = function(user_opts) ---list all existing sessions and their files ---return fzf picker M.list = function() - local iter = vim.loop.fs_scandir(user_config.sessions.sessions_path) + local iter = vim.uv.fs_scandir(user_config.sessions.sessions_path) if iter == nil then print("session folder " .. user_config.sessions.sessions_path .. " does not exist") return end - local next = vim.loop.fs_scandir_next(iter) + local next = vim.uv.fs_scandir_next(iter) if next == nil then print("no saved sessions") return @@ -128,7 +128,7 @@ M.setup = function(user_opts) local function list_sessions(fzf_cb) local sessions = {} - for name, type in vim.fs.dir(user_config.sessions.sessions_path, { depth = math.huge }) do + for name, type in vim.fs.dir(user_config.sessions.sessions_path) do if type == "file" then local stat = vim.uv.fs_stat(user_config.sessions.sessions_path .. name) if stat then @@ -137,13 +137,7 @@ M.setup = function(user_opts) end end table.sort(sessions, function(a, b) - if a.mtime.sec ~= b.mtime.sec then - return a.mtime.sec > b.mtime.sec - end - if a.mtime.nsec ~= b.mtime.nsec then - return a.mtime.nsec > b.mtime.nsec - end - return a.name < b.name + return user_config.sort(a, b) end) for _, sess in ipairs(sessions) do fzf_cb(sess.name) @@ -158,6 +152,7 @@ M.setup = function(user_opts) file_icons = false, git_icons = false, cwd_header = false, + no_header = true, previewer = ui.session_previewer, hls = user_config.fzf_hls, diff --git a/lua/nvim-possession/sorting.lua b/lua/nvim-possession/sorting.lua new file mode 100644 index 0000000..b561acc --- /dev/null +++ b/lua/nvim-possession/sorting.lua @@ -0,0 +1,24 @@ +local M = {} + +---sort sessions by last updated +---@param a table +---@param b table +---@return boolean +M.time_sort = function(a, b) + if a.mtime.sec ~= b.mtime.sec then + return a.mtime.sec > b.mtime.sec + end + if a.mtime.nsec ~= b.mtime.nsec then + return a.mtime.nsec > b.mtime.nsec + end +end + +---sort sessions by name +---@param a table +---@param b table +---@return boolean +M.alpha_sort = function(a, b) + return a.name < b.name +end + +return M From a90395a9c8416823c424f6698172aa4a7603e49f Mon Sep 17 00:00:00 2001 From: gennaro-tedesco Date: Thu, 30 Jan 2025 18:58:13 +0000 Subject: [PATCH 4/8] Auto generate docs --- doc/possession.txt | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/doc/possession.txt b/doc/possession.txt index 78ee2fc..1a6d8e5 100644 --- a/doc/possession.txt +++ b/doc/possession.txt @@ -1,4 +1,4 @@ -*possession.txt* For Neovim >= 0.8.0 Last change: 2024 December 19 +*possession.txt* For Neovim >= 0.8.0 Last change: 2025 January 30 ============================================================================== Table of Contents *possession-table-of-contents* @@ -44,21 +44,12 @@ quickstart configuration is, for instance "ibhagwan/fzf-lua", }, config = true, - init = function() - local possession = require("nvim-possession") - vim.keymap.set("n", "sl", function() - possession.list() - end) - vim.keymap.set("n", "sn", function() - possession.new() - end) - vim.keymap.set("n", "su", function() - possession.update() - end) - vim.keymap.set("n", "sd", function() - possession.delete() - end) - end, + keys = { + { "sl", function() require("nvim-possession").list() end, desc = "📌list sessions", }, + { "sn", function() require("nvim-possession").new() end, desc = "📌create new session", }, + { "su", function() require("nvim-possession").update() end, desc = "📌update current session", }, + { "sd", function() require("nvim-possession").delete() end, desc = "📌delete selected session"}, + }, } < @@ -139,6 +130,9 @@ however if you really want to do so: vertical = "right:30%" } } + sort = require("nvim-possession.sorting").alpha_sort -- callback, sorting function to list sessions + -- require("nvim-possession.sorting").time_sort + -- to sort by last updated instead }) < From b956ae07725fd607482ddea00bca68cedf07f8d8 Mon Sep 17 00:00:00 2001 From: Gennaro Tedesco Date: Thu, 30 Jan 2025 23:53:29 +0100 Subject: [PATCH 5/8] feat: add check for sort attribute to really be a function --- lua/nvim-possession/config.lua | 3 +++ lua/nvim-possession/init.lua | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lua/nvim-possession/config.lua b/lua/nvim-possession/config.lua index 86dc185..05710db 100644 --- a/lua/nvim-possession/config.lua +++ b/lua/nvim-possession/config.lua @@ -16,7 +16,9 @@ M.autoswitch = { exclude_ft = {}, } +---@type function M.save_hook = nil +---@type function M.post_hook = nil ---@class possession.Hls @@ -48,6 +50,7 @@ M.fzf_winopts = { }, } +---@type function M.sort = sort.alpha_sort return M diff --git a/lua/nvim-possession/init.lua b/lua/nvim-possession/init.lua index cfd3f74..4445264 100644 --- a/lua/nvim-possession/init.lua +++ b/lua/nvim-possession/init.lua @@ -1,6 +1,7 @@ local config = require("nvim-possession.config") local ui = require("nvim-possession.ui") local utils = require("nvim-possession.utils") +local sort = require("nvim-possession.sorting") local M = {} @@ -137,7 +138,11 @@ M.setup = function(user_opts) end end table.sort(sessions, function(a, b) - return user_config.sort(a, b) + if type(user_config.sort) == "function" then + return user_config.sort(a, b) + else + return sort.alpha_sort(a, b) + end end) for _, sess in ipairs(sessions) do fzf_cb(sess.name) From acedfb8043e764c015759051e73e2759373de3d1 Mon Sep 17 00:00:00 2001 From: Gennaro Tedesco Date: Fri, 31 Jan 2025 00:54:18 +0100 Subject: [PATCH 6/8] test: add test for time sorting --- lua/nvim-possession/sorting.lua | 1 + lua/spec/sort_spec.lua | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 lua/spec/sort_spec.lua diff --git a/lua/nvim-possession/sorting.lua b/lua/nvim-possession/sorting.lua index b561acc..265d91a 100644 --- a/lua/nvim-possession/sorting.lua +++ b/lua/nvim-possession/sorting.lua @@ -11,6 +11,7 @@ M.time_sort = function(a, b) if a.mtime.nsec ~= b.mtime.nsec then return a.mtime.nsec > b.mtime.nsec end + return M.alpha_sort(a, b) end ---sort sessions by name diff --git a/lua/spec/sort_spec.lua b/lua/spec/sort_spec.lua new file mode 100644 index 0000000..ec13329 --- /dev/null +++ b/lua/spec/sort_spec.lua @@ -0,0 +1,23 @@ +describe("test matching sorting functions", function() + local sort + local sessions, time_sorted_sessions + + setup(function() + sort = require("nvim-possession.sorting") + sessions = { + { name = "aaa", mtime = { sec = 0, nsec = 1 } }, + { name = "zzz", mtime = { sec = 0, nsec = 2 } }, + } + time_sorted_sessions = + { { name = "zzz", mtime = { sec = 0, nsec = 2 } }, { name = "aaa", mtime = { sec = 0, nsec = 1 } } } + end) + + teardown(function() + sort = nil + end) + + it("time sorting", function() + table.sort(sessions, sort.time_sort) + assert.same(time_sorted_sessions, sessions) + end) +end) From 1f3cc3858848e812c40a1b82abef7ab1632559a6 Mon Sep 17 00:00:00 2001 From: Gennaro Tedesco Date: Fri, 31 Jan 2025 16:37:20 +0100 Subject: [PATCH 7/8] fix: improve conventional commit for auto-generated docs --- .github/workflows/panvimdoc.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/panvimdoc.yml b/.github/workflows/panvimdoc.yml index 139bd34..558cd2d 100644 --- a/.github/workflows/panvimdoc.yml +++ b/.github/workflows/panvimdoc.yml @@ -24,5 +24,5 @@ jobs: treesitter: true - uses: stefanzweifel/git-auto-commit-action@v4 with: - commit_message: "Auto generate docs" + commit_message: "docs: auto generate vim documentation" branch: ${{ github.head_ref }} From a47f47ca70e00647a51e6c5320f60f2d6294a1a5 Mon Sep 17 00:00:00 2001 From: gennaro-tedesco Date: Fri, 31 Jan 2025 15:37:46 +0000 Subject: [PATCH 8/8] docs: auto generate vim documentation --- doc/possession.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/possession.txt b/doc/possession.txt index 1a6d8e5..cbd9ae6 100644 --- a/doc/possession.txt +++ b/doc/possession.txt @@ -1,4 +1,4 @@ -*possession.txt* For Neovim >= 0.8.0 Last change: 2025 January 30 +*possession.txt* For Neovim >= 0.8.0 Last change: 2025 January 31 ============================================================================== Table of Contents *possession-table-of-contents*