chore: resolve undefined-field
This commit is contained in:
parent
f6e238b4bc
commit
3ec180ee05
@ -12,10 +12,14 @@ local DirectoryNode = require("nvim-tree.node.directory")
|
||||
---@alias ClipboardAction "copy" | "cut"
|
||||
---@alias ClipboardData table<ClipboardAction, Node[]>
|
||||
|
||||
---@alias ClipboardActionFn fun(source: string, dest: string): boolean, string?
|
||||
|
||||
---@class Clipboard to handle all actions.fs clipboard API
|
||||
---@field config table hydrated user opts.filters
|
||||
---@field private explorer Explorer
|
||||
---@field private data ClipboardData
|
||||
---@field private clipboard_name string
|
||||
---@field private reg string
|
||||
local Clipboard = {}
|
||||
|
||||
---@param opts table user options
|
||||
@ -29,9 +33,10 @@ function Clipboard:new(opts, explorer)
|
||||
copy = {},
|
||||
cut = {},
|
||||
},
|
||||
clipboard_name = opts.actions.use_system_clipboard and "system" or "neovim",
|
||||
reg = opts.actions.use_system_clipboard and "+" or "1",
|
||||
config = {
|
||||
filesystem_watchers = opts.filesystem_watchers,
|
||||
actions = opts.actions,
|
||||
},
|
||||
}
|
||||
|
||||
@ -45,11 +50,11 @@ end
|
||||
---@return boolean
|
||||
---@return string|nil
|
||||
local function do_copy(source, destination)
|
||||
local source_stats, errmsg = vim.loop.fs_stat(source)
|
||||
local source_stats, err = vim.loop.fs_stat(source)
|
||||
|
||||
if not source_stats then
|
||||
log.line("copy_paste", "do_copy fs_stat '%s' failed '%s'", source, errmsg)
|
||||
return false, errmsg
|
||||
log.line("copy_paste", "do_copy fs_stat '%s' failed '%s'", source, err)
|
||||
return false, err
|
||||
end
|
||||
|
||||
log.line("copy_paste", "do_copy %s '%s' -> '%s'", source_stats.type, source, destination)
|
||||
@ -61,27 +66,27 @@ local function do_copy(source, destination)
|
||||
|
||||
if source_stats.type == "file" then
|
||||
local success
|
||||
success, errmsg = vim.loop.fs_copyfile(source, destination)
|
||||
success, err = vim.loop.fs_copyfile(source, destination)
|
||||
if not success then
|
||||
log.line("copy_paste", "do_copy fs_copyfile failed '%s'", errmsg)
|
||||
return false, errmsg
|
||||
log.line("copy_paste", "do_copy fs_copyfile failed '%s'", err)
|
||||
return false, err
|
||||
end
|
||||
return true
|
||||
elseif source_stats.type == "directory" then
|
||||
local handle
|
||||
handle, errmsg = vim.loop.fs_scandir(source)
|
||||
handle, err = vim.loop.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
|
||||
log.line("copy_paste", "do_copy fs_scandir '%s' failed '%s'", source, err)
|
||||
return false, err
|
||||
end
|
||||
|
||||
local success
|
||||
success, errmsg = vim.loop.fs_mkdir(destination, source_stats.mode)
|
||||
success, err = vim.loop.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
|
||||
log.line("copy_paste", "do_copy fs_mkdir '%s' failed '%s'", destination, err)
|
||||
return false, err
|
||||
end
|
||||
|
||||
while true do
|
||||
@ -92,15 +97,15 @@ local function do_copy(source, destination)
|
||||
|
||||
local new_name = utils.path_join({ source, name })
|
||||
local new_destination = utils.path_join({ destination, name })
|
||||
success, errmsg = do_copy(new_name, new_destination)
|
||||
success, err = do_copy(new_name, new_destination)
|
||||
if not success then
|
||||
return false, errmsg
|
||||
return false, err
|
||||
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
|
||||
err = string.format("'%s' illegal file type '%s'", source, source_stats.type)
|
||||
log.line("copy_paste", "do_copy %s", err)
|
||||
return false, err
|
||||
end
|
||||
|
||||
return true
|
||||
@ -109,27 +114,25 @@ end
|
||||
---@param source string
|
||||
---@param dest string
|
||||
---@param action ClipboardAction
|
||||
---@param action_fn fun(source: string, dest: string)
|
||||
---@param action_fn ClipboardActionFn
|
||||
---@return boolean|nil -- success
|
||||
---@return string|nil -- error message
|
||||
local function do_single_paste(source, dest, action, action_fn)
|
||||
local dest_stats
|
||||
local success, errmsg, errcode
|
||||
local notify_source = notify.render_path(source)
|
||||
|
||||
log.line("copy_paste", "do_single_paste '%s' -> '%s'", source, dest)
|
||||
|
||||
dest_stats, errmsg, errcode = vim.loop.fs_stat(dest)
|
||||
if not dest_stats and errcode ~= "ENOENT" then
|
||||
notify.error("Could not " .. action .. " " .. notify_source .. " - " .. (errmsg or "???"))
|
||||
return false, errmsg
|
||||
local dest_stats, err, err_name = vim.loop.fs_stat(dest)
|
||||
if not dest_stats and err_name ~= "ENOENT" then
|
||||
notify.error("Could not " .. action .. " " .. notify_source .. " - " .. (err or "???"))
|
||||
return false, err
|
||||
end
|
||||
|
||||
local function on_process()
|
||||
success, errmsg = action_fn(source, dest)
|
||||
local success, error = action_fn(source, dest)
|
||||
if not success then
|
||||
notify.error("Could not " .. action .. " " .. notify_source .. " - " .. (errmsg or "???"))
|
||||
return false, errmsg
|
||||
notify.error("Could not " .. action .. " " .. notify_source .. " - " .. (error or "???"))
|
||||
return false, error
|
||||
end
|
||||
|
||||
find_file(utils.path_remove_trailing(dest))
|
||||
@ -216,7 +219,7 @@ end
|
||||
---@private
|
||||
---@param node Node
|
||||
---@param action ClipboardAction
|
||||
---@param action_fn fun(source: string, dest: string)
|
||||
---@param action_fn ClipboardActionFn
|
||||
function Clipboard:do_paste(node, action, action_fn)
|
||||
if node.name == ".." then
|
||||
node = self.explorer
|
||||
@ -232,10 +235,10 @@ function Clipboard:do_paste(node, action, action_fn)
|
||||
end
|
||||
|
||||
local destination = node.absolute_path
|
||||
local stats, errmsg, errcode = vim.loop.fs_stat(destination)
|
||||
if not stats and errcode ~= "ENOENT" then
|
||||
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg)
|
||||
notify.error("Could not " .. action .. " " .. notify.render_path(destination) .. " - " .. (errmsg or "???"))
|
||||
local stats, err, err_name = vim.loop.fs_stat(destination)
|
||||
if not stats and err_name ~= "ENOENT" then
|
||||
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, err)
|
||||
notify.error("Could not " .. action .. " " .. notify.render_path(destination) .. " - " .. (err or "???"))
|
||||
return
|
||||
end
|
||||
local is_dir = stats and stats.type == "directory"
|
||||
@ -307,65 +310,45 @@ end
|
||||
|
||||
---@param content string
|
||||
function Clipboard:copy_to_reg(content)
|
||||
local clipboard_name
|
||||
local reg
|
||||
if self.config.actions.use_system_clipboard == true then
|
||||
clipboard_name = "system"
|
||||
reg = "+"
|
||||
else
|
||||
clipboard_name = "neovim"
|
||||
reg = "1"
|
||||
end
|
||||
|
||||
-- manually firing TextYankPost does not set vim.v.event
|
||||
-- workaround: create a scratch buffer with the clipboard contents and send a yank command
|
||||
local temp_buf = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_text(temp_buf, 0, 0, 0, 0, { content })
|
||||
vim.api.nvim_buf_call(temp_buf, function()
|
||||
vim.cmd(string.format('normal! "%sy$', reg))
|
||||
vim.cmd(string.format('normal! "%sy$', self.reg))
|
||||
end)
|
||||
vim.api.nvim_buf_delete(temp_buf, {})
|
||||
|
||||
notify.info(string.format("Copied %s to %s clipboard!", content, clipboard_name))
|
||||
notify.info(string.format("Copied %s to %s clipboard!", content, self.clipboard_name))
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
function Clipboard:copy_filename(node)
|
||||
local content
|
||||
|
||||
if node.name == ".." then
|
||||
-- root
|
||||
content = vim.fn.fnamemodify(self.explorer.absolute_path, ":t")
|
||||
self:copy_to_reg(vim.fn.fnamemodify(self.explorer.absolute_path, ":t"))
|
||||
else
|
||||
-- node
|
||||
content = node.name
|
||||
self:copy_to_reg(node.name)
|
||||
end
|
||||
|
||||
self:copy_to_reg(content)
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
function Clipboard:copy_basename(node)
|
||||
local content
|
||||
|
||||
if node.name == ".." then
|
||||
-- root
|
||||
content = vim.fn.fnamemodify(self.explorer.absolute_path, ":t:r")
|
||||
self:copy_to_reg(vim.fn.fnamemodify(self.explorer.absolute_path, ":t:r"))
|
||||
else
|
||||
-- node
|
||||
content = vim.fn.fnamemodify(node.name, ":r")
|
||||
self:copy_to_reg(vim.fn.fnamemodify(node.name, ":r"))
|
||||
end
|
||||
|
||||
self:copy_to_reg(content)
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
function Clipboard:copy_path(node)
|
||||
local content
|
||||
|
||||
if node.name == ".." then
|
||||
-- root
|
||||
content = utils.path_add_trailing("")
|
||||
self:copy_to_reg(utils.path_add_trailing(""))
|
||||
else
|
||||
-- node
|
||||
local absolute_path = node.absolute_path
|
||||
@ -375,10 +358,12 @@ function Clipboard:copy_path(node)
|
||||
end
|
||||
|
||||
local relative_path = utils.path_relative(absolute_path, cwd)
|
||||
content = node:is(DirectoryNode) and utils.path_add_trailing(relative_path) or relative_path
|
||||
if node:is(DirectoryNode) then
|
||||
self:copy_to_reg(utils.path_add_trailing(relative_path))
|
||||
else
|
||||
self:copy_to_reg(relative_path)
|
||||
end
|
||||
end
|
||||
|
||||
self:copy_to_reg(content)
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
|
||||
Loading…
Reference in New Issue
Block a user