Skip to content

Commit 4ee45d9

Browse files
authored
fix(view): Recreate tree buffer if deleted, and handle scenario where buffer already exists. (#307)
1 parent 48b06ed commit 4ee45d9

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

lua/nvim-tree/lib.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local events = require'nvim-tree.events'
1212
local populate = pops.populate
1313
local refresh_entries = pops.refresh_entries
1414

15+
local first_init_done = false
1516
local window_opts = config.window_options()
1617

1718
local M = {}
@@ -41,7 +42,10 @@ function M.init(with_open, with_reload)
4142
M.Tree.loaded = true
4243
end
4344

44-
events._dispatch_ready()
45+
if not first_init_done then
46+
events._dispatch_ready()
47+
first_init_done = true
48+
end
4549
end
4650

4751
local function get_node_at_line(line)

lua/nvim-tree/view.lua

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,52 @@ M.View = {
6666
}
6767
}
6868

69+
---Find a rogue NvimTree buffer that might have been spawned by i.e. a session.
70+
---@return integer|nil
71+
local function find_rogue_buffer()
72+
for i = 1, vim.fn.bufnr("$"), 1 do
73+
if vim.fn.bufname(i) == "NvimTree" then
74+
return i
75+
end
76+
end
77+
return nil
78+
end
79+
80+
---Check if the tree buffer is valid and loaded.
81+
---@return boolean
82+
local function is_buf_valid()
83+
return a.nvim_buf_is_valid(M.View.bufnr) and a.nvim_buf_is_loaded(M.View.bufnr)
84+
end
85+
86+
---Find pre-existing NvimTree buffer, delete its windows then wipe it.
87+
---@private
88+
function M._wipe_rogue_buffer()
89+
local bn = find_rogue_buffer()
90+
if bn then
91+
local win_ids = vim.fn.win_findbuf(bn)
92+
for _, id in ipairs(win_ids) do
93+
a.nvim_win_close(id, true)
94+
end
95+
a.nvim_buf_delete(bn, {})
96+
end
97+
end
98+
6999
-- set user options and create tree buffer (should never be wiped)
70100
function M.setup()
71101
M.View.auto_resize = vim.g.nvim_tree_auto_resize or M.View.auto_resize
72102
M.View.side = vim.g.nvim_tree_side or M.View.side
73103
M.View.width = vim.g.nvim_tree_width or M.View.width
74104

75105
M.View.bufnr = a.nvim_create_buf(false, false)
106+
107+
if not pcall(a.nvim_buf_set_name, M.View.bufnr, 'NvimTree') then
108+
M._wipe_rogue_buffer()
109+
a.nvim_buf_set_name(M.View.bufnr, 'NvimTree')
110+
end
111+
76112
for k, v in pairs(M.View.bufopts) do
77113
a.nvim_buf_set_option(M.View.bufnr, k, v)
78114
end
79-
a.nvim_buf_set_name(M.View.bufnr, 'NvimTree')
80115

81116
if not vim.g.nvim_tree_disable_keybindings then
82117
M.View.bindings = vim.tbl_extend(
@@ -130,6 +165,10 @@ local move_tbl = {
130165
}
131166

132167
function M.open()
168+
if not is_buf_valid() then
169+
M.setup()
170+
end
171+
133172
a.nvim_command("vsp")
134173
local move_to = move_tbl[M.View.side]
135174
a.nvim_command("wincmd "..move_to)

plugin/tree.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ augroup NvimTree
2525
if get(g:, 'nvim_tree_tab_open') == 1
2626
au TabEnter * lua require'nvim-tree'.tab_change()
2727
endif
28+
au SessionLoadPost * lua require'nvim-tree.view'._wipe_rogue_buffer()
2829
augroup end
2930

3031
command! NvimTreeOpen lua require'nvim-tree'.open()

0 commit comments

Comments
 (0)