From 7d2a0ac661eb0f34280972c24f4a502e278ffe75 Mon Sep 17 00:00:00 2001 From: HumblePresent <60856003+HumblePresent@users.noreply.github.com> Date: Tue, 16 Aug 2022 02:22:44 -0500 Subject: [PATCH] fix: #18 escape pattern matching characters in directory names --- lua/persisted/utils.lua | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lua/persisted/utils.lua b/lua/persisted/utils.lua index 1363411..286fbb7 100644 --- a/lua/persisted/utils.lua +++ b/lua/persisted/utils.lua @@ -26,6 +26,19 @@ function M.get_last_item(table) return table[last] end +--- Escape special pattern matching characters in a string +---@param input string +---@return string +function M.escape_pattern(input) + local magic_chars = { "%", "(", ")", ".", "+", "-", "*", "?", "[", "^", "$" } + + for _, char in ipairs(magic_chars) do + input = input:gsub("%" .. char, "%%" .. char) + end + + return input +end + ---Check if a target directory exists in a given table ---@param dir string ---@param dirs_table table @@ -33,8 +46,8 @@ end function M.dirs_match(dir, dirs_table) dir = vim.fn.expand(dir) return dirs_table - and next(vim.tbl_filter(function(dir_match) - return dir == vim.fn.expand(dir_match) + and next(vim.tbl_filter(function(pattern) + return dir:find(M.escape_pattern(vim.fn.expand(pattern))) end, dirs_table)) end