Skip to content

fix(view): Recreate tree buffer if deleted, and handle scenario where buffer already exists. #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local events = require'nvim-tree.events'
local populate = pops.populate
local refresh_entries = pops.refresh_entries

local first_init_done = false
local window_opts = config.window_options()

local M = {}
Expand Down Expand Up @@ -41,7 +42,10 @@ function M.init(with_open, with_reload)
M.Tree.loaded = true
end

events._dispatch_ready()
if not first_init_done then
events._dispatch_ready()
first_init_done = true
end
end

local function get_node_at_line(line)
Expand Down
41 changes: 40 additions & 1 deletion lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,52 @@ M.View = {
}
}

---Find a rogue NvimTree buffer that might have been spawned by i.e. a session.
---@return integer|nil
local function find_rogue_buffer()
for i = 1, vim.fn.bufnr("$"), 1 do
if vim.fn.bufname(i) == "NvimTree" then
return i
end
end
return nil
end

---Check if the tree buffer is valid and loaded.
---@return boolean
local function is_buf_valid()
return a.nvim_buf_is_valid(M.View.bufnr) and a.nvim_buf_is_loaded(M.View.bufnr)
end

---Find pre-existing NvimTree buffer, delete its windows then wipe it.
---@private
function M._wipe_rogue_buffer()
local bn = find_rogue_buffer()
if bn then
local win_ids = vim.fn.win_findbuf(bn)
for _, id in ipairs(win_ids) do
a.nvim_win_close(id, true)
end
a.nvim_buf_delete(bn, {})
end
end

-- set user options and create tree buffer (should never be wiped)
function M.setup()
M.View.auto_resize = vim.g.nvim_tree_auto_resize or M.View.auto_resize
M.View.side = vim.g.nvim_tree_side or M.View.side
M.View.width = vim.g.nvim_tree_width or M.View.width

M.View.bufnr = a.nvim_create_buf(false, false)

if not pcall(a.nvim_buf_set_name, M.View.bufnr, 'NvimTree') then
M._wipe_rogue_buffer()
a.nvim_buf_set_name(M.View.bufnr, 'NvimTree')
end

for k, v in pairs(M.View.bufopts) do
a.nvim_buf_set_option(M.View.bufnr, k, v)
end
a.nvim_buf_set_name(M.View.bufnr, 'NvimTree')

if not vim.g.nvim_tree_disable_keybindings then
M.View.bindings = vim.tbl_extend(
Expand Down Expand Up @@ -130,6 +165,10 @@ local move_tbl = {
}

function M.open()
if not is_buf_valid() then
M.setup()
end

a.nvim_command("vsp")
local move_to = move_tbl[M.View.side]
a.nvim_command("wincmd "..move_to)
Expand Down
1 change: 1 addition & 0 deletions plugin/tree.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ augroup NvimTree
if get(g:, 'nvim_tree_tab_open') == 1
au TabEnter * lua require'nvim-tree'.tab_change()
endif
au SessionLoadPost * lua require'nvim-tree.view'._wipe_rogue_buffer()
augroup end

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