-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathutils.lua
67 lines (59 loc) · 1.56 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
local M = {}
--- Escapes the given text to be safe for use in file-system paths/names,
--- accounting for cross-platform use.
---@param text string
function M.make_fs_safe(text)
return text:gsub("[\\/:]+", "%%")
end
---Get the directory pattern based on OS
---@return string
function M.dir_pattern()
local pattern = "/"
if vim.fn.has("win32") == 1 then
pattern = "[\\:]"
end
return pattern
end
---Check if a directory is a subdirectory of another
---@param parent string
---@param child string
---@return boolean
function M.is_subdirectory(parent, child)
return vim.startswith(child, parent)
end
---Check if a directory exists in the given table of directories
---@param dir string The directory to check
---@param dirs table The table of directories to search in
---@return boolean
function M.dirs_match(dir, dirs)
dir = M.make_fs_safe(vim.fn.expand(dir))
for _, search in ipairs(dirs) do
if type(search) == "string" then
search = M.make_fs_safe(vim.fn.expand(search))
if M.is_subdirectory(search, dir) then
return true
end
elseif type(search) == "table" then
if search.exact then
search = M.make_fs_safe(vim.fn.expand(search[1]))
if dir == search then
return true
end
end
end
end
return false
end
---Check if a value exists in a table
---@param val any The value to search for
---@param tbl table The table to search in
---@return boolean
function M.in_table(val, tbl)
for _, v in pairs(tbl) do
if v == val then
return true
end
end
return false
end
return M