Skip to content

Commit 9184bb4

Browse files
committed
perf(#3253): extract purge_all_state and document
1 parent d50a205 commit 9184bb4

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

lua/nvim-tree.lua

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,55 +114,53 @@ local function setup_autocommands()
114114
require("nvim-tree.renderer.components.full-name").setup_autocommands()
115115
end
116116

117-
function M.purge_all_state()
118-
local view = require("nvim-tree.view")
119-
local core = require("nvim-tree.core")
120-
121-
view.close_all_tabs()
122-
view.abandon_all_windows()
123-
local explorer = core.get_explorer()
124-
if explorer then
125-
require("nvim-tree.git").purge_state()
126-
explorer:destroy()
127-
core.reset_explorer()
128-
end
129-
-- purge orphaned that were not destroyed by their nodes
130-
require("nvim-tree.watcher").purge_watchers()
131-
end
132-
133-
---@param config_user? nvim_tree.config user supplied subset of config
117+
---`require("nvim-tree").setup` must be called once to initialise nvim-tree.
118+
---
119+
---Call again to apply a change in configuration without restarting Nvim.
120+
---
121+
---See :help nvim-tree-setup
122+
---
123+
---@param config_user? nvim_tree.config subset, uses defaults when nil
134124
function M.setup(config_user)
135125
local log = require("nvim-tree.log")
136126

127+
-- Nvim version check
137128
if vim.fn.has("nvim-0.9") == 0 then
138129
require("nvim-tree.notify").warn("nvim-tree.lua requires Neovim 0.9 or higher")
139130
return
140131
end
141132

133+
-- validate and merge with defaults as config.g
142134
config.setup(config_user)
143135

136+
-- optionally create the log file
144137
log.start()
145138

139+
-- optionally log the configuration
146140
if log.enabled("config") then
147141
log.line("config", "default config + user")
148142
log.raw("config", "%s\n", vim.inspect(config.g))
149143
end
150144

145+
-- idempotent highlight definition
151146
require("nvim-tree.appearance").highlight()
152147

148+
-- set the initial view state based on config
153149
require("nvim-tree.view-state").initialize()
154150

151+
-- idempotent au (re)definition
155152
setup_autocommands()
156153

154+
-- subsequent calls to setup clear all state
157155
if vim.g.NvimTreeSetup == 1 then
158-
-- subsequent calls to setup
159-
M.purge_all_state()
156+
require("nvim-tree.core").purge_all_state()
160157
end
161158

159+
-- hydrate post setup API
160+
require("nvim-tree.api.impl").hydrate_post_setup(require("nvim-tree.api"))
161+
162162
vim.g.NvimTreeSetup = 1
163163
vim.api.nvim_exec_autocmds("User", { pattern = "NvimTreeSetup" })
164-
165-
require("nvim-tree.api.impl").hydrate_post_setup(require("nvim-tree.api"))
166164
end
167165

168166
vim.g.NvimTreeRequired = 1

lua/nvim-tree/core.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local events = require("nvim-tree.events")
22
local notify = require("nvim-tree.notify")
33
local view = require("nvim-tree.view")
44
local log = require("nvim-tree.log")
5+
local git = require("nvim-tree.git")
6+
local watcher = require("nvim-tree.watcher")
57

68
local M = {}
79

@@ -64,4 +66,16 @@ function M.get_nodes_starting_line()
6466
return offset
6567
end
6668

69+
function M.purge_all_state()
70+
view.close_all_tabs()
71+
view.abandon_all_windows()
72+
if TreeExplorer then
73+
git.purge_state()
74+
TreeExplorer:destroy()
75+
M.reset_explorer()
76+
end
77+
-- purge orphaned that were not destroyed by their nodes
78+
watcher.purge_watchers()
79+
end
80+
6781
return M

lua/nvim-tree/watcher.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ M.Watcher = Watcher
238238
function M.disable_watchers(msg)
239239
notify.warn(string.format("Disabling watchers: %s", msg))
240240
config.g.filesystem_watchers.enable = false
241-
require("nvim-tree").purge_all_state()
241+
require("nvim-tree.core").purge_all_state()
242242
end
243243

244244
function M.purge_watchers()

0 commit comments

Comments
 (0)