@@ -18,8 +18,9 @@ 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 remove or replace with single member
21
+ -- TODO multi-instance replace with single members
22
22
--- @field private bufnr_by_tabid table<integer , integer>
23
+ --- @field private winid_by_tabid table<integer , integer>
23
24
local View = Class :extend ()
24
25
25
26
--- @class View
@@ -33,13 +34,14 @@ local View = Class:extend()
33
34
function View :new (args )
34
35
args .explorer :log_new (" View" )
35
36
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 = {}
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 = {}
41
43
42
- self .winopts = {
44
+ self .winopts = {
43
45
relativenumber = self .explorer .opts .view .relativenumber ,
44
46
number = self .explorer .opts .view .number ,
45
47
list = false ,
@@ -60,10 +62,6 @@ function View:new(args)
60
62
61
63
self :configure_width (self .explorer .opts .view .width )
62
64
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 ]
67
65
end
68
66
69
67
function View :destroy ()
@@ -80,6 +78,64 @@ local BUFFER_OPTIONS = {
80
78
{ name = " swapfile" , value = false },
81
79
}
82
80
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
+
83
139
-- TODO multi-instance remove this; delete buffers rather than retaining them
84
140
--- @private
85
141
--- @param bufnr integer
@@ -112,16 +168,18 @@ function View:create_buffer(bufnr)
112
168
113
169
bufnr = bufnr or vim .api .nvim_create_buf (false , false )
114
170
115
- -- set both bufnr registries
116
- globals .BUFNR_BY_TABID [tabid ] = bufnr
117
171
self .bufnr_by_tabid [tabid ] = bufnr
118
172
173
+ globals .BUFNR_BY_TABID [tabid ] = bufnr
174
+
119
175
vim .api .nvim_buf_set_name (bufnr , " NvimTree_" .. tabid )
120
176
121
177
for _ , option in ipairs (BUFFER_OPTIONS ) do
122
178
vim .api .nvim_set_option_value (option .name , option .value , { buf = bufnr })
123
179
end
124
180
181
+ self :create_autocmds (bufnr )
182
+
125
183
require (" nvim-tree.keymap" ).on_attach (bufnr )
126
184
127
185
events ._dispatch_tree_attached_post (bufnr )
@@ -158,7 +216,9 @@ local move_tbl = {
158
216
159
217
--- @private
160
218
function View :set_window_options_and_buffer ()
161
- pcall (vim .api .nvim_command , " buffer " .. self :get_bufnr ())
219
+ if not pcall (vim .api .nvim_command , " buffer " .. self :get_bufnr ()) then
220
+ return
221
+ end
162
222
163
223
if vim .fn .has (" nvim-0.10" ) == 1 then
164
224
local eventignore = vim .api .nvim_get_option_value (" eventignore" , {})
446
506
function View :abandon_current_window ()
447
507
local tab = vim .api .nvim_get_current_tabpage ()
448
508
449
- -- reset both bufnr registries
450
509
globals .BUFNR_BY_TABID [tab ] = nil
451
- self .bufnr_by_tabid [tab ] = nil
452
510
453
511
globals .WINID_BY_TABID [tab ] = nil
454
512
end
532
590
--- @param tabid number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
533
591
--- @return integer ? winid
534
592
function View :winid (tabid )
535
- local bufnr = self . bufnr_by_tabid [tabid ]
593
+ local bufnr = globals . BUFNR_BY_TABID [tabid ]
536
594
537
595
if bufnr then
538
596
for _ , winid in pairs (vim .api .nvim_tabpage_list_wins (tabid or 0 )) do
@@ -543,6 +601,7 @@ function View:winid(tabid)
543
601
end
544
602
end
545
603
604
+ --- TODO this needs to be refactored away; it's private now to contain it
546
605
--- Returns the window number for nvim-tree within the tabpage specified
547
606
--- @param tabid number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
548
607
--- @return number | nil
556
615
function View :get_bufnr ()
557
616
local tab = vim .api .nvim_get_current_tabpage ()
558
617
559
- return self . bufnr_by_tabid [tab ]
618
+ return globals . BUFNR_BY_TABID [tab ]
560
619
end
561
620
562
621
function View :prevent_buffer_override ()
0 commit comments