* 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
@@ -125,7 +125,7 @@ function M.place_cursor_on_node()
|
||||
if not node or node.name == ".." then
|
||||
return
|
||||
end
|
||||
node = node:get_parent_of_group()
|
||||
node = node:get_parent_of_group() or node
|
||||
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
|
||||
@@ -43,6 +43,7 @@ function M.fn(path)
|
||||
return node.absolute_path == path_real or node.link_to == path_real
|
||||
end)
|
||||
:applier(function(node)
|
||||
---@cast node DirectoryNode
|
||||
local incremented_line = false
|
||||
if not node.group_next then
|
||||
line = line + 1
|
||||
|
||||
@@ -7,6 +7,8 @@ local notify = require("nvim-tree.notify")
|
||||
|
||||
local find_file = require("nvim-tree.actions.finders.find-file").fn
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
---@enum ACTION
|
||||
local ACTION = {
|
||||
copy = "copy",
|
||||
@@ -219,7 +221,7 @@ end
|
||||
function Clipboard:do_paste(node, action, action_fn)
|
||||
if node.name == ".." then
|
||||
node = self.explorer
|
||||
else
|
||||
elseif node:is(DirectoryNode) then
|
||||
node = node:last_group_node()
|
||||
end
|
||||
local clip = self.data[action]
|
||||
|
||||
@@ -5,6 +5,9 @@ local notify = require("nvim-tree.notify")
|
||||
|
||||
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 = {}
|
||||
|
||||
---@param file string
|
||||
@@ -29,35 +32,21 @@ local function get_num_nodes(iter)
|
||||
return i
|
||||
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?
|
||||
function M.fn(node)
|
||||
local cwd = core.get_cwd()
|
||||
if cwd == nil then
|
||||
node = node or core.get_explorer() --[[@as Node]]
|
||||
if not node then
|
||||
return
|
||||
end
|
||||
|
||||
if not node or node.name == ".." then
|
||||
node = {
|
||||
absolute_path = cwd,
|
||||
name = "",
|
||||
nodes = core.get_explorer().nodes,
|
||||
open = true,
|
||||
}
|
||||
else
|
||||
node = node:last_group_node()
|
||||
local dir = node:is(FileNode) and node.parent or node:as(DirectoryNode)
|
||||
if not dir then
|
||||
return
|
||||
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 = {
|
||||
prompt = "Create file ",
|
||||
|
||||
@@ -6,6 +6,8 @@ local notify = require("nvim-tree.notify")
|
||||
|
||||
local find_file = require("nvim-tree.actions.finders.find-file").fn
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
local M = {
|
||||
config = {},
|
||||
}
|
||||
@@ -120,7 +122,9 @@ function M.fn(default_modifier)
|
||||
return
|
||||
end
|
||||
|
||||
node = node:last_group_node()
|
||||
if node:is(DirectoryNode) then
|
||||
node = node:last_group_node()
|
||||
end
|
||||
if node.name == ".." then
|
||||
return
|
||||
end
|
||||
|
||||
@@ -78,7 +78,7 @@ local function expand_node(node)
|
||||
---@cast node DirectoryNode
|
||||
-- Expand the node.
|
||||
-- Should never collapse since we checked open.
|
||||
node:expand_or_collapse()
|
||||
node:expand_or_collapse(false)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -102,7 +102,7 @@ local function move_next_recursive(what, skip_gitignored)
|
||||
end
|
||||
if node_init:is(DirectoryNode) and valid and not node_init.open then
|
||||
---@cast node_init DirectoryNode
|
||||
node_init:expand_or_collapse()
|
||||
node_init:expand_or_collapse(false)
|
||||
end
|
||||
|
||||
move("next", what, skip_gitignored)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
local view = require("nvim-tree.view")
|
||||
local utils = require("nvim-tree.utils")
|
||||
local core = require("nvim-tree.core")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
local M = {}
|
||||
|
||||
@@ -9,33 +10,32 @@ local M = {}
|
||||
function M.fn(should_close)
|
||||
should_close = should_close or false
|
||||
|
||||
---@param node Node
|
||||
return function(node)
|
||||
local explorer = core.get_explorer()
|
||||
node = node:last_group_node()
|
||||
if should_close and node.open then
|
||||
node.open = false
|
||||
if explorer then
|
||||
explorer.renderer:draw()
|
||||
local dir = node:as(DirectoryNode)
|
||||
if dir then
|
||||
dir = dir:last_group_node()
|
||||
if should_close and dir.open then
|
||||
dir.open = false
|
||||
dir.explorer.renderer:draw()
|
||||
return
|
||||
end
|
||||
return
|
||||
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
|
||||
return view.set_cursor({ 1, 0 })
|
||||
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
|
||||
end)
|
||||
|
||||
view.set_cursor({ line + 1, 0 })
|
||||
if should_close then
|
||||
parent.open = false
|
||||
if explorer then
|
||||
explorer.renderer:draw()
|
||||
end
|
||||
parent.explorer.renderer:draw()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,8 @@ local core = require("nvim-tree.core")
|
||||
local lib = require("nvim-tree.lib")
|
||||
local Iterator = require("nvim-tree.iterators.node-iterator")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
local M = {}
|
||||
|
||||
---@return fun(path: string): boolean
|
||||
@@ -36,8 +38,9 @@ function M.fn(keep_buffers)
|
||||
Iterator.builder(explorer.nodes)
|
||||
:hidden()
|
||||
:applier(function(n)
|
||||
if n.nodes ~= nil then
|
||||
n.open = keep_buffers == true and matches(n.absolute_path)
|
||||
local dir = n:as(DirectoryNode)
|
||||
if dir then
|
||||
dir.open = keep_buffers and matches(dir.absolute_path)
|
||||
end
|
||||
end)
|
||||
:recursor(function(n)
|
||||
|
||||
@@ -2,6 +2,8 @@ local core = require("nvim-tree.core")
|
||||
local Iterator = require("nvim-tree.iterators.node-iterator")
|
||||
local notify = require("nvim-tree.notify")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
local M = {}
|
||||
|
||||
---@param list string[]
|
||||
@@ -15,7 +17,7 @@ local function to_lookup_table(list)
|
||||
return table
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
---@param node DirectoryNode
|
||||
local function expand(node)
|
||||
node = node:last_group_node()
|
||||
node.open = true
|
||||
@@ -36,6 +38,7 @@ end
|
||||
local function gen_iterator()
|
||||
local expansion_count = 0
|
||||
|
||||
---@param parent DirectoryNode
|
||||
return function(parent)
|
||||
if parent.parent and parent.nodes and not parent.open then
|
||||
expansion_count = expansion_count + 1
|
||||
@@ -44,12 +47,14 @@ local function gen_iterator()
|
||||
|
||||
Iterator.builder(parent.nodes)
|
||||
:hidden()
|
||||
---@param node DirectoryNode
|
||||
:applier(function(node)
|
||||
if should_expand(expansion_count, node) then
|
||||
expansion_count = expansion_count + 1
|
||||
expand(node)
|
||||
end
|
||||
end)
|
||||
---@param node DirectoryNode
|
||||
:recursor(function(node)
|
||||
return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes))
|
||||
end)
|
||||
@@ -61,11 +66,16 @@ local function gen_iterator()
|
||||
end
|
||||
end
|
||||
|
||||
---Expand the directory node or the root
|
||||
---@param node Node
|
||||
function M.fn(node)
|
||||
local explorer = core.get_explorer()
|
||||
node = node.nodes and node or explorer
|
||||
if gen_iterator()(node) then
|
||||
local parent = node:as(DirectoryNode) or explorer
|
||||
if not parent then
|
||||
return
|
||||
end
|
||||
|
||||
if gen_iterator()(parent) then
|
||||
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
|
||||
end
|
||||
if explorer then
|
||||
|
||||
@@ -10,6 +10,7 @@ local keymap = require("nvim-tree.keymap")
|
||||
local notify = require("nvim-tree.notify")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
local RootNode = require("nvim-tree.node.root")
|
||||
|
||||
local Api = {
|
||||
tree = {},
|
||||
@@ -137,9 +138,9 @@ Api.tree.change_root = wrap(function(...)
|
||||
end)
|
||||
|
||||
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("..")
|
||||
elseif node.nodes ~= nil then
|
||||
elseif node:is(DirectoryNode) then
|
||||
actions.root.change_dir.fn(node:last_group_node().absolute_path)
|
||||
end
|
||||
end)
|
||||
@@ -210,13 +211,13 @@ local function edit(mode, node)
|
||||
end
|
||||
|
||||
---@param mode string
|
||||
---@return fun(node: table)
|
||||
---@return fun(node: Node)
|
||||
local function open_or_expand_or_dir_up(mode, toggle_group)
|
||||
---@param node Node
|
||||
return function(node)
|
||||
if node.name == ".." then
|
||||
actions.root.change_dir.fn("..")
|
||||
elseif node:is(DirectoryNode) then
|
||||
---@cast node DirectoryNode
|
||||
node:expand_or_collapse(toggle_group)
|
||||
elseif not toggle_group then
|
||||
edit(mode, node)
|
||||
|
||||
40
lua/nvim-tree/class.lua
Normal file
40
lua/nvim-tree/class.lua
Normal 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
|
||||
@@ -5,6 +5,7 @@ local utils = require("nvim-tree.utils")
|
||||
local view = require("nvim-tree.view")
|
||||
local node_factory = require("nvim-tree.node.factory")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
local RootNode = require("nvim-tree.node.root")
|
||||
local Watcher = require("nvim-tree.watcher")
|
||||
|
||||
@@ -72,12 +73,12 @@ function Explorer:create(path)
|
||||
return o
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
---@param node DirectoryNode
|
||||
function Explorer:expand(node)
|
||||
self:_load(node)
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
---@param node DirectoryNode
|
||||
---@param git_status table|nil
|
||||
function Explorer:reload(node, git_status)
|
||||
local cwd = node.link_to or node.absolute_path
|
||||
@@ -169,11 +170,10 @@ function Explorer:reload(node, git_status)
|
||||
end, node.nodes)
|
||||
)
|
||||
|
||||
local is_root = not node.parent
|
||||
local child_folder_only = node:has_one_child_folder() and node.nodes[1]
|
||||
if config.renderer.group_empty and not is_root and child_folder_only then
|
||||
node.group_next = child_folder_only
|
||||
local ns = self:reload(child_folder_only, git_status)
|
||||
local single_child = node:single_child_directory()
|
||||
if config.renderer.group_empty and node.parent and single_child then
|
||||
node.group_next = single_child
|
||||
local ns = self:reload(single_child, git_status)
|
||||
node.nodes = ns or {}
|
||||
log.profile_end(profile)
|
||||
return ns
|
||||
@@ -219,7 +219,7 @@ function Explorer:refresh_parent_nodes_for_path(path)
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param node Node
|
||||
---@param node DirectoryNode
|
||||
function Explorer:_load(node)
|
||||
local cwd = node.link_to or node.absolute_path
|
||||
local git_status = git.load_project_status(cwd)
|
||||
@@ -243,7 +243,7 @@ end
|
||||
---@private
|
||||
---@param handle uv.uv_fs_t
|
||||
---@param cwd string
|
||||
---@param node Node
|
||||
---@param node DirectoryNode
|
||||
---@param git_status table
|
||||
---@param parent Explorer
|
||||
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
|
||||
|
||||
---@private
|
||||
---@param node Node
|
||||
---@param node DirectoryNode
|
||||
---@param status table
|
||||
---@param parent Explorer
|
||||
---@return Node[]|nil
|
||||
@@ -311,12 +311,12 @@ function Explorer:explore(node, status, parent)
|
||||
self:populate_children(handle, cwd, node, status, parent)
|
||||
|
||||
local is_root = not node.parent
|
||||
local child_folder_only = node:has_one_child_folder() and node.nodes[1]
|
||||
if config.renderer.group_empty and not is_root and child_folder_only then
|
||||
local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path
|
||||
local single_child = node:single_child_directory()
|
||||
if config.renderer.group_empty and not is_root and single_child then
|
||||
local child_cwd = single_child.link_to or single_child.absolute_path
|
||||
local child_status = git.load_project_status(child_cwd)
|
||||
node.group_next = child_folder_only
|
||||
local ns = self:explore(child_folder_only, child_status, parent)
|
||||
node.group_next = single_child
|
||||
local ns = self:explore(single_child, child_status, parent)
|
||||
node.nodes = ns or {}
|
||||
|
||||
log.profile_end(profile)
|
||||
@@ -335,9 +335,10 @@ end
|
||||
function Explorer:refresh_nodes(projects)
|
||||
Iterator.builder({ self })
|
||||
:applier(function(n)
|
||||
if n.nodes then
|
||||
local toplevel = git.get_toplevel(n.cwd or n.link_to or n.absolute_path)
|
||||
self:reload(n, projects[toplevel] or {})
|
||||
local dir = n:as(DirectoryNode)
|
||||
if dir then
|
||||
local toplevel = git.get_toplevel(dir.cwd or dir.link_to or dir.absolute_path)
|
||||
self:reload(dir, projects[toplevel] or {})
|
||||
end
|
||||
end)
|
||||
:recursor(function(n)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
local view = require("nvim-tree.view")
|
||||
local utils = require("nvim-tree.utils")
|
||||
|
||||
local Iterator = require("nvim-tree.iterators.node-iterator")
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
---@class LiveFilter
|
||||
---@field explorer Explorer
|
||||
@@ -31,17 +33,19 @@ local function reset_filter(self, node_)
|
||||
return
|
||||
end
|
||||
|
||||
node_.hidden_stats = vim.tbl_deep_extend("force", node_.hidden_stats or {}, {
|
||||
live_filter = 0,
|
||||
})
|
||||
local dir_ = node_:as(DirectoryNode)
|
||||
if dir_ then
|
||||
dir_.hidden_stats = vim.tbl_deep_extend("force", dir_.hidden_stats or {}, { live_filter = 0, })
|
||||
end
|
||||
|
||||
Iterator.builder(node_.nodes)
|
||||
:hidden()
|
||||
:applier(function(node)
|
||||
node.hidden = false
|
||||
node.hidden_stats = vim.tbl_deep_extend("force", node.hidden_stats or {}, {
|
||||
live_filter = 0,
|
||||
})
|
||||
local dir = node:as(DirectoryNode)
|
||||
if dir then
|
||||
dir.hidden_stats = vim.tbl_deep_extend("force", dir.hidden_stats or {}, { live_filter = 0, })
|
||||
end
|
||||
end)
|
||||
:iterate()
|
||||
end
|
||||
@@ -85,7 +89,7 @@ local function matches(self, node)
|
||||
return vim.regex(self.filter):match_str(name) ~= nil
|
||||
end
|
||||
|
||||
---@param node_ Node?
|
||||
---@param node_ DirectoryNode?
|
||||
function LiveFilter:apply_filter(node_)
|
||||
if not self.filter or self.filter == "" then
|
||||
reset_filter(self, node_)
|
||||
|
||||
@@ -53,7 +53,7 @@ local function is_folder_ignored(path)
|
||||
return false
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
---@param node DirectoryNode
|
||||
---@return Watcher|nil
|
||||
function M.create_watcher(node)
|
||||
if not M.config.filesystem_watchers.enable or type(node) ~= "table" then
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,6 +2,10 @@ local notify = require("nvim-tree.notify")
|
||||
local utils = require("nvim-tree.utils")
|
||||
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 DecoratorCopied = require("nvim-tree.renderer.decorator.copied")
|
||||
local DecoratorCut = require("nvim-tree.renderer.decorator.cut")
|
||||
@@ -341,19 +345,21 @@ function Builder:add_highlights(node)
|
||||
return icon_hl_group, name_hl_group
|
||||
end
|
||||
|
||||
---Insert node line into self.lines, calling Builder:build_lines for each directory
|
||||
---@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)
|
||||
-- various components
|
||||
local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers)
|
||||
local arrows = pad.get_arrows(node)
|
||||
|
||||
-- main components
|
||||
local is_folder = node.nodes ~= nil
|
||||
local is_symlink = node.link_to ~= nil
|
||||
local icon, name
|
||||
if is_folder then
|
||||
if node:is(DirectoryNode) then
|
||||
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)
|
||||
else
|
||||
icon, name = self:build_file(node)
|
||||
@@ -369,11 +375,13 @@ function Builder:build_line(node, idx, num_children)
|
||||
|
||||
self.index = self.index + 1
|
||||
|
||||
node = node:last_group_node()
|
||||
if node.open then
|
||||
self.depth = self.depth + 1
|
||||
self:build_lines(node)
|
||||
self.depth = self.depth - 1
|
||||
if node:is(DirectoryNode) then
|
||||
node = node:last_group_node()
|
||||
if node.open then
|
||||
self.depth = self.depth + 1
|
||||
self:build_lines(node)
|
||||
self.depth = self.depth - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -403,8 +411,11 @@ function Builder:add_hidden_count_string(node, idx, num_children)
|
||||
end
|
||||
end
|
||||
|
||||
---Number of visible nodes
|
||||
---@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
|
||||
return #nodes
|
||||
end
|
||||
@@ -423,7 +434,7 @@ function Builder:build_lines(node)
|
||||
if not node then
|
||||
node = self.explorer
|
||||
end
|
||||
local num_children = self:get_nodes_number(node.nodes)
|
||||
local num_children = self:num_visible(node.nodes)
|
||||
local idx = 1
|
||||
for _, n in ipairs(node.nodes) do
|
||||
if not n.hidden then
|
||||
|
||||
@@ -1,26 +1,16 @@
|
||||
local Class = require("nvim-tree.class")
|
||||
|
||||
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
|
||||
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
|
||||
|
||||
---Abstract Decorator
|
||||
---Uses the factory pattern to instantiate child instances.
|
||||
---@class (exact) Decorator
|
||||
---@field private __index? table
|
||||
---@class (exact) Decorator: Class
|
||||
---@field protected explorer Explorer
|
||||
---@field protected enabled boolean
|
||||
---@field protected hl_pos HL_POSITION
|
||||
---@field protected icon_placement ICON_PLACEMENT
|
||||
local Decorator = {}
|
||||
|
||||
---@param o Decorator|nil
|
||||
---@return Decorator
|
||||
function Decorator:new(o)
|
||||
o = o or {}
|
||||
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
|
||||
return o
|
||||
end
|
||||
local Decorator = Class:new()
|
||||
|
||||
---Maybe highlight groups
|
||||
---@param node Node
|
||||
|
||||
Reference in New Issue
Block a user