diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 06d0cf01617..6e192419e85 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -339,7 +339,7 @@ local function setup_autocommands(opts) create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = function() - if vim.bo.filetype == "NvimTree" then + if utils.is_nvim_tree_buf(0) then view._prevent_buffer_override() end end, @@ -362,7 +362,14 @@ local function setup_autocommands(opts) create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_change) }) end if opts.hijack_cursor then - create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node }) + create_nvim_tree_autocmd("CursorMoved", { + pattern = "NvimTree_*", + callback = function() + if utils.is_nvim_tree_buf(0) then + M.place_cursor_on_node() + end + end, + }) end if opts.sync_root_with_cwd then create_nvim_tree_autocmd("DirChanged", { @@ -384,7 +391,14 @@ local function setup_autocommands(opts) end if opts.reload_on_bufenter and not has_watchers then - create_nvim_tree_autocmd("BufEnter", { pattern = "NvimTree_*", callback = reloaders.reload_explorer }) + create_nvim_tree_autocmd("BufEnter", { + pattern = "NvimTree_*", + callback = function() + if utils.is_nvim_tree_buf(0) then + reloaders.reload_explorer() + end + end, + }) end if opts.view.centralize_selection then @@ -418,7 +432,14 @@ local function setup_autocommands(opts) end if opts.view.float.enable and opts.view.float.quit_on_focus_loss then - create_nvim_tree_autocmd("WinLeave", { pattern = "NvimTree_*", callback = view.close }) + create_nvim_tree_autocmd("WinLeave", { + pattern = "NvimTree_*", + callback = function() + if utils.is_nvim_tree_buf(0) then + view.close() + end + end, + }) end end diff --git a/lua/nvim-tree/live-filter.lua b/lua/nvim-tree/live-filter.lua index 4d8ae7cff82..9143d9fe335 100644 --- a/lua/nvim-tree/live-filter.lua +++ b/lua/nvim-tree/live-filter.lua @@ -1,6 +1,7 @@ local a = vim.api local view = require "nvim-tree.view" +local utils = require "nvim-tree.utils" local Iterator = require "nvim-tree.iterators.node-iterator" local M = { @@ -30,7 +31,11 @@ local function remove_overlay() a.nvim_create_autocmd("WinLeave", { pattern = "NvimTree_*", group = a.nvim_create_augroup("NvimTree", { clear = false }), - callback = view.close, + callback = function() + if utils.is_nvim_tree_buf(0) then + view.close() + end + end, }) end diff --git a/lua/nvim-tree/renderer/components/full-name.lua b/lua/nvim-tree/renderer/components/full-name.lua index b589b47c125..41c2425b140 100644 --- a/lua/nvim-tree/renderer/components/full-name.lua +++ b/lua/nvim-tree/renderer/components/full-name.lua @@ -2,6 +2,7 @@ local M = {} local api = vim.api local fn = vim.fn +local utils = require "nvim-tree.utils" local function hide(win) if win then @@ -67,7 +68,9 @@ M.setup = function(opts) group = group, pattern = { "NvimTree_*" }, callback = function() - hide(M.popup_win) + if utils.is_nvim_tree_buf(0) then + hide(M.popup_win) + end end, }) @@ -75,7 +78,9 @@ M.setup = function(opts) group = group, pattern = { "NvimTree_*" }, callback = function() - show() + if utils.is_nvim_tree_buf(0) then + show() + end end, }) end diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 6d6af956198..e81327ff76b 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -468,13 +468,23 @@ function M.inject_node(f) end end ----Is the buffer a tree? Like /path/to/NvimTree_2 and not a readable file. ----@param bufnr number +---Is the buffer named NvimTree_[0-9]+ a tree? filetype is "NvimTree" or not readable file. +---This is cheap, as the readable test should only ever be needed when resuming a vim session. +---@param bufnr number may be 0 or nil for current ---@return boolean function M.is_nvim_tree_buf(bufnr) + if bufnr == nil then + bufnr = 0 + end if vim.fn.bufexists(bufnr) then local bufname = a.nvim_buf_get_name(bufnr) - return vim.fn.fnamemodify(bufname, ":t"):match "^NvimTree_[0-9]+$" and vim.fn.filereadable(bufname) == 0 + if vim.fn.fnamemodify(bufname, ":t"):match "^NvimTree_[0-9]+$" then + if vim.bo[bufnr].filetype == "NvimTree" then + return true + elseif vim.fn.filereadable(bufname) == 0 then + return true + end + end end return false end