594594--- Retrieve the winid of the open tree.
595595--- @param opts ApiTreeWinIdOpts | nil
596596--- @return number | nil winid unlike get_winnr (), this returns nil if the nvim-tree window is not visible
597- function View :winid (opts )
597+ function View :api_winid (opts )
598598 local tabpage = opts and opts .tabpage
599599 if tabpage == 0 then
600600 tabpage = vim .api .nvim_get_current_tabpage ()
@@ -612,6 +612,28 @@ function View:restore_tab_state()
612612 self :set_cursor (globals .CURSORS [tabpage ])
613613end
614614
615+ --- winid containing the buffer
616+ --- @param tabpage number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
617+ --- @param callsite string
618+ --- @return integer ? winid
619+ function View :winid (tabpage , callsite )
620+ local winid = nil
621+ local bufnr = self .bufnr_by_tab [tabpage ]
622+
623+ local msg = string.format (" View:winid(%3s, %-20.20s)" , tabpage , callsite )
624+
625+ if bufnr then
626+ for _ , w in pairs (vim .api .nvim_tabpage_list_wins (tabpage or 0 )) do
627+ if vim .api .nvim_win_get_buf (w ) == bufnr then
628+ log .line (" dev" , " %s b%d : w%s" , msg , bufnr , winid )
629+ return w
630+ end
631+ end
632+ else
633+ msg = string.format (" %s no bufnr" , msg )
634+ end
635+ end
636+
615637--- Returns the window number for nvim-tree within the tabpage specified
616638--- @param tabpage number | nil (optional ) the number of the chosen tabpage. Defaults to current tabpage.
617639--- @param callsite string
@@ -636,21 +658,14 @@ function View:get_winnr(tabpage, callsite)
636658 ret = tabinfo .winnr
637659 end
638660
639- local winid_from_bufnr
640- if self .bufnr_by_tab [tabpage ] then
641- for _ , winid in pairs (vim .api .nvim_tabpage_list_wins (tabpage )) do
642- if vim .api .nvim_win_get_buf (winid ) == self .bufnr_by_tab [tabpage ] then
643- winid_from_bufnr = winid
644- end
645- end
646- end
647-
648- if ret ~= winid_from_bufnr then
661+ local winid = self :winid (tabpage , " View:get_winnr" )
662+ if ret ~= winid then
649663 if ret then
650- msg = string.format (" %s winid_from_bufnr w%s MISMATCH" , msg , winid_from_bufnr )
664+ msg = string.format (" %s winid_from_bufnr w%s MISMATCH" , msg , winid )
651665 else
652- msg = string.format (" %s winid_from_bufnr w%s STALE" , msg , winid_from_bufnr )
666+ msg = string.format (" %s winid_from_bufnr w%s STALE" , msg , winid )
653667 end
668+ notify .error (string.format (" View:get_winnr w%s View:winnr w%s MISMATCH" , ret , winid ))
654669 end
655670
656671 log .line (" dev" , " %s" , msg )
@@ -677,6 +692,13 @@ function View:get_bufnr(callsite)
677692 globals .BUFNR_PER_TAB [tab ],
678693 self .bufnr_by_tab [tab ],
679694 (globals .BUFNR_PER_TAB [tab ] == self .bufnr_by_tab [tab ]) and " " or " MISMATCH" )
695+
696+ if globals .BUFNR_PER_TAB [tab ] ~= self .bufnr_by_tab [tab ] then
697+ notify .error (string.format (" View:get_bufnr globals.BUFNR_PER_TAB[%s] b%s view.bufnr_by_tab[%s] b%s MISMATCH" ,
698+ tab , globals .BUFNR_PER_TAB [tab ],
699+ tab , self .bufnr_by_tab [tab ]
700+ ))
701+ end
680702 end
681703 return globals .BUFNR_PER_TAB [tab ]
682704end
0 commit comments