Skip to content

Commit b0abdf2

Browse files
committed
perf(#3253): extract purge_all_state and document
1 parent 5c63c35 commit b0abdf2

3 files changed

Lines changed: 29 additions & 22 deletions

File tree

lua/nvim-tree.lua

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,53 +114,46 @@ 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+
---Call again to apply a change in configuration without restarting Nvim.
119+
---@param config_user? nvim_tree.config subset, uses defaults when nil
134120
function M.setup(config_user)
135121
local log = require("nvim-tree.log")
136122

123+
-- Nvim version check
137124
if vim.fn.has("nvim-0.9") == 0 then
138125
require("nvim-tree.notify").warn("nvim-tree.lua requires Neovim 0.9 or higher")
139126
return
140127
end
141128

129+
-- validate and merge with defaults as config.g
142130
config.setup(config_user)
143131

132+
-- optionally create the log file
144133
require("nvim-tree.log").start()
145134

135+
-- optionally log the configuration
146136
if log.enabled("config") then
147137
log.line("config", "default config + user")
148138
log.raw("config", "%s\n", vim.inspect(config.g))
149139
end
150140

141+
-- idempotent highlight definition
151142
require("nvim-tree.appearance").set_hl()
152143

144+
-- idempotent au (re)definition
153145
setup_autocommands()
154146

147+
-- subsequent calls to setup clear all state
155148
if vim.g.NvimTreeSetup == 1 then
156-
-- subsequent calls to setup
157-
M.purge_all_state()
149+
require("nvim-tree.core").purge_all_state()
158150
end
159151

152+
-- hydrate post setup API
153+
require("nvim-tree.api.impl").hydrate_post_setup(require("nvim-tree.api"))
154+
160155
vim.g.NvimTreeSetup = 1
161156
vim.api.nvim_exec_autocmds("User", { pattern = "NvimTreeSetup" })
162-
163-
require("nvim-tree.api.impl").hydrate_post_setup(require("nvim-tree.api"))
164157
end
165158

166159
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)