Skip to content

Commit fbf6960

Browse files
committed
Properly sync nvim-tree across tabs
1 parent c995ce0 commit fbf6960

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lua/nvim-tree.lua

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ end
8888

8989
function M.open(cwd)
9090
cwd = cwd ~= "" and cwd or nil
91+
vim.g.nvim_tree_tab_open = 1
9192
if view.is_visible() then
9293
lib.set_target_win()
9394
view.focus()
@@ -120,7 +121,7 @@ function M.open_replacing_current_buffer(cwd)
120121
end
121122

122123
function M.tab_change()
123-
if view.is_visible { any_tabpage = true } then
124+
if vim.g.nvim_tree_tab_open == 1 then
124125
local bufname = api.nvim_buf_get_name(0)
125126
local ft = api.nvim_buf_get_option(0, "ft")
126127
for _, filter in ipairs(M.config.ignore_buf_on_tab_change) do
@@ -130,6 +131,33 @@ function M.tab_change()
130131
end
131132
view.open { focus_tree = false }
132133
require("nvim-tree.renderer").draw()
134+
else
135+
view.close()
136+
end
137+
end
138+
139+
function M.tab_win_closed(winnr)
140+
local tabnr = vim.api.nvim_win_get_tabpage(winnr)
141+
local bufnr = vim.api.nvim_win_get_buf(winnr)
142+
local buf_info = vim.fn.getbufinfo(bufnr)[1]
143+
local tab_wins = vim.tbl_filter(function(w) return w~=winnr end, vim.api.nvim_tabpage_list_wins(tabnr))
144+
local tab_bufs = vim.tbl_map(vim.api.nvim_win_get_buf, tab_wins)
145+
if buf_info.name:match(".*NvimTree_%d*$") then -- close buffer was nvim tree
146+
if not vim.tbl_isempty(tab_bufs) then -- and was not the last window (closed automatically)
147+
vim.g.nvim_tree_tab_open = 0 -- NOTE: this also catches a lot of cases where nvim tree was already closed.
148+
end
149+
else -- closed buffer was normal buffer
150+
if #tab_bufs == 1 then -- if there is only 1 buffer left
151+
local last_buf_info = vim.fn.getbufinfo(tab_bufs[1])[1]
152+
if last_buf_info.name:match(".*NvimTree_%d*$") then -- and that buffer is nvim tree
153+
vim.schedule(function ()
154+
if #vim.api.nvim_list_wins() == 1 then
155+
vim.cmd "quit"
156+
end
157+
vim.api.nvim_win_close(tab_wins[1], true) -- then close that window
158+
end)
159+
end
160+
end
133161
end
134162
end
135163

@@ -360,6 +388,11 @@ local function setup_autocommands(opts)
360388

361389
if opts.open_on_tab then
362390
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_change) })
391+
create_nvim_tree_autocmd("WinClosed", { callback = function ()
392+
local winnr = tonumber(vim.fn.expand("<amatch>"))
393+
vim.schedule_wrap(M.tab_win_closed(winnr))
394+
end
395+
})
363396
end
364397
if opts.hijack_cursor then
365398
create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node })
@@ -717,6 +750,8 @@ function M.setup(conf)
717750

718751
manage_netrw(opts.disable_netrw, opts.hijack_netrw)
719752

753+
vim.g.nvim_tree_tab_open = 0
754+
720755
M.config = opts
721756
require("nvim-tree.log").setup(opts)
722757

lua/nvim-tree/view.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ function M.close()
189189
if not M.is_visible() then
190190
return
191191
end
192+
vim.g.nvim_tree_tab_open = 0
192193
save_tab_state()
193194
switch_buf_if_last_buf()
194195
local tree_win = M.get_winnr()

0 commit comments

Comments
 (0)