Skip to content

Commit 98cd3de

Browse files
committed
fix: #146 allowed_dirs and ignored_dirs
1 parent 970f689 commit 98cd3de

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ test: $(PLENARY_DIR)
3636
nvim --headless --noplugin -u tests/minimal.vim +TestGitBranching
3737
nvim --headless --noplugin -u tests/minimal.vim +TestFollowCwd
3838
nvim --headless --noplugin -u tests/minimal.vim +TestDefaults
39+
nvim --headless --noplugin -u tests/minimal.vim +TestDirs
3940
nvim --headless --noplugin -u tests/minimal.vim +TearDown
4041

4142
$(PLENARY_DIR):

lua/persisted/utils.lua

+30-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ end
1414
---@param input string
1515
---@return string
1616
function M.escape_dir_pattern(input)
17-
local magic_chars = { "%", "(", ")", ".", "+", "-", "*", "?", "[", "^", "$" }
17+
local magic_chars = { "%", "(", ")", "+", "-", "*", "?", "[", "^", "$" }
1818

1919
for _, char in ipairs(magic_chars) do
2020
input = input:gsub("%" .. char, "%%" .. char)
@@ -23,18 +23,38 @@ function M.escape_dir_pattern(input)
2323
return input
2424
end
2525

26-
---Check if a target directory exists in a given table
27-
---@param dir string
28-
---@param dirs_table table
26+
---Check if a directory is a subdirectory of another
27+
---@param parent string
28+
---@param child string
2929
---@return boolean
30-
function M.dirs_match(dir, dirs_table)
31-
dir = vim.fn.expand(dir)
30+
function M.is_subdirectory(parent, child)
31+
return vim.startswith(child, parent)
32+
end
33+
34+
---Check if a directory exists in the given table of directories
35+
---@param dir string The directory to check
36+
---@param dirs table The table of directories to search in
37+
---@return boolean
38+
function M.dirs_match(dir, dirs)
39+
dir = M.escape_dir_pattern(vim.fn.expand(dir))
3240

33-
local match = M.in_table(dir, dirs_table, function(pattern)
34-
return M.escape_dir_pattern(vim.fn.expand(pattern))
35-
end)
41+
for _, search in ipairs(dirs) do
42+
if type(search) == "string" then
43+
search = M.escape_dir_pattern(vim.fn.expand(search))
44+
if M.is_subdirectory(search, dir) then
45+
return true
46+
end
47+
elseif type(search) == "table" then
48+
if search.exact then
49+
search = M.escape_dir_pattern(vim.fn.expand(search[1]))
50+
if dir == search then
51+
return true
52+
end
53+
end
54+
end
55+
end
3656

37-
return match
57+
return false
3858
end
3959

4060
---Check if a string matches and entry in a given table

tests/dirs_spec.lua

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local session_dir = vim.fn.getcwd() .. "/tests/default_data/"
2+
local utils = require("persisted.utils")
3+
4+
describe("Directory utilities:", function()
5+
it("can match directories", function()
6+
local cwd = "~/Code/Neovim/persisted.nvim"
7+
local allowed_dirs = { "~/Code" }
8+
9+
local match = utils.dirs_match(cwd, allowed_dirs)
10+
assert.equals(true, match)
11+
end)
12+
13+
it("can work with exact directories", function()
14+
local cwd = "~/Code/Neovim/persisted.nvim"
15+
local allowed_dirs = { { "~/Code", exact = true } }
16+
local match = utils.dirs_match(cwd, allowed_dirs)
17+
assert.equals(false, match)
18+
19+
cwd = "~/Code/Neovim/persisted.nvim"
20+
allowed_dirs = { { "~/Code/Neovim/persisted.nvim", exact = true } }
21+
match = utils.dirs_match(cwd, allowed_dirs)
22+
assert.equals(true, match)
23+
end)
24+
end)

tests/minimal.vim

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ command TestAutoloading PlenaryBustedDirectory tests/autoload {minimal_init = 't
77
command TestGitBranching PlenaryBustedDirectory tests/git_branching {minimal_init = 'tests/minimal.vim'}
88
command TestFollowCwd PlenaryBustedDirectory tests/follow_cwd {minimal_init = 'tests/minimal.vim'}
99
command TestDefaults PlenaryBustedFile tests/default_settings_spec.lua
10+
command TestDirs PlenaryBustedFile tests/dirs_spec.lua
1011
command TearDown PlenaryBustedFile tests/teardown/clean_up_dirs.lua

0 commit comments

Comments
 (0)