feat(diagnostics): additional filter options (#1610)
- root_dir. Useful to browse only project-scoped diagnostics. - buflisted status. Useful to exclude diagnostics from unlisted buffers, which are created by language servers that analyze the entire project/workspace (e.g. sumneko-lua).
This commit is contained in:
@@ -1461,7 +1461,7 @@ builtin.lsp_dynamic_workspace_symbols({opts}) *builtin.lsp_dynamic_workspace_sym
|
|||||||
|
|
||||||
|
|
||||||
builtin.diagnostics({opts}) *builtin.diagnostics()*
|
builtin.diagnostics({opts}) *builtin.diagnostics()*
|
||||||
Lists diagnostics for current or all open buffers
|
Lists diagnostics
|
||||||
- Fields:
|
- Fields:
|
||||||
- `All severity flags can be passed as `string` or `number` as per
|
- `All severity flags can be passed as `string` or `number` as per
|
||||||
`:vim.diagnostic.severity:`
|
`:vim.diagnostic.severity:`
|
||||||
@@ -1479,17 +1479,22 @@ builtin.diagnostics({opts}) *builtin.diagnostics()*
|
|||||||
{severity} (string|number) filter diagnostics by severity name
|
{severity} (string|number) filter diagnostics by severity name
|
||||||
(string) or id (number)
|
(string) or id (number)
|
||||||
{severity_limit} (string|number) keep diagnostics equal or more
|
{severity_limit} (string|number) keep diagnostics equal or more
|
||||||
severe wrt severity name (string) or
|
severe wrt severity name (string)
|
||||||
id (number)
|
or id (number)
|
||||||
{severity_bound} (string|number) keep diagnostics equal or less
|
{severity_bound} (string|number) keep diagnostics equal or less
|
||||||
severe wrt severity name (string) or
|
severe wrt severity name (string)
|
||||||
id (number)
|
or id (number)
|
||||||
|
{root_dir} (string|boolean) if set to string, get diagnostics
|
||||||
|
only for buffers under this dir
|
||||||
|
otherwise cwd
|
||||||
|
{no_unlisted} (boolean) if true, get diagnostics only for
|
||||||
|
listed buffers
|
||||||
{no_sign} (boolean) hide DiagnosticSigns from Results
|
{no_sign} (boolean) hide DiagnosticSigns from Results
|
||||||
(default: false)
|
(default: false)
|
||||||
{line_width} (number) set length of diagnostic entry text
|
{line_width} (number) set length of diagnostic entry text
|
||||||
in Results
|
in Results
|
||||||
{namespace} (number) limit your diagnostics to a specific
|
{namespace} (number) limit your diagnostics to a
|
||||||
namespace
|
specific namespace
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -41,13 +41,23 @@ local diagnostics_to_tbl = function(opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
opts.root_dir = opts.root_dir == true and vim.loop.cwd() or opts.root_dir
|
||||||
|
|
||||||
local bufnr_name_map = {}
|
local bufnr_name_map = {}
|
||||||
local preprocess_diag = function(diagnostic)
|
local filter_diag = function(diagnostic)
|
||||||
if bufnr_name_map[diagnostic.bufnr] == nil then
|
if bufnr_name_map[diagnostic.bufnr] == nil then
|
||||||
bufnr_name_map[diagnostic.bufnr] = vim.api.nvim_buf_get_name(diagnostic.bufnr)
|
bufnr_name_map[diagnostic.bufnr] = vim.api.nvim_buf_get_name(diagnostic.bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
local buffer_diag = {
|
local root_dir_test = not opts.root_dir
|
||||||
|
or string.sub(bufnr_name_map[diagnostic.bufnr], 1, #opts.root_dir) == opts.root_dir
|
||||||
|
local listed_test = not opts.no_unlisted or vim.api.nvim_buf_get_option(diagnostic.bufnr, "buflisted")
|
||||||
|
|
||||||
|
return root_dir_test and listed_test
|
||||||
|
end
|
||||||
|
|
||||||
|
local preprocess_diag = function(diagnostic)
|
||||||
|
return {
|
||||||
bufnr = diagnostic.bufnr,
|
bufnr = diagnostic.bufnr,
|
||||||
filename = bufnr_name_map[diagnostic.bufnr],
|
filename = bufnr_name_map[diagnostic.bufnr],
|
||||||
lnum = diagnostic.lnum + 1,
|
lnum = diagnostic.lnum + 1,
|
||||||
@@ -55,12 +65,13 @@ local diagnostics_to_tbl = function(opts)
|
|||||||
text = vim.trim(diagnostic.message:gsub("[\n]", "")),
|
text = vim.trim(diagnostic.message:gsub("[\n]", "")),
|
||||||
type = severities[diagnostic.severity] or severities[1],
|
type = severities[diagnostic.severity] or severities[1],
|
||||||
}
|
}
|
||||||
return buffer_diag
|
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, d in ipairs(vim.diagnostic.get(opts.bufnr, diagnosis_opts)) do
|
for _, d in ipairs(vim.diagnostic.get(opts.bufnr, diagnosis_opts)) do
|
||||||
|
if filter_diag(d) then
|
||||||
table.insert(items, preprocess_diag(d))
|
table.insert(items, preprocess_diag(d))
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- sort results by bufnr (prioritize cur buf), severity, lnum
|
-- sort results by bufnr (prioritize cur buf), severity, lnum
|
||||||
table.sort(items, function(a, b)
|
table.sort(items, function(a, b)
|
||||||
|
|||||||
@@ -426,7 +426,7 @@ builtin.lsp_dynamic_workspace_symbols = require_on_exported_call("telescope.buil
|
|||||||
--
|
--
|
||||||
--
|
--
|
||||||
|
|
||||||
--- Lists diagnostics for current or all open buffers
|
--- Lists diagnostics
|
||||||
--- - Fields:
|
--- - Fields:
|
||||||
--- - `All severity flags can be passed as `string` or `number` as per `:vim.diagnostic.severity:`
|
--- - `All severity flags can be passed as `string` or `number` as per `:vim.diagnostic.severity:`
|
||||||
--- - Default keymaps:
|
--- - Default keymaps:
|
||||||
@@ -436,6 +436,8 @@ builtin.lsp_dynamic_workspace_symbols = require_on_exported_call("telescope.buil
|
|||||||
---@field severity string|number: filter diagnostics by severity name (string) or id (number)
|
---@field severity string|number: filter diagnostics by severity name (string) or id (number)
|
||||||
---@field severity_limit string|number: keep diagnostics equal or more severe wrt severity name (string) or id (number)
|
---@field severity_limit string|number: keep diagnostics equal or more severe wrt severity name (string) or id (number)
|
||||||
---@field severity_bound string|number: keep diagnostics equal or less severe wrt severity name (string) or id (number)
|
---@field severity_bound string|number: keep diagnostics equal or less severe wrt severity name (string) or id (number)
|
||||||
|
---@field root_dir string|boolean: if set to string, get diagnostics only for buffers under this dir otherwise cwd
|
||||||
|
---@field no_unlisted boolean: if true, get diagnostics only for listed buffers
|
||||||
---@field no_sign boolean: hide DiagnosticSigns from Results (default: false)
|
---@field no_sign boolean: hide DiagnosticSigns from Results (default: false)
|
||||||
---@field line_width number: set length of diagnostic entry text in Results
|
---@field line_width number: set length of diagnostic entry text in Results
|
||||||
---@field namespace number: limit your diagnostics to a specific namespace
|
---@field namespace number: limit your diagnostics to a specific namespace
|
||||||
|
|||||||
Reference in New Issue
Block a user