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
This commit is contained in:
Alexander Courtis
2024-10-20 17:23:22 +11:00
committed by GitHub
parent fb2070db94
commit 8331a24c77
22 changed files with 284 additions and 258 deletions

View File

@@ -125,7 +125,7 @@ function M.place_cursor_on_node()
if not node or node.name == ".." then if not node or node.name == ".." then
return return
end end
node = node:get_parent_of_group() node = node:get_parent_of_group() or node
local line = vim.api.nvim_get_current_line() local line = vim.api.nvim_get_current_line()
local cursor = vim.api.nvim_win_get_cursor(0) local cursor = vim.api.nvim_win_get_cursor(0)

View File

@@ -43,6 +43,7 @@ function M.fn(path)
return node.absolute_path == path_real or node.link_to == path_real return node.absolute_path == path_real or node.link_to == path_real
end) end)
:applier(function(node) :applier(function(node)
---@cast node DirectoryNode
local incremented_line = false local incremented_line = false
if not node.group_next then if not node.group_next then
line = line + 1 line = line + 1

View File

@@ -7,6 +7,8 @@ local notify = require("nvim-tree.notify")
local find_file = require("nvim-tree.actions.finders.find-file").fn local find_file = require("nvim-tree.actions.finders.find-file").fn
local DirectoryNode = require("nvim-tree.node.directory")
---@enum ACTION ---@enum ACTION
local ACTION = { local ACTION = {
copy = "copy", copy = "copy",
@@ -219,7 +221,7 @@ end
function Clipboard:do_paste(node, action, action_fn) function Clipboard:do_paste(node, action, action_fn)
if node.name == ".." then if node.name == ".." then
node = self.explorer node = self.explorer
else elseif node:is(DirectoryNode) then
node = node:last_group_node() node = node:last_group_node()
end end
local clip = self.data[action] local clip = self.data[action]

View File

@@ -5,6 +5,9 @@ local notify = require("nvim-tree.notify")
local find_file = require("nvim-tree.actions.finders.find-file").fn local find_file = require("nvim-tree.actions.finders.find-file").fn
local FileNode = require("nvim-tree.node.file")
local DirectoryNode = require("nvim-tree.node.directory")
local M = {} local M = {}
---@param file string ---@param file string
@@ -29,35 +32,21 @@ local function get_num_nodes(iter)
return i return i
end end
---@param node Node
---@return string
local function get_containing_folder(node)
if node.nodes ~= nil then
return utils.path_add_trailing(node.absolute_path)
end
local node_name_size = #(node.name or "")
return node.absolute_path:sub(0, -node_name_size - 1)
end
---@param node Node? ---@param node Node?
function M.fn(node) function M.fn(node)
local cwd = core.get_cwd() node = node or core.get_explorer() --[[@as Node]]
if cwd == nil then if not node then
return return
end end
if not node or node.name == ".." then local dir = node:is(FileNode) and node.parent or node:as(DirectoryNode)
node = { if not dir then
absolute_path = cwd, return
name = "",
nodes = core.get_explorer().nodes,
open = true,
}
else
node = node:last_group_node()
end end
local containing_folder = get_containing_folder(node) dir = dir:last_group_node()
local containing_folder = utils.path_add_trailing(dir.absolute_path)
local input_opts = { local input_opts = {
prompt = "Create file ", prompt = "Create file ",

View File

@@ -6,6 +6,8 @@ local notify = require("nvim-tree.notify")
local find_file = require("nvim-tree.actions.finders.find-file").fn local find_file = require("nvim-tree.actions.finders.find-file").fn
local DirectoryNode = require("nvim-tree.node.directory")
local M = { local M = {
config = {}, config = {},
} }
@@ -120,7 +122,9 @@ function M.fn(default_modifier)
return return
end end
node = node:last_group_node() if node:is(DirectoryNode) then
node = node:last_group_node()
end
if node.name == ".." then if node.name == ".." then
return return
end end

View File

@@ -78,7 +78,7 @@ local function expand_node(node)
---@cast node DirectoryNode ---@cast node DirectoryNode
-- Expand the node. -- Expand the node.
-- Should never collapse since we checked open. -- Should never collapse since we checked open.
node:expand_or_collapse() node:expand_or_collapse(false)
end end
end end
@@ -102,7 +102,7 @@ local function move_next_recursive(what, skip_gitignored)
end end
if node_init:is(DirectoryNode) and valid and not node_init.open then if node_init:is(DirectoryNode) and valid and not node_init.open then
---@cast node_init DirectoryNode ---@cast node_init DirectoryNode
node_init:expand_or_collapse() node_init:expand_or_collapse(false)
end end
move("next", what, skip_gitignored) move("next", what, skip_gitignored)

View File

@@ -1,6 +1,7 @@
local view = require("nvim-tree.view") local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local core = require("nvim-tree.core")
local DirectoryNode = require("nvim-tree.node.directory")
local M = {} local M = {}
@@ -9,33 +10,32 @@ local M = {}
function M.fn(should_close) function M.fn(should_close)
should_close = should_close or false should_close = should_close or false
---@param node Node
return function(node) return function(node)
local explorer = core.get_explorer() local dir = node:as(DirectoryNode)
node = node:last_group_node() if dir then
if should_close and node.open then dir = dir:last_group_node()
node.open = false if should_close and dir.open then
if explorer then dir.open = false
explorer.renderer:draw() dir.explorer.renderer:draw()
return
end end
return
end end
local parent = node:get_parent_of_group().parent local parent = (node:get_parent_of_group() or node).parent
if not parent or not parent.parent then if not parent or not parent.parent then
return view.set_cursor({ 1, 0 }) return view.set_cursor({ 1, 0 })
end end
local _, line = utils.find_node(core.get_explorer().nodes, function(n) local _, line = utils.find_node(parent.explorer.nodes, function(n)
return n.absolute_path == parent.absolute_path return n.absolute_path == parent.absolute_path
end) end)
view.set_cursor({ line + 1, 0 }) view.set_cursor({ line + 1, 0 })
if should_close then if should_close then
parent.open = false parent.open = false
if explorer then parent.explorer.renderer:draw()
explorer.renderer:draw()
end
end end
end end
end end

View File

@@ -3,6 +3,8 @@ local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib") local lib = require("nvim-tree.lib")
local Iterator = require("nvim-tree.iterators.node-iterator") local Iterator = require("nvim-tree.iterators.node-iterator")
local DirectoryNode = require("nvim-tree.node.directory")
local M = {} local M = {}
---@return fun(path: string): boolean ---@return fun(path: string): boolean
@@ -36,8 +38,9 @@ function M.fn(keep_buffers)
Iterator.builder(explorer.nodes) Iterator.builder(explorer.nodes)
:hidden() :hidden()
:applier(function(n) :applier(function(n)
if n.nodes ~= nil then local dir = n:as(DirectoryNode)
n.open = keep_buffers == true and matches(n.absolute_path) if dir then
dir.open = keep_buffers and matches(dir.absolute_path)
end end
end) end)
:recursor(function(n) :recursor(function(n)

View File

@@ -2,6 +2,8 @@ local core = require("nvim-tree.core")
local Iterator = require("nvim-tree.iterators.node-iterator") local Iterator = require("nvim-tree.iterators.node-iterator")
local notify = require("nvim-tree.notify") local notify = require("nvim-tree.notify")
local DirectoryNode = require("nvim-tree.node.directory")
local M = {} local M = {}
---@param list string[] ---@param list string[]
@@ -15,7 +17,7 @@ local function to_lookup_table(list)
return table return table
end end
---@param node Node ---@param node DirectoryNode
local function expand(node) local function expand(node)
node = node:last_group_node() node = node:last_group_node()
node.open = true node.open = true
@@ -36,6 +38,7 @@ end
local function gen_iterator() local function gen_iterator()
local expansion_count = 0 local expansion_count = 0
---@param parent DirectoryNode
return function(parent) return function(parent)
if parent.parent and parent.nodes and not parent.open then if parent.parent and parent.nodes and not parent.open then
expansion_count = expansion_count + 1 expansion_count = expansion_count + 1
@@ -44,12 +47,14 @@ local function gen_iterator()
Iterator.builder(parent.nodes) Iterator.builder(parent.nodes)
:hidden() :hidden()
---@param node DirectoryNode
:applier(function(node) :applier(function(node)
if should_expand(expansion_count, node) then if should_expand(expansion_count, node) then
expansion_count = expansion_count + 1 expansion_count = expansion_count + 1
expand(node) expand(node)
end end
end) end)
---@param node DirectoryNode
:recursor(function(node) :recursor(function(node)
return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes)) return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes))
end) end)
@@ -61,11 +66,16 @@ local function gen_iterator()
end end
end end
---Expand the directory node or the root
---@param node Node ---@param node Node
function M.fn(node) function M.fn(node)
local explorer = core.get_explorer() local explorer = core.get_explorer()
node = node.nodes and node or explorer local parent = node:as(DirectoryNode) or explorer
if gen_iterator()(node) then if not parent then
return
end
if gen_iterator()(parent) then
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
end end
if explorer then if explorer then

View File

@@ -10,6 +10,7 @@ local keymap = require("nvim-tree.keymap")
local notify = require("nvim-tree.notify") local notify = require("nvim-tree.notify")
local DirectoryNode = require("nvim-tree.node.directory") local DirectoryNode = require("nvim-tree.node.directory")
local RootNode = require("nvim-tree.node.root")
local Api = { local Api = {
tree = {}, tree = {},
@@ -137,9 +138,9 @@ Api.tree.change_root = wrap(function(...)
end) end)
Api.tree.change_root_to_node = wrap_node(function(node) Api.tree.change_root_to_node = wrap_node(function(node)
if node.name == ".." then if node.name == ".." or node:is(RootNode) then
actions.root.change_dir.fn("..") actions.root.change_dir.fn("..")
elseif node.nodes ~= nil then elseif node:is(DirectoryNode) then
actions.root.change_dir.fn(node:last_group_node().absolute_path) actions.root.change_dir.fn(node:last_group_node().absolute_path)
end end
end) end)
@@ -210,13 +211,13 @@ local function edit(mode, node)
end end
---@param mode string ---@param mode string
---@return fun(node: table) ---@return fun(node: Node)
local function open_or_expand_or_dir_up(mode, toggle_group) local function open_or_expand_or_dir_up(mode, toggle_group)
---@param node Node
return function(node) return function(node)
if node.name == ".." then if node.name == ".." then
actions.root.change_dir.fn("..") actions.root.change_dir.fn("..")
elseif node:is(DirectoryNode) then elseif node:is(DirectoryNode) then
---@cast node DirectoryNode
node:expand_or_collapse(toggle_group) node:expand_or_collapse(toggle_group)
elseif not toggle_group then elseif not toggle_group then
edit(mode, node) edit(mode, node)

40
lua/nvim-tree/class.lua Normal file
View File

@@ -0,0 +1,40 @@
---Generic class, useful for inheritence.
---@class (exact) Class
---@field private __index? table
local Class = {}
---@param o Class?
---@return Class
function Class:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
---Object is an instance of class
---This will start with the lowest class and loop over all the superclasses.
---@param class table
---@return boolean
function Class:is(class)
local mt = getmetatable(self)
while mt do
if mt == class then
return true
end
mt = getmetatable(mt)
end
return false
end
---Return object if it is an instance of class, otherwise nil
---@generic T
---@param class T
---@return `T`|nil
function Class:as(class)
return self:is(class) and self or nil
end
return Class

View File

@@ -5,6 +5,7 @@ local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view") local view = require("nvim-tree.view")
local node_factory = require("nvim-tree.node.factory") local node_factory = require("nvim-tree.node.factory")
local DirectoryNode = require("nvim-tree.node.directory")
local RootNode = require("nvim-tree.node.root") local RootNode = require("nvim-tree.node.root")
local Watcher = require("nvim-tree.watcher") local Watcher = require("nvim-tree.watcher")
@@ -72,12 +73,12 @@ function Explorer:create(path)
return o return o
end end
---@param node Node ---@param node DirectoryNode
function Explorer:expand(node) function Explorer:expand(node)
self:_load(node) self:_load(node)
end end
---@param node Node ---@param node DirectoryNode
---@param git_status table|nil ---@param git_status table|nil
function Explorer:reload(node, git_status) function Explorer:reload(node, git_status)
local cwd = node.link_to or node.absolute_path local cwd = node.link_to or node.absolute_path
@@ -169,11 +170,10 @@ function Explorer:reload(node, git_status)
end, node.nodes) end, node.nodes)
) )
local is_root = not node.parent local single_child = node:single_child_directory()
local child_folder_only = node:has_one_child_folder() and node.nodes[1] if config.renderer.group_empty and node.parent and single_child then
if config.renderer.group_empty and not is_root and child_folder_only then node.group_next = single_child
node.group_next = child_folder_only local ns = self:reload(single_child, git_status)
local ns = self:reload(child_folder_only, git_status)
node.nodes = ns or {} node.nodes = ns or {}
log.profile_end(profile) log.profile_end(profile)
return ns return ns
@@ -219,7 +219,7 @@ function Explorer:refresh_parent_nodes_for_path(path)
end end
---@private ---@private
---@param node Node ---@param node DirectoryNode
function Explorer:_load(node) function Explorer:_load(node)
local cwd = node.link_to or node.absolute_path local cwd = node.link_to or node.absolute_path
local git_status = git.load_project_status(cwd) local git_status = git.load_project_status(cwd)
@@ -243,7 +243,7 @@ end
---@private ---@private
---@param handle uv.uv_fs_t ---@param handle uv.uv_fs_t
---@param cwd string ---@param cwd string
---@param node Node ---@param node DirectoryNode
---@param git_status table ---@param git_status table
---@param parent Explorer ---@param parent Explorer
function Explorer:populate_children(handle, cwd, node, git_status, parent) function Explorer:populate_children(handle, cwd, node, git_status, parent)
@@ -295,7 +295,7 @@ function Explorer:populate_children(handle, cwd, node, git_status, parent)
end end
---@private ---@private
---@param node Node ---@param node DirectoryNode
---@param status table ---@param status table
---@param parent Explorer ---@param parent Explorer
---@return Node[]|nil ---@return Node[]|nil
@@ -311,12 +311,12 @@ function Explorer:explore(node, status, parent)
self:populate_children(handle, cwd, node, status, parent) self:populate_children(handle, cwd, node, status, parent)
local is_root = not node.parent local is_root = not node.parent
local child_folder_only = node:has_one_child_folder() and node.nodes[1] local single_child = node:single_child_directory()
if config.renderer.group_empty and not is_root and child_folder_only then if config.renderer.group_empty and not is_root and single_child then
local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path local child_cwd = single_child.link_to or single_child.absolute_path
local child_status = git.load_project_status(child_cwd) local child_status = git.load_project_status(child_cwd)
node.group_next = child_folder_only node.group_next = single_child
local ns = self:explore(child_folder_only, child_status, parent) local ns = self:explore(single_child, child_status, parent)
node.nodes = ns or {} node.nodes = ns or {}
log.profile_end(profile) log.profile_end(profile)
@@ -335,9 +335,10 @@ end
function Explorer:refresh_nodes(projects) function Explorer:refresh_nodes(projects)
Iterator.builder({ self }) Iterator.builder({ self })
:applier(function(n) :applier(function(n)
if n.nodes then local dir = n:as(DirectoryNode)
local toplevel = git.get_toplevel(n.cwd or n.link_to or n.absolute_path) if dir then
self:reload(n, projects[toplevel] or {}) local toplevel = git.get_toplevel(dir.cwd or dir.link_to or dir.absolute_path)
self:reload(dir, projects[toplevel] or {})
end end
end) end)
:recursor(function(n) :recursor(function(n)

View File

@@ -1,6 +1,8 @@
local view = require("nvim-tree.view") local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local Iterator = require("nvim-tree.iterators.node-iterator") local Iterator = require("nvim-tree.iterators.node-iterator")
local DirectoryNode = require("nvim-tree.node.directory")
---@class LiveFilter ---@class LiveFilter
---@field explorer Explorer ---@field explorer Explorer
@@ -31,17 +33,19 @@ local function reset_filter(self, node_)
return return
end end
node_.hidden_stats = vim.tbl_deep_extend("force", node_.hidden_stats or {}, { local dir_ = node_:as(DirectoryNode)
live_filter = 0, if dir_ then
}) dir_.hidden_stats = vim.tbl_deep_extend("force", dir_.hidden_stats or {}, { live_filter = 0, })
end
Iterator.builder(node_.nodes) Iterator.builder(node_.nodes)
:hidden() :hidden()
:applier(function(node) :applier(function(node)
node.hidden = false node.hidden = false
node.hidden_stats = vim.tbl_deep_extend("force", node.hidden_stats or {}, { local dir = node:as(DirectoryNode)
live_filter = 0, if dir then
}) dir.hidden_stats = vim.tbl_deep_extend("force", dir.hidden_stats or {}, { live_filter = 0, })
end
end) end)
:iterate() :iterate()
end end
@@ -85,7 +89,7 @@ local function matches(self, node)
return vim.regex(self.filter):match_str(name) ~= nil return vim.regex(self.filter):match_str(name) ~= nil
end end
---@param node_ Node? ---@param node_ DirectoryNode?
function LiveFilter:apply_filter(node_) function LiveFilter:apply_filter(node_)
if not self.filter or self.filter == "" then if not self.filter or self.filter == "" then
reset_filter(self, node_) reset_filter(self, node_)

View File

@@ -53,7 +53,7 @@ local function is_folder_ignored(path)
return false return false
end end
---@param node Node ---@param node DirectoryNode
---@return Watcher|nil ---@return Watcher|nil
function M.create_watcher(node) function M.create_watcher(node)
if not M.config.filesystem_watchers.enable or type(node) ~= "table" then if not M.config.filesystem_watchers.enable or type(node) ~= "table" then

View File

@@ -9,7 +9,7 @@ local DirectoryLinkNode = DirectoryNode:new()
---Static factory method ---Static factory method
---@param explorer Explorer ---@param explorer Explorer
---@param parent Node ---@param parent DirectoryNode
---@param absolute_path string ---@param absolute_path string
---@param link_to string ---@param link_to string
---@param name string ---@param name string

View File

@@ -1,19 +1,20 @@
local git = require("nvim-tree.git") local git = require("nvim-tree.git")
local watch = require("nvim-tree.explorer.watch") 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 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 nodes Node[]
---@field open boolean ---@field open boolean
---@field watcher Watcher?
---@field hidden_stats table? -- Each field of this table is a key for source and value for count ---@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 ---Static factory method
---@param explorer Explorer ---@param explorer Explorer
---@param parent Node? ---@param parent DirectoryNode?
---@param absolute_path string ---@param absolute_path string
---@param name string ---@param name string
---@param fs_stat uv.fs_stat.result|nil ---@param fs_stat uv.fs_stat.result|nil
@@ -51,12 +52,18 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat)
end end
function DirectoryNode:destroy() function DirectoryNode:destroy()
BaseNode.destroy(self) if self.watcher then
self.watcher:destroy()
self.watcher = nil
end
if self.nodes then if self.nodes then
for _, node in pairs(self.nodes) do for _, node in pairs(self.nodes) do
node:destroy() node:destroy()
end end
end end
Node.destroy(self)
end end
---Update the GitStatus of the directory ---Update the GitStatus of the directory
@@ -116,6 +123,75 @@ function DirectoryNode:get_git_status()
end end
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) function DirectoryNode:expand_or_collapse(toggle_group)
toggle_group = toggle_group or false toggle_group = toggle_group or false
if self.has_children then if self.has_children then
@@ -126,7 +202,7 @@ function DirectoryNode:expand_or_collapse(toggle_group)
self.explorer:expand(self) self.explorer:expand(self)
end end
local head_node = self:get_parent_of_group() local head_node = self:get_parent_of_group() or self
if toggle_group then if toggle_group then
head_node:toggle_group_folders() head_node:toggle_group_folders()
end end
@@ -138,8 +214,11 @@ function DirectoryNode:expand_or_collapse(toggle_group)
else else
next_open = not open next_open = not open
end 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 end
self.explorer.renderer:draw() self.explorer.renderer:draw()
@@ -148,7 +227,7 @@ end
---Create a sanitized partial copy of a node, populating children recursively. ---Create a sanitized partial copy of a node, populating children recursively.
---@return DirectoryNode cloned ---@return DirectoryNode cloned
function DirectoryNode:clone() function DirectoryNode:clone()
local clone = BaseNode.clone(self) --[[@as DirectoryNode]] local clone = Node.clone(self) --[[@as DirectoryNode]]
clone.has_children = self.has_children clone.has_children = self.has_children
clone.group_next = nil clone.group_next = nil

View File

@@ -8,7 +8,7 @@ local M = {}
---Factory function to create the appropriate Node ---Factory function to create the appropriate Node
---@param explorer Explorer ---@param explorer Explorer
---@param parent Node ---@param parent DirectoryNode
---@param absolute_path string ---@param absolute_path string
---@param stat uv.fs_stat.result? -- on nil stat return nil Node ---@param stat uv.fs_stat.result? -- on nil stat return nil Node
---@param name string ---@param name string

View File

@@ -9,7 +9,7 @@ local FileLinkNode = FileNode:new()
---Static factory method ---Static factory method
---@param explorer Explorer ---@param explorer Explorer
---@param parent Node ---@param parent DirectoryNode
---@param absolute_path string ---@param absolute_path string
---@param link_to string ---@param link_to string
---@param name string ---@param name string

View File

@@ -1,15 +1,15 @@
local git = require("nvim-tree.git") local git = require("nvim-tree.git")
local utils = require("nvim-tree.utils") 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 ---@field extension string
local FileNode = BaseNode:new() local FileNode = Node:new()
---Static factory method ---Static factory method
---@param explorer Explorer ---@param explorer Explorer
---@param parent Node ---@param parent DirectoryNode
---@param absolute_path string ---@param absolute_path string
---@param name string ---@param name string
---@param fs_stat uv.fs_stat.result? ---@param fs_stat uv.fs_stat.result?
@@ -27,7 +27,6 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
is_dot = false, is_dot = false,
name = name, name = name,
parent = parent, parent = parent,
watcher = nil,
diag_status = nil, diag_status = nil,
extension = string.match(name, ".?[^.]+%.(.*)") or "", extension = string.match(name, ".?[^.]+%.(.*)") or "",
@@ -56,7 +55,7 @@ end
---Create a sanitized partial copy of a node ---Create a sanitized partial copy of a node
---@return FileNode cloned ---@return FileNode cloned
function FileNode:clone() function FileNode:clone()
local clone = BaseNode.clone(self) --[[@as FileNode]] local clone = Node.clone(self) --[[@as FileNode]]
clone.extension = self.extension clone.extension = self.extension

View File

@@ -1,12 +1,14 @@
local git = require("nvim-tree.git") local git = require("nvim-tree.git")
local Class = require("nvim-tree.class")
---TODO #2886
---TODO remove all @cast ---TODO remove all @cast
---TODO remove all references to directory fields: ---TODO remove all references to directory fields:
---Abstract Node class. ---Abstract Node class.
---Uses the abstract factory pattern to instantiate child instances. ---Uses the abstract factory pattern to instantiate child instances.
---@class (exact) BaseNode ---@class (exact) Node: Class
---@field private __index? table
---@field type NODE_TYPE ---@field type NODE_TYPE
---@field explorer Explorer ---@field explorer Explorer
---@field absolute_path string ---@field absolute_path string
@@ -15,68 +17,30 @@ local git = require("nvim-tree.git")
---@field git_status GitStatus? ---@field git_status GitStatus?
---@field hidden boolean ---@field hidden boolean
---@field name string ---@field name string
---@field parent Node? ---@field parent DirectoryNode?
---@field watcher Watcher?
---@field diag_status DiagStatus? ---@field diag_status DiagStatus?
---@field is_dot boolean cached is_dotfile ---@field is_dot boolean cached is_dotfile
local BaseNode = {} local Node = Class:new()
---@alias Node RootNode|BaseNode|DirectoryNode|FileNode|DirectoryLinkNode|FileLinkNode function Node:destroy()
---@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 end
--luacheck: push ignore 212 --luacheck: push ignore 212
---Update the GitStatus of the node ---Update the GitStatus of the node
---@param parent_ignored boolean ---@param parent_ignored boolean
---@param status table? ---@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 end
--luacheck: pop --luacheck: pop
---@return GitStatus? ---@return GitStatus?
function BaseNode:get_git_status() function Node:get_git_status()
end end
---@param projects table ---@param projects table
function BaseNode:reload_node_status(projects) function Node:reload_node_status(projects)
local toplevel = git.get_toplevel(self.absolute_path) local toplevel = git.get_toplevel(self.absolute_path)
local status = projects[toplevel] or {} local status = projects[toplevel] or {}
for _, node in ipairs(self.nodes) do for _, node in ipairs(self.nodes) do
@@ -88,13 +52,13 @@ function BaseNode:reload_node_status(projects)
end end
---@return boolean ---@return boolean
function BaseNode:is_git_ignored() function Node:is_git_ignored()
return self.git_status ~= nil and self.git_status.file == "!!" return self.git_status ~= nil and self.git_status.file == "!!"
end end
---Node or one of its parents begins with a dot ---Node or one of its parents begins with a dot
---@return boolean ---@return boolean
function BaseNode:is_dotfile() function Node:is_dotfile()
if if
self.is_dot self.is_dot
or (self.name and (self.name:sub(1, 1) == ".")) or (self.name and (self.name:sub(1, 1) == "."))
@@ -106,22 +70,9 @@ function BaseNode:is_dotfile()
return false return false
end 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 project table?
---@param root string? ---@param root string?
function BaseNode:update_parent_statuses(project, root) function Node:update_parent_statuses(project, root)
local node = self local node = self
while project and node do while project and node do
-- step up to the containing project -- step up to the containing project
@@ -151,88 +102,30 @@ function BaseNode:update_parent_statuses(project, root)
end end
end end
---Refresh contents and git status for a single node ---Get the highest parent of grouped nodes, nil when not grouped
function BaseNode:refresh() ---@return DirectoryNode?
local parent_node = self:get_parent_of_group() function Node:get_parent_of_group()
local toplevel = git.get_toplevel(self.absolute_path) if not self.parent or not self.parent.group_next then
return nil
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 end
return node
end
---@return Node[] local node = self.parent
function BaseNode:get_all_nodes_in_group() while node do
local next_node = self:get_parent_of_group() if node.parent and node.parent.group_next then
local nodes = {} node = node.parent
while next_node do else
table.insert(nodes, next_node) return node
next_node = next_node.group_next end
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]
end end
end end
---Create a sanitized partial copy of a node, populating children recursively. ---Create a sanitized partial copy of a node, populating children recursively.
---@return BaseNode cloned ---@return Node cloned
function BaseNode:clone() function Node:clone()
---@type Explorer ---@type Explorer
local explorer_placeholder = nil local explorer_placeholder = nil
---@type BaseNode ---@type Node
local clone = { local clone = {
type = self.type, type = self.type,
explorer = explorer_placeholder, explorer = explorer_placeholder,
@@ -244,11 +137,10 @@ function BaseNode:clone()
is_dot = self.is_dot, is_dot = self.is_dot,
name = self.name, name = self.name,
parent = nil, parent = nil,
watcher = nil,
diag_status = nil, diag_status = nil,
} }
return clone return clone
end end
return BaseNode return Node

View File

@@ -2,6 +2,10 @@ local notify = require("nvim-tree.notify")
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view") local view = require("nvim-tree.view")
local DirectoryLinkNode = require("nvim-tree.node.directory-link")
local DirectoryNode = require("nvim-tree.node.directory")
local FileLinkNode = require("nvim-tree.node.file-link")
local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks") local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks")
local DecoratorCopied = require("nvim-tree.renderer.decorator.copied") local DecoratorCopied = require("nvim-tree.renderer.decorator.copied")
local DecoratorCut = require("nvim-tree.renderer.decorator.cut") local DecoratorCut = require("nvim-tree.renderer.decorator.cut")
@@ -341,19 +345,21 @@ function Builder:add_highlights(node)
return icon_hl_group, name_hl_group return icon_hl_group, name_hl_group
end end
---Insert node line into self.lines, calling Builder:build_lines for each directory
---@private ---@private
---@param node Node
---@param idx integer line number starting at 1
---@param num_children integer of node
function Builder:build_line(node, idx, num_children) function Builder:build_line(node, idx, num_children)
-- various components -- various components
local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers) local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers)
local arrows = pad.get_arrows(node) local arrows = pad.get_arrows(node)
-- main components -- main components
local is_folder = node.nodes ~= nil
local is_symlink = node.link_to ~= nil
local icon, name local icon, name
if is_folder then if node:is(DirectoryNode) then
icon, name = self:build_folder(node) icon, name = self:build_folder(node)
elseif is_symlink then elseif node:is(DirectoryLinkNode) or node:is(FileLinkNode) then
icon, name = self:build_symlink(node) icon, name = self:build_symlink(node)
else else
icon, name = self:build_file(node) icon, name = self:build_file(node)
@@ -369,11 +375,13 @@ function Builder:build_line(node, idx, num_children)
self.index = self.index + 1 self.index = self.index + 1
node = node:last_group_node() if node:is(DirectoryNode) then
if node.open then node = node:last_group_node()
self.depth = self.depth + 1 if node.open then
self:build_lines(node) self.depth = self.depth + 1
self.depth = self.depth - 1 self:build_lines(node)
self.depth = self.depth - 1
end
end end
end end
@@ -403,8 +411,11 @@ function Builder:add_hidden_count_string(node, idx, num_children)
end end
end end
---Number of visible nodes
---@private ---@private
function Builder:get_nodes_number(nodes) ---@param nodes Node[]
---@return integer
function Builder:num_visible(nodes)
if not self.explorer.live_filter.filter then if not self.explorer.live_filter.filter then
return #nodes return #nodes
end end
@@ -423,7 +434,7 @@ function Builder:build_lines(node)
if not node then if not node then
node = self.explorer node = self.explorer
end end
local num_children = self:get_nodes_number(node.nodes) local num_children = self:num_visible(node.nodes)
local idx = 1 local idx = 1
for _, n in ipairs(node.nodes) do for _, n in ipairs(node.nodes) do
if not n.hidden then if not n.hidden then

View File

@@ -1,26 +1,16 @@
local Class = require("nvim-tree.class")
local HL_POSITION = require("nvim-tree.enum").HL_POSITION local HL_POSITION = require("nvim-tree.enum").HL_POSITION
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
---Abstract Decorator ---Abstract Decorator
---Uses the factory pattern to instantiate child instances. ---Uses the factory pattern to instantiate child instances.
---@class (exact) Decorator ---@class (exact) Decorator: Class
---@field private __index? table
---@field protected explorer Explorer ---@field protected explorer Explorer
---@field protected enabled boolean ---@field protected enabled boolean
---@field protected hl_pos HL_POSITION ---@field protected hl_pos HL_POSITION
---@field protected icon_placement ICON_PLACEMENT ---@field protected icon_placement ICON_PLACEMENT
local Decorator = {} local Decorator = Class:new()
---@param o Decorator|nil
---@return Decorator
function Decorator:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
---Maybe highlight groups ---Maybe highlight groups
---@param node Node ---@param node Node