Skip to content

Commit cd9c6db

Browse files
authored
refactor(#2882, #2883): multi instance explore, reloaders (#2897)
* refactor(#2883): multi instance explore * refactor(#2882): multi instance reloaders * style
1 parent 03f737e commit cd9c6db

File tree

12 files changed

+247
-115
lines changed

12 files changed

+247
-115
lines changed

lua/nvim-tree.lua

+18-6
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ local function setup_autocommands(opts)
201201
create_nvim_tree_autocmd("BufWritePost", {
202202
callback = function()
203203
if opts.auto_reload_on_write and not opts.filesystem_watchers.enable then
204-
actions.reloaders.reload_explorer()
204+
local explorer = core.get_explorer()
205+
if explorer then
206+
explorer:reload_explorer()
207+
end
205208
end
206209
end,
207210
})
@@ -217,7 +220,7 @@ local function setup_autocommands(opts)
217220
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
218221
then
219222
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
220-
actions.reloaders.reload_explorer()
223+
explorer:reload_explorer()
221224
end)
222225
end
223226
end,
@@ -234,7 +237,7 @@ local function setup_autocommands(opts)
234237
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
235238
then
236239
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
237-
actions.reloaders.reload_explorer()
240+
explorer:reload_explorer()
238241
end)
239242
end
240243
end,
@@ -244,7 +247,10 @@ local function setup_autocommands(opts)
244247
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
245248
callback = function()
246249
if not opts.filesystem_watchers.enable and opts.git.enable then
247-
actions.reloaders.reload_git()
250+
local explorer = core.get_explorer()
251+
if explorer then
252+
explorer:reload_git()
253+
end
248254
end
249255
end,
250256
})
@@ -292,7 +298,10 @@ local function setup_autocommands(opts)
292298
callback = function()
293299
if utils.is_nvim_tree_buf(0) then
294300
if vim.fn.getcwd() ~= core.get_cwd() or (opts.reload_on_bufenter and not opts.filesystem_watchers.enable) then
295-
actions.reloaders.reload_explorer()
301+
local explorer = core.get_explorer()
302+
if explorer then
303+
explorer:reload_explorer()
304+
end
296305
end
297306
end
298307
end,
@@ -343,7 +352,10 @@ local function setup_autocommands(opts)
343352
callback = function()
344353
utils.debounce("Buf:modified", opts.view.debounce_delay, function()
345354
buffers.reload_modified()
346-
actions.reloaders.reload_explorer()
355+
local explorer = core.get_explorer()
356+
if explorer then
357+
explorer:reload_explorer()
358+
end
347359
end)
348360
end,
349361
})

lua/nvim-tree/actions/fs/clipboard.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ local core = require "nvim-tree.core"
55
local events = require "nvim-tree.events"
66
local notify = require "nvim-tree.notify"
77
local renderer = require "nvim-tree.renderer"
8-
local reloaders = require "nvim-tree.actions.reloaders"
98

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

@@ -248,7 +247,7 @@ function Clipboard:do_paste(node, action, action_fn)
248247

249248
self.data[action] = {}
250249
if not self.config.filesystem_watchers.enable then
251-
reloaders.reload_explorer()
250+
self.explorer:reload_explorer()
252251
end
253252
end
254253

lua/nvim-tree/actions/fs/remove-file.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local core = require "nvim-tree.core"
12
local utils = require "nvim-tree.utils"
23
local events = require "nvim-tree.events"
34
local view = require "nvim-tree.view"
@@ -116,8 +117,9 @@ function M.fn(node)
116117

117118
local function do_remove()
118119
M.remove(node)
119-
if not M.config.filesystem_watchers.enable then
120-
require("nvim-tree.actions.reloaders").reload_explorer()
120+
local explorer = core.get_explorer()
121+
if not M.config.filesystem_watchers.enable and explorer then
122+
explorer:reload_explorer()
121123
end
122124
end
123125

lua/nvim-tree/actions/fs/rename-file.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local core = require "nvim-tree.core"
12
local lib = require "nvim-tree.lib"
23
local utils = require "nvim-tree.utils"
34
local events = require "nvim-tree.events"
@@ -155,7 +156,10 @@ function M.fn(default_modifier)
155156

156157
M.rename(node, prepend .. new_file_path .. append)
157158
if not M.config.filesystem_watchers.enable then
158-
require("nvim-tree.actions.reloaders").reload_explorer()
159+
local explorer = core.get_explorer()
160+
if explorer then
161+
explorer:reload_explorer()
162+
end
159163
end
160164

161165
find_file(utils.path_remove_trailing(new_file_path))

lua/nvim-tree/actions/fs/trash.lua

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
local core = require "nvim-tree.core"
12
local lib = require "nvim-tree.lib"
23
local notify = require "nvim-tree.notify"
3-
local reloaders = require "nvim-tree.actions.reloaders"
44

55
local M = {
66
config = {},
@@ -52,15 +52,17 @@ function M.remove(node)
5252
end
5353
end
5454

55+
local explorer = core.get_explorer()
56+
5557
if node.nodes ~= nil and not node.link_to then
5658
trash_path(function(_, rc)
5759
if rc ~= 0 then
5860
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
5961
return
6062
end
6163
events._dispatch_folder_removed(node.absolute_path)
62-
if not M.config.filesystem_watchers.enable then
63-
reloaders.reload_explorer()
64+
if not M.config.filesystem_watchers.enable and explorer then
65+
explorer:reload_explorer()
6466
end
6567
end)
6668
else
@@ -72,8 +74,8 @@ function M.remove(node)
7274
end
7375
events._dispatch_file_removed(node.absolute_path)
7476
clear_buffer(node.absolute_path)
75-
if not M.config.filesystem_watchers.enable then
76-
reloaders.reload_explorer()
77+
if not M.config.filesystem_watchers.enable and explorer then
78+
explorer:reload_explorer()
7779
end
7880
end)
7981
end

lua/nvim-tree/actions/init.lua

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ M.finders = require "nvim-tree.actions.finders"
44
M.fs = require "nvim-tree.actions.fs"
55
M.moves = require "nvim-tree.actions.moves"
66
M.node = require "nvim-tree.actions.node"
7-
M.reloaders = require "nvim-tree.actions.reloaders"
87
M.root = require "nvim-tree.actions.root"
98
M.tree = require "nvim-tree.actions.tree"
109

lua/nvim-tree/actions/reloaders.lua

-72
This file was deleted.

lua/nvim-tree/actions/tree/modifiers/toggles.lua

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
local lib = require "nvim-tree.lib"
22
local utils = require "nvim-tree.utils"
3-
local reloaders = require "nvim-tree.actions.reloaders"
43
local core = require "nvim-tree.core"
54
local M = {}
65

7-
local function reload()
6+
---@param explorer Explorer
7+
local function reload(explorer)
88
local node = lib.get_node_at_cursor()
9-
reloaders.reload_explorer()
9+
explorer:reload_explorer()
1010
utils.focus_node_or_parent(node)
1111
end
1212

@@ -19,39 +19,46 @@ local function wrap_explorer(fn)
1919
end
2020
end
2121

22+
---@param explorer Explorer
2223
local function custom(explorer)
2324
explorer.filters.config.filter_custom = not explorer.filters.config.filter_custom
24-
reload()
25+
reload(explorer)
2526
end
2627

28+
---@param explorer Explorer
2729
local function git_ignored(explorer)
2830
explorer.filters.config.filter_git_ignored = not explorer.filters.config.filter_git_ignored
29-
reload()
31+
reload(explorer)
3032
end
3133

34+
---@param explorer Explorer
3235
local function git_clean(explorer)
3336
explorer.filters.config.filter_git_clean = not explorer.filters.config.filter_git_clean
34-
reload()
37+
reload(explorer)
3538
end
3639

40+
---@param explorer Explorer
3741
local function no_buffer(explorer)
3842
explorer.filters.config.filter_no_buffer = not explorer.filters.config.filter_no_buffer
39-
reload()
43+
reload(explorer)
4044
end
4145

46+
---@param explorer Explorer
4247
local function no_bookmark(explorer)
4348
explorer.filters.config.filter_no_bookmark = not explorer.filters.config.filter_no_bookmark
44-
reload()
49+
reload(explorer)
4550
end
4651

52+
---@param explorer Explorer
4753
local function dotfiles(explorer)
4854
explorer.filters.config.filter_dotfiles = not explorer.filters.config.filter_dotfiles
49-
reload()
55+
reload(explorer)
5056
end
5157

58+
---@param explorer Explorer
5259
local function enable(explorer)
5360
explorer.filters.config.enable = not explorer.filters.config.enable
54-
reload()
61+
reload(explorer)
5562
end
5663

5764
M.custom = wrap_explorer(custom)

lua/nvim-tree/api.lua

+15-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ local function wrap_node_or_nil(fn)
7272
end
7373
end
7474

75+
---Invoke a method on the singleton explorer.
76+
---Print error when setup not called.
77+
---@param explorer_method string explorer method name
78+
---@return fun(...) : any
79+
local function wrap_explorer(explorer_method)
80+
return wrap(function(...)
81+
local explorer = core.get_explorer()
82+
if explorer then
83+
return explorer[explorer_method](explorer, ...)
84+
end
85+
end)
86+
end
87+
7588
---Invoke a member's method on the singleton explorer.
7689
---Print error when setup not called.
7790
---@param explorer_member string explorer member name
@@ -108,7 +121,7 @@ Api.tree.toggle = wrap(actions.tree.toggle.fn)
108121
Api.tree.close = wrap(view.close)
109122
Api.tree.close_in_this_tab = wrap(view.close_this_tab_only)
110123
Api.tree.close_in_all_tabs = wrap(view.close_all_tabs)
111-
Api.tree.reload = wrap(actions.reloaders.reload_explorer)
124+
Api.tree.reload = wrap_explorer "reload_explorer"
112125

113126
---@class ApiTreeResizeOpts
114127
---@field width string|function|number|table|nil
@@ -243,7 +256,7 @@ Api.node.navigate.diagnostics.prev_recursive = wrap_node(actions.moves.item.fn {
243256
Api.node.navigate.opened.next = wrap_node(actions.moves.item.fn { where = "next", what = "opened" })
244257
Api.node.navigate.opened.prev = wrap_node(actions.moves.item.fn { where = "prev", what = "opened" })
245258

246-
Api.git.reload = wrap(actions.reloaders.reload_git)
259+
Api.git.reload = wrap_explorer "reload_git"
247260

248261
Api.events.subscribe = events.subscribe
249262
Api.events.Event = events.Event

0 commit comments

Comments
 (0)