Skip to content

fix(#3117): allow changing filename's casing #3118

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
44 changes: 42 additions & 2 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,51 @@ function M.rename_loaded_buffers(old_path, new_path)
end
end

local is_windows_drive = function(path)
return (M.is_windows) and (path:match('^%a:\\$') ~= nil)
end

---@param path string path to file or directory
---@return boolean
function M.file_exists(path)
local _, error = vim.loop.fs_stat(path)
return error == nil
if not (M.is_windows or M.is_wsl) then
local _, error = vim.loop.fs_stat(path)
return error == nil
end

-- Windows is case-insensetive, but case-preserving
-- If a file's name is being changed into itself
-- with different casing, windows will falsely
-- report that file is already existing, so a hand-rolled
-- implementation of checking for existance is needed.
-- Same holds for WSL, since it can sometimes
-- access Windows files directly.
-- For more details see (#3117).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.


if is_windows_drive(path) then
return vim.fn.isdirectory(path) == 1
end

local parent = vim.fn.fnamemodify(path, ":h")
local filename = vim.fn.fnamemodify(path, ":t")

local handle = vim.loop.fs_scandir(parent)
if not handle then
-- File can not exist if its parent directory does not exist
return false
end

while true do
local name, _ = vim.loop.fs_scandir_next(handle)
if not name then
break
end
if name == filename then
return true
end
end

return false
end

---@param path string
Expand Down
Loading