refactor(#2886): multi instance: node class refactoring (#2950)

* add todo

* refactor(#2886): multi instance: node class refactoring: extract links, *_git_status (#2944)

* extract DirectoryLinkNode and FileLinkNode, move Node methods to children

* temporarily move DirectoryNode methods into BaseNode for easier reviewing

* move mostly unchanged DirectoryNode methods back to BaseNode

* tidy

* git.git_status_file takes an array

* update git status of links

* luacheck hack

* safer git_status_dir

* refactor(#2886): multi instance: node class refactoring: DirectoryNode:expand_or_collapse (#2957)

move expand_or_collapse to DirectoryNode

* refactor(#2886): multi instance: node group functions refactoring (#2959)

* move last_group_node to DirectoryNode

* move add BaseNode:as and more doc

* revert parameter name changes

* revert parameter name changes

* add Class

* move group methods into DN

* tidy group methods

* tidy group methods

* tidy group methods

* tidy group methods

* parent is DirectoryNode

* tidy expand all

* BaseNode -> Node

* move watcher to DirectoryNode

* last_group_node is DirectoryNode only

* simplify create-file

* simplify parent

* simplify collapse-all

* simplify live-filter

* style

* more type safety
This commit is contained in:
Alexander Courtis
2024-10-25 12:24:59 +11:00
committed by GitHub
parent 63c7ad9037
commit 68be6df2fc
25 changed files with 591 additions and 482 deletions

View File

@@ -1,89 +0,0 @@
local watch = require("nvim-tree.explorer.watch")
local BaseNode = require("nvim-tree.node")
---@class (exact) LinkNode: BaseNode
---@field has_children boolean
---@field group_next Node? -- If node is grouped, this points to the next child dir/link node
---@field link_to string absolute path
---@field nodes Node[]
---@field open boolean
local LinkNode = BaseNode:new()
---Static factory method
---@param explorer Explorer
---@param parent Node
---@param absolute_path string
---@param name string
---@param fs_stat uv.fs_stat.result?
---@return LinkNode? nil on vim.loop.fs_realpath failure
function LinkNode:create(explorer, parent, absolute_path, name, fs_stat)
-- INFO: sometimes fs_realpath returns nil
-- I expect this be a bug in glibc, because it fails to retrieve the path for some
-- links (for instance libr2.so in /usr/lib) and thus even with a C program realpath fails
-- when it has no real reason to. Maybe there is a reason, but errno is definitely wrong.
local link_to = vim.loop.fs_realpath(absolute_path)
if not link_to then
return nil
end
local open, nodes, has_children
local is_dir_link = (link_to ~= nil) and vim.loop.fs_stat(link_to).type == "directory"
if is_dir_link and link_to then
local handle = vim.loop.fs_scandir(link_to)
has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil or false
open = false
nodes = {}
end
---@type LinkNode
local o = {
type = "link",
explorer = explorer,
absolute_path = absolute_path,
executable = false,
fs_stat = fs_stat,
hidden = false,
is_dot = false,
name = name,
parent = parent,
watcher = nil,
diag_status = nil,
has_children = has_children,
group_next = nil,
link_to = link_to,
nodes = nodes,
open = open,
}
o = self:new(o) --[[@as LinkNode]]
if is_dir_link then
o.watcher = watch.create_watcher(o)
end
return o
end
---Create a sanitized partial copy of a node, populating children recursively.
---@return LinkNode cloned
function LinkNode:clone()
local clone = BaseNode.clone(self) --[[@as LinkNode]]
clone.has_children = self.has_children
clone.group_next = nil
clone.link_to = self.link_to
clone.nodes = {}
clone.open = self.open
if self.nodes then
for _, child in ipairs(self.nodes) do
table.insert(clone.nodes, child:clone())
end
end
return clone
end
return LinkNode