Update generated nvim config
This commit is contained in:
@ -0,0 +1,45 @@
|
||||
local M = {}
|
||||
|
||||
local defaults = {
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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)
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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>",
|
||||
},
|
||||
icons = { -- icons displayed in the picker
|
||||
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
|
||||
@ -0,0 +1,38 @@
|
||||
---[[
|
||||
--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
|
||||
@ -0,0 +1,381 @@
|
||||
local utils = require("persisted.utils")
|
||||
local config = require("persisted.config")
|
||||
|
||||
local M = {}
|
||||
|
||||
local e = vim.fn.fnameescape
|
||||
|
||||
---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 dir string Branch to be used for the session
|
||||
---@return boolean
|
||||
local function ignore_branch(branch)
|
||||
local ignored_branches = config.options.ignored_branches
|
||||
|
||||
if 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
|
||||
vim.notify(
|
||||
string.format("[Persisted.nvim]: Trying to load a session for branch %s", config.options.default_branch),
|
||||
vim.log.levels.INFO
|
||||
)
|
||||
vim.notify(
|
||||
string.format("[Persisted.nvim]: Could not load a session for branch %s.", git_branch[1]),
|
||||
vim.log.levels.WARN
|
||||
)
|
||||
vim.g.persisted_branch_session = branch_session
|
||||
return config.options.branch_separator .. config.options.default_branch
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Get the current session for the current working directory and git branch
|
||||
---@param dir string Directory to be used for the session
|
||||
---@return string
|
||||
local function get_current(dir)
|
||||
local name = dir:gsub(utils.get_dir_pattern(), "%%")
|
||||
local branch = M.get_branch(dir)
|
||||
|
||||
return config.options.save_dir .. name .. (branch or "") .. ".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()
|
||||
|
||||
return vim.fn.filereadable(get_current(dir)) ~= 0
|
||||
end
|
||||
|
||||
---Setup the plugin
|
||||
---@param opts? table
|
||||
---@return nil
|
||||
function M.setup(opts)
|
||||
config.setup(opts)
|
||||
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
|
||||
M.start()
|
||||
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()
|
||||
local branch = get_branchname()
|
||||
|
||||
local session = opt.session or (opt.last and get_last() or get_current(dir))
|
||||
|
||||
local session_exists = vim.fn.filereadable(session) ~= 0
|
||||
|
||||
if session then
|
||||
if session_exists then
|
||||
vim.g.persisting_session = config.options.follow_cwd and nil or session
|
||||
utils.load_session(session, config.options.silent)
|
||||
elseif type(config.options.on_autoload_no_session) == "function" then
|
||||
config.options.on_autoload_no_session()
|
||||
end
|
||||
end
|
||||
|
||||
if config.options.autosave and (allow_dir(dir) and not ignore_dir(dir)) and not ignore_branch(branch) 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
|
||||
M.load({}, dir)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Start recording a session
|
||||
---@return nil
|
||||
function M.start()
|
||||
vim.g.persisting = true
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedStateChange", data = { action = "start" } })
|
||||
end
|
||||
|
||||
---Stop recording a session
|
||||
---@return nil
|
||||
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
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePost" })
|
||||
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()
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
local session = opt.session or (vim.g.persisted_branch_session or vim.g.persisting_session or get_current(dir))
|
||||
write(session)
|
||||
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 })
|
||||
|
||||
vim.schedule(function()
|
||||
M.stop()
|
||||
vim.fn.system("rm " .. e(session))
|
||||
end)
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedDeletePost", data = session_data })
|
||||
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()
|
||||
|
||||
if vim.g.persisting == nil then
|
||||
return M.load({}, dir)
|
||||
end
|
||||
|
||||
if vim.g.persisting then
|
||||
return M.stop()
|
||||
end
|
||||
|
||||
return M.start()
|
||||
end
|
||||
|
||||
---List all of the sessions
|
||||
---@return table
|
||||
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 = {}
|
||||
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
|
||||
|
||||
return sessions
|
||||
end
|
||||
|
||||
return M
|
||||
@ -0,0 +1,144 @@
|
||||
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)
|
||||
if not separator_index then
|
||||
return nil
|
||||
end
|
||||
|
||||
local branch = session:sub(separator_index + 2):gsub("%.vim$", ""):gsub("%%", "/")
|
||||
|
||||
-- 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))
|
||||
end
|
||||
|
||||
---Get the directory pattern based on OS
|
||||
---@return string
|
||||
function M.get_dir_pattern()
|
||||
local pattern = "/"
|
||||
if vim.fn.has("win32") == 1 then
|
||||
pattern = "[\\:]"
|
||||
end
|
||||
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)
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPre", data = session_data })
|
||||
|
||||
local ok, result = pcall(vim.cmd, (silent and "silent " or "") .. "source " .. e(session))
|
||||
if not ok then
|
||||
return echoerr("Error loading the session! ", result)
|
||||
end
|
||||
|
||||
vim.g.persisted_exists = true
|
||||
vim.g.persisted_loaded_session = session
|
||||
|
||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPost", data = session_data })
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user