Skip to content

Commit

Permalink
feat: ✨ Added exact ignored_dirs support (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
miversen33 authored Nov 26, 2023
1 parent d821524 commit fc9f398
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ require("persisted").setup({

Specifying `~/.config` will prevent any autosaving and autoloading from that directory as well as all its sub-directories.

You can also specify exact directory matches to ignore. In this case, unlike the default behavior which ignores all children of the ignored directory, this will ignore only the specified child. For example:

```lua
require("persisted").setup({
ignored_dirs = {
"~/.config",
"~/.local/nvim",
{ "/", exact = true },
{ "/tmp", exact = true }
},
})
```

In this setup, `~/.config` and `~/.local/nvim` are still going to behave in their default setting (ignoring all listed directory and its children), however `/` and `/tmp` will only ignore those directories exactly.

### Events / Callbacks

The plugin fires events at various points during its lifecycle which users can hook into:
Expand Down
27 changes: 20 additions & 7 deletions doc/persisted.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Table of Contents *persisted.nvim-table-of-contents*

FEATURES *persisted.nvim-features*


- Automatically saves the active session under `.local/share/nvim/sessions` on exiting Neovim
- Supports sessions across multiple git branches
- Supports autosaving and autoloading of sessions with allowed/ignored directories
Expand All @@ -23,7 +22,6 @@ FEATURES *persisted.nvim-features*

REQUIREMENTS *persisted.nvim-requirements*


- Neovim >= 0.8.0


Expand Down Expand Up @@ -105,7 +103,6 @@ COMMANDS ~

The plugin comes with a number of commands:


- `:SessionToggle` - Determines whether to load, start or stop a session
- `:SessionStart` - Start recording a session. Useful if `autosave = false`
- `:SessionStop` - Stop recording a session
Expand All @@ -122,7 +119,6 @@ The Telescope extension may be opened via `:Telescope persisted`.

Once opened, the available keymaps are:


- `<CR>` - Source the session file
- `<C-d>` - Delete the session file

Expand All @@ -131,7 +127,6 @@ GLOBAL VARIABLES ~

The plugin sets global variables which can be utilised in your configuration:


- `vim.g.persisting` - (bool) Determines if the plugin is active for the current session
- `vim.g.persisted_exists` - (bool) Determines if a session exists for the current working directory
- `vim.g.persisted_loaded_session` - (string) The file path to the current session
Expand Down Expand Up @@ -167,7 +162,7 @@ WHAT IS SAVED IN THE SESSION? ~

As the plugin uses Vim’s `:mksession` command then you may change the
`vim.o.sessionoptions` value to determine what to write into the session.
Please see `:h sessionoptions` for more information.
Please see |sessionoptions| for more information.


**Note**The author uses: `vim.o.sessionoptions =
Expand Down Expand Up @@ -333,13 +328,31 @@ autosave and autoload from. For example:
Specifying `~/.config` will prevent any autosaving and autoloading from that
directory as well as all its sub-directories.

You can also specify exact directory matches to ignore. In this case, unlike
the default behavior which ignores all children of the ignored directory, this
will ignore only the specified child. For example:

>lua
require("persisted").setup({
ignored_dirs = {
"~/.config",
"~/.local/nvim",
{ "/", exact = true },
{ "/tmp", exact = true }
},
})
<

In this setup, `~/.config` and `~/.local/nvim` are still going to behave in
their default setting (ignoring all listed directory and its children), however
`/` and `/tmp` will only ignore those directories exactly.


EVENTS / CALLBACKS ~

The plugin fires events at various points during its lifecycle which users can
hook into:


- `PersistedLoadPre` - For _before_ a session is loaded
- `PersistedLoadPost` - For _after_ a session is loaded
- `PersistedTelescopeLoadPre` - For _before_ a session is loaded via Telescope
Expand Down
21 changes: 18 additions & 3 deletions lua/persisted/utils.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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
Expand Down Expand Up @@ -44,9 +46,22 @@ end
function M.dirs_match(dir, dirs_table)
dir = vim.fn.expand(dir)
return dirs_table
and next(vim.tbl_filter(function(pattern)
return dir:find(escape_pattern(vim.fn.expand(pattern)))
end, dirs_table))
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 dir == pattern
else
return dir:find(escape_pattern(vim.fn.expand(pattern)))
end
end,
dirs_table))
end

---Get the directory pattern based on OS
Expand Down
28 changes: 28 additions & 0 deletions tests/autoload/autoload_session_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,32 @@ describe("Autoloading", function()
local content = vim.fn.getline(1, "$")
assert.equals("If you're reading this, I guess auto-loading works", content[1])
end)

it("autoloads the a child directory of ignored_dirs exact", function()
local co = coroutine.running()
vim.defer_fn(function()
coroutine.resume(co)
end, 2000)

local fp_sep = vim.loop.os_uname().sysname:lower():match('windows') and '\\' or '/' -- \ for windows, mac and linux both use \

local session_dir = vim.fn.getcwd() .. "/test/dummy_data/"
require("persisted").setup({
save_dir = session_dir,
autoload = true,
autosave = true,
ignored_dirs = {
-- Setting the parent of our current to an ignored directory
{
string.format("%s%s..%s", vim.fn.getcwd(), fp_sep, fp_sep),
exact = true
}
}
})
coroutine.yield()

local content = vim.fn.getline(1, "$")
assert.equals("If you're reading this, I guess auto-loading works", content[1])

end)
end)

0 comments on commit fc9f398

Please sign in to comment.