Skip to content

Commit

Permalink
feat: setup hooks for sources to provide custom stats for nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
cseickel committed Aug 27, 2023
1 parent 9b5b4c8 commit 1c28fa9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lua/neo-tree/sources/common/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

local vim = vim
local fs_actions = require("neo-tree.sources.filesystem.lib.fs_actions")
local manager = require("neo-tree.sources.manager")
local utils = require("neo-tree.utils")
local renderer = require("neo-tree.ui.renderer")
local events = require("neo-tree.events")
Expand Down Expand Up @@ -421,7 +422,7 @@ end
M.order_by_created = function (state)
set_sort(state, "Created")
state.sort_field_provider = function (node)
local stat = utils.get_stat(node)
local stat = manager.get_node_stat(state, node)
return stat.birthtime and stat.birthtime.sec or 0
end
require("neo-tree.sources.manager").refresh(state.name)
Expand All @@ -430,7 +431,7 @@ end
M.order_by_modified = function (state)
set_sort(state, "Last Modified")
state.sort_field_provider = function (node)
local stat = utils.get_stat(node)
local stat = manager.get_node_stat(state, node)
return stat.mtime and stat.mtime.sec or 0
end
require("neo-tree.sources.manager").refresh(state.name)
Expand All @@ -445,7 +446,7 @@ end
M.order_by_size = function (state)
set_sort(state, "Size")
state.sort_field_provider = function (node)
local stat = utils.get_stat(node)
local stat = manager.get_node_stat(state, node)
return stat.size or 0
end
require("neo-tree.sources.manager").refresh(state.name)
Expand Down Expand Up @@ -507,7 +508,7 @@ M.show_file_details = function (state)
if node.type == "message" then
return
end
local stat = utils.get_stat(node)
local stat = manager.get_node_stat(state, node)
local left = {}
local right = {}
table.insert(left, "Name")
Expand Down
5 changes: 3 additions & 2 deletions lua/neo-tree/sources/common/components.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local utils = require("neo-tree.utils")
local file_nesting = require("neo-tree.sources.common.file-nesting")
local container = require("neo-tree.sources.common.container")
local log = require("neo-tree.log")
local manager = require("neo-tree.sources.manager")

local M = {}

Expand Down Expand Up @@ -453,7 +454,7 @@ M.file_size = function (config, node, state)

local text = "-"
if node.type == "file" then
local stat = utils.get_stat(node)
local stat = manager.get_node_stat(state, node)
local size = stat and stat.size or nil
if size then
local success, human = pcall(utils.human_size, size)
Expand Down Expand Up @@ -484,7 +485,7 @@ local file_time = function(config, node, state, stat_field)
}
end

local stat = utils.get_stat(node)
local stat = manager.get_node_stat(state, node)
local value = stat and stat[stat_field]
local seconds = value and value.sec or nil
local display = seconds and os.date("%Y-%m-%d %I:%M %p", seconds) or "-"
Expand Down
36 changes: 36 additions & 0 deletions lua/neo-tree/sources/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,42 @@ M.set_default_config = function(source_name, config)
end
end

local get_stat_fn_cache = {}

---Get statistics for a given node, such as size, created time, etc.
---@param state table The state of the source.
---@param node table The NuiNode to get statistics for.
--
---@class StatTime
--- @field sec number
---
---@class StatTable
--- @field birthtime StatTime
--- @field mtime StatTime
--- @field size number
---
--- @return StatTable Stats for the given node.
M.get_node_stat = function(state, node)
if node.stat then
return node.stat
end
local get_stat_fn = get_stat_fn_cache[state.name]
if get_stat_fn then
return get_stat_fn(node)
end

-- first time getting stat for this source, figure out which function to use
-- and cache it.
local mod = get_source_data(state.name).module
if type(mod.get_node_stat) == "function" then
get_stat_fn = mod.get_node_stat
else
get_stat_fn = utils.get_stat
end
get_stat_fn_cache[state.name] = get_stat_fn
return get_stat_fn(node)
end

--TODO: we need to track state per window when working with netwrw style "current"
--position. How do we know which one to return when this is called?
M.get_state = function(source_name, tabid, winid)
Expand Down

0 comments on commit 1c28fa9

Please sign in to comment.