@@ -12,7 +12,6 @@ local Class = require("nvim-tree.classic")
1212--- @field live_filter table
1313--- @field side string
1414--- @field private explorer Explorer
15- --- @field private bufnr integer ?
1615--- @field private adaptive_size boolean
1716--- @field private winopts table
1817--- @field private initial_width integer
@@ -81,29 +80,100 @@ local BUFFER_OPTIONS = {
8180 { name = " swapfile" , value = false },
8281}
8382
83+ --- Buffer local autocommands to track state, deleted on buffer wipeout
84+ --- @private
85+ --- @param bufnr integer
86+ function View :create_autocmds (bufnr )
87+ -- clear bufnr, eject buffer opened in window
88+ vim .api .nvim_create_autocmd (" BufWipeout" , {
89+ group = self .explorer .augroup_id ,
90+ buffer = bufnr ,
91+ callback = function (data )
92+ log .line (" dev" , " View BufWipeout self.bufnr_by_tabid=%s data=%s" ,
93+ vim .inspect (self .bufnr_by_tabid , { newline = " " }),
94+ vim .inspect (data , { newline = " " })
95+ )
96+
97+ if self .explorer .opts .actions .open_file .eject then
98+ self :prevent_buffer_override ()
99+ else
100+ self :abandon_current_window ()
101+ end
102+ end ,
103+ })
104+
105+ -- set winid
106+ vim .api .nvim_create_autocmd (" BufWinEnter" , {
107+ group = self .explorer .augroup_id ,
108+ buffer = bufnr ,
109+ callback = function (data )
110+ log .line (" dev" , " View BufWinEnter self.bufnr_by_tabid=%s data=%s" ,
111+ vim .inspect (self .bufnr_by_tabid , { newline = " " }),
112+ vim .inspect (data , { newline = " " })
113+ )
114+ end ,
115+ })
116+
117+ -- clear winid
118+ vim .api .nvim_create_autocmd (" BufWinLeave" , {
119+ group = self .explorer .augroup_id ,
120+ buffer = bufnr ,
121+ callback = function (data )
122+ log .line (" dev" , " View BufWinEnter self.bufnr_by_tabid=%s data=%s" ,
123+ vim .inspect (self .bufnr_by_tabid , { newline = " " }),
124+ vim .inspect (data , { newline = " " })
125+ )
126+ end ,
127+ })
128+ end
129+
130+ -- TODO multi-instance remove this; delete buffers rather than retaining them
131+ --- @private
132+ --- @param bufnr integer
133+ --- @return boolean
134+ function View :matches_bufnr (bufnr )
135+ for _ , b in pairs (globals .BUFNR_BY_TABID ) do
136+ if b == bufnr then
137+ return true
138+ end
139+ end
140+ return false
141+ end
142+
143+ -- TODO multi-instance remove this; delete buffers rather than retaining them
144+ --- @private
145+ function View :wipe_rogue_buffer ()
146+ for _ , bufnr in ipairs (vim .api .nvim_list_bufs ()) do
147+ if not self :matches_bufnr (bufnr ) and utils .is_nvim_tree_buf (bufnr ) then
148+ pcall (vim .api .nvim_buf_delete , bufnr , { force = true })
149+ end
150+ end
151+ end
152+
84153--- @private
85154--- @param bufnr integer | false | nil
86155function View :create_buffer (bufnr )
87- -- maybe wipe existing
88- pcall (vim .api .nvim_buf_delete , self .bufnr , { force = true })
156+ self :wipe_rogue_buffer ()
89157
90158 local tabid = vim .api .nvim_get_current_tabpage ()
91159
92- self . bufnr = bufnr or vim .api .nvim_create_buf (false , false )
160+ bufnr = bufnr or vim .api .nvim_create_buf (false , false )
93161
94162 -- set both bufnr registries
95- globals .BUFNR_BY_TABID [tabid ] = self . bufnr
96- self .bufnr_by_tabid [tabid ] = self . bufnr
163+ globals .BUFNR_BY_TABID [tabid ] = bufnr
164+ self .bufnr_by_tabid [tabid ] = bufnr
97165
98- vim .api .nvim_buf_set_name (self . bufnr , " NvimTree_" .. tabid )
166+ vim .api .nvim_buf_set_name (bufnr , " NvimTree_" .. tabid )
99167
100168 for _ , option in ipairs (BUFFER_OPTIONS ) do
101- vim .api .nvim_set_option_value (option .name , option .value , { buf = self . bufnr })
169+ vim .api .nvim_set_option_value (option .name , option .value , { buf = bufnr })
102170 end
103171
104- require (" nvim-tree.keymap" ).on_attach (self .bufnr )
172+ self :create_autocmds (bufnr )
173+
174+ require (" nvim-tree.keymap" ).on_attach (bufnr )
105175
106- events ._dispatch_tree_attached_post (self . bufnr )
176+ events ._dispatch_tree_attached_post (bufnr )
107177end
108178
109179--- @private
@@ -137,7 +207,7 @@ local move_tbl = {
137207
138208--- @private
139209function View :set_window_options_and_buffer ()
140- if not pcall (vim .api .nvim_command , " buffer " .. self . bufnr ) then
210+ if not pcall (vim .api .nvim_command , " buffer " .. self : get_bufnr () ) then
141211 return
142212 end
143213
222292--- @param tabid integer
223293function View :save_state (tabid )
224294 tabid = tabid or vim .api .nvim_get_current_tabpage ()
225- globals .CURSORS [tabid ] = vim .api .nvim_win_get_cursor (self :get_winid_internal (tabid ) or 0 )
295+ globals .CURSORS [tabid ] = vim .api .nvim_win_get_cursor (self :get_winid (tabid ) or 0 )
226296end
227297
228298--- @private
@@ -233,7 +303,7 @@ function View:close_internal(tabid)
233303 end
234304 self :save_state (tabid )
235305 switch_buf_if_last_buf ()
236- local tree_win = self :get_winid_internal (tabid )
306+ local tree_win = self :get_winid (tabid )
237307 local current_win = vim .api .nvim_get_current_win ()
238308 for _ , win in pairs (vim .api .nvim_tabpage_list_wins (tabid )) do
239309 if vim .api .nvim_win_get_config (win ).relative == " " then
@@ -300,12 +370,12 @@ end
300370--- @private
301371function View :grow ()
302372 local starts_at = self :is_root_folder_visible (require (" nvim-tree.core" ).get_cwd ()) and 1 or 0
303- local lines = vim .api .nvim_buf_get_lines (self . bufnr , starts_at , - 1 , false )
373+ local lines = vim .api .nvim_buf_get_lines (self : get_bufnr () , starts_at , - 1 , false )
304374 -- number of columns of right-padding to indicate end of path
305375 local padding = self :get_size (self .padding )
306376
307377 -- account for sign/number columns etc.
308- local wininfo = vim .fn .getwininfo (self :get_winid_internal ())
378+ local wininfo = vim .fn .getwininfo (self :get_winid ())
309379 if type (wininfo ) == " table" and type (wininfo [1 ]) == " table" then
310380 padding = padding + wininfo [1 ].textoff
311381 end
@@ -324,7 +394,7 @@ function View:grow()
324394 for line_nr , l in pairs (lines ) do
325395 local count = vim .fn .strchars (l )
326396 -- also add space for right-aligned icons
327- local extmarks = vim .api .nvim_buf_get_extmarks (self . bufnr , ns_id , { line_nr , 0 }, { line_nr , - 1 }, { details = true })
397+ local extmarks = vim .api .nvim_buf_get_extmarks (self : get_bufnr () , ns_id , { line_nr , 0 }, { line_nr , - 1 }, { details = true })
328398 count = count + utils .extmarks_length (extmarks )
329399 if resizing_width < count then
330400 resizing_width = count
@@ -373,7 +443,7 @@ function View:resize(size)
373443 return
374444 end
375445
376- local winid = self :get_winid_internal () or 0
446+ local winid = self :get_winid () or 0
377447
378448 local new_size = self :get_width ()
379449
@@ -425,11 +495,8 @@ function View:open_in_win(opts)
425495end
426496
427497function View :abandon_current_window ()
428- self .bufnr = nil
429-
430498 local tab = vim .api .nvim_get_current_tabpage ()
431499
432- -- reset both bufnr registries
433500 globals .BUFNR_BY_TABID [tab ] = nil
434501 self .bufnr_by_tabid [tab ] = nil
435502
@@ -462,26 +529,26 @@ function View:is_visible(opts)
462529 return false
463530 end
464531
465- local winid = self :get_winid_internal ()
532+ local winid = self :get_winid ()
466533 return winid ~= nil and vim .api .nvim_win_is_valid (winid or 0 )
467534end
468535
469536--- @param opts table | nil
470537function View :set_cursor (opts )
471538 if self :is_visible () then
472- pcall (vim .api .nvim_win_set_cursor , self :get_winid_internal (), opts )
539+ pcall (vim .api .nvim_win_set_cursor , self :get_winid (), opts )
473540 end
474541end
475542
476543--- @param winid number | nil
477544--- @param open_if_closed boolean | nil
478545function View :focus (winid , open_if_closed )
479- local wid = winid or self :get_winid_internal (nil )
546+ local wid = winid or self :get_winid (nil )
480547
481548 if vim .api .nvim_win_get_tabpage (wid or 0 ) ~= vim .api .nvim_win_get_tabpage (0 ) then
482549 self :close ()
483550 self :open ()
484- wid = self :get_winid_internal (nil )
551+ wid = self :get_winid (nil )
485552 elseif open_if_closed and not self :is_visible () then
486553 self :open ()
487554 end
@@ -500,7 +567,7 @@ function View:api_winid(opts)
500567 tabpage = vim .api .nvim_get_current_tabpage ()
501568 end
502569 if self :is_visible ({ tabpage = tabpage }) then
503- return self :get_winid_internal (tabpage )
570+ return self :get_winid (tabpage )
504571 else
505572 return nil
506573 end
@@ -528,29 +595,24 @@ end
528595
529596--- TODO this needs to be refactored away; it's private now to contain it
530597--- Returns the window number for nvim-tree within the tabpage specified
531- --- @private
532598--- @param tabid number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
533599--- @return number | nil
534- function View :get_winid_internal (tabid )
600+ function View :get_winid (tabid )
535601 tabid = tabid or vim .api .nvim_get_current_tabpage ()
536602 return self :winid (tabid )
537603end
538604
539- --- first window containing this view's buffer
540- --- @return integer ? winid
541- function View :get_winid ()
542- return utils .first_window_containing_buf (self .bufnr )
543- end
544-
545- --- buffer for this view
546- --- @return integer ?
605+ --- Returns the current nvim tree bufnr
606+ --- @return number
547607function View :get_bufnr ()
548- return self .bufnr
608+ local tab = vim .api .nvim_get_current_tabpage ()
609+
610+ return self .bufnr_by_tabid [tab ]
549611end
550612
551613function View :prevent_buffer_override ()
552- local view_winid = self :get_winid_internal ()
553- local view_bufnr = self . bufnr
614+ local view_winid = self :get_winid ()
615+ local view_bufnr = self : get_bufnr ()
554616
555617 -- need to schedule to let the new buffer populate the window
556618 -- because this event needs to be run on bufWipeout.
604666
605667-- used on ColorScheme event
606668function View :reset_winhl ()
607- local winid = self :get_winid_internal ()
669+ local winid = self :get_winid ()
608670 if winid and vim .api .nvim_win_is_valid (winid ) then
609671 vim .wo [winid ].winhl = appearance .WIN_HL
610672 end
0 commit comments