feat/fix: add rename cmd, fix rename error

- add <C-r> binding to omit the filename on rename (option is
  full_rename).
- call `silent! write!` on rename to avoid the `overwrite existing file`
  error when saving the buffer.
This commit is contained in:
kiyan
2021-02-22 20:53:12 +01:00
committed by Kiyan
parent ce2420b9da
commit 07da8a7242
6 changed files with 28 additions and 18 deletions

View File

@@ -67,6 +67,7 @@ function M.get_bindings()
create = keybindings.create or 'a',
remove = keybindings.remove or 'd',
rename = keybindings.rename or 'r',
full_rename = keybindings.full_rename or '<C-r>',
cut = keybindings.cut or 'x',
copy = keybindings.copy or 'c',
paste = keybindings.paste or 'p',

View File

@@ -239,28 +239,32 @@ function M.remove(node)
end
end
function M.rename(node)
if node.name == '..' then return end
function M.rename(with_sub)
return function(node)
if node.name == '..' then return end
local namelen = node.name:len()
local abs_path = node.absolute_path:sub(0, namelen * (-1) -1)
local new_name = vim.fn.input("Rename " ..node.name.. " to ", abs_path)
clear_prompt()
if not new_name or #new_name == 0 then return end
local namelen = node.name:len()
local abs_path = with_sub and node.absolute_path:sub(0, namelen * (-1) -1) or node.absolute_path
local new_name = vim.fn.input("Rename " ..node.name.. " to ", abs_path)
clear_prompt()
if not new_name or #new_name == 0 then return end
local success = luv.fs_rename(node.absolute_path, new_name)
if not success then
return api.nvim_err_writeln('Could not rename '..node.absolute_path..' to '..new_name)
end
api.nvim_out_write(node.absolute_path..''..new_name..'\n')
for _, buf in pairs(api.nvim_list_bufs()) do
if api.nvim_buf_is_loaded(buf) then
if api.nvim_buf_get_name(buf) == node.absolute_path then
api.nvim_buf_set_name(buf, new_name)
local success = luv.fs_rename(node.absolute_path, new_name)
if not success then
return api.nvim_err_writeln('Could not rename '..node.absolute_path..' to '..new_name)
end
api.nvim_out_write(node.absolute_path..''..new_name..'\n')
for _, buf in pairs(api.nvim_list_bufs()) do
if api.nvim_buf_is_loaded(buf) then
if api.nvim_buf_get_name(buf) == node.absolute_path then
api.nvim_buf_set_name(buf, new_name)
-- to avoid the 'overwrite existing file' error message on write
vim.api.nvim_buf_call(buf, function() vim.cmd("silent! w!") end)
end
end
end
refresh_tree()
end
refresh_tree()
end
function M.copy(node)

View File

@@ -272,6 +272,7 @@ local function set_mappings()
[bindings.create] = 'on_keypress("create")';
[bindings.remove] = 'on_keypress("remove")';
[bindings.rename] = 'on_keypress("rename")';
[bindings.full_rename] = 'on_keypress("full_rename")';
[bindings.preview] = 'on_keypress("preview")';
[bindings.cut] = 'on_keypress("cut")';
[bindings.copy] = 'on_keypress("copy")';

View File

@@ -65,7 +65,8 @@ end
local keypress_funcs = {
create = fs.create,
remove = fs.remove,
rename = fs.rename,
rename = fs.rename(false),
full_rename = fs.rename(true),
copy = fs.copy,
cut = fs.cut,
paste = fs.paste,