From 25d1092710eeb55d6ac86df9760d423b7c092ab5 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Apr 2025 12:45:22 +1000 Subject: [PATCH 01/32] refactor(#2826): singleton View class, WIP --- lua/nvim-tree/actions/tree/resize.lua | 4 +- lua/nvim-tree/view.lua | 144 ++++++++++++++++---------- 2 files changed, 93 insertions(+), 55 deletions(-) diff --git a/lua/nvim-tree/actions/tree/resize.lua b/lua/nvim-tree/actions/tree/resize.lua index e8d4e950729..35b383705ac 100644 --- a/lua/nvim-tree/actions/tree/resize.lua +++ b/lua/nvim-tree/actions/tree/resize.lua @@ -7,7 +7,7 @@ local M = {} function M.fn(opts) if opts == nil then -- reset to config values - view.configure_width() + view.View:configure_width() view.resize() return end @@ -16,7 +16,7 @@ function M.fn(opts) local width_cfg = options.width if width_cfg ~= nil then - view.configure_width(width_cfg) + view.View:configure_width(width_cfg) view.resize() return end diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 4ce95bfb6f4..1d25e6d1f96 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -3,27 +3,55 @@ local utils = require("nvim-tree.utils") local log = require("nvim-tree.log") local notify = require("nvim-tree.notify") +local Class = require("nvim-tree.classic") + ---@class OpenInWinOpts ---@field hijack_current_buf boolean|nil default true ---@field resize boolean|nil default true ---@field winid number|nil 0 or nil for current -local M = {} - -local DEFAULT_MIN_WIDTH = 30 -local DEFAULT_MAX_WIDTH = -1 -local DEFAULT_PADDING = 1 - -M.View = { - adaptive_size = false, - centralize_selection = false, - tabpages = {}, - cursors = {}, - hide_root_folder = false, - live_filter = { +--TODO attempt to type the tables, at least the options ones + +---@class (exact) View: Class +---@field live_filter table +---@field side string +---@field float table +---TODO private below here +---@field explorer Explorer +---@field adaptive_size boolean +---@field centralize_selection boolean +---@field tabpages table +---@field cursors table +---@field hide_root_folder boolean +---@field winopts table +---@field height integer +---@field tab table +---@field preserve_window_proportions boolean +---@field initial_width integer +---@field width (fun():integer)|integer|string +---@field max_width integer +---@field padding integer +local View = Class:extend() + +---@class View +---@overload fun(args: ViewArgs): View + +---@class (exact) ViewArgs +---@field explorer Explorer + +---@protected +---@param args ViewArgs +function View:new(args) + self.explorer = args.explorer + self.adaptive_size = false + self.centralize_selection = false + self.tabpages = {} + self.cursors = {} + self.hide_root_folder = false + self.live_filter = { prev_focused_node = nil, - }, - winopts = { + } + self.winopts = { relativenumber = false, number = false, list = false, @@ -53,8 +81,30 @@ M.View = { "NormalFloat:NvimTreeNormalFloat", "FloatBorder:NvimTreeNormalFloatBorder", }, ","), - }, -} + } + + self.centralize_selection = self.explorer.opts.view.centralize_selection + self.side = (self.explorer.opts.view.side == "right") and "right" or "left" + self.height = self.explorer.opts.view.height + self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false + self.tab = self.explorer.opts.tab + self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions + self.winopts.cursorline = self.explorer.opts.view.cursorline + self.winopts.number = self.explorer.opts.view.number + self.winopts.relativenumber = self.explorer.opts.view.relativenumber + self.winopts.signcolumn = self.explorer.opts.view.signcolumn + self.float = self.explorer.opts.view.float + + self:configure_width(self.explorer.opts.view.width) + + self.initial_width = self:get_width() +end + +local M = {} + +local DEFAULT_MIN_WIDTH = 30 +local DEFAULT_MAX_WIDTH = -1 +local DEFAULT_PADDING = 1 -- The initial state of a tab local tabinitial = { @@ -113,13 +163,14 @@ local function create_buffer(bufnr) events._dispatch_tree_attached_post(M.get_bufnr()) end +---@private ---@param size (fun():integer)|integer|string ---@return integer -local function get_size(size) +function View:get_size(size) if type(size) == "number" then return size elseif type(size) == "function" then - return get_size(size()) + return self:get_size(size()) end local size_as_number = tonumber(size:sub(0, -2)) local percent_as_decimal = size_as_number / 100 @@ -128,11 +179,11 @@ end ---@param size (fun():integer)|integer|nil ---@return integer -local function get_width(size) +function View:get_width(size) if size then - return get_size(size) + return self:get_size(size) else - return get_size(M.View.width) + return self:get_size(self.width) end end @@ -306,7 +357,7 @@ local function grow() local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) -- number of columns of right-padding to indicate end of path - local padding = get_size(M.View.padding) + local padding = M.View:get_size(M.View.padding) -- account for sign/number columns etc. local wininfo = vim.fn.getwininfo(M.get_winnr()) @@ -321,7 +372,7 @@ local function grow() if M.View.max_width == -1 then max_width = -1 else - max_width = get_width(M.View.max_width) - padding + max_width = M.View:get_width(M.View.max_width) - padding end local ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"] @@ -380,7 +431,7 @@ function M.resize(size) local winnr = M.get_winnr() or 0 - local new_size = get_width() + local new_size = M.View:get_width() if new_size ~= vim.api.nvim_win_get_width(winnr) then vim.api.nvim_win_set_width(winnr, new_size) @@ -595,45 +646,32 @@ end ---Configure width-related config ---@param width string|function|number|table|nil -function M.configure_width(width) +function View:configure_width(width) if type(width) == "table" then - M.View.adaptive_size = true - M.View.width = width.min or DEFAULT_MIN_WIDTH - M.View.max_width = width.max or DEFAULT_MAX_WIDTH - M.View.padding = width.padding or DEFAULT_PADDING + self.adaptive_size = true + self.width = width.min or DEFAULT_MIN_WIDTH + self.max_width = width.max or DEFAULT_MAX_WIDTH + self.padding = width.padding or DEFAULT_PADDING elseif width == nil then - if M.config.width ~= nil then + if self.explorer.opts.view.width ~= nil then -- if we had input config - fallback to it - M.configure_width(M.config.width) + self:configure_width(self.explorer.opts.view.width) else -- otherwise - restore initial width - M.View.width = M.View.initial_width + self.width = self.initial_width end else - M.View.adaptive_size = false - M.View.width = width + self.adaptive_size = false + self.width = width end end function M.setup(opts) - local options = opts.view or {} - M.View.centralize_selection = options.centralize_selection - M.View.side = (options.side == "right") and "right" or "left" - M.View.height = options.height - M.View.hide_root_folder = opts.renderer.root_folder_label == false - M.View.tab = opts.tab - M.View.preserve_window_proportions = options.preserve_window_proportions - M.View.winopts.cursorline = options.cursorline - M.View.winopts.number = options.number - M.View.winopts.relativenumber = options.relativenumber - M.View.winopts.signcolumn = options.signcolumn - M.View.float = options.float - M.on_attach = opts.on_attach - - M.config = options - M.configure_width(options.width) - - M.View.initial_width = get_width() + M.View = View({ + explorer = { + opts = opts + } + }) end return M From ca7979a06f209c09ea4d3961fd550fde8b4550d5 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Apr 2025 12:48:22 +1000 Subject: [PATCH 02/32] refactor(#2826): singleton View class, WIP --- lua/nvim-tree/view.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 1d25e6d1f96..72e3f8688d4 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -10,6 +10,12 @@ local Class = require("nvim-tree.classic") ---@field resize boolean|nil default true ---@field winid number|nil 0 or nil for current +local M = {} + +local DEFAULT_MIN_WIDTH = 30 +local DEFAULT_MAX_WIDTH = -1 +local DEFAULT_PADDING = 1 + --TODO attempt to type the tables, at least the options ones ---@class (exact) View: Class @@ -100,12 +106,6 @@ function View:new(args) self.initial_width = self:get_width() end -local M = {} - -local DEFAULT_MIN_WIDTH = 30 -local DEFAULT_MAX_WIDTH = -1 -local DEFAULT_PADDING = 1 - -- The initial state of a tab local tabinitial = { -- The position of the cursor { line, column } From 0a052b7dee18cfe9934d76deb603846a5da1b15f Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Apr 2025 13:20:41 +1000 Subject: [PATCH 03/32] refactor(#2826): singleton View class, WIP --- lua/nvim-tree.lua | 4 +- lua/nvim-tree/actions/node/open-file.lua | 8 +-- lua/nvim-tree/actions/tree/toggle.lua | 2 +- lua/nvim-tree/api.lua | 4 +- lua/nvim-tree/explorer/live-filter.lua | 2 +- lua/nvim-tree/lib.lua | 4 +- lua/nvim-tree/view.lua | 81 +++++++++++++----------- 7 files changed, 57 insertions(+), 48 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index c4cdbcf74f0..33aa6bcb97e 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -89,7 +89,7 @@ function M.tab_enter() return end end - view.open({ focus_tree = false }) + view.View:open({ focus_tree = false }) local explorer = core.get_explorer() if explorer then @@ -692,7 +692,7 @@ local function localise_default_opts() end function M.purge_all_state() - view.close_all_tabs() + view.View:close_all_tabs() view.abandon_all_windows() local explorer = core.get_explorer() if explorer then diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 55885e3f7f4..76da724a605 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -187,7 +187,7 @@ end local function open_file_in_tab(filename) if M.quit_on_open then - view.close() + view.View:close() end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -197,7 +197,7 @@ end local function drop(filename) if M.quit_on_open then - view.close() + view.View:close() end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -207,7 +207,7 @@ end local function tab_drop(filename) if M.quit_on_open then - view.close() + view.View:close() end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -421,7 +421,7 @@ function M.fn(mode, filename) end if M.quit_on_open then - view.close() + view.View:close() end end diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index 10aa978467e..37c823c952f 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -42,7 +42,7 @@ function M.fn(opts, no_focus, cwd, bang) if view.is_visible() then -- close - view.close() + view.View:close() else -- open lib.open({ diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 39fba07d52d..595ff5907ee 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -1,3 +1,5 @@ +---TODO #2826 wrap all the view methods in explorer + local core = require("nvim-tree.core") local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") @@ -245,7 +247,7 @@ local function edit(mode, node, edit_opts) local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then - view.close(cur_tabpage) + view.View:close(cur_tabpage) end local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 62a7dd9ef64..a1f46828eea 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -63,7 +63,7 @@ local function remove_overlay(self) group = vim.api.nvim_create_augroup("NvimTree", { clear = false }), callback = function() if utils.is_nvim_tree_buf(0) then - view.close() + view.View:close() end end, }) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index a464edf39ba..a755ab9ca3c 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -31,7 +31,7 @@ end local function open_view_and_draw() local cwd = vim.fn.getcwd() - view.open() + view.View:open() handle_buf_cwd(cwd) local explorer = core.get_explorer() @@ -110,7 +110,7 @@ function M.open(opts) local explorer = core.get_explorer() if should_hijack_current_buf() then - view.close_this_tab_only() + view.View:close_this_tab_only() view.open_in_win() if explorer then explorer.renderer:draw() diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 72e3f8688d4..5c79524bfb8 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -145,8 +145,9 @@ local function wipe_rogue_buffer() end end +---@private ---@param bufnr integer|boolean|nil -local function create_buffer(bufnr) +function View:create_buffer(bufnr) wipe_rogue_buffer() local tab = vim.api.nvim_get_current_tabpage() @@ -193,20 +194,22 @@ local move_tbl = { } -- setup_tabpage sets up the initial state of a tab +---@private ---@param tabpage integer -local function setup_tabpage(tabpage) +function View:setup_tabpage(tabpage) local winnr = vim.api.nvim_get_current_win() - M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr }) + self.tabpages[tabpage] = vim.tbl_extend("force", self.tabpages[tabpage] or tabinitial, { winnr = winnr }) end -local function set_window_options_and_buffer() +---@private +function View:set_window_options_and_buffer() pcall(vim.api.nvim_command, "buffer " .. M.get_bufnr()) if vim.fn.has("nvim-0.10") == 1 then local eventignore = vim.api.nvim_get_option_value("eventignore", {}) vim.api.nvim_set_option_value("eventignore", "all", {}) - for k, v in pairs(M.View.winopts) do + for k, v in pairs(self.winopts) do vim.api.nvim_set_option_value(k, v, { scope = "local" }) end @@ -217,7 +220,7 @@ local function set_window_options_and_buffer() -- #3009 vim.api.nvim_win_set_option does not set local scope without explicit winid. -- Revert to opt_local instead of propagating it through for just the 0.10 path. - for k, v in pairs(M.View.winopts) do + for k, v in pairs(self.winopts) do vim.opt_local[k] = v end @@ -225,24 +228,26 @@ local function set_window_options_and_buffer() end end +---@private ---@return table -local function open_win_config() - if type(M.View.float.open_win_config) == "function" then - return M.View.float.open_win_config() +function View:open_win_config() + if type(self.float.open_win_config) == "function" then + return self.float.open_win_config() else - return M.View.float.open_win_config + return self.float.open_win_config end end -local function open_window() - if M.View.float.enable then - vim.api.nvim_open_win(0, true, open_win_config()) +---@private +function View:open_window() + if self.float.enable then + vim.api.nvim_open_win(0, true, self:open_win_config()) else vim.api.nvim_command("vsp") M.reposition_window() end - setup_tabpage(vim.api.nvim_get_current_tabpage()) - set_window_options_and_buffer() + self:setup_tabpage(vim.api.nvim_get_current_tabpage()) + self:set_window_options_and_buffer() end ---@param buf integer @@ -276,19 +281,21 @@ local function switch_buf_if_last_buf() end 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 ---@param tabnr integer -local function save_tab_state(tabnr) +function View:save_tab_state(tabnr) local tabpage = tabnr or vim.api.nvim_get_current_tabpage() M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0) end +---@private ---@param tabpage integer -local function close(tabpage) +function View:close_internal(tabpage) if not M.is_visible({ tabpage = tabpage }) then return end - save_tab_state(tabpage) + self:save_tab_state(tabpage) switch_buf_if_last_buf() local tree_win = M.get_winnr(tabpage) local current_win = vim.api.nvim_get_current_win() @@ -310,29 +317,29 @@ local function close(tabpage) end end -function M.close_this_tab_only() - close(vim.api.nvim_get_current_tabpage()) +function View:close_this_tab_only() + self:close_internal(vim.api.nvim_get_current_tabpage()) end -function M.close_all_tabs() - for tabpage, _ in pairs(M.View.tabpages) do - close(tabpage) +function View:close_all_tabs() + for tabpage, _ in pairs(self.tabpages) do + self:close_internal(tabpage) end end ---@param tabpage integer|nil -function M.close(tabpage) +function View:close(tabpage) if M.View.tab.sync.close then - M.close_all_tabs() + self:close_all_tabs() elseif tabpage then - close(tabpage) + self:close_internal(tabpage) else - M.close_this_tab_only() + self:close_this_tab_only() end end ---@param options table|nil -function M.open(options) +function View:open(options) if M.is_visible() then return end @@ -340,8 +347,8 @@ function M.open(options) local profile = log.profile_start("view open") events._dispatch_on_tree_pre_open() - create_buffer() - open_window() + self:create_buffer() + self:open_window() M.resize() local opts = options or { focus_tree = true } @@ -462,10 +469,10 @@ function M.open_in_win(opts) if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then vim.api.nvim_set_current_win(opts.winid) end - create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) - setup_tabpage(vim.api.nvim_get_current_tabpage()) + M.View:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) + M.View:setup_tabpage(vim.api.nvim_get_current_tabpage()) set_current_win() - set_window_options_and_buffer() + M.View:set_window_options_and_buffer() if opts.resize then M.reposition_window() M.resize() @@ -527,10 +534,10 @@ function M.focus(winnr, open_if_closed) if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then M.close() - M.open() + M.View:open() wnr = M.get_winnr() elseif open_if_closed and not M.is_visible() then - M.open() + M.View:open() end if wnr then @@ -605,7 +612,7 @@ function M._prevent_buffer_override() -- might need a better patch vim.cmd("setlocal nowinfixwidth") vim.cmd("setlocal nowinfixheight") - M.open({ focus_tree = false }) + M.View:open({ focus_tree = false }) local explorer = require("nvim-tree.core").get_explorer() if explorer then From ca2ed19be968da231e247a9876511342bdf6f547 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Apr 2025 13:54:37 +1000 Subject: [PATCH 04/32] refactor(#2826): singleton View class, WIP --- lua/nvim-tree.lua | 8 +- lua/nvim-tree/actions/finders/find-file.lua | 6 +- lua/nvim-tree/actions/moves/item.lua | 8 +- lua/nvim-tree/actions/moves/parent.lua | 4 +- lua/nvim-tree/actions/node/open-file.lua | 4 +- lua/nvim-tree/actions/tree/find-file.lua | 2 +- lua/nvim-tree/actions/tree/open.lua | 2 +- lua/nvim-tree/actions/tree/resize.lua | 8 +- lua/nvim-tree/actions/tree/toggle.lua | 2 +- lua/nvim-tree/commands.lua | 2 +- lua/nvim-tree/explorer/init.lua | 2 +- lua/nvim-tree/explorer/live-filter.lua | 2 +- lua/nvim-tree/lib.lua | 6 +- lua/nvim-tree/renderer/init.lua | 2 +- lua/nvim-tree/view.lua | 106 ++++++++++---------- 15 files changed, 84 insertions(+), 80 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 33aa6bcb97e..6a4df3697f6 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -74,7 +74,7 @@ function M.change_root(path, bufnr) end function M.tab_enter() - if view.is_visible({ any_tabpage = true }) then + if view.View:is_visible({ any_tabpage = true }) then local bufname = vim.api.nvim_buf_get_name(0) local ft @@ -99,7 +99,7 @@ function M.tab_enter() end function M.open_on_directory() - local should_proceed = _config.hijack_directories.auto_open or view.is_visible() + local should_proceed = _config.hijack_directories.auto_open or view.View:is_visible() if not should_proceed then return end @@ -160,7 +160,7 @@ local function setup_autocommands(opts) if opts.actions.open_file.eject then view._prevent_buffer_override() else - view.abandon_current_window() + view.View:abandon_current_window() end end, }) @@ -693,7 +693,7 @@ end function M.purge_all_state() view.View:close_all_tabs() - view.abandon_all_windows() + view.View:abandon_all_windows() local explorer = core.get_explorer() if explorer then require("nvim-tree.git").purge_state() diff --git a/lua/nvim-tree/actions/finders/find-file.lua b/lua/nvim-tree/actions/finders/find-file.lua index 55e2f23d337..3ecd7e17c1b 100644 --- a/lua/nvim-tree/actions/finders/find-file.lua +++ b/lua/nvim-tree/actions/finders/find-file.lua @@ -14,7 +14,7 @@ local running = {} ---@param path string relative or absolute function M.fn(path) local explorer = core.get_explorer() - if not explorer or not view.is_visible() then + if not explorer or not view.View:is_visible() then return end @@ -84,9 +84,9 @@ function M.fn(path) end) :iterate() - if found and view.is_visible() then + if found and view.View:is_visible() then explorer.renderer:draw() - view.set_cursor({ line, 0 }) + view.View:set_cursor({ line, 0 }) end running[path_real] = false diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index 8aacaae3b56..2bc849bd03d 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -67,9 +67,9 @@ local function move(explorer, where, what, skip_gitignored) end if nex then - view.set_cursor({ nex, 0 }) + view.View:set_cursor({ nex, 0 }) elseif vim.o.wrapscan and first then - view.set_cursor({ first, 0 }) + view.View:set_cursor({ first, 0 }) end end @@ -189,13 +189,13 @@ local function move_prev_recursive(explorer, what, skip_gitignored) -- 4.3) if node_init.name == ".." then -- root node - view.set_cursor({ 1, 0 }) -- move to root node (position 1) + view.View:set_cursor({ 1, 0 }) -- move to root node (position 1) else local node_init_line = utils.find_node_line(node_init) if node_init_line < 0 then return end - view.set_cursor({ node_init_line, 0 }) + view.View:set_cursor({ node_init_line, 0 }) end -- 4.4) diff --git a/lua/nvim-tree/actions/moves/parent.lua b/lua/nvim-tree/actions/moves/parent.lua index 32ca8398116..2c08521fb66 100644 --- a/lua/nvim-tree/actions/moves/parent.lua +++ b/lua/nvim-tree/actions/moves/parent.lua @@ -25,7 +25,7 @@ function M.fn(should_close) local parent = (node:get_parent_of_group() or node).parent if not parent or not parent.parent then - view.set_cursor({ 1, 0 }) + view.View:set_cursor({ 1, 0 }) return end @@ -33,7 +33,7 @@ function M.fn(should_close) return n.absolute_path == parent.absolute_path end) - view.set_cursor({ line + 1, 0 }) + view.View:set_cursor({ line + 1, 0 }) if should_close then parent.open = false parent.explorer.renderer:draw() diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 76da724a605..eb380408616 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -367,7 +367,7 @@ local function is_already_loaded(filename) end local function edit_in_current_buf(filename) - require("nvim-tree.view").abandon_current_window() + require("nvim-tree.view").View:abandon_current_window() if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) end @@ -413,7 +413,7 @@ function M.fn(mode, filename) end if M.resize_window then - view.resize() + view.View:resize() end if mode == "preview" or mode == "preview_no_picker" then diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua index 8a05bf6db45..345ed6bb70e 100644 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ b/lua/nvim-tree/actions/tree/find-file.lua @@ -41,7 +41,7 @@ function M.fn(opts) return end - if view.is_visible() then + if view.View:is_visible() then -- focus if opts.focus then lib.set_target_win() diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index ff2da837b87..7e7fca79e4f 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -23,7 +23,7 @@ function M.fn(opts) opts.path = nil end - if view.is_visible() then + if view.View:is_visible() then -- focus lib.set_target_win() view.focus() diff --git a/lua/nvim-tree/actions/tree/resize.lua b/lua/nvim-tree/actions/tree/resize.lua index 35b383705ac..60a6bdd0fda 100644 --- a/lua/nvim-tree/actions/tree/resize.lua +++ b/lua/nvim-tree/actions/tree/resize.lua @@ -8,7 +8,7 @@ function M.fn(opts) if opts == nil then -- reset to config values view.View:configure_width() - view.resize() + view.View:resize() return end @@ -17,7 +17,7 @@ function M.fn(opts) if width_cfg ~= nil then view.View:configure_width(width_cfg) - view.resize() + view.View:resize() return end @@ -28,7 +28,7 @@ function M.fn(opts) local absolute = options.absolute if type(absolute) == "number" then - view.resize(absolute) + view.View:resize(absolute) return end @@ -39,7 +39,7 @@ function M.fn(opts) relative_size = "+" .. relative_size end - view.resize(relative_size) + view.View:resize(relative_size) return end end diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index 37c823c952f..34afa4bf56d 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -40,7 +40,7 @@ function M.fn(opts, no_focus, cwd, bang) opts.path = nil end - if view.is_visible() then + if view.View:is_visible() then -- close view.View:close() else diff --git a/lua/nvim-tree/commands.lua b/lua/nvim-tree/commands.lua index bff880c14fd..464728f6d23 100644 --- a/lua/nvim-tree/commands.lua +++ b/lua/nvim-tree/commands.lua @@ -111,7 +111,7 @@ local CMDS = { bar = true, }, command = function(c) - view.resize(c.args) + view.View:resize(c.args) end, }, { diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 58972164afe..b8a22b28d48 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -486,7 +486,7 @@ function Explorer:reload_explorer() local projects = git.reload_all_projects() self:refresh_nodes(projects) - if view.is_visible() then + if view.View:is_visible() then self.renderer:draw() end event_running = false diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index a1f46828eea..a363d99c355 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -204,7 +204,7 @@ function LiveFilter:start_filtering() self.explorer.renderer:draw() local row = require("nvim-tree.core").get_nodes_starting_line() - 1 local col = #self.prefix > 0 and #self.prefix - 1 or 1 - view.set_cursor({ row, col }) + view.View:set_cursor({ row, col }) -- needs scheduling to let the cursor move before initializing the window vim.schedule(function() return create_overlay(self) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index a755ab9ca3c..a4645055d6b 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -111,17 +111,17 @@ function M.open(opts) if should_hijack_current_buf() then view.View:close_this_tab_only() - view.open_in_win() + view.View:open_in_win() if explorer then explorer.renderer:draw() end elseif opts.winid then - view.open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid }) + view.View:open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid }) if explorer then explorer.renderer:draw() end elseif opts.current_window then - view.open_in_win({ hijack_current_buf = false, resize = false }) + view.View:open_in_win({ hijack_current_buf = false, resize = false }) if explorer then explorer.renderer:draw() end diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 30af27e81b9..c0d38e1bd12 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -113,7 +113,7 @@ function Renderer:draw() vim.api.nvim_win_set_cursor(view.get_winnr() or 0, cursor) end - view.grow_from_content() + view.View:grow_from_content() log.profile_end(profile) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 5c79524bfb8..192482d0ff7 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -114,6 +114,7 @@ local tabinitial = { winnr = nil, } +-- TODO member local BUFNR_PER_TAB = {} ---@type { name: string, value: any }[] @@ -244,7 +245,7 @@ function View:open_window() vim.api.nvim_open_win(0, true, self:open_win_config()) else vim.api.nvim_command("vsp") - M.reposition_window() + self:reposition_window() end self:setup_tabpage(vim.api.nvim_get_current_tabpage()) self:set_window_options_and_buffer() @@ -286,13 +287,13 @@ end ---@param tabnr integer function View:save_tab_state(tabnr) local tabpage = tabnr or vim.api.nvim_get_current_tabpage() - M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0) + self.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0) end ---@private ---@param tabpage integer function View:close_internal(tabpage) - if not M.is_visible({ tabpage = tabpage }) then + if not self:is_visible({ tabpage = tabpage }) then return end self:save_tab_state(tabpage) @@ -329,7 +330,7 @@ end ---@param tabpage integer|nil function View:close(tabpage) - if M.View.tab.sync.close then + if self.tab.sync.close then self:close_all_tabs() elseif tabpage then self:close_internal(tabpage) @@ -340,7 +341,7 @@ end ---@param options table|nil function View:open(options) - if M.is_visible() then + if self:is_visible() then return end @@ -349,7 +350,7 @@ function View:open(options) events._dispatch_on_tree_pre_open() self:create_buffer() self:open_window() - M.resize() + self:resize() local opts = options or { focus_tree = true } if not opts.focus_tree then @@ -360,11 +361,12 @@ function View:open(options) log.profile_end(profile) end -local function grow() +---@private +function View:grow() local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) -- number of columns of right-padding to indicate end of path - local padding = M.View:get_size(M.View.padding) + local padding = self:get_size(self.padding) -- account for sign/number columns etc. local wininfo = vim.fn.getwininfo(M.get_winnr()) @@ -372,14 +374,14 @@ local function grow() padding = padding + wininfo[1].textoff end - local resizing_width = M.View.initial_width - padding + local resizing_width = self.initial_width - padding local max_width -- maybe bound max - if M.View.max_width == -1 then + if self.max_width == -1 then max_width = -1 else - max_width = M.View:get_width(M.View.max_width) - padding + max_width = self:get_width(self.max_width) - padding end local ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"] @@ -391,23 +393,23 @@ local function grow() if resizing_width < count then resizing_width = count end - if M.View.adaptive_size and max_width >= 0 and resizing_width >= max_width then + if self.adaptive_size and max_width >= 0 and resizing_width >= max_width then resizing_width = max_width break end end - M.resize(resizing_width + padding) + self:resize(resizing_width + padding) end -function M.grow_from_content() - if M.View.adaptive_size then - grow() +function View:grow_from_content() + if self.adaptive_size then + self:grow() end end ---@param size string|number|nil -function M.resize(size) - if M.View.float.enable and not M.View.adaptive_size then +function View:resize(size) + if self.float.enable and not self.adaptive_size then -- if the floating windows's adaptive size is not desired, then the -- float size should be defined in view.float.open_win_config return @@ -419,7 +421,7 @@ function M.resize(size) size = tonumber(size) if first_char == "+" or first_char == "-" then - size = M.View.width + size + size = self.width + size end end @@ -428,21 +430,21 @@ function M.resize(size) end if size then - M.View.width = size - M.View.height = size + self.width = size + self.height = size end - if not M.is_visible() then + if not self:is_visible() then return end local winnr = M.get_winnr() or 0 - local new_size = M.View:get_width() + local new_size = self:get_width() if new_size ~= vim.api.nvim_win_get_width(winnr) then vim.api.nvim_win_set_width(winnr, new_size) - if not M.View.preserve_window_proportions then + if not self.preserve_window_proportions then vim.cmd(":wincmd =") end end @@ -450,66 +452,68 @@ function M.resize(size) events._dispatch_on_tree_resize(new_size) end -function M.reposition_window() - local move_to = move_tbl[M.View.side] +---@private +function View:reposition_window() + local move_to = move_tbl[self.side] vim.api.nvim_command("wincmd " .. move_to) - M.resize() + self:resize() end -local function set_current_win() +---@private +function View:set_current_win() local current_tab = vim.api.nvim_get_current_tabpage() - M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() + self.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() end ---Open the tree in the a window ---@param opts OpenInWinOpts|nil -function M.open_in_win(opts) +function View:open_in_win(opts) opts = opts or { hijack_current_buf = true, resize = true } events._dispatch_on_tree_pre_open() if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then vim.api.nvim_set_current_win(opts.winid) end - M.View:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) - M.View:setup_tabpage(vim.api.nvim_get_current_tabpage()) - set_current_win() - M.View:set_window_options_and_buffer() + self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) + self:setup_tabpage(vim.api.nvim_get_current_tabpage()) + self:set_current_win() + self:set_window_options_and_buffer() if opts.resize then - M.reposition_window() - M.resize() + self:reposition_window() + self:resize() end events._dispatch_on_tree_open() end -function M.abandon_current_window() +function View:abandon_current_window() local tab = vim.api.nvim_get_current_tabpage() BUFNR_PER_TAB[tab] = nil - if M.View.tabpages[tab] then - M.View.tabpages[tab].winnr = nil + if self.tabpages[tab] then + self.tabpages[tab].winnr = nil end end -function M.abandon_all_windows() +function View:abandon_all_windows() for tab, _ in pairs(vim.api.nvim_list_tabpages()) do BUFNR_PER_TAB[tab] = nil - if M.View.tabpages[tab] then - M.View.tabpages[tab].winnr = nil + if self.tabpages[tab] then + self.tabpages[tab].winnr = nil end end end ---@param opts table|nil ---@return boolean -function M.is_visible(opts) +function View:is_visible(opts) if opts and opts.tabpage then - if M.View.tabpages[opts.tabpage] == nil then + if self.tabpages[opts.tabpage] == nil then return false end - local winnr = M.View.tabpages[opts.tabpage].winnr + local winnr = self.tabpages[opts.tabpage].winnr return winnr and vim.api.nvim_win_is_valid(winnr) end if opts and opts.any_tabpage then - for _, v in pairs(M.View.tabpages) do + for _, v in pairs(self.tabpages) do if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then return true end @@ -521,8 +525,8 @@ function M.is_visible(opts) end ---@param opts table|nil -function M.set_cursor(opts) - if M.is_visible() then +function View:set_cursor(opts) + if self:is_visible() then pcall(vim.api.nvim_win_set_cursor, M.get_winnr(), opts) end end @@ -536,7 +540,7 @@ function M.focus(winnr, open_if_closed) M.close() M.View:open() wnr = M.get_winnr() - elseif open_if_closed and not M.is_visible() then + elseif open_if_closed and not M.View:is_visible() then M.View:open() end @@ -553,7 +557,7 @@ function M.winid(opts) if tabpage == 0 then tabpage = vim.api.nvim_get_current_tabpage() end - if M.is_visible({ tabpage = tabpage }) then + if M.View:is_visible({ tabpage = tabpage }) then return M.get_winnr(tabpage) else return nil @@ -563,7 +567,7 @@ end --- Restores the state of a NvimTree window if it was initialized before. function M.restore_tab_state() local tabpage = vim.api.nvim_get_current_tabpage() - M.set_cursor(M.View.cursors[tabpage]) + M.View:set_cursor(M.View.cursors[tabpage]) end --- Returns the window number for nvim-tree within the tabpage specified From 53643f505a284d0f8833bdbc9dd9b46c341e8700 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Apr 2025 13:57:12 +1000 Subject: [PATCH 05/32] refactor(#2826): singleton View class, WIP --- lua/nvim-tree/api.lua | 2 +- lua/nvim-tree/view.lua | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 595ff5907ee..a04fe44157f 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -1,4 +1,4 @@ ----TODO #2826 wrap all the view methods in explorer +-- TODO #2826 wrap all the view methods in explorer local core = require("nvim-tree.core") local view = require("nvim-tree.view") diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 192482d0ff7..e5906a0ddd5 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -16,13 +16,13 @@ local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MAX_WIDTH = -1 local DEFAULT_PADDING = 1 ---TODO attempt to type the tables, at least the options ones +-- TODO #2826 attempt to type the tables, at least the options ones ---@class (exact) View: Class ---@field live_filter table ---@field side string ---@field float table ----TODO private below here +---TODO #2826 private below here ---@field explorer Explorer ---@field adaptive_size boolean ---@field centralize_selection boolean @@ -42,6 +42,7 @@ local View = Class:extend() ---@class View ---@overload fun(args: ViewArgs): View +--TODO #2826 exact ---@class (exact) ViewArgs ---@field explorer Explorer @@ -114,7 +115,7 @@ local tabinitial = { winnr = nil, } --- TODO member +-- TODO #2826 member local BUFNR_PER_TAB = {} ---@type { name: string, value: any }[] @@ -678,11 +679,8 @@ function View:configure_width(width) end function M.setup(opts) - M.View = View({ - explorer = { - opts = opts - } - }) + -- TODO #2826 move this to explorer constructor + M.View = View({ explorer = { opts = opts } }) ---@diagnostic disable-line: missing-fields end return M From 048e6371342c1b13c523936eae7aeed7d0d6cd71 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Apr 2025 14:07:02 +1000 Subject: [PATCH 06/32] refactor(#2826): singleton View class, WIP --- lua/nvim-tree/actions/node/open-file.lua | 2 +- lua/nvim-tree/actions/tree/find-file.lua | 2 +- lua/nvim-tree/actions/tree/open.lua | 2 +- lua/nvim-tree/api.lua | 2 +- lua/nvim-tree/core.lua | 2 +- lua/nvim-tree/explorer/init.lua | 2 +- lua/nvim-tree/lib.lua | 2 +- lua/nvim-tree/renderer/builder.lua | 2 +- lua/nvim-tree/utils.lua | 6 +++--- lua/nvim-tree/view.lua | 24 ++++++++++++------------ 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index eb380408616..1135c148f18 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -228,7 +228,7 @@ local function on_preview(buf_loaded) once = true, }) end - view.focus() + view.View:focus() end local function get_target_winid(mode) diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua index 345ed6bb70e..19b6a9e576f 100644 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ b/lua/nvim-tree/actions/tree/find-file.lua @@ -45,7 +45,7 @@ function M.fn(opts) -- focus if opts.focus then lib.set_target_win() - view.focus() + view.View:focus() end elseif opts.open then -- open diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index 7e7fca79e4f..d03db93fea9 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -26,7 +26,7 @@ function M.fn(opts) if view.View:is_visible() then -- focus lib.set_target_win() - view.focus() + view.View:focus() else -- open lib.open({ diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index a04fe44157f..0fe8d238d90 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -257,7 +257,7 @@ local function edit(mode, node, edit_opts) if mode == "tabnew" then vim.cmd(":tabprev") end - view.focus() + view.View:focus() end end diff --git a/lua/nvim-tree/core.lua b/lua/nvim-tree/core.lua index 60d4a0a6f6a..943d3c80336 100644 --- a/lua/nvim-tree/core.lua +++ b/lua/nvim-tree/core.lua @@ -55,7 +55,7 @@ end ---@return integer function M.get_nodes_starting_line() local offset = 1 - if view.is_root_folder_visible(M.get_cwd()) then + if view.View:is_root_folder_visible(M.get_cwd()) then offset = offset + 1 end if TreeExplorer and TreeExplorer.live_filter.filter then diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index b8a22b28d48..52c760c9ed4 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -523,7 +523,7 @@ function Explorer:get_node_at_cursor() return end - if cursor[1] == 1 and view.is_root_folder_visible(core.get_cwd()) then + if cursor[1] == 1 and view.View:is_root_folder_visible(core.get_cwd()) then return self end diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index a4645055d6b..2a877915f4c 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -128,7 +128,7 @@ function M.open(opts) else open_view_and_draw() end - view.restore_tab_state() + view.View:restore_tab_state() end function M.setup(opts) diff --git a/lua/nvim-tree/renderer/builder.lua b/lua/nvim-tree/renderer/builder.lua index 819660e5306..531f1a0a909 100644 --- a/lua/nvim-tree/renderer/builder.lua +++ b/lua/nvim-tree/renderer/builder.lua @@ -379,7 +379,7 @@ end ---@private function Builder:build_header() - if view.is_root_folder_visible(self.explorer.absolute_path) then + if view.View:is_root_folder_visible(self.explorer.absolute_path) then local root_name = self:format_root_name(self.explorer.opts.renderer.root_folder_label) table.insert(self.lines, root_name) self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name)) diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 9396ba9d52f..01da141382e 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -143,7 +143,7 @@ function M.find_node(nodes, fn) return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes) end) :iterate() - i = require("nvim-tree.view").is_root_folder_visible() and i or i - 1 + i = require("nvim-tree.view").View:is_root_folder_visible() and i or i - 1 if node and node.explorer.live_filter.filter then i = i + 1 end @@ -537,7 +537,7 @@ function M.focus_file(path) local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node) return node.absolute_path == path end) - require("nvim-tree.view").set_cursor({ i + 1, 1 }) + require("nvim-tree.view").View:set_cursor({ i + 1, 1 }) end ---Focus node passed as parameter if visible, otherwise focus first visible parent. @@ -557,7 +557,7 @@ function M.focus_node_or_parent(node) end) if found_node or node.parent == nil then - require("nvim-tree.view").set_cursor({ i + 1, 1 }) + require("nvim-tree.view").View:set_cursor({ i + 1, 1 }) break end diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index e5906a0ddd5..0b1c3f4ab1a 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -364,7 +364,7 @@ end ---@private function View:grow() - local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 + local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) -- number of columns of right-padding to indicate end of path local padding = self:get_size(self.padding) @@ -534,15 +534,15 @@ end ---@param winnr number|nil ---@param open_if_closed boolean|nil -function M.focus(winnr, open_if_closed) +function View:focus(winnr, open_if_closed) local wnr = winnr or M.get_winnr() if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then - M.close() - M.View:open() + self:close() + self:open() wnr = M.get_winnr() - elseif open_if_closed and not M.View:is_visible() then - M.View:open() + elseif open_if_closed and not self:is_visible() then + self:open() end if wnr then @@ -553,12 +553,12 @@ end --- Retrieve the winid of the open tree. ---@param opts ApiTreeWinIdOpts|nil ---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible -function M.winid(opts) +function View:winid(opts) local tabpage = opts and opts.tabpage if tabpage == 0 then tabpage = vim.api.nvim_get_current_tabpage() end - if M.View:is_visible({ tabpage = tabpage }) then + if self:is_visible({ tabpage = tabpage }) then return M.get_winnr(tabpage) else return nil @@ -566,9 +566,9 @@ function M.winid(opts) end --- Restores the state of a NvimTree window if it was initialized before. -function M.restore_tab_state() +function View:restore_tab_state() local tabpage = vim.api.nvim_get_current_tabpage() - M.View:set_cursor(M.View.cursors[tabpage]) + self:set_cursor(self.cursors[tabpage]) end --- Returns the window number for nvim-tree within the tabpage specified @@ -638,8 +638,8 @@ end ---@param cwd string|nil ---@return boolean -function M.is_root_folder_visible(cwd) - return cwd ~= "/" and not M.View.hide_root_folder +function View:is_root_folder_visible(cwd) + return cwd ~= "/" and not self.hide_root_folder end -- used on ColorScheme event From 00d7a308d6f174890f4bfc16734196512b71dc0a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Apr 2025 14:28:44 +1000 Subject: [PATCH 07/32] refactor(#2826): singleton View class, WIP --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/actions/node/open-file.lua | 2 +- lua/nvim-tree/actions/tree/resize.lua | 2 +- lua/nvim-tree/diagnostics.lua | 2 +- lua/nvim-tree/explorer/init.lua | 4 +- lua/nvim-tree/explorer/live-filter.lua | 2 +- lua/nvim-tree/lib.lua | 2 +- lua/nvim-tree/renderer/init.lua | 8 +- lua/nvim-tree/view.lua | 117 +++++++++++------------ 9 files changed, 70 insertions(+), 71 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 6a4df3697f6..ec3308ea24c 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -158,7 +158,7 @@ local function setup_autocommands(opts) return end if opts.actions.open_file.eject then - view._prevent_buffer_override() + view.View:prevent_buffer_override() else view.View:abandon_current_window() end diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 1135c148f18..c6de718a398 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -21,7 +21,7 @@ end local function usable_win_ids() local tabpage = vim.api.nvim_get_current_tabpage() local win_ids = vim.api.nvim_tabpage_list_wins(tabpage) - local tree_winid = view.get_winnr(tabpage) + local tree_winid = view.View:get_winnr(tabpage) return vim.tbl_filter(function(id) local bufid = vim.api.nvim_win_get_buf(id) diff --git a/lua/nvim-tree/actions/tree/resize.lua b/lua/nvim-tree/actions/tree/resize.lua index 60a6bdd0fda..8976fba8de5 100644 --- a/lua/nvim-tree/actions/tree/resize.lua +++ b/lua/nvim-tree/actions/tree/resize.lua @@ -21,7 +21,7 @@ function M.fn(opts) return end - if not view.is_width_determined() then + if not view.View:is_width_determined() then -- {absolute} and {relative} do nothing when {width} is a function. return end diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index 76810d4a796..2ded2fb535e 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -182,7 +182,7 @@ function M.update_coc() end log.profile_end(profile) - local bufnr = view.get_bufnr() + local bufnr = view.View:get_bufnr() local should_draw = bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr) if should_draw then local explorer = core.get_explorer() diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 52c760c9ed4..d138df10c9b 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -84,7 +84,7 @@ function Explorer:create_autocmds() group = self.augroup_id, callback = function() appearance.setup() - view.reset_winhl() + view.View:reset_winhl() self.renderer:draw() end, }) @@ -508,7 +508,7 @@ end ---nil on no explorer or invalid view win ---@return integer[]|nil function Explorer:get_cursor_position() - local winnr = view.get_winnr() + local winnr = view.View:get_winnr() if not winnr or not vim.api.nvim_win_is_valid(winnr) then return end diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index a363d99c355..22df536efc9 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -156,7 +156,7 @@ end ---@return integer local function calculate_overlay_win_width(self) - local wininfo = vim.fn.getwininfo(view.get_winnr())[1] + local wininfo = vim.fn.getwininfo(view.View:get_winnr())[1] if wininfo then return wininfo.width - wininfo.textoff - #self.prefix diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 2a877915f4c..bd203cf3335 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -13,7 +13,7 @@ local M = { function M.set_target_win() local id = vim.api.nvim_get_current_win() - local tree_id = view.get_winnr() + local tree_id = view.View:get_winnr() if tree_id and id == tree_id then M.target_winid = 0 return diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index c0d38e1bd12..8da0b28424d 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -96,28 +96,28 @@ function Renderer:render_hl(bufnr, hl_range_args) end function Renderer:draw() - local bufnr = view.get_bufnr() + local bufnr = view.View:get_bufnr() if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then return end local profile = log.profile_start("draw") - local cursor = vim.api.nvim_win_get_cursor(view.get_winnr() or 0) + local cursor = vim.api.nvim_win_get_cursor(view.View:get_winnr() or 0) local builder = Builder(self.explorer):build() self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines) if cursor and #builder.lines >= cursor[1] then - vim.api.nvim_win_set_cursor(view.get_winnr() or 0, cursor) + vim.api.nvim_win_set_cursor(view.View:get_winnr() or 0, cursor) end view.View:grow_from_content() log.profile_end(profile) - events._dispatch_on_tree_rendered(bufnr, view.get_winnr()) + events._dispatch_on_tree_rendered(bufnr, view.View:get_winnr()) end return Renderer diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 0b1c3f4ab1a..de71925ba33 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -22,27 +22,26 @@ local DEFAULT_PADDING = 1 ---@field live_filter table ---@field side string ---@field float table ----TODO #2826 private below here ----@field explorer Explorer ----@field adaptive_size boolean ----@field centralize_selection boolean ----@field tabpages table ----@field cursors table ----@field hide_root_folder boolean ----@field winopts table ----@field height integer ----@field tab table ----@field preserve_window_proportions boolean ----@field initial_width integer ----@field width (fun():integer)|integer|string ----@field max_width integer ----@field padding integer +---@field private explorer Explorer +---@field private adaptive_size boolean +---@field private centralize_selection boolean +---@field private tabpages table +---@field private cursors table +---@field private hide_root_folder boolean +---@field private winopts table +---@field private height integer +---@field private tab table +---@field private preserve_window_proportions boolean +---@field private initial_width integer +---@field private width (fun():integer)|integer|string +---@field private max_width integer +---@field private padding integer +---@field private bufnr_per_tab table local View = Class:extend() ---@class View ---@overload fun(args: ViewArgs): View ---TODO #2826 exact ---@class (exact) ViewArgs ---@field explorer Explorer @@ -55,6 +54,7 @@ function View:new(args) self.tabpages = {} self.cursors = {} self.hide_root_folder = false + self.bufnr_per_tab = {} self.live_filter = { prev_focused_node = nil, } @@ -115,9 +115,6 @@ local tabinitial = { winnr = nil, } --- TODO #2826 member -local BUFNR_PER_TAB = {} - ---@type { name: string, value: any }[] local BUFFER_OPTIONS = { { name = "bufhidden", value = "wipe" }, @@ -128,10 +125,11 @@ local BUFFER_OPTIONS = { { name = "swapfile", value = false }, } +---@private ---@param bufnr integer ---@return boolean -local function matches_bufnr(bufnr) - for _, b in pairs(BUFNR_PER_TAB) do +function View:matches_bufnr(bufnr) + for _, b in pairs(self.bufnr_per_tab) do if b == bufnr then return true end @@ -139,31 +137,32 @@ local function matches_bufnr(bufnr) return false end -local function wipe_rogue_buffer() +---@private +function View:wipe_rogue_buffer() for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do - if not matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then + if not self:matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then pcall(vim.api.nvim_buf_delete, bufnr, { force = true }) end end end ---@private ----@param bufnr integer|boolean|nil +---@param bufnr integer|false|nil function View:create_buffer(bufnr) - wipe_rogue_buffer() + self:wipe_rogue_buffer() local tab = vim.api.nvim_get_current_tabpage() - BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) - vim.api.nvim_buf_set_name(M.get_bufnr(), "NvimTree_" .. tab) + self.bufnr_per_tab[tab] = bufnr or vim.api.nvim_create_buf(false, false) + vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab) - bufnr = M.get_bufnr() + bufnr = self:get_bufnr() for _, option in ipairs(BUFFER_OPTIONS) do vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr }) end - require("nvim-tree.keymap").on_attach(M.get_bufnr()) + require("nvim-tree.keymap").on_attach(self:get_bufnr()) - events._dispatch_tree_attached_post(M.get_bufnr()) + events._dispatch_tree_attached_post(self:get_bufnr()) end ---@private @@ -205,7 +204,7 @@ end ---@private function View:set_window_options_and_buffer() - pcall(vim.api.nvim_command, "buffer " .. M.get_bufnr()) + pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr()) if vim.fn.has("nvim-0.10") == 1 then local eventignore = vim.api.nvim_get_option_value("eventignore", {}) @@ -288,7 +287,7 @@ end ---@param tabnr integer function View:save_tab_state(tabnr) local tabpage = tabnr or vim.api.nvim_get_current_tabpage() - self.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0) + self.cursors[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0) end ---@private @@ -299,7 +298,7 @@ function View:close_internal(tabpage) end self:save_tab_state(tabpage) switch_buf_if_last_buf() - local tree_win = M.get_winnr(tabpage) + local tree_win = self:get_winnr(tabpage) local current_win = vim.api.nvim_get_current_win() for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do if vim.api.nvim_win_get_config(win).relative == "" then @@ -365,12 +364,12 @@ end ---@private function View:grow() local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 - local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) + local lines = vim.api.nvim_buf_get_lines(self:get_bufnr(), starts_at, -1, false) -- number of columns of right-padding to indicate end of path local padding = self:get_size(self.padding) -- account for sign/number columns etc. - local wininfo = vim.fn.getwininfo(M.get_winnr()) + local wininfo = vim.fn.getwininfo(self:get_winnr()) if type(wininfo) == "table" and type(wininfo[1]) == "table" then padding = padding + wininfo[1].textoff end @@ -389,7 +388,7 @@ function View:grow() for line_nr, l in pairs(lines) do local count = vim.fn.strchars(l) -- also add space for right-aligned icons - local extmarks = vim.api.nvim_buf_get_extmarks(M.get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true }) + local extmarks = vim.api.nvim_buf_get_extmarks(self:get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true }) count = count + utils.extmarks_length(extmarks) if resizing_width < count then resizing_width = count @@ -439,7 +438,7 @@ function View:resize(size) return end - local winnr = M.get_winnr() or 0 + local winnr = self:get_winnr() or 0 local new_size = self:get_width() @@ -487,7 +486,7 @@ end function View:abandon_current_window() local tab = vim.api.nvim_get_current_tabpage() - BUFNR_PER_TAB[tab] = nil + self.bufnr_per_tab[tab] = nil if self.tabpages[tab] then self.tabpages[tab].winnr = nil end @@ -495,7 +494,7 @@ end function View:abandon_all_windows() for tab, _ in pairs(vim.api.nvim_list_tabpages()) do - BUFNR_PER_TAB[tab] = nil + self.bufnr_per_tab[tab] = nil if self.tabpages[tab] then self.tabpages[tab].winnr = nil end @@ -522,25 +521,25 @@ function View:is_visible(opts) return false end - return M.get_winnr() ~= nil and vim.api.nvim_win_is_valid(M.get_winnr() or 0) + return self:get_winnr() ~= nil and vim.api.nvim_win_is_valid(self:get_winnr() or 0) end ---@param opts table|nil function View:set_cursor(opts) if self:is_visible() then - pcall(vim.api.nvim_win_set_cursor, M.get_winnr(), opts) + pcall(vim.api.nvim_win_set_cursor, self:get_winnr(), opts) end end ---@param winnr number|nil ---@param open_if_closed boolean|nil function View:focus(winnr, open_if_closed) - local wnr = winnr or M.get_winnr() + local wnr = winnr or self:get_winnr() if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then self:close() self:open() - wnr = M.get_winnr() + wnr = self:get_winnr() elseif open_if_closed and not self:is_visible() then self:open() end @@ -559,7 +558,7 @@ function View:winid(opts) tabpage = vim.api.nvim_get_current_tabpage() end if self:is_visible({ tabpage = tabpage }) then - return M.get_winnr(tabpage) + return self:get_winnr(tabpage) else return nil end @@ -574,9 +573,9 @@ end --- 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. ---@return number|nil -function M.get_winnr(tabpage) +function View:get_winnr(tabpage) tabpage = tabpage or vim.api.nvim_get_current_tabpage() - local tabinfo = M.View.tabpages[tabpage] + local tabinfo = self.tabpages[tabpage] if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then return tabinfo.winnr end @@ -584,13 +583,13 @@ end --- Returns the current nvim tree bufnr ---@return number -function M.get_bufnr() - return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()] +function View:get_bufnr() + return self.bufnr_per_tab[vim.api.nvim_get_current_tabpage()] end -function M._prevent_buffer_override() - local view_winnr = M.get_winnr() - local view_bufnr = M.get_bufnr() +function View:prevent_buffer_override() + local view_winnr = self:get_winnr() + local view_bufnr = self:get_bufnr() -- need to schedule to let the new buffer populate the window -- because this event needs to be run on bufWipeout. @@ -602,9 +601,9 @@ function M._prevent_buffer_override() local bufname = vim.api.nvim_buf_get_name(curbuf) if not bufname:match("NvimTree") then - for i, tabpage in ipairs(M.View.tabpages) do + for i, tabpage in ipairs(self.tabpages) do if tabpage.winnr == view_winnr then - M.View.tabpages[i] = nil + self.tabpages[i] = nil break end end @@ -617,7 +616,7 @@ function M._prevent_buffer_override() -- might need a better patch vim.cmd("setlocal nowinfixwidth") vim.cmd("setlocal nowinfixheight") - M.View:open({ focus_tree = false }) + self:open({ focus_tree = false }) local explorer = require("nvim-tree.core").get_explorer() if explorer then @@ -643,17 +642,17 @@ function View:is_root_folder_visible(cwd) end -- used on ColorScheme event -function M.reset_winhl() - local winnr = M.get_winnr() +function View:reset_winhl() + local winnr = self:get_winnr() if winnr and vim.api.nvim_win_is_valid(winnr) then - vim.wo[M.get_winnr()].winhl = M.View.winopts.winhl + vim.wo[self:get_winnr()].winhl = self.winopts.winhl end end ---Check if width determined or calculated on-fly ---@return boolean -function M.is_width_determined() - return type(M.View.width) ~= "function" +function View:is_width_determined() + return type(self.width) ~= "function" end ---Configure width-related config From 937784f28d657ef410b2567eddd233152c3a7bb8 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Apr 2025 14:47:43 +1000 Subject: [PATCH 08/32] refactor(#2826): singleton View class --- lua/nvim-tree/view.lua | 45 +++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index de71925ba33..0bd06f77652 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -16,8 +16,6 @@ local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MAX_WIDTH = -1 local DEFAULT_PADDING = 1 --- TODO #2826 attempt to type the tables, at least the options ones - ---@class (exact) View: Class ---@field live_filter table ---@field side string @@ -26,11 +24,10 @@ local DEFAULT_PADDING = 1 ---@field private adaptive_size boolean ---@field private centralize_selection boolean ---@field private tabpages table ----@field private cursors table +---@field private cursors table as per vim.api.nvim_win_get_cursor ---@field private hide_root_folder boolean ---@field private winopts table ---@field private height integer ----@field private tab table ---@field private preserve_window_proportions boolean ---@field private initial_width integer ---@field private width (fun():integer)|integer|string @@ -50,27 +47,30 @@ local View = Class:extend() function View:new(args) self.explorer = args.explorer self.adaptive_size = false - self.centralize_selection = false - self.tabpages = {} - self.cursors = {} - self.hide_root_folder = false self.bufnr_per_tab = {} - self.live_filter = { - prev_focused_node = nil, - } + self.centralize_selection = self.explorer.opts.view.centralize_selection + self.cursors = {} + self.float = self.explorer.opts.view.float + self.height = self.explorer.opts.view.height + self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false + self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions + self.side = (self.explorer.opts.view.side == "right") and "right" or "left" + self.tabpages = {} + self.live_filter = { prev_focused_node = nil, } + self.winopts = { - relativenumber = false, - number = false, + relativenumber = self.explorer.opts.view.relativenumber, + number = self.explorer.opts.view.number, list = false, foldenable = false, winfixwidth = true, winfixheight = true, spell = false, - signcolumn = "yes", + signcolumn = self.explorer.opts.view.signcolumn, foldmethod = "manual", foldcolumn = "0", cursorcolumn = false, - cursorline = true, + cursorline = self.explorer.opts.view.cursorline, cursorlineopt = "both", colorcolumn = "0", wrap = false, @@ -90,20 +90,7 @@ function View:new(args) }, ","), } - self.centralize_selection = self.explorer.opts.view.centralize_selection - self.side = (self.explorer.opts.view.side == "right") and "right" or "left" - self.height = self.explorer.opts.view.height - self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false - self.tab = self.explorer.opts.tab - self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions - self.winopts.cursorline = self.explorer.opts.view.cursorline - self.winopts.number = self.explorer.opts.view.number - self.winopts.relativenumber = self.explorer.opts.view.relativenumber - self.winopts.signcolumn = self.explorer.opts.view.signcolumn - self.float = self.explorer.opts.view.float - self:configure_width(self.explorer.opts.view.width) - self.initial_width = self:get_width() end @@ -330,7 +317,7 @@ end ---@param tabpage integer|nil function View:close(tabpage) - if self.tab.sync.close then + if self.explorer.opts.tab.sync.close then self:close_all_tabs() elseif tabpage then self:close_internal(tabpage) From 77d2aa891bcece68e017f53ca8d28a2fdf3a3655 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Apr 2025 12:02:19 +1000 Subject: [PATCH 09/32] refactor(#2826): View is an Explorer member --- lua/nvim-tree.lua | 38 ++++++++------- lua/nvim-tree/actions/finders/find-file.lua | 7 ++- lua/nvim-tree/actions/fs/remove-file.lua | 10 ++-- lua/nvim-tree/actions/moves/item.lua | 9 ++-- lua/nvim-tree/actions/moves/parent.lua | 5 +- lua/nvim-tree/actions/node/open-file.lua | 52 +++++++++++++++------ lua/nvim-tree/actions/tree/find-file.lua | 6 +-- lua/nvim-tree/actions/tree/open.lua | 8 ++-- lua/nvim-tree/actions/tree/resize.lua | 21 +++++---- lua/nvim-tree/actions/tree/toggle.lua | 8 ++-- lua/nvim-tree/commands.lua | 7 ++- lua/nvim-tree/core.lua | 3 +- lua/nvim-tree/diagnostics.lua | 10 ++-- lua/nvim-tree/explorer/init.lua | 12 +++-- lua/nvim-tree/explorer/live-filter.lua | 15 +++--- lua/nvim-tree/iterators/node-iterator.lua | 2 +- lua/nvim-tree/lib.lua | 29 ++++++++---- lua/nvim-tree/renderer/builder.lua | 3 +- lua/nvim-tree/renderer/init.lua | 11 ++--- lua/nvim-tree/utils.lua | 19 ++++++-- lua/nvim-tree/view.lua | 9 +--- 21 files changed, 170 insertions(+), 114 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index ec3308ea24c..e03a4a1d0c1 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -1,5 +1,4 @@ local log = require("nvim-tree.log") -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") local actions = require("nvim-tree.actions") local core = require("nvim-tree.core") @@ -74,7 +73,8 @@ function M.change_root(path, bufnr) end function M.tab_enter() - if view.View:is_visible({ any_tabpage = true }) then + local explorer = core.get_explorer() + if explorer and explorer.view:is_visible({ any_tabpage = true }) then local bufname = vim.api.nvim_buf_get_name(0) local ft @@ -89,17 +89,15 @@ function M.tab_enter() return end end - view.View:open({ focus_tree = false }) + explorer.view:open({ focus_tree = false }) - local explorer = core.get_explorer() - if explorer then - explorer.renderer:draw() - end + explorer.renderer:draw() end end function M.open_on_directory() - local should_proceed = _config.hijack_directories.auto_open or view.View:is_visible() + local explorer = core.get_explorer() + local should_proceed = _config.hijack_directories.auto_open or explorer and explorer.view:is_visible() if not should_proceed then return end @@ -150,6 +148,7 @@ local function setup_autocommands(opts) vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) end + -- TODO #2826 move this to explorer -- prevent new opened file from opening in the same window as nvim-tree create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", @@ -157,10 +156,14 @@ local function setup_autocommands(opts) if not utils.is_nvim_tree_buf(0) then return end - if opts.actions.open_file.eject then - view.View:prevent_buffer_override() - else - view.View:abandon_current_window() + + local explorer = core.get_explorer() + if explorer then + if opts.actions.open_file.eject then + explorer.view:prevent_buffer_override() + else + explorer.view:abandon_current_window() + end end end, }) @@ -226,12 +229,16 @@ local function setup_autocommands(opts) }) end + -- TODO #2826 move this to explorer if opts.view.float.enable and opts.view.float.quit_on_focus_loss then create_nvim_tree_autocmd("WinLeave", { pattern = "NvimTree_*", callback = function() if utils.is_nvim_tree_buf(0) then - view.close() + local explorer = core.get_explorer() + if explorer then + explorer.view:close() + end end end, }) @@ -692,10 +699,10 @@ local function localise_default_opts() end function M.purge_all_state() - view.View:close_all_tabs() - view.View:abandon_all_windows() local explorer = core.get_explorer() if explorer then + explorer.view:close_all_tabs() + explorer.view:abandon_all_windows() require("nvim-tree.git").purge_state() explorer:destroy() core.reset_explorer() @@ -748,7 +755,6 @@ function M.setup(conf) require("nvim-tree.explorer.watch").setup(opts) require("nvim-tree.git").setup(opts) require("nvim-tree.git.utils").setup(opts) - require("nvim-tree.view").setup(opts) require("nvim-tree.lib").setup(opts) require("nvim-tree.renderer.components").setup(opts) require("nvim-tree.buffers").setup(opts) diff --git a/lua/nvim-tree/actions/finders/find-file.lua b/lua/nvim-tree/actions/finders/find-file.lua index 3ecd7e17c1b..2ea494fd85c 100644 --- a/lua/nvim-tree/actions/finders/find-file.lua +++ b/lua/nvim-tree/actions/finders/find-file.lua @@ -1,5 +1,4 @@ local log = require("nvim-tree.log") -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") local core = require("nvim-tree.core") @@ -14,7 +13,7 @@ local running = {} ---@param path string relative or absolute function M.fn(path) local explorer = core.get_explorer() - if not explorer or not view.View:is_visible() then + if not explorer or not explorer.view:is_visible() then return end @@ -84,9 +83,9 @@ function M.fn(path) end) :iterate() - if found and view.View:is_visible() then + if found and explorer.view:is_visible() then explorer.renderer:draw() - view.View:set_cursor({ line, 0 }) + explorer.view:set_cursor({ line, 0 }) end running[path_real] = false diff --git a/lua/nvim-tree/actions/fs/remove-file.lua b/lua/nvim-tree/actions/fs/remove-file.lua index 8a0f67cda3a..fd71ae6a376 100644 --- a/lua/nvim-tree/actions/fs/remove-file.lua +++ b/lua/nvim-tree/actions/fs/remove-file.lua @@ -1,7 +1,6 @@ local core = require("nvim-tree.core") local utils = require("nvim-tree.utils") local events = require("nvim-tree.events") -local view = require("nvim-tree.view") local lib = require("nvim-tree.lib") local notify = require("nvim-tree.notify") @@ -14,10 +13,12 @@ local M = { ---@param windows integer[] local function close_windows(windows) + local explorer = core.get_explorer() + -- Prevent from closing when the win count equals 1 or 2, -- where the win to remove could be the last opened. -- For details see #2503. - if view.View.float.enable and #vim.api.nvim_list_wins() < 3 then + if explorer and explorer.view.float.enable and #vim.api.nvim_list_wins() < 3 then return end @@ -30,16 +31,17 @@ end ---@param absolute_path string local function clear_buffer(absolute_path) + local explorer = core.get_explorer() local bufs = vim.fn.getbufinfo({ bufloaded = 1, buflisted = 1 }) for _, buf in pairs(bufs) do if buf.name == absolute_path then local tree_winnr = vim.api.nvim_get_current_win() - if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then + if buf.hidden == 0 and (#bufs > 1 or explorer and explorer.view.float.enable) then vim.api.nvim_set_current_win(buf.windows[1]) vim.cmd(":bn") end vim.api.nvim_buf_delete(buf.bufnr, { force = true }) - if not view.View.float.quit_on_focus_loss then + if explorer and not explorer.view.float.quit_on_focus_loss then vim.api.nvim_set_current_win(tree_winnr) end if M.config.actions.remove_file.close_window then diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index 2bc849bd03d..599a8053027 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -1,5 +1,4 @@ local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") local core = require("nvim-tree.core") local diagnostics = require("nvim-tree.diagnostics") @@ -67,9 +66,9 @@ local function move(explorer, where, what, skip_gitignored) end if nex then - view.View:set_cursor({ nex, 0 }) + explorer.view:set_cursor({ nex, 0 }) elseif vim.o.wrapscan and first then - view.View:set_cursor({ first, 0 }) + explorer.view:set_cursor({ first, 0 }) end end @@ -189,13 +188,13 @@ local function move_prev_recursive(explorer, what, skip_gitignored) -- 4.3) if node_init.name == ".." then -- root node - view.View:set_cursor({ 1, 0 }) -- move to root node (position 1) + explorer.view:set_cursor({ 1, 0 }) -- move to root node (position 1) else local node_init_line = utils.find_node_line(node_init) if node_init_line < 0 then return end - view.View:set_cursor({ node_init_line, 0 }) + explorer.view:set_cursor({ node_init_line, 0 }) end -- 4.4) diff --git a/lua/nvim-tree/actions/moves/parent.lua b/lua/nvim-tree/actions/moves/parent.lua index 2c08521fb66..fab1a440a2f 100644 --- a/lua/nvim-tree/actions/moves/parent.lua +++ b/lua/nvim-tree/actions/moves/parent.lua @@ -1,4 +1,3 @@ -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") local DirectoryNode = require("nvim-tree.node.directory") @@ -25,7 +24,7 @@ function M.fn(should_close) local parent = (node:get_parent_of_group() or node).parent if not parent or not parent.parent then - view.View:set_cursor({ 1, 0 }) + node.explorer.view:set_cursor({ 1, 0 }) return end @@ -33,7 +32,7 @@ function M.fn(should_close) return n.absolute_path == parent.absolute_path end) - view.View:set_cursor({ line + 1, 0 }) + node.explorer.view:set_cursor({ line + 1, 0 }) if should_close then parent.open = false parent.explorer.renderer:draw() diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index c6de718a398..99594520f11 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -2,7 +2,7 @@ local lib = require("nvim-tree.lib") local notify = require("nvim-tree.notify") local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") +local core = require("nvim-tree.core") local M = {} @@ -19,9 +19,10 @@ end ---Get all windows in the current tabpage that aren't NvimTree. ---@return table with valid win_ids local function usable_win_ids() + local explorer = core.get_explorer() local tabpage = vim.api.nvim_get_current_tabpage() local win_ids = vim.api.nvim_tabpage_list_wins(tabpage) - local tree_winid = view.View:get_winnr(tabpage) + local tree_winid = explorer and explorer.view:get_winnr(tabpage) return vim.tbl_filter(function(id) local bufid = vim.api.nvim_win_get_buf(id) @@ -187,7 +188,10 @@ end local function open_file_in_tab(filename) if M.quit_on_open then - view.View:close() + local explorer = core.get_explorer() + if explorer then + explorer.view:close() + end end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -197,7 +201,10 @@ end local function drop(filename) if M.quit_on_open then - view.View:close() + local explorer = core.get_explorer() + if explorer then + explorer.view:close() + end end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -207,7 +214,10 @@ end local function tab_drop(filename) if M.quit_on_open then - view.View:close() + local explorer = core.get_explorer() + if explorer then + explorer.view:close() + end end if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) @@ -228,7 +238,10 @@ local function on_preview(buf_loaded) once = true, }) end - view.View:focus() + local explorer = core.get_explorer() + if explorer then + explorer.view:focus() + end end local function get_target_winid(mode) @@ -273,6 +286,8 @@ local function set_current_win_no_autocmd(winid, autocmd) end local function open_in_new_window(filename, mode) + local explorer = core.get_explorer() + if type(mode) ~= "string" then mode = "" end @@ -295,7 +310,11 @@ local function open_in_new_window(filename, mode) end, vim.api.nvim_list_wins()) local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one - local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright" + + local new_window_side = "belowright" + if explorer and (explorer.view.side == "right") then + new_window_side = "aboveleft" + end -- Target is invalid: create new window if not vim.tbl_contains(win_ids, target_winid) then @@ -327,7 +346,7 @@ local function open_in_new_window(filename, mode) end end - if (mode == "preview" or mode == "preview_no_picker") and view.View.float.enable then + if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.view.float.enable then -- ignore "WinLeave" autocmd on preview -- because the registered "WinLeave" -- will kill the floating window immediately @@ -367,7 +386,12 @@ local function is_already_loaded(filename) end local function edit_in_current_buf(filename) - require("nvim-tree.view").View:abandon_current_window() + local explorer = core.get_explorer() + + if explorer then + explorer.view:abandon_current_window() + end + if M.relative_path then filename = utils.path_relative(filename, vim.fn.getcwd()) end @@ -378,6 +402,8 @@ end ---@param filename string ---@return nil function M.fn(mode, filename) + local explorer = core.get_explorer() + if type(mode) ~= "string" then mode = "" end @@ -412,16 +438,16 @@ function M.fn(mode, filename) vim.bo.bufhidden = "" end - if M.resize_window then - view.View:resize() + if M.resize_window and explorer then + explorer.view:resize() end if mode == "preview" or mode == "preview_no_picker" then return on_preview(buf_loaded) end - if M.quit_on_open then - view.View:close() + if M.quit_on_open and explorer then + explorer.view:close() end end diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua index 19b6a9e576f..73d1df3c75e 100644 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ b/lua/nvim-tree/actions/tree/find-file.lua @@ -1,6 +1,5 @@ local core = require("nvim-tree.core") local lib = require("nvim-tree.lib") -local view = require("nvim-tree.view") local finders_find_file = require("nvim-tree.actions.finders.find-file") local M = {} @@ -41,11 +40,12 @@ function M.fn(opts) return end - if view.View:is_visible() then + local explorer = core.get_explorer() + if explorer and explorer.view:is_visible() then -- focus if opts.focus then lib.set_target_win() - view.View:focus() + explorer.view:focus() end elseif opts.open then -- open diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index d03db93fea9..17e2c581a7b 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -1,5 +1,5 @@ +local core = require("nvim-tree.core") local lib = require("nvim-tree.lib") -local view = require("nvim-tree.view") local finders_find_file = require("nvim-tree.actions.finders.find-file") local M = {} @@ -23,10 +23,12 @@ function M.fn(opts) opts.path = nil end - if view.View:is_visible() then + local explorer = core.get_explorer() + + if explorer and explorer.view:is_visible() then -- focus lib.set_target_win() - view.View:focus() + explorer.view:focus() else -- open lib.open({ diff --git a/lua/nvim-tree/actions/tree/resize.lua b/lua/nvim-tree/actions/tree/resize.lua index 8976fba8de5..48f090887f7 100644 --- a/lua/nvim-tree/actions/tree/resize.lua +++ b/lua/nvim-tree/actions/tree/resize.lua @@ -1,14 +1,19 @@ -local view = require("nvim-tree.view") +local core = require("nvim-tree.core") local M = {} ---Resize the tree, persisting the new size. ---@param opts ApiTreeResizeOpts|nil function M.fn(opts) + local explorer = core.get_explorer() + if not explorer then + return + end + if opts == nil then -- reset to config values - view.View:configure_width() - view.View:resize() + explorer.view:configure_width() + explorer.view:resize() return end @@ -16,19 +21,19 @@ function M.fn(opts) local width_cfg = options.width if width_cfg ~= nil then - view.View:configure_width(width_cfg) - view.View:resize() + explorer.view:configure_width(width_cfg) + explorer.view:resize() return end - if not view.View:is_width_determined() then + if not explorer.view:is_width_determined() then -- {absolute} and {relative} do nothing when {width} is a function. return end local absolute = options.absolute if type(absolute) == "number" then - view.View:resize(absolute) + explorer.view:resize(absolute) return end @@ -39,7 +44,7 @@ function M.fn(opts) relative_size = "+" .. relative_size end - view.View:resize(relative_size) + explorer.view:resize(relative_size) return end end diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index 34afa4bf56d..fb976cfbe71 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -1,5 +1,5 @@ +local core = require("nvim-tree.core") local lib = require("nvim-tree.lib") -local view = require("nvim-tree.view") local finders_find_file = require("nvim-tree.actions.finders.find-file") local M = {} @@ -10,6 +10,8 @@ local M = {} ---@param cwd boolean|nil legacy -> opts.path ---@param bang boolean|nil legacy -> opts.update_root function M.fn(opts, no_focus, cwd, bang) + local explorer = core.get_explorer() + -- legacy arguments if type(opts) == "boolean" then opts = { @@ -40,9 +42,9 @@ function M.fn(opts, no_focus, cwd, bang) opts.path = nil end - if view.View:is_visible() then + if explorer and explorer.view:is_visible() then -- close - view.View:close() + explorer.view:close() else -- open lib.open({ diff --git a/lua/nvim-tree/commands.lua b/lua/nvim-tree/commands.lua index 464728f6d23..88ac3e1e61e 100644 --- a/lua/nvim-tree/commands.lua +++ b/lua/nvim-tree/commands.lua @@ -1,5 +1,5 @@ local api = require("nvim-tree.api") -local view = require("nvim-tree.view") +local core = require("nvim-tree.core") local M = {} @@ -111,7 +111,10 @@ local CMDS = { bar = true, }, command = function(c) - view.View:resize(c.args) + local explorer = core.get_explorer() + if explorer then + explorer.view:resize(c.args) + end end, }, { diff --git a/lua/nvim-tree/core.lua b/lua/nvim-tree/core.lua index 943d3c80336..fe9125e612d 100644 --- a/lua/nvim-tree/core.lua +++ b/lua/nvim-tree/core.lua @@ -1,6 +1,5 @@ local events = require("nvim-tree.events") local notify = require("nvim-tree.notify") -local view = require("nvim-tree.view") local log = require("nvim-tree.log") local M = {} @@ -55,7 +54,7 @@ end ---@return integer function M.get_nodes_starting_line() local offset = 1 - if view.View:is_root_folder_visible(M.get_cwd()) then + if TreeExplorer and TreeExplorer.view:is_root_folder_visible(M.get_cwd()) then offset = offset + 1 end if TreeExplorer and TreeExplorer.live_filter.filter then diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index 2ded2fb535e..e6484caf23e 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -1,6 +1,5 @@ local core = require("nvim-tree.core") local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") local log = require("nvim-tree.log") local DirectoryNode = require("nvim-tree.node.directory") @@ -182,10 +181,15 @@ function M.update_coc() end log.profile_end(profile) - local bufnr = view.View:get_bufnr() + local explorer = core.get_explorer() + + local bufnr + if explorer then + bufnr = explorer.view:get_bufnr() + end + local should_draw = bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr) if should_draw then - local explorer = core.get_explorer() if explorer then explorer.renderer:draw() end diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index d138df10c9b..49715f6b5f0 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -4,7 +4,6 @@ local core = require("nvim-tree.core") local git = require("nvim-tree.git") local log = require("nvim-tree.log") local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") local node_factory = require("nvim-tree.node.factory") local DirectoryNode = require("nvim-tree.node.directory") @@ -20,6 +19,7 @@ local LiveFilter = require("nvim-tree.explorer.live-filter") local Sorter = require("nvim-tree.explorer.sorter") local Clipboard = require("nvim-tree.actions.fs.clipboard") local Renderer = require("nvim-tree.renderer") +local View = require("nvim-tree.view") local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON @@ -35,6 +35,7 @@ local config ---@field sorters Sorter ---@field marks Marks ---@field clipboard Clipboard +---@field view View local Explorer = RootNode:extend() ---@class Explorer @@ -64,6 +65,7 @@ function Explorer:new(args) self.live_filter = LiveFilter({ explorer = self }) self.marks = Marks({ explorer = self }) self.clipboard = Clipboard({ explorer = self }) + self.view = View({ explorer = self }) self:create_autocmds() @@ -84,7 +86,7 @@ function Explorer:create_autocmds() group = self.augroup_id, callback = function() appearance.setup() - view.View:reset_winhl() + self.view:reset_winhl() self.renderer:draw() end, }) @@ -486,7 +488,7 @@ function Explorer:reload_explorer() local projects = git.reload_all_projects() self:refresh_nodes(projects) - if view.View:is_visible() then + if self.view:is_visible() then self.renderer:draw() end event_running = false @@ -508,7 +510,7 @@ end ---nil on no explorer or invalid view win ---@return integer[]|nil function Explorer:get_cursor_position() - local winnr = view.View:get_winnr() + local winnr = self.view:get_winnr() if not winnr or not vim.api.nvim_win_is_valid(winnr) then return end @@ -523,7 +525,7 @@ function Explorer:get_node_at_cursor() return end - if cursor[1] == 1 and view.View:is_root_folder_visible(core.get_cwd()) then + if cursor[1] == 1 and self.view:is_root_folder_visible(core.get_cwd()) then return self end diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 22df536efc9..3a7c469c811 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -1,4 +1,3 @@ -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") local Class = require("nvim-tree.classic") @@ -56,14 +55,14 @@ local overlay_bufnr = 0 local overlay_winnr = 0 local function remove_overlay(self) - if view.View.float.enable and view.View.float.quit_on_focus_loss then + if self.explorer.view.float.enable and self.explorer.view.float.quit_on_focus_loss then -- return to normal nvim-tree float behaviour when filter window is closed vim.api.nvim_create_autocmd("WinLeave", { pattern = "NvimTree_*", group = vim.api.nvim_create_augroup("NvimTree", { clear = false }), callback = function() if utils.is_nvim_tree_buf(0) then - view.View:close() + self.explorer.view:close() end end, }) @@ -156,7 +155,7 @@ end ---@return integer local function calculate_overlay_win_width(self) - local wininfo = vim.fn.getwininfo(view.View:get_winnr())[1] + local wininfo = vim.fn.getwininfo(self.explorer.view:get_winnr())[1] if wininfo then return wininfo.width - wininfo.textoff - #self.prefix @@ -166,7 +165,7 @@ local function calculate_overlay_win_width(self) end local function create_overlay(self) - if view.View.float.enable then + if self.explorer.view.float.enable then -- don't close nvim-tree float when focus is changed to filter window vim.api.nvim_clear_autocmds({ event = "WinLeave", @@ -198,13 +197,13 @@ local function create_overlay(self) end function LiveFilter:start_filtering() - view.View.live_filter.prev_focused_node = self.explorer:get_node_at_cursor() + self.explorer.view.live_filter.prev_focused_node = self.explorer:get_node_at_cursor() self.filter = self.filter or "" self.explorer.renderer:draw() local row = require("nvim-tree.core").get_nodes_starting_line() - 1 local col = #self.prefix > 0 and #self.prefix - 1 or 1 - view.View:set_cursor({ row, col }) + self.explorer.view:set_cursor({ row, col }) -- needs scheduling to let the cursor move before initializing the window vim.schedule(function() return create_overlay(self) @@ -213,7 +212,7 @@ end function LiveFilter:clear_filter() local node = self.explorer:get_node_at_cursor() - local last_node = view.View.live_filter.prev_focused_node + local last_node = self.explorer.view.live_filter.prev_focused_node self.filter = nil reset_filter(self) diff --git a/lua/nvim-tree/iterators/node-iterator.lua b/lua/nvim-tree/iterators/node-iterator.lua index 8cd4abdeda6..925a0a17955 100644 --- a/lua/nvim-tree/iterators/node-iterator.lua +++ b/lua/nvim-tree/iterators/node-iterator.lua @@ -48,7 +48,7 @@ function NodeIterator:recursor(f) end ---@return Node|nil ----@return number|nil +---@return number function NodeIterator:iterate() local iteration_count = 0 local function iter(nodes) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index bd203cf3335..4793075853b 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -1,4 +1,3 @@ -local view = require("nvim-tree.view") local core = require("nvim-tree.core") local notify = require("nvim-tree.notify") @@ -12,9 +11,11 @@ local M = { } function M.set_target_win() + local explorer = core.get_explorer() + local id = vim.api.nvim_get_current_win() - local tree_id = view.View:get_winnr() - if tree_id and id == tree_id then + + if explorer and id == explorer.view:get_winnr() then M.target_winid = 0 return end @@ -30,11 +31,16 @@ local function handle_buf_cwd(cwd) end local function open_view_and_draw() + local explorer = core.get_explorer() + local cwd = vim.fn.getcwd() - view.View:open() + + if explorer then + explorer.view:open() + end + handle_buf_cwd(cwd) - local explorer = core.get_explorer() if explorer then explorer.renderer:draw() end @@ -110,25 +116,28 @@ function M.open(opts) local explorer = core.get_explorer() if should_hijack_current_buf() then - view.View:close_this_tab_only() - view.View:open_in_win() if explorer then + explorer.view:close_this_tab_only() + explorer.view:open_in_win() explorer.renderer:draw() end elseif opts.winid then - view.View:open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid }) if explorer then + explorer.view:open_in_win({ hijack_current_buf = false, resize = false, winid = opts.winid }) explorer.renderer:draw() end elseif opts.current_window then - view.View:open_in_win({ hijack_current_buf = false, resize = false }) if explorer then + explorer.view:open_in_win({ hijack_current_buf = false, resize = false }) explorer.renderer:draw() end else open_view_and_draw() end - view.View:restore_tab_state() + + if explorer then + explorer.view:restore_tab_state() + end end function M.setup(opts) diff --git a/lua/nvim-tree/renderer/builder.lua b/lua/nvim-tree/renderer/builder.lua index 531f1a0a909..d2727f3f4ac 100644 --- a/lua/nvim-tree/renderer/builder.lua +++ b/lua/nvim-tree/renderer/builder.lua @@ -1,6 +1,5 @@ local notify = require("nvim-tree.notify") local utils = require("nvim-tree.utils") -local view = require("nvim-tree.view") local Class = require("nvim-tree.classic") @@ -379,7 +378,7 @@ end ---@private function Builder:build_header() - if view.View:is_root_folder_visible(self.explorer.absolute_path) then + if self.explorer.view:is_root_folder_visible(self.explorer.absolute_path) then local root_name = self:format_root_name(self.explorer.opts.renderer.root_folder_label) table.insert(self.lines, root_name) self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name)) diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 8da0b28424d..66b2098c191 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -1,5 +1,4 @@ local log = require("nvim-tree.log") -local view = require("nvim-tree.view") local events = require("nvim-tree.events") local Class = require("nvim-tree.classic") @@ -96,28 +95,28 @@ function Renderer:render_hl(bufnr, hl_range_args) end function Renderer:draw() - local bufnr = view.View:get_bufnr() + local bufnr = self.explorer.view:get_bufnr() if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then return end local profile = log.profile_start("draw") - local cursor = vim.api.nvim_win_get_cursor(view.View:get_winnr() or 0) + local cursor = vim.api.nvim_win_get_cursor(self.explorer.view:get_winnr() or 0) local builder = Builder(self.explorer):build() self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines) if cursor and #builder.lines >= cursor[1] then - vim.api.nvim_win_set_cursor(view.View:get_winnr() or 0, cursor) + vim.api.nvim_win_set_cursor(self.explorer.view:get_winnr() or 0, cursor) end - view.View:grow_from_content() + self.explorer.view:grow_from_content() log.profile_end(profile) - events._dispatch_on_tree_rendered(bufnr, view.View:get_winnr()) + events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr()) end return Renderer diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 01da141382e..e33aaaf1091 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -143,10 +143,16 @@ function M.find_node(nodes, fn) return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes) end) :iterate() - i = require("nvim-tree.view").View:is_root_folder_visible() and i or i - 1 - if node and node.explorer.live_filter.filter then - i = i + 1 + + if node then + if not node.explorer.view:is_root_folder_visible() then + i = i - 1 + end + if node.explorer.live_filter.filter then + i = i + 1 + end end + return node, i end @@ -537,7 +543,10 @@ function M.focus_file(path) local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node) return node.absolute_path == path end) - require("nvim-tree.view").View:set_cursor({ i + 1, 1 }) + local explorer = require("nvim-tree.core").get_explorer() + if explorer then + explorer.view:set_cursor({ i + 1, 1 }) + end end ---Focus node passed as parameter if visible, otherwise focus first visible parent. @@ -557,7 +566,7 @@ function M.focus_node_or_parent(node) end) if found_node or node.parent == nil then - require("nvim-tree.view").View:set_cursor({ i + 1, 1 }) + explorer.view:set_cursor({ i + 1, 1 }) break end diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 0bd06f77652..fea735a1b53 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -10,8 +10,6 @@ local Class = require("nvim-tree.classic") ---@field resize boolean|nil default true ---@field winid number|nil 0 or nil for current -local M = {} - local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MAX_WIDTH = -1 local DEFAULT_PADDING = 1 @@ -664,9 +662,4 @@ function View:configure_width(width) end end -function M.setup(opts) - -- TODO #2826 move this to explorer constructor - M.View = View({ explorer = { opts = opts } }) ---@diagnostic disable-line: missing-fields -end - -return M +return View From 89d68db3aa5c9c1550f03fed987b139841ab56a6 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Apr 2025 12:17:21 +1000 Subject: [PATCH 10/32] refactor(#2826): move autocmds to Explorer --- lua/nvim-tree.lua | 35 --------------------------------- lua/nvim-tree/explorer/init.lua | 28 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index e03a4a1d0c1..0fdb61e08ac 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -148,26 +148,6 @@ local function setup_autocommands(opts) vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) end - -- TODO #2826 move this to explorer - -- prevent new opened file from opening in the same window as nvim-tree - create_nvim_tree_autocmd("BufWipeout", { - pattern = "NvimTree_*", - callback = function() - if not utils.is_nvim_tree_buf(0) then - return - end - - local explorer = core.get_explorer() - if explorer then - if opts.actions.open_file.eject then - explorer.view:prevent_buffer_override() - else - explorer.view:abandon_current_window() - end - end - end, - }) - if opts.tab.sync.open then create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) }) end @@ -229,21 +209,6 @@ local function setup_autocommands(opts) }) end - -- TODO #2826 move this to explorer - if opts.view.float.enable and opts.view.float.quit_on_focus_loss then - create_nvim_tree_autocmd("WinLeave", { - pattern = "NvimTree_*", - callback = function() - if utils.is_nvim_tree_buf(0) then - local explorer = core.get_explorer() - if explorer then - explorer.view:close() - end - end - end, - }) - end - -- Handles event dispatch when tree is closed by `:q` create_nvim_tree_autocmd("WinClosed", { pattern = "*", diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 49715f6b5f0..6da98b2a65b 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -91,6 +91,18 @@ function Explorer:create_autocmds() end, }) + if self.opts.view.float.enable and self.opts.view.float.quit_on_focus_loss then + vim.api.nvim_create_autocmd("WinLeave", { + group = self.augroup_id, + pattern = "NvimTree_*", + callback = function() + if utils.is_nvim_tree_buf(0) then + self.view:close() + end + end, + }) + end + vim.api.nvim_create_autocmd("BufWritePost", { group = self.augroup_id, callback = function() @@ -143,6 +155,22 @@ function Explorer:create_autocmds() 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", { group = self.augroup_id, pattern = "NvimTree_*", From ee72e97d13f8f1f0a84d19d682df2ae88df4841a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Apr 2025 14:12:12 +1000 Subject: [PATCH 11/32] refactor(#2826): API uses Explorer's View --- lua/nvim-tree/api.lua | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 0fe8d238d90..3f70df2c880 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -1,7 +1,4 @@ --- TODO #2826 wrap all the view methods in explorer - local core = require("nvim-tree.core") -local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") local actions = require("nvim-tree.actions") local appearance_hi_test = require("nvim-tree.appearance.hi-test") @@ -143,9 +140,9 @@ Api.tree.focus = Api.tree.open ---@field focus boolean|nil default true Api.tree.toggle = wrap(actions.tree.toggle.fn) -Api.tree.close = wrap(view.close) -Api.tree.close_in_this_tab = wrap(view.close_this_tab_only) -Api.tree.close_in_all_tabs = wrap(view.close_all_tabs) +Api.tree.close = wrap_explorer_member("view", "close") +Api.tree.close_in_this_tab = wrap_explorer_member("view", "close_this_tab_only") +Api.tree.close_in_all_tabs = wrap_explorer_member("view", "close_all_tabs") Api.tree.reload = wrap_explorer("reload_explorer") ---@class ApiTreeResizeOpts @@ -204,12 +201,12 @@ Api.tree.is_tree_buf = wrap(utils.is_nvim_tree_buf) ---@field tabpage number|nil ---@field any_tabpage boolean|nil default false -Api.tree.is_visible = wrap(view.is_visible) +Api.tree.is_visible = wrap_explorer_member("view", "is_visible") ---@class ApiTreeWinIdOpts ---@field tabpage number|nil default nil -Api.tree.winid = wrap(view.winid) +Api.tree.winid = wrap_explorer_member("view", "winid") Api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn) Api.fs.remove = wrap_node(actions.fs.remove_file.fn) @@ -241,13 +238,17 @@ local function edit(mode, node, edit_opts) local path = file_link and file_link.link_to or node.absolute_path local cur_tabpage = vim.api.nvim_get_current_tabpage() + local explorer = core.get_explorer() + actions.node.open_file.fn(mode, path) edit_opts = edit_opts or {} local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then - view.View:close(cur_tabpage) + if explorer then + explorer.view:close(cur_tabpage) + end end local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" @@ -257,7 +258,9 @@ local function edit(mode, node, edit_opts) if mode == "tabnew" then vim.cmd(":tabprev") end - view.View:focus() + if explorer then + explorer.view:focus() + end end end From 110fa7e7a4c965129e9375667079d026fe420918 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Apr 2025 14:23:34 +1000 Subject: [PATCH 12/32] refactor(#2826): move View into Explorer package --- lua/nvim-tree/explorer/init.lua | 2 +- lua/nvim-tree/{ => explorer}/view.lua | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename lua/nvim-tree/{ => explorer}/view.lua (100%) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 6da98b2a65b..5073652c5ce 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -19,7 +19,7 @@ local LiveFilter = require("nvim-tree.explorer.live-filter") local Sorter = require("nvim-tree.explorer.sorter") local Clipboard = require("nvim-tree.actions.fs.clipboard") local Renderer = require("nvim-tree.renderer") -local View = require("nvim-tree.view") +local View = require("nvim-tree.explorer.view") local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/explorer/view.lua similarity index 100% rename from lua/nvim-tree/view.lua rename to lua/nvim-tree/explorer/view.lua From 8438a74ae14ab16a10e793ac21b2910fd4e1a1ac Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 3 May 2025 08:04:06 +1000 Subject: [PATCH 13/32] refactor(#2826): retain necessary view globals --- lua/nvim-tree.lua | 1 + lua/nvim-tree/actions/fs/clipboard.lua | 2 + lua/nvim-tree/explorer/filters.lua | 2 + lua/nvim-tree/explorer/init.lua | 10 ++++- lua/nvim-tree/explorer/live-filter.lua | 2 + lua/nvim-tree/explorer/sorter.lua | 2 + lua/nvim-tree/explorer/view.lua | 54 +++++++++++++++----------- lua/nvim-tree/log.lua | 2 +- lua/nvim-tree/marks/init.lua | 2 + lua/nvim-tree/renderer/init.lua | 2 + 10 files changed, 55 insertions(+), 24 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 0fdb61e08ac..449ac2b7eea 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -496,6 +496,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS dev = false, diagnostics = false, git = false, + lifecycle = true, profile = false, watcher = false, }, diff --git a/lua/nvim-tree/actions/fs/clipboard.lua b/lua/nvim-tree/actions/fs/clipboard.lua index 3f3ed37c94d..a8c6e15e958 100644 --- a/lua/nvim-tree/actions/fs/clipboard.lua +++ b/lua/nvim-tree/actions/fs/clipboard.lua @@ -31,6 +31,8 @@ local Clipboard = Class:extend() ---@protected ---@param args ClipboardArgs function Clipboard:new(args) + args.explorer:log_lifecycle("Clipboard:new") + self.explorer = args.explorer self.data = { diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 62230687e40..c6640fb42ee 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -23,6 +23,8 @@ local Filters = Class:extend() ---@protected ---@param args FiltersArgs function Filters:new(args) + args.explorer:log_lifecycle("Filters:new") + self.explorer = args.explorer self.ignore_list = {} self.exclude_list = self.explorer.opts.filters.exclude diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 5073652c5ce..71b7b4e1780 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -56,6 +56,8 @@ function Explorer:new(args) self.uid_explorer = vim.loop.hrtime() self.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. self.uid_explorer, {}) + self:log_lifecycle("Explorer:new") + self.open = true self.opts = config @@ -73,7 +75,7 @@ function Explorer:new(args) end function Explorer:destroy() - log.line("dev", "Explorer:destroy") + self:log_lifecycle("Explorer:des") vim.api.nvim_del_augroup_by_id(self.augroup_id) @@ -587,6 +589,12 @@ function Explorer:get_nodes() return self:clone() end +---Log a lifecycle message with uid_explorer and absolute_path +---@param msg string? +function Explorer:log_lifecycle(msg) + log.line("lifecycle", "%-15s %d %s", msg, self.uid_explorer, self.absolute_path) +end + function Explorer:setup(opts) config = opts end diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 3a7c469c811..704945e1dc3 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -20,6 +20,8 @@ local LiveFilter = Class:extend() ---@protected ---@param args LiveFilterArgs function LiveFilter:new(args) + args.explorer:log_lifecycle("LiveFilter:new") + self.explorer = args.explorer self.prefix = self.explorer.opts.live_filter.prefix self.always_show_folders = self.explorer.opts.live_filter.always_show_folders diff --git a/lua/nvim-tree/explorer/sorter.lua b/lua/nvim-tree/explorer/sorter.lua index 799cfa481b1..3fd475e3f43 100644 --- a/lua/nvim-tree/explorer/sorter.lua +++ b/lua/nvim-tree/explorer/sorter.lua @@ -19,6 +19,8 @@ local Sorter = Class:extend() ---@protected ---@param args SorterArgs function Sorter:new(args) + args.explorer:log_lifecycle("Sorter:new") + self.explorer = args.explorer end diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index fea735a1b53..cbe4ebb84b4 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -10,10 +10,18 @@ local Class = require("nvim-tree.classic") ---@field resize boolean|nil default true ---@field winid number|nil 0 or nil for current +local M = {} + local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MAX_WIDTH = -1 local DEFAULT_PADDING = 1 +-- TODO global, rework for multiinstance explorer +-- M.View retained for simpler change history +M.View = { + tabpages = {} +} + ---@class (exact) View: Class ---@field live_filter table ---@field side string @@ -21,7 +29,6 @@ local DEFAULT_PADDING = 1 ---@field private explorer Explorer ---@field private adaptive_size boolean ---@field private centralize_selection boolean ----@field private tabpages table ---@field private cursors table as per vim.api.nvim_win_get_cursor ---@field private hide_root_folder boolean ---@field private winopts table @@ -31,7 +38,6 @@ local DEFAULT_PADDING = 1 ---@field private width (fun():integer)|integer|string ---@field private max_width integer ---@field private padding integer ----@field private bufnr_per_tab table local View = Class:extend() ---@class View @@ -43,9 +49,10 @@ local View = Class:extend() ---@protected ---@param args ViewArgs function View:new(args) + args.explorer:log_lifecycle("View:new") + self.explorer = args.explorer self.adaptive_size = false - self.bufnr_per_tab = {} self.centralize_selection = self.explorer.opts.view.centralize_selection self.cursors = {} self.float = self.explorer.opts.view.float @@ -53,7 +60,6 @@ function View:new(args) self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions self.side = (self.explorer.opts.view.side == "right") and "right" or "left" - self.tabpages = {} self.live_filter = { prev_focused_node = nil, } self.winopts = { @@ -100,6 +106,10 @@ local tabinitial = { winnr = nil, } +-- TODO global, rework for multiinstance explorer +---@type table +local BUFNR_PER_TAB = {} + ---@type { name: string, value: any }[] local BUFFER_OPTIONS = { { name = "bufhidden", value = "wipe" }, @@ -114,7 +124,7 @@ local BUFFER_OPTIONS = { ---@param bufnr integer ---@return boolean function View:matches_bufnr(bufnr) - for _, b in pairs(self.bufnr_per_tab) do + for _, b in pairs(BUFNR_PER_TAB) do if b == bufnr then return true end @@ -137,7 +147,7 @@ function View:create_buffer(bufnr) self:wipe_rogue_buffer() local tab = vim.api.nvim_get_current_tabpage() - self.bufnr_per_tab[tab] = bufnr or vim.api.nvim_create_buf(false, false) + BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab) bufnr = self:get_bufnr() @@ -184,7 +194,7 @@ local move_tbl = { ---@param tabpage integer function View:setup_tabpage(tabpage) local winnr = vim.api.nvim_get_current_win() - self.tabpages[tabpage] = vim.tbl_extend("force", self.tabpages[tabpage] or tabinitial, { winnr = winnr }) + M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr }) end ---@private @@ -308,7 +318,7 @@ function View:close_this_tab_only() end function View:close_all_tabs() - for tabpage, _ in pairs(self.tabpages) do + for tabpage, _ in pairs(M.View.tabpages) do self:close_internal(tabpage) end end @@ -447,7 +457,7 @@ end ---@private function View:set_current_win() local current_tab = vim.api.nvim_get_current_tabpage() - self.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() + M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() end ---Open the tree in the a window @@ -471,17 +481,17 @@ end function View:abandon_current_window() local tab = vim.api.nvim_get_current_tabpage() - self.bufnr_per_tab[tab] = nil - if self.tabpages[tab] then - self.tabpages[tab].winnr = nil + BUFNR_PER_TAB[tab] = nil + if M.View.tabpages[tab] then + M.View.tabpages[tab].winnr = nil end end function View:abandon_all_windows() for tab, _ in pairs(vim.api.nvim_list_tabpages()) do - self.bufnr_per_tab[tab] = nil - if self.tabpages[tab] then - self.tabpages[tab].winnr = nil + BUFNR_PER_TAB[tab] = nil + if M.View.tabpages[tab] then + M.View.tabpages[tab].winnr = nil end end end @@ -490,15 +500,15 @@ end ---@return boolean function View:is_visible(opts) if opts and opts.tabpage then - if self.tabpages[opts.tabpage] == nil then + if M.View.tabpages[opts.tabpage] == nil then return false end - local winnr = self.tabpages[opts.tabpage].winnr + local winnr = M.View.tabpages[opts.tabpage].winnr return winnr and vim.api.nvim_win_is_valid(winnr) end if opts and opts.any_tabpage then - for _, v in pairs(self.tabpages) do + for _, v in pairs(M.View.tabpages) do if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then return true end @@ -560,7 +570,7 @@ end ---@return number|nil function View:get_winnr(tabpage) tabpage = tabpage or vim.api.nvim_get_current_tabpage() - local tabinfo = self.tabpages[tabpage] + local tabinfo = M.View.tabpages[tabpage] if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then return tabinfo.winnr end @@ -569,7 +579,7 @@ end --- Returns the current nvim tree bufnr ---@return number function View:get_bufnr() - return self.bufnr_per_tab[vim.api.nvim_get_current_tabpage()] + return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()] end function View:prevent_buffer_override() @@ -586,9 +596,9 @@ function View:prevent_buffer_override() local bufname = vim.api.nvim_buf_get_name(curbuf) if not bufname:match("NvimTree") then - for i, tabpage in ipairs(self.tabpages) do + for i, tabpage in ipairs(M.View.tabpages) do if tabpage.winnr == view_winnr then - self.tabpages[i] = nil + M.View.tabpages[i] = nil break end end diff --git a/lua/nvim-tree/log.lua b/lua/nvim-tree/log.lua index 9665c1335e6..4aa8e9e1849 100644 --- a/lua/nvim-tree/log.lua +++ b/lua/nvim-tree/log.lua @@ -1,4 +1,4 @@ ----@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "profile" | "watcher" +---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "lifecycle" | "profile" | "watcher" ---@type table local types = {} diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index c940f999983..3771520c4a8 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -25,6 +25,8 @@ local Marks = Class:extend() ---@protected ---@param args MarksArgs function Marks:new(args) + args.explorer:log_lifecycle("Marks:new") + self.explorer = args.explorer self.marks = {} diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 66b2098c191..ff66f041a19 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -25,6 +25,8 @@ local Renderer = Class:extend() ---@protected ---@param args RendererArgs function Renderer:new(args) + args.explorer:log_lifecycle("Renderer:new") + self.explorer = args.explorer end From e0c7118864e405173559d2f81d42d43073114b30 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 16 Jun 2025 13:00:33 +1000 Subject: [PATCH 14/32] refactor(#2826): move all winhl to appearance constants --- lua/nvim-tree/appearance/init.lua | 23 +++++++++++++++++++ lua/nvim-tree/explorer/view.lua | 18 +++------------ lua/nvim-tree/help.lua | 9 ++------ .../renderer/components/full-name.lua | 8 +++---- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/lua/nvim-tree/appearance/init.lua b/lua/nvim-tree/appearance/init.lua index 61714af644e..e3e3ab26690 100644 --- a/lua/nvim-tree/appearance/init.lua +++ b/lua/nvim-tree/appearance/init.lua @@ -134,6 +134,29 @@ M.HIGHLIGHT_GROUPS = { { group = "NvimTreeDiagnosticHintFolderHL", link = "NvimTreeDiagnosticHintFileHL" }, } +-- winhighlight for most cases +M.WIN_HL = table.concat({ + "EndOfBuffer:NvimTreeEndOfBuffer", + "CursorLine:NvimTreeCursorLine", + "CursorLineNr:NvimTreeCursorLineNr", + "LineNr:NvimTreeLineNr", + "WinSeparator:NvimTreeWinSeparator", + "StatusLine:NvimTreeStatusLine", + "StatusLineNC:NvimTreeStatuslineNC", + "SignColumn:NvimTreeSignColumn", + "Normal:NvimTreeNormal", + "NormalNC:NvimTreeNormalNC", + "NormalFloat:NvimTreeNormalFloat", + "FloatBorder:NvimTreeNormalFloatBorder", +}, ",") + +-- winhighlight for help +M.WIN_HL_HELP = table.concat({ + "NormalFloat:NvimTreeNormalFloat", + "WinSeparator:NvimTreeWinSeparator", + "CursorLine:NvimTreeCursorLine", +}, ",") + -- nvim-tree highlight groups to legacy M.LEGACY_LINKS = { NvimTreeModifiedIcon = "NvimTreeModifiedFile", diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index cbe4ebb84b4..ec7ea2d7ed5 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -1,3 +1,4 @@ +local appearance = require("nvim-tree.appearance") local events = require("nvim-tree.events") local utils = require("nvim-tree.utils") local log = require("nvim-tree.log") @@ -78,20 +79,7 @@ function View:new(args) cursorlineopt = "both", colorcolumn = "0", wrap = false, - winhl = table.concat({ - "EndOfBuffer:NvimTreeEndOfBuffer", - "CursorLine:NvimTreeCursorLine", - "CursorLineNr:NvimTreeCursorLineNr", - "LineNr:NvimTreeLineNr", - "WinSeparator:NvimTreeWinSeparator", - "StatusLine:NvimTreeStatusLine", - "StatusLineNC:NvimTreeStatuslineNC", - "SignColumn:NvimTreeSignColumn", - "Normal:NvimTreeNormal", - "NormalNC:NvimTreeNormalNC", - "NormalFloat:NvimTreeNormalFloat", - "FloatBorder:NvimTreeNormalFloatBorder", - }, ","), + winhl = appearance.WIN_HL, } self:configure_width(self.explorer.opts.view.width) @@ -640,7 +628,7 @@ end function View:reset_winhl() local winnr = self:get_winnr() if winnr and vim.api.nvim_win_is_valid(winnr) then - vim.wo[self:get_winnr()].winhl = self.winopts.winhl + vim.wo[self:get_winnr()].winhl = appearance.WIN_HL end end diff --git a/lua/nvim-tree/help.lua b/lua/nvim-tree/help.lua index a5a0dbd6b4a..34f6db2d836 100644 --- a/lua/nvim-tree/help.lua +++ b/lua/nvim-tree/help.lua @@ -1,3 +1,4 @@ +local appearance = require("nvim-tree.appearance") local keymap = require("nvim-tree.keymap") local api = {} -- circular dependency @@ -5,12 +6,6 @@ local PAT_MOUSE = "^<.*Mouse" local PAT_CTRL = "^ Date: Mon, 16 Jun 2025 13:55:35 +1000 Subject: [PATCH 15/32] refactor(#2826): add lifecycle logging to all Explorer members --- doc/nvim-tree-lua.txt | 1 + lua/nvim-tree/actions/fs/clipboard.lua | 6 +++- lua/nvim-tree/explorer/filters.lua | 6 +++- lua/nvim-tree/explorer/init.lua | 40 +++++++++++++++++--------- lua/nvim-tree/explorer/live-filter.lua | 6 +++- lua/nvim-tree/explorer/sorter.lua | 6 +++- lua/nvim-tree/explorer/view.lua | 6 +++- lua/nvim-tree/marks/init.lua | 6 +++- lua/nvim-tree/renderer/init.lua | 6 +++- 9 files changed, 63 insertions(+), 20 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 55fa98b61fb..59d1bdcaff6 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -649,6 +649,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua dev = false, diagnostics = false, git = false, + lifecycle = true, profile = false, watcher = false, }, diff --git a/lua/nvim-tree/actions/fs/clipboard.lua b/lua/nvim-tree/actions/fs/clipboard.lua index a8c6e15e958..aa9ae2c8664 100644 --- a/lua/nvim-tree/actions/fs/clipboard.lua +++ b/lua/nvim-tree/actions/fs/clipboard.lua @@ -31,7 +31,7 @@ local Clipboard = Class:extend() ---@protected ---@param args ClipboardArgs function Clipboard:new(args) - args.explorer:log_lifecycle("Clipboard:new") + args.explorer:log_new("Clipboard") self.explorer = args.explorer @@ -44,6 +44,10 @@ function Clipboard:new(args) self.reg = self.explorer.opts.actions.use_system_clipboard and "+" or "1" end +function Clipboard:destroy() + self.explorer:log_destroy("Clipboard") +end + ---@param source string ---@param destination string ---@return boolean diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index c6640fb42ee..6066e8a32bc 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -23,7 +23,7 @@ local Filters = Class:extend() ---@protected ---@param args FiltersArgs function Filters:new(args) - args.explorer:log_lifecycle("Filters:new") + args.explorer:log_new("Filters") self.explorer = args.explorer self.ignore_list = {} @@ -52,6 +52,10 @@ function Filters:new(args) end end +function Filters:destroy() + self.explorer:log_destroy("Filters") +end + ---@private ---@param path string ---@return boolean diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 71b7b4e1780..398084e5327 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -56,18 +56,18 @@ function Explorer:new(args) self.uid_explorer = vim.loop.hrtime() self.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. self.uid_explorer, {}) - self:log_lifecycle("Explorer:new") + self:log_new("Explorer") - self.open = true - self.opts = config + self.open = true + self.opts = config - self.sorters = Sorter({ explorer = self }) - self.renderer = Renderer({ explorer = self }) - self.filters = Filters({ explorer = self }) - self.live_filter = LiveFilter({ explorer = self }) - self.marks = Marks({ explorer = self }) - self.clipboard = Clipboard({ explorer = self }) - self.view = View({ explorer = self }) + self.sorters = Sorter({ explorer = self }) + self.renderer = Renderer({ explorer = self }) + self.filters = Filters({ explorer = self }) + self.live_filter = LiveFilter({ explorer = self }) + self.marks = Marks({ explorer = self }) + self.clipboard = Clipboard({ explorer = self }) + self.view = View({ explorer = self }) self:create_autocmds() @@ -75,7 +75,15 @@ function Explorer:new(args) end function Explorer:destroy() - self:log_lifecycle("Explorer:des") + self.explorer:log_destroy("Explorer") + + self.clipboard:destroy() + self.filters:destroy() + self.live_filter:destroy() + self.marks:destroy() + self.renderer:destroy() + self.sorters:destroy() + self.window:destroy() vim.api.nvim_del_augroup_by_id(self.augroup_id) @@ -591,8 +599,14 @@ end ---Log a lifecycle message with uid_explorer and absolute_path ---@param msg string? -function Explorer:log_lifecycle(msg) - log.line("lifecycle", "%-15s %d %s", msg, self.uid_explorer, self.absolute_path) +function Explorer:log_new(msg) + log.line("lifecycle", "+ %-15s %d %s", msg, self.uid_explorer, self.absolute_path) +end + +---Log a lifecycle message with uid_explorer and absolute_path +---@param msg string? +function Explorer:log_destroy(msg) + log.line("lifecycle", "- %-15s %d %s", msg, self.uid_explorer, self.absolute_path) end function Explorer:setup(opts) diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 704945e1dc3..9bd24406964 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -20,7 +20,7 @@ local LiveFilter = Class:extend() ---@protected ---@param args LiveFilterArgs function LiveFilter:new(args) - args.explorer:log_lifecycle("LiveFilter:new") + args.explorer:log_new("LiveFilter") self.explorer = args.explorer self.prefix = self.explorer.opts.live_filter.prefix @@ -28,6 +28,10 @@ function LiveFilter:new(args) self.filter = nil end +function LiveFilter:destroy() + self.explorer:log_destroy("LiveFilter") +end + ---@param node_ Node? local function reset_filter(self, node_) node_ = node_ or self.explorer diff --git a/lua/nvim-tree/explorer/sorter.lua b/lua/nvim-tree/explorer/sorter.lua index 3fd475e3f43..4e69d36eac6 100644 --- a/lua/nvim-tree/explorer/sorter.lua +++ b/lua/nvim-tree/explorer/sorter.lua @@ -19,11 +19,15 @@ local Sorter = Class:extend() ---@protected ---@param args SorterArgs function Sorter:new(args) - args.explorer:log_lifecycle("Sorter:new") + args.explorer:log_new("Sorter") self.explorer = args.explorer end +function Sorter:destroy() + self.explorer:log_destroy("Sorter") +end + ---Create a shallow copy of a portion of a list. ---@param t table ---@param first integer First index, inclusive diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index ec7ea2d7ed5..12406726a8e 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -50,7 +50,7 @@ local View = Class:extend() ---@protected ---@param args ViewArgs function View:new(args) - args.explorer:log_lifecycle("View:new") + args.explorer:log_new("View") self.explorer = args.explorer self.adaptive_size = false @@ -86,6 +86,10 @@ function View:new(args) self.initial_width = self:get_width() end +function View:destroy() + self.explorer:log_destroy("View") +end + -- The initial state of a tab local tabinitial = { -- The position of the cursor { line, column } diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index 3771520c4a8..e0fe9a3086e 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -25,13 +25,17 @@ local Marks = Class:extend() ---@protected ---@param args MarksArgs function Marks:new(args) - args.explorer:log_lifecycle("Marks:new") + args.explorer:log_new("Marks") self.explorer = args.explorer self.marks = {} end +function Marks:destroy() + self.explorer:log_destroy("Marks") +end + ---Clear all marks and reload if watchers disabled ---@private function Marks:clear_reload() diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index ff66f041a19..7364fc381c1 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -25,11 +25,15 @@ local Renderer = Class:extend() ---@protected ---@param args RendererArgs function Renderer:new(args) - args.explorer:log_lifecycle("Renderer:new") + args.explorer:log_new("Renderer") self.explorer = args.explorer end +function Renderer:destroy() + self.explorer:log_destroy("Renderer") +end + ---@private ---@param bufnr number ---@param lines string[] From 11c9d0c37ab968ace062f0e8f8617488fb7cdce4 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Wed, 18 Jun 2025 13:40:11 +1000 Subject: [PATCH 16/32] refactor(#2826): fix bad cherry-pick --- lua/nvim-tree/explorer/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 398084e5327..b1177ef2fc2 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -83,7 +83,7 @@ function Explorer:destroy() self.marks:destroy() self.renderer:destroy() self.sorters:destroy() - self.window:destroy() + self.view:destroy() vim.api.nvim_del_augroup_by_id(self.augroup_id) From ca3a817676c9fa2cb886cbf3cfc2fd23be84f1e3 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Tue, 17 Jun 2025 13:11:04 +1000 Subject: [PATCH 17/32] refactor(#2826): better enumerate_options function --- lua/nvim-tree/utils.lua | 43 +++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index e33aaaf1091..77c39e6d4ca 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -661,32 +661,29 @@ function M.is_executable(absolute_path) end end ----List of all option info/values ----@param opts vim.api.keyset.option passed directly to vim.api.nvim_get_option_info2 and vim.api.nvim_get_option_value ----@param was_set boolean filter was_set ----@return { info: vim.api.keyset.get_option_info, val: any }[] -function M.enumerate_options(opts, was_set) - local res = {} - - local infos = vim.tbl_filter(function(info) - if opts.buf and info.scope ~= "buf" then - return false - elseif opts.win and info.scope ~= "win" then - return false +---@class UtilEnumerateOptionsOpts +---@field keyset_opts vim.api.keyset.option +---@field was_set boolean? as per vim.api.keyset.get_option_info + +---Option name/values +---@param opts UtilEnumerateOptionsOpts +---@return table +function M.enumerate_options(opts) + -- enumerate all options, limiting buf and win scopes + return vim.tbl_map(function(info) + if opts.keyset_opts.buf and info.scope ~= "buf" then + return nil + elseif opts.keyset_opts.win and info.scope ~= "win" then + return nil else - return true + -- optional, lazy was_set check + if not opts.was_set or vim.api.nvim_get_option_info2(info.name, opts.keyset_opts).was_set then + return vim.api.nvim_get_option_value(info.name, opts.keyset_opts) + else + return nil + end end end, vim.api.nvim_get_all_options_info()) - - for _, info in vim.spairs(infos) do - local _, info2 = pcall(vim.api.nvim_get_option_info2, info.name, opts) - if not was_set or info2.was_set then - local val = pcall(vim.api.nvim_get_option_value, info.name, opts) - table.insert(res, { info = info2, val = val }) - end - end - - return res end return M From 5db04afa0a7474a31d4de45d36f310c531dece20 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Wed, 18 Jun 2025 13:50:44 +1000 Subject: [PATCH 18/32] refactor(#2826): add View.tab_line for debugging --- lua/nvim-tree/explorer/view.lua | 99 +++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 12406726a8e..b98bce8a5a8 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -39,6 +39,7 @@ M.View = { ---@field private width (fun():integer)|integer|string ---@field private max_width integer ---@field private padding integer +---@field tab_line fun():string local View = Class:extend() ---@class View @@ -664,4 +665,102 @@ function View:configure_width(width) end end +--- Debugging only. +--- Tabs show TABPAGES winnr and BUFNR_PER_TAB bufnr for the tab. +--- Orphans for inexistent tab_ids are shown at the right. +--- lib.target_winid is always shown at the right next to a close button. +--- Enable with: +--- vim.opt.tabline = "%!v:lua.require('nvim-tree.explorer.view').tab_line()" +--- vim.opt.showtabline = 2 +---@return string +function View.tab_line() + local tab_ids = vim.api.nvim_list_tabpages() + local cur_tab_id = vim.api.nvim_get_current_tabpage() + + local bufnr_per_tab = vim.deepcopy(BUFNR_PER_TAB) + local tabpages = vim.deepcopy(M.View.tabpages) + + local tl = "%#TabLine#" + + for i, tab_id in ipairs(tab_ids) do + -- click to select + tl = tl .. "%" .. i .. "T" + + -- style + if tab_id == cur_tab_id then + tl = tl .. "%#StatusLine#|" + else + tl = tl .. "|%#TabLine#" + end + + -- tab_id itself + tl = tl .. " t" .. tab_id + + -- winnr, if present + local tp = M.View.tabpages[tab_id] + if tp then + tl = tl .. " w" .. tp.winnr + else + tl = tl .. " " + end + + -- bufnr, if present + local bpt = BUFNR_PER_TAB[tab_id] + if bpt then + tl = tl .. " b" .. bpt + else + tl = tl .. " " + end + + tl = tl .. " " + + -- remove actively mapped + bufnr_per_tab[tab_id] = nil + tabpages[tab_id] = nil + end + + -- close last and reset + tl = tl .. "|%#CursorLine#%T" + + -- collect orphans + local orphans = {} + for tab_id, bufnr in pairs(bufnr_per_tab) do + orphans[tab_id] = orphans[tab_id] or {} + orphans[tab_id].bufnr = bufnr + end + for tab_id, tp in pairs(tabpages) do + orphans[tab_id] = orphans[tab_id] or {} + orphans[tab_id].winnr = tp.winnr + end + + -- right-align + tl = tl .. "%=%#TabLine#" + + -- print orphans + for tab_id, orphan in pairs(orphans) do + -- inexistent tab + tl = tl .. "%#error#| t" .. tab_id + + -- maybe winnr + if orphan.winnr then + tl = tl .. " w" .. orphan.winnr + else + tl = tl .. " " + end + + -- maybe bufnr + if orphan.bufnr then + tl = tl .. " b" .. orphan.bufnr + else + tl = tl .. " " + end + tl = tl .. " " + end + + -- target win id and close button + tl = tl .. "|%#TabLine# twi" .. (require("nvim-tree.lib").target_winid or "?") .. " %999X| X |" + + return tl +end + return View From 8e667335519cfb45cbcc57ec51c974f072f13115 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Tue, 17 Jun 2025 17:07:03 +1000 Subject: [PATCH 19/32] refactor(#2826): default lifecycle log off --- doc/nvim-tree-lua.txt | 2 +- lua/nvim-tree.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 59d1bdcaff6..4859ad8700e 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -649,7 +649,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua dev = false, diagnostics = false, git = false, - lifecycle = true, + lifecycle = false, profile = false, watcher = false, }, diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 449ac2b7eea..72e7ec75923 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -496,7 +496,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS dev = false, diagnostics = false, git = false, - lifecycle = true, + lifecycle = false, profile = false, watcher = false, }, From 127e0603a750af8163a25d36d0388e4a370a848a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Wed, 18 Jun 2025 14:31:21 +1000 Subject: [PATCH 20/32] refactor(#2826): add experimental.multi_instance_debug, split globals out of view, move diagnostics to its own module --- doc/nvim-tree-lua.txt | 11 +- lua/nvim-tree.lua | 3 +- lua/nvim-tree/explorer/init.lua | 4 +- lua/nvim-tree/explorer/view.lua | 154 ++++--------------------- lua/nvim-tree/globals.lua | 10 ++ lua/nvim-tree/log.lua | 2 +- lua/nvim-tree/multi-instance-debug.lua | 112 ++++++++++++++++++ 7 files changed, 149 insertions(+), 147 deletions(-) create mode 100644 lua/nvim-tree/globals.lua create mode 100644 lua/nvim-tree/multi-instance-debug.lua diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 4859ad8700e..260a01fa258 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -638,6 +638,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua }, }, experimental = { + multi_instance_debug = false, }, log = { enable = false, @@ -649,7 +650,6 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua dev = false, diagnostics = false, git = false, - lifecycle = false, profile = false, watcher = false, }, @@ -1515,11 +1515,6 @@ Configuration options for opening a file from nvim-tree. Resizes the tree when opening a file. Type: `boolean`, Default: `true` - *nvim-tree.experimental.actions.open_file.relative_path* - Buffers opened by nvim-tree will use with relative paths instead of - absolute. - Type: `boolean`, Default: `true` - *nvim-tree.actions.open_file.window_picker* Window picker configuration. @@ -3009,9 +3004,6 @@ Windows WSL and PowerShell - Executable file detection is disabled as this is non-performant and can freeze nvim - Some filesystem watcher error related to permissions will not be reported -- Some users have reported unspecified issues with - |nvim-tree.experimental.actions.open_file.relative_path|. Please report any - issues or disable this feature. ============================================================================== 13. NETRW *nvim-tree-netrw* @@ -3146,7 +3138,6 @@ highlight group is not, hard linking as follows: > |nvim-tree.diagnostics.show_on_open_dirs| |nvim-tree.disable_netrw| |nvim-tree.experimental| -|nvim-tree.experimental.actions.open_file.relative_path| |nvim-tree.filesystem_watchers.debounce_delay| |nvim-tree.filesystem_watchers.enable| |nvim-tree.filesystem_watchers.ignore_dirs| diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 72e7ec75923..e59522bc1aa 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -485,6 +485,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS }, }, experimental = { + multi_instance_debug = false, }, log = { enable = false, @@ -496,7 +497,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS dev = false, diagnostics = false, git = false, - lifecycle = false, profile = false, watcher = false, }, @@ -726,6 +726,7 @@ function M.setup(conf) require("nvim-tree.buffers").setup(opts) require("nvim-tree.help").setup(opts) require("nvim-tree.watcher").setup(opts) + require("nvim-tree.multi-instance-debug").setup(opts) setup_autocommands(opts) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index b1177ef2fc2..e07ff99efe4 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -600,13 +600,13 @@ end ---Log a lifecycle message with uid_explorer and absolute_path ---@param msg string? function Explorer:log_new(msg) - log.line("lifecycle", "+ %-15s %d %s", msg, self.uid_explorer, self.absolute_path) + log.line("dev", "+ %-15s %d %s", msg, self.uid_explorer, self.absolute_path) end ---Log a lifecycle message with uid_explorer and absolute_path ---@param msg string? function Explorer:log_destroy(msg) - log.line("lifecycle", "- %-15s %d %s", msg, self.uid_explorer, self.absolute_path) + log.line("dev", "- %-15s %d %s", msg, self.uid_explorer, self.absolute_path) end function Explorer:setup(opts) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index b98bce8a5a8..1c76a7c8661 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -3,6 +3,7 @@ local events = require("nvim-tree.events") local utils = require("nvim-tree.utils") local log = require("nvim-tree.log") local notify = require("nvim-tree.notify") +local globals = require("nvim-tree.globals") local Class = require("nvim-tree.classic") @@ -11,18 +12,10 @@ local Class = require("nvim-tree.classic") ---@field resize boolean|nil default true ---@field winid number|nil 0 or nil for current -local M = {} - local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MAX_WIDTH = -1 local DEFAULT_PADDING = 1 --- TODO global, rework for multiinstance explorer --- M.View retained for simpler change history -M.View = { - tabpages = {} -} - ---@class (exact) View: Class ---@field live_filter table ---@field side string @@ -30,7 +23,6 @@ M.View = { ---@field private explorer Explorer ---@field private adaptive_size boolean ---@field private centralize_selection boolean ----@field private cursors table as per vim.api.nvim_win_get_cursor ---@field private hide_root_folder boolean ---@field private winopts table ---@field private height integer @@ -39,7 +31,6 @@ M.View = { ---@field private width (fun():integer)|integer|string ---@field private max_width integer ---@field private padding integer ----@field tab_line fun():string local View = Class:extend() ---@class View @@ -56,7 +47,6 @@ function View:new(args) self.explorer = args.explorer self.adaptive_size = false self.centralize_selection = self.explorer.opts.view.centralize_selection - self.cursors = {} self.float = self.explorer.opts.view.float self.height = self.explorer.opts.view.height self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false @@ -99,10 +89,6 @@ local tabinitial = { winnr = nil, } --- TODO global, rework for multiinstance explorer ----@type table -local BUFNR_PER_TAB = {} - ---@type { name: string, value: any }[] local BUFFER_OPTIONS = { { name = "bufhidden", value = "wipe" }, @@ -117,7 +103,7 @@ local BUFFER_OPTIONS = { ---@param bufnr integer ---@return boolean function View:matches_bufnr(bufnr) - for _, b in pairs(BUFNR_PER_TAB) do + for _, b in pairs(globals.BUFNR_PER_TAB) do if b == bufnr then return true end @@ -140,7 +126,7 @@ function View:create_buffer(bufnr) self:wipe_rogue_buffer() local tab = vim.api.nvim_get_current_tabpage() - BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) + globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab) bufnr = self:get_bufnr() @@ -187,7 +173,7 @@ local move_tbl = { ---@param tabpage integer function View:setup_tabpage(tabpage) local winnr = vim.api.nvim_get_current_win() - M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr }) + globals.TABPAGES[tabpage] = vim.tbl_extend("force", globals.TABPAGES[tabpage] or tabinitial, { winnr = winnr }) end ---@private @@ -275,7 +261,7 @@ end ---@param tabnr integer function View:save_tab_state(tabnr) local tabpage = tabnr or vim.api.nvim_get_current_tabpage() - self.cursors[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0) + globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0) end ---@private @@ -311,7 +297,7 @@ function View:close_this_tab_only() end function View:close_all_tabs() - for tabpage, _ in pairs(M.View.tabpages) do + for tabpage, _ in pairs(globals.TABPAGES) do self:close_internal(tabpage) end end @@ -450,7 +436,7 @@ end ---@private function View:set_current_win() local current_tab = vim.api.nvim_get_current_tabpage() - M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() + globals.TABPAGES[current_tab].winnr = vim.api.nvim_get_current_win() end ---Open the tree in the a window @@ -474,17 +460,17 @@ end function View:abandon_current_window() local tab = vim.api.nvim_get_current_tabpage() - BUFNR_PER_TAB[tab] = nil - if M.View.tabpages[tab] then - M.View.tabpages[tab].winnr = nil + globals.BUFNR_PER_TAB[tab] = nil + if globals.TABPAGES[tab] then + globals.TABPAGES[tab].winnr = nil end end function View:abandon_all_windows() for tab, _ in pairs(vim.api.nvim_list_tabpages()) do - BUFNR_PER_TAB[tab] = nil - if M.View.tabpages[tab] then - M.View.tabpages[tab].winnr = nil + globals.BUFNR_PER_TAB[tab] = nil + if globals.TABPAGES[tab] then + globals.TABPAGES[tab].winnr = nil end end end @@ -493,15 +479,15 @@ end ---@return boolean function View:is_visible(opts) if opts and opts.tabpage then - if M.View.tabpages[opts.tabpage] == nil then + if globals.TABPAGES[opts.tabpage] == nil then return false end - local winnr = M.View.tabpages[opts.tabpage].winnr + local winnr = globals.TABPAGES[opts.tabpage].winnr return winnr and vim.api.nvim_win_is_valid(winnr) end if opts and opts.any_tabpage then - for _, v in pairs(M.View.tabpages) do + for _, v in pairs(globals.TABPAGES) do if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then return true end @@ -555,7 +541,7 @@ end --- Restores the state of a NvimTree window if it was initialized before. function View:restore_tab_state() local tabpage = vim.api.nvim_get_current_tabpage() - self:set_cursor(self.cursors[tabpage]) + self:set_cursor(globals.CURSORS[tabpage]) end --- Returns the window number for nvim-tree within the tabpage specified @@ -563,7 +549,7 @@ end ---@return number|nil function View:get_winnr(tabpage) tabpage = tabpage or vim.api.nvim_get_current_tabpage() - local tabinfo = M.View.tabpages[tabpage] + local tabinfo = globals.TABPAGES[tabpage] if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then return tabinfo.winnr end @@ -572,7 +558,7 @@ end --- Returns the current nvim tree bufnr ---@return number function View:get_bufnr() - return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()] + return globals.BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()] end function View:prevent_buffer_override() @@ -589,9 +575,9 @@ function View:prevent_buffer_override() local bufname = vim.api.nvim_buf_get_name(curbuf) if not bufname:match("NvimTree") then - for i, tabpage in ipairs(M.View.tabpages) do + for i, tabpage in ipairs(globals.TABPAGES) do if tabpage.winnr == view_winnr then - M.View.tabpages[i] = nil + globals.TABPAGES[i] = nil break end end @@ -665,102 +651,4 @@ function View:configure_width(width) end end ---- Debugging only. ---- Tabs show TABPAGES winnr and BUFNR_PER_TAB bufnr for the tab. ---- Orphans for inexistent tab_ids are shown at the right. ---- lib.target_winid is always shown at the right next to a close button. ---- Enable with: ---- vim.opt.tabline = "%!v:lua.require('nvim-tree.explorer.view').tab_line()" ---- vim.opt.showtabline = 2 ----@return string -function View.tab_line() - local tab_ids = vim.api.nvim_list_tabpages() - local cur_tab_id = vim.api.nvim_get_current_tabpage() - - local bufnr_per_tab = vim.deepcopy(BUFNR_PER_TAB) - local tabpages = vim.deepcopy(M.View.tabpages) - - local tl = "%#TabLine#" - - for i, tab_id in ipairs(tab_ids) do - -- click to select - tl = tl .. "%" .. i .. "T" - - -- style - if tab_id == cur_tab_id then - tl = tl .. "%#StatusLine#|" - else - tl = tl .. "|%#TabLine#" - end - - -- tab_id itself - tl = tl .. " t" .. tab_id - - -- winnr, if present - local tp = M.View.tabpages[tab_id] - if tp then - tl = tl .. " w" .. tp.winnr - else - tl = tl .. " " - end - - -- bufnr, if present - local bpt = BUFNR_PER_TAB[tab_id] - if bpt then - tl = tl .. " b" .. bpt - else - tl = tl .. " " - end - - tl = tl .. " " - - -- remove actively mapped - bufnr_per_tab[tab_id] = nil - tabpages[tab_id] = nil - end - - -- close last and reset - tl = tl .. "|%#CursorLine#%T" - - -- collect orphans - local orphans = {} - for tab_id, bufnr in pairs(bufnr_per_tab) do - orphans[tab_id] = orphans[tab_id] or {} - orphans[tab_id].bufnr = bufnr - end - for tab_id, tp in pairs(tabpages) do - orphans[tab_id] = orphans[tab_id] or {} - orphans[tab_id].winnr = tp.winnr - end - - -- right-align - tl = tl .. "%=%#TabLine#" - - -- print orphans - for tab_id, orphan in pairs(orphans) do - -- inexistent tab - tl = tl .. "%#error#| t" .. tab_id - - -- maybe winnr - if orphan.winnr then - tl = tl .. " w" .. orphan.winnr - else - tl = tl .. " " - end - - -- maybe bufnr - if orphan.bufnr then - tl = tl .. " b" .. orphan.bufnr - else - tl = tl .. " " - end - tl = tl .. " " - end - - -- target win id and close button - tl = tl .. "|%#TabLine# twi" .. (require("nvim-tree.lib").target_winid or "?") .. " %999X| X |" - - return tl -end - return View diff --git a/lua/nvim-tree/globals.lua b/lua/nvim-tree/globals.lua new file mode 100644 index 00000000000..58e3452f7ee --- /dev/null +++ b/lua/nvim-tree/globals.lua @@ -0,0 +1,10 @@ +-- global state, to be refactored away during multi-instance + +local M = { + -- from View + TABPAGES = {}, + BUFNR_PER_TAB = {}, + CURSORS = {}, +} + +return M diff --git a/lua/nvim-tree/log.lua b/lua/nvim-tree/log.lua index 4aa8e9e1849..36a009e1326 100644 --- a/lua/nvim-tree/log.lua +++ b/lua/nvim-tree/log.lua @@ -1,4 +1,4 @@ ----@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "lifecycle" | "profile" | "watcher" +---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "profile" | "watcher" ---@type table local types = {} diff --git a/lua/nvim-tree/multi-instance-debug.lua b/lua/nvim-tree/multi-instance-debug.lua new file mode 100644 index 00000000000..58ac13734de --- /dev/null +++ b/lua/nvim-tree/multi-instance-debug.lua @@ -0,0 +1,112 @@ +local globals = require("nvim-tree.globals") + +local M = {} + +--- Debugging only. +--- Tabs show TABPAGES winnr and BUFNR_PER_TAB bufnr for the tab. +--- Orphans for inexistent tab_ids are shown at the right. +--- lib.target_winid is always shown at the right next to a close button. +--- Enable with: +--- vim.opt.tabline = "%!v:lua.require('nvim-tree.explorer.view').tab_line()" +--- vim.opt.showtabline = 2 +---@return string +function M.tab_line() + local tab_ids = vim.api.nvim_list_tabpages() + local cur_tab_id = vim.api.nvim_get_current_tabpage() + + local bufnr_per_tab = vim.deepcopy(globals.BUFNR_PER_TAB) + local tabpages = vim.deepcopy(globals.TABPAGES) + + local tl = "%#TabLine#" + + for i, tab_id in ipairs(tab_ids) do + -- click to select + tl = tl .. "%" .. i .. "T" + + -- style + if tab_id == cur_tab_id then + tl = tl .. "%#StatusLine#|" + else + tl = tl .. "|%#TabLine#" + end + + -- tab_id itself + tl = tl .. " t" .. tab_id + + -- winnr, if present + local tp = globals.TABPAGES[tab_id] + if tp then + tl = tl .. " w" .. tp.winnr + else + tl = tl .. " " + end + + -- bufnr, if present + local bpt = globals.BUFNR_PER_TAB[tab_id] + if bpt then + tl = tl .. " b" .. bpt + else + tl = tl .. " " + end + + tl = tl .. " " + + -- remove actively mapped + bufnr_per_tab[tab_id] = nil + tabpages[tab_id] = nil + end + + -- close last and reset + tl = tl .. "|%#CursorLine#%T" + + -- collect orphans + local orphans = {} + for tab_id, bufnr in pairs(bufnr_per_tab) do + orphans[tab_id] = orphans[tab_id] or {} + orphans[tab_id].bufnr = bufnr + end + for tab_id, tp in pairs(tabpages) do + orphans[tab_id] = orphans[tab_id] or {} + orphans[tab_id].winnr = tp.winnr + end + + -- right-align + tl = tl .. "%=%#TabLine#" + + -- print orphans + for tab_id, orphan in pairs(orphans) do + -- inexistent tab + tl = tl .. "%#error#| t" .. tab_id + + -- maybe winnr + if orphan.winnr then + tl = tl .. " w" .. orphan.winnr + else + tl = tl .. " " + end + + -- maybe bufnr + if orphan.bufnr then + tl = tl .. " b" .. orphan.bufnr + else + tl = tl .. " " + end + tl = tl .. " " + end + + -- target win id and close button + tl = tl .. "|%#TabLine# twi" .. (require("nvim-tree.lib").target_winid or "?") .. " %999X| X |" + + return tl +end + +function M.setup(opts) + if not opts.experimental.multi_instance_debug then + return + end + + vim.opt.tabline = "%!v:lua.require('nvim-tree.multi-instance-debug').tab_line()" + vim.opt.showtabline = 2 +end + +return M From cdc43952deb7074807ec1739d51339b8dcd44670 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Wed, 18 Jun 2025 15:22:36 +1000 Subject: [PATCH 21/32] refactor(#2826): instrument View:get_winnr --- lua/nvim-tree/actions/node/open-file.lua | 2 +- lua/nvim-tree/explorer/init.lua | 2 +- lua/nvim-tree/explorer/view.lua | 59 +++++++++++++++++------- lua/nvim-tree/lib.lua | 2 +- lua/nvim-tree/renderer/init.lua | 6 +-- 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 99594520f11..317d3d46ac7 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -22,7 +22,7 @@ local function usable_win_ids() local explorer = core.get_explorer() local tabpage = vim.api.nvim_get_current_tabpage() local win_ids = vim.api.nvim_tabpage_list_wins(tabpage) - local tree_winid = explorer and explorer.view:get_winnr(tabpage) + local tree_winid = explorer and explorer.view:get_winnr(tabpage, "open-file.usable_win_ids") return vim.tbl_filter(function(id) local bufid = vim.api.nvim_win_get_buf(id) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index e07ff99efe4..76d568b2ae2 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -548,7 +548,7 @@ end ---nil on no explorer or invalid view win ---@return integer[]|nil function Explorer:get_cursor_position() - local winnr = self.view:get_winnr() + local winnr = self.view:get_winnr(nil, "Explorer:get_cursor_position") if not winnr or not vim.api.nvim_win_is_valid(winnr) then return end diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 1c76a7c8661..3d1f8914d40 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -261,7 +261,7 @@ end ---@param tabnr integer function View:save_tab_state(tabnr) local tabpage = tabnr or vim.api.nvim_get_current_tabpage() - globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0) + globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage, "View:save_tab_state") or 0) end ---@private @@ -272,7 +272,7 @@ function View:close_internal(tabpage) end self:save_tab_state(tabpage) switch_buf_if_last_buf() - local tree_win = self:get_winnr(tabpage) + local tree_win = self:get_winnr(tabpage, "View:close_internal") local current_win = vim.api.nvim_get_current_win() for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do if vim.api.nvim_win_get_config(win).relative == "" then @@ -343,7 +343,7 @@ function View:grow() local padding = self:get_size(self.padding) -- account for sign/number columns etc. - local wininfo = vim.fn.getwininfo(self:get_winnr()) + local wininfo = vim.fn.getwininfo(self:get_winnr(nil, "View:grow")) if type(wininfo) == "table" and type(wininfo[1]) == "table" then padding = padding + wininfo[1].textoff end @@ -412,7 +412,7 @@ function View:resize(size) return end - local winnr = self:get_winnr() or 0 + local winnr = self:get_winnr(nil, "View:resize") or 0 local new_size = self:get_width() @@ -495,25 +495,25 @@ function View:is_visible(opts) return false end - return self:get_winnr() ~= nil and vim.api.nvim_win_is_valid(self:get_winnr() or 0) + 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) end ---@param opts table|nil function View:set_cursor(opts) if self:is_visible() then - pcall(vim.api.nvim_win_set_cursor, self:get_winnr(), opts) + pcall(vim.api.nvim_win_set_cursor, self:get_winnr(nil, "View:set_cursor"), opts) end end ---@param winnr number|nil ---@param open_if_closed boolean|nil function View:focus(winnr, open_if_closed) - local wnr = winnr or self:get_winnr() + local wnr = winnr or self:get_winnr(nil, "View:focus1") if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then self:close() self:open() - wnr = self:get_winnr() + wnr = self:get_winnr(nil, "View:focus2") elseif open_if_closed and not self:is_visible() then self:open() end @@ -532,7 +532,7 @@ function View:winid(opts) tabpage = vim.api.nvim_get_current_tabpage() end if self:is_visible({ tabpage = tabpage }) then - return self:get_winnr(tabpage) + return self:get_winnr(tabpage, "View:winid") else return nil end @@ -546,12 +546,37 @@ end --- 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 callsite string? for logging purposes ---@return number|nil -function View:get_winnr(tabpage) - tabpage = tabpage or vim.api.nvim_get_current_tabpage() - local tabinfo = globals.TABPAGES[tabpage] - if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then - return tabinfo.winnr +function View:get_winnr(tabpage, callsite) + if self.explorer.opts.experimental.multi_instance_debug then + local msg = string.format("View:get_winnr(%3s, %-20.20s)", tabpage, callsite) + + tabpage = tabpage or vim.api.nvim_get_current_tabpage() + local tabinfo = globals.TABPAGES[tabpage] + + local ret = nil + + if not tabinfo then + msg = string.format("%s t%d no tabinfo", msg, tabpage) + elseif not tabinfo.winnr then + msg = string.format("%s t%d no tabinfo.winnr", msg, tabpage) + elseif not vim.api.nvim_win_is_valid(tabinfo.winnr) then + msg = string.format("%s t%d invalid tabinfo.winnr %d", msg, tabpage, tabinfo.winnr) + else + msg = string.format("%s t%d w%d", msg, tabpage, tabinfo.winnr) + ret = tabinfo.winnr + end + + log.line("dev", "%s", msg) + + return ret + else + tabpage = tabpage or vim.api.nvim_get_current_tabpage() + local tabinfo = globals.TABPAGES[tabpage] + if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then + return tabinfo.winnr + end end end @@ -562,7 +587,7 @@ function View:get_bufnr() end function View:prevent_buffer_override() - local view_winnr = self:get_winnr() + local view_winnr = self:get_winnr(nil, "View:get_bufnr") local view_bufnr = self:get_bufnr() -- need to schedule to let the new buffer populate the window @@ -617,9 +642,9 @@ end -- used on ColorScheme event function View:reset_winhl() - local winnr = self:get_winnr() + local winnr = self:get_winnr(nil, "View:reset_winhl1") if winnr and vim.api.nvim_win_is_valid(winnr) then - vim.wo[self:get_winnr()].winhl = appearance.WIN_HL + vim.wo[self:get_winnr(nil, "View:reset_winhl2")].winhl = appearance.WIN_HL end end diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 4793075853b..b4733624d64 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -15,7 +15,7 @@ function M.set_target_win() local id = vim.api.nvim_get_current_win() - if explorer and id == explorer.view:get_winnr() then + if explorer and id == explorer.view:get_winnr(nil, "lib.set_target_win") then M.target_winid = 0 return end diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 7364fc381c1..46bde0aee65 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -108,21 +108,21 @@ function Renderer:draw() local profile = log.profile_start("draw") - local cursor = vim.api.nvim_win_get_cursor(self.explorer.view:get_winnr() or 0) + local cursor = vim.api.nvim_win_get_cursor(self.explorer.view:get_winnr(nil, "Renderer:draw1") or 0) local builder = Builder(self.explorer):build() self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines) if cursor and #builder.lines >= cursor[1] then - vim.api.nvim_win_set_cursor(self.explorer.view:get_winnr() or 0, cursor) + vim.api.nvim_win_set_cursor(self.explorer.view:get_winnr(nil, "Renderer:draw2") or 0, cursor) end self.explorer.view:grow_from_content() log.profile_end(profile) - events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr()) + events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr(nil, "Renderer:draw3")) end return Renderer From 83859531202e2810b229ed47a1172dc58c564b0c Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Wed, 18 Jun 2025 15:42:25 +1000 Subject: [PATCH 22/32] refactor(#2826): instrument View:setup_tabpage --- lua/nvim-tree/explorer/view.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 3d1f8914d40..d8c6404b5a5 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -171,8 +171,21 @@ local move_tbl = { -- setup_tabpage sets up the initial state of a tab ---@private ---@param tabpage integer -function View:setup_tabpage(tabpage) +---@param callsite string? +function View:setup_tabpage(tabpage, callsite) local winnr = vim.api.nvim_get_current_win() + + local msg = nil + if self.explorer.opts.experimental.multi_instance_debug then + msg = string.format( "View:setup_tabpage(%3s, %-20.20s) w%d", tabpage, callsite, winnr) + if globals.TABPAGES[tabpage] then + msg = string.format("%s %s", msg, vim.inspect(globals.TABPAGES[tabpage], { newline = "" })) + else + msg = string.format("%s tabinitial", msg) + end + log.line("dev", "%s", msg) + end + globals.TABPAGES[tabpage] = vim.tbl_extend("force", globals.TABPAGES[tabpage] or tabinitial, { winnr = winnr }) end @@ -221,7 +234,7 @@ function View:open_window() vim.api.nvim_command("vsp") self:reposition_window() end - self:setup_tabpage(vim.api.nvim_get_current_tabpage()) + self:setup_tabpage(vim.api.nvim_get_current_tabpage(), "View:open_window") self:set_window_options_and_buffer() end @@ -448,7 +461,7 @@ function View:open_in_win(opts) vim.api.nvim_set_current_win(opts.winid) end self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) - self:setup_tabpage(vim.api.nvim_get_current_tabpage()) + self:setup_tabpage(vim.api.nvim_get_current_tabpage(), "View:open_in_win") self:set_current_win() self:set_window_options_and_buffer() if opts.resize then From 3b792d4015310f378c0ad218358ed01508a0667c Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Wed, 18 Jun 2025 15:59:31 +1000 Subject: [PATCH 23/32] refactor(#2826): instrument View:set_current_win, View:prevent_buffer_override --- lua/nvim-tree/explorer/view.lua | 37 ++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index d8c6404b5a5..a6576655682 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -175,15 +175,12 @@ local move_tbl = { function View:setup_tabpage(tabpage, callsite) local winnr = vim.api.nvim_get_current_win() - local msg = nil if self.explorer.opts.experimental.multi_instance_debug then - msg = string.format( "View:setup_tabpage(%3s, %-20.20s) w%d", tabpage, callsite, winnr) - if globals.TABPAGES[tabpage] then - msg = string.format("%s %s", msg, vim.inspect(globals.TABPAGES[tabpage], { newline = "" })) - else - msg = string.format("%s tabinitial", msg) - end - log.line("dev", "%s", msg) + log.line("dev", "View:setup_tabpage(%3s, %-20.20s) w%d %s", + tabpage, + callsite, + winnr, + globals.TABPAGES[tabpage] and vim.inspect(globals.TABPAGES[tabpage], { newline = "" }) or "tabinitial") end globals.TABPAGES[tabpage] = vim.tbl_extend("force", globals.TABPAGES[tabpage] or tabinitial, { winnr = winnr }) @@ -447,9 +444,21 @@ function View:reposition_window() end ---@private -function View:set_current_win() +function View:set_current_win(callsite) local current_tab = vim.api.nvim_get_current_tabpage() - globals.TABPAGES[current_tab].winnr = vim.api.nvim_get_current_win() + local current_win = vim.api.nvim_get_current_win() + + if self.explorer.opts.experimental.multi_instance_debug then + log.line("dev", "View:set_current_win(%-20.20s) t%d w%3d->w%3d %s", + callsite, + current_tab, + globals.TABPAGES[current_tab].winnr, + current_win, + (globals.TABPAGES[current_tab].winnr == current_win) and "" or "UPDATED" + ) + end + + globals.TABPAGES[current_tab].winnr = current_win end ---Open the tree in the a window @@ -462,7 +471,7 @@ function View:open_in_win(opts) end self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) self:setup_tabpage(vim.api.nvim_get_current_tabpage(), "View:open_in_win") - self:set_current_win() + self:set_current_win("View:open_in_win") self:set_window_options_and_buffer() if opts.resize then self:reposition_window() @@ -600,7 +609,7 @@ function View:get_bufnr() end function View:prevent_buffer_override() - local view_winnr = self:get_winnr(nil, "View:get_bufnr") + local view_winnr = self:get_winnr(nil, "View:prevent_buffer_override") local view_bufnr = self:get_bufnr() -- need to schedule to let the new buffer populate the window @@ -615,6 +624,10 @@ function View:prevent_buffer_override() if not bufname:match("NvimTree") then for i, tabpage in ipairs(globals.TABPAGES) do if tabpage.winnr == view_winnr then + if self.explorer.opts.experimental.multi_instance_debug then + log.line("dev", "View:prevent_buffer_override() t%d w%d clearing", i, view_winnr) + end + globals.TABPAGES[i] = nil break end From 223693b4d4ec24bf4d5b408807e8cf01aa0ec351 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Wed, 18 Jun 2025 16:25:09 +1000 Subject: [PATCH 24/32] refactor(#2826): instrument View:get_bufnr --- lua/nvim-tree/diagnostics.lua | 2 +- lua/nvim-tree/explorer/view.lua | 41 ++++++++++++++++++++++----------- lua/nvim-tree/renderer/init.lua | 2 +- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index e6484caf23e..0ed81c74682 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -185,7 +185,7 @@ function M.update_coc() local bufnr if explorer then - bufnr = explorer.view:get_bufnr() + bufnr = explorer.view:get_bufnr("diagnostics.update_coc") end local should_draw = bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index a6576655682..0c993672966 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -31,6 +31,7 @@ local DEFAULT_PADDING = 1 ---@field private width (fun():integer)|integer|string ---@field private max_width integer ---@field private padding integer +---@field private bufnr_by_tab table for diagnostics during multi instance local View = Class:extend() ---@class View @@ -53,6 +54,7 @@ function View:new(args) self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions self.side = (self.explorer.opts.view.side == "right") and "right" or "left" self.live_filter = { prev_focused_node = nil, } + self.bufnr_by_tab = {} self.winopts = { relativenumber = self.explorer.opts.view.relativenumber, @@ -127,16 +129,17 @@ function View:create_buffer(bufnr) local tab = vim.api.nvim_get_current_tabpage() globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) - vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab) + self.bufnr_by_tab[tab] = globals.BUFNR_PER_TAB[tab] + vim.api.nvim_buf_set_name(self:get_bufnr("View:create_buffer1"), "NvimTree_" .. tab) - bufnr = self:get_bufnr() + bufnr = self:get_bufnr("View:create_buffer2") for _, option in ipairs(BUFFER_OPTIONS) do vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr }) end - require("nvim-tree.keymap").on_attach(self:get_bufnr()) + require("nvim-tree.keymap").on_attach(self:get_bufnr("View:create_buffer3")) - events._dispatch_tree_attached_post(self:get_bufnr()) + events._dispatch_tree_attached_post(self:get_bufnr("View:create_buffer4")) end ---@private @@ -171,7 +174,7 @@ local move_tbl = { -- setup_tabpage sets up the initial state of a tab ---@private ---@param tabpage integer ----@param callsite string? +---@param callsite string function View:setup_tabpage(tabpage, callsite) local winnr = vim.api.nvim_get_current_win() @@ -188,7 +191,7 @@ end ---@private function View:set_window_options_and_buffer() - pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr()) + pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr("View:set_window_options_and_buffer")) if vim.fn.has("nvim-0.10") == 1 then local eventignore = vim.api.nvim_get_option_value("eventignore", {}) @@ -348,7 +351,7 @@ end ---@private function View:grow() local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 - local lines = vim.api.nvim_buf_get_lines(self:get_bufnr(), starts_at, -1, false) + local lines = vim.api.nvim_buf_get_lines(self:get_bufnr("View:grow1"), starts_at, -1, false) -- number of columns of right-padding to indicate end of path local padding = self:get_size(self.padding) @@ -372,7 +375,7 @@ function View:grow() for line_nr, l in pairs(lines) do local count = vim.fn.strchars(l) -- also add space for right-aligned icons - local extmarks = vim.api.nvim_buf_get_extmarks(self:get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true }) + local extmarks = vim.api.nvim_buf_get_extmarks(self:get_bufnr("View:grow2"), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true }) count = count + utils.extmarks_length(extmarks) if resizing_width < count then resizing_width = count @@ -449,7 +452,7 @@ function View:set_current_win(callsite) local current_win = vim.api.nvim_get_current_win() if self.explorer.opts.experimental.multi_instance_debug then - log.line("dev", "View:set_current_win(%-20.20s) t%d w%3d->w%3d %s", + log.line("dev", "View:set_current_win(%-20.20s) t%d w%3s->w%3s %s", callsite, current_tab, globals.TABPAGES[current_tab].winnr, @@ -483,6 +486,7 @@ end function View:abandon_current_window() local tab = vim.api.nvim_get_current_tabpage() globals.BUFNR_PER_TAB[tab] = nil + self.bufnr_by_tab[tab] = nil if globals.TABPAGES[tab] then globals.TABPAGES[tab].winnr = nil end @@ -491,6 +495,7 @@ end function View:abandon_all_windows() for tab, _ in pairs(vim.api.nvim_list_tabpages()) do globals.BUFNR_PER_TAB[tab] = nil + self.bufnr_by_tab[tab] = nil if globals.TABPAGES[tab] then globals.TABPAGES[tab].winnr = nil end @@ -568,7 +573,7 @@ end --- 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 callsite string? for logging purposes +---@param callsite string for logging purposes ---@return number|nil function View:get_winnr(tabpage, callsite) if self.explorer.opts.experimental.multi_instance_debug then @@ -603,14 +608,24 @@ function View:get_winnr(tabpage, callsite) end --- Returns the current nvim tree bufnr +---@param callsite string ---@return number -function View:get_bufnr() - return globals.BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()] +function View:get_bufnr(callsite) + local tab = vim.api.nvim_get_current_tabpage() + if self.explorer.opts.experimental.multi_instance_debug then + log.line("dev", "View:get_bufnr(%-20.20s) t%d global b%s member b%s %s", + callsite, + tab, + globals.BUFNR_PER_TAB[tab], + self.bufnr_by_tab[tab], + (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") + end + return globals.BUFNR_PER_TAB[tab] end function View:prevent_buffer_override() local view_winnr = self:get_winnr(nil, "View:prevent_buffer_override") - local view_bufnr = self:get_bufnr() + local view_bufnr = self:get_bufnr("View:prevent_buffer_override") -- need to schedule to let the new buffer populate the window -- because this event needs to be run on bufWipeout. diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 46bde0aee65..f782973726a 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -101,7 +101,7 @@ function Renderer:render_hl(bufnr, hl_range_args) end function Renderer:draw() - local bufnr = self.explorer.view:get_bufnr() + local bufnr = self.explorer.view:get_bufnr("Renderer:draw") if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then return end From c8a7212a8f0c6b22360e522e7d2e1089d9659e67 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Wed, 18 Jun 2025 16:38:16 +1000 Subject: [PATCH 25/32] refactor(#2826): track member bufnr -> winid with global --- lua/nvim-tree/explorer/view.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 0c993672966..45c203f855b 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -595,6 +595,19 @@ function View:get_winnr(tabpage, callsite) ret = tabinfo.winnr end + local winid_from_bufnr + if self.bufnr_by_tab[tabpage] then + for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do + if vim.api.nvim_win_get_buf(winid) == self.bufnr_by_tab[tabpage] then + winid_from_bufnr = winid + end + end + end + + if ret ~= winid_from_bufnr then + msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid_from_bufnr) + end + log.line("dev", "%s", msg) return ret From 3f13193f6dcdd9c16888a21aa866d818187e0f4b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 10:22:23 +1000 Subject: [PATCH 26/32] refactor(#2826): tidy experiment names and logs --- doc/nvim-tree-lua.txt | 2 +- lua/nvim-tree.lua | 2 +- lua/nvim-tree/explorer/view.lua | 12 ++++++------ lua/nvim-tree/multi-instance-debug.lua | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 260a01fa258..0913d854c45 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -638,7 +638,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua }, }, experimental = { - multi_instance_debug = false, + multi_instance = false, }, log = { enable = false, diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index e59522bc1aa..78a27a7a696 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -485,7 +485,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS }, }, experimental = { - multi_instance_debug = false, + multi_instance = false, }, log = { enable = false, diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 45c203f855b..6d5f7226df3 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -178,7 +178,7 @@ local move_tbl = { function View:setup_tabpage(tabpage, callsite) local winnr = vim.api.nvim_get_current_win() - if self.explorer.opts.experimental.multi_instance_debug then + if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:setup_tabpage(%3s, %-20.20s) w%d %s", tabpage, callsite, @@ -451,13 +451,13 @@ function View:set_current_win(callsite) local current_tab = vim.api.nvim_get_current_tabpage() local current_win = vim.api.nvim_get_current_win() - if self.explorer.opts.experimental.multi_instance_debug then + if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:set_current_win(%-20.20s) t%d w%3s->w%3s %s", callsite, current_tab, globals.TABPAGES[current_tab].winnr, current_win, - (globals.TABPAGES[current_tab].winnr == current_win) and "" or "UPDATED" + (globals.TABPAGES[current_tab].winnr == current_win) and "" or "MISMATCH" ) end @@ -576,7 +576,7 @@ end ---@param callsite string for logging purposes ---@return number|nil function View:get_winnr(tabpage, callsite) - if self.explorer.opts.experimental.multi_instance_debug then + if self.explorer.opts.experimental.multi_instance then local msg = string.format("View:get_winnr(%3s, %-20.20s)", tabpage, callsite) tabpage = tabpage or vim.api.nvim_get_current_tabpage() @@ -625,7 +625,7 @@ end ---@return number function View:get_bufnr(callsite) local tab = vim.api.nvim_get_current_tabpage() - if self.explorer.opts.experimental.multi_instance_debug then + if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:get_bufnr(%-20.20s) t%d global b%s member b%s %s", callsite, tab, @@ -652,7 +652,7 @@ function View:prevent_buffer_override() if not bufname:match("NvimTree") then for i, tabpage in ipairs(globals.TABPAGES) do if tabpage.winnr == view_winnr then - if self.explorer.opts.experimental.multi_instance_debug then + if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:prevent_buffer_override() t%d w%d clearing", i, view_winnr) end diff --git a/lua/nvim-tree/multi-instance-debug.lua b/lua/nvim-tree/multi-instance-debug.lua index 58ac13734de..55e2e3c88ea 100644 --- a/lua/nvim-tree/multi-instance-debug.lua +++ b/lua/nvim-tree/multi-instance-debug.lua @@ -101,7 +101,7 @@ function M.tab_line() end function M.setup(opts) - if not opts.experimental.multi_instance_debug then + if not opts.experimental.multi_instance then return end From bca6ed8b086363c072acbf823529aed617153d8d Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 11:42:10 +1000 Subject: [PATCH 27/32] vim: nvim-tree: track bufnr via buffer-update channel --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/actions/node/open-file.lua | 2 +- lua/nvim-tree/explorer/init.lua | 2 +- lua/nvim-tree/explorer/view.lua | 55 +++++++++++++++++++++--- lua/nvim-tree/multi-instance-debug.lua | 4 +- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 78a27a7a696..45087633f9f 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -668,7 +668,7 @@ function M.purge_all_state() local explorer = core.get_explorer() if explorer then explorer.view:close_all_tabs() - explorer.view:abandon_all_windows() + explorer.view:abandon_all_windows("purge_all_state") require("nvim-tree.git").purge_state() explorer:destroy() core.reset_explorer() diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 317d3d46ac7..987fce61de6 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -389,7 +389,7 @@ local function edit_in_current_buf(filename) local explorer = core.get_explorer() if explorer then - explorer.view:abandon_current_window() + explorer.view:abandon_current_window("open-file.edit_in_current_buf") end if M.relative_path then diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 76d568b2ae2..4bcd43e904b 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -176,7 +176,7 @@ function Explorer:create_autocmds() if self.opts.actions.open_file.eject then self.view:prevent_buffer_override() else - self.view:abandon_current_window() + self.view:abandon_current_window("BufWipeout") end end, }) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 6d5f7226df3..70036d1762b 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -129,7 +129,18 @@ function View:create_buffer(bufnr) local tab = vim.api.nvim_get_current_tabpage() globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) - self.bufnr_by_tab[tab] = globals.BUFNR_PER_TAB[tab] + + if self.explorer.opts.experimental.multi_instance then + local tabid = tab + self.bufnr_by_tab[tabid] = globals.BUFNR_PER_TAB[tabid] + log.line("dev", "buffer channel attaching t%d b%d", tabid, self.bufnr_by_tab[tabid]) + vim.api.nvim_buf_attach(self.bufnr_by_tab[tabid], true, { + on_detach = function(op, bnr) + log.line("dev", "buffer channel %s t%d b%d", op, tabid, bnr) + end, + }) + end + vim.api.nvim_buf_set_name(self:get_bufnr("View:create_buffer1"), "NvimTree_" .. tab) bufnr = self:get_bufnr("View:create_buffer2") @@ -447,6 +458,7 @@ function View:reposition_window() end ---@private +---@param callsite string function View:set_current_win(callsite) local current_tab = vim.api.nvim_get_current_tabpage() local current_win = vim.api.nvim_get_current_win() @@ -483,19 +495,44 @@ function View:open_in_win(opts) events._dispatch_on_tree_open() end -function View:abandon_current_window() +---@param callsite string +function View:abandon_current_window(callsite) local tab = vim.api.nvim_get_current_tabpage() + + if self.explorer.opts.experimental.multi_instance then + log.line("dev", "View:abandon_current_window(%-20.20s) t%d w%s b%s member b%s %s", + callsite, + tab, + globals.TABPAGES and globals.TABPAGES.winnr or nil, + globals.BUFNR_PER_TAB[tab], + self.bufnr_by_tab[tab], + (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") + end + + -- TODO multi-instance kill the buffer instead of retaining + globals.BUFNR_PER_TAB[tab] = nil - self.bufnr_by_tab[tab] = nil if globals.TABPAGES[tab] then globals.TABPAGES[tab].winnr = nil end end -function View:abandon_all_windows() +---@param callsite string +function View:abandon_all_windows(callsite) for tab, _ in pairs(vim.api.nvim_list_tabpages()) do + if self.explorer.opts.experimental.multi_instance then + log.line("dev", "View:abandon_all_windows(%-20.20s) t%d w%s b%s member b%s %s", + callsite, + tab, + globals.TABPAGES and globals.TABPAGES.winnr or nil, + globals.BUFNR_PER_TAB[tab], + self.bufnr_by_tab[tab], + (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") + end + + -- TODO multi-instance kill the buffer instead of retaining + globals.BUFNR_PER_TAB[tab] = nil - self.bufnr_by_tab[tab] = nil if globals.TABPAGES[tab] then globals.TABPAGES[tab].winnr = nil end @@ -573,7 +610,7 @@ end --- 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 callsite string for logging purposes +---@param callsite string ---@return number|nil function View:get_winnr(tabpage, callsite) if self.explorer.opts.experimental.multi_instance then @@ -605,7 +642,11 @@ function View:get_winnr(tabpage, callsite) end if ret ~= winid_from_bufnr then - msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid_from_bufnr) + if ret then + msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid_from_bufnr) + else + msg = string.format("%s winid_from_bufnr w%s STALE", msg, winid_from_bufnr) + end end log.line("dev", "%s", msg) diff --git a/lua/nvim-tree/multi-instance-debug.lua b/lua/nvim-tree/multi-instance-debug.lua index 55e2e3c88ea..5b4604837cd 100644 --- a/lua/nvim-tree/multi-instance-debug.lua +++ b/lua/nvim-tree/multi-instance-debug.lua @@ -36,7 +36,7 @@ function M.tab_line() -- winnr, if present local tp = globals.TABPAGES[tab_id] if tp then - tl = tl .. " w" .. tp.winnr + tl = tl .. " w" .. (tp.winnr or "nil") else tl = tl .. " " end @@ -80,7 +80,7 @@ function M.tab_line() -- maybe winnr if orphan.winnr then - tl = tl .. " w" .. orphan.winnr + tl = tl .. " w" .. (orphan.winnr or "nil") else tl = tl .. " " end From 0a437b31be679a524c22034f80df3794009a0c98 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 12:41:20 +1000 Subject: [PATCH 28/32] vim: nvim-tree: more logging --- lua/nvim-tree/actions/node/open-file.lua | 8 ++++---- lua/nvim-tree/actions/tree/toggle.lua | 2 +- lua/nvim-tree/api.lua | 2 +- lua/nvim-tree/explorer/init.lua | 2 +- lua/nvim-tree/explorer/view.lua | 21 +++++++++++++++++---- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 987fce61de6..2f7d4888028 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -190,7 +190,7 @@ local function open_file_in_tab(filename) if M.quit_on_open then local explorer = core.get_explorer() if explorer then - explorer.view:close() + explorer.view:close(nil, "open-file.open_file_in_tab") end end if M.relative_path then @@ -203,7 +203,7 @@ local function drop(filename) if M.quit_on_open then local explorer = core.get_explorer() if explorer then - explorer.view:close() + explorer.view:close(nil, "open-file.drop") end end if M.relative_path then @@ -216,7 +216,7 @@ local function tab_drop(filename) if M.quit_on_open then local explorer = core.get_explorer() if explorer then - explorer.view:close() + explorer.view:close(nil, "open-file.tab_drop") end end if M.relative_path then @@ -447,7 +447,7 @@ function M.fn(mode, filename) end if M.quit_on_open and explorer then - explorer.view:close() + explorer.view:close(nil, "open-file.fn") end end diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index fb976cfbe71..d2ba0262a30 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -44,7 +44,7 @@ function M.fn(opts, no_focus, cwd, bang) if explorer and explorer.view:is_visible() then -- close - explorer.view:close() + explorer.view:close(nil, "toggle.fn") else -- open lib.open({ diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 3f70df2c880..73d84ddece6 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -247,7 +247,7 @@ local function edit(mode, node, edit_opts) local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then if explorer then - explorer.view:close(cur_tabpage) + explorer.view:close(cur_tabpage, "api.edit " .. mode) end end diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 4bcd43e904b..9b601102756 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -107,7 +107,7 @@ function Explorer:create_autocmds() pattern = "NvimTree_*", callback = function() if utils.is_nvim_tree_buf(0) then - self.view:close() + self.view:close(nil, "WinLeave") end end, }) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 70036d1762b..6cb3d8f2858 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -133,10 +133,12 @@ function View:create_buffer(bufnr) if self.explorer.opts.experimental.multi_instance then local tabid = tab self.bufnr_by_tab[tabid] = globals.BUFNR_PER_TAB[tabid] + log.line("dev", "buffer channel attaching t%d b%d", tabid, self.bufnr_by_tab[tabid]) - vim.api.nvim_buf_attach(self.bufnr_by_tab[tabid], true, { + vim.api.nvim_buf_attach(self.bufnr_by_tab[tabid], false, { on_detach = function(op, bnr) log.line("dev", "buffer channel %s t%d b%d", op, tabid, bnr) + self.bufnr_by_tab[tabid] = nil end, }) end @@ -291,6 +293,9 @@ end ---@private ---@param tabpage integer function View:close_internal(tabpage) + if self.explorer.opts.experimental.multi_instance then + log.line("dev", "View:close_internal(t%s)", tabpage) + end if not self:is_visible({ tabpage = tabpage }) then return end @@ -305,6 +310,9 @@ function View:close_internal(tabpage) vim.api.nvim_set_current_win(vim.fn.win_getid(prev_win)) end if vim.api.nvim_win_is_valid(tree_win or 0) then + if self.explorer.opts.experimental.multi_instance then + log.line("dev", "View:close_internal(t%s) w%s", tabpage, tree_win) + end local success, error = pcall(vim.api.nvim_win_close, tree_win or 0, true) if not success then notify.debug("Failed to close window: " .. error) @@ -327,7 +335,12 @@ function View:close_all_tabs() end ---@param tabpage integer|nil -function View:close(tabpage) +---@param callsite string +function View:close(tabpage, callsite) + if self.explorer.opts.experimental.multi_instance then + log.line("dev", "View:close(t%s, %s)", tabpage, callsite) + end + if self.explorer.opts.tab.sync.close then self:close_all_tabs() elseif tabpage then @@ -503,7 +516,7 @@ function View:abandon_current_window(callsite) log.line("dev", "View:abandon_current_window(%-20.20s) t%d w%s b%s member b%s %s", callsite, tab, - globals.TABPAGES and globals.TABPAGES.winnr or nil, + globals.TABPAGES[tab] and globals.TABPAGES[tab].winnr or nil, globals.BUFNR_PER_TAB[tab], self.bufnr_by_tab[tab], (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") @@ -575,7 +588,7 @@ function View:focus(winnr, open_if_closed) local wnr = winnr or self:get_winnr(nil, "View:focus1") if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then - self:close() + self:close(nil, "View:focus") self:open() wnr = self:get_winnr(nil, "View:focus2") elseif open_if_closed and not self:is_visible() then From 6347a2c173559e0aeb0f83d4c698151f35a82964 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 12:58:33 +1000 Subject: [PATCH 29/32] vim: nvim-tree: revert: track bufnr via buffer-update channel --- lua/nvim-tree/explorer/init.lua | 12 +++++++++--- lua/nvim-tree/explorer/view.lua | 19 +++++-------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 9b601102756..ca8c6d39103 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -105,7 +105,10 @@ function Explorer:create_autocmds() vim.api.nvim_create_autocmd("WinLeave", { group = self.augroup_id, pattern = "NvimTree_*", - callback = function() + callback = function(data) + if self.opts.experimental.multi_instance then + log.line("dev", "WinLeave %s", vim.inspect(data, { newline = "" })) + end if utils.is_nvim_tree_buf(0) then self.view:close(nil, "WinLeave") end @@ -169,14 +172,17 @@ function Explorer:create_autocmds() vim.api.nvim_create_autocmd("BufWipeout", { group = self.augroup_id, pattern = "NvimTree_*", - callback = function() + callback = function(data) + if self.opts.experimental.multi_instance then + log.line("dev", "BufWipeout %s", vim.inspect(data, { newline = "" })) + end 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("BufWipeout") + self.view:abandon_current_window() end end, }) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 6cb3d8f2858..609937b1dde 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -131,16 +131,7 @@ function View:create_buffer(bufnr) globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) if self.explorer.opts.experimental.multi_instance then - local tabid = tab - self.bufnr_by_tab[tabid] = globals.BUFNR_PER_TAB[tabid] - - log.line("dev", "buffer channel attaching t%d b%d", tabid, self.bufnr_by_tab[tabid]) - vim.api.nvim_buf_attach(self.bufnr_by_tab[tabid], false, { - on_detach = function(op, bnr) - log.line("dev", "buffer channel %s t%d b%d", op, tabid, bnr) - self.bufnr_by_tab[tabid] = nil - end, - }) + self.bufnr_by_tab[tab] = globals.BUFNR_PER_TAB[tab] end vim.api.nvim_buf_set_name(self:get_bufnr("View:create_buffer1"), "NvimTree_" .. tab) @@ -508,18 +499,18 @@ function View:open_in_win(opts) events._dispatch_on_tree_open() end ----@param callsite string -function View:abandon_current_window(callsite) +function View:abandon_current_window() local tab = vim.api.nvim_get_current_tabpage() if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:abandon_current_window(%-20.20s) t%d w%s b%s member b%s %s", - callsite, + log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s", tab, globals.TABPAGES[tab] and globals.TABPAGES[tab].winnr or nil, globals.BUFNR_PER_TAB[tab], self.bufnr_by_tab[tab], (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") + + self.bufnr_by_tab[tab] = nil end -- TODO multi-instance kill the buffer instead of retaining From de3f48629dd47f773788fd1773f9a18e41d5dc61 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 13:24:02 +1000 Subject: [PATCH 30/32] refactor(#2826): notify error on view winid and bufnr mismatches --- lua/nvim-tree/actions/node/open-file.lua | 2 +- lua/nvim-tree/api.lua | 2 +- lua/nvim-tree/explorer/view.lua | 48 +++++++++++++++++------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 2f7d4888028..dedd61229e5 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -389,7 +389,7 @@ local function edit_in_current_buf(filename) local explorer = core.get_explorer() if explorer then - explorer.view:abandon_current_window("open-file.edit_in_current_buf") + explorer.view:abandon_current_window() end if M.relative_path then diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 73d84ddece6..c75115c6c62 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -206,7 +206,7 @@ Api.tree.is_visible = wrap_explorer_member("view", "is_visible") ---@class ApiTreeWinIdOpts ---@field tabpage number|nil default nil -Api.tree.winid = wrap_explorer_member("view", "winid") +Api.tree.winid = wrap_explorer_member("view", "api_winid") Api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn) Api.fs.remove = wrap_node(actions.fs.remove_file.fn) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 609937b1dde..99a0ad2f1ac 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -594,7 +594,7 @@ end --- Retrieve the winid of the open tree. ---@param opts ApiTreeWinIdOpts|nil ---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible -function View:winid(opts) +function View:api_winid(opts) local tabpage = opts and opts.tabpage if tabpage == 0 then tabpage = vim.api.nvim_get_current_tabpage() @@ -612,6 +612,28 @@ function View:restore_tab_state() self:set_cursor(globals.CURSORS[tabpage]) end +--- winid containing the buffer +---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. +---@param callsite string +---@return integer? winid +function View:winid(tabpage, callsite) + local winid = nil + local bufnr = self.bufnr_by_tab[tabpage] + + local msg = string.format("View:winid(%3s, %-20.20s)", tabpage, callsite) + + if bufnr then + for _, w in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do + if vim.api.nvim_win_get_buf(w) == bufnr then + log.line("dev", "%s b%d : w%s", msg, bufnr, winid) + return w + end + end + else + msg = string.format("%s no bufnr", msg) + end +end + --- 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 callsite string @@ -636,21 +658,14 @@ function View:get_winnr(tabpage, callsite) ret = tabinfo.winnr end - local winid_from_bufnr - if self.bufnr_by_tab[tabpage] then - for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do - if vim.api.nvim_win_get_buf(winid) == self.bufnr_by_tab[tabpage] then - winid_from_bufnr = winid - end - end - end - - if ret ~= winid_from_bufnr then + local winid = self:winid(tabpage, "View:get_winnr") + if ret ~= winid then if ret then - msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid_from_bufnr) + msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid) else - msg = string.format("%s winid_from_bufnr w%s STALE", msg, winid_from_bufnr) + msg = string.format("%s winid_from_bufnr w%s STALE", msg, winid) end + notify.error(string.format("View:get_winnr w%s View:winnr w%s MISMATCH", ret, winid)) end log.line("dev", "%s", msg) @@ -677,6 +692,13 @@ function View:get_bufnr(callsite) globals.BUFNR_PER_TAB[tab], self.bufnr_by_tab[tab], (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") + + if globals.BUFNR_PER_TAB[tab] ~= self.bufnr_by_tab[tab] then + notify.error(string.format("View:get_bufnr globals.BUFNR_PER_TAB[%s] b%s view.bufnr_by_tab[%s] b%s MISMATCH", + tab, globals.BUFNR_PER_TAB[tab], + tab, self.bufnr_by_tab[tab] + )) + end end return globals.BUFNR_PER_TAB[tab] end From d822fdd822edb7837a4f0b1fe3dc3f82644bdf5a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 13:35:48 +1000 Subject: [PATCH 31/32] refactor(#2826): notify error on view winid and bufnr mismatches --- lua/nvim-tree/explorer/view.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 99a0ad2f1ac..2a55ca76f63 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -630,7 +630,7 @@ function View:winid(tabpage, callsite) end end else - msg = string.format("%s no bufnr", msg) + log.line("dev", "%s no bufnr", msg) end end From d1df6d45a40a94fbacb8aacec69a99921d943286 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 13:43:21 +1000 Subject: [PATCH 32/32] refactor(#2826): explorer init logging --- lua/nvim-tree/actions/root/change-dir.lua | 2 +- lua/nvim-tree/core.lua | 5 ++++- lua/nvim-tree/explorer/view.lua | 5 ++--- lua/nvim-tree/lib.lua | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/actions/root/change-dir.lua b/lua/nvim-tree/actions/root/change-dir.lua index f23f3c12086..f98d51d33e2 100644 --- a/lua/nvim-tree/actions/root/change-dir.lua +++ b/lua/nvim-tree/actions/root/change-dir.lua @@ -85,7 +85,7 @@ M.force_dirchange = add_profiling_to(function(foldername, should_open_view) if should_change_dir() then cd(M.options.global, foldername) end - core.init(foldername) + core.init(foldername, "change-dir") end if should_open_view then diff --git a/lua/nvim-tree/core.lua b/lua/nvim-tree/core.lua index fe9125e612d..46f6ef698ca 100644 --- a/lua/nvim-tree/core.lua +++ b/lua/nvim-tree/core.lua @@ -9,9 +9,12 @@ local TreeExplorer = nil local first_init_done = false ---@param foldername string -function M.init(foldername) +---@param callsite string +function M.init(foldername, callsite) local profile = log.profile_start("core init %s", foldername) + log.line("dev", "core.init(%s, %s)", foldername, callsite) + if TreeExplorer then TreeExplorer:destroy() end diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 2a55ca76f63..627364a4e40 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -31,7 +31,7 @@ local DEFAULT_PADDING = 1 ---@field private width (fun():integer)|integer|string ---@field private max_width integer ---@field private padding integer ----@field private bufnr_by_tab table for diagnostics during multi instance +---@field private bufnr_by_tab table stored per tab until multi-instance is complete local View = Class:extend() ---@class View @@ -617,7 +617,6 @@ end ---@param callsite string ---@return integer? winid function View:winid(tabpage, callsite) - local winid = nil local bufnr = self.bufnr_by_tab[tabpage] local msg = string.format("View:winid(%3s, %-20.20s)", tabpage, callsite) @@ -625,7 +624,7 @@ function View:winid(tabpage, callsite) if bufnr then for _, w in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do if vim.api.nvim_win_get_buf(w) == bufnr then - log.line("dev", "%s b%d : w%s", msg, bufnr, winid) + log.line("dev", "%s b%d : w%s", msg, bufnr, w) return w end end diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index b4733624d64..20fe23dbdd4 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -102,14 +102,14 @@ function M.open(opts) M.set_target_win() if not core.get_explorer() or opts.path then if opts.path then - core.init(opts.path) + core.init(opts.path, "lib.open - opts.path") else local cwd, err = vim.loop.cwd() if not cwd then notify.error(string.format("current working directory unavailable: %s", err)) return end - core.init(cwd) + core.init(cwd, "lib.open - cwd") end end