Skip to content

Commit 418878b

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 - The original regex based code captures create a limit where input path cannot have more than 32 special charactors ( `.` , `_` or `-`)
1 parent 08a0aa1 commit 418878b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lua/nvim-tree/utils.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ M.is_wsl = vim.fn.has "wsl" == 1
1111
-- false for WSL
1212
M.is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1
1313

14-
function M.path_to_matching_str(path)
15-
return path:gsub("(%-)", "(%%-)"):gsub("(%.)", "(%%.)"):gsub("(%_)", "(%%_)")
16-
end
17-
1814
function M.str_find(haystack, needle)
1915
return vim.fn.stridx(haystack, needle) ~= -1
2016
end
@@ -59,7 +55,14 @@ end
5955
---@param relative_to string
6056
---@return string
6157
function M.path_relative(path, relative_to)
62-
local p, _ = path:gsub("^" .. M.path_to_matching_str(M.path_add_trailing(relative_to)), "")
58+
local _, r = path:find(M.path_add_trailing(relative_to), 1, true)
59+
local p = path
60+
if r then
61+
-- take the relative path starting after '/'
62+
-- if somehow given a completely matching path,
63+
-- returns ""
64+
p = path:sub(r+1)
65+
end
6366
return p
6467
end
6568

0 commit comments

Comments
 (0)