@@ -3,6 +3,7 @@ local events = require("nvim-tree.events")
33local utils = require (" nvim-tree.utils" )
44local log = require (" nvim-tree.log" )
55local notify = require (" nvim-tree.notify" )
6+ local globals = require (" nvim-tree.globals" )
67
78local Class = require (" nvim-tree.classic" )
89
@@ -11,26 +12,17 @@ local Class = require("nvim-tree.classic")
1112--- @field resize boolean | nil default true
1213--- @field winid number | nil 0 or nil for current
1314
14- local M = {}
15-
1615local DEFAULT_MIN_WIDTH = 30
1716local DEFAULT_MAX_WIDTH = - 1
1817local DEFAULT_PADDING = 1
1918
20- -- TODO global, rework for multiinstance explorer
21- -- M.View retained for simpler change history
22- M .View = {
23- tabpages = {}
24- }
25-
2619--- @class (exact ) View : Class
2720--- @field live_filter table
2821--- @field side string
2922--- @field float table
3023--- @field private explorer Explorer
3124--- @field private adaptive_size boolean
3225--- @field private centralize_selection boolean
33- --- @field private cursors table<integer , integer[]> as per vim.api.nvim_win_get_cursor
3426--- @field private hide_root_folder boolean
3527--- @field private winopts table
3628--- @field private height integer
@@ -39,7 +31,6 @@ M.View = {
3931--- @field private width (fun (): integer )| integer | string
4032--- @field private max_width integer
4133--- @field private padding integer
42- --- @field tab_line fun (): string
4334local View = Class :extend ()
4435
4536--- @class View
@@ -56,7 +47,6 @@ function View:new(args)
5647 self .explorer = args .explorer
5748 self .adaptive_size = false
5849 self .centralize_selection = self .explorer .opts .view .centralize_selection
59- self .cursors = {}
6050 self .float = self .explorer .opts .view .float
6151 self .height = self .explorer .opts .view .height
6252 self .hide_root_folder = self .explorer .opts .renderer .root_folder_label == false
@@ -99,10 +89,6 @@ local tabinitial = {
9989 winnr = nil ,
10090}
10191
102- -- TODO global, rework for multiinstance explorer
103- --- @type table<integer , integer>
104- local BUFNR_PER_TAB = {}
105-
10692--- @type { name : string , value : any } []
10793local BUFFER_OPTIONS = {
10894 { name = " bufhidden" , value = " wipe" },
@@ -117,7 +103,7 @@ local BUFFER_OPTIONS = {
117103--- @param bufnr integer
118104--- @return boolean
119105function View :matches_bufnr (bufnr )
120- for _ , b in pairs (BUFNR_PER_TAB ) do
106+ for _ , b in pairs (globals . BUFNR_PER_TAB ) do
121107 if b == bufnr then
122108 return true
123109 end
@@ -140,7 +126,7 @@ function View:create_buffer(bufnr)
140126 self :wipe_rogue_buffer ()
141127
142128 local tab = vim .api .nvim_get_current_tabpage ()
143- BUFNR_PER_TAB [tab ] = bufnr or vim .api .nvim_create_buf (false , false )
129+ globals . BUFNR_PER_TAB [tab ] = bufnr or vim .api .nvim_create_buf (false , false )
144130 vim .api .nvim_buf_set_name (self :get_bufnr (), " NvimTree_" .. tab )
145131
146132 bufnr = self :get_bufnr ()
@@ -187,7 +173,7 @@ local move_tbl = {
187173--- @param tabpage integer
188174function View :setup_tabpage (tabpage )
189175 local winnr = vim .api .nvim_get_current_win ()
190- M . View . tabpages [tabpage ] = vim .tbl_extend (" force" , M . View . tabpages [tabpage ] or tabinitial , { winnr = winnr })
176+ globals . TABPAGES [tabpage ] = vim .tbl_extend (" force" , globals . TABPAGES [tabpage ] or tabinitial , { winnr = winnr })
191177end
192178
193179--- @private
275261--- @param tabnr integer
276262function View :save_tab_state (tabnr )
277263 local tabpage = tabnr or vim .api .nvim_get_current_tabpage ()
278- self . cursors [tabpage ] = vim .api .nvim_win_get_cursor (self :get_winnr (tabpage ) or 0 )
264+ globals . CURSORS [tabpage ] = vim .api .nvim_win_get_cursor (self :get_winnr (tabpage ) or 0 )
279265end
280266
281267--- @private
@@ -311,7 +297,7 @@ function View:close_this_tab_only()
311297end
312298
313299function View :close_all_tabs ()
314- for tabpage , _ in pairs (M . View . tabpages ) do
300+ for tabpage , _ in pairs (globals . TABPAGES ) do
315301 self :close_internal (tabpage )
316302 end
317303end
450436--- @private
451437function View :set_current_win ()
452438 local current_tab = vim .api .nvim_get_current_tabpage ()
453- M . View . tabpages [current_tab ].winnr = vim .api .nvim_get_current_win ()
439+ globals . TABPAGES [current_tab ].winnr = vim .api .nvim_get_current_win ()
454440end
455441
456442--- Open the tree in the a window
@@ -474,17 +460,17 @@ end
474460
475461function View :abandon_current_window ()
476462 local tab = vim .api .nvim_get_current_tabpage ()
477- BUFNR_PER_TAB [tab ] = nil
478- if M . View . tabpages [tab ] then
479- M . View . tabpages [tab ].winnr = nil
463+ globals . BUFNR_PER_TAB [tab ] = nil
464+ if globals . TABPAGES [tab ] then
465+ globals . TABPAGES [tab ].winnr = nil
480466 end
481467end
482468
483469function View :abandon_all_windows ()
484470 for tab , _ in pairs (vim .api .nvim_list_tabpages ()) do
485- BUFNR_PER_TAB [tab ] = nil
486- if M . View . tabpages [tab ] then
487- M . View . tabpages [tab ].winnr = nil
471+ globals . BUFNR_PER_TAB [tab ] = nil
472+ if globals . TABPAGES [tab ] then
473+ globals . TABPAGES [tab ].winnr = nil
488474 end
489475 end
490476end
@@ -493,15 +479,15 @@ end
493479--- @return boolean
494480function View :is_visible (opts )
495481 if opts and opts .tabpage then
496- if M . View . tabpages [opts .tabpage ] == nil then
482+ if globals . TABPAGES [opts .tabpage ] == nil then
497483 return false
498484 end
499- local winnr = M . View . tabpages [opts .tabpage ].winnr
485+ local winnr = globals . TABPAGES [opts .tabpage ].winnr
500486 return winnr and vim .api .nvim_win_is_valid (winnr )
501487 end
502488
503489 if opts and opts .any_tabpage then
504- for _ , v in pairs (M . View . tabpages ) do
490+ for _ , v in pairs (globals . TABPAGES ) do
505491 if v .winnr and vim .api .nvim_win_is_valid (v .winnr ) then
506492 return true
507493 end
@@ -555,15 +541,15 @@ end
555541--- Restores the state of a NvimTree window if it was initialized before.
556542function View :restore_tab_state ()
557543 local tabpage = vim .api .nvim_get_current_tabpage ()
558- self :set_cursor (self . cursors [tabpage ])
544+ self :set_cursor (globals . CURSORS [tabpage ])
559545end
560546
561547--- Returns the window number for nvim-tree within the tabpage specified
562548--- @param tabpage number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
563549--- @return number | nil
564550function View :get_winnr (tabpage )
565551 tabpage = tabpage or vim .api .nvim_get_current_tabpage ()
566- local tabinfo = M . View . tabpages [tabpage ]
552+ local tabinfo = globals . TABPAGES [tabpage ]
567553 if tabinfo and tabinfo .winnr and vim .api .nvim_win_is_valid (tabinfo .winnr ) then
568554 return tabinfo .winnr
569555 end
572558--- Returns the current nvim tree bufnr
573559--- @return number
574560function View :get_bufnr ()
575- return BUFNR_PER_TAB [vim .api .nvim_get_current_tabpage ()]
561+ return globals . BUFNR_PER_TAB [vim .api .nvim_get_current_tabpage ()]
576562end
577563
578564function View :prevent_buffer_override ()
@@ -589,9 +575,9 @@ function View:prevent_buffer_override()
589575 local bufname = vim .api .nvim_buf_get_name (curbuf )
590576
591577 if not bufname :match (" NvimTree" ) then
592- for i , tabpage in ipairs (M . View . tabpages ) do
578+ for i , tabpage in ipairs (globals . TABPAGES ) do
593579 if tabpage .winnr == view_winnr then
594- M . View . tabpages [i ] = nil
580+ globals . TABPAGES [i ] = nil
595581 break
596582 end
597583 end
@@ -665,102 +651,4 @@ function View:configure_width(width)
665651 end
666652end
667653
668- --- Debugging only.
669- --- Tabs show TABPAGES winnr and BUFNR_PER_TAB bufnr for the tab.
670- --- Orphans for inexistent tab_ids are shown at the right.
671- --- lib.target_winid is always shown at the right next to a close button.
672- --- Enable with:
673- --- vim.opt.tabline = "%!v:lua.require('nvim-tree.explorer.view').tab_line()"
674- --- vim.opt.showtabline = 2
675- --- @return string
676- function View .tab_line ()
677- local tab_ids = vim .api .nvim_list_tabpages ()
678- local cur_tab_id = vim .api .nvim_get_current_tabpage ()
679-
680- local bufnr_per_tab = vim .deepcopy (BUFNR_PER_TAB )
681- local tabpages = vim .deepcopy (M .View .tabpages )
682-
683- local tl = " %#TabLine#"
684-
685- for i , tab_id in ipairs (tab_ids ) do
686- -- click to select
687- tl = tl .. " %" .. i .. " T"
688-
689- -- style
690- if tab_id == cur_tab_id then
691- tl = tl .. " %#StatusLine#|"
692- else
693- tl = tl .. " |%#TabLine#"
694- end
695-
696- -- tab_id itself
697- tl = tl .. " t" .. tab_id
698-
699- -- winnr, if present
700- local tp = M .View .tabpages [tab_id ]
701- if tp then
702- tl = tl .. " w" .. tp .winnr
703- else
704- tl = tl .. " "
705- end
706-
707- -- bufnr, if present
708- local bpt = BUFNR_PER_TAB [tab_id ]
709- if bpt then
710- tl = tl .. " b" .. bpt
711- else
712- tl = tl .. " "
713- end
714-
715- tl = tl .. " "
716-
717- -- remove actively mapped
718- bufnr_per_tab [tab_id ] = nil
719- tabpages [tab_id ] = nil
720- end
721-
722- -- close last and reset
723- tl = tl .. " |%#CursorLine#%T"
724-
725- -- collect orphans
726- local orphans = {}
727- for tab_id , bufnr in pairs (bufnr_per_tab ) do
728- orphans [tab_id ] = orphans [tab_id ] or {}
729- orphans [tab_id ].bufnr = bufnr
730- end
731- for tab_id , tp in pairs (tabpages ) do
732- orphans [tab_id ] = orphans [tab_id ] or {}
733- orphans [tab_id ].winnr = tp .winnr
734- end
735-
736- -- right-align
737- tl = tl .. " %=%#TabLine#"
738-
739- -- print orphans
740- for tab_id , orphan in pairs (orphans ) do
741- -- inexistent tab
742- tl = tl .. " %#error#| t" .. tab_id
743-
744- -- maybe winnr
745- if orphan .winnr then
746- tl = tl .. " w" .. orphan .winnr
747- else
748- tl = tl .. " "
749- end
750-
751- -- maybe bufnr
752- if orphan .bufnr then
753- tl = tl .. " b" .. orphan .bufnr
754- else
755- tl = tl .. " "
756- end
757- tl = tl .. " "
758- end
759-
760- -- target win id and close button
761- tl = tl .. " |%#TabLine# twi" .. (require (" nvim-tree.lib" ).target_winid or " ?" ) .. " %999X| X |"
762-
763- return tl
764- end
765-
766654return View
0 commit comments