Skip to content

Commit de3f486

Browse files
committed
refactor(#2826): notify error on view winid and bufnr mismatches
1 parent 6347a2c commit de3f486

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

lua/nvim-tree/actions/node/open-file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ local function edit_in_current_buf(filename)
389389
local explorer = core.get_explorer()
390390

391391
if explorer then
392-
explorer.view:abandon_current_window("open-file.edit_in_current_buf")
392+
explorer.view:abandon_current_window()
393393
end
394394

395395
if M.relative_path then

lua/nvim-tree/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Api.tree.is_visible = wrap_explorer_member("view", "is_visible")
206206
---@class ApiTreeWinIdOpts
207207
---@field tabpage number|nil default nil
208208

209-
Api.tree.winid = wrap_explorer_member("view", "winid")
209+
Api.tree.winid = wrap_explorer_member("view", "api_winid")
210210

211211
Api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn)
212212
Api.fs.remove = wrap_node(actions.fs.remove_file.fn)

lua/nvim-tree/explorer/view.lua

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ end
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])
613613
end
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]
682704
end

0 commit comments

Comments
 (0)