refactor(#2826): notify error on view winid and bufnr mismatches

This commit is contained in:
Alexander Courtis 2025-06-19 13:24:02 +10:00
parent 6347a2c173
commit de3f48629d
3 changed files with 37 additions and 15 deletions

View File

@ -389,7 +389,7 @@ local function edit_in_current_buf(filename)
local explorer = core.get_explorer()
if explorer then
explorer.view:abandon_current_window("open-file.edit_in_current_buf")
explorer.view:abandon_current_window()
end
if M.relative_path then

View File

@ -206,7 +206,7 @@ Api.tree.is_visible = wrap_explorer_member("view", "is_visible")
---@class ApiTreeWinIdOpts
---@field tabpage number|nil default nil
Api.tree.winid = wrap_explorer_member("view", "winid")
Api.tree.winid = wrap_explorer_member("view", "api_winid")
Api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn)
Api.fs.remove = wrap_node(actions.fs.remove_file.fn)

View File

@ -594,7 +594,7 @@ end
--- Retrieve the winid of the open tree.
---@param opts ApiTreeWinIdOpts|nil
---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible
function View:winid(opts)
function View:api_winid(opts)
local tabpage = opts and opts.tabpage
if tabpage == 0 then
tabpage = vim.api.nvim_get_current_tabpage()
@ -612,6 +612,28 @@ function View:restore_tab_state()
self:set_cursor(globals.CURSORS[tabpage])
end
--- winid containing the buffer
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@param callsite string
---@return integer? winid
function View:winid(tabpage, callsite)
local winid = nil
local bufnr = self.bufnr_by_tab[tabpage]
local msg = string.format("View:winid(%3s, %-20.20s)", tabpage, callsite)
if bufnr then
for _, w in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do
if vim.api.nvim_win_get_buf(w) == bufnr then
log.line("dev", "%s b%d : w%s", msg, bufnr, winid)
return w
end
end
else
msg = string.format("%s no bufnr", msg)
end
end
--- Returns the window number for nvim-tree within the tabpage specified
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@param callsite string
@ -636,21 +658,14 @@ function View:get_winnr(tabpage, callsite)
ret = tabinfo.winnr
end
local winid_from_bufnr
if self.bufnr_by_tab[tabpage] then
for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
if vim.api.nvim_win_get_buf(winid) == self.bufnr_by_tab[tabpage] then
winid_from_bufnr = winid
end
end
end
if ret ~= winid_from_bufnr then
local winid = self:winid(tabpage, "View:get_winnr")
if ret ~= winid then
if ret then
msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid_from_bufnr)
msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid)
else
msg = string.format("%s winid_from_bufnr w%s STALE", msg, winid_from_bufnr)
msg = string.format("%s winid_from_bufnr w%s STALE", msg, winid)
end
notify.error(string.format("View:get_winnr w%s View:winnr w%s MISMATCH", ret, winid))
end
log.line("dev", "%s", msg)
@ -677,6 +692,13 @@ function View:get_bufnr(callsite)
globals.BUFNR_PER_TAB[tab],
self.bufnr_by_tab[tab],
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
if globals.BUFNR_PER_TAB[tab] ~= self.bufnr_by_tab[tab] then
notify.error(string.format("View:get_bufnr globals.BUFNR_PER_TAB[%s] b%s view.bufnr_by_tab[%s] b%s MISMATCH",
tab, globals.BUFNR_PER_TAB[tab],
tab, self.bufnr_by_tab[tab]
))
end
end
return globals.BUFNR_PER_TAB[tab]
end