@@ -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
454464--- @private
455465function View :set_current_win ()
456466 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 ()
458468end
459469
460470--- Open the tree in the a window
@@ -478,17 +488,17 @@ end
478488
479489function View :abandon_current_window ()
480490 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
484494 end
485495end
486496
487497function View :abandon_all_windows ()
488498 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
492502 end
493503 end
494504end
@@ -497,15 +507,15 @@ end
497507--- @return boolean
498508function View :is_visible (opts )
499509 if opts and opts .tabpage then
500- if self .tabpages [opts .tabpage ] == nil then
510+ if M . View .tabpages [opts .tabpage ] == nil then
501511 return false
502512 end
503- local winnr = self .tabpages [opts .tabpage ].winnr
513+ local winnr = M . View .tabpages [opts .tabpage ].winnr
504514 return winnr and vim .api .nvim_win_is_valid (winnr )
505515 end
506516
507517 if opts and opts .any_tabpage then
508- for _ , v in pairs (self .tabpages ) do
518+ for _ , v in pairs (M . View .tabpages ) do
509519 if v .winnr and vim .api .nvim_win_is_valid (v .winnr ) then
510520 return true
511521 end
567577--- @return number | nil
568578function View :get_winnr (tabpage )
569579 tabpage = tabpage or vim .api .nvim_get_current_tabpage ()
570- local tabinfo = self .tabpages [tabpage ]
580+ local tabinfo = M . View .tabpages [tabpage ]
571581 if tabinfo and tabinfo .winnr and vim .api .nvim_win_is_valid (tabinfo .winnr ) then
572582 return tabinfo .winnr
573583 end
576586--- Returns the current nvim tree bufnr
577587--- @return number
578588function 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 ()]
580590end
581591
582592function View :prevent_buffer_override ()
@@ -593,9 +603,9 @@ function View:prevent_buffer_override()
593603 local bufname = vim .api .nvim_buf_get_name (curbuf )
594604
595605 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
597607 if tabpage .winnr == view_winnr then
598- self .tabpages [i ] = nil
608+ M . View .tabpages [i ] = nil
599609 break
600610 end
601611 end
0 commit comments