* refactor(#2875): multi instance renderer * refactor(#2875): multi instance renderer * refactor(#2875): multi instance renderer * refactor(#2875): multi instance renderer * node classes and constructors * node methods * refactor(#2875): multi instance renderer * node classes and constructors * explorer is a directory node * extract methods from explore_node * extract methods from explore_node * extract methods from explore_node * extract methods from lib * use .. name for root node for compatibility * use node.explorer * extract node factory, remove unused code * factories for all nodes, add RootNode * factories for all nodes, add RootNode * use factory pattern for decorators * note regression and commit * fix dir git status regression * destroy nodes, not explorer * add BaseNode:is * revert changes to create-file, handle in #2924 * extract methods from explorer * extract methods from explorer * extract methods from explorer * use Node everywhere in luadoc * extract methods from lib * extract methods from lib * lint * remove unused code * don't call methods on fake root node * get_node_at_cursor returns explorer (root) node instead of { name = '..' } * remove unused inject_node * refactor(#2875): multi instance renderer * refactor(#2875): multi instance renderer * refactor(#2875): multi instance renderer * extract methods from lib * node factory uses stat only * temporary DirectoryNode casting until method extraction into child classes * lua-language-server 3.10.5 -> 3.11.0 * explicitly call Explorer constructor * normalise explorer RootNode new call, tidy annotations
This commit is contained in:
committed by
GitHub
parent
c9104a5d07
commit
38aac09151
79
lua/nvim-tree/node/directory.lua
Normal file
79
lua/nvim-tree/node/directory.lua
Normal file
@@ -0,0 +1,79 @@
|
||||
local watch = require("nvim-tree.explorer.watch")
|
||||
|
||||
local BaseNode = require("nvim-tree.node")
|
||||
|
||||
---@class (exact) DirectoryNode: BaseNode
|
||||
---@field has_children boolean
|
||||
---@field group_next Node? -- If node is grouped, this points to the next child dir/link node
|
||||
---@field nodes Node[]
|
||||
---@field open boolean
|
||||
---@field hidden_stats table? -- Each field of this table is a key for source and value for count
|
||||
local DirectoryNode = 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|nil
|
||||
---@return DirectoryNode
|
||||
function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat)
|
||||
local handle = vim.loop.fs_scandir(absolute_path)
|
||||
local has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil or false
|
||||
|
||||
---@type DirectoryNode
|
||||
local o = {
|
||||
type = "directory",
|
||||
explorer = explorer,
|
||||
absolute_path = absolute_path,
|
||||
executable = false,
|
||||
fs_stat = fs_stat,
|
||||
git_status = nil,
|
||||
hidden = false,
|
||||
is_dot = false,
|
||||
name = name,
|
||||
parent = parent,
|
||||
watcher = nil,
|
||||
diag_status = nil,
|
||||
|
||||
has_children = has_children,
|
||||
group_next = nil,
|
||||
nodes = {},
|
||||
open = false,
|
||||
hidden_stats = nil,
|
||||
}
|
||||
o = self:new(o) --[[@as DirectoryNode]]
|
||||
|
||||
o.watcher = watch.create_watcher(o)
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
function DirectoryNode:destroy()
|
||||
BaseNode.destroy(self)
|
||||
if self.nodes then
|
||||
for _, node in pairs(self.nodes) do
|
||||
node:destroy()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---@return DirectoryNode cloned
|
||||
function DirectoryNode:clone()
|
||||
local clone = BaseNode.clone(self) --[[@as DirectoryNode]]
|
||||
|
||||
clone.has_children = self.has_children
|
||||
clone.group_next = nil
|
||||
clone.nodes = {}
|
||||
clone.open = self.open
|
||||
clone.hidden_stats = nil
|
||||
|
||||
for _, child in ipairs(self.nodes) do
|
||||
table.insert(clone.nodes, child:clone())
|
||||
end
|
||||
|
||||
return clone
|
||||
end
|
||||
|
||||
return DirectoryNode
|
||||
31
lua/nvim-tree/node/factory.lua
Normal file
31
lua/nvim-tree/node/factory.lua
Normal file
@@ -0,0 +1,31 @@
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
local LinkNode = require("nvim-tree.node.link")
|
||||
local FileNode = require("nvim-tree.node.file")
|
||||
local Watcher = require("nvim-tree.watcher")
|
||||
|
||||
local M = {}
|
||||
|
||||
---Factory function to create the appropriate Node
|
||||
---@param explorer Explorer
|
||||
---@param parent Node
|
||||
---@param abs string
|
||||
---@param stat uv.fs_stat.result? -- on nil stat return nil Node
|
||||
---@param name string
|
||||
---@return Node?
|
||||
function M.create_node(explorer, parent, abs, stat, name)
|
||||
if not stat then
|
||||
return nil
|
||||
end
|
||||
|
||||
if stat.type == "directory" and vim.loop.fs_access(abs, "R") and Watcher.is_fs_event_capable(abs) then
|
||||
return DirectoryNode:create(explorer, parent, abs, name, stat)
|
||||
elseif stat.type == "file" then
|
||||
return FileNode:create(explorer, parent, abs, name, stat)
|
||||
elseif stat.type == "link" then
|
||||
return LinkNode:create(explorer, parent, abs, name, stat)
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
return M
|
||||
49
lua/nvim-tree/node/file.lua
Normal file
49
lua/nvim-tree/node/file.lua
Normal file
@@ -0,0 +1,49 @@
|
||||
local utils = require("nvim-tree.utils")
|
||||
|
||||
local BaseNode = require("nvim-tree.node")
|
||||
|
||||
---@class (exact) FileNode: BaseNode
|
||||
---@field extension string
|
||||
local FileNode = 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 FileNode
|
||||
function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
|
||||
---@type FileNode
|
||||
local o = {
|
||||
type = "file",
|
||||
explorer = explorer,
|
||||
absolute_path = absolute_path,
|
||||
executable = utils.is_executable(absolute_path),
|
||||
fs_stat = fs_stat,
|
||||
git_status = nil,
|
||||
hidden = false,
|
||||
is_dot = false,
|
||||
name = name,
|
||||
parent = parent,
|
||||
watcher = nil,
|
||||
diag_status = nil,
|
||||
|
||||
extension = string.match(name, ".?[^.]+%.(.*)") or "",
|
||||
}
|
||||
o = self:new(o) --[[@as FileNode]]
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---@return FileNode cloned
|
||||
function FileNode:clone()
|
||||
local clone = BaseNode.clone(self) --[[@as FileNode]]
|
||||
|
||||
clone.extension = self.extension
|
||||
|
||||
return clone
|
||||
end
|
||||
|
||||
return FileNode
|
||||
347
lua/nvim-tree/node/init.lua
Normal file
347
lua/nvim-tree/node/init.lua
Normal file
@@ -0,0 +1,347 @@
|
||||
local git = require("nvim-tree.git")
|
||||
|
||||
---Abstract Node class.
|
||||
---Uses the abstract factory pattern to instantiate child instances.
|
||||
---@class (exact) BaseNode
|
||||
---@field private __index? table
|
||||
---@field type NODE_TYPE
|
||||
---@field explorer Explorer
|
||||
---@field absolute_path string
|
||||
---@field executable boolean
|
||||
---@field fs_stat uv.fs_stat.result?
|
||||
---@field git_status GitStatus?
|
||||
---@field hidden boolean
|
||||
---@field is_dot boolean
|
||||
---@field name string
|
||||
---@field parent Node?
|
||||
---@field watcher Watcher?
|
||||
---@field diag_status DiagStatus?
|
||||
local BaseNode = {}
|
||||
|
||||
---@alias Node RootNode|BaseNode|DirectoryNode|FileNode|LinkNode
|
||||
|
||||
---@param o BaseNode?
|
||||
---@return BaseNode
|
||||
function BaseNode:new(o)
|
||||
o = o or {}
|
||||
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
function BaseNode:destroy()
|
||||
if self.watcher then
|
||||
self.watcher:destroy()
|
||||
self.watcher = nil
|
||||
end
|
||||
end
|
||||
|
||||
---From plenary
|
||||
---Checks if the object is an instance
|
||||
---This will start with the lowest class and loop over all the superclasses.
|
||||
---@param self BaseNode
|
||||
---@param T BaseNode
|
||||
---@return boolean
|
||||
function BaseNode:is(T)
|
||||
local mt = getmetatable(self)
|
||||
while mt do
|
||||
if mt == T then
|
||||
return true
|
||||
end
|
||||
mt = getmetatable(mt)
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function BaseNode:has_one_child_folder()
|
||||
return #self.nodes == 1 and self.nodes[1].nodes and vim.loop.fs_access(self.nodes[1].absolute_path, "R") or false
|
||||
end
|
||||
|
||||
---@param parent_ignored boolean
|
||||
---@param status table|nil
|
||||
function BaseNode:update_git_status(parent_ignored, status)
|
||||
local get_status
|
||||
if self.nodes then
|
||||
get_status = git.git_status_dir
|
||||
else
|
||||
get_status = git.git_status_file
|
||||
end
|
||||
|
||||
-- status of the node's absolute path
|
||||
self.git_status = get_status(parent_ignored, status, self.absolute_path)
|
||||
|
||||
-- status of the link target, if the link itself is not dirty
|
||||
if self.link_to and not self.git_status then
|
||||
self.git_status = get_status(parent_ignored, status, self.link_to)
|
||||
end
|
||||
end
|
||||
|
||||
---@return GitStatus|nil
|
||||
function BaseNode:get_git_status()
|
||||
if not self.git_status then
|
||||
-- status doesn't exist
|
||||
return nil
|
||||
end
|
||||
|
||||
if not self.nodes then
|
||||
-- file
|
||||
return self.git_status.file and { self.git_status.file }
|
||||
end
|
||||
|
||||
-- dir
|
||||
if not self.explorer.opts.git.show_on_dirs then
|
||||
return nil
|
||||
end
|
||||
|
||||
local status = {}
|
||||
if not self:last_group_node().open or self.explorer.opts.git.show_on_open_dirs then
|
||||
-- dir is closed or we should show on open_dirs
|
||||
if self.git_status.file ~= nil then
|
||||
table.insert(status, self.git_status.file)
|
||||
end
|
||||
if self.git_status.dir ~= nil then
|
||||
if self.git_status.dir.direct ~= nil then
|
||||
for _, s in pairs(self.git_status.dir.direct) do
|
||||
table.insert(status, s)
|
||||
end
|
||||
end
|
||||
if self.git_status.dir.indirect ~= nil then
|
||||
for _, s in pairs(self.git_status.dir.indirect) do
|
||||
table.insert(status, s)
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
-- dir is open and we shouldn't show on open_dirs
|
||||
if self.git_status.file ~= nil then
|
||||
table.insert(status, self.git_status.file)
|
||||
end
|
||||
if self.git_status.dir ~= nil and self.git_status.dir.direct ~= nil then
|
||||
local deleted = {
|
||||
[" D"] = true,
|
||||
["D "] = true,
|
||||
["RD"] = true,
|
||||
["DD"] = true,
|
||||
}
|
||||
for _, s in pairs(self.git_status.dir.direct) do
|
||||
if deleted[s] then
|
||||
table.insert(status, s)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if #status == 0 then
|
||||
return nil
|
||||
else
|
||||
return status
|
||||
end
|
||||
end
|
||||
|
||||
---@param projects table
|
||||
function BaseNode:reload_node_status(projects)
|
||||
local toplevel = git.get_toplevel(self.absolute_path)
|
||||
local status = projects[toplevel] or {}
|
||||
for _, node in ipairs(self.nodes) do
|
||||
node:update_git_status(self:is_git_ignored(), status)
|
||||
if node.nodes and #node.nodes > 0 then
|
||||
self:reload_node_status(projects)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function BaseNode:is_git_ignored()
|
||||
return self.git_status ~= nil and self.git_status.file == "!!"
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function BaseNode:is_dotfile()
|
||||
if
|
||||
self.is_dot --
|
||||
or (self.name and (self.name:sub(1, 1) == ".")) --
|
||||
or (self.parent and self.parent:is_dotfile())
|
||||
then
|
||||
self.is_dot = true
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
|
||||
---@return Node
|
||||
function BaseNode:last_group_node()
|
||||
local node = self --[[@as BaseNode]]
|
||||
|
||||
while node.group_next do
|
||||
node = node.group_next
|
||||
end
|
||||
|
||||
return node
|
||||
end
|
||||
|
||||
---@param project table|nil
|
||||
---@param root string|nil
|
||||
function BaseNode:update_parent_statuses(project, root)
|
||||
local node = self
|
||||
while project and node do
|
||||
-- step up to the containing project
|
||||
if node.absolute_path == root then
|
||||
-- stop at the top of the tree
|
||||
if not node.parent then
|
||||
break
|
||||
end
|
||||
|
||||
root = git.get_toplevel(node.parent.absolute_path)
|
||||
|
||||
-- stop when no more projects
|
||||
if not root then
|
||||
break
|
||||
end
|
||||
|
||||
-- update the containing project
|
||||
project = git.get_project(root)
|
||||
git.reload_project(root, node.absolute_path, nil)
|
||||
end
|
||||
|
||||
-- update status
|
||||
node:update_git_status(node.parent and node.parent:is_git_ignored() or false, project)
|
||||
|
||||
-- maybe parent
|
||||
node = node.parent
|
||||
end
|
||||
end
|
||||
|
||||
---Refresh contents and git status for a single node
|
||||
function BaseNode:refresh()
|
||||
local parent_node = self:get_parent_of_group()
|
||||
local toplevel = git.get_toplevel(self.absolute_path)
|
||||
|
||||
git.reload_project(toplevel, self.absolute_path, function()
|
||||
local project = git.get_project(toplevel) or {}
|
||||
|
||||
self.explorer:reload(parent_node, project)
|
||||
|
||||
parent_node:update_parent_statuses(project, toplevel)
|
||||
|
||||
self.explorer.renderer:draw()
|
||||
end)
|
||||
end
|
||||
|
||||
---Get the highest parent of grouped nodes
|
||||
---@return Node node or parent
|
||||
function BaseNode:get_parent_of_group()
|
||||
local node = self
|
||||
while node and node.parent and node.parent.group_next do
|
||||
node = node.parent or node
|
||||
end
|
||||
return node
|
||||
end
|
||||
|
||||
---@return Node[]
|
||||
function BaseNode:get_all_nodes_in_group()
|
||||
local next_node = self:get_parent_of_group()
|
||||
local nodes = {}
|
||||
while next_node do
|
||||
table.insert(nodes, next_node)
|
||||
next_node = next_node.group_next
|
||||
end
|
||||
return nodes
|
||||
end
|
||||
|
||||
-- Toggle group empty folders
|
||||
function BaseNode:toggle_group_folders()
|
||||
local is_grouped = self.group_next ~= nil
|
||||
|
||||
if is_grouped then
|
||||
self:ungroup_empty_folders()
|
||||
else
|
||||
self:group_empty_folders()
|
||||
end
|
||||
end
|
||||
|
||||
---Group empty folders
|
||||
-- Recursively group nodes
|
||||
---@return Node[]
|
||||
function BaseNode:group_empty_folders()
|
||||
local is_root = not self.parent
|
||||
local child_folder_only = self:has_one_child_folder() and self.nodes[1]
|
||||
if self.explorer.opts.renderer.group_empty and not is_root and child_folder_only then
|
||||
---@cast self DirectoryNode -- TODO move this to the class
|
||||
self.group_next = child_folder_only
|
||||
local ns = child_folder_only:group_empty_folders()
|
||||
self.nodes = ns or {}
|
||||
return ns
|
||||
end
|
||||
return self.nodes
|
||||
end
|
||||
|
||||
---Ungroup empty folders
|
||||
-- If a node is grouped, ungroup it: put node.group_next to the node.nodes and set node.group_next to nil
|
||||
function BaseNode:ungroup_empty_folders()
|
||||
local cur = self
|
||||
while cur and cur.group_next do
|
||||
cur.nodes = { cur.group_next }
|
||||
cur.group_next = nil
|
||||
cur = cur.nodes[1]
|
||||
end
|
||||
end
|
||||
|
||||
function BaseNode:expand_or_collapse(toggle_group)
|
||||
toggle_group = toggle_group or false
|
||||
if self.has_children then
|
||||
---@cast self DirectoryNode -- TODO move this to the class
|
||||
self.has_children = false
|
||||
end
|
||||
|
||||
if #self.nodes == 0 then
|
||||
self.explorer:expand(self)
|
||||
end
|
||||
|
||||
local head_node = self:get_parent_of_group()
|
||||
if toggle_group then
|
||||
head_node:toggle_group_folders()
|
||||
end
|
||||
|
||||
local open = self:last_group_node().open
|
||||
local next_open
|
||||
if toggle_group then
|
||||
next_open = open
|
||||
else
|
||||
next_open = not open
|
||||
end
|
||||
for _, n in ipairs(head_node:get_all_nodes_in_group()) do
|
||||
n.open = next_open
|
||||
end
|
||||
|
||||
self.explorer.renderer:draw()
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---@return BaseNode cloned
|
||||
function BaseNode:clone()
|
||||
---@type Explorer
|
||||
local explorer_placeholder = nil
|
||||
|
||||
---@type BaseNode
|
||||
local clone = {
|
||||
type = self.type,
|
||||
explorer = explorer_placeholder,
|
||||
absolute_path = self.absolute_path,
|
||||
executable = self.executable,
|
||||
fs_stat = self.fs_stat,
|
||||
git_status = self.git_status,
|
||||
hidden = self.hidden,
|
||||
is_dot = self.is_dot,
|
||||
name = self.name,
|
||||
parent = nil,
|
||||
watcher = nil,
|
||||
diag_status = nil,
|
||||
}
|
||||
|
||||
return clone
|
||||
end
|
||||
|
||||
return BaseNode
|
||||
89
lua/nvim-tree/node/link.lua
Normal file
89
lua/nvim-tree/node/link.lua
Normal file
@@ -0,0 +1,89 @@
|
||||
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
|
||||
20
lua/nvim-tree/node/root.lua
Normal file
20
lua/nvim-tree/node/root.lua
Normal file
@@ -0,0 +1,20 @@
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
---@class (exact) RootNode: DirectoryNode
|
||||
local RootNode = DirectoryNode:new()
|
||||
|
||||
---Static factory method
|
||||
---@param explorer Explorer
|
||||
---@param absolute_path string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result|nil
|
||||
---@return RootNode
|
||||
function RootNode:create(explorer, absolute_path, name, fs_stat)
|
||||
local o = DirectoryNode:create(explorer, nil, absolute_path, name, fs_stat)
|
||||
|
||||
o = self:new(o) --[[@as RootNode]]
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
return RootNode
|
||||
Reference in New Issue
Block a user