@@ -3,6 +3,7 @@ local events = require("nvim-tree.events")
3
3
local utils = require (" nvim-tree.utils" )
4
4
local log = require (" nvim-tree.log" )
5
5
local notify = require (" nvim-tree.notify" )
6
+ local globals = require (" nvim-tree.globals" )
6
7
7
8
local Class = require (" nvim-tree.classic" )
8
9
@@ -11,26 +12,17 @@ local Class = require("nvim-tree.classic")
11
12
--- @field resize boolean | nil default true
12
13
--- @field winid number | nil 0 or nil for current
13
14
14
- local M = {}
15
-
16
15
local DEFAULT_MIN_WIDTH = 30
17
16
local DEFAULT_MAX_WIDTH = - 1
18
17
local DEFAULT_PADDING = 1
19
18
20
- -- TODO global, rework for multiinstance explorer
21
- -- M.View retained for simpler change history
22
- M .View = {
23
- tabpages = {}
24
- }
25
-
26
19
--- @class (exact ) View : Class
27
20
--- @field live_filter table
28
21
--- @field side string
29
22
--- @field float table
30
23
--- @field private explorer Explorer
31
24
--- @field private adaptive_size boolean
32
25
--- @field private centralize_selection boolean
33
- --- @field private cursors table<integer , integer[]> as per vim.api.nvim_win_get_cursor
34
26
--- @field private hide_root_folder boolean
35
27
--- @field private winopts table
36
28
--- @field private height integer
@@ -39,7 +31,6 @@ M.View = {
39
31
--- @field private width (fun (): integer )| integer | string
40
32
--- @field private max_width integer
41
33
--- @field private padding integer
42
- --- @field tab_line fun (): string
43
34
local View = Class :extend ()
44
35
45
36
--- @class View
@@ -56,7 +47,6 @@ function View:new(args)
56
47
self .explorer = args .explorer
57
48
self .adaptive_size = false
58
49
self .centralize_selection = self .explorer .opts .view .centralize_selection
59
- self .cursors = {}
60
50
self .float = self .explorer .opts .view .float
61
51
self .height = self .explorer .opts .view .height
62
52
self .hide_root_folder = self .explorer .opts .renderer .root_folder_label == false
@@ -99,10 +89,6 @@ local tabinitial = {
99
89
winnr = nil ,
100
90
}
101
91
102
- -- TODO global, rework for multiinstance explorer
103
- --- @type table<integer , integer>
104
- local BUFNR_PER_TAB = {}
105
-
106
92
--- @type { name : string , value : any } []
107
93
local BUFFER_OPTIONS = {
108
94
{ name = " bufhidden" , value = " wipe" },
@@ -117,7 +103,7 @@ local BUFFER_OPTIONS = {
117
103
--- @param bufnr integer
118
104
--- @return boolean
119
105
function View :matches_bufnr (bufnr )
120
- for _ , b in pairs (BUFNR_PER_TAB ) do
106
+ for _ , b in pairs (globals . BUFNR_PER_TAB ) do
121
107
if b == bufnr then
122
108
return true
123
109
end
@@ -140,7 +126,7 @@ function View:create_buffer(bufnr)
140
126
self :wipe_rogue_buffer ()
141
127
142
128
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 )
144
130
vim .api .nvim_buf_set_name (self :get_bufnr (), " NvimTree_" .. tab )
145
131
146
132
bufnr = self :get_bufnr ()
@@ -187,7 +173,7 @@ local move_tbl = {
187
173
--- @param tabpage integer
188
174
function View :setup_tabpage (tabpage )
189
175
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 })
191
177
end
192
178
193
179
--- @private
275
261
--- @param tabnr integer
276
262
function View :save_tab_state (tabnr )
277
263
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 )
279
265
end
280
266
281
267
--- @private
@@ -311,7 +297,7 @@ function View:close_this_tab_only()
311
297
end
312
298
313
299
function View :close_all_tabs ()
314
- for tabpage , _ in pairs (M . View . tabpages ) do
300
+ for tabpage , _ in pairs (globals . TABPAGES ) do
315
301
self :close_internal (tabpage )
316
302
end
317
303
end
450
436
--- @private
451
437
function View :set_current_win ()
452
438
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 ()
454
440
end
455
441
456
442
--- Open the tree in the a window
@@ -474,17 +460,17 @@ end
474
460
475
461
function View :abandon_current_window ()
476
462
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
480
466
end
481
467
end
482
468
483
469
function View :abandon_all_windows ()
484
470
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
488
474
end
489
475
end
490
476
end
@@ -493,15 +479,15 @@ end
493
479
--- @return boolean
494
480
function View :is_visible (opts )
495
481
if opts and opts .tabpage then
496
- if M . View . tabpages [opts .tabpage ] == nil then
482
+ if globals . TABPAGES [opts .tabpage ] == nil then
497
483
return false
498
484
end
499
- local winnr = M . View . tabpages [opts .tabpage ].winnr
485
+ local winnr = globals . TABPAGES [opts .tabpage ].winnr
500
486
return winnr and vim .api .nvim_win_is_valid (winnr )
501
487
end
502
488
503
489
if opts and opts .any_tabpage then
504
- for _ , v in pairs (M . View . tabpages ) do
490
+ for _ , v in pairs (globals . TABPAGES ) do
505
491
if v .winnr and vim .api .nvim_win_is_valid (v .winnr ) then
506
492
return true
507
493
end
@@ -555,15 +541,15 @@ end
555
541
--- Restores the state of a NvimTree window if it was initialized before.
556
542
function View :restore_tab_state ()
557
543
local tabpage = vim .api .nvim_get_current_tabpage ()
558
- self :set_cursor (self . cursors [tabpage ])
544
+ self :set_cursor (globals . CURSORS [tabpage ])
559
545
end
560
546
561
547
--- Returns the window number for nvim-tree within the tabpage specified
562
548
--- @param tabpage number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
563
549
--- @return number | nil
564
550
function View :get_winnr (tabpage )
565
551
tabpage = tabpage or vim .api .nvim_get_current_tabpage ()
566
- local tabinfo = M . View . tabpages [tabpage ]
552
+ local tabinfo = globals . TABPAGES [tabpage ]
567
553
if tabinfo and tabinfo .winnr and vim .api .nvim_win_is_valid (tabinfo .winnr ) then
568
554
return tabinfo .winnr
569
555
end
572
558
--- Returns the current nvim tree bufnr
573
559
--- @return number
574
560
function 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 ()]
576
562
end
577
563
578
564
function View :prevent_buffer_override ()
@@ -589,9 +575,9 @@ function View:prevent_buffer_override()
589
575
local bufname = vim .api .nvim_buf_get_name (curbuf )
590
576
591
577
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
593
579
if tabpage .winnr == view_winnr then
594
- M . View . tabpages [i ] = nil
580
+ globals . TABPAGES [i ] = nil
595
581
break
596
582
end
597
583
end
@@ -665,102 +651,4 @@ function View:configure_width(width)
665
651
end
666
652
end
667
653
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
-
766
654
return View
0 commit comments