Skip to content

Commit 9222462

Browse files
committed
fix(#2004): Change path_relative to use string find and substring to avoid using regex.
- This removed the original gsub with unintentional captures in path_to_matching_str - Fix original path_to_matching_str (no longer used, kept for future?) - The captures creates a limit where input path cannot have more than 32 special charactors ( `.` , `_` or `-`)l
1 parent 08a0aa1 commit 9222462

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lua/nvim-tree/utils.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ M.is_wsl = vim.fn.has "wsl" == 1
1212
M.is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1
1313

1414
function M.path_to_matching_str(path)
15-
return path:gsub("(%-)", "(%%-)"):gsub("(%.)", "(%%.)"):gsub("(%_)", "(%%_)")
15+
return path:gsub("(%-)", "%%-"):gsub("(%.)", "%%."):gsub("(%_)", "%%_")
1616
end
1717

1818
function M.str_find(haystack, needle)
@@ -59,7 +59,15 @@ end
5959
---@param relative_to string
6060
---@return string
6161
function M.path_relative(path, relative_to)
62-
local p, _ = path:gsub("^" .. M.path_to_matching_str(M.path_add_trailing(relative_to)), "")
62+
local _, r = path:find(M.path_add_trailing(relative_to), 1, true)
63+
local p = path
64+
if r and r < p:len() then
65+
-- take the relative path starting after /
66+
-- if somehow given a completely matching path,
67+
-- preserve the original path. In the original gsub implementation
68+
-- path == relative_to would result in empty string returned.
69+
p = path:sub(r+1)
70+
end
6371
return p
6472
end
6573

0 commit comments

Comments
 (0)