Skip to content

Commit

Permalink
perf: only trigger refresh when event happens in main for some sources
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 25, 2023
1 parent dd24b93 commit cb74fd4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lua/trouble/sources/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ M.config = {
views = {
lsp_document_symbols = {
title = "{hl:Title}Document Symbols{hl} {count}",
events = { "BufEnter", "BufWritePost", "BufReadPost" },
events = { "BufEnter", { event = "TextChanged", main = true } },
-- events = { "CursorHold", "CursorMoved" },
source = "lsp.document_symbols",
flatten = false,
Expand Down Expand Up @@ -40,7 +40,7 @@ M.config = {
for _, mode in ipairs({ "definitions", "references", "implementations", "type_definitions", "declarations" }) do
M.config.views["lsp_" .. mode] = {
title = "{hl:Title}" .. mode:gsub("^%l", string.upper) .. "{hl} {count}",
events = { "CursorHold" },
events = { { event = "CursorHold", main = true } },
-- events = { "CursorHold", "CursorMoved" },
source = "lsp." .. mode,
groups = {
Expand Down
24 changes: 21 additions & 3 deletions lua/trouble/spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
---@class trouble.Section.spec
---@field source string
---@field title? string
---@field events? string[]
---@field events? (string|trouble.Event)[]
---@field groups? trouble.Group.spec[]|trouble.Group.spec
---@field sort? trouble.Sort.spec
---@field filter? trouble.Filter.spec
Expand All @@ -19,6 +19,11 @@

---@alias trouble.Filter table<string, any>|fun(items: trouble.Item[]): trouble.Item[]

---@class trouble.Event
---@field event string|string[]
---@field pattern? string|string[]
---@field main? boolean When true, this event will refresh only when it is the main window

---@class trouble.Sort
---@field field? string
---@field sorter? trouble.SorterFn
Expand All @@ -35,7 +40,7 @@
---@field groups trouble.Group[]
---@field format string
---@field flatten? boolean when true, items with a natural hierarchy will be flattened
---@field events? string[]
---@field events trouble.Event[]
---@field sort? trouble.Sort[]
---@field filter? trouble.Filter
---@field max_items? number
Expand All @@ -47,13 +52,26 @@ local M = {}
function M.section(spec)
local groups = type(spec.groups) == "string" and { spec.groups } or spec.groups
---@cast groups trouble.Group.spec[]
local events = {} ---@type trouble.Event[]
for _, e in ipairs(spec.events or {}) do
if type(e) == "string" then
local event, pattern = e:match("^(%w+)%s+(.*)$")
event = event or e
events[#events + 1] = { event = event, pattern = pattern }
elseif type(e) == "table" and e.event then
events[#events + 1] = e
else
error("invalid event: " .. vim.inspect(e))
end
end

local ret = {
source = spec.source,
groups = vim.tbl_map(M.group, groups or {}),
sort = spec.sort and M.sort(spec.sort) or nil,
filter = spec.filter,
format = spec.format or "{filename} {pos}",
events = spec.events,
events = events,
flatten = spec.flatten,
}
-- A title is just a group without fields
Expand Down
33 changes: 17 additions & 16 deletions lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,25 +205,26 @@ function M:listen()
end
end, { buffer = false })

local events = {} ---@type string[]
for _, section in ipairs(self.sections) do
for _, e in ipairs(section.events or {}) do
if not vim.tbl_contains(events, e) then
table.insert(events, e)
end
for _, event in ipairs(section.events or {}) do
vim.api.nvim_create_autocmd(event.event, {
group = self.win:augroup(),
pattern = event.pattern,
callback = function(e)
if event.main then
local main = self:main()
if main and main.buf ~= e.buf then
return
end
end
if e.event == "BufEnter" and vim.bo[e.buf].buftype ~= "" then
return
end
self:refresh()
end,
})
end
end
for _, spec in ipairs(events) do
local event, pattern = spec:match("^(%w+)%s+(.*)$")
event = event or spec
vim.api.nvim_create_autocmd(event, {
group = self.win:augroup(),
pattern = pattern,
callback = function()
self:refresh()
end,
})
end
end

---@param cursor? number[]
Expand Down

0 comments on commit cb74fd4

Please sign in to comment.