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, once = true,
}) })
end end
view.focus() view.View:focus()
end end
local function get_target_winid(mode) local function get_target_winid(mode)

View File

@ -45,7 +45,7 @@ function M.fn(opts)
-- focus -- focus
if opts.focus then if opts.focus then
lib.set_target_win() lib.set_target_win()
view.focus() view.View:focus()
end end
elseif opts.open then elseif opts.open then
-- open -- open

View File

@ -26,7 +26,7 @@ function M.fn(opts)
if view.View:is_visible() then if view.View:is_visible() then
-- focus -- focus
lib.set_target_win() lib.set_target_win()
view.focus() view.View:focus()
else else
-- open -- open
lib.open({ lib.open({

View File

@ -257,7 +257,7 @@ local function edit(mode, node, edit_opts)
if mode == "tabnew" then if mode == "tabnew" then
vim.cmd(":tabprev") vim.cmd(":tabprev")
end end
view.focus() view.View:focus()
end end
end end

View File

@ -55,7 +55,7 @@ end
---@return integer ---@return integer
function M.get_nodes_starting_line() function M.get_nodes_starting_line()
local offset = 1 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 offset = offset + 1
end end
if TreeExplorer and TreeExplorer.live_filter.filter then if TreeExplorer and TreeExplorer.live_filter.filter then

View File

@ -523,7 +523,7 @@ function Explorer:get_node_at_cursor()
return return
end 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 return self
end end

View File

@ -128,7 +128,7 @@ function M.open(opts)
else else
open_view_and_draw() open_view_and_draw()
end end
view.restore_tab_state() view.View:restore_tab_state()
end end
function M.setup(opts) function M.setup(opts)

View File

@ -379,7 +379,7 @@ end
---@private ---@private
function Builder:build_header() 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) local root_name = self:format_root_name(self.explorer.opts.renderer.root_folder_label)
table.insert(self.lines, root_name) table.insert(self.lines, root_name)
self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(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) return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
end) end)
:iterate() :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 if node and node.explorer.live_filter.filter then
i = i + 1 i = i + 1
end end
@ -537,7 +537,7 @@ function M.focus_file(path)
local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node) local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node)
return node.absolute_path == path return node.absolute_path == path
end) end)
require("nvim-tree.view").set_cursor({ i + 1, 1 }) require("nvim-tree.view").View:set_cursor({ i + 1, 1 })
end end
---Focus node passed as parameter if visible, otherwise focus first visible parent. ---Focus node passed as parameter if visible, otherwise focus first visible parent.
@ -557,7 +557,7 @@ function M.focus_node_or_parent(node)
end) end)
if found_node or node.parent == nil then 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 break
end end

View File

@ -364,7 +364,7 @@ end
---@private ---@private
function View:grow() 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) 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 -- number of columns of right-padding to indicate end of path
local padding = self:get_size(self.padding) local padding = self:get_size(self.padding)
@ -534,15 +534,15 @@ end
---@param winnr number|nil ---@param winnr number|nil
---@param open_if_closed boolean|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() 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 if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then
M.close() self:close()
M.View:open() self:open()
wnr = M.get_winnr() wnr = M.get_winnr()
elseif open_if_closed and not M.View:is_visible() then elseif open_if_closed and not self:is_visible() then
M.View:open() self:open()
end end
if wnr then if wnr then
@ -553,12 +553,12 @@ end
--- Retrieve the winid of the open tree. --- Retrieve the winid of the open tree.
---@param opts ApiTreeWinIdOpts|nil ---@param opts ApiTreeWinIdOpts|nil
---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible ---@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 local tabpage = opts and opts.tabpage
if tabpage == 0 then if tabpage == 0 then
tabpage = vim.api.nvim_get_current_tabpage() tabpage = vim.api.nvim_get_current_tabpage()
end end
if M.View:is_visible({ tabpage = tabpage }) then if self:is_visible({ tabpage = tabpage }) then
return M.get_winnr(tabpage) return M.get_winnr(tabpage)
else else
return nil return nil
@ -566,9 +566,9 @@ function M.winid(opts)
end end
--- Restores the state of a NvimTree window if it was initialized before. --- 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() local tabpage = vim.api.nvim_get_current_tabpage()
M.View:set_cursor(M.View.cursors[tabpage]) self:set_cursor(self.cursors[tabpage])
end end
--- Returns the window number for nvim-tree within the tabpage specified --- Returns the window number for nvim-tree within the tabpage specified
@ -638,8 +638,8 @@ end
---@param cwd string|nil ---@param cwd string|nil
---@return boolean ---@return boolean
function M.is_root_folder_visible(cwd) function View:is_root_folder_visible(cwd)
return cwd ~= "/" and not M.View.hide_root_folder return cwd ~= "/" and not self.hide_root_folder
end end
-- used on ColorScheme event -- used on ColorScheme event