feat: improved configuration and sorting of lsp_{document,workspace}_diagnostics (#867)

This commit is contained in:
fdschmidt93
2021-06-10 17:13:34 +02:00
committed by GitHub
parent f9ce723f2e
commit 1407ac3400
4 changed files with 122 additions and 21 deletions

View File

@@ -342,17 +342,31 @@ builtin.lsp_workspace_symbols = require('telescope.builtin.lsp').workspace_symbo
builtin.lsp_dynamic_workspace_symbols = require('telescope.builtin.lsp').dynamic_workspace_symbols
--- Lists LSP diagnostics for the current buffer
--- - Fields:
--- - `All severity flags can be passed as `string` or `number` as per `:vim.lsp.protocol.DiagnosticSeverity:`
--- - Default keymaps:
--- - `<C-l>`: show autocompletion menu to prefilter your query with the diagnostic you want to see (i.e. `:warning:`)
---@param opts table: options to pass to the picker
---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false)
---@field severity string|number: filter diagnostics by severity name (string) or id (number)
---@field severity_limit string|number: filter for diagnostics equal or more severe wrt severity name (string) or id (number)
---@field severity_bound string|number: filter for diagnostics equal or less severe wrt severity name (string) or id (number)
---@field no_sign bool: hide LspDiagnosticSigns from Results (default is false)
---@field line_width number: set length of diagnostic entry text in Results
builtin.lsp_document_diagnostics = require('telescope.builtin.lsp').diagnostics
--- Lists LSP diagnostics for the current workspace if supported, otherwise searches in all open buffers
--- - Fields:
--- - `All severity flags can be passed as `string` or `number` as per `:vim.lsp.protocol.DiagnosticSeverity:`
--- - Default keymaps:
--- - `<C-l>`: show autocompletion menu to prefilter your query with the diagnostic you want to see (i.e. `:warning:`)
---@param opts table: options to pass to the picker
---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false)
---@field severity string|number: filter diagnostics by severity name (string) or id (number)
---@field severity_limit string|number: filter for diagnostics equal or more severe wrt severity name (string) or id (number)
---@field severity_bound string|number: filter for diagnostics equal or less severe wrt severity name (string) or id (number)
---@field no_sign bool: hide LspDiagnosticSigns from Results (default is false)
---@field line_width number: set length of diagnostic entry text in Results
builtin.lsp_workspace_diagnostics = require('telescope.builtin.lsp').workspace_diagnostics
local apply_config = function(mod)

View File

@@ -29,13 +29,6 @@ local lsp_type_highlight = {
["Variable"] = "TelescopeResultsVariable",
}
local lsp_type_diagnostic = {
[1] = "Error",
[2] = "Warning",
[3] = "Information",
[4] = "Hint"
}
local make_entry = {}
do
@@ -936,18 +929,21 @@ end
function make_entry.gen_from_lsp_diagnostics(opts)
opts = opts or {}
opts.tail_path = utils.get_default(opts.tail_path, true)
local lsp_type_diagnostic = vim.lsp.protocol.DiagnosticSeverity
local signs
if not opts.no_sign then
signs = {}
for _, v in pairs(lsp_type_diagnostic) do
for severity, _ in pairs(lsp_type_diagnostic) do
-- pcall to catch entirely unbound or cleared out sign hl group
local status, sign = pcall(
function() return vim.trim(vim.fn.sign_getdefined("LspDiagnosticsSign" .. v)[1].text) end)
if not status then
sign = v:sub(1,1)
if type(severity) == 'string' then
local status, sign = pcall(
function() return vim.trim(vim.fn.sign_getdefined("LspDiagnosticsSign" .. severity)[1].text) end)
if not status then
sign = severity:sub(1,1)
end
signs[severity] = sign
end
signs[v] = sign
end
end

View File

@@ -82,11 +82,48 @@ utils.quickfix_items_to_entries = function(locations)
return results
end
local convert_diagnostic_type = function(severity)
-- convert from string to int
if type(severity) == 'string' then
-- make sure that e.g. error is uppercased to Error
return vim.lsp.protocol.DiagnosticSeverity[severity:gsub("^%l", string.upper)]
end
-- otherwise keep original value, incl. nil
return severity
end
local filter_diag_severity = function(opts, severity)
if opts.severity ~= nil then
return opts.severity == severity
elseif opts.severity_limit ~= nil then
return severity <= opts.severity_limit
elseif opts.severity_bound ~= nil then
return severity >= opts.severity_bound
else
return true
end
end
utils.diagnostics_to_tbl = function(opts)
opts = opts or {}
local items = {}
local lsp_type_diagnostic = vim.lsp.protocol.DiagnosticSeverity
local current_buf = vim.api.nvim_get_current_buf()
local lsp_type_diagnostic = {[1] = "Error", [2] = "Warning", [3] = "Information", [4] = "Hint"}
opts.severity = convert_diagnostic_type(opts.severity)
opts.severity_limit = convert_diagnostic_type(opts.severity_limit)
opts.severity_bound = convert_diagnostic_type(opts.severity_bound)
local validate_severity = 0
for _, v in pairs({opts.severity, opts.severity_limit, opts.severity_bound}) do
if v ~= nil then
validate_severity = validate_severity + 1
end
if validate_severity > 1 then
print('Please pass valid severity parameters')
return {}
end
end
local preprocess_diag = function(diag, bufnr)
local filename = vim.api.nvim_buf_get_name(bufnr)
@@ -102,11 +139,10 @@ utils.diagnostics_to_tbl = function(opts)
col = col + 1,
start = start,
finish = finish,
-- remove line break to avoid display issues
-- remove line break to avoid display issues
text = vim.trim(diag.message:gsub("[\n]", "")),
type = lsp_type_diagnostic[diag.severity] or lsp_type_diagnostic[1]
}
table.sort(buffer_diag, function(a, b) return a.lnum < b.lnum end)
return buffer_diag
end
@@ -116,10 +152,33 @@ utils.diagnostics_to_tbl = function(opts)
for _, diag in pairs(diags) do
-- workspace diagnostics may include empty tables for unused bufnr
if not vim.tbl_isempty(diag) then
table.insert(items, preprocess_diag(diag, bufnr))
if filter_diag_severity(opts, diag.severity) then
table.insert(items, preprocess_diag(diag, bufnr))
end
end
end
end
-- sort results by bufnr (prioritize cur buf), severity, lnum
table.sort(items, function(a, b)
if a.bufnr == b.bufnr then
if a.type == b.type then
return a.lnum < b.lnum
else
return a.type < b.type
end
else
-- prioritize for current bufnr
if a.bufnr == current_buf then
return true
end
if b.bufnr == current_buf then
return false
end
return a.bufnr < b.bufnr
end
end)
return items
end