Skip to content

Commit

Permalink
feat: filter diagnostics by severity level (#285)
Browse files Browse the repository at this point in the history
* feat: filter diagnostics by severity level, with default keybind `s`

* update doc

* remove level printing

* change severity type to lsp.DiagnosticSeverity

* update README
  • Loading branch information
eatgrass committed May 25, 2023
1 parent 5da13b5 commit b1f607f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Trouble comes with the following defaults:
width = 50, -- width of the list when position is left or right
icons = true, -- use devicons for filenames
mode = "workspace_diagnostics", -- "workspace_diagnostics", "document_diagnostics", "quickfix", "lsp_references", "loclist"
severity = nil, -- nil (ALL) or vim.diagnostic.severity.ERROR | WARN | INFO | HINT
fold_open = "", -- icon used for open folds
fold_closed = "", -- icon used for closed folds
group = true, -- group results by file
Expand All @@ -77,6 +78,7 @@ Trouble comes with the following defaults:
open_tab = { "<c-t>" }, -- open buffer in new tab
jump_close = {"o"}, -- jump to the diagnostic and close the list
toggle_mode = "m", -- toggle between "workspace" and "document" diagnostics mode
switch_severity = "s", -- switch "diagnostics" severity filter level to HINT / INFO / WARN / ERROR
toggle_preview = "P", -- toggle auto_preview
hover = "K", -- opens a small popup with the full multiline message
preview = "p", -- preview the diagnostic location
Expand Down
3 changes: 3 additions & 0 deletions lua/trouble/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ M.namespace = vim.api.nvim_create_namespace("Trouble")
---@class TroubleOptions
---@field buf number|nil
---@field win number|nil
---@field severity lsp.DiagnosticSeverity|nil
-- TODO: make some options configurable per mode
-- TODO: make it possible to have multiple trouble lists open at the same time
local defaults = {
Expand All @@ -17,6 +18,7 @@ local defaults = {
width = 50, -- width of the list when position is left or right
icons = true, -- use devicons for filenames
mode = "workspace_diagnostics", -- "workspace_diagnostics", "document_diagnostics", "quickfix", "lsp_references", "loclist"
severity = nil, -- nil (ALL) or vim.diagnostic.severity.ERROR | WARN | INFO | HINT
fold_open = "", -- icon used for open folds
fold_closed = "", -- icon used for closed folds
action_keys = { -- key mappings for actions in the trouble list
Expand All @@ -29,6 +31,7 @@ local defaults = {
open_tab = { "<c-t>" }, -- open buffer in new tab
jump_close = { "o" }, -- jump to the diagnostic and close the list
toggle_mode = "m", -- toggle between "workspace" and "document" mode
switch_severity = "s", -- switch "diagnostics" severity filter level to ALL / HINT / INFO / WARN / ERROR
toggle_preview = "P", -- toggle auto_preview
hover = "K", -- opens a small popup with the full multiline message
preview = "p", -- preview the diagnostic location
Expand Down
16 changes: 16 additions & 0 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ function Trouble.open(...)
if opts.mode and (opts.mode ~= config.options.mode) then
config.options.mode = opts.mode
end

if opts.severity and (opts.severity ~=config.options.severity) then
config.options.severity = opts.severity
end

opts.focus = true
opts.on_open = true

Expand Down Expand Up @@ -169,6 +174,17 @@ function Trouble.action(action)
action = "refresh"
end

if action == 'switch_severity' then
if config.options.severity == nil then
config.options.severity = vim.diagnostic.severity.ERROR
elseif config.options.severity < 4 then
config.options.severity = config.options.severity + 1
else
config.options.severity = nil
end
action = "refresh"
end

if view and action == "on_win_enter" then
view:on_win_enter()
end
Expand Down
2 changes: 1 addition & 1 deletion lua/trouble/providers/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function M.diagnostics(_, buf, cb, options)
local items = {}

if vim.diagnostic then
local diags = vim.diagnostic.get(buf)
local diags = vim.diagnostic.get(buf, { severity = options.severity })
for _, item in ipairs(diags) do
table.insert(items, util.process_item(item))
end
Expand Down

0 comments on commit b1f607f

Please sign in to comment.