Skip to content

Commit b2302a3

Browse files
committed
WIP, it works!
1 parent dc31895 commit b2302a3

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lua/neo-tree/sources/filesystem/lib/fs_scan.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local scan = require("plenary.scandir")
66
local filter_external = require("neo-tree.sources.filesystem.lib.filter_external")
77
local file_items = require("neo-tree.sources.common.file-items")
88
local log = require("neo-tree.log")
9+
local fs_watch = require("neo-tree.sources.filesystem.lib.fs_watch")
910

1011
local M = {}
1112

@@ -29,6 +30,10 @@ local function do_scan(context, path_to_scan)
2930
end
3031
end,
3132
on_exit = vim.schedule_wrap(function()
33+
if state.use_libuv_file_watcher then
34+
local root = context.folders[path_to_scan]
35+
fs_watch.watch_folder(root.link_to or root.path)
36+
end
3237
local scanned_folder = folders[path_to_scan]
3338
if scanned_folder then
3439
scanned_folder.loaded = true
@@ -61,6 +66,15 @@ local function do_scan(context, path_to_scan)
6166
end
6267

6368
M.get_items_async = function(state, parent_id, path_to_reveal, callback)
69+
if state.use_libuv_file_watcher and state.tree then
70+
-- unwatch any folders that were previously being watched
71+
local loaded_folders = renderer.select_nodes(state.tree, function(node)
72+
return node.type == "directory" and node.loaded
73+
end)
74+
for _, folder in ipairs(loaded_folders) do
75+
fs_watch.unwatch_folder(folder.link_to or folder:get_id())
76+
end
77+
end
6478
local context = file_items.create_context(state)
6579

6680
-- Create root folder

lua/neo-tree/ui/renderer.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,34 @@ M.collapse_all_nodes = function(tree)
269269
root:expand()
270270
end
271271

272+
---Visits all nodes in the tree and returns a list of all nodes that match the
273+
---given predicate.
274+
---@param tree table The NuiTree to search.
275+
---@param selector_func function The predicate function, should return true for
276+
---nodes that should be included in the result.
277+
---@return table table A list of nodes that match the predicate.
278+
M.select_nodes = function(tree, selector_func)
279+
if type(selector_func) ~= "function" then
280+
error("selector_func must be a function")
281+
end
282+
local found_nodes = {}
283+
local visit
284+
visit = function(node)
285+
if selector_func(node) then
286+
table.insert(found_nodes, node)
287+
end
288+
if node:has_children() then
289+
for _, child in ipairs(tree:get_nodes(node:get_id())) do
290+
visit(child)
291+
end
292+
end
293+
end
294+
for _, node in ipairs(tree:get_nodes()) do
295+
visit(node)
296+
end
297+
return found_nodes
298+
end
299+
272300
M.set_expanded_nodes = function(tree, expanded_nodes)
273301
M.collapse_all_nodes(tree)
274302
log.debug("Setting expanded nodes")

0 commit comments

Comments
 (0)