Skip to content

Commit 5db04af

Browse files
committed
refactor(#2826): add View.tab_line for debugging
1 parent ca3a817 commit 5db04af

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

lua/nvim-tree/explorer/view.lua

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ M.View = {
3939
---@field private width (fun():integer)|integer|string
4040
---@field private max_width integer
4141
---@field private padding integer
42+
---@field tab_line fun():string
4243
local View = Class:extend()
4344

4445
---@class View
@@ -664,4 +665,102 @@ function View:configure_width(width)
664665
end
665666
end
666667

668+
--- Debugging only.
669+
--- Tabs show TABPAGES winnr and BUFNR_PER_TAB bufnr for the tab.
670+
--- Orphans for inexistent tab_ids are shown at the right.
671+
--- lib.target_winid is always shown at the right next to a close button.
672+
--- Enable with:
673+
--- vim.opt.tabline = "%!v:lua.require('nvim-tree.explorer.view').tab_line()"
674+
--- vim.opt.showtabline = 2
675+
---@return string
676+
function View.tab_line()
677+
local tab_ids = vim.api.nvim_list_tabpages()
678+
local cur_tab_id = vim.api.nvim_get_current_tabpage()
679+
680+
local bufnr_per_tab = vim.deepcopy(BUFNR_PER_TAB)
681+
local tabpages = vim.deepcopy(M.View.tabpages)
682+
683+
local tl = "%#TabLine#"
684+
685+
for i, tab_id in ipairs(tab_ids) do
686+
-- click to select
687+
tl = tl .. "%" .. i .. "T"
688+
689+
-- style
690+
if tab_id == cur_tab_id then
691+
tl = tl .. "%#StatusLine#|"
692+
else
693+
tl = tl .. "|%#TabLine#"
694+
end
695+
696+
-- tab_id itself
697+
tl = tl .. " t" .. tab_id
698+
699+
-- winnr, if present
700+
local tp = M.View.tabpages[tab_id]
701+
if tp then
702+
tl = tl .. " w" .. tp.winnr
703+
else
704+
tl = tl .. " "
705+
end
706+
707+
-- bufnr, if present
708+
local bpt = BUFNR_PER_TAB[tab_id]
709+
if bpt then
710+
tl = tl .. " b" .. bpt
711+
else
712+
tl = tl .. " "
713+
end
714+
715+
tl = tl .. " "
716+
717+
-- remove actively mapped
718+
bufnr_per_tab[tab_id] = nil
719+
tabpages[tab_id] = nil
720+
end
721+
722+
-- close last and reset
723+
tl = tl .. "|%#CursorLine#%T"
724+
725+
-- collect orphans
726+
local orphans = {}
727+
for tab_id, bufnr in pairs(bufnr_per_tab) do
728+
orphans[tab_id] = orphans[tab_id] or {}
729+
orphans[tab_id].bufnr = bufnr
730+
end
731+
for tab_id, tp in pairs(tabpages) do
732+
orphans[tab_id] = orphans[tab_id] or {}
733+
orphans[tab_id].winnr = tp.winnr
734+
end
735+
736+
-- right-align
737+
tl = tl .. "%=%#TabLine#"
738+
739+
-- print orphans
740+
for tab_id, orphan in pairs(orphans) do
741+
-- inexistent tab
742+
tl = tl .. "%#error#| t" .. tab_id
743+
744+
-- maybe winnr
745+
if orphan.winnr then
746+
tl = tl .. " w" .. orphan.winnr
747+
else
748+
tl = tl .. " "
749+
end
750+
751+
-- maybe bufnr
752+
if orphan.bufnr then
753+
tl = tl .. " b" .. orphan.bufnr
754+
else
755+
tl = tl .. " "
756+
end
757+
tl = tl .. " "
758+
end
759+
760+
-- target win id and close button
761+
tl = tl .. "|%#TabLine# twi" .. (require("nvim-tree.lib").target_winid or "?") .. " %999X| X |"
762+
763+
return tl
764+
end
765+
667766
return View

0 commit comments

Comments
 (0)