Skip to content

Commit

Permalink
perf: prevent autocmd leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 31, 2024
1 parent 5a12185 commit 9e3391c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions lua/trouble/config/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function M.setup()
M.link(M.colors)
M.source("fs")
vim.api.nvim_create_autocmd("ColorScheme", {
group = vim.api.nvim_create_augroup("trouble.colorscheme", { clear = true }),
callback = function()
M._fixed = {}
end,
Expand Down
1 change: 1 addition & 0 deletions lua/trouble/sources/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ local cache = {}

function M.setup()
vim.api.nvim_create_autocmd("DiagnosticChanged", {
group = vim.api.nvim_create_augroup("trouble.diagnostics", { clear = true }),
callback = function(event)
-- NOTE: unfortunately, we can't use the event.data.diagnostics table here,
-- since multiple namespaces exist and we can't tell which namespace the
Expand Down
1 change: 1 addition & 0 deletions lua/trouble/sources/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local M = {}

function M.setup()
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("trouble.lsp.attach", { clear = true }),
callback = function()
Cache.symbols:clear()
end,
Expand Down
11 changes: 5 additions & 6 deletions lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ function M.new(opts)
self:on_mount()
end
self.opts.win.on_close = function()
M._last[self.opts.mode or ""] = self:at()
if not self.opts.auto_open then
for _, section in ipairs(self.sections) do
section:stop()
end
end
end

self.sections = {}
Expand Down Expand Up @@ -123,11 +127,6 @@ function M:on_mount()
self:listen()
self.win:on("WinLeave", function()
Preview.close()
if not self.opts.auto_open then
for _, section in ipairs(self.sections) do
section:stop()
end
end
end)

local _self = Util.weak(self)
Expand Down
9 changes: 7 additions & 2 deletions lua/trouble/view/section.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,18 @@ function M:main()
return self._main
end

function M:augroup()
return "trouble.section." .. self.section.source .. "." .. self.id
end

function M:stop()
pcall(vim.api.nvim_del_augroup_by_name, "trouble-section-" .. self.id)
pcall(vim.api.nvim_del_augroup_by_name, self:augroup())
end

function M:listen()
local _self = Util.weak(self)
local group = vim.api.nvim_create_augroup("trouble-section-" .. self.id, { clear = true })

local group = vim.api.nvim_create_augroup(self:augroup(), { clear = true })
for _, event in ipairs(self.section.events or {}) do
vim.api.nvim_create_autocmd(event.event, {
group = group,
Expand Down
2 changes: 1 addition & 1 deletion lua/trouble/view/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ function M:check_alien()
end

function M:close()
self:augroup(true)
pcall(vim.api.nvim_win_close, self.win, true)
self:augroup(true)
self.win = nil
end

Expand Down

0 comments on commit 9e3391c

Please sign in to comment.