From 3a82885cc3acf9ec241e38eafdd84f21a52f0f4d Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Apr 2025 12:17:21 +1000 Subject: [PATCH] refactor(#2826): move autocmds to Explorer --- lua/nvim-tree.lua | 35 --------------------------------- lua/nvim-tree/explorer/init.lua | 28 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index b9b55e7f..b5df82fb 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -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 = "*", diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 49715f6b..6da98b2a 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -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_*",