@@ -18,7 +18,7 @@ local Class = require("nvim-tree.classic")
1818--- @field private width (fun (): integer )| integer | string
1919--- @field private max_width integer
2020--- @field private padding integer
21- --- @field private bufnr_by_tab table<integer, integer> stored per tab until multi-instance is complete
21+ --- @field private bufnr_by_tabid table<integer, integer> stored per tab until multi-instance is complete
2222local View = Class :extend ()
2323
2424--- @class View
@@ -36,7 +36,7 @@ function View:new(args)
3636 self .adaptive_size = false
3737 self .side = (self .explorer .opts .view .side == " right" ) and " right" or " left"
3838 self .live_filter = { prev_focused_node = nil , }
39- self .bufnr_by_tab = {}
39+ self .bufnr_by_tabid = {}
4040
4141 self .winopts = {
4242 relativenumber = self .explorer .opts .view .relativenumber ,
@@ -62,7 +62,7 @@ function View:new(args)
6262
6363 -- TODO multi-instance remove this; delete buffers rather than retaining them
6464 local tabid = vim .api .nvim_get_current_tabpage ()
65- self .bufnr_by_tab [tabid ] = globals .BUFNR_PER_TAB [tabid ]
65+ self .bufnr_by_tabid [tabid ] = globals .BUFNR_BY_TABID [tabid ]
6666end
6767
6868function View :destroy ()
@@ -84,7 +84,7 @@ local BUFFER_OPTIONS = {
8484--- @param bufnr integer
8585--- @return boolean
8686function View :matches_bufnr (bufnr )
87- for _ , b in pairs (globals .BUFNR_PER_TAB ) do
87+ for _ , b in pairs (globals .BUFNR_BY_TABID ) do
8888 if b == bufnr then
8989 return true
9090 end
@@ -107,15 +107,15 @@ end
107107function View :create_buffer (bufnr )
108108 self :wipe_rogue_buffer ()
109109
110- local tab = vim .api .nvim_get_current_tabpage ()
110+ local tabid = vim .api .nvim_get_current_tabpage ()
111111
112112 bufnr = bufnr or vim .api .nvim_create_buf (false , false )
113113
114114 -- set both bufnr registries
115- globals .BUFNR_PER_TAB [ tab ] = bufnr
116- self .bufnr_by_tab [ tab ] = bufnr
115+ globals .BUFNR_BY_TABID [ tabid ] = bufnr
116+ self .bufnr_by_tabid [ tabid ] = bufnr
117117
118- vim .api .nvim_buf_set_name (bufnr , " NvimTree_" .. tab )
118+ vim .api .nvim_buf_set_name (bufnr , " NvimTree_" .. tabid )
119119
120120 for _ , option in ipairs (BUFFER_OPTIONS ) do
121121 vim .api .nvim_set_option_value (option .name , option .value , { buf = bufnr })
@@ -200,7 +200,7 @@ function View:open_window()
200200 vim .api .nvim_command (" vsp" )
201201 self :reposition_window ()
202202 end
203- globals .WINID_PER_TAB [vim .api .nvim_get_current_tabpage ()] = vim .api .nvim_get_current_win ()
203+ globals .WINID_BY_TABID [vim .api .nvim_get_current_tabpage ()] = vim .api .nvim_get_current_win ()
204204 self :set_window_options_and_buffer ()
205205end
206206
@@ -237,34 +237,34 @@ end
237237
238238--- save_tab_state saves any state that should be preserved across redraws.
239239--- @private
240- --- @param tabnr integer
241- function View :save_tab_state (tabnr )
242- local tabpage = tabnr or vim .api .nvim_get_current_tabpage ()
243- globals .CURSORS [tabpage ] = vim .api .nvim_win_get_cursor (self :get_winid (tabpage , " View:save_tab_state" ) or 0 )
240+ --- @param tabid integer
241+ function View :save_tab_state (tabid )
242+ tabid = tabid or vim .api .nvim_get_current_tabpage ()
243+ globals .CURSORS [tabid ] = vim .api .nvim_win_get_cursor (self :get_winid (tabid , " View:save_tab_state" ) or 0 )
244244end
245245
246246--- @private
247- --- @param tabpage integer
248- function View :close_internal (tabpage )
247+ --- @param tabid integer
248+ function View :close_internal (tabid )
249249 if self .explorer .opts .experimental .multi_instance then
250- log .line (" dev" , " View:close_internal(t%s)" , tabpage )
250+ log .line (" dev" , " View:close_internal(t%s)" , tabid )
251251 end
252- if not self :is_visible ({ tabpage = tabpage }) then
252+ if not self :is_visible ({ tabpage = tabid }) then
253253 return
254254 end
255- self :save_tab_state (tabpage )
255+ self :save_tab_state (tabid )
256256 switch_buf_if_last_buf ()
257- local tree_win = self :get_winid (tabpage , " View:close_internal" )
257+ local tree_win = self :get_winid (tabid , " View:close_internal" )
258258 local current_win = vim .api .nvim_get_current_win ()
259- for _ , win in pairs (vim .api .nvim_tabpage_list_wins (tabpage )) do
259+ for _ , win in pairs (vim .api .nvim_tabpage_list_wins (tabid )) do
260260 if vim .api .nvim_win_get_config (win ).relative == " " then
261261 local prev_win = vim .fn .winnr (" #" ) -- this tab only
262262 if tree_win == current_win and prev_win > 0 then
263263 vim .api .nvim_set_current_win (vim .fn .win_getid (prev_win ))
264264 end
265265 if vim .api .nvim_win_is_valid (tree_win or 0 ) then
266266 if self .explorer .opts .experimental .multi_instance then
267- log .line (" dev" , " View:close_internal(t%s) w%s" , tabpage , tree_win )
267+ log .line (" dev" , " View:close_internal(t%s) w%s" , tabid , tree_win )
268268 end
269269 local success , error = pcall (vim .api .nvim_win_close , tree_win or 0 , true )
270270 if not success then
@@ -278,26 +278,34 @@ function View:close_internal(tabpage)
278278end
279279
280280function View :close_this_tab_only ()
281+ if self .explorer .opts .experimental .multi_instance then
282+ log .line (" dev" , " View:close_this_tab_only()" )
283+ end
281284 self :close_internal (vim .api .nvim_get_current_tabpage ())
282285end
283286
287+ -- TODO this is broken at 1.13.0 - current tab does not close when tab.sync.close is set
284288function View :close_all_tabs ()
285- for tabpage , _ in pairs (globals .WINID_PER_TAB ) do
286- self :close_internal (tabpage )
289+ log .line (" dev" , " View:close_all_tabs() globals.WINID_BY_TABID=%s" , vim .inspect (globals .WINID_BY_TABID ))
290+ for tabid , _ in pairs (globals .WINID_BY_TABID ) do
291+ if self .explorer .opts .experimental .multi_instance then
292+ log .line (" dev" , " View:close_all_tabs()" )
293+ end
294+ self :close_internal (tabid )
287295 end
288296end
289297
290- --- @param tabpage integer | nil
298+ --- @param tabid integer | nil
291299--- @param callsite string
292- function View :close (tabpage , callsite )
300+ function View :close (tabid , callsite )
293301 if self .explorer .opts .experimental .multi_instance then
294- log .line (" dev" , " View:close(t%s, %s)" , tabpage , callsite )
302+ log .line (" dev" , " View:close(t%s, %s)" , tabid , callsite )
295303 end
296304
297305 if self .explorer .opts .tab .sync .close then
298306 self :close_all_tabs ()
299- elseif tabpage then
300- self :close_internal (tabpage )
307+ elseif tabid then
308+ self :close_internal (tabid )
301309 else
302310 self :close_this_tab_only ()
303311 end
425433--- @private
426434function View :set_current_win ()
427435 local current_tab = vim .api .nvim_get_current_tabpage ()
428- globals .WINID_PER_TAB [current_tab ] = vim .api .nvim_get_current_win ()
436+ globals .WINID_BY_TABID [current_tab ] = vim .api .nvim_get_current_win ()
429437end
430438
431439--- @class OpenInWinOpts
@@ -442,7 +450,7 @@ function View:open_in_win(opts)
442450 vim .api .nvim_set_current_win (opts .winid )
443451 end
444452 self :create_buffer (opts .hijack_current_buf and vim .api .nvim_get_current_buf ())
445- globals .WINID_PER_TAB [vim .api .nvim_get_current_tabpage ()] = vim .api .nvim_get_current_win ()
453+ globals .WINID_BY_TABID [vim .api .nvim_get_current_tabpage ()] = vim .api .nvim_get_current_win ()
446454 self :set_current_win ()
447455 self :set_window_options_and_buffer ()
448456 if opts .resize then
@@ -458,26 +466,26 @@ function View:abandon_current_window()
458466 if self .explorer .opts .experimental .multi_instance then
459467 log .line (" dev" , " View:abandon_current_window() t%d w%s b%s member b%s %s" ,
460468 tab ,
461- globals .WINID_PER_TAB [tab ],
462- globals .BUFNR_PER_TAB [tab ],
463- self .bufnr_by_tab [tab ],
464- (globals .BUFNR_PER_TAB [tab ] == self .bufnr_by_tab [tab ]) and " " or " MISMATCH" )
469+ globals .WINID_BY_TABID [tab ],
470+ globals .BUFNR_BY_TABID [tab ],
471+ self .bufnr_by_tabid [tab ],
472+ (globals .BUFNR_BY_TABID [tab ] == self .bufnr_by_tabid [tab ]) and " " or " MISMATCH" )
465473 end
466474
467475 -- TODO multi-instance maybe kill the buffer instead of retaining
468476
469477 -- reset both bufnr registries
470- globals .BUFNR_PER_TAB [tab ] = nil
471- self .bufnr_by_tab [tab ] = nil
478+ globals .BUFNR_BY_TABID [tab ] = nil
479+ self .bufnr_by_tabid [tab ] = nil
472480
473- globals .WINID_PER_TAB [tab ] = nil
481+ globals .WINID_BY_TABID [tab ] = nil
474482end
475483
476484function View :abandon_all_windows ()
477485 -- TODO multi-instance kill the buffer instead of retaining
478486 for tab , _ in pairs (vim .api .nvim_list_tabpages ()) do
479- globals .BUFNR_PER_TAB [tab ] = nil
480- globals .WINID_PER_TAB [tab ] = nil
487+ globals .BUFNR_BY_TABID [tab ] = nil
488+ globals .WINID_BY_TABID [tab ] = nil
481489 end
482490end
483491
@@ -486,15 +494,15 @@ end
486494function View :is_visible (opts )
487495 -- TODO multi-instance rewrite and consistency check
488496 if opts and opts .tabpage then
489- if not globals .WINID_PER_TAB [opts .tabpage ] then
497+ if not globals .WINID_BY_TABID [opts .tabpage ] then
490498 return false
491499 end
492- local winid = globals .WINID_PER_TAB [opts .tabpage ]
500+ local winid = globals .WINID_BY_TABID [opts .tabpage ]
493501 return winid and vim .api .nvim_win_is_valid (winid )
494502 end
495503
496504 if opts and opts .any_tabpage then
497- for _ , winid in pairs (globals .WINID_PER_TAB ) do
505+ for _ , winid in pairs (globals .WINID_BY_TABID ) do
498506 if winid and vim .api .nvim_win_is_valid (winid ) then
499507 return true
500508 end
@@ -548,20 +556,19 @@ end
548556
549557--- Restores the state of a NvimTree window if it was initialized before.
550558function View :restore_tab_state ()
551- local tabpage = vim .api .nvim_get_current_tabpage ()
552- self :set_cursor (globals .CURSORS [tabpage ])
559+ self :set_cursor (globals .CURSORS [vim .api .nvim_get_current_tabpage ()])
553560end
554561
555562--- TODO multi-instance remove comment
556563--- not legacy codepath
557564--- winid containing the buffer
558- --- @param tabpage number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
565+ --- @param tabid number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
559566--- @return integer ? winid
560- function View :winid (tabpage )
561- local bufnr = self .bufnr_by_tab [ tabpage ]
567+ function View :winid (tabid )
568+ local bufnr = self .bufnr_by_tabid [ tabid ]
562569
563570 if bufnr then
564- for _ , winid in pairs (vim .api .nvim_tabpage_list_wins (tabpage or 0 )) do
571+ for _ , winid in pairs (vim .api .nvim_tabpage_list_wins (tabid or 0 )) do
565572 if vim .api .nvim_win_get_buf (winid ) == bufnr then
566573 return winid
567574 end
@@ -570,22 +577,22 @@ function View:winid(tabpage)
570577end
571578
572579--- Returns the window number for nvim-tree within the tabpage specified
573- --- @param tabpage number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
580+ --- @param tabid number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
574581--- @param callsite string
575582--- @return number | nil
576- function View :get_winid (tabpage , callsite )
577- local tabid = tabpage or vim .api .nvim_get_current_tabpage ()
583+ function View :get_winid (tabid , callsite )
584+ local tabid_param = tabid
585+ tabid = tabid or vim .api .nvim_get_current_tabpage ()
578586 local tabinfo_winid = nil
579587
580588 if self .explorer .opts .experimental .multi_instance then
581-
582589 local msg_fault = " "
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 ])
590+ if not globals .WINID_BY_TABID [tabid ] then
591+ msg_fault = " no WINID_BY_TABID "
592+ elseif not vim .api .nvim_win_is_valid (globals .WINID_BY_TABID [tabid ]) then
593+ msg_fault = string.format (" invalid globals.WINID_BY_TABID [tabid] %d" , globals .WINID_BY_TABID [tabid ])
587594 else
588- tabinfo_winid = globals .WINID_PER_TAB [tabid ]
595+ tabinfo_winid = globals .WINID_BY_TABID [tabid ]
589596 end
590597
591598 local winid = self :winid (tabid )
@@ -595,7 +602,7 @@ function View:get_winid(tabpage, callsite)
595602 end
596603
597604 local msg = string.format (" View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s]=w%s view.winid(%s)=w%s %s" ,
598- tabpage ,
605+ tabid_param ,
599606 callsite ,
600607 tabid , tabinfo_winid ,
601608 tabid , winid ,
@@ -621,20 +628,20 @@ end
621628function View :get_bufnr (callsite )
622629 local tab = vim .api .nvim_get_current_tabpage ()
623630 if self .explorer .opts .experimental .multi_instance then
624- local msg = string.format (" View:get_bufnr(%-20.20s) globals.BUFNR_PER_TAB [%s]=b%s view.bufnr_by_tab[%s]=b%s %s" ,
631+ local msg = string.format (" View:get_bufnr(%-20.20s) globals.BUFNR_BY_TABID [%s]=b%s view.bufnr_by_tab[%s]=b%s %s" ,
625632 callsite ,
626- tab , globals .BUFNR_PER_TAB [tab ],
627- tab , self .bufnr_by_tab [tab ],
628- (globals .BUFNR_PER_TAB [tab ] == self .bufnr_by_tab [tab ]) and " " or " MISMATCH"
633+ tab , globals .BUFNR_BY_TABID [tab ],
634+ tab , self .bufnr_by_tabid [tab ],
635+ (globals .BUFNR_BY_TABID [tab ] == self .bufnr_by_tabid [tab ]) and " " or " MISMATCH"
629636 )
630637
631- if globals .BUFNR_PER_TAB [tab ] ~= self .bufnr_by_tab [tab ] then
638+ if globals .BUFNR_BY_TABID [tab ] ~= self .bufnr_by_tabid [tab ] then
632639 notify .error (msg )
633640 end
634641
635642 log .line (" dev" , msg )
636643 end
637- return globals .BUFNR_PER_TAB [tab ]
644+ return globals .BUFNR_BY_TABID [tab ]
638645end
639646
640647function View :prevent_buffer_override ()
@@ -652,9 +659,9 @@ function View:prevent_buffer_override()
652659
653660 --- TODO multi-instance this can be removed as winid() will handle it
654661 if not bufname :match (" NvimTree" ) then
655- for i , winid in ipairs (globals .WINID_PER_TAB ) do
662+ for i , winid in ipairs (globals .WINID_BY_TABID ) do
656663 if winid == view_winid then
657- globals .WINID_PER_TAB [i ] = nil
664+ globals .WINID_BY_TABID [i ] = nil
658665 break
659666 end
660667 end
0 commit comments