Skip to content

Commit 8331a24

Browse files
authored
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
1 parent fb2070d commit 8331a24

22 files changed

+284
-258
lines changed

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)

lua/nvim-tree/actions/finders/find-file.lua

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function M.fn(path)
4343
return node.absolute_path == path_real or node.link_to == path_real
4444
end)
4545
:applier(function(node)
46+
---@cast node DirectoryNode
4647
local incremented_line = false
4748
if not node.group_next then
4849
line = line + 1

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]

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

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

lua/nvim-tree/actions/moves/item.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ local function expand_node(node)
7878
---@cast node DirectoryNode
7979
-- Expand the node.
8080
-- Should never collapse since we checked open.
81-
node:expand_or_collapse()
81+
node:expand_or_collapse(false)
8282
end
8383
end
8484

@@ -102,7 +102,7 @@ local function move_next_recursive(what, skip_gitignored)
102102
end
103103
if node_init:is(DirectoryNode) and valid and not node_init.open then
104104
---@cast node_init DirectoryNode
105-
node_init:expand_or_collapse()
105+
node_init:expand_or_collapse(false)
106106
end
107107

108108
move("next", what, skip_gitignored)

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

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)

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

lua/nvim-tree/api.lua

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local keymap = require("nvim-tree.keymap")
1010
local notify = require("nvim-tree.notify")
1111

1212
local DirectoryNode = require("nvim-tree.node.directory")
13+
local RootNode = require("nvim-tree.node.root")
1314

1415
local Api = {
1516
tree = {},
@@ -137,9 +138,9 @@ Api.tree.change_root = wrap(function(...)
137138
end)
138139

139140
Api.tree.change_root_to_node = wrap_node(function(node)
140-
if node.name == ".." then
141+
if node.name == ".." or node:is(RootNode) then
141142
actions.root.change_dir.fn("..")
142-
elseif node.nodes ~= nil then
143+
elseif node:is(DirectoryNode) then
143144
actions.root.change_dir.fn(node:last_group_node().absolute_path)
144145
end
145146
end)
@@ -210,13 +211,13 @@ local function edit(mode, node)
210211
end
211212

212213
---@param mode string
213-
---@return fun(node: table)
214+
---@return fun(node: Node)
214215
local function open_or_expand_or_dir_up(mode, toggle_group)
216+
---@param node Node
215217
return function(node)
216218
if node.name == ".." then
217219
actions.root.change_dir.fn("..")
218220
elseif node:is(DirectoryNode) then
219-
---@cast node DirectoryNode
220221
node:expand_or_collapse(toggle_group)
221222
elseif not toggle_group then
222223
edit(mode, node)

lua/nvim-tree/class.lua

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---Generic class, useful for inheritence.
2+
---@class (exact) Class
3+
---@field private __index? table
4+
local Class = {}
5+
6+
---@param o Class?
7+
---@return Class
8+
function Class:new(o)
9+
o = o or {}
10+
11+
setmetatable(o, self)
12+
self.__index = self
13+
14+
return o
15+
end
16+
17+
---Object is an instance of class
18+
---This will start with the lowest class and loop over all the superclasses.
19+
---@param class table
20+
---@return boolean
21+
function Class:is(class)
22+
local mt = getmetatable(self)
23+
while mt do
24+
if mt == class then
25+
return true
26+
end
27+
mt = getmetatable(mt)
28+
end
29+
return false
30+
end
31+
32+
---Return object if it is an instance of class, otherwise nil
33+
---@generic T
34+
---@param class T
35+
---@return `T`|nil
36+
function Class:as(class)
37+
return self:is(class) and self or nil
38+
end
39+
40+
return Class

0 commit comments

Comments
 (0)