Skip to content

Commit 6492d43

Browse files
authored
Rename child buffers when renaming directory (#1095)
1 parent 57d6f70 commit 6492d43

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

lua/nvim-tree/utils.lua

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,23 @@ function M.is_windows_exe(ext)
137137
return pathexts[ext:upper()]
138138
end
139139

140-
function M.rename_loaded_buffers(old_name, new_name)
140+
function M.rename_loaded_buffers(old_path, new_path)
141141
for _, buf in pairs(a.nvim_list_bufs()) do
142142
if a.nvim_buf_is_loaded(buf) then
143-
if a.nvim_buf_get_name(buf) == old_name then
144-
a.nvim_buf_set_name(buf, new_name)
145-
-- to avoid the 'overwrite existing file' error message on write
146-
vim.api.nvim_buf_call(buf, function()
147-
vim.cmd "silent! w!"
148-
end)
143+
local buf_name = a.nvim_buf_get_name(buf)
144+
local exact_match = buf_name == old_path
145+
local child_match = (
146+
buf_name:sub(1, #old_path) == old_path and buf_name:sub(#old_path + 1, #old_path + 1) == path_separator
147+
)
148+
if exact_match or child_match then
149+
a.nvim_buf_set_name(buf, new_path .. buf_name:sub(#old_path + 1))
150+
-- to avoid the 'overwrite existing file' error message on write for
151+
-- normal files
152+
if a.nvim_buf_get_option(buf, "buftype") == "" then
153+
a.nvim_buf_call(buf, function()
154+
vim.cmd "silent! write!"
155+
end)
156+
end
149157
end
150158
end
151159
end

0 commit comments

Comments
 (0)