* add todo * refactor(#2886): multi instance: node class refactoring: extract links, *_git_status (#2944) * extract DirectoryLinkNode and FileLinkNode, move Node methods to children * temporarily move DirectoryNode methods into BaseNode for easier reviewing * move mostly unchanged DirectoryNode methods back to BaseNode * tidy * git.git_status_file takes an array * update git status of links * luacheck hack * safer git_status_dir * refactor(#2886): multi instance: node class refactoring: DirectoryNode:expand_or_collapse (#2957) move expand_or_collapse to DirectoryNode * refactor(#2886): multi instance: node group functions refactoring (#2959) * move last_group_node to DirectoryNode * move add BaseNode:as and more doc * revert parameter name changes * revert parameter name changes * add Class * move group methods into DN * tidy group methods * tidy group methods * tidy group methods * tidy group methods * parent is DirectoryNode * tidy expand all * BaseNode -> Node * move watcher to DirectoryNode * last_group_node is DirectoryNode only * simplify create-file * simplify parent * simplify collapse-all * simplify live-filter * style * more type safety
This commit is contained in:
parent
63c7ad9037
commit
68be6df2fc
@ -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)
|
||||
|
||||
@ -2,6 +2,8 @@ local log = require("nvim-tree.log")
|
||||
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 Iterator = require("nvim-tree.iterators.node-iterator")
|
||||
|
||||
local M = {}
|
||||
@ -58,19 +60,27 @@ function M.fn(path)
|
||||
local link_match = node.link_to and vim.startswith(path_real, node.link_to .. utils.path_separator)
|
||||
|
||||
if abs_match or link_match then
|
||||
if not node.group_next then
|
||||
node.open = true
|
||||
end
|
||||
if #node.nodes == 0 then
|
||||
core.get_explorer():expand(node)
|
||||
if node.group_next and incremented_line then
|
||||
line = line - 1
|
||||
local dir = node:as(DirectoryNode)
|
||||
if dir then
|
||||
if not dir.group_next then
|
||||
dir.open = true
|
||||
end
|
||||
if #dir.nodes == 0 then
|
||||
core.get_explorer():expand(dir)
|
||||
if dir.group_next and incremented_line then
|
||||
line = line - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
:recursor(function(node)
|
||||
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
|
||||
node = node and node:as(DirectoryNode)
|
||||
if node then
|
||||
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end)
|
||||
:iterate()
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -4,6 +4,8 @@ local core = require("nvim-tree.core")
|
||||
local lib = require("nvim-tree.lib")
|
||||
local diagnostics = require("nvim-tree.diagnostics")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
local M = {}
|
||||
local MAX_DEPTH = 100
|
||||
|
||||
@ -70,11 +72,12 @@ local function move(where, what, skip_gitignored)
|
||||
end
|
||||
end
|
||||
|
||||
---@param node DirectoryNode
|
||||
local function expand_node(node)
|
||||
if not node.open then
|
||||
-- Expand the node.
|
||||
-- Should never collapse since we checked open.
|
||||
node:expand_or_collapse()
|
||||
node:expand_or_collapse(false)
|
||||
end
|
||||
end
|
||||
|
||||
@ -96,8 +99,9 @@ local function move_next_recursive(what, skip_gitignored)
|
||||
if node_init.name ~= ".." then -- root node cannot have a status
|
||||
valid = status_is_valid(node_init, what, skip_gitignored)
|
||||
end
|
||||
if node_init.nodes ~= nil and valid and not node_init.open then
|
||||
node_init:expand_or_collapse()
|
||||
local node_dir = node_init:as(DirectoryNode)
|
||||
if node_dir and valid and not node_dir.open then
|
||||
node_dir:expand_or_collapse(false)
|
||||
end
|
||||
|
||||
move("next", what, skip_gitignored)
|
||||
@ -114,20 +118,15 @@ local function move_next_recursive(what, skip_gitignored)
|
||||
|
||||
-- i is used to limit iterations.
|
||||
local i = 0
|
||||
local is_dir = node_cur.nodes ~= nil
|
||||
while is_dir and i < MAX_DEPTH do
|
||||
expand_node(node_cur)
|
||||
local dir_cur = node_cur:as(DirectoryNode)
|
||||
while dir_cur and i < MAX_DEPTH do
|
||||
expand_node(dir_cur)
|
||||
|
||||
move("next", what, skip_gitignored)
|
||||
|
||||
-- Save current node.
|
||||
node_cur = lib.get_node_at_cursor()
|
||||
-- Update is_dir.
|
||||
if node_cur then
|
||||
is_dir = node_cur.nodes ~= nil
|
||||
else
|
||||
is_dir = false
|
||||
end
|
||||
dir_cur = node_cur and node_cur:as(DirectoryNode)
|
||||
|
||||
i = i + 1
|
||||
end
|
||||
@ -180,8 +179,10 @@ local function move_prev_recursive(what, skip_gitignored)
|
||||
end
|
||||
|
||||
-- 4.2)
|
||||
local node_dir = node_cur
|
||||
expand_node(node_dir)
|
||||
local node_dir = node_cur:as(DirectoryNode)
|
||||
if node_dir then
|
||||
expand_node(node_dir)
|
||||
end
|
||||
|
||||
-- 4.3)
|
||||
if node_init.name == ".." then -- root node
|
||||
|
||||
@ -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
|
||||
|
||||
@ -9,6 +9,9 @@ local help = require("nvim-tree.help")
|
||||
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 = {},
|
||||
node = {
|
||||
@ -135,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)
|
||||
@ -208,12 +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.nodes then
|
||||
elseif node:is(DirectoryNode) then
|
||||
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
|
||||
|
||||
@ -283,33 +283,46 @@ function M.load_project_status(path)
|
||||
end
|
||||
end
|
||||
|
||||
---Git file and directory status for an absolute path with optional file fallback
|
||||
---@param parent_ignored boolean
|
||||
---@param status table|nil
|
||||
---@param absolute_path string
|
||||
---@param path string
|
||||
---@param path_file string? alternative file path when no other file status
|
||||
---@return GitStatus|nil
|
||||
function M.git_status_dir(parent_ignored, status, absolute_path)
|
||||
function M.git_status_dir(parent_ignored, status, path, path_file)
|
||||
if parent_ignored then
|
||||
return { file = "!!" }
|
||||
end
|
||||
|
||||
if status then
|
||||
return {
|
||||
file = status.files and status.files[absolute_path],
|
||||
file = status.files and (status.files[path] or status.files[path_file]),
|
||||
dir = status.dirs and {
|
||||
direct = status.dirs.direct[absolute_path],
|
||||
indirect = status.dirs.indirect[absolute_path],
|
||||
direct = status.dirs.direct and status.dirs.direct[path],
|
||||
indirect = status.dirs.indirect and status.dirs.indirect[path],
|
||||
},
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
---Git file status for an absolute path with optional fallback
|
||||
---@param parent_ignored boolean
|
||||
---@param status table|nil
|
||||
---@param absolute_path string
|
||||
---@param path string
|
||||
---@param path_fallback string?
|
||||
---@return GitStatus
|
||||
function M.git_status_file(parent_ignored, status, absolute_path)
|
||||
local file_status = parent_ignored and "!!" or (status and status.files and status.files[absolute_path])
|
||||
return { file = file_status }
|
||||
function M.git_status_file(parent_ignored, status, path, path_fallback)
|
||||
if parent_ignored then
|
||||
return { file = "!!" }
|
||||
end
|
||||
|
||||
if not status or not status.files then
|
||||
return {}
|
||||
end
|
||||
|
||||
return {
|
||||
file = status.files[path] or status.files[path_fallback]
|
||||
}
|
||||
end
|
||||
|
||||
function M.purge_state()
|
||||
|
||||
@ -21,6 +21,25 @@ function M.raw(typ, fmt, ...)
|
||||
end
|
||||
end
|
||||
|
||||
--- Write to a new file
|
||||
---@param typ string as per log.types config
|
||||
---@param path string absolute path
|
||||
---@param fmt string for string.format
|
||||
---@param ... any arguments for string.format
|
||||
function M.file(typ, path, fmt, ...)
|
||||
if not M.enabled(typ) then
|
||||
return
|
||||
end
|
||||
|
||||
local line = string.format(fmt, ...)
|
||||
local file = io.open(path, "w")
|
||||
if file then
|
||||
io.output(file)
|
||||
io.write(line)
|
||||
io.close(file)
|
||||
end
|
||||
end
|
||||
|
||||
---@class Profile
|
||||
---@field start number nanos
|
||||
---@field tag string
|
||||
|
||||
54
lua/nvim-tree/node/directory-link.lua
Normal file
54
lua/nvim-tree/node/directory-link.lua
Normal file
@ -0,0 +1,54 @@
|
||||
local git = require("nvim-tree.git")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
---@class (exact) DirectoryLinkNode: DirectoryNode
|
||||
---@field link_to string absolute path
|
||||
---@field fs_stat_target uv.fs_stat.result
|
||||
local DirectoryLinkNode = DirectoryNode:new()
|
||||
|
||||
---Static factory method
|
||||
---@param explorer Explorer
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param link_to string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result?
|
||||
---@param fs_stat_target uv.fs_stat.result
|
||||
---@return DirectoryLinkNode? nil on vim.loop.fs_realpath failure
|
||||
function DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
|
||||
-- create DirectoryNode with the target path for the watcher
|
||||
local o = DirectoryNode:create(explorer, parent, link_to, name, fs_stat)
|
||||
|
||||
o = self:new(o) --[[@as DirectoryLinkNode]]
|
||||
|
||||
-- reset absolute path to the link itself
|
||||
o.absolute_path = absolute_path
|
||||
|
||||
o.type = "link"
|
||||
o.link_to = link_to
|
||||
o.fs_stat_target = fs_stat_target
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
-----Update the directory GitStatus of link target and the file status of the link itself
|
||||
-----@param parent_ignored boolean
|
||||
-----@param status table|nil
|
||||
function DirectoryLinkNode:update_git_status(parent_ignored, status)
|
||||
self.git_status = git.git_status_dir(parent_ignored, status, self.link_to, self.absolute_path)
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---@return DirectoryLinkNode cloned
|
||||
function DirectoryLinkNode:clone()
|
||||
local clone = DirectoryNode.clone(self) --[[@as DirectoryLinkNode]]
|
||||
|
||||
clone.type = self.type
|
||||
clone.link_to = self.link_to
|
||||
clone.fs_stat_target = self.fs_stat_target
|
||||
|
||||
return clone
|
||||
end
|
||||
|
||||
return DirectoryLinkNode
|
||||
@ -1,18 +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
|
||||
@ -50,18 +52,182 @@ 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
|
||||
---@param parent_ignored boolean
|
||||
---@param status table|nil
|
||||
function DirectoryNode:update_git_status(parent_ignored, status)
|
||||
self.git_status = git.git_status_dir(parent_ignored, status, self.absolute_path, nil)
|
||||
end
|
||||
|
||||
---@return GitStatus|nil
|
||||
function DirectoryNode:get_git_status()
|
||||
if not self.git_status or 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
|
||||
|
||||
---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
|
||||
self.has_children = false
|
||||
end
|
||||
|
||||
if #self.nodes == 0 then
|
||||
self.explorer:expand(self)
|
||||
end
|
||||
|
||||
local head_node = self:get_parent_of_group() or self
|
||||
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
|
||||
|
||||
local node = self
|
||||
while node do
|
||||
node.open = next_open
|
||||
node = node.group_next
|
||||
end
|
||||
|
||||
self.explorer.renderer:draw()
|
||||
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
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
local DirectoryLinkNode = require("nvim-tree.node.directory-link")
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
local LinkNode = require("nvim-tree.node.link")
|
||||
local FileLinkNode = require("nvim-tree.node.file-link")
|
||||
local FileNode = require("nvim-tree.node.file")
|
||||
local Watcher = require("nvim-tree.watcher")
|
||||
|
||||
@ -7,22 +8,38 @@ local M = {}
|
||||
|
||||
---Factory function to create the appropriate Node
|
||||
---@param explorer Explorer
|
||||
---@param parent Node
|
||||
---@param abs string
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path 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)
|
||||
function M.create_node(explorer, parent, absolute_path, 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)
|
||||
if stat.type == "directory" then
|
||||
-- directory must be readable and enumerable
|
||||
if vim.loop.fs_access(absolute_path, "R") and Watcher.is_fs_event_capable(absolute_path) then
|
||||
return DirectoryNode:create(explorer, parent, absolute_path, name, stat)
|
||||
end
|
||||
elseif stat.type == "file" then
|
||||
return FileNode:create(explorer, parent, abs, name, stat)
|
||||
-- any file
|
||||
return FileNode:create(explorer, parent, absolute_path, name, stat)
|
||||
elseif stat.type == "link" then
|
||||
return LinkNode:create(explorer, parent, abs, name, stat)
|
||||
-- link target path and stat must resolve
|
||||
local link_to = vim.loop.fs_realpath(absolute_path)
|
||||
local link_to_stat = link_to and vim.loop.fs_stat(link_to)
|
||||
if not link_to or not link_to_stat then
|
||||
return
|
||||
end
|
||||
|
||||
-- choose directory or file
|
||||
if link_to_stat.type == "directory" then
|
||||
return DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name, stat, link_to_stat)
|
||||
else
|
||||
return FileLinkNode:create(explorer, parent, absolute_path, link_to, name, stat, link_to_stat)
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
|
||||
50
lua/nvim-tree/node/file-link.lua
Normal file
50
lua/nvim-tree/node/file-link.lua
Normal file
@ -0,0 +1,50 @@
|
||||
local git = require("nvim-tree.git")
|
||||
|
||||
local FileNode = require("nvim-tree.node.file")
|
||||
|
||||
---@class (exact) FileLinkNode: FileNode
|
||||
---@field link_to string absolute path
|
||||
---@field fs_stat_target uv.fs_stat.result
|
||||
local FileLinkNode = FileNode:new()
|
||||
|
||||
---Static factory method
|
||||
---@param explorer Explorer
|
||||
---@param parent DirectoryNode
|
||||
---@param absolute_path string
|
||||
---@param link_to string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result?
|
||||
---@param fs_stat_target uv.fs_stat.result
|
||||
---@return FileLinkNode? nil on vim.loop.fs_realpath failure
|
||||
function FileLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target)
|
||||
local o = FileNode:create(explorer, parent, absolute_path, name, fs_stat)
|
||||
|
||||
o = self:new(o) --[[@as FileLinkNode]]
|
||||
|
||||
o.type = "link"
|
||||
o.link_to = link_to
|
||||
o.fs_stat_target = fs_stat_target
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
-----Update the GitStatus of the target otherwise the link itself
|
||||
-----@param parent_ignored boolean
|
||||
-----@param status table|nil
|
||||
function FileLinkNode:update_git_status(parent_ignored, status)
|
||||
self.git_status = git.git_status_file(parent_ignored, status, self.link_to, self.absolute_path)
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node
|
||||
---@return FileLinkNode cloned
|
||||
function FileLinkNode:clone()
|
||||
local clone = FileNode.clone(self) --[[@as FileLinkNode]]
|
||||
|
||||
clone.type = self.type
|
||||
clone.link_to = self.link_to
|
||||
clone.fs_stat_target = self.fs_stat_target
|
||||
|
||||
return clone
|
||||
end
|
||||
|
||||
return FileLinkNode
|
||||
@ -1,14 +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?
|
||||
@ -26,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 "",
|
||||
@ -36,10 +36,26 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
|
||||
return o
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---Update the GitStatus of the file
|
||||
---@param parent_ignored boolean
|
||||
---@param status table|nil
|
||||
function FileNode:update_git_status(parent_ignored, status)
|
||||
self.git_status = git.git_status_file(parent_ignored, status, self.absolute_path, nil)
|
||||
end
|
||||
|
||||
---@return GitStatus|nil
|
||||
function FileNode:get_git_status()
|
||||
if not self.git_status then
|
||||
return nil
|
||||
end
|
||||
|
||||
return self.git_status.file and { self.git_status.file }
|
||||
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,9 +1,10 @@
|
||||
local git = require("nvim-tree.git")
|
||||
|
||||
local Class = require("nvim-tree.class")
|
||||
|
||||
---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
|
||||
@ -12,136 +13,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|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
|
||||
function Node:destroy()
|
||||
end
|
||||
|
||||
--luacheck: push ignore 212
|
||||
---Update the GitStatus of the node
|
||||
---@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
|
||||
---@param status table?
|
||||
function Node:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local
|
||||
---TODO find a way to declare abstract methods
|
||||
end
|
||||
|
||||
---@return GitStatus|nil
|
||||
function BaseNode:get_git_status()
|
||||
if not self.git_status then
|
||||
-- status doesn't exist
|
||||
return nil
|
||||
end
|
||||
--luacheck: pop
|
||||
|
||||
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
|
||||
---@return GitStatus?
|
||||
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
|
||||
@ -153,13 +48,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) == "."))
|
||||
@ -171,21 +66,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 --[[@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)
|
||||
---@param project table?
|
||||
---@param root string?
|
||||
function Node:update_parent_statuses(project, root)
|
||||
local node = self
|
||||
while project and node do
|
||||
-- step up to the containing project
|
||||
@ -215,118 +98,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
|
||||
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]
|
||||
end
|
||||
end
|
||||
|
||||
function BaseNode:expand_or_collapse(toggle_group)
|
||||
toggle_group = toggle_group or false
|
||||
if self.has_children then
|
||||
---@cast self DirectoryNode -- TODO #2886 move this to the class
|
||||
self.has_children = false
|
||||
---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
|
||||
|
||||
if #self.nodes == 0 then
|
||||
self.explorer:expand(self)
|
||||
local node = self.parent
|
||||
while node do
|
||||
if node.parent and node.parent.group_next then
|
||||
node = node.parent
|
||||
else
|
||||
return node
|
||||
end
|
||||
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()
|
||||
---@return Node cloned
|
||||
function Node:clone()
|
||||
---@type Explorer
|
||||
local explorer_placeholder = nil
|
||||
|
||||
---@type BaseNode
|
||||
---@type Node
|
||||
local clone = {
|
||||
type = self.type,
|
||||
explorer = explorer_placeholder,
|
||||
@ -338,11 +133,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
|
||||
|
||||
@ -1,89 +0,0 @@
|
||||
local watch = require("nvim-tree.explorer.watch")
|
||||
|
||||
local BaseNode = require("nvim-tree.node")
|
||||
|
||||
---@class (exact) LinkNode: BaseNode
|
||||
---@field has_children boolean
|
||||
---@field group_next Node? -- If node is grouped, this points to the next child dir/link node
|
||||
---@field link_to string absolute path
|
||||
---@field nodes Node[]
|
||||
---@field open boolean
|
||||
local LinkNode = BaseNode:new()
|
||||
|
||||
---Static factory method
|
||||
---@param explorer Explorer
|
||||
---@param parent Node
|
||||
---@param absolute_path string
|
||||
---@param name string
|
||||
---@param fs_stat uv.fs_stat.result?
|
||||
---@return LinkNode? nil on vim.loop.fs_realpath failure
|
||||
function LinkNode:create(explorer, parent, absolute_path, name, fs_stat)
|
||||
-- INFO: sometimes fs_realpath returns nil
|
||||
-- I expect this be a bug in glibc, because it fails to retrieve the path for some
|
||||
-- links (for instance libr2.so in /usr/lib) and thus even with a C program realpath fails
|
||||
-- when it has no real reason to. Maybe there is a reason, but errno is definitely wrong.
|
||||
local link_to = vim.loop.fs_realpath(absolute_path)
|
||||
if not link_to then
|
||||
return nil
|
||||
end
|
||||
|
||||
local open, nodes, has_children
|
||||
local is_dir_link = (link_to ~= nil) and vim.loop.fs_stat(link_to).type == "directory"
|
||||
|
||||
if is_dir_link and link_to then
|
||||
local handle = vim.loop.fs_scandir(link_to)
|
||||
has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil or false
|
||||
open = false
|
||||
nodes = {}
|
||||
end
|
||||
|
||||
---@type LinkNode
|
||||
local o = {
|
||||
type = "link",
|
||||
explorer = explorer,
|
||||
absolute_path = absolute_path,
|
||||
executable = false,
|
||||
fs_stat = fs_stat,
|
||||
hidden = false,
|
||||
is_dot = false,
|
||||
name = name,
|
||||
parent = parent,
|
||||
watcher = nil,
|
||||
diag_status = nil,
|
||||
|
||||
has_children = has_children,
|
||||
group_next = nil,
|
||||
link_to = link_to,
|
||||
nodes = nodes,
|
||||
open = open,
|
||||
}
|
||||
o = self:new(o) --[[@as LinkNode]]
|
||||
|
||||
if is_dir_link then
|
||||
o.watcher = watch.create_watcher(o)
|
||||
end
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
---Create a sanitized partial copy of a node, populating children recursively.
|
||||
---@return LinkNode cloned
|
||||
function LinkNode:clone()
|
||||
local clone = BaseNode.clone(self) --[[@as LinkNode]]
|
||||
|
||||
clone.has_children = self.has_children
|
||||
clone.group_next = nil
|
||||
clone.link_to = self.link_to
|
||||
clone.nodes = {}
|
||||
clone.open = self.open
|
||||
|
||||
if self.nodes then
|
||||
for _, child in ipairs(self.nodes) do
|
||||
table.insert(clone.nodes, child:clone())
|
||||
end
|
||||
end
|
||||
|
||||
return clone
|
||||
end
|
||||
|
||||
return LinkNode
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user