feat: #5 improve auto save/load pattern match
parent
4e3310f6b8
commit
48ecaa7c6e
13
README.md
13
README.md
|
|
@ -23,7 +23,8 @@ The plugin was forked from the fantastic [Persistence.nvim](https://github.com/f
|
||||||
- [Callbacks](#callbacks)
|
- [Callbacks](#callbacks)
|
||||||
- [Telescope extension](#telescope-extension)
|
- [Telescope extension](#telescope-extension)
|
||||||
- [Usage](#rocket-usage)
|
- [Usage](#rocket-usage)
|
||||||
- [Commands](#commands)
|
- [Default commands](#default-commands)
|
||||||
|
- [Telescope](#telescope)
|
||||||
- [Lazy loading](#lazy-loading)
|
- [Lazy loading](#lazy-loading)
|
||||||
- [Helpers](#helpers)
|
- [Helpers](#helpers)
|
||||||
- [License](#page_with_curl-license)
|
- [License](#page_with_curl-license)
|
||||||
|
|
@ -135,12 +136,14 @@ You may specify a table of directories for which the plugin will autosave and/or
|
||||||
```lua
|
```lua
|
||||||
{
|
{
|
||||||
allowed_dirs = {
|
allowed_dirs = {
|
||||||
"~/Code/Neovim",
|
"~/.dotfiles",
|
||||||
"~/Code/Projects/Ruby
|
"~/Code",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Specifying `~/Code` will autosave and autoload from the that directory as well as all its sub-directories.
|
||||||
|
|
||||||
> **Note:** If `allowed_dirs` is set, then the plugin will only autosave and/or autoload from the specificed directories
|
> **Note:** If `allowed_dirs` is set, then the plugin will only autosave and/or autoload from the specificed directories
|
||||||
|
|
||||||
> **Note:** If `allowed_dirs` is left at its default value and `autosave` and/or `autoload` are set to `true`, then the plugin will autoload/autosave from *any* directory
|
> **Note:** If `allowed_dirs` is left at its default value and `autosave` and/or `autoload` are set to `true`, then the plugin will autoload/autosave from *any* directory
|
||||||
|
|
@ -158,6 +161,8 @@ You may specify a table of directories for which the plugin will **never** autos
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Specifying `~/.config` will prevent any autosaving and autoloading from that directory as well as all its sub-directories.
|
||||||
|
|
||||||
### Callbacks
|
### Callbacks
|
||||||
|
|
||||||
The plugin allows for *before* and *after* callbacks to be executed before and after a session is saved. This is achieved via the `before_save` and `after_save` configuration options. For example:
|
The plugin allows for *before* and *after* callbacks to be executed before and after a session is saved. This is achieved via the `before_save` and `after_save` configuration options. For example:
|
||||||
|
|
@ -217,7 +222,7 @@ The plugin comes with a number of commands:
|
||||||
|
|
||||||
> **Note:** The author only binds `SessionToggle` to a keymap for simplicity.
|
> **Note:** The author only binds `SessionToggle` to a keymap for simplicity.
|
||||||
|
|
||||||
### Telescope extension
|
### Telescope
|
||||||
|
|
||||||
The Telescope extension may be opened via `:Telescope persisted`.
|
The Telescope extension may be opened via `:Telescope persisted`.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,11 @@
|
||||||
|
local utils = require("persisted.utils")
|
||||||
local config = require("persisted.config")
|
local config = require("persisted.config")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local e = vim.fn.fnameescape
|
local e = vim.fn.fnameescape
|
||||||
local echo = vim.api.nvim_echo
|
|
||||||
local default_branch = "main"
|
local default_branch = "main"
|
||||||
|
|
||||||
local echoerr = function(msg, error)
|
|
||||||
echo({
|
|
||||||
{ "[persisted.nvim]: ", "ErrorMsg" },
|
|
||||||
{ msg, "WarningMsg" },
|
|
||||||
{ error, "Normal" },
|
|
||||||
}, true, {})
|
|
||||||
end
|
|
||||||
|
|
||||||
---Setup the plugin's commands
|
---Setup the plugin's commands
|
||||||
---@return nil
|
---@return nil
|
||||||
local function setup_commands()
|
local function setup_commands()
|
||||||
|
|
@ -28,19 +20,6 @@ local function setup_commands()
|
||||||
]])
|
]])
|
||||||
end
|
end
|
||||||
|
|
||||||
---Check if a target directory exists in a given table
|
|
||||||
---@param dir_target string
|
|
||||||
---@param dir_table table
|
|
||||||
---@return boolean
|
|
||||||
local function dirs_match(dir_target, dir_table)
|
|
||||||
for _, dir in pairs(dir_table) do
|
|
||||||
dir = string.gsub(vim.fn.expand(dir), "/+$", "")
|
|
||||||
if dir_target == dir then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
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?
|
||||||
---@return boolean
|
---@return boolean
|
||||||
|
|
@ -50,7 +29,7 @@ local function allow_dir()
|
||||||
if allowed_dirs == nil then
|
if allowed_dirs == nil then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
return dirs_match(vim.fn.getcwd(), allowed_dirs)
|
return utils.dirs_match(vim.fn.getcwd(), allowed_dirs)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Is the current working directory ignored for auto-saving and loading?
|
---Is the current working directory ignored for auto-saving and loading?
|
||||||
|
|
@ -61,7 +40,7 @@ local function ignore_dir()
|
||||||
if ignored_dirs == nil then
|
if ignored_dirs == nil then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
return dirs_match(vim.fn.getcwd(), ignored_dirs)
|
return utils.dirs_match(vim.fn.getcwd(), ignored_dirs)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get the session that was saved last
|
---Get the session that was saved last
|
||||||
|
|
@ -94,20 +73,10 @@ local function get_branch()
|
||||||
return "_" .. default_branch
|
return "_" .. default_branch
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get the directory pattern based on OS
|
|
||||||
---@return string
|
|
||||||
local function get_dir_pattern()
|
|
||||||
local pattern = "/"
|
|
||||||
if vim.fn.has("win32") == 1 then
|
|
||||||
pattern = "[\\:]"
|
|
||||||
end
|
|
||||||
return pattern
|
|
||||||
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
|
||||||
---@return string
|
---@return string
|
||||||
local function get_current()
|
local function get_current()
|
||||||
local name = vim.fn.getcwd():gsub(get_dir_pattern(), "%%")
|
local name = vim.fn.getcwd():gsub(utils.get_dir_pattern(), "%%")
|
||||||
return config.options.dir .. name .. get_branch() .. ".vim"
|
return config.options.dir .. name .. get_branch() .. ".vim"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -139,7 +108,7 @@ function M.load(opt)
|
||||||
if session and vim.fn.filereadable(session) ~= 0 then
|
if session and vim.fn.filereadable(session) ~= 0 then
|
||||||
local ok, result = pcall(vim.cmd, "source " .. e(session))
|
local ok, result = pcall(vim.cmd, "source " .. e(session))
|
||||||
if not ok then
|
if not ok then
|
||||||
echoerr("Error loading the session! ", result)
|
utils.echoerr("Error loading the session! ", result)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -194,21 +163,19 @@ end
|
||||||
---List all of the sessions in the session directory
|
---List all of the sessions in the session directory
|
||||||
---@return table
|
---@return table
|
||||||
function M.list()
|
function M.list()
|
||||||
local utils = require("persisted.utils")
|
|
||||||
|
|
||||||
local session_files = vim.fn.glob(config.options.dir .. "*.vim", true, true)
|
local session_files = vim.fn.glob(config.options.dir .. "*.vim", true, true)
|
||||||
|
|
||||||
local sessions = {}
|
local sessions = {}
|
||||||
for _, session in pairs(session_files) do
|
for _, session in pairs(session_files) do
|
||||||
local session_name = session
|
local session_name = session
|
||||||
:gsub(config.options.dir, "")
|
:gsub(config.options.dir, "")
|
||||||
:gsub("%%", get_dir_pattern())
|
:gsub("%%", utils.get_dir_pattern())
|
||||||
:gsub(vim.fn.expand("~"), get_dir_pattern())
|
:gsub(vim.fn.expand("~"), utils.get_dir_pattern())
|
||||||
:gsub("//", "")
|
:gsub("//", "")
|
||||||
|
|
||||||
local branch = utils.get_last_item(utils.split_str(session_name, "_")):gsub(".vim", "")
|
local branch = utils.get_last_item(utils.split_str(session_name, "_")):gsub(".vim", "")
|
||||||
|
|
||||||
local pwd = vim.fn.expand("~") .. get_dir_pattern() .. session_name
|
local pwd = vim.fn.expand("~") .. utils.get_dir_pattern() .. session_name
|
||||||
local branch_name = "_" .. utils.get_last_item(utils.split_str(pwd, "_"))
|
local branch_name = "_" .. utils.get_last_item(utils.split_str(pwd, "_"))
|
||||||
pwd = pwd:gsub(branch_name, "")
|
pwd = pwd:gsub(branch_name, "")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,4 +25,37 @@ function M.get_last_item(table)
|
||||||
return table[last]
|
return table[last]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Check if a target directory exists in a given table
|
||||||
|
---@param dir_target string
|
||||||
|
---@param dir_table table
|
||||||
|
---@return boolean
|
||||||
|
function M.dirs_match(dir, dirs_table)
|
||||||
|
local dir = vim.fn.expand(dir)
|
||||||
|
return dirs_table and next(vim.tbl_filter(function(pattern)
|
||||||
|
return dir:match(vim.fn.expand(pattern))
|
||||||
|
end, dirs_table))
|
||||||
|
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
|
||||||
|
|
||||||
|
---Print an error message
|
||||||
|
--@param msg string
|
||||||
|
--@param error string
|
||||||
|
--@return string
|
||||||
|
function M.echoerr(msg, error)
|
||||||
|
vim.api.nvim_echo({
|
||||||
|
{ "[persisted.nvim]: ", "ErrorMsg" },
|
||||||
|
{ msg, "WarningMsg" },
|
||||||
|
{ error, "Normal" },
|
||||||
|
}, true, {})
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue