* 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
This commit is contained in:
committed by
GitHub
parent
fb2070db94
commit
8331a24c77
@@ -9,7 +9,7 @@ local DirectoryLinkNode = DirectoryNode:new()
|
||||
|
||||
---Static factory method
|
||||
---@param explorer Explorer
|
||||
---@param parent Node
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param link_to string
|
||||
---@param name string
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
local git = require("nvim-tree.git")
|
||||
local watch = require("nvim-tree.explorer.watch")
|
||||
|
||||
local BaseNode = require("nvim-tree.node")
|
||||
local Node = require("nvim-tree.node")
|
||||
|
||||
---@class (exact) DirectoryNode: BaseNode
|
||||
---@class (exact) DirectoryNode: Node
|
||||
---@field has_children boolean
|
||||
---@field group_next Node? -- If node is grouped, this points to the next child dir/link node
|
||||
---@field group_next DirectoryNode? -- If node is grouped, this points to the next child dir/link node
|
||||
---@field nodes Node[]
|
||||
---@field open boolean
|
||||
---@field watcher Watcher?
|
||||
---@field hidden_stats table? -- Each field of this table is a key for source and value for count
|
||||
local DirectoryNode = BaseNode:new()
|
||||
local DirectoryNode = Node:new()
|
||||
|
||||
---Static factory method
|
||||
---@param explorer Explorer
|
||||
---@param parent Node?
|
||||
---@param parent DirectoryNode?
|
||||
---@param absolute_path string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result|nil
|
||||
@@ -51,12 +52,18 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat)
|
||||
end
|
||||
|
||||
function DirectoryNode:destroy()
|
||||
BaseNode.destroy(self)
|
||||
if self.watcher then
|
||||
self.watcher:destroy()
|
||||
self.watcher = nil
|
||||
end
|
||||
|
||||
if self.nodes then
|
||||
for _, node in pairs(self.nodes) do
|
||||
node:destroy()
|
||||
end
|
||||
end
|
||||
|
||||
Node.destroy(self)
|
||||
end
|
||||
|
||||
---Update the GitStatus of the directory
|
||||
@@ -116,6 +123,75 @@ function DirectoryNode:get_git_status()
|
||||
end
|
||||
end
|
||||
|
||||
---Refresh contents and git status for a single node
|
||||
function DirectoryNode:refresh()
|
||||
local node = self:get_parent_of_group() or self
|
||||
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(node, project)
|
||||
|
||||
node:update_parent_statuses(project, toplevel)
|
||||
|
||||
self.explorer.renderer:draw()
|
||||
end)
|
||||
end
|
||||
|
||||
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
|
||||
---@return DirectoryNode
|
||||
function DirectoryNode:last_group_node()
|
||||
return self.group_next and self.group_next:last_group_node() or self
|
||||
end
|
||||
|
||||
---Return the one and only one child directory
|
||||
---@return DirectoryNode?
|
||||
function DirectoryNode:single_child_directory()
|
||||
if #self.nodes == 1 then
|
||||
return self.nodes[1]:as(DirectoryNode)
|
||||
end
|
||||
end
|
||||
|
||||
---@private
|
||||
-- Toggle group empty folders
|
||||
function DirectoryNode: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
|
||||
---@private
|
||||
---@return Node[]
|
||||
function DirectoryNode:group_empty_folders()
|
||||
local single_child = self:single_child_directory()
|
||||
if self.explorer.opts.renderer.group_empty and self.parent and single_child then
|
||||
self.group_next = single_child
|
||||
local ns = single_child: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
|
||||
---@private
|
||||
function DirectoryNode:ungroup_empty_folders()
|
||||
if self.group_next then
|
||||
self.group_next:ungroup_empty_folders()
|
||||
self.nodes = { self.group_next }
|
||||
self.group_next = nil
|
||||
end
|
||||
end
|
||||
|
||||
---@param toggle_group boolean
|
||||
function DirectoryNode:expand_or_collapse(toggle_group)
|
||||
toggle_group = toggle_group or false
|
||||
if self.has_children then
|
||||
@@ -126,7 +202,7 @@ function DirectoryNode:expand_or_collapse(toggle_group)
|
||||
self.explorer:expand(self)
|
||||
end
|
||||
|
||||
local head_node = self:get_parent_of_group()
|
||||
local head_node = self:get_parent_of_group() or self
|
||||
if toggle_group then
|
||||
head_node:toggle_group_folders()
|
||||
end
|
||||
@@ -138,8 +214,11 @@ function DirectoryNode:expand_or_collapse(toggle_group)
|
||||
else
|
||||
next_open = not open
|
||||
end
|
||||
for _, n in ipairs(head_node:get_all_nodes_in_group()) do
|
||||
n.open = next_open
|
||||
|
||||
local node = self
|
||||
while node do
|
||||
node.open = next_open
|
||||
node = node.group_next
|
||||
end
|
||||
|
||||
self.explorer.renderer:draw()
|
||||
@@ -148,7 +227,7 @@ 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]]
|
||||
local clone = Node.clone(self) --[[@as DirectoryNode]]
|
||||
|
||||
clone.has_children = self.has_children
|
||||
clone.group_next = nil
|
||||
|
||||
@@ -8,7 +8,7 @@ local M = {}
|
||||
|
||||
---Factory function to create the appropriate Node
|
||||
---@param explorer Explorer
|
||||
---@param parent Node
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param stat uv.fs_stat.result? -- on nil stat return nil Node
|
||||
---@param name string
|
||||
|
||||
@@ -9,7 +9,7 @@ local FileLinkNode = FileNode:new()
|
||||
|
||||
---Static factory method
|
||||
---@param explorer Explorer
|
||||
---@param parent Node
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param link_to string
|
||||
---@param name string
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
local git = require("nvim-tree.git")
|
||||
local utils = require("nvim-tree.utils")
|
||||
|
||||
local BaseNode = require("nvim-tree.node")
|
||||
local Node = require("nvim-tree.node")
|
||||
|
||||
---@class (exact) FileNode: BaseNode
|
||||
---@class (exact) FileNode: Node
|
||||
---@field extension string
|
||||
local FileNode = BaseNode:new()
|
||||
local FileNode = Node:new()
|
||||
|
||||
---Static factory method
|
||||
---@param explorer Explorer
|
||||
---@param parent Node
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result?
|
||||
@@ -27,7 +27,6 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
|
||||
is_dot = false,
|
||||
name = name,
|
||||
parent = parent,
|
||||
watcher = nil,
|
||||
diag_status = nil,
|
||||
|
||||
extension = string.match(name, ".?[^.]+%.(.*)") or "",
|
||||
@@ -56,7 +55,7 @@ end
|
||||
---Create a sanitized partial copy of a node
|
||||
---@return FileNode cloned
|
||||
function FileNode:clone()
|
||||
local clone = BaseNode.clone(self) --[[@as FileNode]]
|
||||
local clone = Node.clone(self) --[[@as FileNode]]
|
||||
|
||||
clone.extension = self.extension
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
local git = require("nvim-tree.git")
|
||||
|
||||
local Class = require("nvim-tree.class")
|
||||
|
||||
---TODO #2886
|
||||
---TODO remove all @cast
|
||||
---TODO remove all references to directory fields:
|
||||
|
||||
---Abstract Node class.
|
||||
---Uses the abstract factory pattern to instantiate child instances.
|
||||
---@class (exact) BaseNode
|
||||
---@field private __index? table
|
||||
---@class (exact) Node: Class
|
||||
---@field type NODE_TYPE
|
||||
---@field explorer Explorer
|
||||
---@field absolute_path string
|
||||
@@ -15,68 +17,30 @@ local git = require("nvim-tree.git")
|
||||
---@field git_status GitStatus?
|
||||
---@field hidden boolean
|
||||
---@field name string
|
||||
---@field parent Node?
|
||||
---@field watcher Watcher?
|
||||
---@field parent DirectoryNode?
|
||||
---@field diag_status DiagStatus?
|
||||
---@field is_dot boolean cached is_dotfile
|
||||
local BaseNode = {}
|
||||
local Node = Class:new()
|
||||
|
||||
---@alias Node RootNode|BaseNode|DirectoryNode|FileNode|DirectoryLinkNode|FileLinkNode
|
||||
|
||||
---@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
|
||||
function Node:destroy()
|
||||
end
|
||||
|
||||
--luacheck: push ignore 212
|
||||
---Update the GitStatus of the node
|
||||
---@param parent_ignored boolean
|
||||
---@param status table?
|
||||
function BaseNode:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local
|
||||
function Node:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local
|
||||
---TODO find a way to declare abstract methods
|
||||
end
|
||||
|
||||
--luacheck: pop
|
||||
|
||||
---@return GitStatus?
|
||||
function BaseNode:get_git_status()
|
||||
function Node:get_git_status()
|
||||
end
|
||||
|
||||
---@param projects table
|
||||
function BaseNode:reload_node_status(projects)
|
||||
function Node:reload_node_status(projects)
|
||||
local toplevel = git.get_toplevel(self.absolute_path)
|
||||
local status = projects[toplevel] or {}
|
||||
for _, node in ipairs(self.nodes) do
|
||||
@@ -88,13 +52,13 @@ function BaseNode:reload_node_status(projects)
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function BaseNode:is_git_ignored()
|
||||
function Node:is_git_ignored()
|
||||
return self.git_status ~= nil and self.git_status.file == "!!"
|
||||
end
|
||||
|
||||
---Node or one of its parents begins with a dot
|
||||
---@return boolean
|
||||
function BaseNode:is_dotfile()
|
||||
function Node:is_dotfile()
|
||||
if
|
||||
self.is_dot
|
||||
or (self.name and (self.name:sub(1, 1) == "."))
|
||||
@@ -106,22 +70,9 @@ function BaseNode:is_dotfile()
|
||||
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
|
||||
--- @cast node BaseNode
|
||||
|
||||
while node.group_next do
|
||||
node = node.group_next
|
||||
end
|
||||
|
||||
return node
|
||||
end
|
||||
|
||||
---@param project table?
|
||||
---@param root string?
|
||||
function BaseNode:update_parent_statuses(project, root)
|
||||
function Node:update_parent_statuses(project, root)
|
||||
local node = self
|
||||
while project and node do
|
||||
-- step up to the containing project
|
||||
@@ -151,88 +102,30 @@ function BaseNode:update_parent_statuses(project, root)
|
||||
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
|
||||
---Get the highest parent of grouped nodes, nil when not grouped
|
||||
---@return DirectoryNode?
|
||||
function Node:get_parent_of_group()
|
||||
if not self.parent or not self.parent.group_next then
|
||||
return nil
|
||||
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 #2886 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]
|
||||
local node = self.parent
|
||||
while node do
|
||||
if node.parent and node.parent.group_next then
|
||||
node = node.parent
|
||||
else
|
||||
return node
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---@return BaseNode cloned
|
||||
function BaseNode:clone()
|
||||
---@return Node cloned
|
||||
function Node:clone()
|
||||
---@type Explorer
|
||||
local explorer_placeholder = nil
|
||||
|
||||
---@type BaseNode
|
||||
---@type Node
|
||||
local clone = {
|
||||
type = self.type,
|
||||
explorer = explorer_placeholder,
|
||||
@@ -244,11 +137,10 @@ function BaseNode:clone()
|
||||
is_dot = self.is_dot,
|
||||
name = self.name,
|
||||
parent = nil,
|
||||
watcher = nil,
|
||||
diag_status = nil,
|
||||
}
|
||||
|
||||
return clone
|
||||
end
|
||||
|
||||
return BaseNode
|
||||
return Node
|
||||
|
||||
Reference in New Issue
Block a user