From d84dfad1c368a44767044f20cc35f0c41c17a2b2 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 12:46:30 +1000 Subject: [PATCH] refactor(#2826): move global CURSORS to view member --- lua/nvim-tree/explorer/view.lua | 11 ++++++----- lua/nvim-tree/globals.lua | 1 - lua/nvim-tree/lib.lua | 3 +-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 0cbdb899..a3e6d900 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -19,6 +19,7 @@ local Class = require("nvim-tree.classic") ---@field private max_width integer ---@field private padding integer ---@field private bufnr_by_tabid table stored per tab until multi-instance is complete +---@field private cursor integer[] as per vim.api.nvim_win_get_cursor local View = Class:extend() ---@class View @@ -235,12 +236,12 @@ local function switch_buf_if_last_buf() end end ----save_tab_state saves any state that should be preserved across redraws. +---save any state that should be preserved on reopening ---@private ---@param tabid integer function View:save_tab_state(tabid) tabid = tabid or vim.api.nvim_get_current_tabpage() - globals.CURSORS[tabid] = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0) + self.cursor = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0) end ---@private @@ -608,9 +609,9 @@ function View:api_winid(opts) end end ---- Restores the state of a NvimTree window if it was initialized before. -function View:restore_tab_state() - self:set_cursor(globals.CURSORS[vim.api.nvim_get_current_tabpage()]) +--- restore any state from last close +function View:restore_state() + self:set_cursor(self.cursor) end --- winid containing the buffer diff --git a/lua/nvim-tree/globals.lua b/lua/nvim-tree/globals.lua index 66211260..a2ab7527 100644 --- a/lua/nvim-tree/globals.lua +++ b/lua/nvim-tree/globals.lua @@ -4,7 +4,6 @@ local M = { -- from View WINID_BY_TABID = {}, BUFNR_BY_TABID = {}, - CURSORS = {}, } return M diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 854e33bf..6c1ef1ef 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -135,9 +135,8 @@ function M.open(opts) open_view_and_draw() end - -- TODO multi-instance is this actually necessary? if explorer then - explorer.view:restore_tab_state() + explorer.view:restore_state() end end