@@ -10,18 +10,25 @@ local Class = require("nvim-tree.classic")
10
10
--- @field resize boolean | nil default true
11
11
--- @field winid number | nil 0 or nil for current
12
12
13
+ local M = {}
14
+
13
15
local DEFAULT_MIN_WIDTH = 30
14
16
local DEFAULT_MAX_WIDTH = - 1
15
17
local DEFAULT_PADDING = 1
16
18
19
+ -- TODO global, rework for multiinstance explorer
20
+ -- M.View retained for simpler change history
21
+ M .View = {
22
+ tabpages = {}
23
+ }
24
+
17
25
--- @class (exact ) View : Class
18
26
--- @field live_filter table
19
27
--- @field side string
20
28
--- @field float table
21
29
--- @field private explorer Explorer
22
30
--- @field private adaptive_size boolean
23
31
--- @field private centralize_selection boolean
24
- --- @field private tabpages table
25
32
--- @field private cursors table<integer , integer[]> as per vim.api.nvim_win_get_cursor
26
33
--- @field private hide_root_folder boolean
27
34
--- @field private winopts table
@@ -31,7 +38,6 @@ local DEFAULT_PADDING = 1
31
38
--- @field private width (fun (): integer )| integer | string
32
39
--- @field private max_width integer
33
40
--- @field private padding integer
34
- --- @field private bufnr_per_tab table<integer , integer>
35
41
local View = Class :extend ()
36
42
37
43
--- @class View
@@ -43,17 +49,17 @@ local View = Class:extend()
43
49
--- @protected
44
50
--- @param args ViewArgs
45
51
function View :new (args )
52
+ args .explorer :log_lifecycle (" View:new" )
53
+
46
54
self .explorer = args .explorer
47
55
self .adaptive_size = false
48
- self .bufnr_per_tab = {}
49
56
self .centralize_selection = self .explorer .opts .view .centralize_selection
50
57
self .cursors = {}
51
58
self .float = self .explorer .opts .view .float
52
59
self .height = self .explorer .opts .view .height
53
60
self .hide_root_folder = self .explorer .opts .renderer .root_folder_label == false
54
61
self .preserve_window_proportions = self .explorer .opts .view .preserve_window_proportions
55
62
self .side = (self .explorer .opts .view .side == " right" ) and " right" or " left"
56
- self .tabpages = {}
57
63
self .live_filter = { prev_focused_node = nil , }
58
64
59
65
self .winopts = {
@@ -100,6 +106,10 @@ local tabinitial = {
100
106
winnr = nil ,
101
107
}
102
108
109
+ -- TODO global, rework for multiinstance explorer
110
+ --- @type table<integer , integer>
111
+ local BUFNR_PER_TAB = {}
112
+
103
113
--- @type { name : string , value : any } []
104
114
local BUFFER_OPTIONS = {
105
115
{ name = " bufhidden" , value = " wipe" },
@@ -114,7 +124,7 @@ local BUFFER_OPTIONS = {
114
124
--- @param bufnr integer
115
125
--- @return boolean
116
126
function View :matches_bufnr (bufnr )
117
- for _ , b in pairs (self . bufnr_per_tab ) do
127
+ for _ , b in pairs (BUFNR_PER_TAB ) do
118
128
if b == bufnr then
119
129
return true
120
130
end
@@ -137,7 +147,7 @@ function View:create_buffer(bufnr)
137
147
self :wipe_rogue_buffer ()
138
148
139
149
local tab = vim .api .nvim_get_current_tabpage ()
140
- self . bufnr_per_tab [tab ] = bufnr or vim .api .nvim_create_buf (false , false )
150
+ BUFNR_PER_TAB [tab ] = bufnr or vim .api .nvim_create_buf (false , false )
141
151
vim .api .nvim_buf_set_name (self :get_bufnr (), " NvimTree_" .. tab )
142
152
143
153
bufnr = self :get_bufnr ()
@@ -184,7 +194,7 @@ local move_tbl = {
184
194
--- @param tabpage integer
185
195
function View :setup_tabpage (tabpage )
186
196
local winnr = vim .api .nvim_get_current_win ()
187
- self . tabpages [tabpage ] = vim .tbl_extend (" force" , self .tabpages [tabpage ] or tabinitial , { winnr = winnr })
197
+ M . View . tabpages [tabpage ] = vim .tbl_extend (" force" , M . View .tabpages [tabpage ] or tabinitial , { winnr = winnr })
188
198
end
189
199
190
200
--- @private
@@ -308,7 +318,7 @@ function View:close_this_tab_only()
308
318
end
309
319
310
320
function View :close_all_tabs ()
311
- for tabpage , _ in pairs (self .tabpages ) do
321
+ for tabpage , _ in pairs (M . View .tabpages ) do
312
322
self :close_internal (tabpage )
313
323
end
314
324
end
454
464
--- @private
455
465
function View :set_current_win ()
456
466
local current_tab = vim .api .nvim_get_current_tabpage ()
457
- self .tabpages [current_tab ].winnr = vim .api .nvim_get_current_win ()
467
+ M . View .tabpages [current_tab ].winnr = vim .api .nvim_get_current_win ()
458
468
end
459
469
460
470
--- Open the tree in the a window
@@ -478,17 +488,17 @@ end
478
488
479
489
function View :abandon_current_window ()
480
490
local tab = vim .api .nvim_get_current_tabpage ()
481
- self . bufnr_per_tab [tab ] = nil
482
- if self .tabpages [tab ] then
483
- self .tabpages [tab ].winnr = nil
491
+ BUFNR_PER_TAB [tab ] = nil
492
+ if M . View .tabpages [tab ] then
493
+ M . View .tabpages [tab ].winnr = nil
484
494
end
485
495
end
486
496
487
497
function View :abandon_all_windows ()
488
498
for tab , _ in pairs (vim .api .nvim_list_tabpages ()) do
489
- self . bufnr_per_tab [tab ] = nil
490
- if self .tabpages [tab ] then
491
- self .tabpages [tab ].winnr = nil
499
+ BUFNR_PER_TAB [tab ] = nil
500
+ if M . View .tabpages [tab ] then
501
+ M . View .tabpages [tab ].winnr = nil
492
502
end
493
503
end
494
504
end
@@ -497,15 +507,15 @@ end
497
507
--- @return boolean
498
508
function View :is_visible (opts )
499
509
if opts and opts .tabpage then
500
- if self .tabpages [opts .tabpage ] == nil then
510
+ if M . View .tabpages [opts .tabpage ] == nil then
501
511
return false
502
512
end
503
- local winnr = self .tabpages [opts .tabpage ].winnr
513
+ local winnr = M . View .tabpages [opts .tabpage ].winnr
504
514
return winnr and vim .api .nvim_win_is_valid (winnr )
505
515
end
506
516
507
517
if opts and opts .any_tabpage then
508
- for _ , v in pairs (self .tabpages ) do
518
+ for _ , v in pairs (M . View .tabpages ) do
509
519
if v .winnr and vim .api .nvim_win_is_valid (v .winnr ) then
510
520
return true
511
521
end
567
577
--- @return number | nil
568
578
function View :get_winnr (tabpage )
569
579
tabpage = tabpage or vim .api .nvim_get_current_tabpage ()
570
- local tabinfo = self .tabpages [tabpage ]
580
+ local tabinfo = M . View .tabpages [tabpage ]
571
581
if tabinfo and tabinfo .winnr and vim .api .nvim_win_is_valid (tabinfo .winnr ) then
572
582
return tabinfo .winnr
573
583
end
576
586
--- Returns the current nvim tree bufnr
577
587
--- @return number
578
588
function View :get_bufnr ()
579
- return self . bufnr_per_tab [vim .api .nvim_get_current_tabpage ()]
589
+ return BUFNR_PER_TAB [vim .api .nvim_get_current_tabpage ()]
580
590
end
581
591
582
592
function View :prevent_buffer_override ()
@@ -593,9 +603,9 @@ function View:prevent_buffer_override()
593
603
local bufname = vim .api .nvim_buf_get_name (curbuf )
594
604
595
605
if not bufname :match (" NvimTree" ) then
596
- for i , tabpage in ipairs (self .tabpages ) do
606
+ for i , tabpage in ipairs (M . View .tabpages ) do
597
607
if tabpage .winnr == view_winnr then
598
- self .tabpages [i ] = nil
608
+ M . View .tabpages [i ] = nil
599
609
break
600
610
end
601
611
end
0 commit comments