From 07da8a724225b01c580b5e5c77511f94eeb45575 Mon Sep 17 00:00:00 2001 From: kiyan Date: Mon, 22 Feb 2021 20:53:12 +0100 Subject: [PATCH] feat/fix: add rename cmd, fix rename error - add 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. --- README.md | 2 ++ doc/nvim-tree-lua.txt | 1 + lua/lib/config.lua | 1 + lua/lib/fs.lua | 38 +++++++++++++++++++++----------------- lua/lib/lib.lua | 1 + lua/nvim-tree.lua | 3 ++- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 53897076..7fced059 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ let g:nvim_tree_bindings = { \ 'create': 'a', \ 'remove': 'd', \ 'rename': 'r', + \ 'full_rename': '', \ '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 `` 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) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 432b86ac..4e1637b4 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -211,6 +211,7 @@ INFORMATIONS *nvim-tree-info* - type 'a' to add a file - type 'r' to rename a file +- type `` 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 diff --git a/lua/lib/config.lua b/lua/lib/config.lua index d4338c48..bb2ac16a 100644 --- a/lua/lib/config.lua +++ b/lua/lib/config.lua @@ -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 '', cut = keybindings.cut or 'x', copy = keybindings.copy or 'c', paste = keybindings.paste or 'p', diff --git a/lua/lib/fs.lua b/lua/lib/fs.lua index 7c6f6d18..2f3e343b 100644 --- a/lua/lib/fs.lua +++ b/lua/lib/fs.lua @@ -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) diff --git a/lua/lib/lib.lua b/lua/lib/lib.lua index 5ab25766..337081c2 100644 --- a/lua/lib/lib.lua +++ b/lua/lib/lib.lua @@ -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")'; diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 5ccfb62d..dedee9cf 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -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,