Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(statusline): add trouble diagnostic counts to statusline #525

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ config.defaults.actions.files["ctrl-t"] = actions.open

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

### Statusline Component
### Statusline LSP Document Symbols Component

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

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

### Statusline Diagnostic Count Component

Have the diagnostics counts in statusline widget match the trouble diagnostics filter you use.

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

```lua
{
"nvim-lualine/lualine.nvim",
opts = function(_, opts)
local trouble = require 'trouble'
local troubleDignosticsCount = trouble.diagnosticsCount ({
-- use diagnostics mode and specify filter
mode = 'diagnostics',
filter = {
-- limit to files in the current project
function(item)
return item.filename:find((vim.loop or vim.uv).cwd(), 1, true)
end,
},
-- Or use a custom mode you created that already contains the filter
-- mode = 'onlyworkspace',
})
table.insert(opts.sections.lualine_c, {
'diagnostics',
sources = {
function()
return troubleDignosticsCount.get()
end,
},
})
end,
}
```

## 🎨 Colors

The table below shows all the highlight groups defined for Trouble.
Expand Down
50 changes: 50 additions & 0 deletions lua/trouble/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,56 @@ function M.statusline(opts)
}
end

-- Renders a trouble trable of diagnostic counts
-- { error=error_cnt, warn=warn_cnt, info=info_cnt, hint=hint_cnt }
-- Check the docs for examples.
---@param opts? trouble.Mode|string|{hl_group?:string}
---@return {get: (fun():table)}
function M.diagnosticsCount(opts)
local Spec = require("trouble.spec")
local Section = require("trouble.view.section")
local Render = require("trouble.view.render")
opts.groups = {
{ "severity", format = "{severity}{count}" },
}
opts.title = false
opts.format = ""
opts = Config.get(opts)

local renderer = Render.new(opts, {
multiline = false,
indent = false,
})
local status = nil ---@type table?
---@cast opts trouble.Mode

local s = Spec.section(opts)
s.max_items = s.max_items or opts.max_items
local section = Section.new(s, opts)
section.on_update = function()
status = nil
if package.loaded["lualine"] then
vim.schedule(function()
require("lualine").refresh()
end)
else
vim.cmd.redrawstatus()
end
end
section:listen()
section:refresh()
return {
get = function()
if status then
return status
end
renderer:clear()
renderer:sections({ section })
return renderer:diagnosticCount()
end,
}
end

return setmetatable(M, {
__index = function(_, k)
if k == "last_mode" then
Expand Down
20 changes: 20 additions & 0 deletions lua/trouble/view/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ function M:statusline(opts)
return table.concat(lines, sep)
end

---@return table -- { error = count, warn = count, info = count, hint = count }
function M:diagnosticCount()
local list = {}
for _, line in ipairs(self._lines) do
for _, segment in ipairs(line) do
local str = segment.str:gsub("%s+", "")
table.insert(list, str)
end
end

local lookupTable = {}
for i = 1, #list, 2 do
local key = list[i]:lower()
local value = tonumber(list[i + 1])
lookupTable[key] = value
end

return lookupTable
end

function M:render(buf)
local lines = {}

Expand Down