local api, if_nil = vim.api, vim.F.if_nil local M = {} ---@enum DiagnosticSeverity M.severity = { ERROR = 1, WARN = 2, INFO = 3, HINT = 4, } vim.tbl_add_reverse_lookup(M.severity) -- Mappings from qflist/loclist error types to severities M.severity.E = M.severity.ERROR M.severity.W = M.severity.WARN M.severity.I = M.severity.INFO M.severity.N = M.severity.HINT local global_diagnostic_options = { signs = true, underline = true, virtual_text = true, float = true, update_in_insert = false, severity_sort = false, } M.handlers = setmetatable({}, { __newindex = function(t, name, handler) vim.validate({ handler = { handler, 't' } }) rawset(t, name, handler) if global_diagnostic_options[name] == nil then global_diagnostic_options[name] = true end end, }) -- Metatable that automatically creates an empty table when assigning to a missing key local bufnr_and_namespace_cacher_mt = { __index = function(t, bufnr) assert(bufnr > 0, 'Invalid buffer number') t[bufnr] = {} return t[bufnr] end, } local diagnostic_cache do local group = api.nvim_create_augroup('DiagnosticBufWipeout', {}) diagnostic_cache = setmetatable({}, { __index = function(t, bufnr) assert(bufnr > 0, 'Invalid buffer number') api.nvim_create_autocmd('BufWipeout', { group = group, buffer = bufnr, callback = function() rawset(t, bufnr, nil) end, }) t[bufnr] = {} return t[bufnr] end, }) end local diagnostic_cache_extmarks = setmetatable({}, bufnr_and_namespace_cacher_mt) local diagnostic_attached_buffers = {} local diagnostic_disabled = {} local bufs_waiting_to_update = setmetatable({}, bufnr_and_namespace_cacher_mt) local all_namespaces = {} local function to_severity(severity) if type(severity) == 'string' then return assert( M.severity[string.upper(severity)], string.format('Invalid severity: %s', severity) ) end return severity end local function filter_by_severity(severity, diagnostics) if not severity then return diagnostics end if type(severity) ~= 'table' then severity = to_severity(severity) return vim.tbl_filter(function(t) return t.severity == severity end, diagnostics) end 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 --- @param t table return vim.tbl_filter(function(t) return severities[t.severity] end, diagnostics) end local function count_sources(bufnr) local seen = {} local count = 0 for _, namespace_diagnostics in pairs(diagnostic_cache[bufnr]) do for _, diagnostic in ipairs(namespace_diagnostics) do if diagnostic.source and not seen[diagnostic.source] then seen[diagnostic.source] = true count = count + 1 end end end return count end local function prefix_source(diagnostics) return vim.tbl_map(function(d) if not d.source then return d end local t = vim.deepcopy(d, true) t.message = string.format('%s: %s', d.source, d.message) return t end, diagnostics) end local function reformat_diagnostics(format, diagnostics) vim.validate({ format = { format, 'f' }, diagnostics = { diagnostics, 't' }, }) local formatted = vim.deepcopy(diagnostics, true) for _, diagnostic in ipairs(formatted) do diagnostic.message = format(diagnostic) end return formatted end 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 return ns.opts[option] end if type(global_diagnostic_options[option]) == 'table' then return global_diagnostic_options[option] end return {} end local function resolve_optional_value(option, value, namespace, bufnr) if not value then return false elseif value == true then return enabled_value(option, namespace) elseif type(value) == 'function' then local val = value(namespace, bufnr) if val == true then return enabled_value(option, namespace) else return val end elseif type(value) == 'table' then return value else error('Unexpected option type: ' .. vim.inspect(value)) end end 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 local resolved = vim.tbl_extend('keep', opts or {}, ns.opts or {}, global_diagnostic_options) for k in pairs(global_diagnostic_options) do if resolved[k] ~= nil then resolved[k] = resolve_optional_value(k, resolved[k], namespace, bufnr) end end return resolved end -- Default diagnostic highlights local diagnostic_severities = { [M.severity.ERROR] = { ctermfg = 1, guifg = 'Red' }, [M.severity.WARN] = { ctermfg = 3, guifg = 'Orange' }, [M.severity.INFO] = { ctermfg = 4, guifg = 'LightBlue' }, [M.severity.HINT] = { ctermfg = 7, guifg = 'LightGrey' }, } -- Make a map from DiagnosticSeverity -> Highlight Name local function make_highlight_map(base_name) local result = {} for k in pairs(diagnostic_severities) do local name = M.severity[k] name = name:sub(1, 1) .. name:sub(2):lower() result[k] = 'Diagnostic' .. base_name .. name end return result end local virtual_text_highlight_map = make_highlight_map('VirtualText') local underline_highlight_map = make_highlight_map('Underline') local floating_highlight_map = make_highlight_map('Floating') local sign_highlight_map = make_highlight_map('Sign') local function get_bufnr(bufnr) if not bufnr or bufnr == 0 then return api.nvim_get_current_buf() end return bufnr end local function diagnostic_lines(diagnostics) if not diagnostics then return {} end local diagnostics_by_line = {} for _, diagnostic in ipairs(diagnostics) do local line_diagnostics = diagnostics_by_line[diagnostic.lnum] if not line_diagnostics then line_diagnostics = {} diagnostics_by_line[diagnostic.lnum] = line_diagnostics end table.insert(line_diagnostics, diagnostic) end return diagnostics_by_line end local function set_diagnostic_cache(namespace, bufnr, diagnostics) for _, diagnostic in ipairs(diagnostics) do assert(diagnostic.lnum, 'Diagnostic line number is required') assert(diagnostic.col, 'Diagnostic column is required') diagnostic.severity = diagnostic.severity and to_severity(diagnostic.severity) or M.severity.ERROR diagnostic.end_lnum = diagnostic.end_lnum or diagnostic.lnum diagnostic.end_col = diagnostic.end_col or diagnostic.col diagnostic.namespace = namespace diagnostic.bufnr = bufnr end diagnostic_cache[bufnr][namespace] = diagnostics end 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 }) local found = {} for _, extmark in ipairs(extmarks_current) do -- nvim_buf_set_lines will move any extmark to the line after the last -- nvim_buf_set_text will move any extmark to the last line if extmark[2] ~= last + 1 then found[extmark[1]] = true end end for _, extmark in ipairs(extmarks) do if not found[extmark[1]] then local opts = extmark[4] opts.id = extmark[1] pcall(api.nvim_buf_set_extmark, bufnr, ns, extmark[2], extmark[3], opts) end end end end local function save_extmarks(namespace, bufnr) bufnr = get_bufnr(bufnr) if not diagnostic_attached_buffers[bufnr] then api.nvim_buf_attach(bufnr, false, { on_lines = function(_, _, _, _, _, last) restore_extmarks(bufnr, last - 1) end, on_detach = function() diagnostic_cache_extmarks[bufnr] = nil end, }) diagnostic_attached_buffers[bufnr] = true end diagnostic_cache_extmarks[bufnr][namespace] = api.nvim_buf_get_extmarks(bufnr, namespace, 0, -1, { details = true }) end local registered_autocmds = {} local function make_augroup_key(namespace, bufnr) local ns = M.get_namespace(namespace) return string.format('DiagnosticInsertLeave:%s:%s', bufnr, ns.name) end local function execute_scheduled_display(namespace, bufnr) local args = bufs_waiting_to_update[bufnr][namespace] if not args then return end -- Clear the args so we don't display unnecessarily. bufs_waiting_to_update[bufnr][namespace] = nil M.show(namespace, bufnr, nil, args) end --- Table of autocmd events to fire the update for displaying new diagnostic information local insert_leave_auto_cmds = { 'InsertLeave', 'CursorHoldI' } local function schedule_display(namespace, bufnr, args) bufs_waiting_to_update[bufnr][namespace] = args local key = make_augroup_key(namespace, bufnr) if not registered_autocmds[key] then local group = api.nvim_create_augroup(key, { clear = true }) api.nvim_create_autocmd(insert_leave_auto_cmds, { group = group, buffer = bufnr, callback = function() execute_scheduled_display(namespace, bufnr) end, desc = 'vim.diagnostic: display diagnostics', }) registered_autocmds[key] = true end end local function clear_scheduled_display(namespace, bufnr) local key = make_augroup_key(namespace, bufnr) if registered_autocmds[key] then api.nvim_del_augroup_by_name(key) registered_autocmds[key] = nil end end local function get_diagnostics(bufnr, opts, clamp) opts = opts or {} local namespace = opts.namespace local diagnostics = {} -- Memoized results of buf_line_count per bufnr local buf_line_count = setmetatable({}, { __index = function(t, k) t[k] = api.nvim_buf_line_count(k) return rawget(t, k) end, }) 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 local line_count = buf_line_count[b] - 1 if d.lnum > line_count or d.end_lnum > line_count or d.lnum < 0 or d.end_lnum < 0 or d.col < 0 or d.end_col < 0 then d = vim.deepcopy(d, true) 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) d.end_col = math.max(d.end_col, 0) end end table.insert(diagnostics, d) end end local function add_all_diags(buf, diags) for _, diagnostic in pairs(diags) do add(buf, diagnostic) end end if namespace == nil and bufnr == nil then for b, t in pairs(diagnostic_cache) do for _, v in pairs(t) do add_all_diags(b, v) end end elseif namespace == nil then bufnr = get_bufnr(bufnr) for iter_namespace in pairs(diagnostic_cache[bufnr]) do add_all_diags(bufnr, diagnostic_cache[bufnr][iter_namespace]) end elseif bufnr == nil then for b, t in pairs(diagnostic_cache) do add_all_diags(b, t[namespace] or {}) end else bufnr = get_bufnr(bufnr) add_all_diags(bufnr, diagnostic_cache[bufnr][namespace] or {}) end if opts.severity then diagnostics = filter_by_severity(opts.severity, diagnostics) end return diagnostics end local function set_list(loclist, opts) opts = opts or {} local open = vim.F.if_nil(opts.open, true) local title = opts.title or 'Diagnostics' local winnr = opts.winnr or 0 local bufnr if loclist then bufnr = api.nvim_win_get_buf(winnr) end -- Don't clamp line numbers since the quickfix list can already handle line -- numbers beyond the end of the buffer local diagnostics = get_diagnostics(bufnr, opts, false) local items = M.toqflist(diagnostics) if loclist then vim.fn.setloclist(winnr, {}, ' ', { title = title, items = items }) else vim.fn.setqflist({}, ' ', { title = title, items = items }) end if open then api.nvim_command(loclist and 'lwindow' or 'botright cwindow') end end local function next_diagnostic(position, search_forward, bufnr, opts, namespace) position[1] = position[1] - 1 bufnr = get_bufnr(bufnr) local wrap = vim.F.if_nil(opts.wrap, true) local line_count = api.nvim_buf_line_count(bufnr) 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 if lnum < 0 or lnum >= line_count then if not wrap then return end lnum = (lnum + line_count) % line_count end if line_diagnostics[lnum] and not vim.tbl_isempty(line_diagnostics[lnum]) then local line_length = #api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1] local sort_diagnostics, is_next if search_forward then sort_diagnostics = function(a, b) return a.col < b.col end is_next = function(d) return math.min(d.col, line_length - 1) > position[2] end else sort_diagnostics = function(a, b) return a.col > b.col end is_next = function(d) return math.min(d.col, line_length - 1) < position[2] end end table.sort(line_diagnostics[lnum], sort_diagnostics) if i == 0 then for _, v in pairs(line_diagnostics[lnum]) do if is_next(v) then return v end end else return line_diagnostics[lnum][1] end end end end local function diagnostic_move_pos(opts, pos) opts = opts or {} local float = vim.F.if_nil(opts.float, true) local win_id = opts.win_id or api.nvim_get_current_win() if not pos then api.nvim_echo({ { 'No more valid diagnostics to move to', 'WarningMsg' } }, true, {}) return end api.nvim_win_call(win_id, function() -- Save position in the window's jumplist vim.cmd("normal! m'") api.nvim_win_set_cursor(win_id, { pos[1] + 1, pos[2] }) -- Open folds under the cursor vim.cmd('normal! zv') end) if float then local float_opts = type(float) == 'table' and float or {} vim.schedule(function() M.open_float(vim.tbl_extend('keep', float_opts, { bufnr = api.nvim_win_get_buf(win_id), scope = 'cursor', focus = false, })) end) end end --- Configure diagnostic options globally or for a specific diagnostic --- namespace. --- --- Configuration can be specified globally, per-namespace, or ephemerally --- (i.e. only for a single call to |vim.diagnostic.set()| or --- |vim.diagnostic.show()|). Ephemeral configuration has highest priority, --- 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 }) --- ``` --- --- and a diagnostic producer sets diagnostics with --- --- ```lua --- vim.diagnostic.set(ns, 0, diagnostics, { virtual_text = false }) --- ``` --- --- then virtual text will not be enabled for those diagnostics. --- ---@note Each of the configuration options below accepts one of the following: --- - `false`: Disable this feature --- - `true`: Enable this feature, use default settings. --- - `table`: Enable this feature with overrides. Use an empty table to use default values. --- - `function`: Function with signature (namespace, bufnr) that returns any of the above. --- ---@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| --- - 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. 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| --- * source: (boolean or string) Include the diagnostic source in virtual --- text. Use "if_many" to only show sources if there is more than --- one diagnostic source in the buffer. Otherwise, any truthy value --- means to always show the diagnostic source. --- * 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, 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) -> --- string, where {diagnostic} is of type |diagnostic-structure|. --- This can be used to render an LSP diagnostic error code. --- * 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. Example: ---
lua --- function(diagnostic) --- if diagnostic.severity == vim.diagnostic.severity.ERROR then --- return string.format("E: %s", diagnostic.message) --- end --- return diagnostic.message --- end ------ - signs: (default true) Use signs for diagnostics |diagnostic-signs|. Options: --- * 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. --- * text: (table) A table mapping |diagnostic-severity| to the sign text --- to display in the sign column. The default is to use "E", "W", "I", and "H" --- for errors, warnings, information, and hints, respectively. Example: ---
lua --- vim.diagnostic.config({ --- signs = { text = { [vim.diagnostic.severity.ERROR] = 'E', ... } } --- }) ------ * numhl: (table) A table mapping |diagnostic-severity| to the highlight --- group used for the line number where the sign is placed. --- * linehl: (table) A table mapping |diagnostic-severity| to the highlight group --- used for the whole line the sign is placed in. --- - float: Options for floating windows. See |vim.diagnostic.open_float()|. --- - update_in_insert: (default false) Update diagnostics in Insert mode (if false, --- diagnostics are updated on InsertLeave) --- - severity_sort: (default false) Sort diagnostics by severity. This affects the order in --- which signs and virtual text are displayed. When true, higher severities --- are displayed before lower severities (e.g. ERROR is displayed before WARN). --- Options: --- * reverse: (boolean) Reverse sort order --- ---@param namespace integer|nil Update the options for the given namespace. When omitted, update the --- global diagnostic options. --- ---@return table|nil table of current diagnostic config if `opts` is omitted. function M.config(opts, namespace) vim.validate({ opts = { opts, 't', true }, namespace = { namespace, 'n', true }, }) local t if namespace then local ns = M.get_namespace(namespace) t = ns.opts else t = global_diagnostic_options end if not opts then -- Return current config return vim.deepcopy(t, true) end for k, v in pairs(opts) do t[k] = v end if namespace then for bufnr, v in pairs(diagnostic_cache) do if v[namespace] then M.show(namespace, bufnr) end end else for bufnr, v in pairs(diagnostic_cache) do for ns in pairs(v) do M.show(ns, bufnr) end end end end --- Set diagnostics for the given namespace and buffer. --- ---@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) vim.validate({ namespace = { namespace, 'n' }, bufnr = { bufnr, 'n' }, diagnostics = { diagnostics, vim.tbl_islist, 'a list of diagnostics', }, opts = { opts, 't', true }, }) bufnr = get_bufnr(bufnr) if vim.tbl_isempty(diagnostics) then diagnostic_cache[bufnr][namespace] = nil else set_diagnostic_cache(namespace, bufnr, diagnostics) end M.show(namespace, bufnr, nil, opts) api.nvim_exec_autocmds('DiagnosticChanged', { modeline = false, buffer = bufnr, data = { diagnostics = diagnostics }, }) end --- Get namespace metadata. --- ---@param namespace integer Diagnostic namespace ---@return table Namespace metadata function M.get_namespace(namespace) vim.validate({ namespace = { namespace, 'n' } }) if not all_namespaces[namespace] then local name for k, v in pairs(api.nvim_get_namespaces()) do if namespace == v then name = k break end end assert(name, 'namespace does not exist or is anonymous') all_namespaces[namespace] = { name = name, opts = {}, user_data = {}, } end return all_namespaces[namespace] end --- Get current diagnostic namespaces. --- ---@return table A list of active diagnostic namespaces |vim.diagnostic|. function M.get_namespaces() return vim.deepcopy(all_namespaces, true) end ---@class Diagnostic ---@field bufnr? integer ---@field lnum integer 0-indexed ---@field end_lnum? integer 0-indexed ---@field col integer 0-indexed ---@field end_col? integer 0-indexed ---@field severity? DiagnosticSeverity ---@field message string ---@field source? string ---@field code? string ---@field _tags? { deprecated: boolean, unnecessary: boolean} ---@field user_data? any arbitrary data plugins can add --- 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: --- - 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|. Keys `bufnr`, `end_lnum`, `end_col`, and `severity` are guaranteed to be present. function M.get(bufnr, opts) vim.validate({ bufnr = { bufnr, 'n', true }, opts = { opts, 't', true }, }) return vim.deepcopy(get_diagnostics(bufnr, opts, false), true) end --- Get current diagnostics count. --- ---@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. --- - lnum: (number) Limit diagnostics to the given line number. --- - severity: See |diagnostic-severity|. ---@return table A table with actually present severity values as keys (see |diagnostic-severity|) and integer counts as values. function M.count(bufnr, opts) vim.validate({ bufnr = { bufnr, 'n', true }, opts = { opts, 't', true }, }) local diagnostics = get_diagnostics(bufnr, opts, false) local count = {} for i = 1, #diagnostics do local severity = diagnostics[i].severity count[severity] = (count[severity] or 0) + 1 end return count end --- Get the previous diagnostic closest to the cursor position. --- ---@param opts nil|table See |vim.diagnostic.goto_next()| ---@return Diagnostic|nil Previous diagnostic function M.get_prev(opts) opts = opts or {} local win_id = opts.win_id or api.nvim_get_current_win() local bufnr = api.nvim_win_get_buf(win_id) local cursor_position = opts.cursor_position or api.nvim_win_get_cursor(win_id) return next_diagnostic(cursor_position, false, bufnr, opts, opts.namespace) end --- Return the position of the previous diagnostic in the current buffer. --- ---@param opts table|nil See |vim.diagnostic.goto_next()| ---@return table|false Previous diagnostic position as a (row, col) tuple or false if there is no --- prior diagnostic function M.get_prev_pos(opts) local prev = M.get_prev(opts) if not prev then return false end return { prev.lnum, prev.col } end --- Move to the previous diagnostic in the current buffer. ---@param opts table|nil See |vim.diagnostic.goto_next()| function M.goto_prev(opts) return diagnostic_move_pos(opts, M.get_prev_pos(opts)) end --- Get the next diagnostic closest to the cursor position. --- ---@param opts table|nil See |vim.diagnostic.goto_next()| ---@return Diagnostic|nil Next diagnostic function M.get_next(opts) opts = opts or {} local win_id = opts.win_id or api.nvim_get_current_win() local bufnr = api.nvim_win_get_buf(win_id) local cursor_position = opts.cursor_position or api.nvim_win_get_cursor(win_id) return next_diagnostic(cursor_position, true, bufnr, opts, opts.namespace) end --- Return the position of the next diagnostic in the current buffer. --- ---@param opts table|nil See |vim.diagnostic.goto_next()| ---@return table|false Next diagnostic position as a (row, col) tuple or false if no next --- diagnostic. function M.get_next_pos(opts) local next = M.get_next(opts) if not next then return false end return { next.lnum, next.col } end --- Move to the next diagnostic. --- ---@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. --- - 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 --- diagnostics at the new cursor position (as if "cursor" were passed to --- the "scope" option). --- - win_id: (number, default 0) Window ID function M.goto_next(opts) return diagnostic_move_pos(opts, M.get_next_pos(opts)) end M.handlers.signs = { show = function(namespace, bufnr, diagnostics, opts) vim.validate({ namespace = { namespace, 'n' }, bufnr = { bufnr, 'n' }, diagnostics = { diagnostics, vim.tbl_islist, 'a list of diagnostics', }, opts = { opts, 't', true }, }) bufnr = get_bufnr(bufnr) opts = opts or {} if opts.signs and opts.signs.severity then diagnostics = filter_by_severity(opts.signs.severity, diagnostics) end -- 10 is the default sign priority when none is explicitly specified local priority = opts.signs and opts.signs.priority or 10 local get_priority if opts.severity_sort then if type(opts.severity_sort) == 'table' and opts.severity_sort.reverse then get_priority = function(severity) return priority + (severity - vim.diagnostic.severity.ERROR) end else get_priority = function(severity) return priority + (vim.diagnostic.severity.HINT - severity) end end else get_priority = function() return priority end end local ns = M.get_namespace(namespace) if not ns.user_data.sign_ns then ns.user_data.sign_ns = api.nvim_create_namespace(string.format('%s/diagnostic/signs', ns.name)) end --- Handle legacy diagnostic sign definitions --- These were deprecated in 0.10 and will be removed in 0.12 if opts.signs and not opts.signs.text and not opts.signs.numhl and not opts.signs.texthl then for _, v in ipairs({ 'Error', 'Warn', 'Info', 'Hint' }) do local name = string.format('DiagnosticSign%s', v) local sign = vim.fn.sign_getdefined(name)[1] if sign then local severity = M.severity[v:upper()] vim.deprecate( 'Defining diagnostic signs with :sign-define or sign_define()', 'vim.diagnostic.config()', '0.12', nil, false ) if not opts.signs.text then opts.signs.text = {} end if not opts.signs.numhl then opts.signs.numhl = {} end if not opts.signs.linehl then opts.signs.linehl = {} end if opts.signs.text[severity] == nil then opts.signs.text[severity] = sign.text or '' end if opts.signs.numhl[severity] == nil then opts.signs.numhl[severity] = sign.numhl end if opts.signs.linehl[severity] == nil then opts.signs.linehl[severity] = sign.linehl end end end end local text = {} ---@type table