Skip to content

Commit 83fdff7

Browse files
committed
refactor(#2826): globals.TABPAGES -> WINID_PER_TAB
1 parent 09ec00c commit 83fdff7

File tree

4 files changed

+36
-105
lines changed

4 files changed

+36
-105
lines changed

lua/nvim-tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ function M.purge_all_state()
668668
local explorer = core.get_explorer()
669669
if explorer then
670670
explorer.view:close_all_tabs()
671-
explorer.view:abandon_all_windows("purge_all_state")
671+
explorer.view:abandon_all_windows()
672672
require("nvim-tree.git").purge_state()
673673
explorer:destroy()
674674
core.reset_explorer()

lua/nvim-tree/explorer/view.lua

Lines changed: 29 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ function View:destroy()
6969
self.explorer:log_destroy("View")
7070
end
7171

72-
-- The initial state of a tab
73-
local tabinitial = {
74-
-- The position of the cursor { line, column }
75-
cursor = { 0, 0 },
76-
-- The NvimTree window number
77-
winid = nil,
78-
}
79-
8072
---@type { name: string, value: any }[]
8173
local BUFFER_OPTIONS = {
8274
{ name = "bufhidden", value = "wipe" },
@@ -163,24 +155,6 @@ local move_tbl = {
163155
right = "L",
164156
}
165157

166-
-- setup_tabpage sets up the initial state of a tab
167-
---@private
168-
---@param tabpage integer
169-
---@param callsite string
170-
function View:setup_tabpage(tabpage, callsite)
171-
local winid = vim.api.nvim_get_current_win()
172-
173-
if self.explorer.opts.experimental.multi_instance then
174-
log.line("dev", "View:setup_tabpage(%3s, %-20.20s) w%d %s",
175-
tabpage,
176-
callsite,
177-
winid,
178-
globals.TABPAGES[tabpage] and vim.inspect(globals.TABPAGES[tabpage], { newline = "" }) or "tabinitial")
179-
end
180-
181-
globals.TABPAGES[tabpage] = vim.tbl_extend("force", globals.TABPAGES[tabpage] or tabinitial, { winid = winid })
182-
end
183-
184158
---@private
185159
function View:set_window_options_and_buffer()
186160
pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr("View:set_window_options_and_buffer"))
@@ -226,7 +200,7 @@ function View:open_window()
226200
vim.api.nvim_command("vsp")
227201
self:reposition_window()
228202
end
229-
self:setup_tabpage(vim.api.nvim_get_current_tabpage(), "View:open_window")
203+
globals.WINID_PER_TAB[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win()
230204
self:set_window_options_and_buffer()
231205
end
232206

@@ -308,7 +282,7 @@ function View:close_this_tab_only()
308282
end
309283

310284
function View:close_all_tabs()
311-
for tabpage, _ in pairs(globals.TABPAGES) do
285+
for tabpage, _ in pairs(globals.WINID_PER_TAB) do
312286
self:close_internal(tabpage)
313287
end
314288
end
@@ -449,22 +423,9 @@ function View:reposition_window()
449423
end
450424

451425
---@private
452-
---@param callsite string
453-
function View:set_current_win(callsite)
426+
function View:set_current_win()
454427
local current_tab = vim.api.nvim_get_current_tabpage()
455-
local current_win = vim.api.nvim_get_current_win()
456-
457-
if self.explorer.opts.experimental.multi_instance then
458-
log.line("dev", "View:set_current_win(%-20.20s) t%d w%3s->w%3s %s",
459-
callsite,
460-
current_tab,
461-
globals.TABPAGES[current_tab].winid,
462-
current_win,
463-
(globals.TABPAGES[current_tab].winid == current_win) and "" or "MISMATCH"
464-
)
465-
end
466-
467-
globals.TABPAGES[current_tab].winid = current_win
428+
globals.WINID_PER_TAB[current_tab] = vim.api.nvim_get_current_win()
468429
end
469430

470431
---@class OpenInWinOpts
@@ -481,8 +442,8 @@ function View:open_in_win(opts)
481442
vim.api.nvim_set_current_win(opts.winid)
482443
end
483444
self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
484-
self:setup_tabpage(vim.api.nvim_get_current_tabpage(), "View:open_in_win")
485-
self:set_current_win("View:open_in_win")
445+
globals.WINID_PER_TAB[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win()
446+
self:set_current_win()
486447
self:set_window_options_and_buffer()
487448
if opts.resize then
488449
self:reposition_window()
@@ -497,7 +458,7 @@ function View:abandon_current_window()
497458
if self.explorer.opts.experimental.multi_instance then
498459
log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s",
499460
tab,
500-
globals.TABPAGES[tab] and globals.TABPAGES[tab].winid or nil,
461+
globals.WINID_PER_TAB[tab],
501462
globals.BUFNR_PER_TAB[tab],
502463
self.bufnr_by_tab[tab],
503464
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
@@ -509,30 +470,14 @@ function View:abandon_current_window()
509470
globals.BUFNR_PER_TAB[tab] = nil
510471
self.bufnr_by_tab[tab] = nil
511472

512-
if globals.TABPAGES[tab] then
513-
globals.TABPAGES[tab].winid = nil
514-
end
473+
globals.WINID_PER_TAB[tab] = nil
515474
end
516475

517-
---@param callsite string
518-
function View:abandon_all_windows(callsite)
476+
function View:abandon_all_windows()
477+
-- TODO multi-instance kill the buffer instead of retaining
519478
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
520-
if self.explorer.opts.experimental.multi_instance then
521-
log.line("dev", "View:abandon_all_windows(%-20.20s) t%d w%s b%s member b%s %s",
522-
callsite,
523-
tab,
524-
globals.TABPAGES and globals.TABPAGES.winid or nil,
525-
globals.BUFNR_PER_TAB[tab],
526-
self.bufnr_by_tab[tab],
527-
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
528-
end
529-
530-
-- TODO multi-instance kill the buffer instead of retaining
531-
532479
globals.BUFNR_PER_TAB[tab] = nil
533-
if globals.TABPAGES[tab] then
534-
globals.TABPAGES[tab].winid = nil
535-
end
480+
globals.WINID_PER_TAB[tab] = nil
536481
end
537482
end
538483

@@ -541,16 +486,16 @@ end
541486
function View:is_visible(opts)
542487
-- TODO multi-instance rewrite and consistency check
543488
if opts and opts.tabpage then
544-
if globals.TABPAGES[opts.tabpage] == nil then
489+
if not globals.WINID_PER_TAB[opts.tabpage] then
545490
return false
546491
end
547-
local winid = globals.TABPAGES[opts.tabpage].winid
492+
local winid = globals.WINID_PER_TAB[opts.tabpage]
548493
return winid and vim.api.nvim_win_is_valid(winid)
549494
end
550495

551496
if opts and opts.any_tabpage then
552-
for _, v in pairs(globals.TABPAGES) do
553-
if v.winid and vim.api.nvim_win_is_valid(v.winid) then
497+
for _, winid in pairs(globals.WINID_PER_TAB) do
498+
if winid and vim.api.nvim_win_is_valid(winid) then
554499
return true
555500
end
556501
end
@@ -611,22 +556,16 @@ end
611556
--- not legacy codepath
612557
--- winid containing the buffer
613558
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
614-
---@param callsite string
615559
---@return integer? winid
616-
function View:winid(tabpage, callsite)
560+
function View:winid(tabpage)
617561
local bufnr = self.bufnr_by_tab[tabpage]
618562

619-
local msg = string.format("View:winid(%3s, %-20.20s)", tabpage, callsite)
620-
621563
if bufnr then
622564
for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do
623565
if vim.api.nvim_win_get_buf(winid) == bufnr then
624-
log.line("dev", "%s b%d : w%s", msg, bufnr, winid)
625566
return winid
626567
end
627568
end
628-
else
629-
log.line("dev", "%s no bufnr", msg)
630569
end
631570
end
632571

@@ -636,28 +575,26 @@ end
636575
---@return number|nil
637576
function View:get_winid(tabpage, callsite)
638577
local tabid = tabpage or vim.api.nvim_get_current_tabpage()
639-
local tabinfo = globals.TABPAGES[tabid]
640578
local tabinfo_winid = nil
641579

642580
if self.explorer.opts.experimental.multi_instance then
581+
643582
local msg_fault = ""
644-
if not tabinfo then
645-
msg_fault = "no tabinfo"
646-
elseif not tabinfo.winid then
647-
msg_fault = "no tabinfo.winid"
648-
elseif not vim.api.nvim_win_is_valid(tabinfo.winid) then
649-
msg_fault = string.format("invalid tabinfo.winid %d", tabinfo.winid)
583+
if not globals.WINID_PER_TAB[tabid] then
584+
msg_fault = "no WINID_PER_TAB"
585+
elseif not vim.api.nvim_win_is_valid(globals.WINID_PER_TAB[tabid]) then
586+
msg_fault = string.format("invalid globals.WINID_PER_TAB[tabid] %d", globals.WINID_PER_TAB[tabid])
650587
else
651-
tabinfo_winid = tabinfo.winid
588+
tabinfo_winid = globals.WINID_PER_TAB[tabid]
652589
end
653590

654-
local winid = self:winid(tabid, "View:get_winid")
591+
local winid = self:winid(tabid)
655592

656593
if winid ~= tabinfo_winid then
657594
msg_fault = "MISMATCH"
658595
end
659596

660-
local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s].winid=w%s view.winid(%s)=w%s %s",
597+
local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s]=w%s view.winid(%s)=w%s %s",
661598
tabpage,
662599
callsite,
663600
tabid, tabinfo_winid,
@@ -670,13 +607,11 @@ function View:get_winid(tabpage, callsite)
670607
if winid ~= tabinfo_winid then
671608
notify.error(msg)
672609
end
673-
674-
return winid
675610
end
676611

677612
-- legacy codepath
678-
if tabinfo and tabinfo.winid and vim.api.nvim_win_is_valid(tabinfo.winid) then
679-
return tabinfo.winid
613+
if tabinfo_winid and vim.api.nvim_win_is_valid(tabinfo_winid) then
614+
return tabinfo_winid
680615
end
681616
end
682617

@@ -717,13 +652,9 @@ function View:prevent_buffer_override()
717652

718653
--- TODO multi-instance this can be removed as winid() will handle it
719654
if not bufname:match("NvimTree") then
720-
for i, tabpage in ipairs(globals.TABPAGES) do
721-
if tabpage.winid == view_winid then
722-
if self.explorer.opts.experimental.multi_instance then
723-
log.line("dev", "View:prevent_buffer_override() t%d w%d clearing", i, view_winid)
724-
end
725-
726-
globals.TABPAGES[i] = nil
655+
for i, winid in ipairs(globals.WINID_PER_TAB) do
656+
if winid == view_winid then
657+
globals.WINID_PER_TAB[i] = nil
727658
break
728659
end
729660
end

lua/nvim-tree/globals.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
local M = {
44
-- from View
5-
TABPAGES = {},
5+
WINID_PER_TAB = {},
66
BUFNR_PER_TAB = {},
77
CURSORS = {},
88
}

lua/nvim-tree/multi-instance-debug.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local globals = require("nvim-tree.globals")
33
local M = {}
44

55
--- Debugging only.
6-
--- Tabs show TABPAGES winid and BUFNR_PER_TAB bufnr for the tab.
6+
--- Tabs show WINID_PER_TAB winid and BUFNR_PER_TAB bufnr for the tab.
77
--- Orphans for inexistent tab_ids are shown at the right.
88
--- lib.target_winid is always shown at the right next to a close button.
99
--- Enable with:
@@ -15,7 +15,7 @@ function M.tab_line()
1515
local cur_tab_id = vim.api.nvim_get_current_tabpage()
1616

1717
local bufnr_per_tab = vim.deepcopy(globals.BUFNR_PER_TAB)
18-
local tabpages = vim.deepcopy(globals.TABPAGES)
18+
local tabpages = vim.deepcopy(globals.WINID_PER_TAB)
1919

2020
local tl = "%#TabLine#"
2121

@@ -34,9 +34,9 @@ function M.tab_line()
3434
tl = tl .. " t" .. tab_id
3535

3636
-- winid, if present
37-
local tp = globals.TABPAGES[tab_id]
37+
local tp = globals.WINID_PER_TAB[tab_id]
3838
if tp then
39-
tl = tl .. " w" .. (tp.winid or "nil")
39+
tl = tl .. " w" .. (tp or "nil")
4040
else
4141
tl = tl .. " "
4242
end
@@ -67,7 +67,7 @@ function M.tab_line()
6767
end
6868
for tab_id, tp in pairs(tabpages) do
6969
orphans[tab_id] = orphans[tab_id] or {}
70-
orphans[tab_id].winid = tp.winid
70+
orphans[tab_id].winid = tp
7171
end
7272

7373
-- right-align

0 commit comments

Comments
 (0)