diff options
| author | Simon Hauser <Simon-Hauser@outlook.de> | 2021-12-10 17:49:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-10 17:49:06 +0100 |
| commit | 56325fefb21a8a9331a25e294c074887dfb2a60c (patch) | |
| tree | 1b0c08e44dc437ed96824eae941a991a6a204ac1 | |
| parent | 61240ac75a93b3cdafb4325ab4e9d407f83965f4 (diff) | |
feat: migrate to Telescope diagnostics using vim.diagnostic (#1553)
| -rw-r--r-- | doc/telescope.txt | 41 | ||||
| -rw-r--r-- | doc/telescope_changelog.txt | 18 | ||||
| -rw-r--r-- | lua/telescope/builtin/diagnostics.lua | 129 | ||||
| -rw-r--r-- | lua/telescope/builtin/init.lua | 44 | ||||
| -rw-r--r-- | lua/telescope/builtin/lsp.lua | 31 | ||||
| -rw-r--r-- | lua/telescope/make_entry.lua | 52 | ||||
| -rw-r--r-- | lua/telescope/utils.lua | 101 |
7 files changed, 208 insertions, 208 deletions
diff --git a/doc/telescope.txt b/doc/telescope.txt index f4c9c8a..eae3ebe 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -1458,11 +1458,11 @@ builtin.lsp_dynamic_workspace_symbols({opts}) *builtin.lsp_dynamic_workspace_sym with hl_group -builtin.lsp_document_diagnostics({opts}) *builtin.lsp_document_diagnostics()* - Lists LSP diagnostics for the current buffer +builtin.diagnostics({opts}) *builtin.diagnostics()* + Lists diagnostics for current or all open buffers - Fields: - `All severity flags can be passed as `string` or `number` as per - `:vim.lsp.protocol.DiagnosticSeverity:` + `:vim.diagnostic.severity:` - Default keymaps: - `<C-l>`: show autocompletion menu to prefilter your query with the diagnostic you want to see (i.e. `:warning:`) @@ -1472,6 +1472,8 @@ builtin.lsp_document_diagnostics({opts}) *builtin.lsp_document_diagnostics()* {opts} (table) options to pass to the picker Options: ~ + {bufnr} (string|number) if nil get diagnostics for all open + buffers. Use 0 for current buffer {severity} (string|number) filter diagnostics by severity name (string) or id (number) {severity_limit} (string|number) keep diagnostics equal or more @@ -1480,39 +1482,12 @@ builtin.lsp_document_diagnostics({opts}) *builtin.lsp_document_diagnostics()* {severity_bound} (string|number) keep diagnostics equal or less severe wrt severity name (string) or id (number) - {no_sign} (boolean) hide LspDiagnosticSigns from Results - (default: false) - {line_width} (number) set length of diagnostic entry text - in Results - - -builtin.lsp_workspace_diagnostics({opts}) *builtin.lsp_workspace_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:`) - - - Parameters: ~ - {opts} (table) options to pass to the picker - - Options: ~ - {severity} (string|number) filter diagnostics by severity name - (string) or id (number) - {severity_limit} (string|number) keep diagnostics equal or more - severe wrt severity name (string) or - id (number) - {severity_bound} (string|number) keep diagnostics equal or less - severe wrt severity name (string) or - id (number) - {no_sign} (boolean) hide LspDiagnosticSigns from Results + {no_sign} (boolean) hide DiagnosticSigns from Results (default: false) {line_width} (number) set length of diagnostic entry text in Results + {namespace} (number) limit your diagnostics to a specific + namespace diff --git a/doc/telescope_changelog.txt b/doc/telescope_changelog.txt index 6d90aa3..f909404 100644 --- a/doc/telescope_changelog.txt +++ b/doc/telescope_changelog.txt @@ -142,4 +142,22 @@ LATEST version. Every other commit is not supported. So make sure you build the newest nightly before reporting issues. + *telescope.changelog-1553* + +Date: December 10, 2021 +PR: https://github.com/nvim-telescope/telescope.nvim/pull/1553 + +Move from `vim.lsp.diagnostic` to `vim.diagnostic`. + +Because the newly added `vim.diagnostic` has no longer anything to do with lsp +we also decided to rename our diagnostic functions: + Telescope lsp_document_diagnostics -> Telescope diagnostics bufnr=0 + Telescope lsp_workspace_diagnostics -> Telescope diagnostics +Because of that the `lsp_*_diagnostics` inside Telescope will be deprecated +and removed soon. The new `diagnostics` works almost identical to the previous +functions. Note that there is no longer a workspace diagnostics. You can only +get all diagnostics for all open buffers. + + + vim:tw=78:ts=8:ft=help:norl: diff --git a/lua/telescope/builtin/diagnostics.lua b/lua/telescope/builtin/diagnostics.lua new file mode 100644 index 0000000..2a26eaf --- /dev/null +++ b/lua/telescope/builtin/diagnostics.lua @@ -0,0 +1,129 @@ +local conf = require("telescope.config").values +local finders = require "telescope.finders" +local make_entry = require "telescope.make_entry" +local pickers = require "telescope.pickers" + +local diagnostics = {} + +local convert_diagnostic_type = function(severities, severity) + -- convert from string to int + if type(severity) == "string" then + -- make sure that e.g. error is uppercased to ERROR + return severities[severity:upper()] + end + -- otherwise keep original value, incl. nil + return severity +end + +local diagnostics_to_tbl = function(opts) + opts = vim.F.if_nil(opts, {}) + local items = {} + local severities = vim.diagnostic.severity + local current_buf = vim.api.nvim_get_current_buf() + + opts.severity = convert_diagnostic_type(severities, opts.severity) + opts.severity_limit = convert_diagnostic_type(severities, opts.severity_limit) + opts.severity_bound = convert_diagnostic_type(severities, opts.severity_bound) + + local diagnosis_opts = { severity = {}, namespace = opts.namespace } + if opts.severity ~= nil then + if opts.severity_limit ~= nil or opts.severity_bound ~= nil then + print "Invalid severity parameters. Both a specific severity and a limit/bound is not allowed" + return {} + end + diagnosis_opts.severity = opts.severity + else + if opts.severity_limit ~= nil then + diagnosis_opts.severity["min"] = opts.severity_limit + end + if opts.severity_bound ~= nil then + diagnosis_opts.severity["max"] = opts.severity_bound + end + end + + local bufnr_name_map = {} + local preprocess_diag = function(diagnostic) + if bufnr_name_map[diagnostic.bufnr] == nil then + bufnr_name_map[diagnostic.bufnr] = vim.api.nvim_buf_get_name(diagnostic.bufnr) + end + + local buffer_diag = { + bufnr = diagnostic.bufnr, + filename = bufnr_name_map[diagnostic.bufnr], + lnum = diagnostic.lnum + 1, + col = diagnostic.col + 1, + text = vim.trim(diagnostic.message:gsub("[\n]", "")), + type = severities[diagnostic.severity] or severities[1], + } + return buffer_diag + end + + for _, d in ipairs(vim.diagnostic.get(opts.bufnr, diagnosis_opts)) do + table.insert(items, preprocess_diag(d)) + 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 + +diagnostics.get = function(opts) + if opts.bufnr == nil then + opts.path_display = vim.F.if_nil(opts.path_display, {}) + end + if type(opts.bufnr) == "string" then + opts.bufnr = tonumber(opts.bufnr) + end + + local locations = diagnostics_to_tbl(opts) + + if vim.tbl_isempty(locations) then + print "No diagnostics found" + return + end + + opts.path_display = vim.F.if_nil(opts.path_display, "hidden") + pickers.new(opts, { + prompt_title = "Document Diagnostics", + finder = finders.new_table { + results = locations, + entry_maker = opts.entry_maker or make_entry.gen_from_diagnostics(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.prefilter_sorter { + tag = "type", + sorter = conf.generic_sorter(opts), + }, + }):find() +end + +local function apply_checks(mod) + for k, v in pairs(mod) do + mod[k] = function(opts) + opts = opts or {} + v(opts) + end + end + + return mod +end + +return apply_checks(diagnostics) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 66f3eda..c7bceba 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -436,31 +436,43 @@ builtin.lsp_workspace_symbols = require_on_exported_call("telescope.builtin.lsp" ---@field symbol_highlights table: string -> string. Matches symbol with hl_group builtin.lsp_dynamic_workspace_symbols = require_on_exported_call("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 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_bound string|number: keep diagnostics equal or less severe wrt severity name (string) or id (number) ----@field no_sign boolean: hide LspDiagnosticSigns from Results (default: false) ----@field line_width number: set length of diagnostic entry text in Results -builtin.lsp_document_diagnostics = require_on_exported_call("telescope.builtin.lsp").diagnostics +builtin.lsp_document_diagnostics = function(...) + vim.api.nvim_err_write( + "`lsp_document_diagnostics` is deprecated and will be removed. Please use `Telescope diagnostics bufnr=0`.\n" + .. "For more information see `:help telescope.changelog-1553`\n" + ) + local new = ... + new.bufnr = 0 + require("telescope.builtin.diagnostics").get(new) +end +builtin.lsp_workspace_diagnostics = function(...) + vim.api.nvim_err_write( + "`lsp_workspace_diagnostics` is deprecated and will be removed. Please use `Telescope diagnostics`.\n" + .. "For more information see `:help telescope.changelog-1553`\n" + ) + require("telescope.builtin.diagnostics").get(...) +end + +-- +-- +-- Diagnostics Pickers +-- +-- ---- Lists LSP diagnostics for the current workspace if supported, otherwise searches in all open buffers +--- Lists diagnostics for current or all open buffers --- - Fields: ---- - `All severity flags can be passed as `string` or `number` as per `:vim.lsp.protocol.DiagnosticSeverity:` +--- - `All severity flags can be passed as `string` or `number` as per `:vim.diagnostic.severity:` --- - 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 bufnr string|number: if nil get diagnostics for all open buffers. Use 0 for current buffer ---@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_bound string|number: keep diagnostics equal or less severe wrt severity name (string) or id (number) ----@field no_sign boolean: hide LspDiagnosticSigns 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 -builtin.lsp_workspace_diagnostics = require_on_exported_call("telescope.builtin.lsp").workspace_diagnostics +---@field namespace number: limit your diagnostics to a specific namespace +builtin.diagnostics = require_on_exported_call("telescope.builtin.diagnostics").get local apply_config = function(mod) local pickers_conf = require("telescope.config").pickers diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 149cfcb..3992ba6 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -405,37 +405,6 @@ lsp.dynamic_workspace_symbols = function(opts) }):find() end -lsp.diagnostics = function(opts) - local locations = utils.diagnostics_to_tbl(opts) - - if vim.tbl_isempty(locations) then - print "No diagnostics found" - return - end - - opts.path_display = utils.get_default(opts.path_display, "hidden") - pickers.new(opts, { - prompt_title = "LSP Document Diagnostics", - finder = finders.new_table { - results = locations, - entry_maker = opts.entry_maker or make_entry.gen_from_lsp_diagnostics(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.prefilter_sorter { - tag = "type", - sorter = conf.generic_sorter(opts), - }, - }):find() -end - -lsp.workspace_diagnostics = function(opts) - opts = utils.get_default(opts, {}) - opts.path_display = utils.get_default(opts.path_display, {}) - 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 1e48c8c..2d53248 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -979,33 +979,33 @@ function make_entry.gen_from_ctags(opts) end end -function make_entry.gen_from_lsp_diagnostics(opts) +function make_entry.gen_from_diagnostics(opts) opts = opts or {} - local lsp_type_diagnostic = vim.lsp.protocol.DiagnosticSeverity - - local signs - if not opts.no_sign then - signs = {} - for severity, _ in pairs(lsp_type_diagnostic) do - -- pcall to catch entirely unbound or cleared out sign hl group - 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 + local signs = (function() + if opts.no_sign then + return + end + local signs = {} + local type_diagnostic = vim.diagnostic.severity + for _, severity in ipairs(type_diagnostic) do + local status, sign = pcall(function() + -- only the first char is upper all others are lowercalse + return vim.trim(vim.fn.sign_getdefined("DiagnosticSign" .. severity:lower():gsub("^%l", string.upper))[1].text) + end) + if not status then + sign = severity:sub(1, 1) end + signs[severity] = sign end - end + return signs + end)() local display_items = { { width = utils.if_nil(signs, 8, 10) }, { remaining = true }, } - local line_width = utils.get_default(opts.line_width, 0.5) + local line_width = vim.F.if_nil(opts.line_width, 0.5) if not utils.is_path_hidden(opts) then table.insert(display_items, 2, { width = line_width }) end @@ -1021,9 +1021,13 @@ function make_entry.gen_from_lsp_diagnostics(opts) local pos = string.format("%4d:%2d", entry.lnum, entry.col) local line_info = { (signs and signs[entry.type] .. " " or "") .. pos, - "LspDiagnosticsDefault" .. entry.type, + "Diagnostic" .. entry.type, } + --TODO(conni2461): I dont like that this is symbol lnum:col | msg | filename + -- i want: symbol filename:lnum:col | msg + -- or : symbol lnum:col | msg + -- I think this is more natural return displayer { line_info, entry.text, @@ -1032,21 +1036,15 @@ function make_entry.gen_from_lsp_diagnostics(opts) end return function(entry) - local filename = entry.filename or vim.api.nvim_buf_get_name(entry.bufnr) - return { - valid = true, - value = entry, - ordinal = (not opts.ignore_filename and filename or "") .. " " .. entry.text, + ordinal = ("%s %s"):format(not opts.ignore_filename and entry.filename or "", entry.text), display = make_display, - filename = filename, + filename = entry.filename, type = entry.type, lnum = entry.lnum, col = entry.col, text = entry.text, - start = entry.start, - finish = entry.finish, } end end diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index c9a5bbe..7fba7e9 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -156,107 +156,6 @@ utils.filter_symbols = function(results, opts) -- only account for string|table as function otherwise already printed message and returned nil local symbols = type(opts.symbols) == "string" and opts.symbols or table.concat(opts.symbols, ", ") print(string.format("%s symbol(s) were not part of the query results", symbols)) - return -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() - - 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 ipairs { 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) - local start = diag.range["start"] - local finish = diag.range["end"] - local row = start.line - local col = start.character - - local buffer_diag = { - bufnr = bufnr, - filename = filename, - lnum = row + 1, - col = col + 1, - start = start, - finish = finish, - -- 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], - } - return buffer_diag - 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 ipairs(diags) do - -- workspace diagnostics may include empty tables for unused bufnr - if not vim.tbl_isempty(diag) then - 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 utils.path_smart = (function() |
