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