Skip to content

Commit 3a82885

Browse files
committed
refactor(#2826): move autocmds to Explorer
1 parent 44cb3d2 commit 3a82885

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

lua/nvim-tree.lua

-35
Original file line numberDiff line numberDiff line change
@@ -148,26 +148,6 @@ local function setup_autocommands(opts)
148148
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
149149
end
150150

151-
-- TODO #2826 move this to explorer
152-
-- prevent new opened file from opening in the same window as nvim-tree
153-
create_nvim_tree_autocmd("BufWipeout", {
154-
pattern = "NvimTree_*",
155-
callback = function()
156-
if not utils.is_nvim_tree_buf(0) then
157-
return
158-
end
159-
160-
local explorer = core.get_explorer()
161-
if explorer then
162-
if opts.actions.open_file.eject then
163-
explorer.view:prevent_buffer_override()
164-
else
165-
explorer.view:abandon_current_window()
166-
end
167-
end
168-
end,
169-
})
170-
171151
if opts.tab.sync.open then
172152
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) })
173153
end
@@ -229,21 +209,6 @@ local function setup_autocommands(opts)
229209
})
230210
end
231211

232-
-- TODO #2826 move this to explorer
233-
if opts.view.float.enable and opts.view.float.quit_on_focus_loss then
234-
create_nvim_tree_autocmd("WinLeave", {
235-
pattern = "NvimTree_*",
236-
callback = function()
237-
if utils.is_nvim_tree_buf(0) then
238-
local explorer = core.get_explorer()
239-
if explorer then
240-
explorer.view:close()
241-
end
242-
end
243-
end,
244-
})
245-
end
246-
247212
-- Handles event dispatch when tree is closed by `:q`
248213
create_nvim_tree_autocmd("WinClosed", {
249214
pattern = "*",

lua/nvim-tree/explorer/init.lua

+28
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ function Explorer:create_autocmds()
9191
end,
9292
})
9393

94+
if self.opts.view.float.enable and self.opts.view.float.quit_on_focus_loss then
95+
vim.api.nvim_create_autocmd("WinLeave", {
96+
group = self.augroup_id,
97+
pattern = "NvimTree_*",
98+
callback = function()
99+
if utils.is_nvim_tree_buf(0) then
100+
self.view:close()
101+
end
102+
end,
103+
})
104+
end
105+
94106
vim.api.nvim_create_autocmd("BufWritePost", {
95107
group = self.augroup_id,
96108
callback = function()
@@ -143,6 +155,22 @@ function Explorer:create_autocmds()
143155
end,
144156
})
145157

158+
-- prevent new opened file from opening in the same window as nvim-tree
159+
vim.api.nvim_create_autocmd("BufWipeout", {
160+
group = self.augroup_id,
161+
pattern = "NvimTree_*",
162+
callback = function()
163+
if not utils.is_nvim_tree_buf(0) then
164+
return
165+
end
166+
if self.opts.actions.open_file.eject then
167+
self.view:prevent_buffer_override()
168+
else
169+
self.view:abandon_current_window()
170+
end
171+
end,
172+
})
173+
146174
vim.api.nvim_create_autocmd("BufEnter", {
147175
group = self.augroup_id,
148176
pattern = "NvimTree_*",

0 commit comments

Comments
 (0)