Skip to content

Commit 223693b

Browse files
committed
refactor(#2826): instrument View:get_bufnr
1 parent 3b792d4 commit 223693b

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

lua/nvim-tree/diagnostics.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function M.update_coc()
185185

186186
local bufnr
187187
if explorer then
188-
bufnr = explorer.view:get_bufnr()
188+
bufnr = explorer.view:get_bufnr("diagnostics.update_coc")
189189
end
190190

191191
local should_draw = bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr)

lua/nvim-tree/explorer/view.lua

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ local DEFAULT_PADDING = 1
3131
---@field private width (fun():integer)|integer|string
3232
---@field private max_width integer
3333
---@field private padding integer
34+
---@field private bufnr_by_tab table<integer, integer> for diagnostics during multi instance
3435
local View = Class:extend()
3536

3637
---@class View
@@ -53,6 +54,7 @@ function View:new(args)
5354
self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions
5455
self.side = (self.explorer.opts.view.side == "right") and "right" or "left"
5556
self.live_filter = { prev_focused_node = nil, }
57+
self.bufnr_by_tab = {}
5658

5759
self.winopts = {
5860
relativenumber = self.explorer.opts.view.relativenumber,
@@ -127,16 +129,17 @@ function View:create_buffer(bufnr)
127129

128130
local tab = vim.api.nvim_get_current_tabpage()
129131
globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
130-
vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab)
132+
self.bufnr_by_tab[tab] = globals.BUFNR_PER_TAB[tab]
133+
vim.api.nvim_buf_set_name(self:get_bufnr("View:create_buffer1"), "NvimTree_" .. tab)
131134

132-
bufnr = self:get_bufnr()
135+
bufnr = self:get_bufnr("View:create_buffer2")
133136
for _, option in ipairs(BUFFER_OPTIONS) do
134137
vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr })
135138
end
136139

137-
require("nvim-tree.keymap").on_attach(self:get_bufnr())
140+
require("nvim-tree.keymap").on_attach(self:get_bufnr("View:create_buffer3"))
138141

139-
events._dispatch_tree_attached_post(self:get_bufnr())
142+
events._dispatch_tree_attached_post(self:get_bufnr("View:create_buffer4"))
140143
end
141144

142145
---@private
@@ -171,7 +174,7 @@ local move_tbl = {
171174
-- setup_tabpage sets up the initial state of a tab
172175
---@private
173176
---@param tabpage integer
174-
---@param callsite string?
177+
---@param callsite string
175178
function View:setup_tabpage(tabpage, callsite)
176179
local winnr = vim.api.nvim_get_current_win()
177180

@@ -188,7 +191,7 @@ end
188191

189192
---@private
190193
function View:set_window_options_and_buffer()
191-
pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr())
194+
pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr("View:set_window_options_and_buffer"))
192195

193196
if vim.fn.has("nvim-0.10") == 1 then
194197
local eventignore = vim.api.nvim_get_option_value("eventignore", {})
@@ -348,7 +351,7 @@ end
348351
---@private
349352
function View:grow()
350353
local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
351-
local lines = vim.api.nvim_buf_get_lines(self:get_bufnr(), starts_at, -1, false)
354+
local lines = vim.api.nvim_buf_get_lines(self:get_bufnr("View:grow1"), starts_at, -1, false)
352355
-- number of columns of right-padding to indicate end of path
353356
local padding = self:get_size(self.padding)
354357

@@ -372,7 +375,7 @@ function View:grow()
372375
for line_nr, l in pairs(lines) do
373376
local count = vim.fn.strchars(l)
374377
-- also add space for right-aligned icons
375-
local extmarks = vim.api.nvim_buf_get_extmarks(self:get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true })
378+
local extmarks = vim.api.nvim_buf_get_extmarks(self:get_bufnr("View:grow2"), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true })
376379
count = count + utils.extmarks_length(extmarks)
377380
if resizing_width < count then
378381
resizing_width = count
@@ -449,7 +452,7 @@ function View:set_current_win(callsite)
449452
local current_win = vim.api.nvim_get_current_win()
450453

451454
if self.explorer.opts.experimental.multi_instance_debug then
452-
log.line("dev", "View:set_current_win(%-20.20s) t%d w%3d->w%3d %s",
455+
log.line("dev", "View:set_current_win(%-20.20s) t%d w%3s->w%3s %s",
453456
callsite,
454457
current_tab,
455458
globals.TABPAGES[current_tab].winnr,
@@ -483,6 +486,7 @@ end
483486
function View:abandon_current_window()
484487
local tab = vim.api.nvim_get_current_tabpage()
485488
globals.BUFNR_PER_TAB[tab] = nil
489+
self.bufnr_by_tab[tab] = nil
486490
if globals.TABPAGES[tab] then
487491
globals.TABPAGES[tab].winnr = nil
488492
end
@@ -491,6 +495,7 @@ end
491495
function View:abandon_all_windows()
492496
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
493497
globals.BUFNR_PER_TAB[tab] = nil
498+
self.bufnr_by_tab[tab] = nil
494499
if globals.TABPAGES[tab] then
495500
globals.TABPAGES[tab].winnr = nil
496501
end
@@ -568,7 +573,7 @@ end
568573

569574
--- Returns the window number for nvim-tree within the tabpage specified
570575
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
571-
---@param callsite string? for logging purposes
576+
---@param callsite string for logging purposes
572577
---@return number|nil
573578
function View:get_winnr(tabpage, callsite)
574579
if self.explorer.opts.experimental.multi_instance_debug then
@@ -603,14 +608,24 @@ function View:get_winnr(tabpage, callsite)
603608
end
604609

605610
--- Returns the current nvim tree bufnr
611+
---@param callsite string
606612
---@return number
607-
function View:get_bufnr()
608-
return globals.BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()]
613+
function View:get_bufnr(callsite)
614+
local tab = vim.api.nvim_get_current_tabpage()
615+
if self.explorer.opts.experimental.multi_instance_debug then
616+
log.line("dev", "View:get_bufnr(%-20.20s) t%d global b%s member b%s %s",
617+
callsite,
618+
tab,
619+
globals.BUFNR_PER_TAB[tab],
620+
self.bufnr_by_tab[tab],
621+
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
622+
end
623+
return globals.BUFNR_PER_TAB[tab]
609624
end
610625

611626
function View:prevent_buffer_override()
612627
local view_winnr = self:get_winnr(nil, "View:prevent_buffer_override")
613-
local view_bufnr = self:get_bufnr()
628+
local view_bufnr = self:get_bufnr("View:prevent_buffer_override")
614629

615630
-- need to schedule to let the new buffer populate the window
616631
-- because this event needs to be run on bufWipeout.

lua/nvim-tree/renderer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function Renderer:render_hl(bufnr, hl_range_args)
101101
end
102102

103103
function Renderer:draw()
104-
local bufnr = self.explorer.view:get_bufnr()
104+
local bufnr = self.explorer.view:get_bufnr("Renderer:draw")
105105
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
106106
return
107107
end

0 commit comments

Comments
 (0)