Skip to content

Commit 224854c

Browse files
committed
Refactor close to work with tabid
1 parent 485b0ec commit 224854c

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

lua/nvim-tree/view.lua

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,20 @@ local function switch_buf_if_last_buf()
179179
end
180180

181181
-- save_tab_state saves any state that should be preserved across redraws.
182-
local function save_tab_state()
183-
local tabpage = vim.api.nvim_get_current_tabpage()
184-
M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr())
182+
local function save_tab_state(tabnr)
183+
local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
184+
M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage))
185185
end
186186

187-
local function close(all_tabpages)
188-
if not M.is_visible() then
187+
local function close(tabpage)
188+
if not M.is_visible { tabpage = tabpage } then
189189
return
190190
end
191-
save_tab_state()
191+
save_tab_state(tabpage)
192192
switch_buf_if_last_buf()
193-
local tree_win = M.get_winnr()
193+
local tree_win = M.get_winnr(tabpage)
194194
local current_win = vim.api.nvim_get_current_win()
195-
for _, win in pairs(vim.api.nvim_list_wins()) do
195+
for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
196196
if tree_win ~= win and vim.api.nvim_win_get_config(win).relative == "" then
197197
local prev_win = vim.fn.winnr "#" -- this tab only
198198
if tree_win == current_win and prev_win > 0 then
@@ -201,29 +201,28 @@ local function close(all_tabpages)
201201
if vim.api.nvim_win_is_valid(tree_win) then
202202
vim.api.nvim_win_close(tree_win, true)
203203
end
204-
if all_tabpages then
205-
for _, v in pairs(M.View.tabpages) do
206-
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
207-
vim.api.nvim_win_close(v.winnr, true)
208-
end
209-
end
210-
end
211204
events._dispatch_on_tree_close()
212205
return
213206
end
214207
end
215208
end
216209

217210
function M.close_this_tab_only()
218-
close(false)
211+
close(vim.api.nvim_get_current_tabpage())
219212
end
220213

221214
function M.close_all_tabs()
222-
close(true)
215+
for tabpage, _ in pairs(M.View.tabpages) do
216+
close(tabpage)
217+
end
223218
end
224219

225220
function M.close()
226-
close(M.View.tab.sync.close)
221+
if M.View.tab.sync.close then
222+
M.close_all_tabs()
223+
else
224+
M.close_this_tab_only()
225+
end
227226
end
228227

229228
function M.open(options)
@@ -331,6 +330,14 @@ function M.abandon_current_window()
331330
end
332331

333332
function M.is_visible(opts)
333+
if opts and opts.tabpage then
334+
if M.View.tabpages[opts.tabpage] == nil then
335+
return false
336+
end
337+
local winnr = M.View.tabpages[opts.tabpage].winnr
338+
return winnr and vim.api.nvim_win_is_valid(winnr)
339+
end
340+
334341
if opts and opts.any_tabpage then
335342
for _, v in pairs(M.View.tabpages) do
336343
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then

0 commit comments

Comments
 (0)