Skip to content

Commit 400839b

Browse files
committed
perf(#3253): extract autocmd
1 parent c59fc31 commit 400839b

File tree

2 files changed

+146
-137
lines changed

2 files changed

+146
-137
lines changed

lua/nvim-tree.lua

Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -2,142 +2,6 @@ local config = require("nvim-tree.config")
22

33
local M = {}
44

5-
local function setup_autocommands()
6-
local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true })
7-
8-
vim.api.nvim_create_autocmd("BufWipeout", {
9-
group = augroup_id,
10-
pattern = "NvimTree_*",
11-
callback = function()
12-
require("nvim-tree.view").wipeout()
13-
end,
14-
})
15-
16-
if config.g.tab.sync.open then
17-
vim.api.nvim_create_autocmd("TabEnter", {
18-
group = augroup_id,
19-
callback = vim.schedule_wrap(function()
20-
require("nvim-tree.actions.tree.open").tab_enter()
21-
end)
22-
})
23-
end
24-
25-
if config.g.sync_root_with_cwd then
26-
vim.api.nvim_create_autocmd("DirChanged", {
27-
group = augroup_id,
28-
callback = function()
29-
require("nvim-tree.actions.tree.change-dir").fn(vim.loop.cwd())
30-
end,
31-
})
32-
end
33-
34-
if config.g.update_focused_file.enable then
35-
vim.api.nvim_create_autocmd("BufEnter", {
36-
group = augroup_id,
37-
callback = function(event)
38-
if type(config.g.update_focused_file.exclude) == "function" and config.g.update_focused_file.exclude(event) then
39-
return
40-
end
41-
require("nvim-tree.utils").debounce("BufEnter:find_file", config.g.view.debounce_delay, function()
42-
require("nvim-tree.actions.tree.find-file").fn()
43-
end)
44-
end,
45-
})
46-
end
47-
48-
if config.g.hijack_directories.enable and (config.g.disable_netrw or config.g.hijack_netrw) then
49-
vim.api.nvim_create_autocmd({ "BufEnter", "BufNewFile" }, {
50-
group = augroup_id,
51-
nested = true,
52-
callback = function(data)
53-
local bufname = vim.api.nvim_buf_get_name(data.buf)
54-
if vim.fn.isdirectory(bufname) == 1 then
55-
require("nvim-tree.actions.tree.open").open_on_directory(bufname)
56-
end
57-
end,
58-
})
59-
end
60-
61-
if config.g.view.centralize_selection then
62-
vim.api.nvim_create_autocmd("BufEnter", {
63-
group = augroup_id,
64-
pattern = "NvimTree_*",
65-
callback = vim.schedule_wrap(function()
66-
vim.api.nvim_buf_call(0, function()
67-
local is_term_mode = vim.api.nvim_get_mode().mode == "t"
68-
if is_term_mode then
69-
return
70-
end
71-
vim.cmd([[norm! zz]])
72-
end)
73-
end)
74-
})
75-
end
76-
77-
if config.g.diagnostics.enable then
78-
vim.api.nvim_create_autocmd("DiagnosticChanged", {
79-
group = augroup_id,
80-
callback = function(ev)
81-
require("nvim-tree.diagnostics").update_lsp(ev)
82-
end,
83-
})
84-
85-
vim.api.nvim_create_autocmd("User", {
86-
group = augroup_id,
87-
pattern = "CocDiagnosticChange",
88-
callback = function()
89-
require("nvim-tree.diagnostics").update_coc()
90-
end,
91-
})
92-
end
93-
94-
if config.g.view.float.enable and config.g.view.float.quit_on_focus_loss then
95-
vim.api.nvim_create_autocmd("WinLeave", {
96-
group = augroup_id,
97-
pattern = "NvimTree_*",
98-
callback = function()
99-
if require("nvim-tree.utils").is_nvim_tree_buf(0) then
100-
require("nvim-tree.view").close()
101-
end
102-
end,
103-
})
104-
end
105-
106-
-- Handles event dispatch when tree is closed by `:q`
107-
vim.api.nvim_create_autocmd("WinClosed", {
108-
group = augroup_id,
109-
pattern = "*",
110-
---@param ev vim.api.keyset.create_autocmd.callback_args
111-
callback = function(ev)
112-
if not vim.api.nvim_buf_is_valid(ev.buf) then
113-
return
114-
end
115-
if vim.api.nvim_get_option_value("filetype", { buf = ev.buf }) == "NvimTree" then
116-
require("nvim-tree.events")._dispatch_on_tree_close()
117-
end
118-
end,
119-
})
120-
121-
if config.g.renderer.full_name then
122-
local group = vim.api.nvim_create_augroup("nvim_tree_floating_node", { clear = true })
123-
vim.api.nvim_create_autocmd({ "BufLeave", "CursorMoved" }, {
124-
group = group,
125-
pattern = { "NvimTree_*" },
126-
callback = function()
127-
require("nvim-tree.renderer.components.full-name").hide()
128-
end,
129-
})
130-
131-
vim.api.nvim_create_autocmd({ "CursorMoved" }, {
132-
group = group,
133-
pattern = { "NvimTree_*" },
134-
callback = function()
135-
require("nvim-tree.renderer.components.full-name").show()
136-
end,
137-
})
138-
end
139-
end
140-
1415
---`require("nvim-tree").setup` must be called once to initialise nvim-tree.
1426
---
1437
---Call again to apply a change in configuration without restarting Nvim.
@@ -173,7 +37,7 @@ function M.setup(config_user)
17337
require("nvim-tree.view-state").initialize()
17438

17539
-- idempotent au (re)definition
176-
setup_autocommands()
40+
require("nvim-tree.autocmd").global()
17741

17842
-- subsequent calls to setup clear all state
17943
if vim.g.NvimTreeSetup == 1 then

lua/nvim-tree/autocmd.lua

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
local config = require("nvim-tree.config")
2+
3+
local M = {}
4+
5+
---Create all global autocommands after setup.
6+
---Idempotent: removes existing autocommands first.
7+
---For startup performance reasons, all requires must be done inline during the callback.
8+
---Some short circuiting logic is done directly inside the callback to prevent unnecessary requires.
9+
function M.global()
10+
local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true })
11+
12+
vim.api.nvim_create_autocmd("BufWipeout", {
13+
group = augroup_id,
14+
pattern = "NvimTree_*",
15+
callback = function()
16+
require("nvim-tree.view").wipeout()
17+
end,
18+
})
19+
20+
if config.g.tab.sync.open then
21+
vim.api.nvim_create_autocmd("TabEnter", {
22+
group = augroup_id,
23+
callback = vim.schedule_wrap(function()
24+
require("nvim-tree.actions.tree.open").tab_enter()
25+
end)
26+
})
27+
end
28+
29+
if config.g.sync_root_with_cwd then
30+
vim.api.nvim_create_autocmd("DirChanged", {
31+
group = augroup_id,
32+
callback = function()
33+
require("nvim-tree.actions.tree.change-dir").fn(vim.loop.cwd())
34+
end,
35+
})
36+
end
37+
38+
if config.g.update_focused_file.enable then
39+
vim.api.nvim_create_autocmd("BufEnter", {
40+
group = augroup_id,
41+
callback = function(event)
42+
if type(config.g.update_focused_file.exclude) == "function" and config.g.update_focused_file.exclude(event) then
43+
return
44+
end
45+
require("nvim-tree.utils").debounce("BufEnter:find_file", config.g.view.debounce_delay, function()
46+
require("nvim-tree.actions.tree.find-file").fn()
47+
end)
48+
end,
49+
})
50+
end
51+
52+
if config.g.hijack_directories.enable and (config.g.disable_netrw or config.g.hijack_netrw) then
53+
vim.api.nvim_create_autocmd({ "BufEnter", "BufNewFile" }, {
54+
group = augroup_id,
55+
nested = true,
56+
callback = function(data)
57+
local bufname = vim.api.nvim_buf_get_name(data.buf)
58+
if vim.fn.isdirectory(bufname) == 1 then
59+
require("nvim-tree.actions.tree.open").open_on_directory(bufname)
60+
end
61+
end,
62+
})
63+
end
64+
65+
if config.g.view.centralize_selection then
66+
vim.api.nvim_create_autocmd("BufEnter", {
67+
group = augroup_id,
68+
pattern = "NvimTree_*",
69+
callback = vim.schedule_wrap(function()
70+
vim.api.nvim_buf_call(0, function()
71+
local is_term_mode = vim.api.nvim_get_mode().mode == "t"
72+
if is_term_mode then
73+
return
74+
end
75+
vim.cmd([[norm! zz]])
76+
end)
77+
end)
78+
})
79+
end
80+
81+
if config.g.diagnostics.enable then
82+
vim.api.nvim_create_autocmd("DiagnosticChanged", {
83+
group = augroup_id,
84+
callback = function(ev)
85+
require("nvim-tree.diagnostics").update_lsp(ev)
86+
end,
87+
})
88+
89+
vim.api.nvim_create_autocmd("User", {
90+
group = augroup_id,
91+
pattern = "CocDiagnosticChange",
92+
callback = function()
93+
require("nvim-tree.diagnostics").update_coc()
94+
end,
95+
})
96+
end
97+
98+
if config.g.view.float.enable and config.g.view.float.quit_on_focus_loss then
99+
vim.api.nvim_create_autocmd("WinLeave", {
100+
group = augroup_id,
101+
pattern = "NvimTree_*",
102+
callback = function()
103+
if require("nvim-tree.utils").is_nvim_tree_buf(0) then
104+
require("nvim-tree.view").close()
105+
end
106+
end,
107+
})
108+
end
109+
110+
-- Handles event dispatch when tree is closed by `:q`
111+
vim.api.nvim_create_autocmd("WinClosed", {
112+
group = augroup_id,
113+
pattern = "*",
114+
---@param ev vim.api.keyset.create_autocmd.callback_args
115+
callback = function(ev)
116+
if not vim.api.nvim_buf_is_valid(ev.buf) then
117+
return
118+
end
119+
if vim.api.nvim_get_option_value("filetype", { buf = ev.buf }) == "NvimTree" then
120+
require("nvim-tree.events")._dispatch_on_tree_close()
121+
end
122+
end,
123+
})
124+
125+
if config.g.renderer.full_name then
126+
local group = vim.api.nvim_create_augroup("nvim_tree_floating_node", { clear = true })
127+
vim.api.nvim_create_autocmd({ "BufLeave", "CursorMoved" }, {
128+
group = group,
129+
pattern = { "NvimTree_*" },
130+
callback = function()
131+
require("nvim-tree.renderer.components.full-name").hide()
132+
end,
133+
})
134+
135+
vim.api.nvim_create_autocmd({ "CursorMoved" }, {
136+
group = group,
137+
pattern = { "NvimTree_*" },
138+
callback = function()
139+
require("nvim-tree.renderer.components.full-name").show()
140+
end,
141+
})
142+
end
143+
end
144+
145+
return M

0 commit comments

Comments
 (0)