Skip to content

Commit ff85d80

Browse files
committed
move many autocommands into Explorer
1 parent e2e6b2b commit ff85d80

File tree

6 files changed

+124
-113
lines changed

6 files changed

+124
-113
lines changed

lua/nvim-tree.lua

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local log = require("nvim-tree.log")
2-
local appearance = require("nvim-tree.appearance")
32
local view = require("nvim-tree.view")
43
local utils = require("nvim-tree.utils")
54
local actions = require("nvim-tree.actions")
@@ -151,19 +150,6 @@ local function setup_autocommands(opts)
151150
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
152151
end
153152

154-
-- reset and draw (highlights) when colorscheme is changed
155-
create_nvim_tree_autocmd("ColorScheme", {
156-
callback = function()
157-
appearance.setup()
158-
view.reset_winhl()
159-
160-
local explorer = core.get_explorer()
161-
if explorer then
162-
explorer.renderer:draw()
163-
end
164-
end,
165-
})
166-
167153
-- prevent new opened file from opening in the same window as nvim-tree
168154
create_nvim_tree_autocmd("BufWipeout", {
169155
pattern = "NvimTree_*",
@@ -179,79 +165,9 @@ local function setup_autocommands(opts)
179165
end,
180166
})
181167

182-
create_nvim_tree_autocmd("BufWritePost", {
183-
callback = function()
184-
if opts.auto_reload_on_write and not opts.filesystem_watchers.enable then
185-
local explorer = core.get_explorer()
186-
if explorer then
187-
explorer:reload_explorer()
188-
end
189-
end
190-
end,
191-
})
192-
193-
create_nvim_tree_autocmd("BufReadPost", {
194-
callback = function(data)
195-
-- update opened file buffers
196-
local explorer = core.get_explorer()
197-
if not explorer then
198-
return
199-
end
200-
if
201-
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
202-
then
203-
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
204-
explorer:reload_explorer()
205-
end)
206-
end
207-
end,
208-
})
209-
210-
create_nvim_tree_autocmd("BufUnload", {
211-
callback = function(data)
212-
-- update opened file buffers
213-
local explorer = core.get_explorer()
214-
if not explorer then
215-
return
216-
end
217-
if
218-
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
219-
then
220-
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
221-
explorer:reload_explorer()
222-
end)
223-
end
224-
end,
225-
})
226-
227-
create_nvim_tree_autocmd("User", {
228-
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
229-
callback = function()
230-
if not opts.filesystem_watchers.enable and opts.git.enable then
231-
local explorer = core.get_explorer()
232-
if explorer then
233-
explorer:reload_git()
234-
end
235-
end
236-
end,
237-
})
238-
239168
if opts.tab.sync.open then
240169
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) })
241170
end
242-
if opts.hijack_cursor then
243-
create_nvim_tree_autocmd("CursorMoved", {
244-
pattern = "NvimTree_*",
245-
callback = function()
246-
if utils.is_nvim_tree_buf(0) then
247-
local explorer = core.get_explorer()
248-
if explorer then
249-
explorer:place_cursor_on_node()
250-
end
251-
end
252-
end,
253-
})
254-
end
255171
if opts.sync_root_with_cwd then
256172
create_nvim_tree_autocmd("DirChanged", {
257173
callback = function()
@@ -277,20 +193,6 @@ local function setup_autocommands(opts)
277193
create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory })
278194
end
279195

280-
create_nvim_tree_autocmd("BufEnter", {
281-
pattern = "NvimTree_*",
282-
callback = function()
283-
if utils.is_nvim_tree_buf(0) then
284-
if vim.fn.getcwd() ~= core.get_cwd() or (opts.reload_on_bufenter and not opts.filesystem_watchers.enable) then
285-
local explorer = core.get_explorer()
286-
if explorer then
287-
explorer:reload_explorer()
288-
end
289-
end
290-
end
291-
end,
292-
})
293-
294196
if opts.view.centralize_selection then
295197
create_nvim_tree_autocmd("BufEnter", {
296198
pattern = "NvimTree_*",
@@ -330,20 +232,6 @@ local function setup_autocommands(opts)
330232
end,
331233
})
332234
end
333-
334-
if opts.modified.enable then
335-
create_nvim_tree_autocmd({ "BufModifiedSet", "BufWritePost" }, {
336-
callback = function()
337-
utils.debounce("Buf:modified", opts.view.debounce_delay, function()
338-
require("nvim-tree.buffers").reload_modified()
339-
local explorer = core.get_explorer()
340-
if explorer then
341-
explorer:reload_explorer()
342-
end
343-
end)
344-
end,
345-
})
346-
end
347235
end
348236

349237
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
@@ -839,6 +727,7 @@ function M.setup(conf)
839727
require("nvim-tree.appearance").setup()
840728
require("nvim-tree.diagnostics").setup(opts)
841729
require("nvim-tree.explorer"):setup(opts)
730+
require("nvim-tree.explorer.watch").setup(opts)
842731
require("nvim-tree.git").setup(opts)
843732
require("nvim-tree.git.utils").setup(opts)
844733
require("nvim-tree.view").setup(opts)

lua/nvim-tree/explorer/init.lua

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local appearance = require("nvim-tree.appearance")
2+
local buffers = require("nvim-tree.buffers")
13
local core = require("nvim-tree.core")
24
local git = require("nvim-tree.git")
35
local log = require("nvim-tree.log")
@@ -25,7 +27,9 @@ local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
2527
local config
2628

2729
---@class (exact) Explorer: RootNode
30+
---@field uid_explorer number vim.uv.hrtime() at construction time
2831
---@field opts table user options
32+
---@field augroup_id integer
2933
---@field renderer Renderer
3034
---@field filters Filters
3135
---@field live_filter LiveFilter
@@ -59,6 +63,9 @@ function Explorer:create(path)
5963

6064
o.explorer = o
6165

66+
o.uid_explorer = vim.uv.hrtime()
67+
o.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. o.uid_explorer, {})
68+
6269
o.open = true
6370
o.opts = config
6471

@@ -69,11 +76,111 @@ function Explorer:create(path)
6976
o.marks = Marks:new(config, o)
7077
o.clipboard = Clipboard:new(config, o)
7178

79+
o:create_autocmds()
80+
7281
o:_load(o)
7382

7483
return o
7584
end
7685

86+
function Explorer:destroy()
87+
log.line("dev", "Explorer:destroy")
88+
89+
vim.api.nvim_del_augroup_by_id(self.augroup_id)
90+
91+
RootNode.destroy(self)
92+
end
93+
94+
function Explorer:create_autocmds()
95+
-- reset and draw (highlights) when colorscheme is changed
96+
vim.api.nvim_create_autocmd("ColorScheme", {
97+
group = self.augroup_id,
98+
callback = function()
99+
appearance.setup()
100+
view.reset_winhl()
101+
self:draw()
102+
end,
103+
})
104+
105+
vim.api.nvim_create_autocmd("BufWritePost", {
106+
group = self.augroup_id,
107+
callback = function()
108+
if self.opts.auto_reload_on_write and not self.opts.filesystem_watchers.enable then
109+
self:reload_explorer()
110+
end
111+
end,
112+
})
113+
114+
vim.api.nvim_create_autocmd("BufReadPost", {
115+
group = self.augroup_id,
116+
callback = function(data)
117+
if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
118+
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
119+
self:reload_explorer()
120+
end)
121+
end
122+
end,
123+
})
124+
125+
-- update opened file buffers
126+
vim.api.nvim_create_autocmd("BufUnload", {
127+
group = self.augroup_id,
128+
callback = function(data)
129+
if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
130+
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
131+
self:reload_explorer()
132+
end)
133+
end
134+
end,
135+
})
136+
137+
vim.api.nvim_create_autocmd("BufEnter", {
138+
group = self.augroup_id,
139+
pattern = "NvimTree_*",
140+
callback = function()
141+
if utils.is_nvim_tree_buf(0) then
142+
if vim.fn.getcwd() ~= core.get_cwd() or (self.opts.reload_on_bufenter and not self.opts.filesystem_watchers.enable) then
143+
self:reload_explorer()
144+
end
145+
end
146+
end,
147+
})
148+
149+
vim.api.nvim_create_autocmd("User", {
150+
group = self.augroup_id,
151+
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
152+
callback = function()
153+
if not self.opts.filesystem_watchers.enable and self.opts.git.enable then
154+
self:reload_git()
155+
end
156+
end,
157+
})
158+
159+
if self.opts.hijack_cursor then
160+
vim.api.nvim_create_autocmd("CursorMoved", {
161+
group = self.augroup_id,
162+
pattern = "NvimTree_*",
163+
callback = function()
164+
if utils.is_nvim_tree_buf(0) then
165+
self:place_cursor_on_node()
166+
end
167+
end,
168+
})
169+
end
170+
171+
if self.opts.modified.enable then
172+
vim.api.nvim_create_autocmd({ "BufModifiedSet", "BufWritePost" }, {
173+
group = self.augroup_id,
174+
callback = function()
175+
utils.debounce("Buf:modified_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
176+
buffers.reload_modified()
177+
self:reload_explorer()
178+
end)
179+
end,
180+
})
181+
end
182+
end
183+
77184
---@param node DirectoryNode
78185
function Explorer:expand(node)
79186
self:_load(node)
@@ -430,7 +537,6 @@ end
430537

431538
function Explorer:setup(opts)
432539
config = opts
433-
require("nvim-tree.explorer.watch").setup(opts)
434540
end
435541

436542
return Explorer

lua/nvim-tree/node/directory-link.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ function DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name
3232
return o
3333
end
3434

35+
function DirectoryLinkNode:destroy()
36+
DirectoryNode.destroy(self)
37+
end
38+
3539
-----Update the directory GitStatus of link target and the file status of the link itself
3640
-----@param parent_ignored boolean
3741
-----@param status table|nil

lua/nvim-tree/node/file-link.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ function FileLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_
2828
return o
2929
end
3030

31+
function FileLinkNode:destroy()
32+
FileNode.destroy(self)
33+
end
34+
3135
-----Update the GitStatus of the target otherwise the link itself
3236
-----@param parent_ignored boolean
3337
-----@param status table|nil

lua/nvim-tree/node/file.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
3636
return o
3737
end
3838

39+
function FileNode:destroy()
40+
Node.destroy(self)
41+
end
42+
3943
---Update the GitStatus of the file
4044
---@param parent_ignored boolean
4145
---@param status table|nil

lua/nvim-tree/node/root.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ function RootNode:is_dotfile()
2323
return false
2424
end
2525

26+
function RootNode:destroy()
27+
DirectoryNode.destroy(self)
28+
end
29+
2630
return RootNode

0 commit comments

Comments
 (0)