Skip to content

Commit 3d72e5c

Browse files
committed
refactor(#2883): multi instance explore
1 parent 03f737e commit 3d72e5c

File tree

1 file changed

+110
-13
lines changed

1 file changed

+110
-13
lines changed

Diff for: lua/nvim-tree/explorer/init.lua

+110-13
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ local config
3030
---@field clipboard Clipboard
3131
local Explorer = {}
3232

33-
Explorer.explore = require("nvim-tree.explorer.explore").explore
34-
3533
---@param path string|nil
3634
---@return Explorer|nil
3735
function Explorer:new(path)
@@ -264,17 +262,7 @@ end
264262
function Explorer:_load(node)
265263
local cwd = node.link_to or node.absolute_path
266264
local git_status = git.load_project_status(cwd)
267-
Explorer.explore(node, git_status, self)
268-
end
269-
270-
function Explorer.setup(opts)
271-
config = opts
272-
require("nvim-tree.explorer.node").setup(opts)
273-
require("nvim-tree.explorer.explore").setup(opts)
274-
require("nvim-tree.explorer.watch").setup(opts)
275-
276-
Marks = require "nvim-tree.marks"
277-
Clipboard = require "nvim-tree.actions.fs.clipboard"
265+
self:explore(node, git_status, self)
278266
end
279267

280268
---@private
@@ -337,4 +325,113 @@ function Explorer:update_parent_statuses(node, project, root)
337325
end
338326
end
339327

328+
---@private
329+
---@param handle uv.uv_fs_t
330+
---@param cwd string
331+
---@param node Node
332+
---@param git_status table
333+
---@param parent Explorer
334+
function Explorer:populate_children(handle, cwd, node, git_status, parent)
335+
local node_ignored = explorer_node.is_git_ignored(node)
336+
local nodes_by_path = utils.bool_record(node.nodes, "absolute_path")
337+
338+
local filter_status = parent.filters:prepare(git_status)
339+
340+
node.hidden_stats = vim.tbl_deep_extend("force", node.hidden_stats or {}, {
341+
git = 0,
342+
buf = 0,
343+
dotfile = 0,
344+
custom = 0,
345+
bookmark = 0,
346+
})
347+
348+
while true do
349+
local name, t = vim.loop.fs_scandir_next(handle)
350+
if not name then
351+
break
352+
end
353+
354+
local abs = utils.path_join { cwd, name }
355+
356+
if Watcher.is_fs_event_capable(abs) then
357+
local profile = log.profile_start("populate_children %s", abs)
358+
359+
---@type uv.fs_stat.result|nil
360+
local stat = vim.loop.fs_stat(abs)
361+
local filter_reason = parent.filters:should_filter_as_reason(abs, stat, filter_status)
362+
if filter_reason == FILTER_REASON.none and not nodes_by_path[abs] then
363+
local child = nil
364+
if t == "directory" and vim.loop.fs_access(abs, "R") then
365+
child = builders.folder(node, abs, name, stat)
366+
elseif t == "file" then
367+
child = builders.file(node, abs, name, stat)
368+
elseif t == "link" then
369+
local link = builders.link(node, abs, name, stat)
370+
if link.link_to ~= nil then
371+
child = link
372+
end
373+
end
374+
if child then
375+
table.insert(node.nodes, child)
376+
nodes_by_path[child.absolute_path] = true
377+
explorer_node.update_git_status(child, node_ignored, git_status)
378+
end
379+
else
380+
for reason, value in pairs(FILTER_REASON) do
381+
if filter_reason == value then
382+
node.hidden_stats[reason] = node.hidden_stats[reason] + 1
383+
end
384+
end
385+
end
386+
387+
log.profile_end(profile)
388+
end
389+
end
390+
end
391+
392+
---@private
393+
---@param node Node
394+
---@param status table
395+
---@param parent Explorer
396+
---@return Node[]|nil
397+
function Explorer:explore(node, status, parent)
398+
local cwd = node.link_to or node.absolute_path
399+
local handle = vim.loop.fs_scandir(cwd)
400+
if not handle then
401+
return
402+
end
403+
404+
local profile = log.profile_start("explore %s", node.absolute_path)
405+
406+
self:populate_children(handle, cwd, node, status, parent)
407+
408+
local is_root = not node.parent
409+
local child_folder_only = explorer_node.has_one_child_folder(node) and node.nodes[1]
410+
if config.renderer.group_empty and not is_root and child_folder_only then
411+
local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path
412+
local child_status = git.load_project_status(child_cwd)
413+
node.group_next = child_folder_only
414+
local ns = self:explore(child_folder_only, child_status, parent)
415+
node.nodes = ns or {}
416+
417+
log.profile_end(profile)
418+
return ns
419+
end
420+
421+
parent.sorters:sort(node.nodes)
422+
parent.live_filter:apply_filter(node)
423+
424+
log.profile_end(profile)
425+
return node.nodes
426+
end
427+
428+
function Explorer.setup(opts)
429+
config = opts
430+
require("nvim-tree.explorer.node").setup(opts)
431+
require("nvim-tree.explorer.watch").setup(opts)
432+
433+
Marks = require "nvim-tree.marks"
434+
Clipboard = require "nvim-tree.actions.fs.clipboard"
435+
end
436+
340437
return Explorer

0 commit comments

Comments
 (0)