Skip to content

Commit ec494af

Browse files
committed
fix(#3117): allow changing filename's casing
1 parent 582ae48 commit ec494af

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

lua/nvim-tree/utils.lua

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,40 @@ end
292292
---@param path string path to file or directory
293293
---@return boolean
294294
function M.file_exists(path)
295-
local _, error = vim.loop.fs_stat(path)
296-
return error == nil
295+
if not (M.is_windows or M.is_wsl) then
296+
local _, error = vim.loop.fs_stat(path)
297+
return error == nil
298+
end
299+
300+
-- Windows is case-insensetive, but case-preserving
301+
-- If a file's name is being changed into itself
302+
-- with different casing, windows will falsely
303+
-- report that file is already existing, so a hand-rolled
304+
-- implementation of checking for existance is needed.
305+
-- Same holds for WSL, since it can sometimes
306+
-- access Windows files directly.
307+
-- For more details see (#3117).
308+
309+
local parent = vim.fn.fnamemodify(path, ":h")
310+
local filename = vim.fn.fnamemodify(path, ":t")
311+
312+
local handle = vim.loop.fs_scandir(parent)
313+
if not handle then
314+
-- File can not exist if its parent directory does not exist
315+
return false
316+
end
317+
318+
while true do
319+
local name, _ = vim.loop.fs_scandir_next(handle)
320+
if not name then
321+
break
322+
end
323+
if name == filename then
324+
return true
325+
end
326+
end
327+
328+
return false
297329
end
298330

299331
---@param path string

0 commit comments

Comments
 (0)