move many autocommands into Explorer
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
local log = require("nvim-tree.log")
|
||||
local appearance = require("nvim-tree.appearance")
|
||||
local view = require("nvim-tree.view")
|
||||
local utils = require("nvim-tree.utils")
|
||||
local actions = require("nvim-tree.actions")
|
||||
@@ -151,19 +150,6 @@ local function setup_autocommands(opts)
|
||||
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
|
||||
end
|
||||
|
||||
-- reset and draw (highlights) when colorscheme is changed
|
||||
create_nvim_tree_autocmd("ColorScheme", {
|
||||
callback = function()
|
||||
appearance.setup()
|
||||
view.reset_winhl()
|
||||
|
||||
local explorer = core.get_explorer()
|
||||
if explorer then
|
||||
explorer.renderer:draw()
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
-- prevent new opened file from opening in the same window as nvim-tree
|
||||
create_nvim_tree_autocmd("BufWipeout", {
|
||||
pattern = "NvimTree_*",
|
||||
@@ -179,79 +165,9 @@ local function setup_autocommands(opts)
|
||||
end,
|
||||
})
|
||||
|
||||
create_nvim_tree_autocmd("BufWritePost", {
|
||||
callback = function()
|
||||
if opts.auto_reload_on_write and not opts.filesystem_watchers.enable then
|
||||
local explorer = core.get_explorer()
|
||||
if explorer then
|
||||
explorer:reload_explorer()
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
create_nvim_tree_autocmd("BufReadPost", {
|
||||
callback = function(data)
|
||||
-- update opened file buffers
|
||||
local explorer = core.get_explorer()
|
||||
if not explorer then
|
||||
return
|
||||
end
|
||||
if
|
||||
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
|
||||
then
|
||||
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
|
||||
explorer:reload_explorer()
|
||||
end)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
create_nvim_tree_autocmd("BufUnload", {
|
||||
callback = function(data)
|
||||
-- update opened file buffers
|
||||
local explorer = core.get_explorer()
|
||||
if not explorer then
|
||||
return
|
||||
end
|
||||
if
|
||||
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
|
||||
then
|
||||
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
|
||||
explorer:reload_explorer()
|
||||
end)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
create_nvim_tree_autocmd("User", {
|
||||
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
|
||||
callback = function()
|
||||
if not opts.filesystem_watchers.enable and opts.git.enable then
|
||||
local explorer = core.get_explorer()
|
||||
if explorer then
|
||||
explorer:reload_git()
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
if opts.tab.sync.open then
|
||||
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) })
|
||||
end
|
||||
if opts.hijack_cursor then
|
||||
create_nvim_tree_autocmd("CursorMoved", {
|
||||
pattern = "NvimTree_*",
|
||||
callback = function()
|
||||
if utils.is_nvim_tree_buf(0) then
|
||||
local explorer = core.get_explorer()
|
||||
if explorer then
|
||||
explorer:place_cursor_on_node()
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
if opts.sync_root_with_cwd then
|
||||
create_nvim_tree_autocmd("DirChanged", {
|
||||
callback = function()
|
||||
@@ -277,20 +193,6 @@ local function setup_autocommands(opts)
|
||||
create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory })
|
||||
end
|
||||
|
||||
create_nvim_tree_autocmd("BufEnter", {
|
||||
pattern = "NvimTree_*",
|
||||
callback = function()
|
||||
if utils.is_nvim_tree_buf(0) then
|
||||
if vim.fn.getcwd() ~= core.get_cwd() or (opts.reload_on_bufenter and not opts.filesystem_watchers.enable) then
|
||||
local explorer = core.get_explorer()
|
||||
if explorer then
|
||||
explorer:reload_explorer()
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
if opts.view.centralize_selection then
|
||||
create_nvim_tree_autocmd("BufEnter", {
|
||||
pattern = "NvimTree_*",
|
||||
@@ -330,20 +232,6 @@ local function setup_autocommands(opts)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
if opts.modified.enable then
|
||||
create_nvim_tree_autocmd({ "BufModifiedSet", "BufWritePost" }, {
|
||||
callback = function()
|
||||
utils.debounce("Buf:modified", opts.view.debounce_delay, function()
|
||||
require("nvim-tree.buffers").reload_modified()
|
||||
local explorer = core.get_explorer()
|
||||
if explorer then
|
||||
explorer:reload_explorer()
|
||||
end
|
||||
end)
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
||||
@@ -839,6 +727,7 @@ function M.setup(conf)
|
||||
require("nvim-tree.appearance").setup()
|
||||
require("nvim-tree.diagnostics").setup(opts)
|
||||
require("nvim-tree.explorer"):setup(opts)
|
||||
require("nvim-tree.explorer.watch").setup(opts)
|
||||
require("nvim-tree.git").setup(opts)
|
||||
require("nvim-tree.git.utils").setup(opts)
|
||||
require("nvim-tree.view").setup(opts)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
local appearance = require("nvim-tree.appearance")
|
||||
local buffers = require("nvim-tree.buffers")
|
||||
local core = require("nvim-tree.core")
|
||||
local git = require("nvim-tree.git")
|
||||
local log = require("nvim-tree.log")
|
||||
@@ -25,7 +27,9 @@ local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
|
||||
local config
|
||||
|
||||
---@class (exact) Explorer: RootNode
|
||||
---@field uid_explorer number vim.uv.hrtime() at construction time
|
||||
---@field opts table user options
|
||||
---@field augroup_id integer
|
||||
---@field renderer Renderer
|
||||
---@field filters Filters
|
||||
---@field live_filter LiveFilter
|
||||
@@ -59,6 +63,9 @@ function Explorer:create(path)
|
||||
|
||||
o.explorer = o
|
||||
|
||||
o.uid_explorer = vim.uv.hrtime()
|
||||
o.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. o.uid_explorer, {})
|
||||
|
||||
o.open = true
|
||||
o.opts = config
|
||||
|
||||
@@ -69,11 +76,111 @@ function Explorer:create(path)
|
||||
o.marks = Marks:new(config, o)
|
||||
o.clipboard = Clipboard:new(config, o)
|
||||
|
||||
o:create_autocmds()
|
||||
|
||||
o:_load(o)
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
function Explorer:destroy()
|
||||
log.line("dev", "Explorer:destroy")
|
||||
|
||||
vim.api.nvim_del_augroup_by_id(self.augroup_id)
|
||||
|
||||
RootNode.destroy(self)
|
||||
end
|
||||
|
||||
function Explorer:create_autocmds()
|
||||
-- reset and draw (highlights) when colorscheme is changed
|
||||
vim.api.nvim_create_autocmd("ColorScheme", {
|
||||
group = self.augroup_id,
|
||||
callback = function()
|
||||
appearance.setup()
|
||||
view.reset_winhl()
|
||||
self:draw()
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd("BufWritePost", {
|
||||
group = self.augroup_id,
|
||||
callback = function()
|
||||
if self.opts.auto_reload_on_write and not self.opts.filesystem_watchers.enable then
|
||||
self:reload_explorer()
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd("BufReadPost", {
|
||||
group = self.augroup_id,
|
||||
callback = function(data)
|
||||
if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
|
||||
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
|
||||
self:reload_explorer()
|
||||
end)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
-- update opened file buffers
|
||||
vim.api.nvim_create_autocmd("BufUnload", {
|
||||
group = self.augroup_id,
|
||||
callback = function(data)
|
||||
if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
|
||||
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
|
||||
self:reload_explorer()
|
||||
end)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd("BufEnter", {
|
||||
group = self.augroup_id,
|
||||
pattern = "NvimTree_*",
|
||||
callback = function()
|
||||
if utils.is_nvim_tree_buf(0) then
|
||||
if vim.fn.getcwd() ~= core.get_cwd() or (self.opts.reload_on_bufenter and not self.opts.filesystem_watchers.enable) then
|
||||
self:reload_explorer()
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd("User", {
|
||||
group = self.augroup_id,
|
||||
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
|
||||
callback = function()
|
||||
if not self.opts.filesystem_watchers.enable and self.opts.git.enable then
|
||||
self:reload_git()
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
if self.opts.hijack_cursor then
|
||||
vim.api.nvim_create_autocmd("CursorMoved", {
|
||||
group = self.augroup_id,
|
||||
pattern = "NvimTree_*",
|
||||
callback = function()
|
||||
if utils.is_nvim_tree_buf(0) then
|
||||
self:place_cursor_on_node()
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
if self.opts.modified.enable then
|
||||
vim.api.nvim_create_autocmd({ "BufModifiedSet", "BufWritePost" }, {
|
||||
group = self.augroup_id,
|
||||
callback = function()
|
||||
utils.debounce("Buf:modified_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
|
||||
buffers.reload_modified()
|
||||
self:reload_explorer()
|
||||
end)
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
---@param node DirectoryNode
|
||||
function Explorer:expand(node)
|
||||
self:_load(node)
|
||||
@@ -430,7 +537,6 @@ end
|
||||
|
||||
function Explorer:setup(opts)
|
||||
config = opts
|
||||
require("nvim-tree.explorer.watch").setup(opts)
|
||||
end
|
||||
|
||||
return Explorer
|
||||
|
||||
@@ -32,6 +32,10 @@ function DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name
|
||||
return o
|
||||
end
|
||||
|
||||
function DirectoryLinkNode:destroy()
|
||||
DirectoryNode.destroy(self)
|
||||
end
|
||||
|
||||
-----Update the directory GitStatus of link target and the file status of the link itself
|
||||
-----@param parent_ignored boolean
|
||||
-----@param status table|nil
|
||||
|
||||
@@ -28,6 +28,10 @@ function FileLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_
|
||||
return o
|
||||
end
|
||||
|
||||
function FileLinkNode:destroy()
|
||||
FileNode.destroy(self)
|
||||
end
|
||||
|
||||
-----Update the GitStatus of the target otherwise the link itself
|
||||
-----@param parent_ignored boolean
|
||||
-----@param status table|nil
|
||||
|
||||
@@ -36,6 +36,10 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
|
||||
return o
|
||||
end
|
||||
|
||||
function FileNode:destroy()
|
||||
Node.destroy(self)
|
||||
end
|
||||
|
||||
---Update the GitStatus of the file
|
||||
---@param parent_ignored boolean
|
||||
---@param status table|nil
|
||||
|
||||
@@ -23,4 +23,8 @@ function RootNode:is_dotfile()
|
||||
return false
|
||||
end
|
||||
|
||||
function RootNode:destroy()
|
||||
DirectoryNode.destroy(self)
|
||||
end
|
||||
|
||||
return RootNode
|
||||
|
||||
Reference in New Issue
Block a user