@@ -12,6 +12,7 @@ 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 ?
1516--- @field private adaptive_size boolean
1617--- @field private winopts table
1718--- @field private initial_width integer
@@ -80,51 +81,29 @@ local BUFFER_OPTIONS = {
8081 { name = " swapfile" , value = false },
8182}
8283
83- -- TODO multi-instance remove this; delete buffers rather than retaining them
84- --- @private
85- --- @param bufnr integer
86- --- @return boolean
87- function View :matches_bufnr (bufnr )
88- for _ , b in pairs (globals .BUFNR_BY_TABID ) do
89- if b == bufnr then
90- return true
91- end
92- end
93- return false
94- end
95-
96- -- TODO multi-instance remove this; delete buffers rather than retaining them
97- --- @private
98- function View :wipe_rogue_buffer ()
99- for _ , bufnr in ipairs (vim .api .nvim_list_bufs ()) do
100- if not self :matches_bufnr (bufnr ) and utils .is_nvim_tree_buf (bufnr ) then
101- pcall (vim .api .nvim_buf_delete , bufnr , { force = true })
102- end
103- end
104- end
105-
10684--- @private
10785--- @param bufnr integer | false | nil
10886function View :create_buffer (bufnr )
109- self :wipe_rogue_buffer ()
87+ -- maybe wipe existing
88+ pcall (vim .api .nvim_buf_delete , self .bufnr , { force = true })
11089
11190 local tabid = vim .api .nvim_get_current_tabpage ()
11291
113- bufnr = bufnr or vim .api .nvim_create_buf (false , false )
92+ self . bufnr = bufnr or vim .api .nvim_create_buf (false , false )
11493
11594 -- set both bufnr registries
116- globals .BUFNR_BY_TABID [tabid ] = bufnr
117- self .bufnr_by_tabid [tabid ] = bufnr
95+ globals .BUFNR_BY_TABID [tabid ] = self . bufnr
96+ self .bufnr_by_tabid [tabid ] = self . bufnr
11897
119- vim .api .nvim_buf_set_name (bufnr , " NvimTree_" .. tabid )
98+ vim .api .nvim_buf_set_name (self . bufnr , " NvimTree_" .. tabid )
12099
121100 for _ , option in ipairs (BUFFER_OPTIONS ) do
122- vim .api .nvim_set_option_value (option .name , option .value , { buf = bufnr })
101+ vim .api .nvim_set_option_value (option .name , option .value , { buf = self . bufnr })
123102 end
124103
125- require (" nvim-tree.keymap" ).on_attach (bufnr )
104+ require (" nvim-tree.keymap" ).on_attach (self . bufnr )
126105
127- events ._dispatch_tree_attached_post (bufnr )
106+ events ._dispatch_tree_attached_post (self . bufnr )
128107end
129108
130109--- @private
@@ -158,7 +137,7 @@ local move_tbl = {
158137
159138--- @private
160139function View :set_window_options_and_buffer ()
161- pcall (vim .api .nvim_command , " buffer " .. self :get_bufnr ())
140+ pcall (vim .api .nvim_command , " buffer " .. self :get_bufnr_internal ())
162141
163142 if vim .fn .has (" nvim-0.10" ) == 1 then
164143 local eventignore = vim .api .nvim_get_option_value (" eventignore" , {})
241220--- @param tabid integer
242221function View :save_state (tabid )
243222 tabid = tabid or vim .api .nvim_get_current_tabpage ()
244- globals .CURSORS [tabid ] = vim .api .nvim_win_get_cursor (self :get_winid (tabid ) or 0 )
223+ globals .CURSORS [tabid ] = vim .api .nvim_win_get_cursor (self :get_winid_internal (tabid ) or 0 )
245224end
246225
247226--- @private
@@ -252,7 +231,7 @@ function View:close_internal(tabid)
252231 end
253232 self :save_state (tabid )
254233 switch_buf_if_last_buf ()
255- local tree_win = self :get_winid (tabid )
234+ local tree_win = self :get_winid_internal (tabid )
256235 local current_win = vim .api .nvim_get_current_win ()
257236 for _ , win in pairs (vim .api .nvim_tabpage_list_wins (tabid )) do
258237 if vim .api .nvim_win_get_config (win ).relative == " " then
@@ -319,12 +298,12 @@ end
319298--- @private
320299function View :grow ()
321300 local starts_at = self :is_root_folder_visible (require (" nvim-tree.core" ).get_cwd ()) and 1 or 0
322- local lines = vim .api .nvim_buf_get_lines (self :get_bufnr (), starts_at , - 1 , false )
301+ local lines = vim .api .nvim_buf_get_lines (self :get_bufnr_internal (), starts_at , - 1 , false )
323302 -- number of columns of right-padding to indicate end of path
324303 local padding = self :get_size (self .padding )
325304
326305 -- account for sign/number columns etc.
327- local wininfo = vim .fn .getwininfo (self :get_winid ())
306+ local wininfo = vim .fn .getwininfo (self :get_winid_internal ())
328307 if type (wininfo ) == " table" and type (wininfo [1 ]) == " table" then
329308 padding = padding + wininfo [1 ].textoff
330309 end
@@ -343,7 +322,7 @@ function View:grow()
343322 for line_nr , l in pairs (lines ) do
344323 local count = vim .fn .strchars (l )
345324 -- also add space for right-aligned icons
346- local extmarks = vim .api .nvim_buf_get_extmarks (self :get_bufnr (), ns_id , { line_nr , 0 }, { line_nr , - 1 }, { details = true })
325+ local extmarks = vim .api .nvim_buf_get_extmarks (self :get_bufnr_internal (), ns_id , { line_nr , 0 }, { line_nr , - 1 }, { details = true })
347326 count = count + utils .extmarks_length (extmarks )
348327 if resizing_width < count then
349328 resizing_width = count
@@ -392,7 +371,7 @@ function View:resize(size)
392371 return
393372 end
394373
395- local winid = self :get_winid () or 0
374+ local winid = self :get_winid_internal () or 0
396375
397376 local new_size = self :get_width ()
398377
@@ -444,6 +423,8 @@ function View:open_in_win(opts)
444423end
445424
446425function View :abandon_current_window ()
426+ self .bufnr = nil
427+
447428 local tab = vim .api .nvim_get_current_tabpage ()
448429
449430 -- reset both bufnr registries
@@ -479,26 +460,26 @@ function View:is_visible(opts)
479460 return false
480461 end
481462
482- local winid = self :get_winid ()
463+ local winid = self :get_winid_internal ()
483464 return winid ~= nil and vim .api .nvim_win_is_valid (winid or 0 )
484465end
485466
486467--- @param opts table | nil
487468function View :set_cursor (opts )
488469 if self :is_visible () then
489- pcall (vim .api .nvim_win_set_cursor , self :get_winid (), opts )
470+ pcall (vim .api .nvim_win_set_cursor , self :get_winid_internal (), opts )
490471 end
491472end
492473
493474--- @param winid number | nil
494475--- @param open_if_closed boolean | nil
495476function View :focus (winid , open_if_closed )
496- local wid = winid or self :get_winid (nil )
477+ local wid = winid or self :get_winid_internal (nil )
497478
498479 if vim .api .nvim_win_get_tabpage (wid or 0 ) ~= vim .api .nvim_win_get_tabpage (0 ) then
499480 self :close ()
500481 self :open ()
501- wid = self :get_winid (nil )
482+ wid = self :get_winid_internal (nil )
502483 elseif open_if_closed and not self :is_visible () then
503484 self :open ()
504485 end
@@ -517,7 +498,7 @@ function View:api_winid(opts)
517498 tabpage = vim .api .nvim_get_current_tabpage ()
518499 end
519500 if self :is_visible ({ tabpage = tabpage }) then
520- return self :get_winid (tabpage )
501+ return self :get_winid_internal (tabpage )
521502 else
522503 return nil
523504 end
@@ -543,25 +524,41 @@ function View:winid(tabid)
543524 end
544525end
545526
527+ --- TODO this needs to be refactored away; it's private now to contain it
546528--- Returns the window number for nvim-tree within the tabpage specified
529+ --- @private
547530--- @param tabid number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
548531--- @return number | nil
549- function View :get_winid (tabid )
532+ function View :get_winid_internal (tabid )
550533 tabid = tabid or vim .api .nvim_get_current_tabpage ()
551534 return self :winid (tabid )
552535end
553536
537+ --- first window containing this view's buffer
538+ --- @return integer ? winid
539+ function View :get_winid ()
540+ return utils .first_window_containing_buf (self .bufnr )
541+ end
542+
543+ --- TODO this needs to be refactored away; it's private now to contain it
554544--- Returns the current nvim tree bufnr
545+ --- @private
555546--- @return number
556- function View :get_bufnr ()
547+ function View :get_bufnr_internal ()
557548 local tab = vim .api .nvim_get_current_tabpage ()
558549
559550 return self .bufnr_by_tabid [tab ]
560551end
561552
553+ --- buffer for this view
554+ --- @return integer ?
555+ function View :get_bufnr ()
556+ return self .bufnr
557+ end
558+
562559function View :prevent_buffer_override ()
563- local view_winid = self :get_winid ()
564- local view_bufnr = self :get_bufnr ()
560+ local view_winid = self :get_winid_internal ()
561+ local view_bufnr = self :get_bufnr_internal ()
565562
566563 -- need to schedule to let the new buffer populate the window
567564 -- because this event needs to be run on bufWipeout.
615612
616613-- used on ColorScheme event
617614function View :reset_winhl ()
618- local winid = self :get_winid ()
615+ local winid = self :get_winid_internal ()
619616 if winid and vim .api .nvim_win_is_valid (winid ) then
620617 vim .wo [winid ].winhl = appearance .WIN_HL
621618 end
0 commit comments