refactor(actions): move actions into semantic modules (#1410)

This commit is contained in:
Kiyan
2022-07-10 09:47:52 +02:00
committed by GitHub
parent 90bf14014e
commit 831f1158c3
23 changed files with 85 additions and 76 deletions

View File

@@ -0,0 +1,250 @@
local a = vim.api
local uv = vim.loop
local lib = require "nvim-tree.lib"
local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local M = {}
local clipboard = {
move = {},
copy = {},
}
local function do_copy(source, destination)
local source_stats, handle
local success, errmsg
source_stats, errmsg = uv.fs_stat(source)
if not source_stats then
log.line("copy_paste", "do_copy fs_stat '%s' failed '%s'", source, errmsg)
return false, errmsg
end
log.line("copy_paste", "do_copy %s '%s' -> '%s'", source_stats.type, source, destination)
if source == destination then
log.line("copy_paste", "do_copy source and destination are the same, exiting early")
return true
end
if source_stats.type == "file" then
success, errmsg = uv.fs_copyfile(source, destination)
if not success then
log.line("copy_paste", "do_copy fs_copyfile failed '%s'", errmsg)
return false, errmsg
end
return true
elseif source_stats.type == "directory" then
handle, errmsg = uv.fs_scandir(source)
if type(handle) == "string" then
return false, handle
elseif not handle then
log.line("copy_paste", "do_copy fs_scandir '%s' failed '%s'", source, errmsg)
return false, errmsg
end
success, errmsg = uv.fs_mkdir(destination, source_stats.mode)
if not success then
log.line("copy_paste", "do_copy fs_mkdir '%s' failed '%s'", destination, errmsg)
return false, errmsg
end
while true do
local name, _ = uv.fs_scandir_next(handle)
if not name then
break
end
local new_name = utils.path_join { source, name }
local new_destination = utils.path_join { destination, name }
success, errmsg = do_copy(new_name, new_destination)
if not success then
return false, errmsg
end
end
else
errmsg = string.format("'%s' illegal file type '%s'", source, source_stats.type)
log.line("copy_paste", "do_copy %s", errmsg)
return false, errmsg
end
return true
end
local function do_single_paste(source, dest, action_type, action_fn)
local dest_stats
local success, errmsg, errcode
log.line("copy_paste", "do_single_paste '%s' -> '%s'", source, dest)
dest_stats, errmsg, errcode = uv.fs_stat(dest)
if not dest_stats and errcode ~= "ENOENT" then
a.nvim_err_writeln("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
return false, errmsg
end
local should_process = true
local should_rename = false
if dest_stats then
print(dest .. " already exists. Overwrite? y/n/r(ename)")
local ans = utils.get_user_input_char()
utils.clear_prompt()
should_process = ans:match "^y"
should_rename = ans:match "^r"
end
if should_rename then
local new_dest = vim.fn.input("New name: ", dest)
return do_single_paste(source, new_dest, action_type, action_fn)
end
if should_process then
success, errmsg = action_fn(source, dest)
if not success then
a.nvim_err_writeln("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
return false, errmsg
end
end
end
local function add_to_clipboard(node, clip)
if node.name == ".." then
return
end
for idx, _node in ipairs(clip) do
if _node.absolute_path == node.absolute_path then
table.remove(clip, idx)
return a.nvim_out_write(node.absolute_path .. " removed to clipboard.\n")
end
end
table.insert(clip, node)
a.nvim_out_write(node.absolute_path .. " added to clipboard.\n")
end
function M.copy(node)
add_to_clipboard(node, clipboard.copy)
end
function M.cut(node)
add_to_clipboard(node, clipboard.move)
end
local function do_paste(node, action_type, action_fn)
node = lib.get_last_group_node(node)
if node.name == ".." then
return
end
local clip = clipboard[action_type]
if #clip == 0 then
return
end
local destination = node.absolute_path
local stats, errmsg, errcode = uv.fs_stat(destination)
if not stats and errcode ~= "ENOENT" then
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg)
a.nvim_err_writeln("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
return
end
local is_dir = stats and stats.type == "directory"
if not is_dir then
destination = vim.fn.fnamemodify(destination, ":p:h")
elseif not node.open then
destination = vim.fn.fnamemodify(destination, ":p:h:h")
end
for _, _node in ipairs(clip) do
local dest = utils.path_join { destination, _node.name }
do_single_paste(_node.absolute_path, dest, action_type, action_fn)
end
clipboard[action_type] = {}
if M.enable_reload then
return require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end
end
local function do_cut(source, destination)
log.line("copy_paste", "do_cut '%s' -> '%s'", source, destination)
if source == destination then
log.line("copy_paste", "do_cut source and destination are the same, exiting early")
return true
end
local success, errmsg = uv.fs_rename(source, destination)
if not success then
log.line("copy_paste", "do_cut fs_rename failed '%s'", errmsg)
return false, errmsg
end
utils.rename_loaded_buffers(source, destination)
return true
end
function M.paste(node)
if clipboard.move[1] ~= nil then
return do_paste(node, "move", do_cut)
end
return do_paste(node, "copy", do_copy)
end
function M.print_clipboard()
local content = {}
if #clipboard.move > 0 then
table.insert(content, "Cut")
for _, item in pairs(clipboard.move) do
table.insert(content, " * " .. item.absolute_path)
end
end
if #clipboard.copy > 0 then
table.insert(content, "Copy")
for _, item in pairs(clipboard.copy) do
table.insert(content, " * " .. item.absolute_path)
end
end
return a.nvim_out_write(table.concat(content, "\n") .. "\n")
end
local function copy_to_clipboard(content)
if M.use_system_clipboard == true then
vim.fn.setreg("+", content)
vim.fn.setreg('"', content)
return a.nvim_out_write(string.format("Copied %s to system clipboard! \n", content))
else
vim.fn.setreg('"', content)
vim.fn.setreg("1", content)
return a.nvim_out_write(string.format("Copied %s to neovim clipboard \n", content))
end
end
function M.copy_filename(node)
return copy_to_clipboard(node.name)
end
function M.copy_path(node)
local absolute_path = node.absolute_path
local relative_path = utils.path_relative(absolute_path, core.get_cwd())
local content = node.nodes ~= nil and utils.path_add_trailing(relative_path) or relative_path
return copy_to_clipboard(content)
end
function M.copy_absolute_path(node)
local absolute_path = node.absolute_path
local content = node.nodes ~= nil and utils.path_add_trailing(absolute_path) or absolute_path
return copy_to_clipboard(content)
end
function M.setup(opts)
M.use_system_clipboard = opts.actions.use_system_clipboard
M.enable_reload = not opts.filesystem_watchers.enable
end
return M

View File

@@ -0,0 +1,125 @@
local a = vim.api
local uv = vim.loop
local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
local lib = require "nvim-tree.lib"
local core = require "nvim-tree.core"
local M = {}
local function focus_file(file)
local _, i = utils.find_node(core.get_explorer().nodes, function(node)
return node.absolute_path == file
end)
require("nvim-tree.view").set_cursor { i + 1, 1 }
end
local function create_file(file)
if utils.file_exists(file) then
print(file .. " already exists. Overwrite? y/n")
local ans = utils.get_user_input_char()
utils.clear_prompt()
if ans ~= "y" then
return
end
end
local ok, fd = pcall(uv.fs_open, file, "w", 420)
if not ok then
a.nvim_err_writeln("Couldn't create file " .. file)
return
end
uv.fs_close(fd)
events._dispatch_file_created(file)
end
local function get_num_nodes(iter)
local i = 0
for _ in iter do
i = i + 1
end
return i
end
local function get_containing_folder(node)
local is_open = M.create_in_closed_folder or node.open
if node.nodes ~= nil and is_open then
return utils.path_add_trailing(node.absolute_path)
end
local node_name_size = #(node.name or "")
return node.absolute_path:sub(0, -node_name_size - 1)
end
function M.fn(node)
node = lib.get_last_group_node(node)
if node.name == ".." then
node = {
absolute_path = core.get_cwd(),
nodes = core.get_explorer().nodes,
open = true,
}
end
local containing_folder = get_containing_folder(node)
local input_opts = { prompt = "Create file ", default = containing_folder, completion = "file" }
vim.ui.input(input_opts, function(new_file_path)
if not new_file_path or new_file_path == containing_folder then
return
end
utils.clear_prompt()
if utils.file_exists(new_file_path) then
utils.warn "Cannot create: file already exists"
return
end
-- create a folder for each path element if the folder does not exist
-- if the answer ends with a /, create a file for the last path element
local is_last_path_file = not new_file_path:match(utils.path_separator .. "$")
local path_to_create = ""
local idx = 0
local num_nodes = get_num_nodes(utils.path_split(utils.path_remove_trailing(new_file_path)))
local is_error = false
for path in utils.path_split(new_file_path) do
idx = idx + 1
local p = utils.path_remove_trailing(path)
if #path_to_create == 0 and vim.fn.has "win32" == 1 then
path_to_create = utils.path_join { p, path_to_create }
else
path_to_create = utils.path_join { path_to_create, p }
end
if is_last_path_file and idx == num_nodes then
create_file(path_to_create)
elseif not utils.file_exists(path_to_create) then
local success = uv.fs_mkdir(path_to_create, 493)
if not success then
a.nvim_err_writeln("Could not create folder " .. path_to_create)
is_error = true
break
end
end
end
if not is_error then
a.nvim_out_write(new_file_path .. " was properly created\n")
end
events._dispatch_folder_created(new_file_path)
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end
-- INFO: defer needed when reload is automatic (watchers)
vim.defer_fn(function()
focus_file(new_file_path)
end, 50)
end)
end
function M.setup(opts)
M.create_in_closed_folder = opts.create_in_closed_folder
M.enable_reload = not opts.filesystem_watchers.enable
end
return M

View File

@@ -0,0 +1,100 @@
local a = vim.api
local luv = vim.loop
local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
local M = {}
local function close_windows(windows)
for _, window in ipairs(windows) do
if a.nvim_win_is_valid(window) then
a.nvim_win_close(window, true)
end
end
end
local function clear_buffer(absolute_path)
local bufs = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
for _, buf in pairs(bufs) do
if buf.name == absolute_path then
if buf.hidden == 0 and #bufs > 1 then
local winnr = a.nvim_get_current_win()
a.nvim_set_current_win(buf.windows[1])
vim.cmd ":bn"
a.nvim_set_current_win(winnr)
end
a.nvim_buf_delete(buf.bufnr, { force = true })
if M.close_window then
close_windows(buf.windows)
end
return
end
end
end
local function remove_dir(cwd)
local handle = luv.fs_scandir(cwd)
if type(handle) == "string" then
return a.nvim_err_writeln(handle)
end
while true do
local name, t = luv.fs_scandir_next(handle)
if not name then
break
end
local new_cwd = utils.path_join { cwd, name }
if t == "directory" then
local success = remove_dir(new_cwd)
if not success then
return false
end
else
local success = luv.fs_unlink(new_cwd)
if not success then
return false
end
clear_buffer(new_cwd)
end
end
return luv.fs_rmdir(cwd)
end
function M.fn(node)
if node.name == ".." then
return
end
print("Remove " .. node.name .. " ? y/n")
local ans = utils.get_user_input_char()
utils.clear_prompt()
if ans:match "^y" then
if node.nodes ~= nil and not node.link_to then
local success = remove_dir(node.absolute_path)
if not success then
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 a.nvim_err_writeln("Could not remove " .. node.name)
end
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
end
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end
end
end
function M.setup(opts)
M.enable_reload = not opts.filesystem_watchers.enable
M.close_window = opts.actions.remove_file.close_window
end
return M

View File

@@ -0,0 +1,51 @@
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 input_opts = { prompt = "Rename to ", default = abs_path, completion = "file" }
vim.ui.input(input_opts, function(new_file_path)
if not new_file_path then
return
end
if utils.file_exists(new_file_path) then
utils.warn "Cannot rename: file already exists"
return
end
local success = uv.fs_rename(node.absolute_path, new_file_path)
if not success then
return a.nvim_err_writeln("Could not rename " .. node.absolute_path .. " to " .. new_file_path)
end
utils.clear_prompt()
a.nvim_out_write(node.absolute_path .. "" .. new_file_path .. "\n")
utils.rename_loaded_buffers(node.absolute_path, new_file_path)
events._dispatch_node_renamed(abs_path, new_file_path)
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end
end)
end
end
function M.setup(opts)
M.enable_reload = not opts.filesystem_watchers.enable
end
return M

View File

@@ -0,0 +1,115 @@
local a = vim.api
local M = {
config = {
is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1,
is_macos = vim.fn.has "mac" == 1 or vim.fn.has "macunix" == 1,
is_unix = vim.fn.has "unix" == 1,
},
}
local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
local function clear_buffer(absolute_path)
local bufs = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
for _, buf in pairs(bufs) do
if buf.name == absolute_path then
if buf.hidden == 0 and #bufs > 1 then
local winnr = a.nvim_get_current_win()
a.nvim_set_current_win(buf.windows[1])
vim.cmd ":bn"
a.nvim_set_current_win(winnr)
end
vim.api.nvim_buf_delete(buf.bufnr, {})
return
end
end
end
function M.fn(node)
if node.name == ".." then
return
end
-- configs
if M.config.is_unix then
if M.config.trash.cmd == nil then
M.config.trash.cmd = "trash"
end
if M.config.trash.require_confirm == nil then
M.config.trash.require_confirm = true
end
else
utils.warn "Trash is currently a UNIX only feature!"
return
end
local binary = M.config.trash.cmd:gsub(" .*$", "")
if vim.fn.executable(binary) == 0 then
utils.warn(binary .. " is not executable.")
return
end
local err_msg = ""
local function on_stderr(_, data)
err_msg = err_msg .. (data and table.concat(data, " "))
end
-- trashes a path (file or folder)
local function trash_path(on_exit)
vim.fn.jobstart(M.config.trash.cmd .. ' "' .. node.absolute_path .. '"', {
detach = true,
on_exit = on_exit,
on_stderr = on_stderr,
})
end
local is_confirmed = true
-- confirmation prompt
if M.config.trash.require_confirm then
is_confirmed = false
print("Trash " .. node.name .. " ? y/n")
local ans = utils.get_user_input_char()
if ans:match "^y" then
is_confirmed = true
end
utils.clear_prompt()
end
-- trashing
if is_confirmed then
if node.nodes ~= nil and not node.link_to then
trash_path(function(_, rc)
if rc ~= 0 then
utils.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
return
end
events._dispatch_folder_removed(node.absolute_path)
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end
end)
else
trash_path(function(_, rc)
if rc ~= 0 then
utils.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
return
end
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end
end)
end
end
end
function M.setup(opts)
M.config.trash = opts.trash or {}
M.enable_reload = not opts.filesystem_watchers.enable
end
return M