Skip to content

Commit

Permalink
feat: filters, formatters and sorters are now configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 23, 2023
1 parent 19c01b7 commit d69200e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
7 changes: 7 additions & 0 deletions lua/trouble/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ local defaults = {
fold_closed = "",
ws = " ",
},
---@type table<string, trouble.Formatter>
formatters = {}, -- custom formatters
},
---@type table<string, trouble.FilterFn>
filters = {}, -- custom filters
---@type table<string, trouble.Sorter>
sorters = {}, -- custom sorters
---@type table<string, string|trouble.Action>
keys = {
["?"] = "help",
r = "refresh",
q = "close",
o = "jump_close",
Expand Down
5 changes: 3 additions & 2 deletions lua/trouble/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ function M.is(item, filter, view)
is = false
break
end
elseif M.filters[k] then
if not M.filters[k](item, v, view) then
elseif view.opts.filters[k] or M.filters[k] then
local f = view.opts.filters[k] or M.filters[k]
if not f(item, v, view) then
is = false
break
end
Expand Down
10 changes: 6 additions & 4 deletions lua/trouble/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local M = {}
---@alias trouble.Format {text:string, hl?:string}

---@alias trouble.Formatter fun(ctx: trouble.Formatter.ctx): trouble.spec.format?
---@alias trouble.Formatter.ctx {item: trouble.Item, node:trouble.Node, field:string, value:string}
---@alias trouble.Formatter.ctx {item: trouble.Item, node:trouble.Node, field:string, value:string, opts:trouble.Render.opts}

---@param source string
---@param field string
Expand Down Expand Up @@ -90,8 +90,10 @@ function M.field(ctx)
---@type trouble.Format[]
local format = { { fi = ctx.field, text = tostring(ctx.item[ctx.field] or "") } }

if M.formatters[ctx.field] then
local result = M.formatters[ctx.field](ctx)
local formatter = ctx.opts and ctx.opts.formatters and ctx.opts.formatters[ctx.field] or M.formatters[ctx.field]

if formatter then
local result = formatter(ctx)
if result then
result = type(result) == "table" and vim.tbl_islist(result) and result or { result }
format = {}
Expand All @@ -109,7 +111,7 @@ function M.field(ctx)
end

---@param format string
---@param ctx {item: trouble.Item, node:trouble.Node}
---@param ctx {item: trouble.Item, node:trouble.Node, opts:trouble.Render.opts}
function M.format(format, ctx)
---@type trouble.Format[]
local ret = {}
Expand Down
5 changes: 3 additions & 2 deletions lua/trouble/sort.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ function M.sort(items, opts, view)
fields[f] = { sorter = field }
elseif type(field) == "table" and field.field then
---@cast field {field:string, desc?:boolean}
if M.sorters[field.field] then
fields[f] = { sorter = M.sorters[field.field] }
local sorter = view.opts.sorters[field.field] or M.sorters[field.field]
if sorter then
fields[f] = { sorter = sorter }
else
fields[f] = { field = field.field }
end
Expand Down
3 changes: 2 additions & 1 deletion lua/trouble/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ M.__index = M

---@class trouble.Render.opts: trouble.Text.opts
---@field indent? trouble.Indent.symbols
---@field formatters? table<string, trouble.Formatter>

---@param opts? trouble.Render.opts|trouble.Text.opts
function M.new(opts)
Expand Down Expand Up @@ -164,7 +165,7 @@ function M:item(item, node, format_string, is_group, indent)
if line then
self:append(line)
else
local format = Format.format(format_string, { item = item, node = node })
local format = Format.format(format_string, { item = item, node = node, opts = self.opts })
indent:multi_line()
for _, ff in ipairs(format) do
self:append(ff.text, ff.hl, {
Expand Down

0 comments on commit d69200e

Please sign in to comment.