Skip to content

Commit f972194

Browse files
committed
perf(#3253): extract au find-file, view
1 parent 4c096cd commit f972194

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

lua/nvim-tree.lua

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
local api = require("nvim-tree.api")
2-
local log = require("nvim-tree.log")
3-
local notify = require("nvim-tree.notify")
41
local config = require("nvim-tree.config")
52

63
local M = {}
@@ -19,20 +16,11 @@ end
1916
local function setup_autocommands()
2017
local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true })
2118

22-
-- prevent new opened file from opening in the same window as nvim-tree
2319
vim.api.nvim_create_autocmd("BufWipeout", {
2420
group = augroup_id,
2521
pattern = "NvimTree_*",
2622
callback = function()
27-
if not require("nvim-tree.utils").is_nvim_tree_buf(0) then
28-
return
29-
end
30-
local view = require("nvim-tree.view")
31-
if config.g.actions.open_file.eject then
32-
view._prevent_buffer_override()
33-
else
34-
view.abandon_current_window()
35-
end
23+
require("nvim-tree.view").wipeout()
3624
end,
3725
})
3826

@@ -44,6 +32,7 @@ local function setup_autocommands()
4432
end)
4533
})
4634
end
35+
4736
if config.g.sync_root_with_cwd then
4837
vim.api.nvim_create_autocmd("DirChanged", {
4938
group = augroup_id,
@@ -52,17 +41,12 @@ local function setup_autocommands()
5241
end,
5342
})
5443
end
44+
5545
if config.g.update_focused_file.enable then
5646
vim.api.nvim_create_autocmd("BufEnter", {
5747
group = augroup_id,
5848
callback = function(event)
59-
local exclude = config.g.update_focused_file.exclude
60-
if type(exclude) == "function" and exclude(event) then
61-
return
62-
end
63-
require("nvim-tree.utils").debounce("BufEnter:find_file", config.g.view.debounce_delay, function()
64-
require("nvim-tree.actions.tree.find-file").fn()
65-
end)
49+
require("nvim-tree.actions.tree.find-file").buf_enter(event)
6650
end,
6751
})
6852
end
@@ -97,15 +81,14 @@ local function setup_autocommands()
9781
vim.api.nvim_create_autocmd("DiagnosticChanged", {
9882
group = augroup_id,
9983
callback = function(ev)
100-
log.line("diagnostics", "DiagnosticChanged")
10184
require("nvim-tree.diagnostics").update_lsp(ev)
10285
end,
10386
})
87+
10488
vim.api.nvim_create_autocmd("User", {
10589
group = augroup_id,
10690
pattern = "CocDiagnosticChange",
10791
callback = function()
108-
log.line("diagnostics", "CocDiagnosticChange")
10992
require("nvim-tree.diagnostics").update_coc()
11093
end,
11194
})
@@ -145,6 +128,7 @@ end
145128
function M.purge_all_state()
146129
local view = require("nvim-tree.view")
147130
local core = require("nvim-tree.core")
131+
148132
view.close_all_tabs()
149133
view.abandon_all_windows()
150134
local explorer = core.get_explorer()
@@ -159,8 +143,10 @@ end
159143

160144
---@param config_user? nvim_tree.config user supplied subset of config
161145
function M.setup(config_user)
146+
local log = require("nvim-tree.log")
147+
162148
if vim.fn.has("nvim-0.9") == 0 then
163-
notify.warn("nvim-tree.lua requires Neovim 0.9 or higher")
149+
require("nvim-tree.notify").warn("nvim-tree.lua requires Neovim 0.9 or higher")
164150
return
165151
end
166152

@@ -189,7 +175,7 @@ function M.setup(config_user)
189175
vim.g.NvimTreeSetup = 1
190176
vim.api.nvim_exec_autocmds("User", { pattern = "NvimTreeSetup" })
191177

192-
require("nvim-tree.api.impl").hydrate_post_setup(api)
178+
require("nvim-tree.api.impl").hydrate_post_setup(require("nvim-tree.api"))
193179
end
194180

195181
vim.g.NvimTreeRequired = 1

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local core = require("nvim-tree.core")
22
local lib = require("nvim-tree.lib")
3+
local utils = require("nvim-tree.utils")
34
local view = require("nvim-tree.view")
45
local config = require("nvim-tree.config")
56
local finders_find_file = require("nvim-tree.actions.finders.find-file")
@@ -66,4 +67,15 @@ function M.fn(opts)
6667
finders_find_file.fn(path)
6768
end
6869

70+
---@param event vim.api.keyset.create_autocmd.callback_args
71+
function M.buf_enter(event)
72+
local exclude = config.g.update_focused_file.exclude
73+
if type(exclude) == "function" and exclude(event) then
74+
return
75+
end
76+
utils.debounce("BufEnter:find_file", config.g.view.debounce_delay, function()
77+
M.fn()
78+
end)
79+
end
80+
6981
return M

lua/nvim-tree/diagnostics.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ end
128128
---On disabling LSP, a reset event will be sent for all buffers.
129129
---@param ev table standard event with data.diagnostics populated
130130
function M.update_lsp(ev)
131+
log.line("diagnostics", "DiagnosticChanged")
131132
if not config.g.diagnostics.enable or not ev or not ev.data or not ev.data.diagnostics then
132133
return
133134
end
@@ -174,6 +175,7 @@ end
174175
---Fired on CocDiagnosticChanged events:
175176
---debounced retrieval, cache update, version increment and draw
176177
function M.update_coc()
178+
log.line("diagnostics", "CocDiagnosticChange")
177179
if not config.g.diagnostics.enable then
178180
return
179181
end

lua/nvim-tree/view.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,17 @@ function M.is_width_determined()
521521
return type(view_state.Active.width) ~= "function"
522522
end
523523

524+
---Called on BufWipeout
525+
---Prevent new opened file from opening in the same window as nvim-tree
526+
function M.wipeout()
527+
if not utils.is_nvim_tree_buf(0) then
528+
return
529+
end
530+
if config.g.actions.open_file.eject then
531+
M._prevent_buffer_override()
532+
else
533+
M.abandon_current_window()
534+
end
535+
end
536+
524537
return M

0 commit comments

Comments
 (0)