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', \ 'create': 'a',
\ 'remove': 'd', \ 'remove': 'd',
\ 'rename': 'r', \ 'rename': 'r',
\ 'full_rename': '<C-r>',
\ 'cut': 'x', \ 'cut': 'x',
\ 'copy': 'c', \ 'copy': 'c',
\ 'paste': 'p', \ '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. - 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 > 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 `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 `x` to add/remove file/directory to cut clipboard
- type `c` to add/remove file/directory to copy 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) - 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 'a' to add a file
- type 'r' to rename 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 'x' to add/remove file/directory to cut clipboard
- type 'c' to add/remove file/directory to copy clipboard - type 'c' to add/remove file/directory to copy clipboard
- type 'p' to paste from clipboard. Cut clipboard has precedence over copy - 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', create = keybindings.create or 'a',
remove = keybindings.remove or 'd', remove = keybindings.remove or 'd',
rename = keybindings.rename or 'r', rename = keybindings.rename or 'r',
full_rename = keybindings.full_rename or '<C-r>',
cut = keybindings.cut or 'x', cut = keybindings.cut or 'x',
copy = keybindings.copy or 'c', copy = keybindings.copy or 'c',
paste = keybindings.paste or 'p', paste = keybindings.paste or 'p',

View File

@@ -239,11 +239,12 @@ function M.remove(node)
end end
end end
function M.rename(node) function M.rename(with_sub)
return function(node)
if node.name == '..' then return end if node.name == '..' then return end
local namelen = node.name:len() local namelen = node.name:len()
local abs_path = node.absolute_path:sub(0, namelen * (-1) -1) 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) local new_name = vim.fn.input("Rename " ..node.name.. " to ", abs_path)
clear_prompt() clear_prompt()
if not new_name or #new_name == 0 then return end if not new_name or #new_name == 0 then return end
@@ -257,11 +258,14 @@ function M.rename(node)
if api.nvim_buf_is_loaded(buf) then if api.nvim_buf_is_loaded(buf) then
if api.nvim_buf_get_name(buf) == node.absolute_path then if api.nvim_buf_get_name(buf) == node.absolute_path then
api.nvim_buf_set_name(buf, new_name) 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 end
end end
refresh_tree() refresh_tree()
end end
end
function M.copy(node) function M.copy(node)
add_to_clipboard(node, clipboard.copy) add_to_clipboard(node, clipboard.copy)

View File

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

View File

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