Skip to content

Commit 02e1bb1

Browse files
committed
refactor(#2883): multi instance explore
1 parent ea55ef1 commit 02e1bb1

File tree

2 files changed

+110
-127
lines changed

2 files changed

+110
-127
lines changed

lua/nvim-tree/explorer/explore.lua

-114
This file was deleted.

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)
@@ -261,17 +259,7 @@ end
261259
function Explorer:_load(node)
262260
local cwd = node.link_to or node.absolute_path
263261
local git_status = git.load_project_status(cwd)
264-
Explorer.explore(node, git_status, self)
265-
end
266-
267-
function Explorer.setup(opts)
268-
config = opts
269-
require("nvim-tree.explorer.node").setup(opts)
270-
require("nvim-tree.explorer.explore").setup(opts)
271-
require("nvim-tree.explorer.watch").setup(opts)
272-
273-
Marks = require "nvim-tree.marks"
274-
Clipboard = require "nvim-tree.actions.fs.clipboard"
262+
self:explore(node, git_status, self)
275263
end
276264

277265
---@private
@@ -334,4 +322,113 @@ function Explorer:update_parent_statuses(node, project, root)
334322
end
335323
end
336324

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

0 commit comments

Comments
 (0)