refacto: extract functions in explorer

This commit is contained in:
kiyan
2022-02-20 15:24:23 +01:00
parent 527d88d54e
commit ec7043c53f

View File

@@ -7,22 +7,18 @@ local builders = require'nvim-tree.explorer.node-builders'
local M = {} local M = {}
function M.explore(node, status) local function get_type_from(type_, cwd)
local cwd = node.cwd or node.link_to or node.absolute_path return type_ or (uv.fs_stat(cwd) or {}).type
local handle = uv.fs_scandir(cwd) end
if type(handle) == 'string' then
api.nvim_err_writeln(handle)
return
end
local function populate_children(handle, cwd, node, status)
local node_ignored = node.git_status == '!!' local node_ignored = node.git_status == '!!'
while true do while true do
local name, t = uv.fs_scandir_next(handle) local name, t = uv.fs_scandir_next(handle)
if not name then break end if not name then break end
local abs = utils.path_join({cwd, name}) local abs = utils.path_join({cwd, name})
t = t or (uv.fs_stat(abs) or {}).type t = get_type_from(t, abs)
if not eutils.should_ignore(abs) and not eutils.should_ignore_git(abs, status.files) then if 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 if t == 'directory' and uv.fs_access(abs, 'R') then
table.insert(node.nodes, builders.folder(abs, name, status, node_ignored)) table.insert(node.nodes, builders.folder(abs, name, status, node_ignored))
@@ -36,6 +32,23 @@ function M.explore(node, status)
end end
end end
end end
end
local function get_dir_handle(cwd)
local handle = uv.fs_scandir(cwd)
if type(handle) == 'string' then
api.nvim_err_writeln(handle)
return
end
return handle
end
function M.explore(node, status)
local cwd = node.cwd or node.link_to or node.absolute_path
local handle = get_dir_handle(cwd)
if not handle then return end
populate_children(handle, cwd, node, status)
local is_root = node.cwd ~= nil local is_root = node.cwd ~= nil
local child_folder_only = eutils.has_one_child_folder(node) and node.nodes[1] local child_folder_only = eutils.has_one_child_folder(node) and node.nodes[1]