Skip to content

Commit 657df23

Browse files
committed
feat: add trouble diagnostic counts to statusline
Allows you to use trouble diagnostic counts on your status line. This is best used when you are running a filter on trouble diagnostics. For example, I only want to see workspace diagnostics and counts.
1 parent 6efc446 commit 657df23

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ config.defaults.actions.files["ctrl-t"] = actions.open
637637

638638
When you open fzf-lua, you can now hit `<c-t>` to open the results in **Trouble**
639639

640-
### Statusline Component
640+
### Statusline LSP Document Symbols Component
641641

642642
Example for [lualine.nvim](https://github.com/nvim-lualine/lualine.nvim):
643643

@@ -664,6 +664,41 @@ Example for [lualine.nvim](https://github.com/nvim-lualine/lualine.nvim):
664664
}
665665
```
666666

667+
### Statusline Diagnostic Count Component
668+
669+
Have the diagnostics counts in statusline widget match the trouble diagnostics filter you use.
670+
671+
Example for [lualine.nvim](https://github.com/nvim-lualine/lualine.nvim):
672+
673+
```lua
674+
{
675+
"nvim-lualine/lualine.nvim",
676+
opts = function(_, opts)
677+
local trouble = require 'trouble'
678+
local troubleDignosticsCount = trouble.diagnosticsCount ({
679+
-- use diagnostics mode and specify filter
680+
mode = 'diagnostics',
681+
filter = {
682+
-- limit to files in the current project
683+
function(item)
684+
return item.filename:find((vim.loop or vim.uv).cwd(), 1, true)
685+
end,
686+
},
687+
-- Or use a custom mode you created that already contains the filter
688+
-- mode = 'onlyworkspace',
689+
})
690+
table.insert(opts.sections.lualine_c, {
691+
'diagnostics',
692+
sources = {
693+
function()
694+
return troubleDignosticsCount.get()
695+
end,
696+
},
697+
})
698+
end,
699+
}
700+
```
701+
667702
## 🎨 Colors
668703

669704
The table below shows all the highlight groups defined for Trouble.

lua/trouble/api.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,56 @@ function M.statusline(opts)
193193
}
194194
end
195195

196+
-- Renders a trouble trable of diagnostic counts
197+
-- { error=error_cnt, warn=warn_cnt, info=info_cnt, hint=hint_cnt }
198+
-- Check the docs for examples.
199+
---@param opts? trouble.Mode|string|{hl_group?:string}
200+
---@return {get: (fun():table)}
201+
function M.diagnosticsCount(opts)
202+
local Spec = require("trouble.spec")
203+
local Section = require("trouble.view.section")
204+
local Render = require("trouble.view.render")
205+
opts.groups = {
206+
{ "severity", format = "{severity}{count}" },
207+
}
208+
opts.title = false
209+
opts.format = ""
210+
opts = Config.get(opts)
211+
212+
local renderer = Render.new(opts, {
213+
multiline = false,
214+
indent = false,
215+
})
216+
local status = nil ---@type table?
217+
---@cast opts trouble.Mode
218+
219+
local s = Spec.section(opts)
220+
s.max_items = s.max_items or opts.max_items
221+
local section = Section.new(s, opts)
222+
section.on_update = function()
223+
status = nil
224+
if package.loaded["lualine"] then
225+
vim.schedule(function()
226+
require("lualine").refresh()
227+
end)
228+
else
229+
vim.cmd.redrawstatus()
230+
end
231+
end
232+
section:listen()
233+
section:refresh()
234+
return {
235+
get = function()
236+
if status then
237+
return status
238+
end
239+
renderer:clear()
240+
renderer:sections({ section })
241+
return renderer:diagnosticCount()
242+
end,
243+
}
244+
end
245+
196246
return setmetatable(M, {
197247
__index = function(_, k)
198248
if k == "last_mode" then

lua/trouble/view/text.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ function M:statusline(opts)
122122
return table.concat(lines, sep)
123123
end
124124

125+
---@return table -- { error = count, warn = count, info = count, hint = count }
126+
function M:diagnosticCount()
127+
local list = {}
128+
for _, line in ipairs(self._lines) do
129+
for _, segment in ipairs(line) do
130+
local str = segment.str:gsub("%s+", "")
131+
table.insert(list, str)
132+
end
133+
end
134+
135+
local lookupTable = {}
136+
for i = 1, #list, 2 do
137+
local key = list[i]:lower()
138+
local value = tonumber(list[i + 1])
139+
lookupTable[key] = value
140+
end
141+
142+
return lookupTable
143+
end
144+
125145
function M:render(buf)
126146
local lines = {}
127147

0 commit comments

Comments
 (0)