fix(#1831): improve fs_scandir error handling, add profiling

This commit is contained in:
Alexander Courtis 2022-12-17 16:59:09 +11:00
parent 87409bb4af
commit 89c79cb33b
5 changed files with 62 additions and 14 deletions

View File

@ -18,7 +18,7 @@ local function populate_children(handle, cwd, node, git_status)
local nodes_by_path = utils.bool_record(node.nodes, "absolute_path")
local filter_status = filters.prepare(git_status)
while true do
local name, t = vim.loop.fs_scandir_next(handle)
local name, t = utils.fs_scandir_next_profiled(handle, cwd)
if not name then
break
end
@ -47,9 +47,9 @@ local function populate_children(handle, cwd, node, git_status)
end
local function get_dir_handle(cwd)
local handle = vim.loop.fs_scandir(cwd)
if type(handle) == "string" then
notify.error(handle)
local handle, err = utils.fs_scandir_profiled(cwd)
if err then
notify.error(string.format("Failed exploring %s: %s", cwd, vim.inspect(err)))
return
end
return handle

View File

@ -7,8 +7,8 @@ local M = {
}
function M.folder(parent, absolute_path, name)
local handle = vim.loop.fs_scandir(absolute_path)
local has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil
local handle = utils.fs_scandir_profiled(absolute_path)
local has_children = handle and utils.fs_scandir_next_profiled(handle, absolute_path) ~= nil
local node = {
type = "directory",
@ -71,8 +71,8 @@ function M.link(parent, absolute_path, name)
local is_dir_link = (link_to ~= nil) and vim.loop.fs_stat(link_to).type == "directory"
if is_dir_link then
local handle = vim.loop.fs_scandir(link_to)
has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil
local handle = utils.fs_scandir_profiled(link_to)
has_children = handle and utils.fs_scandir_next_profiled(handle, link_to) ~= nil
open = false
nodes = {}
end

View File

@ -36,9 +36,9 @@ end
function M.reload(node, git_status, unloaded_bufnr)
local cwd = node.link_to or node.absolute_path
local handle = vim.loop.fs_scandir(cwd)
if type(handle) == "string" then
notify.error(handle)
local handle, err = utils.fs_scandir_profiled(cwd)
if err then
notify.error(string.format("Failed reloading %s: %s", cwd, vim.inspect(err)))
return
end
@ -56,8 +56,8 @@ function M.reload(node, git_status, unloaded_bufnr)
local node_ignored = node.git_status == "!!"
local nodes_by_path = utils.key_by(node.nodes, "absolute_path")
while true do
local ok, name, t = pcall(vim.loop.fs_scandir_next, handle)
if not ok or not name then
local name, t = utils.fs_scandir_next_profiled(handle, cwd)
if not name then
break
end

View File

@ -24,7 +24,7 @@ do
if has_notify and notify_plugin then
notify_plugin(msg, level, { title = "NvimTree" })
else
vim.notify("[NvimTree] " .. msg, level)
vim.notify(string.format("[NvimTree] %s", vim.inspect(msg)), level)
end
end)
end

View File

@ -1,5 +1,6 @@
local Iterator = require "nvim-tree.iterators.node-iterator"
local notify = require "nvim-tree.notify"
local log = require "nvim-tree.log"
local M = {
debouncers = {},
@ -477,4 +478,51 @@ function M.is_nvim_tree_buf(bufnr)
return false
end
---Profile a call to vim.loop.fs_scandir
---This should be removed following resolution of #1831
---@param path string
---@return userdata|nil uv_fs_t
---@return string|nil type
---@return string|nil err (fail)
---@return string|nil name (fail)
function M.fs_scandir_profiled(path)
local pn = string.format("fs_scandir %s", path)
local ps = log.profile_start(pn)
local handle, err, name = vim.loop.fs_scandir(path)
if err or name then
log.line("profile", " %s err '%s'", pn, vim.inspect(err))
log.line("profile", " %s name '%s'", pn, vim.inspect(name))
end
log.profile_end(ps, pn)
return handle, err, name
end
---Profile a call to vim.loop.fs_scandir_next
---This should be removed following resolution of #1831
---@param handle userdata uv_fs_t
---@param tag string arbitrary
---@return string|nil name
---@return string|nil type
---@return string|nil err (fail)
---@return string|nil name (fail)
function M.fs_scandir_next_profiled(handle, tag)
local pn = string.format("fs_scandir_next %s", tag)
local ps = log.profile_start(pn)
local n, t, err, name = vim.loop.fs_scandir_next(handle)
if err or name then
log.line("profile", " %s err '%s'", pn, vim.inspect(err))
log.line("profile", " %s name '%s'", pn, vim.inspect(name))
end
log.profile_end(ps, pn)
return n, t, err, name
end
return M