refactor(#2826): singleton View class, WIP

This commit is contained in:
Alexander Courtis 2025-04-20 14:07:02 +10:00
parent 53643f505a
commit 048e637134
10 changed files with 23 additions and 23 deletions

View File

@ -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)

View File

@ -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

View File

@ -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({

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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))

View File

@ -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

View File

@ -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