Skip to content

Commit 48992fd

Browse files
authored
fix(#1639): ensure tree autocommands match filetype as well as name (#1640)
* fix(#1629): nvim start with file named *NvimTree* opens tree instead of buffer * Revert "fix(#1629): nvim start with file named *NvimTree* opens tree instead of buffer" This reverts commit e713607. * fix(#1629): nvim start with file named *NvimTree* treats file as tree * fix(#1629): nvim start with file named *NvimTree* treats file as tree * fix(#1639): ensure tree autocommands match filetype as well as name * fix(#1639): fix bad merge * fix(#1639): ensure tree autocommands match filetype as well as name
1 parent c995ce0 commit 48992fd

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

lua/nvim-tree.lua

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ local function setup_autocommands(opts)
339339
create_nvim_tree_autocmd("BufWipeout", {
340340
pattern = "NvimTree_*",
341341
callback = function()
342-
if vim.bo.filetype == "NvimTree" then
342+
if utils.is_nvim_tree_buf(0) then
343343
view._prevent_buffer_override()
344344
end
345345
end,
@@ -362,7 +362,14 @@ local function setup_autocommands(opts)
362362
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_change) })
363363
end
364364
if opts.hijack_cursor then
365-
create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node })
365+
create_nvim_tree_autocmd("CursorMoved", {
366+
pattern = "NvimTree_*",
367+
callback = function()
368+
if utils.is_nvim_tree_buf(0) then
369+
M.place_cursor_on_node()
370+
end
371+
end,
372+
})
366373
end
367374
if opts.sync_root_with_cwd then
368375
create_nvim_tree_autocmd("DirChanged", {
@@ -384,7 +391,14 @@ local function setup_autocommands(opts)
384391
end
385392

386393
if opts.reload_on_bufenter and not has_watchers then
387-
create_nvim_tree_autocmd("BufEnter", { pattern = "NvimTree_*", callback = reloaders.reload_explorer })
394+
create_nvim_tree_autocmd("BufEnter", {
395+
pattern = "NvimTree_*",
396+
callback = function()
397+
if utils.is_nvim_tree_buf(0) then
398+
reloaders.reload_explorer()
399+
end
400+
end,
401+
})
388402
end
389403

390404
if opts.view.centralize_selection then
@@ -418,7 +432,14 @@ local function setup_autocommands(opts)
418432
end
419433

420434
if opts.view.float.enable and opts.view.float.quit_on_focus_loss then
421-
create_nvim_tree_autocmd("WinLeave", { pattern = "NvimTree_*", callback = view.close })
435+
create_nvim_tree_autocmd("WinLeave", {
436+
pattern = "NvimTree_*",
437+
callback = function()
438+
if utils.is_nvim_tree_buf(0) then
439+
view.close()
440+
end
441+
end,
442+
})
422443
end
423444
end
424445

lua/nvim-tree/live-filter.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local a = vim.api
22

33
local view = require "nvim-tree.view"
4+
local utils = require "nvim-tree.utils"
45
local Iterator = require "nvim-tree.iterators.node-iterator"
56

67
local M = {
@@ -30,7 +31,11 @@ local function remove_overlay()
3031
a.nvim_create_autocmd("WinLeave", {
3132
pattern = "NvimTree_*",
3233
group = a.nvim_create_augroup("NvimTree", { clear = false }),
33-
callback = view.close,
34+
callback = function()
35+
if utils.is_nvim_tree_buf(0) then
36+
view.close()
37+
end
38+
end,
3439
})
3540
end
3641

lua/nvim-tree/renderer/components/full-name.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local M = {}
22

33
local api = vim.api
44
local fn = vim.fn
5+
local utils = require "nvim-tree.utils"
56

67
local function hide(win)
78
if win then
@@ -67,15 +68,19 @@ M.setup = function(opts)
6768
group = group,
6869
pattern = { "NvimTree_*" },
6970
callback = function()
70-
hide(M.popup_win)
71+
if utils.is_nvim_tree_buf(0) then
72+
hide(M.popup_win)
73+
end
7174
end,
7275
})
7376

7477
api.nvim_create_autocmd({ "CursorMoved" }, {
7578
group = group,
7679
pattern = { "NvimTree_*" },
7780
callback = function()
78-
show()
81+
if utils.is_nvim_tree_buf(0) then
82+
show()
83+
end
7984
end,
8085
})
8186
end

lua/nvim-tree/utils.lua

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,23 @@ function M.inject_node(f)
468468
end
469469
end
470470

471-
---Is the buffer a tree? Like /path/to/NvimTree_2 and not a readable file.
472-
---@param bufnr number
471+
---Is the buffer named NvimTree_[0-9]+ a tree? filetype is "NvimTree" or not readable file.
472+
---This is cheap, as the readable test should only ever be needed when resuming a vim session.
473+
---@param bufnr number may be 0 or nil for current
473474
---@return boolean
474475
function M.is_nvim_tree_buf(bufnr)
476+
if bufnr == nil then
477+
bufnr = 0
478+
end
475479
if vim.fn.bufexists(bufnr) then
476480
local bufname = a.nvim_buf_get_name(bufnr)
477-
return vim.fn.fnamemodify(bufname, ":t"):match "^NvimTree_[0-9]+$" and vim.fn.filereadable(bufname) == 0
481+
if vim.fn.fnamemodify(bufname, ":t"):match "^NvimTree_[0-9]+$" then
482+
if vim.bo[bufnr].filetype == "NvimTree" then
483+
return true
484+
elseif vim.fn.filereadable(bufname) == 0 then
485+
return true
486+
end
487+
end
478488
end
479489
return false
480490
end

0 commit comments

Comments
 (0)