@@ -18,7 +18,7 @@ local Class = require("nvim-tree.classic")
18
18
--- @field private width (fun (): integer )| integer | string
19
19
--- @field private max_width integer
20
20
--- @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
22
22
local View = Class :extend ()
23
23
24
24
--- @class View
@@ -36,7 +36,7 @@ function View:new(args)
36
36
self .adaptive_size = false
37
37
self .side = (self .explorer .opts .view .side == " right" ) and " right" or " left"
38
38
self .live_filter = { prev_focused_node = nil , }
39
- self .bufnr_by_tab = {}
39
+ self .bufnr_by_tabid = {}
40
40
41
41
self .winopts = {
42
42
relativenumber = self .explorer .opts .view .relativenumber ,
@@ -62,7 +62,7 @@ function View:new(args)
62
62
63
63
-- TODO multi-instance remove this; delete buffers rather than retaining them
64
64
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 ]
66
66
end
67
67
68
68
function View :destroy ()
@@ -84,7 +84,7 @@ local BUFFER_OPTIONS = {
84
84
--- @param bufnr integer
85
85
--- @return boolean
86
86
function View :matches_bufnr (bufnr )
87
- for _ , b in pairs (globals .BUFNR_PER_TAB ) do
87
+ for _ , b in pairs (globals .BUFNR_BY_TABID ) do
88
88
if b == bufnr then
89
89
return true
90
90
end
@@ -107,15 +107,15 @@ end
107
107
function View :create_buffer (bufnr )
108
108
self :wipe_rogue_buffer ()
109
109
110
- local tab = vim .api .nvim_get_current_tabpage ()
110
+ local tabid = vim .api .nvim_get_current_tabpage ()
111
111
112
112
bufnr = bufnr or vim .api .nvim_create_buf (false , false )
113
113
114
114
-- 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
117
117
118
- vim .api .nvim_buf_set_name (bufnr , " NvimTree_" .. tab )
118
+ vim .api .nvim_buf_set_name (bufnr , " NvimTree_" .. tabid )
119
119
120
120
for _ , option in ipairs (BUFFER_OPTIONS ) do
121
121
vim .api .nvim_set_option_value (option .name , option .value , { buf = bufnr })
@@ -200,7 +200,7 @@ function View:open_window()
200
200
vim .api .nvim_command (" vsp" )
201
201
self :reposition_window ()
202
202
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 ()
204
204
self :set_window_options_and_buffer ()
205
205
end
206
206
@@ -237,34 +237,34 @@ end
237
237
238
238
--- save_tab_state saves any state that should be preserved across redraws.
239
239
--- @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 )
244
244
end
245
245
246
246
--- @private
247
- --- @param tabpage integer
248
- function View :close_internal (tabpage )
247
+ --- @param tabid integer
248
+ function View :close_internal (tabid )
249
249
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 )
251
251
end
252
- if not self :is_visible ({ tabpage = tabpage }) then
252
+ if not self :is_visible ({ tabpage = tabid }) then
253
253
return
254
254
end
255
- self :save_tab_state (tabpage )
255
+ self :save_tab_state (tabid )
256
256
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" )
258
258
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
260
260
if vim .api .nvim_win_get_config (win ).relative == " " then
261
261
local prev_win = vim .fn .winnr (" #" ) -- this tab only
262
262
if tree_win == current_win and prev_win > 0 then
263
263
vim .api .nvim_set_current_win (vim .fn .win_getid (prev_win ))
264
264
end
265
265
if vim .api .nvim_win_is_valid (tree_win or 0 ) then
266
266
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 )
268
268
end
269
269
local success , error = pcall (vim .api .nvim_win_close , tree_win or 0 , true )
270
270
if not success then
@@ -278,26 +278,34 @@ function View:close_internal(tabpage)
278
278
end
279
279
280
280
function 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
281
284
self :close_internal (vim .api .nvim_get_current_tabpage ())
282
285
end
283
286
287
+ -- TODO this is broken at 1.13.0 - current tab does not close when tab.sync.close is set
284
288
function 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 )
287
295
end
288
296
end
289
297
290
- --- @param tabpage integer | nil
298
+ --- @param tabid integer | nil
291
299
--- @param callsite string
292
- function View :close (tabpage , callsite )
300
+ function View :close (tabid , callsite )
293
301
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 )
295
303
end
296
304
297
305
if self .explorer .opts .tab .sync .close then
298
306
self :close_all_tabs ()
299
- elseif tabpage then
300
- self :close_internal (tabpage )
307
+ elseif tabid then
308
+ self :close_internal (tabid )
301
309
else
302
310
self :close_this_tab_only ()
303
311
end
425
433
--- @private
426
434
function View :set_current_win ()
427
435
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 ()
429
437
end
430
438
431
439
--- @class OpenInWinOpts
@@ -442,7 +450,7 @@ function View:open_in_win(opts)
442
450
vim .api .nvim_set_current_win (opts .winid )
443
451
end
444
452
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 ()
446
454
self :set_current_win ()
447
455
self :set_window_options_and_buffer ()
448
456
if opts .resize then
@@ -458,26 +466,26 @@ function View:abandon_current_window()
458
466
if self .explorer .opts .experimental .multi_instance then
459
467
log .line (" dev" , " View:abandon_current_window() t%d w%s b%s member b%s %s" ,
460
468
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" )
465
473
end
466
474
467
475
-- TODO multi-instance maybe kill the buffer instead of retaining
468
476
469
477
-- 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
472
480
473
- globals .WINID_PER_TAB [tab ] = nil
481
+ globals .WINID_BY_TABID [tab ] = nil
474
482
end
475
483
476
484
function View :abandon_all_windows ()
477
485
-- TODO multi-instance kill the buffer instead of retaining
478
486
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
481
489
end
482
490
end
483
491
@@ -486,15 +494,15 @@ end
486
494
function View :is_visible (opts )
487
495
-- TODO multi-instance rewrite and consistency check
488
496
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
490
498
return false
491
499
end
492
- local winid = globals .WINID_PER_TAB [opts .tabpage ]
500
+ local winid = globals .WINID_BY_TABID [opts .tabpage ]
493
501
return winid and vim .api .nvim_win_is_valid (winid )
494
502
end
495
503
496
504
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
498
506
if winid and vim .api .nvim_win_is_valid (winid ) then
499
507
return true
500
508
end
@@ -548,20 +556,19 @@ end
548
556
549
557
--- Restores the state of a NvimTree window if it was initialized before.
550
558
function 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 ()])
553
560
end
554
561
555
562
--- TODO multi-instance remove comment
556
563
--- not legacy codepath
557
564
--- 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.
559
566
--- @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 ]
562
569
563
570
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
565
572
if vim .api .nvim_win_get_buf (winid ) == bufnr then
566
573
return winid
567
574
end
@@ -570,22 +577,22 @@ function View:winid(tabpage)
570
577
end
571
578
572
579
--- 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.
574
581
--- @param callsite string
575
582
--- @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 ()
578
586
local tabinfo_winid = nil
579
587
580
588
if self .explorer .opts .experimental .multi_instance then
581
-
582
589
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 ])
587
594
else
588
- tabinfo_winid = globals .WINID_PER_TAB [tabid ]
595
+ tabinfo_winid = globals .WINID_BY_TABID [tabid ]
589
596
end
590
597
591
598
local winid = self :winid (tabid )
@@ -595,7 +602,7 @@ function View:get_winid(tabpage, callsite)
595
602
end
596
603
597
604
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 ,
599
606
callsite ,
600
607
tabid , tabinfo_winid ,
601
608
tabid , winid ,
@@ -621,20 +628,20 @@ end
621
628
function View :get_bufnr (callsite )
622
629
local tab = vim .api .nvim_get_current_tabpage ()
623
630
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" ,
625
632
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"
629
636
)
630
637
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
632
639
notify .error (msg )
633
640
end
634
641
635
642
log .line (" dev" , msg )
636
643
end
637
- return globals .BUFNR_PER_TAB [tab ]
644
+ return globals .BUFNR_BY_TABID [tab ]
638
645
end
639
646
640
647
function View :prevent_buffer_override ()
@@ -652,9 +659,9 @@ function View:prevent_buffer_override()
652
659
653
660
--- TODO multi-instance this can be removed as winid() will handle it
654
661
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
656
663
if winid == view_winid then
657
- globals .WINID_PER_TAB [i ] = nil
664
+ globals .WINID_BY_TABID [i ] = nil
658
665
break
659
666
end
660
667
end
0 commit comments