Skip to content

Commit 0a04e43

Browse files
committed
refactor(#2826): singleton View class, WIP
1 parent b95b873 commit 0a04e43

File tree

10 files changed

+23
-23
lines changed

10 files changed

+23
-23
lines changed

lua/nvim-tree/actions/node/open-file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ local function on_preview(buf_loaded)
239239
once = true,
240240
})
241241
end
242-
view.focus()
242+
view.View:focus()
243243
end
244244

245245
local function get_target_winid(mode)

lua/nvim-tree/actions/tree/find-file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function M.fn(opts)
4545
-- focus
4646
if opts.focus then
4747
lib.set_target_win()
48-
view.focus()
48+
view.View:focus()
4949
end
5050
elseif opts.open then
5151
-- open

lua/nvim-tree/actions/tree/open.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function M.fn(opts)
2626
if view.View:is_visible() then
2727
-- focus
2828
lib.set_target_win()
29-
view.focus()
29+
view.View:focus()
3030
else
3131
-- open
3232
lib.open({

lua/nvim-tree/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ local function edit(mode, node, edit_opts)
253253
if mode == "tabnew" then
254254
vim.cmd(":tabprev")
255255
end
256-
view.focus()
256+
view.View:focus()
257257
end
258258
end
259259

lua/nvim-tree/core.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ end
5555
---@return integer
5656
function M.get_nodes_starting_line()
5757
local offset = 1
58-
if view.is_root_folder_visible(M.get_cwd()) then
58+
if view.View:is_root_folder_visible(M.get_cwd()) then
5959
offset = offset + 1
6060
end
6161
if TreeExplorer and TreeExplorer.live_filter.filter then

lua/nvim-tree/explorer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ function Explorer:get_node_at_cursor()
523523
return
524524
end
525525

526-
if cursor[1] == 1 and view.is_root_folder_visible(core.get_cwd()) then
526+
if cursor[1] == 1 and view.View:is_root_folder_visible(core.get_cwd()) then
527527
return self
528528
end
529529

lua/nvim-tree/lib.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function M.open(opts)
128128
else
129129
open_view_and_draw()
130130
end
131-
view.restore_tab_state()
131+
view.View:restore_tab_state()
132132
end
133133

134134
function M.setup(opts)

lua/nvim-tree/renderer/builder.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ end
379379

380380
---@private
381381
function Builder:build_header()
382-
if view.is_root_folder_visible(self.explorer.absolute_path) then
382+
if view.View:is_root_folder_visible(self.explorer.absolute_path) then
383383
local root_name = self:format_root_name(self.explorer.opts.renderer.root_folder_label)
384384
table.insert(self.lines, root_name)
385385
self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name))

lua/nvim-tree/utils.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function M.find_node(nodes, fn)
144144
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
145145
end)
146146
:iterate()
147-
i = require("nvim-tree.view").is_root_folder_visible() and i or i - 1
147+
i = require("nvim-tree.view").View:is_root_folder_visible() and i or i - 1
148148
if node and node.explorer.live_filter.filter then
149149
i = i + 1
150150
end
@@ -497,7 +497,7 @@ function M.focus_file(path)
497497
local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node)
498498
return node.absolute_path == path
499499
end)
500-
require("nvim-tree.view").set_cursor({ i + 1, 1 })
500+
require("nvim-tree.view").View:set_cursor({ i + 1, 1 })
501501
end
502502

503503
---Focus node passed as parameter if visible, otherwise focus first visible parent.
@@ -517,7 +517,7 @@ function M.focus_node_or_parent(node)
517517
end)
518518

519519
if found_node or node.parent == nil then
520-
require("nvim-tree.view").set_cursor({ i + 1, 1 })
520+
require("nvim-tree.view").View:set_cursor({ i + 1, 1 })
521521
break
522522
end
523523

lua/nvim-tree/view.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ end
363363

364364
---@private
365365
function View:grow()
366-
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
366+
local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
367367
local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false)
368368
-- number of columns of right-padding to indicate end of path
369369
local padding = self:get_size(self.padding)
@@ -539,15 +539,15 @@ end
539539

540540
---@param winnr number|nil
541541
---@param open_if_closed boolean|nil
542-
function M.focus(winnr, open_if_closed)
542+
function View:focus(winnr, open_if_closed)
543543
local wnr = winnr or M.get_winnr()
544544

545545
if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then
546-
M.close()
547-
M.View:open()
546+
self:close()
547+
self:open()
548548
wnr = M.get_winnr()
549-
elseif open_if_closed and not M.View:is_visible() then
550-
M.View:open()
549+
elseif open_if_closed and not self:is_visible() then
550+
self:open()
551551
end
552552

553553
if wnr then
@@ -558,22 +558,22 @@ end
558558
--- Retrieve the winid of the open tree.
559559
---@param opts ApiTreeWinIdOpts|nil
560560
---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible
561-
function M.winid(opts)
561+
function View:winid(opts)
562562
local tabpage = opts and opts.tabpage
563563
if tabpage == 0 then
564564
tabpage = vim.api.nvim_get_current_tabpage()
565565
end
566-
if M.View:is_visible({ tabpage = tabpage }) then
566+
if self:is_visible({ tabpage = tabpage }) then
567567
return M.get_winnr(tabpage)
568568
else
569569
return nil
570570
end
571571
end
572572

573573
--- Restores the state of a NvimTree window if it was initialized before.
574-
function M.restore_tab_state()
574+
function View:restore_tab_state()
575575
local tabpage = vim.api.nvim_get_current_tabpage()
576-
M.View:set_cursor(M.View.cursors[tabpage])
576+
self:set_cursor(self.cursors[tabpage])
577577
end
578578

579579
--- Returns the window number for nvim-tree within the tabpage specified
@@ -643,8 +643,8 @@ end
643643

644644
---@param cwd string|nil
645645
---@return boolean
646-
function M.is_root_folder_visible(cwd)
647-
return cwd ~= "/" and not M.View.hide_root_folder
646+
function View:is_root_folder_visible(cwd)
647+
return cwd ~= "/" and not self.hide_root_folder
648648
end
649649

650650
-- used on ColorScheme event

0 commit comments

Comments
 (0)