refactor(#2826): consistent naming of tabid

This commit is contained in:
Alexander Courtis 2025-06-20 12:01:24 +10:00
parent 83fdff7c4a
commit e875f15b32
3 changed files with 89 additions and 82 deletions

View File

@ -18,7 +18,7 @@ local Class = require("nvim-tree.classic")
---@field private width (fun():integer)|integer|string ---@field private width (fun():integer)|integer|string
---@field private max_width integer ---@field private max_width integer
---@field private padding integer ---@field private padding integer
---@field private bufnr_by_tab table<integer, integer> stored per tab until multi-instance is complete ---@field private bufnr_by_tabid table<integer, integer> stored per tab until multi-instance is complete
local View = Class:extend() local View = Class:extend()
---@class View ---@class View
@ -36,7 +36,7 @@ function View:new(args)
self.adaptive_size = false self.adaptive_size = false
self.side = (self.explorer.opts.view.side == "right") and "right" or "left" self.side = (self.explorer.opts.view.side == "right") and "right" or "left"
self.live_filter = { prev_focused_node = nil, } self.live_filter = { prev_focused_node = nil, }
self.bufnr_by_tab = {} self.bufnr_by_tabid = {}
self.winopts = { self.winopts = {
relativenumber = self.explorer.opts.view.relativenumber, relativenumber = self.explorer.opts.view.relativenumber,
@ -62,7 +62,7 @@ function View:new(args)
-- TODO multi-instance remove this; delete buffers rather than retaining them -- TODO multi-instance remove this; delete buffers rather than retaining them
local tabid = vim.api.nvim_get_current_tabpage() local tabid = vim.api.nvim_get_current_tabpage()
self.bufnr_by_tab[tabid] = globals.BUFNR_PER_TAB[tabid] self.bufnr_by_tabid[tabid] = globals.BUFNR_BY_TABID[tabid]
end end
function View:destroy() function View:destroy()
@ -84,7 +84,7 @@ local BUFFER_OPTIONS = {
---@param bufnr integer ---@param bufnr integer
---@return boolean ---@return boolean
function View:matches_bufnr(bufnr) function View:matches_bufnr(bufnr)
for _, b in pairs(globals.BUFNR_PER_TAB) do for _, b in pairs(globals.BUFNR_BY_TABID) do
if b == bufnr then if b == bufnr then
return true return true
end end
@ -107,15 +107,15 @@ end
function View:create_buffer(bufnr) function View:create_buffer(bufnr)
self:wipe_rogue_buffer() self:wipe_rogue_buffer()
local tab = vim.api.nvim_get_current_tabpage() local tabid = vim.api.nvim_get_current_tabpage()
bufnr = bufnr or vim.api.nvim_create_buf(false, false) bufnr = bufnr or vim.api.nvim_create_buf(false, false)
-- set both bufnr registries -- set both bufnr registries
globals.BUFNR_PER_TAB[tab] = bufnr globals.BUFNR_BY_TABID[tabid] = bufnr
self.bufnr_by_tab[tab] = bufnr self.bufnr_by_tabid[tabid] = bufnr
vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tab) vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tabid)
for _, option in ipairs(BUFFER_OPTIONS) do for _, option in ipairs(BUFFER_OPTIONS) do
vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr }) vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr })
@ -200,7 +200,7 @@ function View:open_window()
vim.api.nvim_command("vsp") vim.api.nvim_command("vsp")
self:reposition_window() self:reposition_window()
end end
globals.WINID_PER_TAB[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win() globals.WINID_BY_TABID[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win()
self:set_window_options_and_buffer() self:set_window_options_and_buffer()
end end
@ -237,26 +237,26 @@ end
---save_tab_state saves any state that should be preserved across redraws. ---save_tab_state saves any state that should be preserved across redraws.
---@private ---@private
---@param tabnr integer ---@param tabid integer
function View:save_tab_state(tabnr) function View:save_tab_state(tabid)
local tabpage = tabnr or vim.api.nvim_get_current_tabpage() tabid = tabid or vim.api.nvim_get_current_tabpage()
globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winid(tabpage, "View:save_tab_state") or 0) globals.CURSORS[tabid] = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0)
end end
---@private ---@private
---@param tabpage integer ---@param tabid integer
function View:close_internal(tabpage) function View:close_internal(tabid)
if self.explorer.opts.experimental.multi_instance then if self.explorer.opts.experimental.multi_instance then
log.line("dev", "View:close_internal(t%s)", tabpage) log.line("dev", "View:close_internal(t%s)", tabid)
end end
if not self:is_visible({ tabpage = tabpage }) then if not self:is_visible({ tabpage = tabid }) then
return return
end end
self:save_tab_state(tabpage) self:save_tab_state(tabid)
switch_buf_if_last_buf() switch_buf_if_last_buf()
local tree_win = self:get_winid(tabpage, "View:close_internal") local tree_win = self:get_winid(tabid, "View:close_internal")
local current_win = vim.api.nvim_get_current_win() local current_win = vim.api.nvim_get_current_win()
for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabid)) do
if vim.api.nvim_win_get_config(win).relative == "" then if vim.api.nvim_win_get_config(win).relative == "" then
local prev_win = vim.fn.winnr("#") -- this tab only local prev_win = vim.fn.winnr("#") -- this tab only
if tree_win == current_win and prev_win > 0 then if tree_win == current_win and prev_win > 0 then
@ -264,7 +264,7 @@ function View:close_internal(tabpage)
end end
if vim.api.nvim_win_is_valid(tree_win or 0) then if vim.api.nvim_win_is_valid(tree_win or 0) then
if self.explorer.opts.experimental.multi_instance then if self.explorer.opts.experimental.multi_instance then
log.line("dev", "View:close_internal(t%s) w%s", tabpage, tree_win) log.line("dev", "View:close_internal(t%s) w%s", tabid, tree_win)
end end
local success, error = pcall(vim.api.nvim_win_close, tree_win or 0, true) local success, error = pcall(vim.api.nvim_win_close, tree_win or 0, true)
if not success then if not success then
@ -278,26 +278,34 @@ function View:close_internal(tabpage)
end end
function View:close_this_tab_only() function View:close_this_tab_only()
if self.explorer.opts.experimental.multi_instance then
log.line("dev", "View:close_this_tab_only()")
end
self:close_internal(vim.api.nvim_get_current_tabpage()) self:close_internal(vim.api.nvim_get_current_tabpage())
end end
-- TODO this is broken at 1.13.0 - current tab does not close when tab.sync.close is set
function View:close_all_tabs() function View:close_all_tabs()
for tabpage, _ in pairs(globals.WINID_PER_TAB) do log.line("dev", "View:close_all_tabs() globals.WINID_BY_TABID=%s", vim.inspect(globals.WINID_BY_TABID))
self:close_internal(tabpage) for tabid, _ in pairs(globals.WINID_BY_TABID) do
if self.explorer.opts.experimental.multi_instance then
log.line("dev", "View:close_all_tabs()")
end
self:close_internal(tabid)
end end
end end
---@param tabpage integer|nil ---@param tabid integer|nil
---@param callsite string ---@param callsite string
function View:close(tabpage, callsite) function View:close(tabid, callsite)
if self.explorer.opts.experimental.multi_instance then if self.explorer.opts.experimental.multi_instance then
log.line("dev", "View:close(t%s, %s)", tabpage, callsite) log.line("dev", "View:close(t%s, %s)", tabid, callsite)
end end
if self.explorer.opts.tab.sync.close then if self.explorer.opts.tab.sync.close then
self:close_all_tabs() self:close_all_tabs()
elseif tabpage then elseif tabid then
self:close_internal(tabpage) self:close_internal(tabid)
else else
self:close_this_tab_only() self:close_this_tab_only()
end end
@ -425,7 +433,7 @@ end
---@private ---@private
function View:set_current_win() function View:set_current_win()
local current_tab = vim.api.nvim_get_current_tabpage() local current_tab = vim.api.nvim_get_current_tabpage()
globals.WINID_PER_TAB[current_tab] = vim.api.nvim_get_current_win() globals.WINID_BY_TABID[current_tab] = vim.api.nvim_get_current_win()
end end
---@class OpenInWinOpts ---@class OpenInWinOpts
@ -442,7 +450,7 @@ function View:open_in_win(opts)
vim.api.nvim_set_current_win(opts.winid) vim.api.nvim_set_current_win(opts.winid)
end end
self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
globals.WINID_PER_TAB[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win() globals.WINID_BY_TABID[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win()
self:set_current_win() self:set_current_win()
self:set_window_options_and_buffer() self:set_window_options_and_buffer()
if opts.resize then if opts.resize then
@ -458,26 +466,26 @@ function View:abandon_current_window()
if self.explorer.opts.experimental.multi_instance then if self.explorer.opts.experimental.multi_instance then
log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s", log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s",
tab, tab,
globals.WINID_PER_TAB[tab], globals.WINID_BY_TABID[tab],
globals.BUFNR_PER_TAB[tab], globals.BUFNR_BY_TABID[tab],
self.bufnr_by_tab[tab], self.bufnr_by_tabid[tab],
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") (globals.BUFNR_BY_TABID[tab] == self.bufnr_by_tabid[tab]) and "" or "MISMATCH")
end end
-- TODO multi-instance maybe kill the buffer instead of retaining -- TODO multi-instance maybe kill the buffer instead of retaining
-- reset both bufnr registries -- reset both bufnr registries
globals.BUFNR_PER_TAB[tab] = nil globals.BUFNR_BY_TABID[tab] = nil
self.bufnr_by_tab[tab] = nil self.bufnr_by_tabid[tab] = nil
globals.WINID_PER_TAB[tab] = nil globals.WINID_BY_TABID[tab] = nil
end end
function View:abandon_all_windows() function View:abandon_all_windows()
-- TODO multi-instance kill the buffer instead of retaining -- TODO multi-instance kill the buffer instead of retaining
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
globals.BUFNR_PER_TAB[tab] = nil globals.BUFNR_BY_TABID[tab] = nil
globals.WINID_PER_TAB[tab] = nil globals.WINID_BY_TABID[tab] = nil
end end
end end
@ -486,15 +494,15 @@ end
function View:is_visible(opts) function View:is_visible(opts)
-- TODO multi-instance rewrite and consistency check -- TODO multi-instance rewrite and consistency check
if opts and opts.tabpage then if opts and opts.tabpage then
if not globals.WINID_PER_TAB[opts.tabpage] then if not globals.WINID_BY_TABID[opts.tabpage] then
return false return false
end end
local winid = globals.WINID_PER_TAB[opts.tabpage] local winid = globals.WINID_BY_TABID[opts.tabpage]
return winid and vim.api.nvim_win_is_valid(winid) return winid and vim.api.nvim_win_is_valid(winid)
end end
if opts and opts.any_tabpage then if opts and opts.any_tabpage then
for _, winid in pairs(globals.WINID_PER_TAB) do for _, winid in pairs(globals.WINID_BY_TABID) do
if winid and vim.api.nvim_win_is_valid(winid) then if winid and vim.api.nvim_win_is_valid(winid) then
return true return true
end end
@ -548,20 +556,19 @@ end
--- Restores the state of a NvimTree window if it was initialized before. --- Restores the state of a NvimTree window if it was initialized before.
function View:restore_tab_state() function View:restore_tab_state()
local tabpage = vim.api.nvim_get_current_tabpage() self:set_cursor(globals.CURSORS[vim.api.nvim_get_current_tabpage()])
self:set_cursor(globals.CURSORS[tabpage])
end end
--- TODO multi-instance remove comment --- TODO multi-instance remove comment
--- not legacy codepath --- not legacy codepath
--- winid containing the buffer --- winid containing the buffer
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. ---@param tabid number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return integer? winid ---@return integer? winid
function View:winid(tabpage) function View:winid(tabid)
local bufnr = self.bufnr_by_tab[tabpage] local bufnr = self.bufnr_by_tabid[tabid]
if bufnr then if bufnr then
for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabid or 0)) do
if vim.api.nvim_win_get_buf(winid) == bufnr then if vim.api.nvim_win_get_buf(winid) == bufnr then
return winid return winid
end end
@ -570,22 +577,22 @@ function View:winid(tabpage)
end end
--- Returns the window number for nvim-tree within the tabpage specified --- 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 tabid number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@param callsite string ---@param callsite string
---@return number|nil ---@return number|nil
function View:get_winid(tabpage, callsite) function View:get_winid(tabid, callsite)
local tabid = tabpage or vim.api.nvim_get_current_tabpage() local tabid_param = tabid
tabid = tabid or vim.api.nvim_get_current_tabpage()
local tabinfo_winid = nil local tabinfo_winid = nil
if self.explorer.opts.experimental.multi_instance then if self.explorer.opts.experimental.multi_instance then
local msg_fault = "" local msg_fault = ""
if not globals.WINID_PER_TAB[tabid] then if not globals.WINID_BY_TABID[tabid] then
msg_fault = "no WINID_PER_TAB" msg_fault = "no WINID_BY_TABID"
elseif not vim.api.nvim_win_is_valid(globals.WINID_PER_TAB[tabid]) then elseif not vim.api.nvim_win_is_valid(globals.WINID_BY_TABID[tabid]) then
msg_fault = string.format("invalid globals.WINID_PER_TAB[tabid] %d", globals.WINID_PER_TAB[tabid]) msg_fault = string.format("invalid globals.WINID_BY_TABID[tabid] %d", globals.WINID_BY_TABID[tabid])
else else
tabinfo_winid = globals.WINID_PER_TAB[tabid] tabinfo_winid = globals.WINID_BY_TABID[tabid]
end end
local winid = self:winid(tabid) local winid = self:winid(tabid)
@ -595,7 +602,7 @@ function View:get_winid(tabpage, callsite)
end end
local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s]=w%s view.winid(%s)=w%s %s", local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s]=w%s view.winid(%s)=w%s %s",
tabpage, tabid_param,
callsite, callsite,
tabid, tabinfo_winid, tabid, tabinfo_winid,
tabid, winid, tabid, winid,
@ -621,20 +628,20 @@ end
function View:get_bufnr(callsite) function View:get_bufnr(callsite)
local tab = vim.api.nvim_get_current_tabpage() local tab = vim.api.nvim_get_current_tabpage()
if self.explorer.opts.experimental.multi_instance then if self.explorer.opts.experimental.multi_instance then
local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_PER_TAB[%s]=b%s view.bufnr_by_tab[%s]=b%s %s", local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_BY_TABID[%s]=b%s view.bufnr_by_tab[%s]=b%s %s",
callsite, callsite,
tab, globals.BUFNR_PER_TAB[tab], tab, globals.BUFNR_BY_TABID[tab],
tab, self.bufnr_by_tab[tab], tab, self.bufnr_by_tabid[tab],
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH" (globals.BUFNR_BY_TABID[tab] == self.bufnr_by_tabid[tab]) and "" or "MISMATCH"
) )
if globals.BUFNR_PER_TAB[tab] ~= self.bufnr_by_tab[tab] then if globals.BUFNR_BY_TABID[tab] ~= self.bufnr_by_tabid[tab] then
notify.error(msg) notify.error(msg)
end end
log.line("dev", msg) log.line("dev", msg)
end end
return globals.BUFNR_PER_TAB[tab] return globals.BUFNR_BY_TABID[tab]
end end
function View:prevent_buffer_override() function View:prevent_buffer_override()
@ -652,9 +659,9 @@ function View:prevent_buffer_override()
--- TODO multi-instance this can be removed as winid() will handle it --- TODO multi-instance this can be removed as winid() will handle it
if not bufname:match("NvimTree") then if not bufname:match("NvimTree") then
for i, winid in ipairs(globals.WINID_PER_TAB) do for i, winid in ipairs(globals.WINID_BY_TABID) do
if winid == view_winid then if winid == view_winid then
globals.WINID_PER_TAB[i] = nil globals.WINID_BY_TABID[i] = nil
break break
end end
end end

View File

@ -2,8 +2,8 @@
local M = { local M = {
-- from View -- from View
WINID_PER_TAB = {}, WINID_BY_TABID = {},
BUFNR_PER_TAB = {}, BUFNR_BY_TABID = {},
CURSORS = {}, CURSORS = {},
} }

View File

@ -3,7 +3,7 @@ local globals = require("nvim-tree.globals")
local M = {} local M = {}
--- Debugging only. --- Debugging only.
--- Tabs show WINID_PER_TAB winid and BUFNR_PER_TAB bufnr for the tab. --- Tabs show WINID_BY_TABID winid and BUFNR_BY_TABID bufnr for the tab.
--- Orphans for inexistent tab_ids are shown at the right. --- Orphans for inexistent tab_ids are shown at the right.
--- lib.target_winid is always shown at the right next to a close button. --- lib.target_winid is always shown at the right next to a close button.
--- Enable with: --- Enable with:
@ -11,30 +11,30 @@ local M = {}
--- vim.opt.showtabline = 2 --- vim.opt.showtabline = 2
---@return string ---@return string
function M.tab_line() function M.tab_line()
local tab_ids = vim.api.nvim_list_tabpages() local tabids = vim.api.nvim_list_tabpages()
local cur_tab_id = vim.api.nvim_get_current_tabpage() local tabid_cur = vim.api.nvim_get_current_tabpage()
local bufnr_per_tab = vim.deepcopy(globals.BUFNR_PER_TAB) local bufnr_by_tabid = vim.deepcopy(globals.BUFNR_BY_TABID)
local tabpages = vim.deepcopy(globals.WINID_PER_TAB) local winid_by_tabid = vim.deepcopy(globals.WINID_BY_TABID)
local tl = "%#TabLine#" local tl = "%#TabLine#"
for i, tab_id in ipairs(tab_ids) do for i, tabid in ipairs(tabids) do
-- click to select -- click to select
tl = tl .. "%" .. i .. "T" tl = tl .. "%" .. i .. "T"
-- style -- style
if tab_id == cur_tab_id then if tabid == tabid_cur then
tl = tl .. "%#StatusLine#|" tl = tl .. "%#StatusLine#|"
else else
tl = tl .. "|%#TabLine#" tl = tl .. "|%#TabLine#"
end end
-- tab_id itself -- tab_id itself
tl = tl .. " t" .. tab_id tl = tl .. " t" .. tabid
-- winid, if present -- winid, if present
local tp = globals.WINID_PER_TAB[tab_id] local tp = globals.WINID_BY_TABID[tabid]
if tp then if tp then
tl = tl .. " w" .. (tp or "nil") tl = tl .. " w" .. (tp or "nil")
else else
@ -42,7 +42,7 @@ function M.tab_line()
end end
-- bufnr, if present -- bufnr, if present
local bpt = globals.BUFNR_PER_TAB[tab_id] local bpt = globals.BUFNR_BY_TABID[tabid]
if bpt then if bpt then
tl = tl .. " b" .. bpt tl = tl .. " b" .. bpt
else else
@ -52,8 +52,8 @@ function M.tab_line()
tl = tl .. " " tl = tl .. " "
-- remove actively mapped -- remove actively mapped
bufnr_per_tab[tab_id] = nil bufnr_by_tabid[tabid] = nil
tabpages[tab_id] = nil winid_by_tabid[tabid] = nil
end end
-- close last and reset -- close last and reset
@ -61,11 +61,11 @@ function M.tab_line()
-- collect orphans -- collect orphans
local orphans = {} local orphans = {}
for tab_id, bufnr in pairs(bufnr_per_tab) do for tab_id, bufnr in pairs(bufnr_by_tabid) do
orphans[tab_id] = orphans[tab_id] or {} orphans[tab_id] = orphans[tab_id] or {}
orphans[tab_id].bufnr = bufnr orphans[tab_id].bufnr = bufnr
end end
for tab_id, tp in pairs(tabpages) do for tab_id, tp in pairs(winid_by_tabid) do
orphans[tab_id] = orphans[tab_id] or {} orphans[tab_id] = orphans[tab_id] or {}
orphans[tab_id].winid = tp orphans[tab_id].winid = tp
end end