Skip to content
29 changes: 25 additions & 4 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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", {
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion lua/nvim-tree/live-filter.lua
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions lua/nvim-tree/renderer/components/full-name.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,15 +68,19 @@ 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,
})

api.nvim_create_autocmd({ "CursorMoved" }, {
group = group,
pattern = { "NvimTree_*" },
callback = function()
show()
if utils.is_nvim_tree_buf(0) then
show()
end
end,
})
end
Expand Down
16 changes: 13 additions & 3 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down