Skip to content

Commit

Permalink
feat: global view filters and easy toggling of just items of the curr…
Browse files Browse the repository at this point in the history
…ent buffer
  • Loading branch information
folke committed May 30, 2024
1 parent d8265a6 commit 11e7c39
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lua/trouble/config/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ local M = {
toggle_refresh = function(self)
self.opts.auto_refresh = not self.opts.auto_refresh
end,

filter = function(self, ctx)
self:filter(ctx.opts.filter)
end,
-- Show the help
help = function(self)
self:help()
Expand Down
7 changes: 7 additions & 0 deletions lua/trouble/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ local defaults = {
zn = "fold_disable",
zN = "fold_enable",
zi = "fold_toggle_enable",
gb = { -- example of a custom action that toggles the active view filter
action = function(view)
view.state.filter_buffer = not view.state.filter_buffer
view:filter(view.state.filter_buffer and { buf = 0 } or nil)
end,
desc = "Toggle Current Buffer Filter",
},
},
---@type table<string, trouble.Mode>
modes = {
Expand Down
10 changes: 10 additions & 0 deletions lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local Window = require("trouble.view.window")
---@field renderer trouble.Render
---@field first_update? boolean
---@field moving uv_timer_t
---@field state table<string,any>
---@field private _main? trouble.Main
local M = {}
M.__index = M
Expand All @@ -32,6 +33,7 @@ function M.new(opts)
local self = setmetatable({}, M)
_idx = _idx + 1
M._views[self] = _idx
self.state = {}
self.opts = opts or {}
self.first_update = true
self.opts.win = self.opts.win or {}
Expand Down Expand Up @@ -99,6 +101,14 @@ function M.get(filter)
return ret
end

---@param filter trouble.Filter
function M:filter(filter)
for _, section in ipairs(self.sections) do
section.filter = filter
end
self:refresh()
end

function M:on_mount()
vim.w[self.win.win].trouble = {
mode = self.opts.mode,
Expand Down
4 changes: 4 additions & 0 deletions lua/trouble/view/section.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local Util = require("trouble.util")
---@field items trouble.Item[]
---@field node? trouble.Node
---@field fetching boolean
---@field filter? trouble.Filter
---@field on_refresh? fun(self: trouble.Section)
---@field on_update? fun(self: trouble.Section)
local M = {}
Expand Down Expand Up @@ -65,6 +66,9 @@ function M:refresh()
local ctx = { opts = self.opts, main = main }
self.finder(function(items)
items = Filter.filter(items, self.section.filter, ctx)
if self.filter then
items = Filter.filter(items, self.filter, ctx)
end
items = Sort.sort(items, self.section.sort, ctx)
self.items = items
self.node = Tree.build(items, self.section)
Expand Down

0 comments on commit 11e7c39

Please sign in to comment.