@@ -18,9 +18,8 @@ local Class = require("nvim-tree.classic")
18
18
--- @field private width (fun (): integer )| integer | string
19
19
--- @field private max_width integer
20
20
--- @field private padding integer
21
- -- TODO multi-instance replace with single members
21
+ -- TODO multi-instance remove or replace with single member
22
22
--- @field private bufnr_by_tabid table<integer , integer>
23
- --- @field private winid_by_tabid table<integer , integer>
24
23
local View = Class :extend ()
25
24
26
25
--- @class View
@@ -34,14 +33,13 @@ local View = Class:extend()
34
33
function View :new (args )
35
34
args .explorer :log_new (" View" )
36
35
37
- self .explorer = args .explorer
38
- self .adaptive_size = false
39
- self .side = (self .explorer .opts .view .side == " right" ) and " right" or " left"
40
- self .live_filter = { prev_focused_node = nil , }
41
- self .bufnr_by_tabid = {}
42
- self .winid_by_tabid = {}
36
+ self .explorer = args .explorer
37
+ self .adaptive_size = false
38
+ self .side = (self .explorer .opts .view .side == " right" ) and " right" or " left"
39
+ self .live_filter = { prev_focused_node = nil , }
40
+ self .bufnr_by_tabid = {}
43
41
44
- self .winopts = {
42
+ self .winopts = {
45
43
relativenumber = self .explorer .opts .view .relativenumber ,
46
44
number = self .explorer .opts .view .number ,
47
45
list = false ,
@@ -62,6 +60,10 @@ function View:new(args)
62
60
63
61
self :configure_width (self .explorer .opts .view .width )
64
62
self .initial_width = self :get_width ()
63
+
64
+ -- TODO multi-instance remove this; delete buffers rather than retaining them
65
+ local tabid = vim .api .nvim_get_current_tabpage ()
66
+ self .bufnr_by_tabid [tabid ] = globals .BUFNR_BY_TABID [tabid ]
65
67
end
66
68
67
69
function View :destroy ()
@@ -78,64 +80,6 @@ local BUFFER_OPTIONS = {
78
80
{ name = " swapfile" , value = false },
79
81
}
80
82
81
- --- Buffer local autocommands to track state, deleted on buffer wipeout
82
- --- @private
83
- --- @param bufnr integer
84
- function View :create_autocmds (bufnr )
85
- -- clear bufnr and winid
86
- -- eject buffer opened in the nvim-tree window and create a new buffer
87
- vim .api .nvim_create_autocmd (" BufWipeout" , {
88
- group = self .explorer .augroup_id ,
89
- buffer = bufnr ,
90
- callback = function (data )
91
- log .line (" dev" ,
92
- " View BufWipeout\n bufnr = %s\n data.buf = %s\n self.bufnr_by_tabid = %s\n self.winid_by_tabid = %s" ,
93
- bufnr ,
94
- data .buf ,
95
- vim .inspect (self .bufnr_by_tabid , { newline = " " }),
96
- vim .inspect (self .winid_by_tabid , { newline = " " }),
97
- vim .inspect (data , { newline = " " })
98
- )
99
-
100
- -- clear the tab's buffer
101
- self .bufnr_by_tabid = vim .tbl_map (function (b )
102
- return b ~= bufnr and b or nil
103
- end , self .bufnr_by_tabid )
104
-
105
- -- clear the tab's window(s)
106
- local winids = vim .fn .win_findbuf (bufnr )
107
- self .winid_by_tabid = vim .tbl_map (function (winid )
108
- return not vim .tbl_contains (winids , winid ) and winid or nil
109
- end , self .winid_by_tabid )
110
-
111
- if self .explorer .opts .actions .open_file .eject then
112
- self :prevent_buffer_override ()
113
- else
114
- self :abandon_current_window ()
115
- end
116
- end ,
117
- })
118
-
119
- -- set winid
120
- vim .api .nvim_create_autocmd (" BufWinEnter" , {
121
- group = self .explorer .augroup_id ,
122
- buffer = bufnr ,
123
- callback = function (data )
124
- local tabid = vim .api .nvim_get_current_tabpage ()
125
-
126
- log .line (" dev" ,
127
- " View BufWinEnter\n bufnr = %s\n data.buf = %s\n self.bufnr_by_tabid = %s\n self.winid_by_tabid = %s" ,
128
- bufnr ,
129
- data .buf ,
130
- vim .inspect (self .bufnr_by_tabid , { newline = " " }),
131
- vim .inspect (self .winid_by_tabid , { newline = " " })
132
- )
133
-
134
- self .winid_by_tabid [tabid ] = vim .fn .bufwinid (data .buf ) -- first on current tabpage
135
- end ,
136
- })
137
- end
138
-
139
83
-- TODO multi-instance remove this; delete buffers rather than retaining them
140
84
--- @private
141
85
--- @param bufnr integer
@@ -168,18 +112,16 @@ function View:create_buffer(bufnr)
168
112
169
113
bufnr = bufnr or vim .api .nvim_create_buf (false , false )
170
114
171
- self .bufnr_by_tabid [tabid ] = bufnr
172
-
115
+ -- set both bufnr registries
173
116
globals .BUFNR_BY_TABID [tabid ] = bufnr
117
+ self .bufnr_by_tabid [tabid ] = bufnr
174
118
175
119
vim .api .nvim_buf_set_name (bufnr , " NvimTree_" .. tabid )
176
120
177
121
for _ , option in ipairs (BUFFER_OPTIONS ) do
178
122
vim .api .nvim_set_option_value (option .name , option .value , { buf = bufnr })
179
123
end
180
124
181
- self :create_autocmds (bufnr )
182
-
183
125
require (" nvim-tree.keymap" ).on_attach (bufnr )
184
126
185
127
events ._dispatch_tree_attached_post (bufnr )
@@ -216,9 +158,7 @@ local move_tbl = {
216
158
217
159
--- @private
218
160
function View :set_window_options_and_buffer ()
219
- if not pcall (vim .api .nvim_command , " buffer " .. self :get_bufnr ()) then
220
- return
221
- end
161
+ pcall (vim .api .nvim_command , " buffer " .. self :get_bufnr ())
222
162
223
163
if vim .fn .has (" nvim-0.10" ) == 1 then
224
164
local eventignore = vim .api .nvim_get_option_value (" eventignore" , {})
506
446
function View :abandon_current_window ()
507
447
local tab = vim .api .nvim_get_current_tabpage ()
508
448
449
+ -- reset both bufnr registries
509
450
globals .BUFNR_BY_TABID [tab ] = nil
451
+ self .bufnr_by_tabid [tab ] = nil
510
452
511
453
globals .WINID_BY_TABID [tab ] = nil
512
454
end
590
532
--- @param tabid number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
591
533
--- @return integer ? winid
592
534
function View :winid (tabid )
593
- local bufnr = globals . BUFNR_BY_TABID [tabid ]
535
+ local bufnr = self . bufnr_by_tabid [tabid ]
594
536
595
537
if bufnr then
596
538
for _ , winid in pairs (vim .api .nvim_tabpage_list_wins (tabid or 0 )) do
@@ -601,7 +543,6 @@ function View:winid(tabid)
601
543
end
602
544
end
603
545
604
- --- TODO this needs to be refactored away; it's private now to contain it
605
546
--- Returns the window number for nvim-tree within the tabpage specified
606
547
--- @param tabid number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
607
548
--- @return number | nil
615
556
function View :get_bufnr ()
616
557
local tab = vim .api .nvim_get_current_tabpage ()
617
558
618
- return globals . BUFNR_BY_TABID [tab ]
559
+ return self . bufnr_by_tabid [tab ]
619
560
end
620
561
621
562
function View :prevent_buffer_override ()
0 commit comments