14
14
--- @param input string
15
15
--- @return string
16
16
function M .escape_dir_pattern (input )
17
- local magic_chars = { " %" , " (" , " )" , " . " , " +" , " -" , " *" , " ?" , " [" , " ^" , " $" }
17
+ local magic_chars = { " %" , " (" , " )" , " +" , " -" , " *" , " ?" , " [" , " ^" , " $" }
18
18
19
19
for _ , char in ipairs (magic_chars ) do
20
20
input = input :gsub (" %" .. char , " %%" .. char )
@@ -23,18 +23,38 @@ function M.escape_dir_pattern(input)
23
23
return input
24
24
end
25
25
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
29
29
--- @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 ))
32
40
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
36
56
37
- return match
57
+ return false
38
58
end
39
59
40
60
--- Check if a string matches and entry in a given table
0 commit comments