Skip to content

Commit cdc4395

Browse files
committed
refactor(#2826): instrument View:get_winnr
1 parent 127e060 commit cdc4395

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ local function usable_win_ids()
2222
local explorer = core.get_explorer()
2323
local tabpage = vim.api.nvim_get_current_tabpage()
2424
local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
25-
local tree_winid = explorer and explorer.view:get_winnr(tabpage)
25+
local tree_winid = explorer and explorer.view:get_winnr(tabpage, "open-file.usable_win_ids")
2626

2727
return vim.tbl_filter(function(id)
2828
local bufid = vim.api.nvim_win_get_buf(id)

lua/nvim-tree/explorer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ end
548548
---nil on no explorer or invalid view win
549549
---@return integer[]|nil
550550
function Explorer:get_cursor_position()
551-
local winnr = self.view:get_winnr()
551+
local winnr = self.view:get_winnr(nil, "Explorer:get_cursor_position")
552552
if not winnr or not vim.api.nvim_win_is_valid(winnr) then
553553
return
554554
end

lua/nvim-tree/explorer/view.lua

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ end
261261
---@param tabnr integer
262262
function View:save_tab_state(tabnr)
263263
local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
264-
globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0)
264+
globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage, "View:save_tab_state") or 0)
265265
end
266266

267267
---@private
@@ -272,7 +272,7 @@ function View:close_internal(tabpage)
272272
end
273273
self:save_tab_state(tabpage)
274274
switch_buf_if_last_buf()
275-
local tree_win = self:get_winnr(tabpage)
275+
local tree_win = self:get_winnr(tabpage, "View:close_internal")
276276
local current_win = vim.api.nvim_get_current_win()
277277
for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
278278
if vim.api.nvim_win_get_config(win).relative == "" then
@@ -343,7 +343,7 @@ function View:grow()
343343
local padding = self:get_size(self.padding)
344344

345345
-- account for sign/number columns etc.
346-
local wininfo = vim.fn.getwininfo(self:get_winnr())
346+
local wininfo = vim.fn.getwininfo(self:get_winnr(nil, "View:grow"))
347347
if type(wininfo) == "table" and type(wininfo[1]) == "table" then
348348
padding = padding + wininfo[1].textoff
349349
end
@@ -412,7 +412,7 @@ function View:resize(size)
412412
return
413413
end
414414

415-
local winnr = self:get_winnr() or 0
415+
local winnr = self:get_winnr(nil, "View:resize") or 0
416416

417417
local new_size = self:get_width()
418418

@@ -495,25 +495,25 @@ function View:is_visible(opts)
495495
return false
496496
end
497497

498-
return self:get_winnr() ~= nil and vim.api.nvim_win_is_valid(self:get_winnr() or 0)
498+
return self:get_winnr(nil, "View:is_visible1") ~= nil and vim.api.nvim_win_is_valid(self:get_winnr(nil, "View:is_visible2") or 0)
499499
end
500500

501501
---@param opts table|nil
502502
function View:set_cursor(opts)
503503
if self:is_visible() then
504-
pcall(vim.api.nvim_win_set_cursor, self:get_winnr(), opts)
504+
pcall(vim.api.nvim_win_set_cursor, self:get_winnr(nil, "View:set_cursor"), opts)
505505
end
506506
end
507507

508508
---@param winnr number|nil
509509
---@param open_if_closed boolean|nil
510510
function View:focus(winnr, open_if_closed)
511-
local wnr = winnr or self:get_winnr()
511+
local wnr = winnr or self:get_winnr(nil, "View:focus1")
512512

513513
if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then
514514
self:close()
515515
self:open()
516-
wnr = self:get_winnr()
516+
wnr = self:get_winnr(nil, "View:focus2")
517517
elseif open_if_closed and not self:is_visible() then
518518
self:open()
519519
end
@@ -532,7 +532,7 @@ function View:winid(opts)
532532
tabpage = vim.api.nvim_get_current_tabpage()
533533
end
534534
if self:is_visible({ tabpage = tabpage }) then
535-
return self:get_winnr(tabpage)
535+
return self:get_winnr(tabpage, "View:winid")
536536
else
537537
return nil
538538
end
@@ -546,12 +546,37 @@ end
546546

547547
--- Returns the window number for nvim-tree within the tabpage specified
548548
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
549+
---@param callsite string? for logging purposes
549550
---@return number|nil
550-
function View:get_winnr(tabpage)
551-
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
552-
local tabinfo = globals.TABPAGES[tabpage]
553-
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
554-
return tabinfo.winnr
551+
function View:get_winnr(tabpage, callsite)
552+
if self.explorer.opts.experimental.multi_instance_debug then
553+
local msg = string.format("View:get_winnr(%3s, %-20.20s)", tabpage, callsite)
554+
555+
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
556+
local tabinfo = globals.TABPAGES[tabpage]
557+
558+
local ret = nil
559+
560+
if not tabinfo then
561+
msg = string.format("%s t%d no tabinfo", msg, tabpage)
562+
elseif not tabinfo.winnr then
563+
msg = string.format("%s t%d no tabinfo.winnr", msg, tabpage)
564+
elseif not vim.api.nvim_win_is_valid(tabinfo.winnr) then
565+
msg = string.format("%s t%d invalid tabinfo.winnr %d", msg, tabpage, tabinfo.winnr)
566+
else
567+
msg = string.format("%s t%d w%d", msg, tabpage, tabinfo.winnr)
568+
ret = tabinfo.winnr
569+
end
570+
571+
log.line("dev", "%s", msg)
572+
573+
return ret
574+
else
575+
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
576+
local tabinfo = globals.TABPAGES[tabpage]
577+
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
578+
return tabinfo.winnr
579+
end
555580
end
556581
end
557582

@@ -562,7 +587,7 @@ function View:get_bufnr()
562587
end
563588

564589
function View:prevent_buffer_override()
565-
local view_winnr = self:get_winnr()
590+
local view_winnr = self:get_winnr(nil, "View:get_bufnr")
566591
local view_bufnr = self:get_bufnr()
567592

568593
-- need to schedule to let the new buffer populate the window
@@ -617,9 +642,9 @@ end
617642

618643
-- used on ColorScheme event
619644
function View:reset_winhl()
620-
local winnr = self:get_winnr()
645+
local winnr = self:get_winnr(nil, "View:reset_winhl1")
621646
if winnr and vim.api.nvim_win_is_valid(winnr) then
622-
vim.wo[self:get_winnr()].winhl = appearance.WIN_HL
647+
vim.wo[self:get_winnr(nil, "View:reset_winhl2")].winhl = appearance.WIN_HL
623648
end
624649
end
625650

lua/nvim-tree/lib.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function M.set_target_win()
1515

1616
local id = vim.api.nvim_get_current_win()
1717

18-
if explorer and id == explorer.view:get_winnr() then
18+
if explorer and id == explorer.view:get_winnr(nil, "lib.set_target_win") then
1919
M.target_winid = 0
2020
return
2121
end

lua/nvim-tree/renderer/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,21 @@ function Renderer:draw()
108108

109109
local profile = log.profile_start("draw")
110110

111-
local cursor = vim.api.nvim_win_get_cursor(self.explorer.view:get_winnr() or 0)
111+
local cursor = vim.api.nvim_win_get_cursor(self.explorer.view:get_winnr(nil, "Renderer:draw1") or 0)
112112

113113
local builder = Builder(self.explorer):build()
114114

115115
self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines)
116116

117117
if cursor and #builder.lines >= cursor[1] then
118-
vim.api.nvim_win_set_cursor(self.explorer.view:get_winnr() or 0, cursor)
118+
vim.api.nvim_win_set_cursor(self.explorer.view:get_winnr(nil, "Renderer:draw2") or 0, cursor)
119119
end
120120

121121
self.explorer.view:grow_from_content()
122122

123123
log.profile_end(profile)
124124

125-
events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr())
125+
events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr(nil, "Renderer:draw3"))
126126
end
127127

128128
return Renderer

0 commit comments

Comments
 (0)