Update generated neovim config
This commit is contained in:
@ -1,46 +1,35 @@
|
||||
local M = {}
|
||||
return {
|
||||
autostart = true, -- Automatically start the plugin on load?
|
||||
|
||||
local defaults = {
|
||||
log_level = "ERROR", -- One of "TRACE", "DEBUG", "ERROR"
|
||||
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
|
||||
silent = false, -- silent nvim message when sourcing session file
|
||||
-- Function to determine if a session should be saved
|
||||
---@type fun(): boolean
|
||||
should_save = function()
|
||||
return true
|
||||
end,
|
||||
|
||||
use_git_branch = false, -- create session files based on the branch of a git enabled repository
|
||||
branch_separator = "@@", -- string used to separate session directory name from branch name
|
||||
default_branch = "main", -- the branch to load if a session file is not found for the current branch
|
||||
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- Directory where session files are saved
|
||||
|
||||
autosave = true, -- automatically save session files when exiting Neovim
|
||||
should_autosave = nil, -- function to determine if a session should be autosaved (resolve to a boolean)
|
||||
follow_cwd = true, -- Change the session file to match any change in the cwd?
|
||||
use_git_branch = false, -- Include the git branch in the session file name?
|
||||
autoload = false, -- Automatically load the session for the cwd on Neovim startup?
|
||||
|
||||
autoload = false, -- automatically load the session for the cwd on Neovim startup
|
||||
on_autoload_no_session = nil, -- function to run when `autoload = true` but there is no session to load
|
||||
-- Function to run when `autoload = true` but there is no session to load
|
||||
---@type fun(): any
|
||||
on_autoload_no_session = function() end,
|
||||
|
||||
follow_cwd = true, -- change session file name with changes in current working directory
|
||||
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
|
||||
ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading
|
||||
ignored_branches = nil, -- table of branch patterns that are ignored for auto-saving and auto-loading
|
||||
allowed_dirs = {}, -- Table of dirs that the plugin will start and autoload from
|
||||
ignored_dirs = {}, -- Table of dirs that are ignored for starting and autoloading
|
||||
|
||||
telescope = {
|
||||
reset_prompt = true, -- Reset prompt after a telescope action?
|
||||
--TODO: We should add a deprecation notice for the old API here
|
||||
mappings = {
|
||||
change_branch = "<c-b>",
|
||||
copy_session = "<c-c>",
|
||||
delete_session = "<c-d>",
|
||||
mappings = { -- Mappings for managing sessions in Telescope
|
||||
copy_session = "<C-c>",
|
||||
change_branch = "<C-b>",
|
||||
delete_session = "<C-d>",
|
||||
},
|
||||
icons = { -- icons displayed in the picker
|
||||
icons = { -- icons displayed in the Telescope picker
|
||||
selected = " ",
|
||||
dir = " ",
|
||||
branch = " ",
|
||||
dir = " ",
|
||||
selected = " ",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
M.options = {}
|
||||
|
||||
function M.setup(opts)
|
||||
M.options = vim.tbl_deep_extend("force", defaults, opts or {})
|
||||
vim.fn.mkdir(M.options.save_dir, "p")
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@ -1,38 +0,0 @@
|
||||
---[[
|
||||
--Courtesy of the awesome work in Nightfox.nvim
|
||||
--https://github.com/EdenEast/nightfox.nvim/blob/main/lua/nightfox/lib/deprecation.lua
|
||||
--]
|
||||
local M = {
|
||||
_list = { { "[Persisted.nvim]\n", "Question" }, { "The following have been " }, { "deprecated:\n", "WarningMsg" } },
|
||||
_has_registered = false,
|
||||
}
|
||||
|
||||
function M.write(...)
|
||||
for _, e in ipairs({ ... }) do
|
||||
table.insert(M._list, type(e) == "string" and { e } or e)
|
||||
end
|
||||
|
||||
M._list[#M._list][1] = M._list[#M._list][1] .. "\n"
|
||||
|
||||
if not M._has_registered then
|
||||
vim.cmd([[
|
||||
augroup PersistedDeprecations
|
||||
au!
|
||||
autocmd VimEnter * ++once lua require("persisted.deprecate").flush()
|
||||
augroup END
|
||||
]])
|
||||
M._has_registered = true
|
||||
end
|
||||
end
|
||||
|
||||
function M.flush()
|
||||
M.write(
|
||||
"----------\n",
|
||||
"See ",
|
||||
{ "https://github.com/olimorris/persisted.nvim/issues/51", "Title" },
|
||||
" for more information."
|
||||
)
|
||||
vim.api.nvim_echo(M._list, true, {})
|
||||
end
|
||||
|
||||
return M
|
||||
@ -1,413 +1,245 @@
|
||||
local config = require("persisted.config")
|
||||
local log = require("persisted.log")
|
||||
local utils = require("persisted.utils")
|
||||
|
||||
local M = {}
|
||||
|
||||
local config
|
||||
local e = vim.fn.fnameescape
|
||||
local uv = vim.uv or vim.loop
|
||||
|
||||
---Escapes special characters before performing string substitution
|
||||
---@param str string
|
||||
---@param pattern string
|
||||
---@param replace string
|
||||
---@param n? integer
|
||||
---@return string
|
||||
---@return integer
|
||||
local function escape_pattern(str, pattern, replace, n)
|
||||
pattern = string.gsub(pattern, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") -- escape pattern
|
||||
replace = string.gsub(replace, "[%%]", "%%%%") -- escape replacement
|
||||
|
||||
return string.gsub(str, pattern, replace, n)
|
||||
end
|
||||
|
||||
---Gets the directory from the file/path argument passed to Neovim if there's
|
||||
---exactly one and it resolves to a valid directory
|
||||
---@return string|nil
|
||||
local function args_path()
|
||||
if vim.fn.argc() ~= 1 then
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Use expand() to resolve '~' and use fs_realpath to resolve both '.' and
|
||||
-- relative paths passed as arguments. Note that argv() will only ever return
|
||||
-- paths/files passed as arguments and does not include other
|
||||
-- parameters/arguments. fs_realpath() returns nil if the path doesn't exist.
|
||||
-- Use isdirectory to validate it's a directory and not a file.
|
||||
local dir = vim.loop.fs_realpath(vim.fn.expand(vim.fn.argv(0)))
|
||||
if dir ~= nil and vim.fn.isdirectory(dir) ~= 0 then
|
||||
return dir
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
---Check any arguments passed to Neovim and verify if they're a directory
|
||||
---@return boolean
|
||||
local function args_check()
|
||||
-- Args are valid if a single directory was resolved or if no args were used.
|
||||
return args_path() ~= nil or vim.fn.argc() == 0
|
||||
end
|
||||
|
||||
---Get the directory to be used for the session
|
||||
---@return string
|
||||
local function session_dir()
|
||||
-- Use specified directory from arguments or the working directory otherwise.
|
||||
return args_path() or vim.fn.getcwd()
|
||||
end
|
||||
|
||||
---Does the current working directory allow for the auto-saving and loading?
|
||||
---@param dir string Directory to be used for the session
|
||||
---@return boolean
|
||||
local function allow_dir(dir)
|
||||
local allowed_dirs = config.options.allowed_dirs
|
||||
|
||||
if allowed_dirs == nil then
|
||||
return true
|
||||
end
|
||||
|
||||
return utils.dirs_match(dir, allowed_dirs)
|
||||
end
|
||||
|
||||
---Is the current working directory ignored for auto-saving and loading?
|
||||
---@param dir string Directory to be used for the session
|
||||
---@return boolean
|
||||
local function ignore_dir(dir)
|
||||
local ignored_dirs = config.options.ignored_dirs
|
||||
|
||||
if ignored_dirs == nil then
|
||||
return false
|
||||
end
|
||||
|
||||
return utils.dirs_match(dir, ignored_dirs)
|
||||
end
|
||||
|
||||
---Is the current branch ignored for auto-saving and loading?
|
||||
---@param branch? string Branch to be used for the session
|
||||
---@return boolean
|
||||
local function ignore_branch(branch)
|
||||
local ignored_branches = config.options.ignored_branches
|
||||
|
||||
if not branch or ignored_branches == nil then
|
||||
return false
|
||||
end
|
||||
|
||||
return utils.table_match(branch, ignored_branches) ~= nil
|
||||
end
|
||||
|
||||
---Get the session that was saved last
|
||||
---@return string
|
||||
local function get_last()
|
||||
local sessions = vim.fn.glob(config.options.save_dir .. "*.vim", true, true)
|
||||
|
||||
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
|
||||
|
||||
---Get the current Git branch name, untouched
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return string|nil
|
||||
local function get_branchname(dir)
|
||||
dir = dir or session_dir()
|
||||
vim.fn.system('git -C "' .. dir .. '" rev-parse 2>/dev/null')
|
||||
|
||||
local git_enabled = (vim.v.shell_error == 0)
|
||||
|
||||
if git_enabled then
|
||||
local git_branch = vim.fn.systemlist('git -C "' .. dir .. '" rev-parse --abbrev-ref HEAD 2>/dev/null')
|
||||
return git_branch[1]
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
---Get the current Git branch
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return string|nil
|
||||
function M.get_branch(dir)
|
||||
dir = dir or session_dir()
|
||||
|
||||
if config.options.use_git_branch then
|
||||
vim.fn.system('git -C "' .. dir .. '" rev-parse 2>/dev/null')
|
||||
|
||||
local git_enabled = (vim.v.shell_error == 0)
|
||||
|
||||
if git_enabled then
|
||||
local git_branch = vim.fn.systemlist('git -C "' .. dir .. '" rev-parse --abbrev-ref HEAD 2>/dev/null')
|
||||
|
||||
if vim.v.shell_error == 0 then
|
||||
local branch = config.options.branch_separator .. git_branch[1]:gsub("/", "%%")
|
||||
local branch_session = config.options.save_dir .. dir:gsub(utils.get_dir_pattern(), "%%") .. branch .. ".vim"
|
||||
|
||||
-- Try to load the session for the current branch
|
||||
if vim.fn.filereadable(branch_session) ~= 0 then
|
||||
return branch
|
||||
else
|
||||
log:debug("Trying to load a session for branch: %s", config.options.default_branch)
|
||||
log:error("Could not load a session for branch: %s", git_branch[1])
|
||||
vim.g.persisted_branch_session = branch_session
|
||||
return config.options.branch_separator .. config.options.default_branch
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
---Fire an event
|
||||
---@param event string
|
||||
function M.fire(event)
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "Persisted" .. event })
|
||||
end
|
||||
|
||||
---Get the current session for the current working directory and git branch
|
||||
---@param dir string Directory to be used for the session
|
||||
---@param opts? {branch?: boolean}
|
||||
---@return string
|
||||
local function get_current(dir)
|
||||
local name = dir:gsub(utils.get_dir_pattern(), "%%")
|
||||
local branch = M.get_branch(dir)
|
||||
function M.current(opts)
|
||||
opts = opts or {}
|
||||
local name = utils.make_fs_safe(vim.fn.getcwd())
|
||||
|
||||
return config.options.save_dir .. name .. (branch or "") .. ".vim"
|
||||
if config.use_git_branch and opts.branch ~= false then
|
||||
local branch = M.branch()
|
||||
if branch then
|
||||
branch = utils.make_fs_safe(branch)
|
||||
name = name .. "@@" .. branch
|
||||
end
|
||||
end
|
||||
|
||||
return config.save_dir .. name .. ".vim"
|
||||
end
|
||||
|
||||
---Determine if a session for the current wording directory, exists
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return boolean
|
||||
function M.session_exists(dir)
|
||||
dir = dir or session_dir()
|
||||
---Automatically load the session for the current dir
|
||||
---@param opts? { force?: boolean }
|
||||
function M.autoload(opts)
|
||||
opts = opts or {}
|
||||
|
||||
return vim.fn.filereadable(get_current(dir)) ~= 0
|
||||
end
|
||||
if not opts.force and (vim.fn.argc() > 0 or vim.g.started_with_stdin) then
|
||||
return
|
||||
end
|
||||
|
||||
---Setup the plugin
|
||||
---@param opts? table
|
||||
---@return nil
|
||||
function M.setup(opts)
|
||||
config.setup(opts)
|
||||
|
||||
log.set_root(log.new({
|
||||
handlers = {
|
||||
{
|
||||
type = "echo",
|
||||
level = vim.log.levels.WARN,
|
||||
},
|
||||
{
|
||||
type = "file",
|
||||
filename = "persisted.log",
|
||||
level = vim.log.levels[config.options.log_level],
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
local dir = session_dir()
|
||||
local branch = get_branchname()
|
||||
|
||||
if
|
||||
config.options.autosave
|
||||
and (allow_dir(dir) and not ignore_dir(dir) and vim.g.persisting == nil)
|
||||
and not ignore_branch(branch)
|
||||
and args_check()
|
||||
then
|
||||
log:trace("Starting session")
|
||||
M.start()
|
||||
if config.autoload and M.allowed_dir() then
|
||||
M.load({ autoload = true })
|
||||
end
|
||||
end
|
||||
|
||||
---Load a session
|
||||
---@param opt? table
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return nil
|
||||
function M.load(opt, dir)
|
||||
opt = opt or {}
|
||||
dir = dir or session_dir()
|
||||
---@param opts? { last?: boolean, autoload?: boolean, session?: string }
|
||||
function M.load(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local branch
|
||||
local session
|
||||
if opt.session then
|
||||
session = opt.session
|
||||
local session_data = utils.make_session_data(session)
|
||||
branch = session_data and session_data.branch or ""
|
||||
log:trace("Session branch %s", branch)
|
||||
log:trace("Session data %s", session_data)
|
||||
if not branch then
|
||||
vim.notify(string.format("[Persisted.nvim]: Invalid session file %s", session), vim.log.levels.WARN)
|
||||
end
|
||||
|
||||
if opts.last then
|
||||
session = M.last()
|
||||
elseif opts.session then
|
||||
session = opts.session
|
||||
else
|
||||
branch = get_branchname()
|
||||
session = opt.last and get_last() or get_current(dir)
|
||||
log:trace("Session branch: %s", branch)
|
||||
end
|
||||
|
||||
if session then
|
||||
if vim.fn.filereadable(session) ~= 0 then
|
||||
vim.g.persisting_session = not config.options.follow_cwd and session or nil
|
||||
log:trace("Session load: %s", session)
|
||||
utils.load_session(session, config.options.silent)
|
||||
elseif type(config.options.on_autoload_no_session) == "function" then
|
||||
log:trace("No session to load")
|
||||
config.options.on_autoload_no_session()
|
||||
session = M.current()
|
||||
if vim.fn.filereadable(session) == 0 then
|
||||
session = M.current({ branch = false })
|
||||
end
|
||||
end
|
||||
|
||||
dir = session_dir()
|
||||
if config.options.autosave and (allow_dir(dir) and not ignore_dir(dir)) and not ignore_branch(branch) then
|
||||
if session and vim.fn.filereadable(session) ~= 0 then
|
||||
vim.g.persisting_session = not config.follow_cwd and session or nil
|
||||
vim.g.persisted_loaded_session = session
|
||||
M.fire("LoadPre")
|
||||
vim.cmd("silent! source " .. e(session))
|
||||
M.fire("LoadPost")
|
||||
elseif opts.autoload and type(config.on_autoload_no_session) == "function" then
|
||||
config.on_autoload_no_session()
|
||||
end
|
||||
|
||||
if config.autostart and M.allowed_dir() then
|
||||
M.start()
|
||||
end
|
||||
end
|
||||
|
||||
---Automatically load the session for the current dir
|
||||
---@return nil
|
||||
function M.autoload()
|
||||
local dir = session_dir()
|
||||
local branch = get_branchname()
|
||||
|
||||
if config.options.autoload and args_check() then
|
||||
if allow_dir(dir) and not ignore_dir(dir) and not ignore_branch(branch) then
|
||||
log:trace("Autoloading from %s", dir)
|
||||
M.load({}, dir)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Start recording a session
|
||||
---@return nil
|
||||
---Start a session
|
||||
function M.start()
|
||||
vim.api.nvim_create_autocmd("VimLeavePre", {
|
||||
group = vim.api.nvim_create_augroup("Persisted", { clear = true }),
|
||||
callback = function()
|
||||
M.save()
|
||||
end,
|
||||
})
|
||||
|
||||
vim.g.persisting = true
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedStateChange", data = { action = "start" } })
|
||||
M.fire("Start")
|
||||
end
|
||||
|
||||
---Stop recording a session
|
||||
---@return nil
|
||||
---Stop a session
|
||||
function M.stop()
|
||||
vim.g.persisting = false
|
||||
vim.g.persisting_session = nil
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedStateChange", data = { action = "stop" } })
|
||||
end
|
||||
|
||||
---Write the session to disk
|
||||
---@param session string
|
||||
---@return nil
|
||||
local function write(session)
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePre" })
|
||||
vim.cmd("mks! " .. e(session))
|
||||
vim.g.persisting = true
|
||||
log:trace("Session saved")
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePost" })
|
||||
pcall(vim.api.nvim_del_augroup_by_name, "Persisted")
|
||||
M.fire("Stop")
|
||||
end
|
||||
|
||||
---Save the session
|
||||
---@param opt? table
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return nil
|
||||
function M.save(opt, dir)
|
||||
opt = opt or {}
|
||||
dir = dir or session_dir()
|
||||
---@param opts? { force?: boolean, session?: string }
|
||||
function M.save(opts)
|
||||
opts = opts or {}
|
||||
|
||||
if not opt.session then
|
||||
-- Do not save the session if the user has manually stopped it...unless it's forced
|
||||
if (vim.g.persisting == false or vim.g.persisting == nil) and not opt.force then
|
||||
return
|
||||
end
|
||||
|
||||
-- Do not save the session if autosave is turned off...unless it's forced
|
||||
if not config.options.autosave and not opt.force then
|
||||
return
|
||||
end
|
||||
|
||||
-- Do not save the session if the callback returns false...unless it's forced
|
||||
if
|
||||
not opt.force
|
||||
and type(config.options.should_autosave) == "function"
|
||||
and not config.options.should_autosave()
|
||||
then
|
||||
return
|
||||
end
|
||||
-- Do not save the session if should_save evals to false...unless it's forced
|
||||
if type(config.should_save) == "function" and not config.should_save() and not opts.force then
|
||||
return
|
||||
end
|
||||
|
||||
local session = opt.session or (vim.g.persisted_branch_session or vim.g.persisting_session or get_current(dir))
|
||||
write(session)
|
||||
M.fire("SavePre")
|
||||
vim.cmd("mks! " .. e(opts.session or vim.g.persisting_session or M.current()))
|
||||
vim.cmd("sleep 10m")
|
||||
M.fire("SavePost")
|
||||
end
|
||||
|
||||
---Delete the current session
|
||||
---@param dir? string Directory to be used for the session
|
||||
---@return nil
|
||||
function M.delete(dir)
|
||||
dir = dir or session_dir()
|
||||
local session = get_current(dir)
|
||||
|
||||
if session and vim.loop.fs_stat(session) ~= 0 then
|
||||
local session_data = utils.make_session_data(session)
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedDeletePre", data = session_data })
|
||||
---@param opts? { session?: string }
|
||||
function M.delete(opts)
|
||||
opts = opts or {}
|
||||
local session = opts.session or M.current()
|
||||
|
||||
if session and uv.fs_stat(session) ~= 0 then
|
||||
M.fire("DeletePre")
|
||||
vim.schedule(function()
|
||||
log:trace("Deleting session %s", session)
|
||||
M.stop()
|
||||
vim.fn.system("rm " .. e(session))
|
||||
vim.fn.delete(vim.fn.expand(session))
|
||||
end)
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedDeletePost", data = session_data })
|
||||
M.fire("DeletePost")
|
||||
end
|
||||
end
|
||||
|
||||
---Get the current Git branch
|
||||
---@return string?
|
||||
function M.branch()
|
||||
if uv.fs_stat(".git") then
|
||||
local branch = vim.fn.systemlist("git branch --show-current")[1]
|
||||
return vim.v.shell_error == 0 and branch or nil
|
||||
end
|
||||
end
|
||||
|
||||
---Select a session to load
|
||||
function M.select()
|
||||
---@type { session: string, dir: string, branch?: string }[]
|
||||
local items = {}
|
||||
local found = {} ---@type table<string, boolean>
|
||||
for _, session in ipairs(M.list()) do
|
||||
if uv.fs_stat(session) then
|
||||
local file = session:sub(#M.config.save_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 found[dir .. (branch or "")] then
|
||||
found[dir .. (branch or "")] = 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)
|
||||
local name = vim.fn.fnamemodify(item.dir, ":p:~")
|
||||
if item.branch then
|
||||
name = name .. " (" .. item.branch .. ")"
|
||||
end
|
||||
return name
|
||||
end,
|
||||
}, function(item)
|
||||
if item then
|
||||
vim.fn.chdir(item.dir)
|
||||
M.load()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---Determines whether to load, start or stop a session
|
||||
---@param dir? string The directory whose associated session saving should be toggled. If not set, the current working directory is used.
|
||||
---@return nil
|
||||
function M.toggle(dir)
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedToggled" })
|
||||
|
||||
dir = dir or session_dir()
|
||||
|
||||
function M.toggle()
|
||||
M.fire("Toggle")
|
||||
if vim.g.persisting == nil then
|
||||
log:trace("Toggling load")
|
||||
return M.load({}, dir)
|
||||
return M.load()
|
||||
end
|
||||
|
||||
if vim.g.persisting then
|
||||
log:trace("Toggling stop")
|
||||
return M.stop()
|
||||
end
|
||||
|
||||
log:trace("Toggling start")
|
||||
return M.start()
|
||||
end
|
||||
|
||||
---List all of the sessions
|
||||
---@return table
|
||||
---Allow autosaving and autoloading for the given dir?
|
||||
---@param opts? {dir?: string}
|
||||
---@return boolean
|
||||
function M.allowed_dir(opts)
|
||||
opts = opts or {}
|
||||
local dir = opts.dir or vim.fn.getcwd()
|
||||
|
||||
return (next(config.allowed_dirs) and utils.dirs_match(dir, config.allowed_dirs) or true)
|
||||
and not (next(config.ignored_dirs) and utils.dirs_match(dir, config.ignored_dirs) or false)
|
||||
end
|
||||
|
||||
---Get an ordered list of sessions, sorted by modified time
|
||||
---@return string[]
|
||||
function M.list()
|
||||
local save_dir = config.options.save_dir
|
||||
local session_files = vim.fn.glob(save_dir .. "*.vim", true, true)
|
||||
local branch_separator = config.options.branch_separator
|
||||
local dir_separator = utils.get_dir_pattern()
|
||||
local sessions = vim.fn.glob(config.save_dir .. "*.vim", true, true)
|
||||
|
||||
local sessions = {}
|
||||
for _, session in pairs(session_files) do
|
||||
local session_name = escape_pattern(session, save_dir, "")
|
||||
:gsub("%%", dir_separator)
|
||||
:gsub(vim.fn.expand("~"), dir_separator)
|
||||
:gsub("//", "")
|
||||
:sub(1, -5)
|
||||
|
||||
if vim.fn.has("win32") == 1 then
|
||||
-- format drive letter (no trailing separator)
|
||||
session_name = escape_pattern(session_name, dir_separator, ":", 1)
|
||||
-- format remaining filepath separator(s)
|
||||
session_name = escape_pattern(session_name, dir_separator, "\\")
|
||||
end
|
||||
|
||||
local branch, dir_path
|
||||
|
||||
if string.find(session_name, branch_separator, 1, true) then
|
||||
local splits = vim.split(session_name, branch_separator, { plain = true })
|
||||
branch = table.remove(splits, #splits)
|
||||
dir_path = vim.fn.join(splits, branch_separator)
|
||||
else
|
||||
dir_path = session_name
|
||||
end
|
||||
|
||||
table.insert(sessions, {
|
||||
["name"] = session_name,
|
||||
["file_path"] = session,
|
||||
["branch"] = branch,
|
||||
["dir_path"] = dir_path,
|
||||
})
|
||||
end
|
||||
table.sort(sessions, function(a, b)
|
||||
return uv.fs_stat(a).mtime.sec > uv.fs_stat(b).mtime.sec
|
||||
end)
|
||||
|
||||
return sessions
|
||||
end
|
||||
|
||||
---Get the last session that was saved
|
||||
---@return string
|
||||
function M.last()
|
||||
return M.list()[1]
|
||||
end
|
||||
|
||||
---Setup the plugin
|
||||
---@param opts? table
|
||||
function M.setup(opts)
|
||||
-- Account for old config options
|
||||
if opts and opts.autosave then
|
||||
opts.autostart = opts.autosave
|
||||
end
|
||||
if opts and opts.should_autosave then
|
||||
opts.should_save = opts.should_autosave
|
||||
end
|
||||
if opts and opts.allowed_dirs == nil then
|
||||
opts.allowed_dirs = {}
|
||||
end
|
||||
if opts and opts.ignored_dirs == nil then
|
||||
opts.ignored_dirs = {}
|
||||
end
|
||||
|
||||
config = vim.tbl_deep_extend("force", require("persisted.config"), opts or {})
|
||||
M.config = config
|
||||
|
||||
vim.fn.mkdir(config.save_dir, "p")
|
||||
|
||||
if config.autostart and M.allowed_dir() and vim.g.persisting == nil then
|
||||
M.start()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@ -1,280 +0,0 @@
|
||||
---@type boolean
|
||||
local is_windows = vim.loop.os_uname().version:match("Windows")
|
||||
|
||||
---@type string
|
||||
local sep = is_windows and "\\" or "/"
|
||||
|
||||
---@return string
|
||||
local join = function(...)
|
||||
local joined = table.concat({ ... }, sep)
|
||||
if is_windows then
|
||||
joined = joined:gsub("\\\\+", "\\")
|
||||
else
|
||||
joined = joined:gsub("//+", "/")
|
||||
end
|
||||
return joined
|
||||
end
|
||||
|
||||
---@class LogHandler
|
||||
---@field type string
|
||||
---@field level integer
|
||||
---@field formatter? fun(level: integer, msg: string, ...: any)
|
||||
---@field handle? fun(level: integer, text: string)
|
||||
local LogHandler = {}
|
||||
|
||||
local levels_reverse = {}
|
||||
for k, v in pairs(vim.log.levels) do
|
||||
levels_reverse[v] = k
|
||||
end
|
||||
|
||||
function LogHandler.new(opts)
|
||||
vim.validate({
|
||||
type = { opts.type, "s" },
|
||||
handle = { opts.handle, "f" },
|
||||
formatter = { opts.formatter, "f" },
|
||||
level = { opts.level, "n", true },
|
||||
})
|
||||
return setmetatable({
|
||||
type = opts.type,
|
||||
handle = opts.handle,
|
||||
formatter = opts.formatter,
|
||||
level = opts.level or vim.log.levels.INFO,
|
||||
}, { __index = LogHandler })
|
||||
end
|
||||
|
||||
function LogHandler:log(level, msg, ...)
|
||||
if self.level <= level then
|
||||
local text = self.formatter(level, msg, ...)
|
||||
self.handle(level, text)
|
||||
end
|
||||
end
|
||||
|
||||
local function default_formatter(level, msg, ...)
|
||||
local args = vim.F.pack_len(...)
|
||||
for i = 1, args.n do
|
||||
local v = args[i]
|
||||
if type(v) == "table" then
|
||||
args[i] = vim.inspect(v)
|
||||
elseif v == nil then
|
||||
args[i] = "nil"
|
||||
end
|
||||
end
|
||||
local ok, text = pcall(string.format, msg, vim.F.unpack_len(args))
|
||||
if ok then
|
||||
local str_level = levels_reverse[level]
|
||||
return string.format("[%s] %s\n%s", str_level, os.date("%Y-%m-%d %H:%M:%S"), text)
|
||||
else
|
||||
return string.format("[ERROR] error formatting log line: '%s' args %s", msg, vim.inspect(args))
|
||||
end
|
||||
end
|
||||
|
||||
---@param opts table
|
||||
---@return LogHandler
|
||||
local function create_file_handler(opts)
|
||||
vim.validate({
|
||||
filename = { opts.filename, "s" },
|
||||
})
|
||||
local ok, stdpath = pcall(vim.fn.stdpath, "log")
|
||||
if not ok then
|
||||
stdpath = vim.fn.stdpath("cache")
|
||||
end
|
||||
local filepath = join(stdpath, opts.filename)
|
||||
local logfile, openerr = io.open(filepath, "a+")
|
||||
if not logfile then
|
||||
local err_msg = string.format("Failed to open the CodeCompanion log file: %s", openerr)
|
||||
vim.notify(err_msg, vim.log.levels.ERROR)
|
||||
opts.handle = function() end
|
||||
else
|
||||
opts.handle = function(level, text)
|
||||
logfile:write(text)
|
||||
logfile:write("\n")
|
||||
logfile:flush()
|
||||
end
|
||||
end
|
||||
return LogHandler.new(opts)
|
||||
end
|
||||
|
||||
---@param opts table
|
||||
---@return LogHandler
|
||||
local function create_notify_handler(opts)
|
||||
opts.handle = function(level, text)
|
||||
vim.notify(text, level)
|
||||
end
|
||||
return LogHandler.new(opts)
|
||||
end
|
||||
|
||||
---@param opts table
|
||||
---@return LogHandler
|
||||
local function create_echo_handler(opts)
|
||||
opts.handle = function(level, text)
|
||||
local hl = "Normal"
|
||||
if level == vim.log.levels.ERROR then
|
||||
hl = "DiagnosticError"
|
||||
elseif level == vim.log.levels.WARN then
|
||||
hl = "DiagnosticWarn"
|
||||
end
|
||||
vim.api.nvim_echo({ { text, hl } }, true, {})
|
||||
end
|
||||
return LogHandler.new(opts)
|
||||
end
|
||||
|
||||
---@return LogHandler
|
||||
local function create_null_handler()
|
||||
return LogHandler.new({
|
||||
formatter = function() end,
|
||||
handle = function() end,
|
||||
})
|
||||
end
|
||||
|
||||
---@param opts table
|
||||
---@return LogHandler
|
||||
local function create_handler(opts)
|
||||
vim.validate({
|
||||
type = { opts.type, "s" },
|
||||
})
|
||||
if not opts.formatter then
|
||||
opts.formatter = default_formatter
|
||||
end
|
||||
if opts.type == "file" then
|
||||
return create_file_handler(opts)
|
||||
elseif opts.type == "notify" then
|
||||
return create_notify_handler(opts)
|
||||
elseif opts.type == "echo" then
|
||||
return create_echo_handler(opts)
|
||||
else
|
||||
vim.notify(string.format("Unknown log handler %s", opts.type), vim.log.levels.ERROR)
|
||||
return create_null_handler()
|
||||
end
|
||||
end
|
||||
|
||||
---@class Logger
|
||||
---@field handlers LogHandler[]
|
||||
local Logger = {}
|
||||
|
||||
---@class LoggerArgs
|
||||
---@field handlers LogHandler[]
|
||||
---@field level nil|integer
|
||||
|
||||
---@param opts LoggerArgs
|
||||
function Logger.new(opts)
|
||||
vim.validate({
|
||||
handlers = { opts.handlers, "t" },
|
||||
level = { opts.level, "n", true },
|
||||
})
|
||||
local handlers = {}
|
||||
for _, defn in ipairs(opts.handlers) do
|
||||
table.insert(handlers, create_handler(defn))
|
||||
end
|
||||
local log = setmetatable({
|
||||
handlers = handlers,
|
||||
}, { __index = Logger })
|
||||
if opts.level then
|
||||
log:set_level(opts.level)
|
||||
end
|
||||
return log
|
||||
end
|
||||
|
||||
---@param level integer
|
||||
function Logger:set_level(level)
|
||||
for _, handler in ipairs(self.handlers) do
|
||||
handler.level = level
|
||||
end
|
||||
end
|
||||
|
||||
---@return LogHandler[]
|
||||
function Logger:get_handlers()
|
||||
return self.handlers
|
||||
end
|
||||
|
||||
---@param level integer
|
||||
---@param msg string
|
||||
---@param ... any[]
|
||||
function Logger:log(level, msg, ...)
|
||||
for _, handler in ipairs(self.handlers) do
|
||||
handler:log(level, msg, ...)
|
||||
end
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:trace(msg, ...)
|
||||
self:log(vim.log.levels.TRACE, msg, ...)
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:debug(msg, ...)
|
||||
self:log(vim.log.levels.DEBUG, msg, ...)
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:info(msg, ...)
|
||||
self:log(vim.log.levels.INFO, msg, ...)
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:warn(msg, ...)
|
||||
self:log(vim.log.levels.WARN, msg, ...)
|
||||
end
|
||||
|
||||
---@param msg string
|
||||
---@param ... any
|
||||
function Logger:error(msg, ...)
|
||||
self:log(vim.log.levels.ERROR, msg, ...)
|
||||
end
|
||||
|
||||
---@generic T : any
|
||||
---@param cb T
|
||||
---@param message nil|string
|
||||
---@return T
|
||||
function Logger:wrap_cb(cb, message)
|
||||
return function(err, ...)
|
||||
if err then
|
||||
self:error(message or "Error: %s", err)
|
||||
end
|
||||
return cb(err, ...)
|
||||
end
|
||||
end
|
||||
|
||||
local root = Logger.new({
|
||||
handlers = {
|
||||
{
|
||||
type = "echo",
|
||||
level = vim.log.levels.WARN,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
---@class Logger
|
||||
local M = {}
|
||||
|
||||
M.new = Logger.new
|
||||
|
||||
M.get_logfile = function()
|
||||
local ok, stdpath = pcall(vim.fn.stdpath, "log")
|
||||
if not ok then
|
||||
stdpath = vim.fn.stdpath("cache")
|
||||
end
|
||||
|
||||
return join(stdpath, "persisted.log")
|
||||
end
|
||||
|
||||
---@param logger Logger
|
||||
M.set_root = function(logger)
|
||||
root = logger
|
||||
end
|
||||
|
||||
---@return Logger
|
||||
M.get_root = function()
|
||||
return root
|
||||
end
|
||||
|
||||
setmetatable(M, {
|
||||
__index = function(_, key)
|
||||
return root[key]
|
||||
end,
|
||||
})
|
||||
|
||||
return M
|
||||
@ -1,118 +1,15 @@
|
||||
local M = {}
|
||||
local e = vim.fn.fnameescape
|
||||
local fp_sep = vim.loop.os_uname().sysname:lower():match("windows") and "\\" or "/" -- \ for windows, mac and linux both use \
|
||||
|
||||
---Print an error message
|
||||
--@param msg string
|
||||
--@param error string
|
||||
--@return string
|
||||
local function echoerr(msg, error)
|
||||
vim.api.nvim_echo({
|
||||
{ "[Persisted.nvim]: ", "ErrorMsg" },
|
||||
{ msg, "WarningMsg" },
|
||||
{ error, "Normal" },
|
||||
}, true, {})
|
||||
end
|
||||
|
||||
--- Escape special pattern matching characters in a string
|
||||
---@param input string
|
||||
---@return string
|
||||
local function escape_pattern(input)
|
||||
local magic_chars = { "%", "(", ")", ".", "+", "-", "*", "?", "[", "^", "$" }
|
||||
|
||||
for _, char in ipairs(magic_chars) do
|
||||
input = input:gsub("%" .. char, "%%" .. char)
|
||||
end
|
||||
|
||||
return input
|
||||
end
|
||||
|
||||
---Form a table of session data
|
||||
---@param session string
|
||||
---@return table|nil
|
||||
function M.make_session_data(session)
|
||||
local config = require("persisted.config").options
|
||||
|
||||
local home = os.getenv("HOME") or os.getenv("USERPROFILE") or ""
|
||||
|
||||
-- Split the session string into path and branch parts
|
||||
local separator_index = session:find(config.branch_separator)
|
||||
local branch = ""
|
||||
if separator_index then
|
||||
branch = session:sub(separator_index + 2):gsub("%.vim$", ""):gsub("%%", "/")
|
||||
end
|
||||
|
||||
-- Removing the home directory from the path and cleaning leading `/`
|
||||
local name = session:gsub(config.save_dir, ""):gsub("%%", "/"):gsub(home, "")
|
||||
-- Remove the .vim extension
|
||||
name = name:sub(1, #name - 4)
|
||||
if name:sub(1, 1) == "/" then
|
||||
name = name:sub(2)
|
||||
end
|
||||
|
||||
local dir_path = name:gsub(branch, ""):gsub(config.branch_separator, ""):gsub(home, "")
|
||||
|
||||
return {
|
||||
name = name,
|
||||
dir_path = dir_path,
|
||||
file_path = session,
|
||||
branch = branch,
|
||||
}
|
||||
end
|
||||
|
||||
--- Get the last element in a table
|
||||
---@param table table
|
||||
---@return string
|
||||
function M.get_last_item(table)
|
||||
local last
|
||||
for _, _ in pairs(table) do
|
||||
last = #table - 0
|
||||
end
|
||||
return table[last]
|
||||
end
|
||||
|
||||
---Check if a target directory exists in a given table
|
||||
---@param dir string
|
||||
---@param dirs_table table
|
||||
---@return boolean
|
||||
function M.dirs_match(dir, dirs_table)
|
||||
dir = vim.fn.expand(dir)
|
||||
return M.table_match(dir, dirs_table, function(pattern)
|
||||
return escape_pattern(vim.fn.expand(pattern))
|
||||
end)
|
||||
end
|
||||
|
||||
---Check if a string matches and entry in a given table
|
||||
---@param needle string
|
||||
---@param heystack table
|
||||
---@return boolean
|
||||
function M.table_match(needle, heystack, escape_fct)
|
||||
if needle == nil then
|
||||
return false
|
||||
end
|
||||
return heystack
|
||||
and next(vim.tbl_filter(function(pattern)
|
||||
if pattern.exact then
|
||||
-- The pattern is actually a table
|
||||
pattern = pattern[1]
|
||||
-- Stripping off the trailing backslash that a user might put here,
|
||||
-- but only if we aren't looking at the root directory
|
||||
if pattern:sub(-1) == fp_sep and pattern:len() > 1 then
|
||||
pattern = pattern:sub(1, -2)
|
||||
end
|
||||
return needle == pattern
|
||||
else
|
||||
if escape_fct then
|
||||
pattern = escape_fct(pattern)
|
||||
end
|
||||
return needle:match(pattern)
|
||||
end
|
||||
end, heystack))
|
||||
--- Escapes the given text to be safe for use in file-system paths/names,
|
||||
--- accounting for cross-platform use.
|
||||
---@param text string
|
||||
function M.make_fs_safe(text)
|
||||
return text:gsub("[\\/:]+", "%%")
|
||||
end
|
||||
|
||||
---Get the directory pattern based on OS
|
||||
---@return string
|
||||
function M.get_dir_pattern()
|
||||
function M.dir_pattern()
|
||||
local pattern = "/"
|
||||
if vim.fn.has("win32") == 1 then
|
||||
pattern = "[\\:]"
|
||||
@ -120,24 +17,51 @@ function M.get_dir_pattern()
|
||||
return pattern
|
||||
end
|
||||
|
||||
---Load the given session
|
||||
---@param session string
|
||||
---@param silent boolean Load the session silently?
|
||||
---@return nil|string
|
||||
function M.load_session(session, silent)
|
||||
local session_data = M.make_session_data(session)
|
||||
---Check if a directory is a subdirectory of another
|
||||
---@param parent string
|
||||
---@param child string
|
||||
---@return boolean
|
||||
function M.is_subdirectory(parent, child)
|
||||
return vim.startswith(child, parent)
|
||||
end
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPre", data = session_data })
|
||||
---Check if a directory exists in the given table of directories
|
||||
---@param dir string The directory to check
|
||||
---@param dirs table The table of directories to search in
|
||||
---@return boolean
|
||||
function M.dirs_match(dir, dirs)
|
||||
dir = M.make_fs_safe(vim.fn.expand(dir))
|
||||
|
||||
local ok, result = pcall(vim.cmd, (silent and "silent " or "") .. "source " .. e(session))
|
||||
if not ok then
|
||||
return echoerr("Error loading the session! ", result)
|
||||
for _, search in ipairs(dirs) do
|
||||
if type(search) == "string" then
|
||||
search = M.make_fs_safe(vim.fn.expand(search))
|
||||
if M.is_subdirectory(search, dir) then
|
||||
return true
|
||||
end
|
||||
elseif type(search) == "table" then
|
||||
if search.exact then
|
||||
search = M.make_fs_safe(vim.fn.expand(search[1]))
|
||||
if dir == search then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
vim.g.persisted_exists = true
|
||||
vim.g.persisted_loaded_session = session
|
||||
return false
|
||||
end
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPost", data = session_data })
|
||||
---Check if a value exists in a table
|
||||
---@param val any The value to search for
|
||||
---@param tbl table The table to search in
|
||||
---@return boolean
|
||||
function M.in_table(val, tbl)
|
||||
for _, v in pairs(tbl) do
|
||||
if v == val then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user