Skip to content

Commit

Permalink
fix: session dir creation question & change cmdline architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
AbaoFromCUG committed Jan 24, 2024
1 parent 9798187 commit 4828b62
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 46 deletions.
32 changes: 29 additions & 3 deletions lua/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,15 @@ function M.save_session()
M.execute_hooks("post_save")
end

---@class session.Session
---@field work_directory string
---@field session_file string
---@field name? string

---get session list
---@return session.Session[]
function M.get_session_list()
-- local session_files = vim.fn.glob(path.get_session_root() .. "*_.vim", true, true)
local session_files = vim.fs.find(function(name, p)
local session_files = vim.fs.find(function(name)
return name:match("[%w_]+_.vim")
end, { path = path.get_session_root(), limit = math.huge, type = "file" })
local session_list = {}
Expand All @@ -136,7 +142,27 @@ function M.get_session_list()
local filename = vim.fs.basename(session_file)
local name = filename:match("([%w_]+)_.vim")
local raw_path = path.unescape_path(name)
table.insert(session_list, raw_path)
table.insert(session_list, {
work_directory = raw_path,
session_file = session_file,
name = vim.fs.basename(raw_path),
})
end
local find_one = true
while find_one do
find_one = false
for _, sess in ipairs(session_list) do
local same_list = vim.tbl_filter(function(other)
return other.name == sess.name
end, session_list)
if #same_list > 1 then
find_one = true
vim.tbl_map(function(one)
local parent_dir = one.work_directory:sub(1, #one.work_directory - #one.name - 1)
one.name = path.join(vim.fs.basename(parent_dir), one.name)
end, same_list)
end
end
end
return session_list
end
Expand Down
68 changes: 55 additions & 13 deletions lua/session/command.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,61 @@
local session = require("session")
local path = require("session.path")

local M = {
open = function(dir)
local session_file = path.get_session_file(dir)
if vim.fn.filereadable(session_file) then
session.save_session()
session.restore_session(dir)
else
vim.notify(string.format("session file [%s] not exists", session_file), vim.log.levels.ERROR, { title = "Session" })
end
end,
delete = function(dir)
session.delete_session(dir)
end,
local M = {}

M = {
open = {
complete = function()
return vim.tbl_map(function(sess)
return sess.work_directory
end, session.get_session_list())
end,
execute = function(dir)
if dir == nil then
vim.ui.select(session.get_session_list(), {
prompt = "Session open...",
format_item = function(item)
return item.work_directory
end,
}, function(choice)
if choice ~= nil then
M.open.execute(choice.work_directory)
end
end)
return
end
local session_file = path.get_session_file(dir)
if vim.fn.filereadable(session_file) then
session.save_session()
session.restore_session(dir)
else
vim.notify(string.format("session file [%s] not exists", session_file), vim.log.levels.ERROR, { title = "Session" })
end
end,
},
delete = {
complete = function()
return vim.tbl_map(function(sess)
return sess.name
end, session.get_session_list())
end,
execute = function(dir)
if dir == nil then
vim.ui.select(session.get_session_list(), {
prompt = "Session delete...",
format_item = function(item)
return item.work_directory
end,
}, function(choice)
if choice ~= nil then
M.delete.execute(choice.work_directory)
end
end)
return
end
session.delete_session(dir)
end,
},
}

return M
2 changes: 1 addition & 1 deletion lua/session/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end
---@return string path
function M.get_session_root()
local path = M.join(vim.fn.stdpath("state"), "session")
if not vim.fn.isdirectory(path) then
if vim.fn.isdirectory(path) == 0 then
vim.fn.mkdir(path)
end
return path
Expand Down
52 changes: 23 additions & 29 deletions plugin/session.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local session = require("session")
local command = require("session.command")
local cmds = require("session.command")

local group = vim.api.nvim_create_augroup("Session", {})
vim.api.nvim_create_autocmd("StdinReadPre", {
Expand Down Expand Up @@ -30,38 +30,32 @@ vim.api.nvim_create_autocmd("VimLeavePre", {
})

vim.api.nvim_create_user_command("Session", function(opts)
local function run(cmds)
if #cmds < 1 then
vim.ui.select({ "open", "delete" }, { prompt = "Session" }, function(item)
if item then
table.insert(cmds, item)
run(cmds)
end
end)
elseif #cmds == 1 then
vim.ui.select(session.get_session_list(), { prompt = "Session " .. cmds[1] }, function(item)
if item then
table.insert(cmds, item)
run(cmds)
end
end)
elseif #cmds == 2 then
if not command[cmds[1]] then
vim.notify(string.format("Unknown session action [%s]", cmds[1]), vim.log.levels.ERROR, { prompt = "Session" })
return
end
command[cmds[1]](cmds[2])
end
local cmd = cmds[opts.fargs[1]]
if cmd ~= nil then
table.remove(opts.fargs, 1)
cmd.execute(unpack(opts.fargs))
end
run(opts.fargs)
end, {
desc = "Session manager",
desc = "Session/Project Manager",
nargs = "*",
complete = function(_, line, _)
local parts = vim.split(vim.trim(line), "%s+")
if line:match("Session open ") or line:match("Session delete ") then
return session.get_session_list()
local l = vim.split(line, "%s+")
local n = #l - 2
if n == 0 then
return vim.tbl_filter(function(val)
return vim.startswith(val, l[2])
end, vim.tbl_keys(cmds))
elseif n == 1 and cmds[l[2]] ~= nil then
local cmd = cmds[l[2]]
local condition = {}
if type(cmd["complete"]) == "function" then
condition = cmd.complete()
elseif type(cmd["complete"]) == "table" then
condition = cmd.complete --[[@as table]]
end
return vim.tbl_filter(function(val)
return vim.startswith(val, l[3])
end, condition)
end
return { "open", "delete" }
end,
})

0 comments on commit 4828b62

Please sign in to comment.