Skip to content

Commit 3d385d3

Browse files
authored
perf(#3257): remove setup for modules: buffers, diagnostics, git, help, keymap, lib, watcher (#3295)
* perf(#3257): remove keymap setup * perf(#3257): remove diagnostics setup * perf(#3257): remove diagnostics setup * perf(#3257): remove help setup * perf(#3257): remove help setup * perf(#3257): remove watcher setup * perf(#3257): remove buffers setup * perf(#3257): remove lib setup * perf(#3257): remove git setup * perf(#3257): remove git utils setup * perf(#3257): remove buffers setup
1 parent f37ebac commit 3d385d3

File tree

12 files changed

+102
-126
lines changed

12 files changed

+102
-126
lines changed

doc/nvim-tree-lua.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1915,9 +1915,14 @@ Config: bookmarks *nvim-tree-config-bookmarks*
19151915
Config: help *nvim-tree-config-help*
19161916

19171917
*nvim_tree.config.help*
1918+
{sort_by} *nvim_tree.config.help.sort_by*
1919+
1920+
Sort help entries alphabetically by:
1921+
`"key"`: map lhs
1922+
`"desc"`: description
19181923

19191924
Fields: ~
1920-
• {sort_by}? (`"key"|"desc"`, default: `"key"`) Alphabetically.
1925+
• {sort_by}? (`nvim_tree.config.help.sort_by`) (default: `"key"`)
19211926

19221927

19231928

lua/nvim-tree.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,11 @@ function M.setup(config_user)
285285
end
286286

287287
require("nvim-tree.actions").setup(config.g)
288-
require("nvim-tree.keymap").setup(config.g)
289288
require("nvim-tree.appearance").setup()
290-
require("nvim-tree.diagnostics").setup(config.g)
291289
require("nvim-tree.explorer"):setup(config.g)
292290
require("nvim-tree.explorer.watch").setup(config.g)
293-
require("nvim-tree.git").setup(config.g)
294-
require("nvim-tree.git.utils").setup(config.g)
295291
require("nvim-tree.view").setup(config.g)
296-
require("nvim-tree.lib").setup(config.g)
297292
require("nvim-tree.renderer.components").setup(config.g)
298-
require("nvim-tree.buffers").setup(config.g)
299-
require("nvim-tree.help").setup(config.g)
300-
require("nvim-tree.watcher").setup(config.g)
301293

302294
setup_autocommands()
303295

lua/nvim-tree/_meta/config/help.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ error("Cannot require a meta file")
33

44

55

6+
---{sort_by} [nvim_tree.config.help.sort_by]()
7+
---
8+
---Sort help entries alphabetically by:
9+
---- `"key"`: map lhs
10+
---- `"desc"`: description
11+
---@alias nvim_tree.config.help.sort_by "key"|"desc"
612
---
713
---@class nvim_tree.config.help
814
---
9-
---Alphabetically.
1015
---(default: `"key"`)
11-
---@field sort_by? "key"|"desc"
16+
---@field sort_by? nvim_tree.config.help.sort_by

lua/nvim-tree/buffers.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local config = require("nvim-tree.config")
2+
13
local DirectoryNode = require("nvim-tree.node.directory")
24

35
local M = {}
@@ -26,7 +28,7 @@ end
2628
---@param node Node
2729
---@return boolean
2830
function M.is_modified(node)
29-
if not M.config.modified.enable then
31+
if not config.g.modified.enable then
3032
return false
3133
end
3234

@@ -36,11 +38,11 @@ function M.is_modified(node)
3638

3739
local dir = node:as(DirectoryNode)
3840
if dir then
39-
if not M.config.modified.show_on_dirs then
41+
if not config.g.modified.show_on_dirs then
4042
return false
4143
end
4244

43-
if dir.open and not M.config.modified.show_on_open_dirs then
45+
if dir.open and not config.g.modified.show_on_open_dirs then
4446
return false
4547
end
4648
end
@@ -55,11 +57,4 @@ function M.is_opened(node)
5557
return node and vim.fn.bufloaded(node.absolute_path) > 0
5658
end
5759

58-
---@param opts nvim_tree.config
59-
function M.setup(opts)
60-
M.config = {
61-
modified = opts.modified,
62-
}
63-
end
64-
6560
return M

lua/nvim-tree/config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ local M = {
1111
---@type nvim_tree.config immutable default config
1212
d = {},
1313

14-
---@type nvim_tree.config? global current config, nil until setup called
14+
---@type nvim_tree.config? global current config, nil until setup called, mutable
1515
g = nil,
1616

17-
---@type nvim_tree.config? raw user config, nil when no user config passed to setup
17+
---@type nvim_tree.config? immutable raw user config, nil when no user config passed to setup
1818
u = nil,
1919
}
2020

lua/nvim-tree/diagnostics.lua

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local config = require("nvim-tree.config")
12
local core = require("nvim-tree.core")
23
local utils = require("nvim-tree.utils")
34
local view = require("nvim-tree.view")
@@ -38,11 +39,16 @@ local function uniformize_path(path)
3839
end
3940

4041
---Severity is within diagnostics.severity.min, diagnostics.severity.max
41-
---@param severity lsp.DiagnosticSeverity
42-
---@param config table
42+
---Alway in range when using vim.diagnostic.Opts:
43+
---@see nvim_tree.config.diagnostics.diagnostic_opts
44+
---@param severity vim.diagnostic.Severity
4345
---@return boolean
44-
local function is_severity_in_range(severity, config)
45-
return config.max <= severity and severity <= config.min
46+
local function is_severity_in_range(severity)
47+
if config.g.diagnostics.diagnostic_opts then
48+
return true
49+
else
50+
return severity >= config.g.diagnostics.severity.max and severity <= config.g.diagnostics.severity.min
51+
end
4652
end
4753

4854
---Handle any COC exceptions, preventing any propagation
@@ -86,7 +92,7 @@ local function from_coc()
8692
local bufname = uniformize_path(diagnostic.file)
8793
local coc_severity = COC_SEVERITY_LEVELS[diagnostic.severity]
8894
local highest_severity = buffer_severity[bufname] or coc_severity
89-
if is_severity_in_range(highest_severity, M.severity) then
95+
if is_severity_in_range(highest_severity) then
9096
buffer_severity[bufname] = math.min(highest_severity, coc_severity)
9197
end
9298
end
@@ -122,7 +128,7 @@ end
122128
---On disabling LSP, a reset event will be sent for all buffers.
123129
---@param ev table standard event with data.diagnostics populated
124130
function M.update_lsp(ev)
125-
if not M.enable or not ev or not ev.data or not ev.data.diagnostics then
131+
if not config.g.diagnostics.enable or not ev or not ev.data or not ev.data.diagnostics then
126132
return
127133
end
128134

@@ -138,7 +144,7 @@ function M.update_lsp(ev)
138144

139145
-- most severe (lowest) severity in user range
140146
for _, diagnostic in ipairs(diagnostics) do
141-
if diagnostic.severity >= M.severity.max and diagnostic.severity <= M.severity.min then
147+
if is_severity_in_range(diagnostic.severity) then
142148
if not new_severity or diagnostic.severity < new_severity then
143149
new_severity = diagnostic.severity
144150
end
@@ -150,7 +156,7 @@ function M.update_lsp(ev)
150156
NODE_SEVERITIES[bufname] = new_severity
151157
NODE_SEVERITIES_VERSION = NODE_SEVERITIES_VERSION + 1
152158

153-
utils.debounce("DiagnosticChanged redraw", M.debounce_delay, function()
159+
utils.debounce("DiagnosticChanged redraw", config.g.diagnostics.debounce_delay, function()
154160
local profile_redraw = log.profile_start("DiagnosticChanged redraw")
155161

156162
local explorer = core.get_explorer()
@@ -168,10 +174,10 @@ end
168174
---Fired on CocDiagnosticChanged events:
169175
---debounced retrieval, cache update, version increment and draw
170176
function M.update_coc()
171-
if not M.enable then
177+
if not config.g.diagnostics.enable then
172178
return
173179
end
174-
utils.debounce("CocDiagnosticChanged update", M.debounce_delay, function()
180+
utils.debounce("CocDiagnosticChanged update", config.g.diagnostics.debounce_delay, function()
175181
local profile = log.profile_start("CocDiagnosticChanged update")
176182
NODE_SEVERITIES = from_coc()
177183
NODE_SEVERITIES_VERSION = NODE_SEVERITIES_VERSION + 1
@@ -198,12 +204,12 @@ end
198204
---@param node Node
199205
---@return DiagStatus|nil
200206
function M.get_diag_status(node)
201-
if not M.enable then
207+
if not config.g.diagnostics.enable then
202208
return nil
203209
end
204210

205211
-- dir but we shouldn't show on dirs at all
206-
if node:is(DirectoryNode) and not M.show_on_dirs then
212+
if node:is(DirectoryNode) and not config.g.diagnostics.show_on_dirs then
207213
return nil
208214
end
209215

@@ -222,26 +228,10 @@ function M.get_diag_status(node)
222228
end
223229

224230
-- dir is closed or we should show on open_dirs
225-
if not dir.open or M.show_on_open_dirs then
231+
if not dir.open or config.g.diagnostics.show_on_open_dirs then
226232
return node.diag_status
227233
end
228234
return nil
229235
end
230236

231-
function M.setup(opts)
232-
M.enable = opts.diagnostics.enable
233-
M.debounce_delay = opts.diagnostics.debounce_delay
234-
M.severity = opts.diagnostics.diagnostic_opts and {
235-
min = vim.diagnostic.severity.HINT,
236-
max = vim.diagnostic.severity.ERROR
237-
} or opts.diagnostics.severity
238-
239-
if M.enable then
240-
log.line("diagnostics", "setup")
241-
end
242-
243-
M.show_on_dirs = opts.diagnostics.show_on_dirs
244-
M.show_on_open_dirs = opts.diagnostics.show_on_open_dirs
245-
end
246-
247237
return M

lua/nvim-tree/git/init.lua

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local log = require("nvim-tree.log")
22
local utils = require("nvim-tree.utils")
3+
local config = require("nvim-tree.config")
34
local git_utils = require("nvim-tree.git.utils")
45

56
local GitRunner = require("nvim-tree.git.runner")
@@ -88,7 +89,7 @@ end
8889

8990
---@return nvim_tree.git.Project[] maybe empty
9091
function M.reload_all_projects()
91-
if not M.config.git.enable then
92+
if not config.g.git.enable then
9293
return {}
9394
end
9495

@@ -106,7 +107,7 @@ end
106107
function M.reload_project(toplevel, path, callback)
107108
local project = M._projects_by_toplevel[toplevel] --[[@as nvim_tree.git.Project]]
108109

109-
if not toplevel or not project or not M.config.git.enable then
110+
if not toplevel or not project or not config.g.git.enable then
110111
if callback then
111112
callback()
112113
end
@@ -126,7 +127,7 @@ function M.reload_project(toplevel, path, callback)
126127
path = path,
127128
list_untracked = git_utils.should_show_untracked(toplevel),
128129
list_ignored = true,
129-
timeout = M.config.git.timeout,
130+
timeout = config.g.git.timeout,
130131
}
131132

132133
if callback then
@@ -161,7 +162,7 @@ function M.get_toplevel(path)
161162
return nil
162163
end
163164

164-
if not M.config.git.enable then
165+
if not config.g.git.enable then
165166
return nil
166167
end
167168

@@ -194,15 +195,15 @@ function M.get_toplevel(path)
194195
local toplevel_norm = vim.fn.fnamemodify(toplevel, ":p")
195196

196197
-- ignore disabled paths
197-
if type(M.config.git.disable_for_dirs) == "table" then
198-
for _, disabled_for_dir in ipairs(M.config.git.disable_for_dirs) do
198+
if type(config.g.git.disable_for_dirs) == "table" then
199+
for _, disabled_for_dir in ipairs(config.g.git.disable_for_dirs --[[@as string[] ]]) do
199200
local disabled_norm = vim.fn.fnamemodify(disabled_for_dir, ":p")
200201
if toplevel_norm == disabled_norm then
201202
return nil
202203
end
203204
end
204-
elseif type(M.config.git.disable_for_dirs) == "function" then
205-
if M.config.git.disable_for_dirs(toplevel_norm) then
205+
elseif type(config.g.git.disable_for_dirs) == "function" then
206+
if config.g.git.disable_for_dirs(toplevel_norm) then
206207
return nil
207208
end
208209
end
@@ -220,7 +221,7 @@ function M.get_toplevel(path)
220221
end
221222

222223
local function reload_tree_at(toplevel)
223-
if not M.config.git.enable or not toplevel then
224+
if not config.g.git.enable or not toplevel then
224225
return nil
225226
end
226227

@@ -259,7 +260,7 @@ end
259260
---@param path string absolute
260261
---@return nvim_tree.git.Project maybe empty
261262
function M.load_project(path)
262-
if not M.config.git.enable then
263+
if not config.g.git.enable then
263264
return {}
264265
end
265266

@@ -278,17 +279,17 @@ function M.load_project(path)
278279
toplevel = toplevel,
279280
list_untracked = git_utils.should_show_untracked(toplevel),
280281
list_ignored = true,
281-
timeout = M.config.git.timeout,
282+
timeout = config.g.git.timeout,
282283
})
283284

284285
local watcher = nil
285-
if M.config.filesystem_watchers.enable then
286+
if config.g.filesystem_watchers.enable then
286287
log.line("watcher", "git start")
287288

288289
---@param w Watcher
289290
local callback = function(w)
290291
log.line("watcher", "git event scheduled '%s'", w.data.toplevel)
291-
utils.debounce("git:watcher:" .. w.data.toplevel, M.config.filesystem_watchers.debounce_delay, function()
292+
utils.debounce("git:watcher:" .. w.data.toplevel, config.g.filesystem_watchers.debounce_delay, function()
292293
if w.destroyed then
293294
return
294295
end
@@ -403,12 +404,7 @@ end
403404
function M.disable_git_integration()
404405
log.line("git", "disabling git integration")
405406
M.purge_state()
406-
M.config.git.enable = false
407-
end
408-
409-
function M.setup(opts)
410-
M.config.git = opts.git
411-
M.config.filesystem_watchers = opts.filesystem_watchers
407+
config.g.git.enable = false
412408
end
413409

414410
return M

lua/nvim-tree/git/utils.lua

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
local log = require("nvim-tree.log")
22
local utils = require("nvim-tree.utils")
3+
local config = require("nvim-tree.config")
34

4-
local M = {
5-
use_cygpath = false,
6-
}
5+
local M = {}
6+
7+
---@type boolean?
8+
local use_cygpath_cached = nil
9+
10+
---true when cygwin enabled and present
11+
---@return boolean
12+
local function use_cygpath()
13+
if use_cygpath_cached == nil then
14+
if config.g.git.cygwin_support then
15+
use_cygpath_cached = vim.fn.executable("cygpath") == 1
16+
else
17+
use_cygpath_cached = false
18+
end
19+
end
20+
return use_cygpath_cached
21+
end
722

823
--- Execute system command
924
---@param cmd string[]
1025
---@return string stdout
1126
---@return integer exit code
1227
local function system(cmd)
1328
if vim.fn.has("nvim-0.10") == 1 then
14-
local obj = vim.system(cmd):wait(M.opts.git.timeout)
29+
local obj = vim.system(cmd):wait(config.g.git.timeout)
1530
return obj.stdout or "", obj.code
1631
else
1732
return vim.fn.system(cmd), vim.v.shell_error
@@ -50,7 +65,7 @@ function M.get_toplevel(cwd)
5065
if vim.fn.has("win32") == 1 then
5166
-- msys2 git support
5267
-- cygpath calls must in array format to avoid shell compatibility issues
53-
if M.use_cygpath then
68+
if use_cygpath() then
5469
toplevel = vim.fn.system({ "cygpath", "-w", toplevel })
5570
if vim.v.shell_error ~= 0 then
5671
return nil, nil
@@ -195,11 +210,4 @@ function M.git_status_dir(parent_ignored, project, path, path_fallback)
195210
return ns
196211
end
197212

198-
function M.setup(opts)
199-
if opts.git.cygwin_support then
200-
M.use_cygpath = vim.fn.executable("cygpath") == 1
201-
end
202-
M.opts = opts
203-
end
204-
205213
return M

0 commit comments

Comments
 (0)