@@ -10,18 +10,25 @@ local Class = require("nvim-tree.classic")
1010--- @field resize boolean | nil default true
1111--- @field winid number | nil 0 or nil for current
1212
13+ local M = {}
14+
1315local DEFAULT_MIN_WIDTH = 30
1416local DEFAULT_MAX_WIDTH = - 1
1517local DEFAULT_PADDING = 1
1618
19+ -- TODO global, rework for multiinstance explorer
20+ -- M.View retained for simpler change history
21+ M .View = {
22+ tabpages = {}
23+ }
24+
1725--- @class (exact ) View : Class
1826--- @field live_filter table
1927--- @field side string
2028--- @field float table
2129--- @field private explorer Explorer
2230--- @field private adaptive_size boolean
2331--- @field private centralize_selection boolean
24- --- @field private tabpages table
2532--- @field private cursors table<integer , integer[]> as per vim.api.nvim_win_get_cursor
2633--- @field private hide_root_folder boolean
2734--- @field private winopts table
@@ -31,7 +38,6 @@ local DEFAULT_PADDING = 1
3138--- @field private width (fun (): integer )| integer | string
3239--- @field private max_width integer
3340--- @field private padding integer
34- --- @field private bufnr_per_tab table<integer , integer>
3541local View = Class :extend ()
3642
3743--- @class View
@@ -43,17 +49,17 @@ local View = Class:extend()
4349--- @protected
4450--- @param args ViewArgs
4551function View :new (args )
52+ args .explorer :log_lifecycle (" View:new" )
53+
4654 self .explorer = args .explorer
4755 self .adaptive_size = false
48- self .bufnr_per_tab = {}
4956 self .centralize_selection = self .explorer .opts .view .centralize_selection
5057 self .cursors = {}
5158 self .float = self .explorer .opts .view .float
5259 self .height = self .explorer .opts .view .height
5360 self .hide_root_folder = self .explorer .opts .renderer .root_folder_label == false
5461 self .preserve_window_proportions = self .explorer .opts .view .preserve_window_proportions
5562 self .side = (self .explorer .opts .view .side == " right" ) and " right" or " left"
56- self .tabpages = {}
5763 self .live_filter = { prev_focused_node = nil , }
5864
5965 self .winopts = {
@@ -100,6 +106,10 @@ local tabinitial = {
100106 winnr = nil ,
101107}
102108
109+ -- TODO global, rework for multiinstance explorer
110+ --- @type table<integer , integer>
111+ local BUFNR_PER_TAB = {}
112+
103113--- @type { name : string , value : any } []
104114local BUFFER_OPTIONS = {
105115 { name = " bufhidden" , value = " wipe" },
@@ -114,7 +124,7 @@ local BUFFER_OPTIONS = {
114124--- @param bufnr integer
115125--- @return boolean
116126function View :matches_bufnr (bufnr )
117- for _ , b in pairs (self . bufnr_per_tab ) do
127+ for _ , b in pairs (BUFNR_PER_TAB ) do
118128 if b == bufnr then
119129 return true
120130 end
@@ -137,7 +147,7 @@ function View:create_buffer(bufnr)
137147 self :wipe_rogue_buffer ()
138148
139149 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 )
141151 vim .api .nvim_buf_set_name (self :get_bufnr (), " NvimTree_" .. tab )
142152
143153 bufnr = self :get_bufnr ()
@@ -184,7 +194,7 @@ local move_tbl = {
184194--- @param tabpage integer
185195function View :setup_tabpage (tabpage )
186196 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 })
188198end
189199
190200--- @private
@@ -308,7 +318,7 @@ function View:close_this_tab_only()
308318end
309319
310320function View :close_all_tabs ()
311- for tabpage , _ in pairs (self .tabpages ) do
321+ for tabpage , _ in pairs (M . View .tabpages ) do
312322 self :close_internal (tabpage )
313323 end
314324end
447457--- @private
448458function View :set_current_win ()
449459 local current_tab = vim .api .nvim_get_current_tabpage ()
450- self .tabpages [current_tab ].winnr = vim .api .nvim_get_current_win ()
460+ M . View .tabpages [current_tab ].winnr = vim .api .nvim_get_current_win ()
451461end
452462
453463--- Open the tree in the a window
@@ -471,17 +481,17 @@ end
471481
472482function View :abandon_current_window ()
473483 local tab = vim .api .nvim_get_current_tabpage ()
474- self . bufnr_per_tab [tab ] = nil
475- if self .tabpages [tab ] then
476- self .tabpages [tab ].winnr = nil
484+ BUFNR_PER_TAB [tab ] = nil
485+ if M . View .tabpages [tab ] then
486+ M . View .tabpages [tab ].winnr = nil
477487 end
478488end
479489
480490function View :abandon_all_windows ()
481491 for tab , _ in pairs (vim .api .nvim_list_tabpages ()) do
482- self . bufnr_per_tab [tab ] = nil
483- if self .tabpages [tab ] then
484- self .tabpages [tab ].winnr = nil
492+ BUFNR_PER_TAB [tab ] = nil
493+ if M . View .tabpages [tab ] then
494+ M . View .tabpages [tab ].winnr = nil
485495 end
486496 end
487497end
@@ -490,15 +500,15 @@ end
490500--- @return boolean
491501function View :is_visible (opts )
492502 if opts and opts .tabpage then
493- if self .tabpages [opts .tabpage ] == nil then
503+ if M . View .tabpages [opts .tabpage ] == nil then
494504 return false
495505 end
496- local winnr = self .tabpages [opts .tabpage ].winnr
506+ local winnr = M . View .tabpages [opts .tabpage ].winnr
497507 return winnr and vim .api .nvim_win_is_valid (winnr )
498508 end
499509
500510 if opts and opts .any_tabpage then
501- for _ , v in pairs (self .tabpages ) do
511+ for _ , v in pairs (M . View .tabpages ) do
502512 if v .winnr and vim .api .nvim_win_is_valid (v .winnr ) then
503513 return true
504514 end
560570--- @return number | nil
561571function View :get_winnr (tabpage )
562572 tabpage = tabpage or vim .api .nvim_get_current_tabpage ()
563- local tabinfo = self .tabpages [tabpage ]
573+ local tabinfo = M . View .tabpages [tabpage ]
564574 if tabinfo and tabinfo .winnr and vim .api .nvim_win_is_valid (tabinfo .winnr ) then
565575 return tabinfo .winnr
566576 end
569579--- Returns the current nvim tree bufnr
570580--- @return number
571581function View :get_bufnr ()
572- return self . bufnr_per_tab [vim .api .nvim_get_current_tabpage ()]
582+ return BUFNR_PER_TAB [vim .api .nvim_get_current_tabpage ()]
573583end
574584
575585function View :prevent_buffer_override ()
@@ -586,9 +596,9 @@ function View:prevent_buffer_override()
586596 local bufname = vim .api .nvim_buf_get_name (curbuf )
587597
588598 if not bufname :match (" NvimTree" ) then
589- for i , tabpage in ipairs (self .tabpages ) do
599+ for i , tabpage in ipairs (M . View .tabpages ) do
590600 if tabpage .winnr == view_winnr then
591- self .tabpages [i ] = nil
601+ M . View .tabpages [i ] = nil
592602 break
593603 end
594604 end
0 commit comments