Skip to content

Commit 68be6df

Browse files
authored
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
1 parent 63c7ad9 commit 68be6df

25 files changed

+591
-482
lines changed

Diff for: lua/nvim-tree.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function M.place_cursor_on_node()
125125
if not node or node.name == ".." then
126126
return
127127
end
128-
node = node:get_parent_of_group()
128+
node = node:get_parent_of_group() or node
129129

130130
local line = vim.api.nvim_get_current_line()
131131
local cursor = vim.api.nvim_win_get_cursor(0)

Diff for: lua/nvim-tree/actions/finders/find-file.lua

+18-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local log = require("nvim-tree.log")
22
local view = require("nvim-tree.view")
33
local utils = require("nvim-tree.utils")
44
local core = require("nvim-tree.core")
5+
6+
local DirectoryNode = require("nvim-tree.node.directory")
57
local Iterator = require("nvim-tree.iterators.node-iterator")
68

79
local M = {}
@@ -58,19 +60,27 @@ function M.fn(path)
5860
local link_match = node.link_to and vim.startswith(path_real, node.link_to .. utils.path_separator)
5961

6062
if abs_match or link_match then
61-
if not node.group_next then
62-
node.open = true
63-
end
64-
if #node.nodes == 0 then
65-
core.get_explorer():expand(node)
66-
if node.group_next and incremented_line then
67-
line = line - 1
63+
local dir = node:as(DirectoryNode)
64+
if dir then
65+
if not dir.group_next then
66+
dir.open = true
67+
end
68+
if #dir.nodes == 0 then
69+
core.get_explorer():expand(dir)
70+
if dir.group_next and incremented_line then
71+
line = line - 1
72+
end
6873
end
6974
end
7075
end
7176
end)
7277
:recursor(function(node)
73-
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
78+
node = node and node:as(DirectoryNode)
79+
if node then
80+
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
81+
else
82+
return nil
83+
end
7484
end)
7585
:iterate()
7686

Diff for: lua/nvim-tree/actions/fs/clipboard.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local notify = require("nvim-tree.notify")
77

88
local find_file = require("nvim-tree.actions.finders.find-file").fn
99

10+
local DirectoryNode = require("nvim-tree.node.directory")
11+
1012
---@enum ACTION
1113
local ACTION = {
1214
copy = "copy",
@@ -219,7 +221,7 @@ end
219221
function Clipboard:do_paste(node, action, action_fn)
220222
if node.name == ".." then
221223
node = self.explorer
222-
else
224+
elseif node:is(DirectoryNode) then
223225
node = node:last_group_node()
224226
end
225227
local clip = self.data[action]

Diff for: lua/nvim-tree/actions/fs/create-file.lua

+11-22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ local notify = require("nvim-tree.notify")
55

66
local find_file = require("nvim-tree.actions.finders.find-file").fn
77

8+
local FileNode = require("nvim-tree.node.file")
9+
local DirectoryNode = require("nvim-tree.node.directory")
10+
811
local M = {}
912

1013
---@param file string
@@ -29,35 +32,21 @@ local function get_num_nodes(iter)
2932
return i
3033
end
3134

32-
---@param node Node
33-
---@return string
34-
local function get_containing_folder(node)
35-
if node.nodes ~= nil then
36-
return utils.path_add_trailing(node.absolute_path)
37-
end
38-
local node_name_size = #(node.name or "")
39-
return node.absolute_path:sub(0, -node_name_size - 1)
40-
end
41-
4235
---@param node Node?
4336
function M.fn(node)
44-
local cwd = core.get_cwd()
45-
if cwd == nil then
37+
node = node or core.get_explorer() --[[@as Node]]
38+
if not node then
4639
return
4740
end
4841

49-
if not node or node.name == ".." then
50-
node = {
51-
absolute_path = cwd,
52-
name = "",
53-
nodes = core.get_explorer().nodes,
54-
open = true,
55-
}
56-
else
57-
node = node:last_group_node()
42+
local dir = node:is(FileNode) and node.parent or node:as(DirectoryNode)
43+
if not dir then
44+
return
5845
end
5946

60-
local containing_folder = get_containing_folder(node)
47+
dir = dir:last_group_node()
48+
49+
local containing_folder = utils.path_add_trailing(dir.absolute_path)
6150

6251
local input_opts = {
6352
prompt = "Create file ",

Diff for: lua/nvim-tree/actions/fs/rename-file.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ local notify = require("nvim-tree.notify")
66

77
local find_file = require("nvim-tree.actions.finders.find-file").fn
88

9+
local DirectoryNode = require("nvim-tree.node.directory")
10+
911
local M = {
1012
config = {},
1113
}
@@ -120,7 +122,9 @@ function M.fn(default_modifier)
120122
return
121123
end
122124

123-
node = node:last_group_node()
125+
if node:is(DirectoryNode) then
126+
node = node:last_group_node()
127+
end
124128
if node.name == ".." then
125129
return
126130
end

Diff for: lua/nvim-tree/actions/moves/item.lua

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ local core = require("nvim-tree.core")
44
local lib = require("nvim-tree.lib")
55
local diagnostics = require("nvim-tree.diagnostics")
66

7+
local DirectoryNode = require("nvim-tree.node.directory")
8+
79
local M = {}
810
local MAX_DEPTH = 100
911

@@ -70,11 +72,12 @@ local function move(where, what, skip_gitignored)
7072
end
7173
end
7274

75+
---@param node DirectoryNode
7376
local function expand_node(node)
7477
if not node.open then
7578
-- Expand the node.
7679
-- Should never collapse since we checked open.
77-
node:expand_or_collapse()
80+
node:expand_or_collapse(false)
7881
end
7982
end
8083

@@ -96,8 +99,9 @@ local function move_next_recursive(what, skip_gitignored)
9699
if node_init.name ~= ".." then -- root node cannot have a status
97100
valid = status_is_valid(node_init, what, skip_gitignored)
98101
end
99-
if node_init.nodes ~= nil and valid and not node_init.open then
100-
node_init:expand_or_collapse()
102+
local node_dir = node_init:as(DirectoryNode)
103+
if node_dir and valid and not node_dir.open then
104+
node_dir:expand_or_collapse(false)
101105
end
102106

103107
move("next", what, skip_gitignored)
@@ -114,20 +118,15 @@ local function move_next_recursive(what, skip_gitignored)
114118

115119
-- i is used to limit iterations.
116120
local i = 0
117-
local is_dir = node_cur.nodes ~= nil
118-
while is_dir and i < MAX_DEPTH do
119-
expand_node(node_cur)
121+
local dir_cur = node_cur:as(DirectoryNode)
122+
while dir_cur and i < MAX_DEPTH do
123+
expand_node(dir_cur)
120124

121125
move("next", what, skip_gitignored)
122126

123127
-- Save current node.
124128
node_cur = lib.get_node_at_cursor()
125-
-- Update is_dir.
126-
if node_cur then
127-
is_dir = node_cur.nodes ~= nil
128-
else
129-
is_dir = false
130-
end
129+
dir_cur = node_cur and node_cur:as(DirectoryNode)
131130

132131
i = i + 1
133132
end
@@ -180,8 +179,10 @@ local function move_prev_recursive(what, skip_gitignored)
180179
end
181180

182181
-- 4.2)
183-
local node_dir = node_cur
184-
expand_node(node_dir)
182+
local node_dir = node_cur:as(DirectoryNode)
183+
if node_dir then
184+
expand_node(node_dir)
185+
end
185186

186187
-- 4.3)
187188
if node_init.name == ".." then -- root node

Diff for: lua/nvim-tree/actions/moves/parent.lua

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local view = require("nvim-tree.view")
22
local utils = require("nvim-tree.utils")
3-
local core = require("nvim-tree.core")
3+
4+
local DirectoryNode = require("nvim-tree.node.directory")
45

56
local M = {}
67

@@ -9,33 +10,32 @@ local M = {}
910
function M.fn(should_close)
1011
should_close = should_close or false
1112

13+
---@param node Node
1214
return function(node)
13-
local explorer = core.get_explorer()
14-
node = node:last_group_node()
15-
if should_close and node.open then
16-
node.open = false
17-
if explorer then
18-
explorer.renderer:draw()
15+
local dir = node:as(DirectoryNode)
16+
if dir then
17+
dir = dir:last_group_node()
18+
if should_close and dir.open then
19+
dir.open = false
20+
dir.explorer.renderer:draw()
21+
return
1922
end
20-
return
2123
end
2224

23-
local parent = node:get_parent_of_group().parent
25+
local parent = (node:get_parent_of_group() or node).parent
2426

2527
if not parent or not parent.parent then
2628
return view.set_cursor({ 1, 0 })
2729
end
2830

29-
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
31+
local _, line = utils.find_node(parent.explorer.nodes, function(n)
3032
return n.absolute_path == parent.absolute_path
3133
end)
3234

3335
view.set_cursor({ line + 1, 0 })
3436
if should_close then
3537
parent.open = false
36-
if explorer then
37-
explorer.renderer:draw()
38-
end
38+
parent.explorer.renderer:draw()
3939
end
4040
end
4141
end

Diff for: lua/nvim-tree/actions/tree/modifiers/collapse-all.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ local core = require("nvim-tree.core")
33
local lib = require("nvim-tree.lib")
44
local Iterator = require("nvim-tree.iterators.node-iterator")
55

6+
local DirectoryNode = require("nvim-tree.node.directory")
7+
68
local M = {}
79

810
---@return fun(path: string): boolean
@@ -36,8 +38,9 @@ function M.fn(keep_buffers)
3638
Iterator.builder(explorer.nodes)
3739
:hidden()
3840
:applier(function(n)
39-
if n.nodes ~= nil then
40-
n.open = keep_buffers == true and matches(n.absolute_path)
41+
local dir = n:as(DirectoryNode)
42+
if dir then
43+
dir.open = keep_buffers and matches(dir.absolute_path)
4144
end
4245
end)
4346
:recursor(function(n)

Diff for: lua/nvim-tree/actions/tree/modifiers/expand-all.lua

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local core = require("nvim-tree.core")
22
local Iterator = require("nvim-tree.iterators.node-iterator")
33
local notify = require("nvim-tree.notify")
44

5+
local DirectoryNode = require("nvim-tree.node.directory")
6+
57
local M = {}
68

79
---@param list string[]
@@ -15,7 +17,7 @@ local function to_lookup_table(list)
1517
return table
1618
end
1719

18-
---@param node Node
20+
---@param node DirectoryNode
1921
local function expand(node)
2022
node = node:last_group_node()
2123
node.open = true
@@ -36,6 +38,7 @@ end
3638
local function gen_iterator()
3739
local expansion_count = 0
3840

41+
---@param parent DirectoryNode
3942
return function(parent)
4043
if parent.parent and parent.nodes and not parent.open then
4144
expansion_count = expansion_count + 1
@@ -44,12 +47,14 @@ local function gen_iterator()
4447

4548
Iterator.builder(parent.nodes)
4649
:hidden()
50+
---@param node DirectoryNode
4751
:applier(function(node)
4852
if should_expand(expansion_count, node) then
4953
expansion_count = expansion_count + 1
5054
expand(node)
5155
end
5256
end)
57+
---@param node DirectoryNode
5358
:recursor(function(node)
5459
return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes))
5560
end)
@@ -61,11 +66,16 @@ local function gen_iterator()
6166
end
6267
end
6368

69+
---Expand the directory node or the root
6470
---@param node Node
6571
function M.fn(node)
6672
local explorer = core.get_explorer()
67-
node = node.nodes and node or explorer
68-
if gen_iterator()(node) then
73+
local parent = node:as(DirectoryNode) or explorer
74+
if not parent then
75+
return
76+
end
77+
78+
if gen_iterator()(parent) then
6979
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
7080
end
7181
if explorer then

Diff for: lua/nvim-tree/api.lua

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ local help = require("nvim-tree.help")
99
local keymap = require("nvim-tree.keymap")
1010
local notify = require("nvim-tree.notify")
1111

12+
local DirectoryNode = require("nvim-tree.node.directory")
13+
local RootNode = require("nvim-tree.node.root")
14+
1215
local Api = {
1316
tree = {},
1417
node = {
@@ -135,9 +138,9 @@ Api.tree.change_root = wrap(function(...)
135138
end)
136139

137140
Api.tree.change_root_to_node = wrap_node(function(node)
138-
if node.name == ".." then
141+
if node.name == ".." or node:is(RootNode) then
139142
actions.root.change_dir.fn("..")
140-
elseif node.nodes ~= nil then
143+
elseif node:is(DirectoryNode) then
141144
actions.root.change_dir.fn(node:last_group_node().absolute_path)
142145
end
143146
end)
@@ -208,12 +211,13 @@ local function edit(mode, node)
208211
end
209212

210213
---@param mode string
211-
---@return fun(node: table)
214+
---@return fun(node: Node)
212215
local function open_or_expand_or_dir_up(mode, toggle_group)
216+
---@param node Node
213217
return function(node)
214218
if node.name == ".." then
215219
actions.root.change_dir.fn("..")
216-
elseif node.nodes then
220+
elseif node:is(DirectoryNode) then
217221
node:expand_or_collapse(toggle_group)
218222
elseif not toggle_group then
219223
edit(mode, node)

0 commit comments

Comments
 (0)