From 82b77900d7f85088c676be886937628230d0d5c2 Mon Sep 17 00:00:00 2001 From: Jaehwang Jung Date: Sat, 4 Mar 2023 22:06:20 +0900 Subject: docs(diagnostic): number → integer (#22512) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- runtime/lua/vim/diagnostic.lua | 46 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 6fd000a029..56532d0184 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -630,7 +630,7 @@ end --- Options: --- * reverse: (boolean) Reverse sort order --- ----@param namespace number|nil Update the options for the given namespace. When omitted, update the +---@param namespace integer|nil Update the options for the given namespace. When omitted, update the --- global diagnostic options. function M.config(opts, namespace) vim.validate({ @@ -674,8 +674,8 @@ end --- Set diagnostics for the given namespace and buffer. --- ----@param namespace number The diagnostic namespace ----@param bufnr number Buffer number +---@param namespace integer The diagnostic namespace +---@param bufnr integer Buffer number ---@param diagnostics table A list of diagnostic items |diagnostic-structure| ---@param opts table|nil Display options to pass to |vim.diagnostic.show()| function M.set(namespace, bufnr, diagnostics, opts) @@ -711,7 +711,7 @@ end --- Get namespace metadata. --- ----@param namespace number Diagnostic namespace +---@param namespace integer Diagnostic namespace ---@return table Namespace metadata function M.get_namespace(namespace) vim.validate({ namespace = { namespace, 'n' } }) @@ -743,11 +743,11 @@ function M.get_namespaces() end ---@class Diagnostic ----@field buffer number ----@field lnum number 0-indexed ----@field end_lnum nil|number 0-indexed ----@field col number 0-indexed ----@field end_col nil|number 0-indexed +---@field buffer integer +---@field lnum integer 0-indexed +---@field end_lnum nil|integer 0-indexed +---@field col integer 0-indexed +---@field end_col nil|integer 0-indexed ---@field severity DiagnosticSeverity ---@field message string ---@field source nil|string @@ -756,7 +756,7 @@ end --- Get current diagnostics. --- ----@param bufnr number|nil Buffer number to get diagnostics from. Use 0 for +---@param bufnr integer|nil Buffer number to get diagnostics from. Use 0 for --- current buffer or nil for all buffers. ---@param opts table|nil A table with the following keys: --- - namespace: (number) Limit diagnostics to the given namespace. @@ -1083,9 +1083,9 @@ end --- To hide diagnostics and prevent them from re-displaying, use --- |vim.diagnostic.disable()|. --- ----@param namespace number|nil Diagnostic namespace. When omitted, hide +---@param namespace integer|nil Diagnostic namespace. When omitted, hide --- diagnostics from all namespaces. ----@param bufnr number|nil Buffer number, or 0 for current buffer. When +---@param bufnr integer|nil Buffer number, or 0 for current buffer. When --- omitted, hide diagnostics in all buffers. function M.hide(namespace, bufnr) vim.validate({ @@ -1108,8 +1108,8 @@ end --- Check whether diagnostics are disabled in a given buffer. --- ----@param bufnr number|nil Buffer number, or 0 for current buffer. ----@param namespace number|nil Diagnostic namespace. When omitted, checks if +---@param bufnr integer|nil Buffer number, or 0 for current buffer. +---@param namespace integer|nil Diagnostic namespace. When omitted, checks if --- all diagnostics are disabled in {bufnr}. --- Otherwise, only checks if diagnostics from --- {namespace} are disabled. @@ -1129,9 +1129,9 @@ end --- Display diagnostics for the given namespace and buffer. --- ----@param namespace number|nil Diagnostic namespace. When omitted, show +---@param namespace integer|nil Diagnostic namespace. When omitted, show --- diagnostics from all namespaces. ----@param bufnr number|nil Buffer number, or 0 for current buffer. When omitted, show +---@param bufnr integer|nil Buffer number, or 0 for current buffer. When omitted, show --- diagnostics in all buffers. ---@param diagnostics table|nil The diagnostics to display. When omitted, use the --- saved diagnostics for the given namespace and @@ -1256,7 +1256,7 @@ end --- Overrides the setting from |vim.diagnostic.config()|. --- - suffix: Same as {prefix}, but appends the text to the diagnostic instead of --- prepending it. Overrides the setting from |vim.diagnostic.config()|. ----@return number|nil, number|nil: ({float_bufnr}, {win_id}) +---@return integer|nil, integer|nil: ({float_bufnr}, {win_id}) function M.open_float(opts, ...) -- Support old (bufnr, opts) signature local bufnr @@ -1463,9 +1463,9 @@ end --- simply remove diagnostic decorations in a way that they can be --- re-displayed, use |vim.diagnostic.hide()|. --- ----@param namespace number|nil Diagnostic namespace. When omitted, remove +---@param namespace integer|nil Diagnostic namespace. When omitted, remove --- diagnostics from all namespaces. ----@param bufnr number|nil Remove diagnostics for the given buffer. When omitted, +---@param bufnr integer|nil Remove diagnostics for the given buffer. When omitted, --- diagnostics are removed for all buffers. function M.reset(namespace, bufnr) vim.validate({ @@ -1518,9 +1518,9 @@ end --- Disable diagnostics in the given buffer. --- ----@param bufnr number|nil Buffer number, or 0 for current buffer. When +---@param bufnr integer|nil Buffer number, or 0 for current buffer. When --- omitted, disable diagnostics in all buffers. ----@param namespace number|nil Only disable diagnostics for the given namespace. +---@param namespace integer|nil Only disable diagnostics for the given namespace. function M.disable(bufnr, namespace) vim.validate({ bufnr = { bufnr, 'n', true }, namespace = { namespace, 'n', true } }) if bufnr == nil then @@ -1555,9 +1555,9 @@ end --- Enable diagnostics in the given buffer. --- ----@param bufnr number|nil Buffer number, or 0 for current buffer. When +---@param bufnr integer|nil Buffer number, or 0 for current buffer. When --- omitted, enable diagnostics in all buffers. ----@param namespace number|nil Only enable diagnostics for the given namespace. +---@param namespace integer|nil Only enable diagnostics for the given namespace. function M.enable(bufnr, namespace) vim.validate({ bufnr = { bufnr, 'n', true }, namespace = { namespace, 'n', true } }) if bufnr == nil then -- cgit From 226a6c3eaef2a7220841d3d5e69e1baf543b3d6f Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 30 Mar 2023 14:49:58 +0100 Subject: feat(diagnostic): add support for tags The LSP spec supports two tags that can be added to diagnostics: unnecessary and deprecated. Extend vim.diagnostic to be able to handle these. --- runtime/lua/vim/diagnostic.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 56532d0184..714038f8e4 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -483,6 +483,7 @@ local function next_diagnostic(position, search_forward, bufnr, opts, namespace) local diagnostics = get_diagnostics(bufnr, vim.tbl_extend('keep', opts, { namespace = namespace }), true) local line_diagnostics = diagnostic_lines(diagnostics) + for i = 0, line_count do local offset = i * (search_forward and 1 or -1) local lnum = position[1] + offset @@ -752,6 +753,7 @@ end ---@field message string ---@field source nil|string ---@field code nil|string +---@field _tags { deprecated: boolean, unnecessary: boolean} ---@field user_data nil|any arbitrary data plugins can add --- Get current diagnostics. @@ -948,6 +950,16 @@ M.handlers.underline = { higroup = underline_highlight_map.Error end + if diagnostic._tags then + -- TODO(lewis6991): we should be able to stack these. + if diagnostic._tags.unnecessary then + higroup = 'DiagnosticUnnecessary' + end + if diagnostic._tags.deprecated then + higroup = 'DiagnosticDeprecated' + end + end + vim.highlight.range( bufnr, underline_ns, -- cgit From 37011bc45ef333776bccea84a1a7ced6c68e3b51 Mon Sep 17 00:00:00 2001 From: Mathias Fußenegger Date: Wed, 12 Apr 2023 15:16:15 +0200 Subject: fix(diagnostic): rename buffer → bufnr in type annotation (#23042) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See `:h diagnostic-structure`, the property name is `bufnr`, not `buffer`. --- runtime/lua/vim/diagnostic.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 714038f8e4..d1b50304c7 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -744,7 +744,7 @@ function M.get_namespaces() end ---@class Diagnostic ----@field buffer integer +---@field bufnr integer ---@field lnum integer 0-indexed ---@field end_lnum nil|integer 0-indexed ---@field col integer 0-indexed -- cgit From 07b60efd8058bb515998f50048b511d50f9671f8 Mon Sep 17 00:00:00 2001 From: Isak Samsten Date: Mon, 17 Apr 2023 13:53:34 +0200 Subject: feat(diagnostic): specify diagnostic virtual text prefix as a function - vim.diagnostic.config() now accepts a function for the virtual_text.prefix option, which allows for rendering e.g., diagnostic severities differently. --- runtime/lua/vim/diagnostic.lua | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index d1b50304c7..0d1d01b391 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -600,7 +600,10 @@ end --- means to always show the diagnostic source. --- * spacing: (number) Amount of empty spaces inserted at the beginning --- of the virtual text. ---- * prefix: (string) Prepend diagnostic message with prefix. +--- * prefix: (string or function) prepend diagnostic message with prefix. +--- If a function, it must have the signature (diagnostic) -> string, +--- where {diagnostic} is of type |diagnostic-structure|. This can be +--- used to render diagnostic symbols or error codes. --- * suffix: (string or function) Append diagnostic message with suffix. --- If a function, it must have the signature (diagnostic) -> --- string, where {diagnostic} is of type |diagnostic-structure|. @@ -1066,8 +1069,15 @@ function M._get_virt_text_chunks(line_diags, opts) -- Create a little more space between virtual text and contents local virt_texts = { { string.rep(' ', spacing) } } - for i = 1, #line_diags - 1 do - table.insert(virt_texts, { prefix, virtual_text_highlight_map[line_diags[i].severity] }) + for i = 1, #line_diags do + local resolved_prefix = prefix + if type(prefix) == 'function' then + resolved_prefix = prefix(line_diags[i]) or '' + end + table.insert( + virt_texts, + { resolved_prefix, virtual_text_highlight_map[line_diags[i].severity] } + ) end local last = line_diags[#line_diags] @@ -1078,7 +1088,7 @@ function M._get_virt_text_chunks(line_diags, opts) suffix = suffix(last) or '' end table.insert(virt_texts, { - string.format('%s %s%s', prefix, last.message:gsub('\r', ''):gsub('\n', ' '), suffix), + string.format(' %s%s', last.message:gsub('\r', ''):gsub('\n', ' '), suffix), virtual_text_highlight_map[last.severity], }) -- cgit From 929e4865d1f59cb356f3f2f8a10ad6095b435544 Mon Sep 17 00:00:00 2001 From: NAKAI Tsuyoshi <82267684+uga-rosa@users.noreply.github.com> Date: Wed, 28 Jun 2023 00:14:17 +0900 Subject: docs(diagnostic): return value of get() #24144 --- runtime/lua/vim/diagnostic.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 0d1d01b391..093bfb631e 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -767,7 +767,7 @@ end --- - namespace: (number) Limit diagnostics to the given namespace. --- - lnum: (number) Limit diagnostics to the given line number. --- - severity: See |diagnostic-severity|. ----@return Diagnostic[] table A list of diagnostic items |diagnostic-structure|. +---@return Diagnostic[] table A list of diagnostic items |diagnostic-structure|. Keys `bufnr`, `end_lnum`, `end_col`, and `severity` are guaranteed to be present. function M.get(bufnr, opts) vim.validate({ bufnr = { bufnr, 'n', true }, -- cgit From be74807eef13ff8c90d55cf8b22b01d6d33b1641 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 18 Jul 2023 15:42:30 +0100 Subject: docs(lua): more improvements (#24387) * docs(lua): teach lua2dox how to table * docs(lua): teach gen_vimdoc.py about local functions No more need to mark local functions with @private * docs(lua): mention @nodoc and @meta in dev-lua-doc * fixup! Co-authored-by: Justin M. Keyes --------- Co-authored-by: Justin M. Keyes --- runtime/lua/vim/diagnostic.lua | 24 ------------------------ 1 file changed, 24 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 093bfb631e..f0adc4104e 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -72,7 +72,6 @@ local bufs_waiting_to_update = setmetatable({}, bufnr_and_namespace_cacher_mt) local all_namespaces = {} ----@private local function to_severity(severity) if type(severity) == 'string' then return assert( @@ -83,7 +82,6 @@ local function to_severity(severity) return severity end ----@private local function filter_by_severity(severity, diagnostics) if not severity then return diagnostics @@ -104,7 +102,6 @@ local function filter_by_severity(severity, diagnostics) end, diagnostics) end ----@private local function count_sources(bufnr) local seen = {} local count = 0 @@ -119,7 +116,6 @@ local function count_sources(bufnr) return count end ----@private local function prefix_source(diagnostics) return vim.tbl_map(function(d) if not d.source then @@ -132,7 +128,6 @@ local function prefix_source(diagnostics) end, diagnostics) end ----@private local function reformat_diagnostics(format, diagnostics) vim.validate({ format = { format, 'f' }, @@ -146,7 +141,6 @@ local function reformat_diagnostics(format, diagnostics) return formatted end ----@private local function enabled_value(option, namespace) local ns = namespace and M.get_namespace(namespace) or {} if ns.opts and type(ns.opts[option]) == 'table' then @@ -160,7 +154,6 @@ local function enabled_value(option, namespace) return {} end ----@private local function resolve_optional_value(option, value, namespace, bufnr) if not value then return false @@ -180,7 +173,6 @@ local function resolve_optional_value(option, value, namespace, bufnr) end end ----@private local function get_resolved_options(opts, namespace, bufnr) local ns = namespace and M.get_namespace(namespace) or {} -- Do not use tbl_deep_extend so that an empty table can be used to reset to default values @@ -202,7 +194,6 @@ local diagnostic_severities = { } -- Make a map from DiagnosticSeverity -> Highlight Name ----@private local function make_highlight_map(base_name) local result = {} for k in pairs(diagnostic_severities) do @@ -243,7 +234,6 @@ local define_default_signs = (function() end end)() ----@private local function get_bufnr(bufnr) if not bufnr or bufnr == 0 then return api.nvim_get_current_buf() @@ -251,7 +241,6 @@ local function get_bufnr(bufnr) return bufnr end ----@private local function diagnostic_lines(diagnostics) if not diagnostics then return {} @@ -269,7 +258,6 @@ local function diagnostic_lines(diagnostics) return diagnostics_by_line end ----@private local function set_diagnostic_cache(namespace, bufnr, diagnostics) for _, diagnostic in ipairs(diagnostics) do assert(diagnostic.lnum, 'Diagnostic line number is required') @@ -284,7 +272,6 @@ local function set_diagnostic_cache(namespace, bufnr, diagnostics) diagnostic_cache[bufnr][namespace] = diagnostics end ----@private local function restore_extmarks(bufnr, last) for ns, extmarks in pairs(diagnostic_cache_extmarks[bufnr]) do local extmarks_current = api.nvim_buf_get_extmarks(bufnr, ns, 0, -1, { details = true }) @@ -306,7 +293,6 @@ local function restore_extmarks(bufnr, last) end end ----@private local function save_extmarks(namespace, bufnr) bufnr = get_bufnr(bufnr) if not diagnostic_attached_buffers[bufnr] then @@ -326,13 +312,11 @@ end local registered_autocmds = {} ----@private local function make_augroup_key(namespace, bufnr) local ns = M.get_namespace(namespace) return string.format('DiagnosticInsertLeave:%s:%s', bufnr, ns.name) end ----@private local function execute_scheduled_display(namespace, bufnr) local args = bufs_waiting_to_update[bufnr][namespace] if not args then @@ -348,7 +332,6 @@ end --- Table of autocmd events to fire the update for displaying new diagnostic information local insert_leave_auto_cmds = { 'InsertLeave', 'CursorHoldI' } ----@private local function schedule_display(namespace, bufnr, args) bufs_waiting_to_update[bufnr][namespace] = args @@ -367,7 +350,6 @@ local function schedule_display(namespace, bufnr, args) end end ----@private local function clear_scheduled_display(namespace, bufnr) local key = make_augroup_key(namespace, bufnr) @@ -377,7 +359,6 @@ local function clear_scheduled_display(namespace, bufnr) end end ----@private local function get_diagnostics(bufnr, opts, clamp) opts = opts or {} @@ -392,7 +373,6 @@ local function get_diagnostics(bufnr, opts, clamp) end, }) - ---@private local function add(b, d) if not opts.lnum or d.lnum == opts.lnum then if clamp and api.nvim_buf_is_loaded(b) then @@ -416,7 +396,6 @@ local function get_diagnostics(bufnr, opts, clamp) end end - ---@private local function add_all_diags(buf, diags) for _, diagnostic in pairs(diags) do add(buf, diagnostic) @@ -450,7 +429,6 @@ local function get_diagnostics(bufnr, opts, clamp) return diagnostics end ----@private local function set_list(loclist, opts) opts = opts or {} local open = vim.F.if_nil(opts.open, true) @@ -474,7 +452,6 @@ local function set_list(loclist, opts) end end ----@private local function next_diagnostic(position, search_forward, bufnr, opts, namespace) position[1] = position[1] - 1 bufnr = get_bufnr(bufnr) @@ -525,7 +502,6 @@ local function next_diagnostic(position, search_forward, bufnr, opts, namespace) end end ----@private local function diagnostic_move_pos(opts, pos) opts = opts or {} -- cgit From c43c745a14dced87a23227d7be4f1c33d4455193 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Wed, 9 Aug 2023 11:06:13 +0200 Subject: fix(lua): improve annotations for stricter luals diagnostics (#24609) Problem: luals returns stricter diagnostics with bundled luarc.json Solution: Improve some function and type annotations: * use recognized uv.* types * disable diagnostic for global `vim` in shared.lua * docs: don't start comment lines with taglink (otherwise LuaLS will interpret it as a type) * add type alias for lpeg pattern * fix return annotation for `vim.secure.trust` * rename local Range object in vim.version (shadows `Range` in vim.treesitter) * fix some "missing fields" warnings * add missing required fields for test functions in eval.lua * rename lsp meta files for consistency --- runtime/lua/vim/diagnostic.lua | 46 ++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index f0adc4104e..180b9ad3df 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -562,8 +562,8 @@ end ---@param opts table|nil When omitted or "nil", retrieve the current configuration. Otherwise, a --- configuration table with the following keys: --- - underline: (default true) Use underline for diagnostics. Options: ---- * severity: Only underline diagnostics matching the given severity ---- |diagnostic-severity| +--- * severity: Only underline diagnostics matching the given +--- severity |diagnostic-severity| --- - virtual_text: (default true) Use virtual text for diagnostics. If multiple diagnostics --- are set for a namespace, one prefix per diagnostic + the last diagnostic --- message are shown. @@ -596,8 +596,8 @@ end --- end --- --- - signs: (default true) Use signs for diagnostics. Options: ---- * severity: Only show signs for diagnostics matching the given severity ---- |diagnostic-severity| +--- * severity: Only show signs for diagnostics matching the given +--- severity |diagnostic-severity| --- * priority: (number, default 10) Base priority to use for signs. When --- {severity_sort} is used, the priority of a sign is adjusted based on --- its severity. Otherwise, all signs use the same priority. @@ -723,17 +723,17 @@ function M.get_namespaces() end ---@class Diagnostic ----@field bufnr integer +---@field bufnr? integer ---@field lnum integer 0-indexed ----@field end_lnum nil|integer 0-indexed +---@field end_lnum? integer 0-indexed ---@field col integer 0-indexed ----@field end_col nil|integer 0-indexed ----@field severity DiagnosticSeverity +---@field end_col? integer 0-indexed +---@field severity? DiagnosticSeverity ---@field message string ----@field source nil|string ----@field code nil|string ----@field _tags { deprecated: boolean, unnecessary: boolean} ----@field user_data nil|any arbitrary data plugins can add +---@field source? string +---@field code? string +---@field _tags? { deprecated: boolean, unnecessary: boolean} +---@field user_data? any arbitrary data plugins can add --- Get current diagnostics. --- @@ -819,13 +819,13 @@ end --- ---@param opts table|nil Configuration table with the following keys: --- - namespace: (number) Only consider diagnostics from the given namespace. ---- - cursor_position: (cursor position) Cursor position as a (row, col) tuple. See ---- |nvim_win_get_cursor()|. Defaults to the current cursor position. +--- - cursor_position: (cursor position) Cursor position as a (row, col) tuple. +--- See |nvim_win_get_cursor()|. Defaults to the current cursor position. --- - wrap: (boolean, default true) Whether to loop around file or not. Similar to 'wrapscan'. --- - severity: See |diagnostic-severity|. --- - float: (boolean or table, default true) If "true", call |vim.diagnostic.open_float()| ---- after moving. If a table, pass the table as the {opts} parameter to ---- |vim.diagnostic.open_float()|. Unless overridden, the float will show +--- after moving. If a table, pass the table as the {opts} parameter +--- to |vim.diagnostic.open_float()|. Unless overridden, the float will show --- diagnostics at the new cursor position (as if "cursor" were passed to --- the "scope" option). --- - win_id: (number, default 0) Window ID @@ -1213,8 +1213,8 @@ end --- Show diagnostics in a floating window. --- ----@param opts table|nil Configuration table with the same keys as ---- |vim.lsp.util.open_floating_preview()| in addition to the following: +---@param opts table|nil Configuration table with the same keys +--- as |vim.lsp.util.open_floating_preview()| in addition to the following: --- - bufnr: (number) Buffer number to show diagnostics from. --- Defaults to the current buffer. --- - namespace: (number) Limit diagnostics to the given namespace @@ -1227,16 +1227,15 @@ end --- otherwise, a (row, col) tuple. --- - severity_sort: (default false) Sort diagnostics by severity. Overrides the setting --- from |vim.diagnostic.config()|. ---- - severity: See |diagnostic-severity|. Overrides the setting from ---- |vim.diagnostic.config()|. +--- - severity: See |diagnostic-severity|. Overrides the setting +--- from |vim.diagnostic.config()|. --- - header: (string or table) String to use as the header for the floating window. If a --- table, it is interpreted as a [text, hl_group] tuple. Overrides the setting --- from |vim.diagnostic.config()|. --- - source: (boolean or string) Include the diagnostic source in the message. --- Use "if_many" to only show sources if there is more than one source of --- diagnostics in the buffer. Otherwise, any truthy value means to always show ---- the diagnostic source. Overrides the setting from ---- |vim.diagnostic.config()|. +--- the diagnostic source. Overrides the setting from |vim.diagnostic.config()|. --- - format: (function) A function that takes a diagnostic as input and returns a --- string. The return value is the text used to display the diagnostic. --- Overrides the setting from |vim.diagnostic.config()|. @@ -1692,8 +1691,7 @@ end --- Convert a list of quickfix items to a list of diagnostics. --- ----@param list table A list of quickfix items from |getqflist()| or ---- |getloclist()|. +---@param list table[] List of quickfix items from |getqflist()| or |getloclist()|. ---@return Diagnostic[] array of |diagnostic-structure| function M.fromqflist(list) vim.validate({ -- cgit From 9cb7e00b9748b08fce661f8cbeb06c5994c749ae Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Wed, 16 Aug 2023 08:21:32 -0500 Subject: feat(diagnostic): provide more control over virtual text display (#24724) Allow users to pass virtual text options to nvim_buf_set_extmark through the "virtual_text" table in vim.diagnostic.config(). Fixes: https://github.com/neovim/neovim/issues/16545 --- runtime/lua/vim/diagnostic.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 180b9ad3df..1391dafd75 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -566,7 +566,9 @@ end --- severity |diagnostic-severity| --- - virtual_text: (default true) Use virtual text for diagnostics. If multiple diagnostics --- are set for a namespace, one prefix per diagnostic + the last diagnostic ---- message are shown. +--- message are shown. In addition to the options listed below, the +--- "virt_text" options of |nvim_buf_set_extmark()| may also be used here +--- (e.g. "virt_text_pos" and "hl_mode"). --- Options: --- * severity: Only show virtual text for diagnostics matching the given --- severity |diagnostic-severity| @@ -1008,8 +1010,11 @@ M.handlers.virtual_text = { if virt_texts then api.nvim_buf_set_extmark(bufnr, virt_text_ns, line, 0, { - hl_mode = 'combine', + hl_mode = opts.virtual_text.hl_mode or 'combine', virt_text = virt_texts, + virt_text_pos = opts.virtual_text.virt_text_pos, + virt_text_hide = opts.virtual_text.virt_text_hide, + virt_text_win_col = opts.virtual_text.virt_text_win_col, }) end end -- cgit From e7801775060e2d8f9f20572fac687f438e81caa0 Mon Sep 17 00:00:00 2001 From: Michael Strobel <71396679+Kibadda@users.noreply.github.com> Date: Wed, 16 Aug 2023 15:49:14 +0200 Subject: feat(diagnostic): filter diagnostics by specific severities (#24736) Allow users to filter diagnostics by specifying severities --- runtime/lua/vim/diagnostic.lua | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 1391dafd75..96d1cb7629 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -94,11 +94,22 @@ local function filter_by_severity(severity, diagnostics) end, diagnostics) end - local min_severity = to_severity(severity.min) or M.severity.HINT - local max_severity = to_severity(severity.max) or M.severity.ERROR + if severity.min or severity.max then + local min_severity = to_severity(severity.min) or M.severity.HINT + local max_severity = to_severity(severity.max) or M.severity.ERROR + + return vim.tbl_filter(function(t) + return t.severity <= min_severity and t.severity >= max_severity + end, diagnostics) + end + + local severities = {} + for _, s in ipairs(severity) do + severities[to_severity(s)] = true + end return vim.tbl_filter(function(t) - return t.severity <= min_severity and t.severity >= max_severity + return severities[t.severity] end, diagnostics) end -- cgit From d27214331815324ea5762b5aa22996b9019085c6 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Wed, 6 Sep 2023 20:54:18 +0300 Subject: fix(diagnostic): always return copies of diagnostic items (#25010) --- runtime/lua/vim/diagnostic.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 96d1cb7629..a1f3020c88 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -386,6 +386,7 @@ local function get_diagnostics(bufnr, opts, clamp) local function add(b, d) if not opts.lnum or d.lnum == opts.lnum then + d = vim.deepcopy(d) if clamp and api.nvim_buf_is_loaded(b) then local line_count = buf_line_count[b] - 1 if @@ -396,7 +397,6 @@ local function get_diagnostics(bufnr, opts, clamp) or d.col < 0 or d.end_col < 0 then - d = vim.deepcopy(d) d.lnum = math.max(math.min(d.lnum, line_count), 0) d.end_lnum = math.max(math.min(d.end_lnum, line_count), 0) d.col = math.max(d.col, 0) @@ -750,6 +750,8 @@ end --- Get current diagnostics. --- +--- Modifying diagnostics in the returned table has no effect. To set diagnostics in a buffer, use |vim.diagnostic.set()|. +--- ---@param bufnr integer|nil Buffer number to get diagnostics from. Use 0 for --- current buffer or nil for all buffers. ---@param opts table|nil A table with the following keys: -- cgit From 2e92065686f62851318150a315591c30b8306a4b Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 14 Sep 2023 08:23:01 -0500 Subject: docs: replace
 with ``` (#25136)

---
 runtime/lua/vim/diagnostic.lua | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

(limited to 'runtime/lua/vim/diagnostic.lua')

diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
index a1f3020c88..b8d3906b7f 100644
--- a/runtime/lua/vim/diagnostic.lua
+++ b/runtime/lua/vim/diagnostic.lua
@@ -553,14 +553,16 @@ end
 --- followed by namespace configuration, and finally global configuration.
 ---
 --- For example, if a user enables virtual text globally with
---- 
lua
----   vim.diagnostic.config({ virtual_text = true })
---- 
+--- +--- ```lua +--- vim.diagnostic.config({ virtual_text = true }) +--- ``` --- --- and a diagnostic producer sets diagnostics with ----
lua
----   vim.diagnostic.set(ns, 0, diagnostics, { virtual_text = false })
---- 
+--- +--- ```lua +--- vim.diagnostic.set(ns, 0, diagnostics, { virtual_text = false }) +--- ``` --- --- then virtual text will not be enabled for those diagnostics. --- @@ -1601,18 +1603,20 @@ end --- Parse a diagnostic from a string. --- --- For example, consider a line of output from a linter: ----
+---
+--- ```
 --- WARNING filename:27:3: Variable 'foo' does not exist
---- 
+--- ``` --- --- This can be parsed into a diagnostic |diagnostic-structure| --- with: ----
lua
----   local s = "WARNING filename:27:3: Variable 'foo' does not exist"
----   local pattern = "^(%w+) %w+:(%d+):(%d+): (.+)$"
----   local groups = { "severity", "lnum", "col", "message" }
----   vim.diagnostic.match(s, pattern, groups, { WARNING = vim.diagnostic.WARN })
---- 
+--- +--- ```lua +--- local s = "WARNING filename:27:3: Variable 'foo' does not exist" +--- local pattern = "^(%w+) %w+:(%d+):(%d+): (.+)$" +--- local groups = { "severity", "lnum", "col", "message" } +--- vim.diagnostic.match(s, pattern, groups, { WARNING = vim.diagnostic.WARN }) +--- ``` --- ---@param str string String to parse diagnostics from. ---@param pat string Lua pattern with capture groups. -- cgit From 35f475d0a51aad03abfc005a0224d315c45ffd67 Mon Sep 17 00:00:00 2001 From: Jaehwang Jung Date: Tue, 17 Oct 2023 10:51:36 +0900 Subject: fix(diagnostics): if buffer not loaded, skip handlers that set extmark (#25628) Problem: When enabling diagnostics, there can be diagnostics for unloaded buffer, but some handlers nevertheless attempt to set extmarks in such buffers. Solution: * Exit underline/virtual_text handler if buffer is not loaded. * Don't require is_loaded as precondition for show(), because handlers don't necessarily depend on it. --- runtime/lua/vim/diagnostic.lua | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index b8d3906b7f..9e9b09ed1d 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -652,16 +652,14 @@ function M.config(opts, namespace) if namespace then for bufnr, v in pairs(diagnostic_cache) do - if api.nvim_buf_is_loaded(bufnr) and v[namespace] then + if v[namespace] then M.show(namespace, bufnr) end end else for bufnr, v in pairs(diagnostic_cache) do - if api.nvim_buf_is_loaded(bufnr) then - for ns in pairs(v) do - M.show(ns, bufnr) - end + for ns in pairs(v) do + M.show(ns, bufnr) end end end @@ -693,9 +691,7 @@ function M.set(namespace, bufnr, diagnostics, opts) set_diagnostic_cache(namespace, bufnr, diagnostics) end - if api.nvim_buf_is_loaded(bufnr) then - M.show(namespace, bufnr, nil, opts) - end + M.show(namespace, bufnr, nil, opts) api.nvim_exec_autocmds('DiagnosticChanged', { modeline = false, @@ -928,6 +924,10 @@ M.handlers.underline = { bufnr = get_bufnr(bufnr) opts = opts or {} + if not vim.api.nvim_buf_is_loaded(bufnr) then + return + end + if opts.underline and opts.underline.severity then diagnostics = filter_by_severity(opts.underline.severity, diagnostics) end @@ -994,6 +994,10 @@ M.handlers.virtual_text = { bufnr = get_bufnr(bufnr) opts = opts or {} + if not vim.api.nvim_buf_is_loaded(bufnr) then + return + end + local severity if opts.virtual_text then if opts.virtual_text.format then -- cgit From add1b10b79011d1af61419a63cc8ef4645f45bbf Mon Sep 17 00:00:00 2001 From: Jongwook Choi Date: Fri, 27 Oct 2023 09:17:46 -0400 Subject: fix(diagnostic): virtual_text prefix function should have index and total (#25801) The prefix option of the diagnostic virtual text can be a function, but previously it was only a function of diagnostic. This function should also have additional parameters index and total, more consistently and similarily as in the prefix function for `vim.diagnostic.open_float()`. These additional parameters will be useful when there are too many number of diagnostics in a single line. --- runtime/lua/vim/diagnostic.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 9e9b09ed1d..99448982b4 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -592,8 +592,10 @@ end --- * spacing: (number) Amount of empty spaces inserted at the beginning --- of the virtual text. --- * prefix: (string or function) prepend diagnostic message with prefix. ---- If a function, it must have the signature (diagnostic) -> string, ---- where {diagnostic} is of type |diagnostic-structure|. This can be +--- If a function, it must have the signature (diagnostic, i, total) +--- -> string, where {diagnostic} is of type |diagnostic-structure|, +--- {i} is the index of the diagnostic being evaluated, and {total} +--- is the total number of diagnostics for the line. This can be --- used to render diagnostic symbols or error codes. --- * suffix: (string or function) Append diagnostic message with suffix. --- If a function, it must have the signature (diagnostic) -> @@ -1072,7 +1074,7 @@ function M._get_virt_text_chunks(line_diags, opts) for i = 1, #line_diags do local resolved_prefix = prefix if type(prefix) == 'function' then - resolved_prefix = prefix(line_diags[i]) or '' + resolved_prefix = prefix(line_diags[i], i, #line_diags) or '' end table.insert( virt_texts, -- cgit