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

@ -60,6 +60,7 @@ let g:nvim_tree_bindings = {
\ 'create': 'a',
\ 'remove': 'd',
\ 'rename': 'r',
\ 'full_rename': '<C-r>',
\ 'cut': 'x',
\ 'copy': 'c',
\ 'paste': 'p',
@ -114,6 +115,7 @@ highlight NvimTreeFolderIcon guibg=blue
- type `a` to add a file. Adding a directory requires leaving a leading `/` at the end of the path.
> you can add multiple directories by doing foo/bar/baz/f and it will add foo bar and baz directories and f as a file
- type `r` to rename a file
- type `<C-r>` to rename a file and omit the filename on input
- type `x` to add/remove file/directory to cut clipboard
- type `c` to add/remove file/directory to copy clipboard
- type `p` to paste from clipboard. Cut clipboard has precedence over copy (will prompt for confirmation)

View File

@ -211,6 +211,7 @@ INFORMATIONS *nvim-tree-info*
- type 'a' to add a file
- type 'r' to rename a file
- type `<C-r>` to rename a file and omit the filename on input
- type 'x' to add/remove file/directory to cut clipboard
- type 'c' to add/remove file/directory to copy clipboard
- type 'p' to paste from clipboard. Cut clipboard has precedence over copy

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,