Skip to content

Commit 1875461

Browse files
committed
feat(statusline): 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 2f3b537 commit 1875461

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ config.defaults.actions.files["ctrl-t"] = actions.open
647647

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

650-
### Statusline Component
650+
### Statusline LSP Document Symbols Component
651651

652652
Example for [lualine.nvim](https://github.com/nvim-lualine/lualine.nvim):
653653

@@ -674,6 +674,41 @@ Example for [lualine.nvim](https://github.com/nvim-lualine/lualine.nvim):
674674
}
675675
```
676676

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

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

lua/trouble/api.lua

+50
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

+20
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)