Skip to content

Commit f3efc25

Browse files
authored
refactor(#2941): move lib methods to explorer (#2964)
* 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 * move lib.get_cursor_position to Explorer * move lib.get_node_at_cursor to Explorer * move lib.get_nodes to Explorer * move place_cursor_on_node to Explorer * resolve resource leak in purge_all_state * move many autocommands into Explorer * post merge tidy
1 parent 8760d76 commit f3efc25

File tree

15 files changed

+250
-237
lines changed

15 files changed

+250
-237
lines changed

Diff for: lua/nvim-tree.lua

+6-133
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
local lib = require("nvim-tree.lib")
21
local log = require("nvim-tree.log")
3-
local appearance = require("nvim-tree.appearance")
42
local view = require("nvim-tree.view")
53
local utils = require("nvim-tree.utils")
64
local actions = require("nvim-tree.actions")
@@ -115,27 +113,6 @@ function M.open_on_directory()
115113
actions.root.change_dir.force_dirchange(bufname, true)
116114
end
117115

118-
function M.place_cursor_on_node()
119-
local ok, search = pcall(vim.fn.searchcount)
120-
if ok and search and search.exact_match == 1 then
121-
return
122-
end
123-
124-
local node = lib.get_node_at_cursor()
125-
if not node or node.name == ".." then
126-
return
127-
end
128-
node = node:get_parent_of_group() or node
129-
130-
local line = vim.api.nvim_get_current_line()
131-
local cursor = vim.api.nvim_win_get_cursor(0)
132-
local idx = vim.fn.stridx(line, node.name)
133-
134-
if idx >= 0 then
135-
vim.api.nvim_win_set_cursor(0, { cursor[1], idx })
136-
end
137-
end
138-
139116
---@return table
140117
function M.get_config()
141118
return M.config
@@ -173,19 +150,6 @@ local function setup_autocommands(opts)
173150
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
174151
end
175152

176-
-- reset and draw (highlights) when colorscheme is changed
177-
create_nvim_tree_autocmd("ColorScheme", {
178-
callback = function()
179-
appearance.setup()
180-
view.reset_winhl()
181-
182-
local explorer = core.get_explorer()
183-
if explorer then
184-
explorer.renderer:draw()
185-
end
186-
end,
187-
})
188-
189153
-- prevent new opened file from opening in the same window as nvim-tree
190154
create_nvim_tree_autocmd("BufWipeout", {
191155
pattern = "NvimTree_*",
@@ -201,76 +165,9 @@ local function setup_autocommands(opts)
201165
end,
202166
})
203167

204-
create_nvim_tree_autocmd("BufWritePost", {
205-
callback = function()
206-
if opts.auto_reload_on_write and not opts.filesystem_watchers.enable then
207-
local explorer = core.get_explorer()
208-
if explorer then
209-
explorer:reload_explorer()
210-
end
211-
end
212-
end,
213-
})
214-
215-
create_nvim_tree_autocmd("BufReadPost", {
216-
callback = function(data)
217-
-- update opened file buffers
218-
local explorer = core.get_explorer()
219-
if not explorer then
220-
return
221-
end
222-
if
223-
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
224-
then
225-
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
226-
explorer:reload_explorer()
227-
end)
228-
end
229-
end,
230-
})
231-
232-
create_nvim_tree_autocmd("BufUnload", {
233-
callback = function(data)
234-
-- update opened file buffers
235-
local explorer = core.get_explorer()
236-
if not explorer then
237-
return
238-
end
239-
if
240-
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
241-
then
242-
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
243-
explorer:reload_explorer()
244-
end)
245-
end
246-
end,
247-
})
248-
249-
create_nvim_tree_autocmd("User", {
250-
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
251-
callback = function()
252-
if not opts.filesystem_watchers.enable and opts.git.enable then
253-
local explorer = core.get_explorer()
254-
if explorer then
255-
explorer:reload_git()
256-
end
257-
end
258-
end,
259-
})
260-
261168
if opts.tab.sync.open then
262169
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) })
263170
end
264-
if opts.hijack_cursor then
265-
create_nvim_tree_autocmd("CursorMoved", {
266-
pattern = "NvimTree_*",
267-
callback = function()
268-
if utils.is_nvim_tree_buf(0) then
269-
M.place_cursor_on_node()
270-
end
271-
end,
272-
})
273-
end
274171
if opts.sync_root_with_cwd then
275172
create_nvim_tree_autocmd("DirChanged", {
276173
callback = function()
@@ -296,20 +193,6 @@ local function setup_autocommands(opts)
296193
create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory })
297194
end
298195

299-
create_nvim_tree_autocmd("BufEnter", {
300-
pattern = "NvimTree_*",
301-
callback = function()
302-
if utils.is_nvim_tree_buf(0) then
303-
if vim.fn.getcwd() ~= core.get_cwd() or (opts.reload_on_bufenter and not opts.filesystem_watchers.enable) then
304-
local explorer = core.get_explorer()
305-
if explorer then
306-
explorer:reload_explorer()
307-
end
308-
end
309-
end
310-
end,
311-
})
312-
313196
if opts.view.centralize_selection then
314197
create_nvim_tree_autocmd("BufEnter", {
315198
pattern = "NvimTree_*",
@@ -349,20 +232,6 @@ local function setup_autocommands(opts)
349232
end,
350233
})
351234
end
352-
353-
if opts.modified.enable then
354-
create_nvim_tree_autocmd({ "BufModifiedSet", "BufWritePost" }, {
355-
callback = function()
356-
utils.debounce("Buf:modified", opts.view.debounce_delay, function()
357-
require("nvim-tree.buffers").reload_modified()
358-
local explorer = core.get_explorer()
359-
if explorer then
360-
explorer:reload_explorer()
361-
end
362-
end)
363-
end,
364-
})
365-
end
366235
end
367236

368237
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
@@ -805,13 +674,16 @@ local function localise_default_opts()
805674
end
806675

807676
function M.purge_all_state()
808-
require("nvim-tree.watcher").purge_watchers()
809677
view.close_all_tabs()
810678
view.abandon_all_windows()
811-
if core.get_explorer() ~= nil then
679+
local explorer = core.get_explorer()
680+
if explorer then
812681
require("nvim-tree.git").purge_state()
682+
explorer:destroy()
813683
core.reset_explorer()
814684
end
685+
-- purge orphaned that were not destroyed by their nodes
686+
require("nvim-tree.watcher").purge_watchers()
815687
end
816688

817689
---@param conf table|nil
@@ -855,6 +727,7 @@ function M.setup(conf)
855727
require("nvim-tree.appearance").setup()
856728
require("nvim-tree.diagnostics").setup(opts)
857729
require("nvim-tree.explorer"):setup(opts)
730+
require("nvim-tree.explorer.watch").setup(opts)
858731
require("nvim-tree.git").setup(opts)
859732
require("nvim-tree.git.utils").setup(opts)
860733
require("nvim-tree.view").setup(opts)

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local core = require("nvim-tree.core")
2-
local lib = require("nvim-tree.lib")
32
local utils = require("nvim-tree.utils")
43
local events = require("nvim-tree.events")
54
local notify = require("nvim-tree.notify")
@@ -104,11 +103,15 @@ function M.fn(default_modifier)
104103
default_modifier = default_modifier or ":t"
105104

106105
return function(node, modifier)
107-
if type(node) ~= "table" then
108-
node = lib.get_node_at_cursor()
106+
local explorer = core.get_explorer()
107+
if not explorer then
108+
return
109109
end
110110

111-
if node == nil then
111+
if type(node) ~= "table" then
112+
node = explorer:get_node_at_cursor()
113+
end
114+
if not node then
112115
return
113116
end
114117

@@ -160,10 +163,7 @@ function M.fn(default_modifier)
160163

161164
M.rename(node, prepend .. new_file_path .. append)
162165
if not M.config.filesystem_watchers.enable then
163-
local explorer = core.get_explorer()
164-
if explorer then
165-
explorer:reload_explorer()
166-
end
166+
explorer:reload_explorer()
167167
end
168168

169169
find_file(utils.path_remove_trailing(new_file_path))

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

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local utils = require("nvim-tree.utils")
22
local view = require("nvim-tree.view")
33
local core = require("nvim-tree.core")
4-
local lib = require("nvim-tree.lib")
54
local diagnostics = require("nvim-tree.diagnostics")
65

76
local DirectoryNode = require("nvim-tree.node.directory")
@@ -30,15 +29,16 @@ local function status_is_valid(node, what, skip_gitignored)
3029
end
3130

3231
---Move to the next node that has a valid status. If none found, don't move.
32+
---@param explorer Explorer
3333
---@param where string where to move (forwards or backwards)
3434
---@param what string type of status
3535
---@param skip_gitignored boolean default false
36-
local function move(where, what, skip_gitignored)
36+
local function move(explorer, where, what, skip_gitignored)
3737
local first_node_line = core.get_nodes_starting_line()
38-
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, first_node_line)
38+
local nodes_by_line = utils.get_nodes_by_line(explorer.nodes, first_node_line)
3939
local iter_start, iter_end, iter_step, cur, first, nex
4040

41-
local cursor = lib.get_cursor_position()
41+
local cursor = explorer:get_cursor_position()
4242
if cursor and cursor[1] < first_node_line then
4343
cur = cursor[1]
4444
end
@@ -82,16 +82,17 @@ local function expand_node(node)
8282
end
8383

8484
--- Move to the next node recursively.
85+
---@param explorer Explorer
8586
---@param what string type of status
8687
---@param skip_gitignored boolean default false
87-
local function move_next_recursive(what, skip_gitignored)
88+
local function move_next_recursive(explorer, what, skip_gitignored)
8889
-- If the current node:
8990
-- * is a directory
9091
-- * and is not the root node
9192
-- * and has a git/diag status
9293
-- * and is not opened
9394
-- expand it.
94-
local node_init = lib.get_node_at_cursor()
95+
local node_init = explorer:get_node_at_cursor()
9596
if not node_init then
9697
return
9798
end
@@ -104,9 +105,9 @@ local function move_next_recursive(what, skip_gitignored)
104105
node_dir:expand_or_collapse(false)
105106
end
106107

107-
move("next", what, skip_gitignored)
108+
move(explorer, "next", what, skip_gitignored)
108109

109-
local node_cur = lib.get_node_at_cursor()
110+
local node_cur = explorer:get_node_at_cursor()
110111
if not node_cur then
111112
return
112113
end
@@ -122,10 +123,10 @@ local function move_next_recursive(what, skip_gitignored)
122123
while dir_cur and i < MAX_DEPTH do
123124
expand_node(dir_cur)
124125

125-
move("next", what, skip_gitignored)
126+
move(explorer, "next", what, skip_gitignored)
126127

127128
-- Save current node.
128-
node_cur = lib.get_node_at_cursor()
129+
node_cur = explorer:get_node_at_cursor()
129130
dir_cur = node_cur and node_cur:as(DirectoryNode)
130131

131132
i = i + 1
@@ -147,24 +148,25 @@ end
147148
--- 4.4) Call a non-recursive prev.
148149
--- 4.5) Save the current node and start back from 4.1.
149150
---
151+
---@param explorer Explorer
150152
---@param what string type of status
151153
---@param skip_gitignored boolean default false
152-
local function move_prev_recursive(what, skip_gitignored)
154+
local function move_prev_recursive(explorer, what, skip_gitignored)
153155
local node_init, node_cur
154156

155157
-- 1)
156-
node_init = lib.get_node_at_cursor()
158+
node_init = explorer:get_node_at_cursor()
157159
if node_init == nil then
158160
return
159161
end
160162

161163
-- 2)
162-
move("prev", what, skip_gitignored)
164+
move(explorer, "prev", what, skip_gitignored)
163165

164-
node_cur = lib.get_node_at_cursor()
166+
node_cur = explorer:get_node_at_cursor()
165167
if node_cur == node_init.parent then
166168
-- 3)
167-
move_prev_recursive(what, skip_gitignored)
169+
move_prev_recursive(explorer, what, skip_gitignored)
168170
else
169171
-- i is used to limit iterations.
170172
local i = 0
@@ -196,10 +198,10 @@ local function move_prev_recursive(what, skip_gitignored)
196198
end
197199

198200
-- 4.4)
199-
move("prev", what, skip_gitignored)
201+
move(explorer, "prev", what, skip_gitignored)
200202

201203
-- 4.5)
202-
node_cur = lib.get_node_at_cursor()
204+
node_cur = explorer:get_node_at_cursor()
203205

204206
i = i + 1
205207
end
@@ -214,6 +216,11 @@ end
214216
---@return fun()
215217
function M.fn(opts)
216218
return function()
219+
local explorer = core.get_explorer()
220+
if not explorer then
221+
return
222+
end
223+
217224
local recurse = false
218225
local skip_gitignored = false
219226

@@ -227,14 +234,14 @@ function M.fn(opts)
227234
end
228235

229236
if not recurse then
230-
move(opts.where, opts.what, skip_gitignored)
237+
move(explorer, opts.where, opts.what, skip_gitignored)
231238
return
232239
end
233240

234241
if opts.where == "next" then
235-
move_next_recursive(opts.what, skip_gitignored)
242+
move_next_recursive(explorer, opts.what, skip_gitignored)
236243
elseif opts.where == "prev" then
237-
move_prev_recursive(opts.what, skip_gitignored)
244+
move_prev_recursive(explorer, opts.what, skip_gitignored)
238245
end
239246
end
240247
end

0 commit comments

Comments
 (0)