refactor(#2826): View is an Explorer member

This commit is contained in:
Alexander Courtis
2025-04-21 12:02:19 +10:00
parent 0eb21f66f7
commit 44cb3d2f0a
21 changed files with 170 additions and 114 deletions

View File

@@ -4,7 +4,6 @@ local core = require("nvim-tree.core")
local git = require("nvim-tree.git")
local log = require("nvim-tree.log")
local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")
local node_factory = require("nvim-tree.node.factory")
local DirectoryNode = require("nvim-tree.node.directory")
@@ -20,6 +19,7 @@ local LiveFilter = require("nvim-tree.explorer.live-filter")
local Sorter = require("nvim-tree.explorer.sorter")
local Clipboard = require("nvim-tree.actions.fs.clipboard")
local Renderer = require("nvim-tree.renderer")
local View = require("nvim-tree.view")
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
@@ -35,6 +35,7 @@ local config
---@field sorters Sorter
---@field marks Marks
---@field clipboard Clipboard
---@field view View
local Explorer = RootNode:extend()
---@class Explorer
@@ -64,6 +65,7 @@ function Explorer:new(args)
self.live_filter = LiveFilter({ explorer = self })
self.marks = Marks({ explorer = self })
self.clipboard = Clipboard({ explorer = self })
self.view = View({ explorer = self })
self:create_autocmds()
@@ -84,7 +86,7 @@ function Explorer:create_autocmds()
group = self.augroup_id,
callback = function()
appearance.setup()
view.View:reset_winhl()
self.view:reset_winhl()
self.renderer:draw()
end,
})
@@ -486,7 +488,7 @@ function Explorer:reload_explorer()
local projects = git.reload_all_projects()
self:refresh_nodes(projects)
if view.View:is_visible() then
if self.view:is_visible() then
self.renderer:draw()
end
event_running = false
@@ -508,7 +510,7 @@ end
---nil on no explorer or invalid view win
---@return integer[]|nil
function Explorer:get_cursor_position()
local winnr = view.View:get_winnr()
local winnr = self.view:get_winnr()
if not winnr or not vim.api.nvim_win_is_valid(winnr) then
return
end
@@ -523,7 +525,7 @@ function Explorer:get_node_at_cursor()
return
end
if cursor[1] == 1 and view.View:is_root_folder_visible(core.get_cwd()) then
if cursor[1] == 1 and self.view:is_root_folder_visible(core.get_cwd()) then
return self
end

View File

@@ -1,4 +1,3 @@
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils")
local Class = require("nvim-tree.classic")
@@ -56,14 +55,14 @@ local overlay_bufnr = 0
local overlay_winnr = 0
local function remove_overlay(self)
if view.View.float.enable and view.View.float.quit_on_focus_loss then
if self.explorer.view.float.enable and self.explorer.view.float.quit_on_focus_loss then
-- return to normal nvim-tree float behaviour when filter window is closed
vim.api.nvim_create_autocmd("WinLeave", {
pattern = "NvimTree_*",
group = vim.api.nvim_create_augroup("NvimTree", { clear = false }),
callback = function()
if utils.is_nvim_tree_buf(0) then
view.View:close()
self.explorer.view:close()
end
end,
})
@@ -156,7 +155,7 @@ end
---@return integer
local function calculate_overlay_win_width(self)
local wininfo = vim.fn.getwininfo(view.View:get_winnr())[1]
local wininfo = vim.fn.getwininfo(self.explorer.view:get_winnr())[1]
if wininfo then
return wininfo.width - wininfo.textoff - #self.prefix
@@ -166,7 +165,7 @@ local function calculate_overlay_win_width(self)
end
local function create_overlay(self)
if view.View.float.enable then
if self.explorer.view.float.enable then
-- don't close nvim-tree float when focus is changed to filter window
vim.api.nvim_clear_autocmds({
event = "WinLeave",
@@ -198,13 +197,13 @@ local function create_overlay(self)
end
function LiveFilter:start_filtering()
view.View.live_filter.prev_focused_node = self.explorer:get_node_at_cursor()
self.explorer.view.live_filter.prev_focused_node = self.explorer:get_node_at_cursor()
self.filter = self.filter or ""
self.explorer.renderer:draw()
local row = require("nvim-tree.core").get_nodes_starting_line() - 1
local col = #self.prefix > 0 and #self.prefix - 1 or 1
view.View:set_cursor({ row, col })
self.explorer.view:set_cursor({ row, col })
-- needs scheduling to let the cursor move before initializing the window
vim.schedule(function()
return create_overlay(self)
@@ -213,7 +212,7 @@ end
function LiveFilter:clear_filter()
local node = self.explorer:get_node_at_cursor()
local last_node = view.View.live_filter.prev_focused_node
local last_node = self.explorer.view.live_filter.prev_focused_node
self.filter = nil
reset_filter(self)