From 908752fc67f0aff43e35ffb175b5e6226874bad5 Mon Sep 17 00:00:00 2001 From: fdschmidt93 <39233597+fdschmidt93@users.noreply.github.com> Date: Thu, 4 Mar 2021 15:01:17 +0100 Subject: [PATCH] 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 Co-authored-by: elianiva --- README.md | 3 ++- lua/telescope/builtin/init.lua | 3 ++- lua/telescope/builtin/lsp.lua | 11 ++++++++++- lua/telescope/make_entry.lua | 20 +++++++++----------- lua/telescope/utils.lua | 34 ++++++++++++++++++++-------------- 5 files changed, 43 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 54dd401..1736cfd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 940d3a3..5339b9b 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -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 diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 8324cf1..22bf0ba 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -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) diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 2b27328..45601af 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -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 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 = { - { width = 11 }, - { width = line_width }, - { remaining = true } - } + 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, diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index cd58c13..4e8c1d3 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -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