Skip to content

Commit 2c99276

Browse files
authored
feat: allow sources to register custom stat_providers for nodes (#1121)
1 parent 8602bfa commit 2c99276

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lua/neo-tree/ui/renderer.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ create_nodes = function(source_items, state, level)
270270
skip_node = item.skip_node,
271271
is_empty_with_hidden_root = item.is_empty_with_hidden_root,
272272
stat = item.stat,
273+
stat_provider = item.stat_provider,
273274
-- TODO: The below properties are not universal and should not be here.
274275
-- Maybe they should be moved to the "extra" field?
275276
is_link = item.is_link,

lua/neo-tree/utils/init.lua

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,42 @@ M.get_inner_win_width = function(winid)
324324
end
325325
end
326326

327+
local stat_providers = {
328+
default = function (node)
329+
return vim.loop.fs_stat(node.path)
330+
end,
331+
}
332+
327333
--- Gets the statics for a node in the file system. The `stat` object will be cached
328334
--- for the lifetime of the node.
335+
---
329336
---@param node table The Nui TreeNode node to get the stats for.
330-
---@return table stat The stat object from libuv.
337+
---@return StatTable | table
338+
---
339+
---@class StatTime
340+
--- @field sec number
341+
---
342+
---@class StatTable
343+
--- @field birthtime StatTime
344+
--- @field mtime StatTime
345+
--- @field size number
331346
M.get_stat = function (node)
332347
if node.stat == nil then
333-
local success, stat = pcall(vim.loop.fs_stat, node.path)
348+
local provider = stat_providers[node.stat_provider or "default"]
349+
local success, stat = pcall(provider, node)
334350
node.stat = success and stat or {}
335351
end
336352
return node.stat
337353
end
338354

355+
---Register a function to provide stats for a node.
356+
---@param name string The name of the stat provider.
357+
---@param func function The function to call to get the stats.
358+
M.register_stat_provider = function (name, func)
359+
stat_providers[name] = func
360+
log.debug("Registered stat provider", name)
361+
end
362+
339363
---Handles null coalescing into a table at any depth.
340364
---@param sourceObject table The table to get a vlue from.
341365
---@param valuePath string The path to the value to get.

0 commit comments

Comments
 (0)