feat(#2948): add custom decorators, :help nvim-tree-decorators (#2996)

* feat(#2948): add UserDecorator, proof of concept

* feat(#2948): add UserDecorator, proof of concept

* feat(#2948): add UserDecorator, proof of concept

* feat(#2948): add UserDecorator

* feat(#2948): add UserDecorator

* feat(#2948): add UserDecorator

* feat(#2948): add Decorator node icon override

* feat(#2948): add nvim_tree.api.* node classes

* feat(#2948): extract _meta following nvim pattern

* feat(#2948): extract _meta following nvim pattern

* feat(#2948): add decorator registry and order

* feat(#2948): add decorator registry and order

* feat(#2948): tidy

* feat(#2948): document API

* feat(#2948): document API

* feat(#2948): document API

* feat(#2948): pass api nodes to user decorators

* feat(#2948): document API

* feat(#2948): use renderer.decorators to define order and register

* feat(#2948): tidy decorator args and complete documentation

* feat(#2948): decorator classes specified by prefix rather than suffix

* feat(#2948): improve doc

* feat(#2948): improve doc

* feat(#2948): improve doc

* feat(#2948): additional user decorator safety

* feat(#2948): create nvim_tree.api.decorator.UserDecorator class in API, add :extend

* feat(#2948): improve doc
This commit is contained in:
Alexander Courtis
2024-12-07 16:03:29 +11:00
committed by GitHub
parent ca7c4c33ca
commit 7a4ff1a516
25 changed files with 571 additions and 322 deletions

View File

@@ -73,9 +73,10 @@ function DirectoryLinkNode:highlighted_name()
end
---Create a sanitized partial copy of a node, populating children recursively.
---@return DirectoryLinkNode cloned
function DirectoryLinkNode:clone()
local clone = DirectoryNode.clone(self) --[[@as DirectoryLinkNode]]
---@param api_nodes table<number, nvim_tree.api.Node>? optional map of uids to api node to populate
---@return nvim_tree.api.DirectoryLinkNode cloned
function DirectoryLinkNode:clone(api_nodes)
local clone = DirectoryNode.clone(self, api_nodes) --[[@as nvim_tree.api.DirectoryLinkNode]]
clone.link_to = self.link_to
clone.fs_stat_target = self.fs_stat_target

View File

@@ -271,18 +271,20 @@ function DirectoryNode:highlighted_name()
end
---Create a sanitized partial copy of a node, populating children recursively.
---@return DirectoryNode cloned
function DirectoryNode:clone()
local clone = Node.clone(self) --[[@as DirectoryNode]]
---@param api_nodes table<number, nvim_tree.api.Node>? optional map of uids to api node to populate
---@return nvim_tree.api.DirectoryNode cloned
function DirectoryNode:clone(api_nodes)
local clone = Node.clone(self, api_nodes) --[[@as nvim_tree.api.DirectoryNode]]
clone.has_children = self.has_children
clone.group_next = nil
clone.nodes = {}
clone.open = self.open
clone.hidden_stats = nil
local clone_child
for _, child in ipairs(self.nodes) do
table.insert(clone.nodes, child:clone())
clone_child = child:clone(api_nodes)
clone_child.parent = clone
table.insert(clone.nodes, clone_child)
end
return clone

View File

@@ -58,9 +58,10 @@ function FileLinkNode:highlighted_name()
end
---Create a sanitized partial copy of a node
---@return FileLinkNode cloned
function FileLinkNode:clone()
local clone = FileNode.clone(self) --[[@as FileLinkNode]]
---@param api_nodes table<number, nvim_tree.api.Node>? optional map of uids to api node to populate
---@return nvim_tree.api.FileLinkNode cloned
function FileLinkNode:clone(api_nodes)
local clone = FileNode.clone(self, api_nodes) --[[@as nvim_tree.api.FileLinkNode]]
clone.link_to = self.link_to
clone.fs_stat_target = self.fs_stat_target

View File

@@ -94,9 +94,10 @@ function FileNode:highlighted_name()
end
---Create a sanitized partial copy of a node
---@return FileNode cloned
function FileNode:clone()
local clone = Node.clone(self) --[[@as FileNode]]
---@param api_nodes table<number, nvim_tree.api.Node>? optional map of uids to api node to populate
---@return nvim_tree.api.FileNode cloned
function FileNode:clone(api_nodes)
local clone = Node.clone(self, api_nodes) --[[@as nvim_tree.api.FileNode]]
clone.extension = self.extension

View File

@@ -2,6 +2,7 @@ local Class = require("nvim-tree.classic")
---Abstract Node class.
---@class (exact) Node: Class
---@field uid_node number vim.loop.hrtime() at construction time
---@field type "file" | "directory" | "link" uv.fs_stat.result.type
---@field explorer Explorer
---@field absolute_path string
@@ -25,6 +26,7 @@ local Node = Class:extend()
---@protected
---@param args NodeArgs
function Node:new(args)
self.uid_node = vim.loop.hrtime()
self.explorer = args.explorer
self.absolute_path = args.absolute_path
self.executable = false
@@ -112,21 +114,19 @@ end
---Highlighted name for the node
---Empty for base Node
---@return HighlightedString icon
---@return HighlightedString name
function Node:highlighted_name()
return self:highlighted_name_empty()
end
---Create a sanitized partial copy of a node, populating children recursively.
---@return Node cloned
function Node:clone()
---@type Explorer
local explorer_placeholder = nil
---@type Node
---@param api_nodes table<number, nvim_tree.api.Node>? optional map of uids to api node to populate
---@return nvim_tree.api.Node cloned
function Node:clone(api_nodes)
---@type nvim_tree.api.Node
local clone = {
uid_node = self.uid_node,
type = self.type,
explorer = explorer_placeholder,
absolute_path = self.absolute_path,
executable = self.executable,
fs_stat = self.fs_stat,
@@ -134,10 +134,13 @@ function Node:clone()
hidden = self.hidden,
name = self.name,
parent = nil,
diag_status = nil,
is_dot = self.is_dot,
diag_severity = self.diag_status and self.diag_status.value or nil,
}
if api_nodes then
api_nodes[self.uid_node] = clone
end
return clone
end

View File

@@ -2,7 +2,7 @@ local Class = require("nvim-tree.classic")
---@class (exact) LinkNode: Class
---@field link_to string
---@field protected fs_stat_target uv.fs_stat.result
---@field fs_stat_target uv.fs_stat.result
local LinkNode = Class:extend()
---@class (exact) LinkNodeArgs: NodeArgs

View File

@@ -22,4 +22,13 @@ function RootNode:destroy()
DirectoryNode.destroy(self)
end
---Create a sanitized partial copy of a node, populating children recursively.
---@param api_nodes table<number, nvim_tree.api.Node>? optional map of uids to api node to populate
---@return nvim_tree.api.RootNode cloned
function RootNode:clone(api_nodes)
local clone = DirectoryNode.clone(self, api_nodes) --[[@as nvim_tree.api.RootNode]]
return clone
end
return RootNode