Files
nvim-tree.lua/lua/nvim-tree/explorer/reload.lua
kiyan e1c3744631 refacto: rewrite reloader
next step needs to merge the reloader and the explorer, the ancient code
was super complicated and long and the new one is very similar to the explorer.
2022-02-06 23:18:12 +01:00

76 lines
2.0 KiB
Lua

local api = vim.api
local uv = vim.loop
local utils = require'nvim-tree.utils'
local eutils = require'nvim-tree.explorer.utils'
local builders = require'nvim-tree.explorer.node-builders'
local M = {}
local function key_by(nodes, key)
local v = {}
for _, node in ipairs(nodes) do
v[node[key]] = node
end
return v
end
function M.reload(node, cwd, status)
local handle = uv.fs_scandir(cwd)
if type(handle) == 'string' then
api.nvim_err_writeln(handle)
return
end
if node.group_next then
node.group_next = nil
node.nodes = {}
end
local child_names = {}
local node_ignored = node.git_status == '!!'
local nodes_by_path = key_by(node.nodes, "absolute_path")
while true do
local name, t = uv.fs_scandir_next(handle)
if not name then break end
local abs = utils.path_join({cwd, name})
t = t or (uv.fs_stat(abs) or {}).type
child_names[abs] = true
if not nodes_by_path[abs] and not eutils.should_ignore(abs) and not eutils.should_ignore_git(abs, status.files) then
if t == 'directory' and uv.fs_access(abs, 'R') then
table.insert(node.nodes, builders.folder(abs, name, status, node_ignored))
elseif t == 'file' then
table.insert(node.nodes, builders.file(abs, name, status, node_ignored))
elseif t == 'link' then
local link = builders.link(abs, name, status, node_ignored)
if link.link_to ~= nil then
table.insert(node.nodes, link)
end
end
end
end
for i, n in ipairs(node.nodes) do
if not child_names[n.absolute_path] then
table.remove(node.nodes, i)
end
end
if vim.g.nvim_tree_group_empty == 1 then
local child_node = node.nodes[1]
if #(node.nodes) == 1 and child_node.nodes and uv.fs_access(child_node.absolute_path, 'R') then
node.group_next = child_node
local ns = M.reload(child_node, child_node.absolute_path, status)
node.nodes = ns or {}
return ns
end
end
utils.merge_sort(node.nodes, eutils.node_comparator)
return node.nodes
end
return M