From 048e6371342c1b13c523936eae7aeed7d0d6cd71 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Apr 2025 14:07:02 +1000 Subject: [PATCH] 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 eb380408..1135c148 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 345ed6bb..19b6a9e5 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 7e7fca79..d03db93f 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 a04fe441..0fe8d238 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 60d4a0a6..943d3c80 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 b8a22b28..52c760c9 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 a4645055..2a877915 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 819660e5..531f1a0a 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 9396ba9d..01da1413 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 e5906a0d..0b1c3f4a 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