From dc5eae65123c41908139cf6f095a062760604414 Mon Sep 17 00:00:00 2001 From: kiyan Date: Fri, 21 Jan 2022 11:54:35 +0100 Subject: [PATCH] refacto: remove fs folder and move code to actions - remove file moved to actions.remove-file.lua - rename file moved to actions.rename-file.lua --- lua/nvim-tree.lua | 7 ++- .../{fs.lua => actions/remove-file.lua} | 44 ++++--------------- lua/nvim-tree/actions/rename-file.lua | 38 ++++++++++++++++ 3 files changed, 49 insertions(+), 40 deletions(-) rename lua/nvim-tree/{fs.lua => actions/remove-file.lua} (56%) create mode 100644 lua/nvim-tree/actions/rename-file.lua diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 7fe66f2a..14592231 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -5,7 +5,6 @@ local lib = require'nvim-tree.lib' local config = require'nvim-tree.config' local colors = require'nvim-tree.colors' local renderer = require'nvim-tree.renderer' -local fs = require'nvim-tree.fs' local view = require'nvim-tree.view' local utils = require'nvim-tree.utils' @@ -71,9 +70,9 @@ end local keypress_funcs = { create = require'nvim-tree.actions.create-file'.fn, - remove = fs.remove, - rename = fs.rename(false), - full_rename = fs.rename(true), + remove = require'nvim-tree.actions.remove-file'.fn, + rename = require'nvim-tree.actions.rename-file'.fn(false), + full_rename = require'nvim-tree.actions.rename-file'.fn(true), copy = require'nvim-tree.actions.copy-paste'.copy, copy_name = require'nvim-tree.actions.copy-paste'.copy_filename, copy_path = require'nvim-tree.actions.copy-paste'.copy_path, diff --git a/lua/nvim-tree/fs.lua b/lua/nvim-tree/actions/remove-file.lua similarity index 56% rename from lua/nvim-tree/fs.lua rename to lua/nvim-tree/actions/remove-file.lua index 76cf70e9..b3b388dd 100644 --- a/lua/nvim-tree/fs.lua +++ b/lua/nvim-tree/actions/remove-file.lua @@ -1,4 +1,4 @@ -local api = vim.api +local a = vim.api local luv = vim.loop local utils = require'nvim-tree.utils' @@ -12,10 +12,10 @@ local function clear_buffer(absolute_path) for _, buf in pairs(bufs) do if buf.name == absolute_path then if buf.hidden == 0 and #bufs > 1 then - local winnr = api.nvim_get_current_win() - api.nvim_set_current_win(buf.windows[1]) + local winnr = a.nvim_get_current_win() + a.nvim_set_current_win(buf.windows[1]) vim.cmd(':bn') - api.nvim_set_current_win(winnr) + a.nvim_set_current_win(winnr) end vim.api.nvim_buf_delete(buf.bufnr, {}) if buf.windows[1] then @@ -29,7 +29,7 @@ end local function remove_dir(cwd) local handle = luv.fs_scandir(cwd) if type(handle) == 'string' then - return api.nvim_err_writeln(handle) + return a.nvim_err_writeln(handle) end while true do @@ -51,7 +51,7 @@ local function remove_dir(cwd) end -function M.remove(node) +function M.fn(node) if node.name == '..' then return end print("Remove " ..node.name.. " ? y/n") @@ -61,13 +61,13 @@ function M.remove(node) if node.entries ~= nil and not node.link_to then local success = remove_dir(node.absolute_path) if not success then - return api.nvim_err_writeln('Could not remove '..node.name) + return a.nvim_err_writeln('Could not remove '..node.name) end events._dispatch_folder_removed(node.absolute_path) else local success = luv.fs_unlink(node.absolute_path) if not success then - return api.nvim_err_writeln('Could not remove '..node.name) + return a.nvim_err_writeln('Could not remove '..node.name) end events._dispatch_file_removed(node.absolute_path) clear_buffer(node.absolute_path) @@ -76,32 +76,4 @@ function M.remove(node) end end -function M.rename(with_sub) - return function(node) - node = lib.get_last_group_node(node) - if node.name == '..' 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) - utils.clear_prompt() - if not new_name or #new_name == 0 then - return - end - if utils.file_exists(new_name) then - utils.warn("Cannot rename: file already exists") - 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') - utils.rename_loaded_buffers(node.absolute_path, new_name) - events._dispatch_node_renamed(abs_path, new_name) - lib.refresh_tree() - end -end - return M diff --git a/lua/nvim-tree/actions/rename-file.lua b/lua/nvim-tree/actions/rename-file.lua new file mode 100644 index 00000000..1d72d55e --- /dev/null +++ b/lua/nvim-tree/actions/rename-file.lua @@ -0,0 +1,38 @@ +local a = vim.api +local uv = vim.loop + +local lib = require'nvim-tree.lib' +local utils = require'nvim-tree.utils' +local events = require'nvim-tree.events' + +local M = {} + +function M.fn(with_sub) + return function(node) + node = lib.get_last_group_node(node) + if node.name == '..' 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) + utils.clear_prompt() + if not new_name or #new_name == 0 then + return + end + if utils.file_exists(new_name) then + utils.warn("Cannot rename: file already exists") + return + end + + local success = uv.fs_rename(node.absolute_path, new_name) + if not success then + return a.nvim_err_writeln('Could not rename '..node.absolute_path..' to '..new_name) + end + a.nvim_out_write(node.absolute_path..' ➜ '..new_name..'\n') + utils.rename_loaded_buffers(node.absolute_path, new_name) + events._dispatch_node_renamed(abs_path, new_name) + lib.refresh_tree() + end +end + +return M