feat: workspace diagnostics, jump to and improved styling (#599)

Changes: `Telescope lsp_diagnostics` is now `Telescope lsp_document_diagnostics`
New: `Telescope lsp_workspace_diagnostics`

Co-authored-by: Fabian David Schmidt <fabian.david.schmidt@hotmail.com>
Co-authored-by: elianiva <dicha.arkana03@gmail.com>
This commit is contained in:
fdschmidt93
2021-03-04 15:01:17 +01:00
committed by GitHub
parent 8dc00b08aa
commit 908752fc67
5 changed files with 43 additions and 28 deletions

View File

@@ -409,7 +409,8 @@ Built-in functions. Ready to be bound to any key you like. :smile:
| `builtin.lsp_workspace_symbols` | Searches in LSP all workspace symbols. |
| `builtin.lsp_code_actions` | Lists LSP action to be trigged on enter. |
| `builtin.lsp_range_code_actions` | Lists LSP range code action to be trigged on enter. |
| `builtin.lsp_diagnostics` | Lists LSP Diagnostics in the current document. |
| `builtin.lsp_document_diagnostics` | Lists LSP Diagnostics in the current document. |
| `builtin.lsp_workspace_diagnostics` | Lists LSP Diagnostics in the workspace if supported and otherwise open buffers. |
| .................................. | Your next awesome picker function here :D |
### Git Pickers

View File

@@ -64,7 +64,8 @@ builtin.spell_suggest = require('telescope.builtin.internal').spell_suggest
builtin.lsp_references = require('telescope.builtin.lsp').references
builtin.lsp_document_symbols = require('telescope.builtin.lsp').document_symbols
builtin.lsp_code_actions = require('telescope.builtin.lsp').code_actions
builtin.lsp_diagnostics = require('telescope.builtin.lsp').diagnostics
builtin.lsp_document_diagnostics = require('telescope.builtin.lsp').diagnostics
builtin.lsp_workspace_diagnostics = require('telescope.builtin.lsp').workspace_diagnostics
builtin.lsp_range_code_actions = require('telescope.builtin.lsp').range_code_actions
builtin.lsp_workspace_symbols = require('telescope.builtin.lsp').workspace_symbols

View File

@@ -198,8 +198,9 @@ lsp.diagnostics = function(opts)
return
end
opts.hide_filename = utils.get_default(opts.hide_filename, true)
pickers.new(opts, {
prompt_title = 'LSP Diagnostics',
prompt_title = 'LSP Document Diagnostics',
finder = finders.new_table {
results = locations,
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_diagnostics(opts)
@@ -212,6 +213,14 @@ lsp.diagnostics = function(opts)
}):find()
end
lsp.workspace_diagnostics = function(opts)
opts = utils.get_default(opts, {})
opts.hide_filename = utils.get_default(opts.hide_filename, false)
opts.prompt_title = 'LSP Workspace Diagnostics'
opts.get_all = true
lsp.diagnostics(opts)
end
local function check_capabilities(feature)
local clients = vim.lsp.buf_get_clients(0)

View File

@@ -862,15 +862,16 @@ function make_entry.gen_from_lsp_diagnostics(opts)
for _, v in pairs(lsp_type_diagnostic) do
signs[v] = vim.trim(vim.fn.sign_getdefined("LspDiagnosticsSign" .. v)[1].text)
end
-- expose line width for longer msg if opts.hide_filename
local line_width = utils.get_default(opts.line_width, 48)
local displayer = entry_display.create {
separator = "",
items = {
{ width = 11 },
{ width = line_width },
local layout = {
{ width = 9 },
{ remaining = true }
}
local line_width = utils.get_default(opts.line_width, 47)
if not opts.hide_filename then table.insert(layout, 2, {width = line_width}) end
local displayer = entry_display.create {
separator = "",
items = layout
}
local make_display = function(entry)
@@ -890,12 +891,10 @@ function make_entry.gen_from_lsp_diagnostics(opts)
string.format("%s %s", signs[entry.type], pos),
string.format("LspDiagnosticsDefault%s", entry.type)
}
-- remove line break to avoid display issues
local text = string.format("%-" .. line_width .."s", entry.text:gsub(".* | ", ""):gsub("[\n]", ""))
return displayer {
line_info,
text,
entry.text,
filename,
}
end
@@ -912,7 +911,6 @@ function make_entry.gen_from_lsp_diagnostics(opts)
or ''
) .. ' ' .. entry.text,
display = make_display,
bufnr = entry.bufnr,
filename = filename,
type = entry.type,
lnum = entry.lnum,

View File

@@ -84,36 +84,42 @@ end
utils.diagnostics_to_tbl = function(opts)
opts = opts or {}
local bufnr = vim.api.nvim_get_current_buf()
local filename = vim.api.nvim_buf_get_name(bufnr)
local buffer_diags = vim.lsp.diagnostic.get(bufnr, opts.client_id)
local items = {}
local current_buf = vim.api.nvim_get_current_buf()
local lsp_type_diagnostic = {[1] = "Error", [2] = "Warning", [3] = "Information", [4] = "Hint"}
local insert_diag = function(diag)
local preprocess_diag = function(diag, bufnr)
local filename = vim.api.nvim_buf_get_name(bufnr)
local start = diag.range['start']
local finish = diag.range['end']
local row = start.line
local col = start.character
local line = (vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false) or {""})[1]
table.insert(items, {
local buffer_diag = {
bufnr = bufnr,
filename = filename,
lnum = row + 1,
col = col + 1,
start = start,
finish = finish,
text = vim.trim(line .. " | " .. diag.message),
-- 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
for _, diag in ipairs(buffer_diags) do insert_diag(diag) end
table.sort(items, function(a, b) return a.lnum < b.lnum end)
local buffer_diags = opts.get_all and vim.lsp.diagnostic.get_all() or
{[current_buf] = vim.lsp.diagnostic.get(current_buf, opts.client_id)}
for bufnr, diags in pairs(buffer_diags) do
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))
end
end
end
return items
end