Revert "refactor(#2826): View tracks winids and bufnrs via events, unused for now (#3170)"

This reverts commit 65bae44922.
This commit is contained in:
Alexander Courtis 2025-08-10 11:06:56 +10:00
parent 99b2980283
commit fd0bcad2c1
2 changed files with 34 additions and 77 deletions

View File

@ -165,6 +165,22 @@ function Explorer:create_autocmds()
end, end,
}) })
-- prevent new opened file from opening in the same window as nvim-tree
vim.api.nvim_create_autocmd("BufWipeout", {
group = self.augroup_id,
pattern = "NvimTree_*",
callback = function()
if not utils.is_nvim_tree_buf(0) then
return
end
if self.opts.actions.open_file.eject then
self.view:prevent_buffer_override()
else
self.view:abandon_current_window()
end
end,
})
vim.api.nvim_create_autocmd("BufEnter", { vim.api.nvim_create_autocmd("BufEnter", {
group = self.augroup_id, group = self.augroup_id,
pattern = "NvimTree_*", pattern = "NvimTree_*",

View File

@ -18,9 +18,8 @@ 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
-- TODO multi-instance replace with single members -- TODO multi-instance remove or replace with single member
---@field private bufnr_by_tabid table<integer, integer> ---@field private bufnr_by_tabid table<integer, integer>
---@field private winid_by_tabid table<integer, integer>
local View = Class:extend() local View = Class:extend()
---@class View ---@class View
@ -34,14 +33,13 @@ local View = Class:extend()
function View:new(args) function View:new(args)
args.explorer:log_new("View") args.explorer:log_new("View")
self.explorer = args.explorer self.explorer = args.explorer
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_tabid = {} self.bufnr_by_tabid = {}
self.winid_by_tabid = {}
self.winopts = { self.winopts = {
relativenumber = self.explorer.opts.view.relativenumber, relativenumber = self.explorer.opts.view.relativenumber,
number = self.explorer.opts.view.number, number = self.explorer.opts.view.number,
list = false, list = false,
@ -62,6 +60,10 @@ function View:new(args)
self:configure_width(self.explorer.opts.view.width) self:configure_width(self.explorer.opts.view.width)
self.initial_width = self:get_width() self.initial_width = self:get_width()
-- TODO multi-instance remove this; delete buffers rather than retaining them
local tabid = vim.api.nvim_get_current_tabpage()
self.bufnr_by_tabid[tabid] = globals.BUFNR_BY_TABID[tabid]
end end
function View:destroy() function View:destroy()
@ -78,64 +80,6 @@ local BUFFER_OPTIONS = {
{ name = "swapfile", value = false }, { name = "swapfile", value = false },
} }
---Buffer local autocommands to track state, deleted on buffer wipeout
---@private
---@param bufnr integer
function View:create_autocmds(bufnr)
-- clear bufnr and winid
-- eject buffer opened in the nvim-tree window and create a new buffer
vim.api.nvim_create_autocmd("BufWipeout", {
group = self.explorer.augroup_id,
buffer = bufnr,
callback = function(data)
log.line("dev",
"View BufWipeout\n bufnr = %s\n data.buf = %s\n self.bufnr_by_tabid = %s\n self.winid_by_tabid = %s",
bufnr,
data.buf,
vim.inspect(self.bufnr_by_tabid, { newline = "" }),
vim.inspect(self.winid_by_tabid, { newline = "" }),
vim.inspect(data, { newline = "" })
)
-- clear the tab's buffer
self.bufnr_by_tabid = vim.tbl_map(function(b)
return b ~= bufnr and b or nil
end, self.bufnr_by_tabid)
-- clear the tab's window(s)
local winids = vim.fn.win_findbuf(bufnr)
self.winid_by_tabid = vim.tbl_map(function(winid)
return not vim.tbl_contains(winids, winid) and winid or nil
end, self.winid_by_tabid)
if self.explorer.opts.actions.open_file.eject then
self:prevent_buffer_override()
else
self:abandon_current_window()
end
end,
})
-- set winid
vim.api.nvim_create_autocmd("BufWinEnter", {
group = self.explorer.augroup_id,
buffer = bufnr,
callback = function(data)
local tabid = vim.api.nvim_get_current_tabpage()
log.line("dev",
"View BufWinEnter\n bufnr = %s\n data.buf = %s\n self.bufnr_by_tabid = %s\n self.winid_by_tabid = %s",
bufnr,
data.buf,
vim.inspect(self.bufnr_by_tabid, { newline = "" }),
vim.inspect(self.winid_by_tabid, { newline = "" })
)
self.winid_by_tabid[tabid] = vim.fn.bufwinid(data.buf) -- first on current tabpage
end,
})
end
-- TODO multi-instance remove this; delete buffers rather than retaining them -- TODO multi-instance remove this; delete buffers rather than retaining them
---@private ---@private
---@param bufnr integer ---@param bufnr integer
@ -168,9 +112,9 @@ function View:create_buffer(bufnr)
bufnr = bufnr or vim.api.nvim_create_buf(false, false) bufnr = bufnr or vim.api.nvim_create_buf(false, false)
self.bufnr_by_tabid[tabid] = bufnr -- set both bufnr registries
globals.BUFNR_BY_TABID[tabid] = bufnr globals.BUFNR_BY_TABID[tabid] = bufnr
self.bufnr_by_tabid[tabid] = bufnr
vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tabid) vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tabid)
@ -178,8 +122,6 @@ function View:create_buffer(bufnr)
vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr }) vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr })
end end
self:create_autocmds(bufnr)
require("nvim-tree.keymap").on_attach(bufnr) require("nvim-tree.keymap").on_attach(bufnr)
events._dispatch_tree_attached_post(bufnr) events._dispatch_tree_attached_post(bufnr)
@ -216,9 +158,7 @@ local move_tbl = {
---@private ---@private
function View:set_window_options_and_buffer() function View:set_window_options_and_buffer()
if not pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr()) then pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr())
return
end
if vim.fn.has("nvim-0.10") == 1 then if vim.fn.has("nvim-0.10") == 1 then
local eventignore = vim.api.nvim_get_option_value("eventignore", {}) local eventignore = vim.api.nvim_get_option_value("eventignore", {})
@ -506,7 +446,9 @@ end
function View:abandon_current_window() function View:abandon_current_window()
local tab = vim.api.nvim_get_current_tabpage() local tab = vim.api.nvim_get_current_tabpage()
-- reset both bufnr registries
globals.BUFNR_BY_TABID[tab] = nil globals.BUFNR_BY_TABID[tab] = nil
self.bufnr_by_tabid[tab] = nil
globals.WINID_BY_TABID[tab] = nil globals.WINID_BY_TABID[tab] = nil
end end
@ -590,7 +532,7 @@ end
---@param tabid 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(tabid) function View:winid(tabid)
local bufnr = globals.BUFNR_BY_TABID[tabid] local bufnr = self.bufnr_by_tabid[tabid]
if bufnr then if bufnr then
for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabid or 0)) do for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabid or 0)) do
@ -601,7 +543,6 @@ function View:winid(tabid)
end end
end end
--- TODO this needs to be refactored away; it's private now to contain it
--- Returns the window number for nvim-tree within the tabpage specified --- Returns the window number for nvim-tree within the tabpage specified
---@param tabid 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 number|nil ---@return number|nil
@ -615,7 +556,7 @@ end
function View:get_bufnr() function View:get_bufnr()
local tab = vim.api.nvim_get_current_tabpage() local tab = vim.api.nvim_get_current_tabpage()
return globals.BUFNR_BY_TABID[tab] return self.bufnr_by_tabid[tab]
end end
function View:prevent_buffer_override() function View:prevent_buffer_override()