Skip to content

Commit bdd0bd2

Browse files
authored
fix: fixes #97, normalize path when checking subpath (#98)
1 parent 1020de3 commit bdd0bd2

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lua/neo-tree/sources/manager.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ M.reveal_current_file = function(source_name)
258258
if cwd == nil then
259259
cwd = vim.fn.getcwd()
260260
end
261-
if string.sub(path, 1, string.len(cwd)) ~= cwd then
261+
if not utils.is_subpath(cwd, path) then
262262
cwd, _ = utils.split_path(path)
263263
inputs.confirm("File not in cwd. Change cwd to " .. cwd .. "?", function(response)
264264
if response == true then

lua/neo-tree/utils.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ if M.is_windows == true then
274274
M.path_separator = "\\"
275275
end
276276

277+
---Normalize a path, to avoid errors when comparing paths.
278+
---@param path string The path to be normalize.
279+
---@return string string The normalized path.
280+
M.normalize_path = function(path)
281+
if M.is_windows then
282+
-- normalize the drive letter to uppercase
283+
path = path:sub(1, 1):upper() .. path:sub(2)
284+
end
285+
return path
286+
end
287+
288+
---Check if a path is a subpath of another.
289+
--@param base string The base path.
290+
--@param path string The path to check is a subpath.
291+
--@return boolean boolean True if it is a subpath, false otherwise.
292+
M.is_subpath = function(base, path)
293+
base = M.normalize_path(base)
294+
path = M.normalize_path(path)
295+
return string.sub(path, 1, string.len(base)) == base
296+
end
297+
277298
---Split string into a table of strings using a separator.
278299
---@param inputString string The string to split.
279300
---@param sep string The separator to use.

0 commit comments

Comments
 (0)