refactor: function names and comment blocks
parent
7944b9afe3
commit
548b43aec4
|
|
@ -60,7 +60,7 @@ Install the plugin with your preferred package manager:
|
||||||
>vim
|
>vim
|
||||||
" Vim Script
|
" Vim Script
|
||||||
Plug 'olimorris/persisted.nvim'
|
Plug 'olimorris/persisted.nvim'
|
||||||
|
|
||||||
lua << EOF
|
lua << EOF
|
||||||
require("persisted").setup {
|
require("persisted").setup {
|
||||||
-- your configuration comes here
|
-- your configuration comes here
|
||||||
|
|
@ -275,8 +275,8 @@ Autoloading can be further controlled for certain directories by specifying
|
||||||
|
|
||||||
**Note**Autoloading will not occur if the plugin is lazy loaded or a user opens
|
**Note**Autoloading will not occur if the plugin is lazy loaded or a user opens
|
||||||
Neovim with arguments other than a single directory argument. For example:
|
Neovim with arguments other than a single directory argument. For example:
|
||||||
`nvim some_file.rb` will not result in autoloading but
|
`nvim some_file.rb` will not not result in autoloading but `nvim
|
||||||
`nvim some/existing/path` or `nvim .` will.
|
some/existing/path` or `nvim .` will.
|
||||||
|
|
||||||
FOLLOWING CURRENT WORKING DIRECTORY ~
|
FOLLOWING CURRENT WORKING DIRECTORY ~
|
||||||
|
|
||||||
|
|
@ -380,7 +380,7 @@ autocmd can be created to hook into the `PersistedSavePre` event:
|
||||||
|
|
||||||
>lua
|
>lua
|
||||||
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
|
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd({ "User" }, {
|
vim.api.nvim_create_autocmd({ "User" }, {
|
||||||
pattern = "PersistedSavePre",
|
pattern = "PersistedSavePre",
|
||||||
group = group,
|
group = group,
|
||||||
|
|
@ -397,14 +397,14 @@ made available to the callbacks:
|
||||||
|
|
||||||
>lua
|
>lua
|
||||||
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
|
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd({ "User" }, {
|
vim.api.nvim_create_autocmd({ "User" }, {
|
||||||
pattern = "PersistedTelescopeLoadPre",
|
pattern = "PersistedTelescopeLoadPre",
|
||||||
group = group,
|
group = group,
|
||||||
callback = function(session)
|
callback = function(session)
|
||||||
-- Save the currently loaded session
|
-- Save the currently loaded session
|
||||||
require("persisted").save({ session = vim.g.persisted_loaded_session })
|
require("persisted").save({ session = vim.g.persisted_loaded_session })
|
||||||
|
|
||||||
-- Delete all of the open buffers
|
-- Delete all of the open buffers
|
||||||
vim.api.nvim_input("<ESC>:%bd!<CR>")
|
vim.api.nvim_input("<ESC>:%bd!<CR>")
|
||||||
end,
|
end,
|
||||||
|
|
|
||||||
|
|
@ -11,40 +11,44 @@ local e = vim.fn.fnameescape
|
||||||
---@param replace string
|
---@param replace string
|
||||||
---@param n? integer
|
---@param n? integer
|
||||||
---@return string
|
---@return string
|
||||||
---@return integer count
|
---@return integer
|
||||||
local function escape_pattern(str, pattern, replace, n)
|
local function escape_pattern(str, pattern, replace, n)
|
||||||
pattern = string.gsub(pattern, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") -- escape pattern
|
pattern = string.gsub(pattern, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") -- escape pattern
|
||||||
replace = string.gsub(replace, "[%%]", "%%%%") -- escape replacement
|
replace = string.gsub(replace, "[%%]", "%%%%") -- escape replacement
|
||||||
|
|
||||||
return string.gsub(str, pattern, replace, n)
|
return string.gsub(str, pattern, replace, n)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Checks if vim was started with either zero arguments or a single argument
|
---Check any arguments passed to Neovim and verify if they're a directory
|
||||||
---which is a drectory.
|
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function is_neovim_start_ok()
|
local function args_check()
|
||||||
if vim.fn.argc() == 1 then
|
if vim.fn.argc() == 1 then
|
||||||
return vim.fn.isdirectory(vim.fn.argv(0)) ~= 0
|
return vim.fn.isdirectory(vim.fn.argv(0)) ~= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
return vim.fn.argc() == 0
|
return vim.fn.argc() == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
---Gets the directory to be used for sessions.
|
---Get the directory to be used for the session
|
||||||
---@return string
|
---@return string
|
||||||
local function get_start_dir()
|
local function session_dir()
|
||||||
if vim.fn.argc() == 1 then
|
if vim.fn.argc() == 1 then
|
||||||
local dir = vim.fn.expand(vim.fn.argv(0))
|
local dir = vim.fn.expand(vim.fn.argv(0))
|
||||||
if dir == '.' then
|
|
||||||
return vim.fn.getcwd()
|
if dir == "." then
|
||||||
|
return vim.fn.getcwd()
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.fn.isdirectory(dir) ~= 0 then
|
if vim.fn.isdirectory(dir) ~= 0 then
|
||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return vim.fn.getcwd()
|
return vim.fn.getcwd()
|
||||||
end
|
end
|
||||||
|
|
||||||
---Does the current working directory allow for the auto-saving and loading?
|
---Does the current working directory allow for the auto-saving and loading?
|
||||||
---@param dir string
|
---@param dir string Directory to be used for the session
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function allow_dir(dir)
|
local function allow_dir(dir)
|
||||||
local allowed_dirs = config.options.allowed_dirs
|
local allowed_dirs = config.options.allowed_dirs
|
||||||
|
|
@ -57,7 +61,7 @@ local function allow_dir(dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Is the current working directory ignored for auto-saving and loading?
|
---Is the current working directory ignored for auto-saving and loading?
|
||||||
---@param dir string
|
---@param dir string Directory to be used for the session
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function ignore_dir(dir)
|
local function ignore_dir(dir)
|
||||||
local ignored_dirs = config.options.ignored_dirs
|
local ignored_dirs = config.options.ignored_dirs
|
||||||
|
|
@ -70,9 +74,8 @@ local function ignore_dir(dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get the session that was saved last
|
---Get the session that was saved last
|
||||||
---@param dir string The directory whose last session to load.
|
|
||||||
---@return string
|
---@return string
|
||||||
local function get_last(dir)
|
local function get_last()
|
||||||
local sessions = vim.fn.glob(config.options.save_dir .. "*.vim", true, true)
|
local sessions = vim.fn.glob(config.options.save_dir .. "*.vim", true, true)
|
||||||
|
|
||||||
table.sort(sessions, function(a, b)
|
table.sort(sessions, function(a, b)
|
||||||
|
|
@ -83,10 +86,11 @@ local function get_last(dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get the current Git branch
|
---Get the current Git branch
|
||||||
---@param dir? string The directory to inspect for a Git branch. If not set then the current working directory is used.
|
---@param dir? string Directory to be used for the session
|
||||||
---@return string|nil
|
---@return string|nil
|
||||||
function M.get_branch(dir)
|
function M.get_branch(dir)
|
||||||
dir = dir or get_start_dir()
|
dir = dir or session_dir()
|
||||||
|
|
||||||
if config.options.use_git_branch then
|
if config.options.use_git_branch then
|
||||||
vim.fn.system("git -C " .. dir .. " rev-parse 2>/dev/null")
|
vim.fn.system("git -C " .. dir .. " rev-parse 2>/dev/null")
|
||||||
|
|
||||||
|
|
@ -97,10 +101,7 @@ function M.get_branch(dir)
|
||||||
|
|
||||||
if vim.v.shell_error == 0 then
|
if vim.v.shell_error == 0 then
|
||||||
local branch = config.options.branch_separator .. git_branch[1]:gsub("/", "%%")
|
local branch = config.options.branch_separator .. git_branch[1]:gsub("/", "%%")
|
||||||
local branch_session = config.options.save_dir
|
local branch_session = config.options.save_dir .. dir:gsub(utils.get_dir_pattern(), "%%") .. branch .. ".vim"
|
||||||
.. dir:gsub(utils.get_dir_pattern(), "%%")
|
|
||||||
.. branch
|
|
||||||
.. ".vim"
|
|
||||||
|
|
||||||
-- Try to load the session for the current branch
|
-- Try to load the session for the current branch
|
||||||
if vim.fn.filereadable(branch_session) ~= 0 then
|
if vim.fn.filereadable(branch_session) ~= 0 then
|
||||||
|
|
@ -120,17 +121,11 @@ function M.get_branch(dir)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- -- INFO: This allows users who have `@@main` in their session name to load
|
|
||||||
-- -- repositories that are not git enabled
|
|
||||||
-- if config.options.use_old_branching then
|
|
||||||
-- 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
|
---Get the current session for the current working directory and git branch
|
||||||
---@param dir string The directory whose session file to get.
|
---@param dir string Directory to be used for the session
|
||||||
---@return string
|
---@return string
|
||||||
local function get_current(dir)
|
local function get_current(dir)
|
||||||
local name = dir:gsub(utils.get_dir_pattern(), "%%")
|
local name = dir:gsub(utils.get_dir_pattern(), "%%")
|
||||||
|
|
@ -140,10 +135,11 @@ local function get_current(dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Determine if a session for the current wording directory, exists
|
---Determine if a session for the current wording directory, exists
|
||||||
---@param dir? string The directory whose associated session to check if exists. If not set, current working directory is used.
|
---@param dir? string Directory to be used for the session
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.session_exists(dir)
|
function M.session_exists(dir)
|
||||||
dir = dir or get_start_dir()
|
dir = dir or session_dir()
|
||||||
|
|
||||||
return vim.fn.filereadable(get_current(dir)) ~= 0
|
return vim.fn.filereadable(get_current(dir)) ~= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -152,12 +148,12 @@ end
|
||||||
---@return nil
|
---@return nil
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
config.setup(opts)
|
config.setup(opts)
|
||||||
|
local dir = session_dir()
|
||||||
|
|
||||||
local dir = get_start_dir()
|
|
||||||
if
|
if
|
||||||
config.options.autosave
|
config.options.autosave
|
||||||
and (allow_dir(dir) and not ignore_dir(dir) and vim.g.persisting == nil)
|
and (allow_dir(dir) and not ignore_dir(dir) and vim.g.persisting == nil)
|
||||||
and is_neovim_start_ok()
|
and args_check()
|
||||||
then
|
then
|
||||||
M.start()
|
M.start()
|
||||||
end
|
end
|
||||||
|
|
@ -165,12 +161,13 @@ end
|
||||||
|
|
||||||
---Load a session
|
---Load a session
|
||||||
---@param opt? table
|
---@param opt? table
|
||||||
---@param dir? string
|
---@param dir string Directory to be used for the session
|
||||||
---@return nil
|
---@return nil
|
||||||
function M.load(opt, dir)
|
function M.load(opt, dir)
|
||||||
opt = opt or {}
|
opt = opt or {}
|
||||||
dir = dir or get_start_dir()
|
dir = dir or session_dir()
|
||||||
local session = opt.session or (opt.last and get_last(dir) or get_current(dir))
|
|
||||||
|
local session = opt.session or (opt.last and get_last() or get_current(dir))
|
||||||
|
|
||||||
local session_exists = vim.fn.filereadable(session) ~= 0
|
local session_exists = vim.fn.filereadable(session) ~= 0
|
||||||
|
|
||||||
|
|
@ -202,8 +199,9 @@ end
|
||||||
---Automatically load the session for the current dir
|
---Automatically load the session for the current dir
|
||||||
---@return nil
|
---@return nil
|
||||||
function M.autoload()
|
function M.autoload()
|
||||||
local dir = get_start_dir()
|
local dir = session_dir()
|
||||||
if config.options.autoload and is_neovim_start_ok() then
|
|
||||||
|
if config.options.autoload and args_check() then
|
||||||
if allow_dir(dir) and not ignore_dir(dir) then
|
if allow_dir(dir) and not ignore_dir(dir) then
|
||||||
M.load({}, dir)
|
M.load({}, dir)
|
||||||
end
|
end
|
||||||
|
|
@ -237,11 +235,11 @@ end
|
||||||
|
|
||||||
---Save the session
|
---Save the session
|
||||||
---@param opt? table
|
---@param opt? table
|
||||||
---@param dir? string
|
---@param dir? string Directory to be used for the session
|
||||||
---@return nil
|
---@return nil
|
||||||
function M.save(opt, dir)
|
function M.save(opt, dir)
|
||||||
opt = opt or {}
|
opt = opt or {}
|
||||||
dir = dir or get_start_dir()
|
dir = dir or session_dir()
|
||||||
|
|
||||||
if not opt.session then
|
if not opt.session then
|
||||||
-- Do not save the session if the user has manually stopped it...unless it's forced
|
-- Do not save the session if the user has manually stopped it...unless it's forced
|
||||||
|
|
@ -265,11 +263,12 @@ function M.save(opt, dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Delete the current session
|
---Delete the current session
|
||||||
---@param dir? string The directory whose associated session to delete. If not set, the current working directory is used.
|
---@param dir? string Directory to be used for the session
|
||||||
---@return nil
|
---@return nil
|
||||||
function M.delete(dir)
|
function M.delete(dir)
|
||||||
dir = dir or get_start_dir()
|
dir = dir or session_dir()
|
||||||
local session = get_current(dir)
|
local session = get_current(dir)
|
||||||
|
|
||||||
if session and vim.loop.fs_stat(session) ~= 0 then
|
if session and vim.loop.fs_stat(session) ~= 0 then
|
||||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedDeletePre", data = { name = session } })
|
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedDeletePre", data = { name = session } })
|
||||||
|
|
||||||
|
|
@ -287,7 +286,7 @@ end
|
||||||
---@return nil
|
---@return nil
|
||||||
function M.toggle(dir)
|
function M.toggle(dir)
|
||||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedToggled" })
|
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedToggled" })
|
||||||
dir = dir or get_start_dir()
|
dir = dir or session_dir()
|
||||||
|
|
||||||
if vim.g.persisting == nil then
|
if vim.g.persisting == nil then
|
||||||
return M.load({}, dir)
|
return M.load({}, dir)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue