@@ -69,14 +69,6 @@ function View:destroy()
69
69
self .explorer :log_destroy (" View" )
70
70
end
71
71
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
-
80
72
--- @type { name : string , value : any } []
81
73
local BUFFER_OPTIONS = {
82
74
{ name = " bufhidden" , value = " wipe" },
@@ -163,24 +155,6 @@ local move_tbl = {
163
155
right = " L" ,
164
156
}
165
157
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
-
184
158
--- @private
185
159
function View :set_window_options_and_buffer ()
186
160
pcall (vim .api .nvim_command , " buffer " .. self :get_bufnr (" View:set_window_options_and_buffer" ))
@@ -226,7 +200,7 @@ function View:open_window()
226
200
vim .api .nvim_command (" vsp" )
227
201
self :reposition_window ()
228
202
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 ( )
230
204
self :set_window_options_and_buffer ()
231
205
end
232
206
@@ -308,7 +282,7 @@ function View:close_this_tab_only()
308
282
end
309
283
310
284
function View :close_all_tabs ()
311
- for tabpage , _ in pairs (globals .TABPAGES ) do
285
+ for tabpage , _ in pairs (globals .WINID_PER_TAB ) do
312
286
self :close_internal (tabpage )
313
287
end
314
288
end
@@ -449,22 +423,9 @@ function View:reposition_window()
449
423
end
450
424
451
425
--- @private
452
- --- @param callsite string
453
- function View :set_current_win (callsite )
426
+ function View :set_current_win ()
454
427
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 ()
468
429
end
469
430
470
431
--- @class OpenInWinOpts
@@ -481,8 +442,8 @@ function View:open_in_win(opts)
481
442
vim .api .nvim_set_current_win (opts .winid )
482
443
end
483
444
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 ()
486
447
self :set_window_options_and_buffer ()
487
448
if opts .resize then
488
449
self :reposition_window ()
@@ -497,7 +458,7 @@ function View:abandon_current_window()
497
458
if self .explorer .opts .experimental .multi_instance then
498
459
log .line (" dev" , " View:abandon_current_window() t%d w%s b%s member b%s %s" ,
499
460
tab ,
500
- globals .TABPAGES [tab ] and globals . TABPAGES [ tab ]. winid or nil ,
461
+ globals .WINID_PER_TAB [tab ],
501
462
globals .BUFNR_PER_TAB [tab ],
502
463
self .bufnr_by_tab [tab ],
503
464
(globals .BUFNR_PER_TAB [tab ] == self .bufnr_by_tab [tab ]) and " " or " MISMATCH" )
@@ -509,30 +470,14 @@ function View:abandon_current_window()
509
470
globals .BUFNR_PER_TAB [tab ] = nil
510
471
self .bufnr_by_tab [tab ] = nil
511
472
512
- if globals .TABPAGES [tab ] then
513
- globals .TABPAGES [tab ].winid = nil
514
- end
473
+ globals .WINID_PER_TAB [tab ] = nil
515
474
end
516
475
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
519
478
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
-
532
479
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
536
481
end
537
482
end
538
483
@@ -541,16 +486,16 @@ end
541
486
function View :is_visible (opts )
542
487
-- TODO multi-instance rewrite and consistency check
543
488
if opts and opts .tabpage then
544
- if globals .TABPAGES [opts .tabpage ] == nil then
489
+ if not globals .WINID_PER_TAB [opts .tabpage ] then
545
490
return false
546
491
end
547
- local winid = globals .TABPAGES [opts .tabpage ]. winid
492
+ local winid = globals .WINID_PER_TAB [opts .tabpage ]
548
493
return winid and vim .api .nvim_win_is_valid (winid )
549
494
end
550
495
551
496
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
554
499
return true
555
500
end
556
501
end
@@ -611,22 +556,16 @@ end
611
556
--- not legacy codepath
612
557
--- winid containing the buffer
613
558
--- @param tabpage number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
614
- --- @param callsite string
615
559
--- @return integer ? winid
616
- function View :winid (tabpage , callsite )
560
+ function View :winid (tabpage )
617
561
local bufnr = self .bufnr_by_tab [tabpage ]
618
562
619
- local msg = string.format (" View:winid(%3s, %-20.20s)" , tabpage , callsite )
620
-
621
563
if bufnr then
622
564
for _ , winid in pairs (vim .api .nvim_tabpage_list_wins (tabpage or 0 )) do
623
565
if vim .api .nvim_win_get_buf (winid ) == bufnr then
624
- log .line (" dev" , " %s b%d : w%s" , msg , bufnr , winid )
625
566
return winid
626
567
end
627
568
end
628
- else
629
- log .line (" dev" , " %s no bufnr" , msg )
630
569
end
631
570
end
632
571
@@ -636,28 +575,26 @@ end
636
575
--- @return number | nil
637
576
function View :get_winid (tabpage , callsite )
638
577
local tabid = tabpage or vim .api .nvim_get_current_tabpage ()
639
- local tabinfo = globals .TABPAGES [tabid ]
640
578
local tabinfo_winid = nil
641
579
642
580
if self .explorer .opts .experimental .multi_instance then
581
+
643
582
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 ])
650
587
else
651
- tabinfo_winid = tabinfo . winid
588
+ tabinfo_winid = globals . WINID_PER_TAB [ tabid ]
652
589
end
653
590
654
- local winid = self :winid (tabid , " View:get_winid " )
591
+ local winid = self :winid (tabid )
655
592
656
593
if winid ~= tabinfo_winid then
657
594
msg_fault = " MISMATCH"
658
595
end
659
596
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" ,
661
598
tabpage ,
662
599
callsite ,
663
600
tabid , tabinfo_winid ,
@@ -670,13 +607,11 @@ function View:get_winid(tabpage, callsite)
670
607
if winid ~= tabinfo_winid then
671
608
notify .error (msg )
672
609
end
673
-
674
- return winid
675
610
end
676
611
677
612
-- 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
680
615
end
681
616
end
682
617
@@ -717,13 +652,9 @@ function View:prevent_buffer_override()
717
652
718
653
--- TODO multi-instance this can be removed as winid() will handle it
719
654
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
727
658
break
728
659
end
729
660
end
0 commit comments