refacto: create actions folder and move some code to it

- move trash.lua into actions
- move system-open from root file to actions
- move copypasta code from fs to actions
- add file_exists in utils
- add rename_loaded_buffers in utils (might need to move that in the
  future).
This commit is contained in:
kiyan
2022-01-21 11:25:49 +01:00
parent 691a1c9699
commit 1248bcec86
7 changed files with 313 additions and 269 deletions

View File

@@ -0,0 +1,172 @@
local a = vim.api
local uv = vim.loop
local lib = require'nvim-tree.lib'
local utils = require'nvim-tree.utils'
local M = {}
local clipboard = {
move = {},
copy = {}
}
local function do_copy(source, destination)
local source_stats = uv.fs_stat(source)
if source_stats and source_stats.type == 'file' then
return uv.fs_copyfile(source, destination)
end
local handle = uv.fs_scandir(source)
if type(handle) == 'string' then
return false, handle
end
uv.fs_mkdir(destination, source_stats.mode)
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})
local success, msg = do_copy(new_name, new_destination)
if not success then return success, msg end
end
return true
end
local function do_single_paste(source, dest, action_type, action_fn)
local dest_stats = uv.fs_stat(dest)
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
local success, errmsg = action_fn(source, dest)
if not success then
a.nvim_err_writeln('Could not '..action_type..' '..source..' - '..errmsg)
end
end
end
local function add_to_clipboard(node, clip)
if node.name == '..' then return end
for idx, entry in ipairs(clip) do
if entry.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 = uv.fs_stat(destination)
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 _, entry in ipairs(clip) do
local dest = utils.path_join({destination, entry.name })
do_single_paste(entry.absolute_path, dest, action_type, action_fn)
end
clipboard[action_type] = {}
return lib.refresh_tree()
end
local function do_cut(source, destination)
local success = uv.fs_rename(source, destination)
if not success then
return success
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)
vim.fn.setreg('+', content);
vim.fn.setreg('"', content);
return a.nvim_out_write(string.format('Copied %s to system clipboard! \n', content))
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, lib.Tree.cwd)
local content = node.entries ~= 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.entries ~= nil and utils.path_add_trailing(absolute_path) or absolute_path
return copy_to_clipboard(content)
end
return M

View File

@@ -0,0 +1,8 @@
local M = {}
function M.setup(opts)
require'nvim-tree.actions.system-open'.setup(opts.system_open)
require'nvim-tree.actions.trash'.setup(opts.trash)
end
return M

View File

@@ -0,0 +1,68 @@
local uv = vim.loop
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,
}
}
function M.fn(node)
if not M.config.system_open.cmd then
require'nvim-tree.utils'.warn("Cannot open file with system application. Unrecognized platform.")
return
end
local process = {
cmd = M.config.system_open.cmd,
args = M.config.system_open.args,
errors = '\n',
stderr = uv.new_pipe(false)
}
table.insert(process.args, node.link_to or node.absolute_path)
process.handle, process.pid = uv.spawn(process.cmd,
{ args = process.args, stdio = { nil, nil, process.stderr }, detached = true },
function(code)
process.stderr:read_stop()
process.stderr:close()
process.handle:close()
if code ~= 0 then
process.errors = process.errors .. string.format('NvimTree system_open: return code %d.', code)
error(process.errors)
end
end
)
table.remove(process.args)
if not process.handle then
error("\n" .. process.pid .. "\nNvimTree system_open: failed to spawn process using '" .. process.cmd .. "'.")
return
end
uv.read_start(process.stderr,
function(err, data)
if err then return end
if data then process.errors = process.errors .. data end
end
)
uv.unref(process.handle)
end
function M.setup(opts)
M.config.system_open = opts or {}
if not M.config.system_open.cmd then
if M.config.is_windows then
M.config.system_open = {
cmd = "cmd",
args = {'/c', 'start', '""'}
}
elseif M.config.is_macos then
M.config.system_open.cmd = 'open'
elseif M.config.is_unix then
M.config.system_open.cmd = 'xdg-open'
end
end
end
return M

View File

@@ -0,0 +1,84 @@
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 lib = require'nvim-tree.lib'
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.trash_node(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
-- 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,
})
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.entries ~= nil and not node.link_to then
trash_path(function()
events._dispatch_folder_removed(node.absolute_path)
lib.refresh_tree()
end)
else
trash_path(function()
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
lib.refresh_tree()
end)
end
end
end
function M.setup(opts)
M.config.trash = opts or {}
end
return M