refactor(#2886): multi instance: node class refactoring (#2950)

* 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:
Alexander Courtis
2024-10-25 12:24:59 +11:00
committed by GitHub
parent 63c7ad9037
commit 68be6df2fc
25 changed files with 591 additions and 482 deletions

View File

@@ -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()

View File

@@ -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]

View File

@@ -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 ",

View 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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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