refactor(#2826): move autocmds to Explorer

This commit is contained in:
Alexander Courtis 2025-04-21 12:17:21 +10:00
parent 77d2aa891b
commit 89d68db3aa
2 changed files with 28 additions and 35 deletions

View File

@ -148,26 +148,6 @@ local function setup_autocommands(opts)
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
end
-- TODO #2826 move this to explorer
-- prevent new opened file from opening in the same window as nvim-tree
create_nvim_tree_autocmd("BufWipeout", {
pattern = "NvimTree_*",
callback = function()
if not utils.is_nvim_tree_buf(0) then
return
end
local explorer = core.get_explorer()
if explorer then
if opts.actions.open_file.eject then
explorer.view:prevent_buffer_override()
else
explorer.view:abandon_current_window()
end
end
end,
})
if opts.tab.sync.open then
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) })
end
@ -229,21 +209,6 @@ local function setup_autocommands(opts)
})
end
-- TODO #2826 move this to explorer
if opts.view.float.enable and opts.view.float.quit_on_focus_loss then
create_nvim_tree_autocmd("WinLeave", {
pattern = "NvimTree_*",
callback = function()
if utils.is_nvim_tree_buf(0) then
local explorer = core.get_explorer()
if explorer then
explorer.view:close()
end
end
end,
})
end
-- Handles event dispatch when tree is closed by `:q`
create_nvim_tree_autocmd("WinClosed", {
pattern = "*",

View File

@ -91,6 +91,18 @@ function Explorer:create_autocmds()
end,
})
if self.opts.view.float.enable and self.opts.view.float.quit_on_focus_loss then
vim.api.nvim_create_autocmd("WinLeave", {
group = self.augroup_id,
pattern = "NvimTree_*",
callback = function()
if utils.is_nvim_tree_buf(0) then
self.view:close()
end
end,
})
end
vim.api.nvim_create_autocmd("BufWritePost", {
group = self.augroup_id,
callback = function()
@ -143,6 +155,22 @@ function Explorer:create_autocmds()
end,
})
-- prevent new opened file from opening in the same window as nvim-tree
vim.api.nvim_create_autocmd("BufWipeout", {
group = self.augroup_id,
pattern = "NvimTree_*",
callback = function()
if not utils.is_nvim_tree_buf(0) then
return
end
if self.opts.actions.open_file.eject then
self.view:prevent_buffer_override()
else
self.view:abandon_current_window()
end
end,
})
vim.api.nvim_create_autocmd("BufEnter", {
group = self.augroup_id,
pattern = "NvimTree_*",