Skip to content

Commit

Permalink
Merge pull request #51 from gennaro-tedesco/session_sort
Browse files Browse the repository at this point in the history
Session sort
  • Loading branch information
gennaro-tedesco authored Jan 31, 2025
2 parents c6adf12 + a47f47c commit aec03d5
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/panvimdoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<leader>sl", function()
possession.list()
end)
vim.keymap.set("n", "<leader>sn", function()
possession.new()
end)
vim.keymap.set("n", "<leader>su", function()
possession.update()
end)
vim.keymap.set("n", "<leader>sd", function()
possession.delete()
end)
end,
keys = {
{ "<leader>sl", function() require("nvim-possession").list() end, desc = "📌list sessions", },
{ "<leader>sn", function() require("nvim-possession").new() end, desc = "📌create new session", },
{ "<leader>su", function() require("nvim-possession").update() end, desc = "📌update current session", },
{ "<leader>sd", function() require("nvim-possession").delete() end, desc = "📌delete selected session"},
},
}
```

Expand Down Expand Up @@ -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
})
```

Expand Down
26 changes: 10 additions & 16 deletions doc/possession.txt
Original file line number Diff line number Diff line change
@@ -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*
Expand Down Expand Up @@ -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", "<leader>sl", function()
possession.list()
end)
vim.keymap.set("n", "<leader>sn", function()
possession.new()
end)
vim.keymap.set("n", "<leader>su", function()
possession.update()
end)
vim.keymap.set("n", "<leader>sd", function()
possession.delete()
end)
end,
keys = {
{ "<leader>sl", function() require("nvim-possession").list() end, desc = "📌list sessions", },
{ "<leader>sn", function() require("nvim-possession").new() end, desc = "📌create new session", },
{ "<leader>su", function() require("nvim-possession").update() end, desc = "📌update current session", },
{ "<leader>sd", function() require("nvim-possession").delete() end, desc = "📌delete selected session"},
},
}
<

Expand Down Expand Up @@ -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
})
<

Expand Down
7 changes: 7 additions & 0 deletions lua/nvim-possession/config.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local sort = require("nvim-possession.sorting")

local M = {}

M.sessions = {
Expand All @@ -14,7 +16,9 @@ M.autoswitch = {
exclude_ft = {},
}

---@type function
M.save_hook = nil
---@type function
M.post_hook = nil

---@class possession.Hls
Expand Down Expand Up @@ -46,4 +50,7 @@ M.fzf_winopts = {
},
}

---@type function
M.sort = sort.alpha_sort

return M
36 changes: 33 additions & 3 deletions lua/nvim-possession/init.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -125,23 +127,51 @@ 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,
winopts = user_config.fzf_winopts,
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
Expand Down
25 changes: 25 additions & 0 deletions lua/nvim-possession/sorting.lua
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions lua/spec/sort_spec.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit aec03d5

Please sign in to comment.