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 }} 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/doc/possession.txt b/doc/possession.txt index 78ee2fc..cbd9ae6 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 31 ============================================================================== 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 }) < diff --git a/lua/nvim-possession/config.lua b/lua/nvim-possession/config.lua index b8fcbfe..05710db 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 = { @@ -14,7 +16,9 @@ M.autoswitch = { exclude_ft = {}, } +---@type function M.save_hook = nil +---@type function M.post_hook = nil ---@class possession.Hls @@ -46,4 +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 f972ebc..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 = {} @@ -45,6 +46,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,13 +127,37 @@ M.setup = function(user_opts) return end - return fzf.files({ + local function list_sessions(fzf_cb) + local sessions = {} + 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 + table.insert(sessions, { name = name, mtime = stat.mtime }) + end + end + end + table.sort(sessions, function(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) + end + fzf_cb() + end + + local opts = { user_config = user_config, prompt = user_config.sessions.sessions_icon .. user_config.sessions.sessions_prompt, cwd_prompt = false, file_icons = false, git_icons = false, cwd_header = false, + no_header = true, previewer = ui.session_previewer, hls = user_config.fzf_hls, @@ -139,9 +165,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"] = { 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(list_sessions, opts) end if user_config.autoload and vim.fn.argc() == 0 then diff --git a/lua/nvim-possession/sorting.lua b/lua/nvim-possession/sorting.lua new file mode 100644 index 0000000..265d91a --- /dev/null +++ b/lua/nvim-possession/sorting.lua @@ -0,0 +1,25 @@ +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 + return M.alpha_sort(a, b) +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 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)