Skip to content

Commit

Permalink
fix: close section session when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 96c30dc commit 2caf73d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ 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 Expand Up @@ -282,7 +287,6 @@ function M:goto_main()
end

function M:listen()
local _self = Util.weak(self)
self:main()

for _, section in ipairs(self.sections) do
Expand Down
10 changes: 10 additions & 0 deletions lua/trouble/view/section.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ local Util = require("trouble.util")
---@field fetching boolean
---@field filter? trouble.Filter
---@field first_update boolean
---@field id number
---@field on_refresh? fun(self: trouble.Section)
---@field on_update? fun(self: trouble.Section)
local M = {}
M._id = 0

---@param section trouble.Section.opts
---@param opts trouble.Config
function M.new(section, opts)
local self = setmetatable({}, { __index = M })
self.section = section
self.opts = opts
M._id = M._id + 1
self.id = M._id
self.finder = Sources.get(section.source)
self.items = {}
self:main()
Expand Down Expand Up @@ -120,10 +124,16 @@ function M:main()
return self._main
end

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

function M:listen()
local _self = Util.weak(self)
local group = vim.api.nvim_create_augroup("trouble-section-" .. self.id, { clear = true })
for _, event in ipairs(self.section.events or {}) do
vim.api.nvim_create_autocmd(event.event, {
group = group,
pattern = event.pattern,
callback = function(e)
local this = _self()
Expand Down

0 comments on commit 2caf73d

Please sign in to comment.