Skip to content

fix: fixes #97, normalize path when checking subpath #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/neo-tree/sources/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ M.reveal_current_file = function(source_name)
if cwd == nil then
cwd = vim.fn.getcwd()
end
if string.sub(path, 1, string.len(cwd)) ~= cwd then
if not utils.is_subpath(cwd, path) then
cwd, _ = utils.split_path(path)
inputs.confirm("File not in cwd. Change cwd to " .. cwd .. "?", function(response)
if response == true then
Expand Down
21 changes: 21 additions & 0 deletions lua/neo-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,27 @@ if M.is_windows == true then
M.path_separator = "\\"
end

---Normalize a path, to avoid errors when comparing paths.
---@param path string The path to be normalize.
---@return string string The normalized path.
M.normalize_path = function(path)
if M.is_windows then
-- normalize the drive letter to uppercase
path = path:sub(1, 1):upper() .. path:sub(2)
end
return path
end

---Check if a path is a subpath of another.
--@param base string The base path.
--@param path string The path to check is a subpath.
--@return boolean boolean True if it is a subpath, false otherwise.
M.is_subpath = function(base, path)
base = M.normalize_path(base)
path = M.normalize_path(path)
return string.sub(path, 1, string.len(base)) == base
end

---Split string into a table of strings using a separator.
---@param inputString string The string to split.
---@param sep string The separator to use.
Expand Down