| local api, if_nil = vim.api, vim.F.if_nil |
|
|
| local M = {} |
|
|
| |
| |
| local function get_qf_id_for_title(title) |
| local lastqflist = vim.fn.getqflist({ nr = '$' }) |
| for i = 1, lastqflist.nr do |
| local qflist = vim.fn.getqflist({ nr = i, id = 0, title = 0 }) |
| if qflist.title == title then |
| return qflist.id |
| end |
| end |
|
|
| return nil |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| M.severity = { |
| ERROR = 1, |
| WARN = 2, |
| INFO = 3, |
| HINT = 4, |
| [1] = 'ERROR', |
| [2] = 'WARN', |
| [3] = 'INFO', |
| [4] = 'HINT', |
| |
| E = 1, |
| W = 2, |
| I = 3, |
| N = 4, |
| } |
|
|
| |
|
|
| |
| |
|
|
| |
| local global_diagnostic_options = { |
| signs = true, |
| underline = true, |
| virtual_text = false, |
| virtual_lines = false, |
| float = true, |
| update_in_insert = false, |
| severity_sort = false, |
| jump = { |
| |
| float = false, |
|
|
| |
| wrap = true, |
| }, |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| M.handlers = setmetatable({}, { |
| __newindex = function(t, name, handler) |
| vim.validate('handler', handler, 'table') |
| rawset(t, name, handler) |
| if global_diagnostic_options[name] == nil then |
| global_diagnostic_options[name] = true |
| end |
| end, |
| }) |
|
|
| |
| 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('nvim.diagnostic.buf_wipeout', {}) |
| setmetatable(diagnostic_cache, { |
| |
| |
| __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 |
| assert(M.severity[string.upper(severity)], string.format('Invalid severity: %s', severity)) |
| return M.severity[string.upper(severity)] |
| end |
| return severity |
| end |
|
|
| |
| |
| local function severity_predicate(severity) |
| if type(severity) ~= 'table' then |
| severity = assert(to_severity(severity)) |
| |
| return function(d) |
| return d.severity == severity |
| end |
| 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 function(d) |
| return d.severity <= min_severity and d.severity >= max_severity |
| end |
| end |
|
|
| |
| local severities = {} |
| for _, s in ipairs(severity) do |
| severities[assert(to_severity(s))] = true |
| end |
|
|
| |
| return function(d) |
| return severities[d.severity] |
| end |
| end |
|
|
| |
| |
| |
| local function filter_by_severity(severity, diagnostics) |
| if not severity then |
| return diagnostics |
| end |
| return vim.tbl_filter(severity_predicate(severity), 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 |
| local source = diagnostic.source |
| if source and not seen[source] then |
| seen[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, 'function') |
| vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') |
|
|
| local formatted = {} |
| for _, diagnostic in ipairs(diagnostics) do |
| local message = format(diagnostic) |
| if message ~= nil then |
| local formatted_diagnostic = vim.deepcopy(diagnostic, true) |
| formatted_diagnostic.message = message |
| table.insert(formatted, formatted_diagnostic) |
| end |
| 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 |
|
|
| local global_opt = global_diagnostic_options[option] |
| if type(global_opt) == 'table' then |
| return global_opt |
| 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 |
| end |
| error('Unexpected option type: ' .. vim.inspect(value)) |
| end |
|
|
| |
| |
| |
| |
| local function get_resolved_options(opts, namespace, bufnr) |
| local ns = namespace and M.get_namespace(namespace) or {} |
| |
| 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 |
|
|
| |
| 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' }, |
| } |
|
|
| |
| |
| |
| 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 virtual_lines_highlight_map = make_highlight_map('VirtualLines') |
| 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 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 diagnostics_at_cursor(diagnostics) |
| local lnum = api.nvim_win_get_cursor(0)[1] - 1 |
|
|
| if diagnostics[lnum] ~= nil then |
| return diagnostics[lnum] |
| end |
|
|
| local cursor_diagnostics = {} |
| for _, line_diags in pairs(diagnostics) do |
| for _, diag in ipairs(line_diags) do |
| if diag.end_lnum and lnum >= diag.lnum and lnum <= diag.end_lnum then |
| table.insert(cursor_diagnostics, diag) |
| end |
| end |
| end |
| return cursor_diagnostics |
| 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 |
| |
| |
| 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 = vim._resolve_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 function severity_to_extmark_priority(priority, opts) |
| if opts.severity_sort then |
| if type(opts.severity_sort) == 'table' and opts.severity_sort.reverse then |
| return function(severity) |
| return priority + (severity - vim.diagnostic.severity.ERROR) |
| end |
| end |
|
|
| return function(severity) |
| return priority + (vim.diagnostic.severity.HINT - severity) |
| end |
| end |
|
|
| return function() |
| return priority |
| end |
| 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 |
|
|
| |
| bufs_waiting_to_update[bufnr][namespace] = nil |
|
|
| M.show(namespace, bufnr, nil, args) |
| end |
|
|
| |
| 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 |
|
|
| if type(namespace) == 'number' then |
| namespace = { namespace } |
| end |
|
|
| |
|
|
| local diagnostics = {} |
|
|
| |
| |
| local buf_line_count = setmetatable({}, { |
| |
| |
| |
| __index = function(t, k) |
| t[k] = api.nvim_buf_line_count(k) |
| return rawget(t, k) |
| end, |
| }) |
|
|
| local match_severity = opts.severity and severity_predicate(opts.severity) |
| or function(_) |
| return true |
| end |
|
|
| |
| |
| local function add(b, d) |
| if |
| match_severity(d) |
| and (not opts.lnum or (opts.lnum >= d.lnum and opts.lnum <= (d.end_lnum or d.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(assert(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 = vim._resolve_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 |
| for _, iter_namespace in ipairs(namespace) do |
| add_all_diags(b, t[iter_namespace] or {}) |
| end |
| end |
| else |
| bufnr = vim._resolve_bufnr(bufnr) |
| for _, iter_namespace in ipairs(namespace) do |
| add_all_diags(bufnr, diagnostic_cache[bufnr][iter_namespace] or {}) |
| end |
| end |
|
|
| return diagnostics |
| end |
|
|
| |
| |
| local function set_list(loclist, opts) |
| opts = opts or {} |
| local open = 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 |
| |
| |
| local diagnostics = get_diagnostics(bufnr, opts , false) |
| local items = M.toqflist(diagnostics) |
| local qf_id = nil |
| if loclist then |
| vim.fn.setloclist(winnr, {}, 'u', { title = title, items = items }) |
| else |
| qf_id = get_qf_id_for_title(title) |
|
|
| |
| |
| |
| vim.fn.setqflist({}, qf_id and 'u' or ' ', { |
| title = title, |
| items = items, |
| id = qf_id, |
| }) |
| end |
|
|
| if open then |
| if not loclist then |
| |
| |
| local nr = vim.fn.getqflist({ id = qf_id, nr = 0 }).nr |
| api.nvim_command(('silent %dchistory'):format(nr)) |
|
|
| |
| api.nvim_command('botright cwindow') |
| else |
| api.nvim_command('lwindow') |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| local function filter_highest(diagnostics) |
| table.sort(diagnostics, function(a, b) |
| return a.severity < b.severity |
| end) |
|
|
| |
| |
| local worst = (diagnostics[1] or {}).severity |
| local len = #diagnostics |
| for i = 2, len do |
| if diagnostics[i].severity ~= worst then |
| for j = i, len do |
| diagnostics[j] = nil |
| end |
| break |
| end |
| end |
| end |
|
|
| |
| |
| |
| local function next_diagnostic(search_forward, opts) |
| opts = opts or {} |
|
|
| |
| if opts.win_id then |
| vim.deprecate('opts.win_id', 'opts.winid', '0.13') |
| opts.winid = opts.win_id |
| opts.win_id = nil |
| end |
|
|
| |
| if opts.cursor_position then |
| vim.deprecate('opts.cursor_position', 'opts.pos', '0.13') |
| opts.pos = opts.cursor_position |
| opts.cursor_position = nil |
| end |
|
|
| local winid = opts.winid or api.nvim_get_current_win() |
| local bufnr = api.nvim_win_get_buf(winid) |
| local position = opts.pos or api.nvim_win_get_cursor(winid) |
|
|
| |
| position[1] = position[1] - 1 |
|
|
| local wrap = if_nil(opts.wrap, true) |
|
|
| local diagnostics = get_diagnostics(bufnr, opts, true) |
|
|
| if opts._highest then |
| filter_highest(diagnostics) |
| end |
|
|
| local line_diagnostics = diagnostic_lines(diagnostics) |
|
|
| local line_count = api.nvim_buf_line_count(bufnr) |
| 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, math.max(line_length - 1, 0)) > position[2] |
| end |
| else |
| sort_diagnostics = function(a, b) |
| return a.col > b.col |
| end |
| is_next = function(d) |
| return math.min(d.col, math.max(line_length - 1, 0)) < 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 goto_diagnostic(diagnostic, opts) |
| if not diagnostic then |
| api.nvim_echo({ { 'No more valid diagnostics to move to', 'WarningMsg' } }, true, {}) |
| return |
| end |
|
|
| opts = opts or {} |
|
|
| |
| if opts.win_id then |
| vim.deprecate('opts.win_id', 'opts.winid', '0.13') |
| opts.winid = opts.win_id |
| opts.win_id = nil |
| end |
|
|
| local winid = opts.winid or api.nvim_get_current_win() |
|
|
| vim._with({ win = winid }, function() |
| |
| vim.cmd("normal! m'") |
| api.nvim_win_set_cursor(winid, { diagnostic.lnum + 1, diagnostic.col }) |
| |
| vim.cmd('normal! zv') |
| end) |
|
|
| local float_opts = opts.float |
| if float_opts then |
| float_opts = type(float_opts) == 'table' and float_opts or {} |
| vim.schedule(function() |
| M.open_float(vim.tbl_extend('keep', float_opts, { |
| bufnr = api.nvim_win_get_buf(winid), |
| scope = 'cursor', |
| focus = false, |
| })) |
| end) |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.config(opts, namespace) |
| vim.validate('opts', opts, 'table', true) |
| vim.validate('namespace', namespace, 'number', 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 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 |
|
|
| |
| |
| |
| |
| |
| |
| function M.set(namespace, bufnr, diagnostics, opts) |
| vim.validate('namespace', namespace, 'number') |
| vim.validate('bufnr', bufnr, 'number') |
| vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') |
| vim.validate('opts', opts, 'table', true) |
|
|
| bufnr = vim._resolve_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 |
|
|
| |
| |
| |
| |
| function M.get_namespace(namespace) |
| vim.validate('namespace', namespace, 'number') |
| 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 |
|
|
| |
| |
| |
| function M.get_namespaces() |
| return vim.deepcopy(all_namespaces, true) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.get(bufnr, opts) |
| vim.validate('bufnr', bufnr, 'number', true) |
| vim.validate('opts', opts, 'table', true) |
|
|
| return vim.deepcopy(get_diagnostics(bufnr, opts, false), true) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| function M.count(bufnr, opts) |
| vim.validate('bufnr', bufnr, 'number', true) |
| vim.validate('opts', opts, 'table', 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 |
|
|
| |
| |
| |
| |
| function M.get_prev(opts) |
| return next_diagnostic(false, opts) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function M.get_prev_pos(opts) |
| vim.deprecate( |
| 'vim.diagnostic.get_prev_pos()', |
| 'access the lnum and col fields from get_prev() instead', |
| '0.13' |
| ) |
| local prev = M.get_prev(opts) |
| if not prev then |
| return false |
| end |
|
|
| return { prev.lnum, prev.col } |
| end |
|
|
| |
| |
| |
| function M.goto_prev(opts) |
| vim.deprecate('vim.diagnostic.goto_prev()', 'vim.diagnostic.jump()', '0.13') |
| opts = opts or {} |
| opts.float = if_nil(opts.float, true) |
| goto_diagnostic(M.get_prev(opts), opts) |
| end |
|
|
| |
| |
| |
| |
| function M.get_next(opts) |
| return next_diagnostic(true, opts) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function M.get_next_pos(opts) |
| vim.deprecate( |
| 'vim.diagnostic.get_next_pos()', |
| 'access the lnum and col fields from get_next() instead', |
| '0.13' |
| ) |
| local next = M.get_next(opts) |
| if not next then |
| return false |
| end |
|
|
| return { next.lnum, next.col } |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| function M.jump(opts) |
| vim.validate('opts', opts, 'table') |
|
|
| |
| assert( |
| opts.diagnostic or opts.count, |
| 'One of "diagnostic" or "count" must be specified in the options to vim.diagnostic.jump()' |
| ) |
|
|
| |
| opts = vim.tbl_deep_extend('keep', opts, global_diagnostic_options.jump) |
|
|
| if opts.diagnostic then |
| goto_diagnostic(opts.diagnostic, opts) |
| return opts.diagnostic |
| end |
|
|
| local count = opts.count |
| if count == 0 then |
| return nil |
| end |
|
|
| |
| if opts.cursor_position then |
| vim.deprecate('opts.cursor_position', 'opts.pos', '0.13') |
| opts.pos = opts.cursor_position |
| opts.cursor_position = nil |
| end |
|
|
| local diag = nil |
| while count ~= 0 do |
| local next = next_diagnostic(count > 0, opts) |
| if not next then |
| break |
| end |
|
|
| |
| opts.pos = { next.lnum + 1, next.col } |
|
|
| if count > 0 then |
| count = count - 1 |
| else |
| count = count + 1 |
| end |
| diag = next |
| end |
|
|
| goto_diagnostic(diag, opts) |
|
|
| return diag |
| end |
|
|
| |
| |
| |
| |
| function M.goto_next(opts) |
| vim.deprecate('vim.diagnostic.goto_next()', 'vim.diagnostic.jump()', '0.13') |
| opts = opts or {} |
| opts.float = if_nil(opts.float, true) |
| goto_diagnostic(M.get_next(opts), opts) |
| end |
|
|
| M.handlers.signs = { |
| show = function(namespace, bufnr, diagnostics, opts) |
| vim.validate('namespace', namespace, 'number') |
| vim.validate('bufnr', bufnr, 'number') |
| vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') |
| vim.validate('opts', opts, 'table', true) |
| vim.validate('opts.signs', (opts and opts or {}).signs, 'table', true) |
|
|
| bufnr = vim._resolve_bufnr(bufnr) |
| opts = opts or {} |
|
|
| if not api.nvim_buf_is_loaded(bufnr) then |
| return |
| end |
|
|
| |
| local priority = opts.signs and opts.signs.priority or 10 |
| local get_priority = severity_to_extmark_priority(priority, opts) |
|
|
| 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('nvim.%s.diagnostic.signs', ns.name)) |
| end |
|
|
| |
| |
| if opts.signs and not opts.signs.text and not opts.signs.numhl 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' |
| ) |
|
|
| 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 = {} |
| for k in pairs(M.severity) do |
| if opts.signs.text and opts.signs.text[k] then |
| text[k] = opts.signs.text[k] |
| elseif type(k) == 'string' and not text[k] then |
| text[k] = string.sub(k, 1, 1):upper() |
| end |
| end |
|
|
| local numhl = opts.signs.numhl or {} |
| local linehl = opts.signs.linehl or {} |
|
|
| local line_count = api.nvim_buf_line_count(bufnr) |
|
|
| for _, diagnostic in ipairs(diagnostics) do |
| if diagnostic.lnum <= line_count then |
| api.nvim_buf_set_extmark(bufnr, ns.user_data.sign_ns, diagnostic.lnum, 0, { |
| sign_text = text[diagnostic.severity] or text[M.severity[diagnostic.severity]] or 'U', |
| sign_hl_group = sign_highlight_map[diagnostic.severity], |
| number_hl_group = numhl[diagnostic.severity], |
| line_hl_group = linehl[diagnostic.severity], |
| priority = get_priority(diagnostic.severity), |
| }) |
| end |
| end |
| end, |
|
|
| |
| |
| hide = function(namespace, bufnr) |
| local ns = M.get_namespace(namespace) |
| if ns.user_data.sign_ns and api.nvim_buf_is_valid(bufnr) then |
| api.nvim_buf_clear_namespace(bufnr, ns.user_data.sign_ns, 0, -1) |
| end |
| end, |
| } |
|
|
| M.handlers.underline = { |
| show = function(namespace, bufnr, diagnostics, opts) |
| vim.validate('namespace', namespace, 'number') |
| vim.validate('bufnr', bufnr, 'number') |
| vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') |
| vim.validate('opts', opts, 'table', true) |
|
|
| bufnr = vim._resolve_bufnr(bufnr) |
| opts = opts or {} |
|
|
| if not vim.api.nvim_buf_is_loaded(bufnr) then |
| return |
| end |
|
|
| local ns = M.get_namespace(namespace) |
| if not ns.user_data.underline_ns then |
| ns.user_data.underline_ns = |
| api.nvim_create_namespace(string.format('nvim.%s.diagnostic.underline', ns.name)) |
| end |
|
|
| local underline_ns = ns.user_data.underline_ns |
| local get_priority = severity_to_extmark_priority(vim.hl.priorities.diagnostics, opts) |
|
|
| for _, diagnostic in ipairs(diagnostics) do |
| |
| local higroup = underline_highlight_map[assert(diagnostic.severity)] |
| or underline_highlight_map[vim.diagnostic.severity.ERROR] |
|
|
| if diagnostic._tags then |
| |
| if diagnostic._tags.unnecessary then |
| higroup = 'DiagnosticUnnecessary' |
| end |
| if diagnostic._tags.deprecated then |
| higroup = 'DiagnosticDeprecated' |
| end |
| end |
|
|
| vim.hl.range( |
| bufnr, |
| underline_ns, |
| higroup, |
| { diagnostic.lnum, diagnostic.col }, |
| { diagnostic.end_lnum, diagnostic.end_col }, |
| { priority = get_priority(diagnostic.severity) } |
| ) |
| end |
| save_extmarks(underline_ns, bufnr) |
| end, |
| hide = function(namespace, bufnr) |
| local ns = M.get_namespace(namespace) |
| if ns.user_data.underline_ns then |
| diagnostic_cache_extmarks[bufnr][ns.user_data.underline_ns] = {} |
| if api.nvim_buf_is_valid(bufnr) then |
| api.nvim_buf_clear_namespace(bufnr, ns.user_data.underline_ns, 0, -1) |
| end |
| end |
| end, |
| } |
|
|
| |
| |
| |
| |
| local function render_virtual_text(namespace, bufnr, diagnostics, opts) |
| api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1) |
|
|
| for line, line_diagnostics in pairs(diagnostics) do |
| local virt_texts = M._get_virt_text_chunks(line_diagnostics, opts) |
|
|
| if virt_texts then |
| api.nvim_buf_set_extmark(bufnr, namespace, line, 0, { |
| hl_mode = opts.hl_mode or 'combine', |
| virt_text = virt_texts, |
| virt_text_pos = opts.virt_text_pos, |
| virt_text_hide = opts.virt_text_hide, |
| virt_text_win_col = opts.virt_text_win_col, |
| }) |
| end |
| end |
| end |
|
|
| M.handlers.virtual_text = { |
| show = function(namespace, bufnr, diagnostics, opts) |
| vim.validate('namespace', namespace, 'number') |
| vim.validate('bufnr', bufnr, 'number') |
| vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') |
| vim.validate('opts', opts, 'table', true) |
|
|
| bufnr = vim._resolve_bufnr(bufnr) |
| opts = opts or {} |
|
|
| if not vim.api.nvim_buf_is_loaded(bufnr) then |
| return |
| end |
|
|
| if opts.virtual_text then |
| if opts.virtual_text.format then |
| diagnostics = reformat_diagnostics(opts.virtual_text.format, diagnostics) |
| end |
| if |
| opts.virtual_text.source |
| and (opts.virtual_text.source ~= 'if_many' or count_sources(bufnr) > 1) |
| then |
| diagnostics = prefix_source(diagnostics) |
| end |
| end |
|
|
| local ns = M.get_namespace(namespace) |
| if not ns.user_data.virt_text_ns then |
| ns.user_data.virt_text_ns = |
| api.nvim_create_namespace(string.format('nvim.%s.diagnostic.virtual_text', ns.name)) |
| end |
| if not ns.user_data.virt_text_augroup then |
| ns.user_data.virt_text_augroup = api.nvim_create_augroup( |
| string.format('nvim.%s.diagnostic.virt_text', ns.name), |
| { clear = true } |
| ) |
| end |
|
|
| api.nvim_clear_autocmds({ group = ns.user_data.virt_text_augroup, buffer = bufnr }) |
|
|
| local line_diagnostics = diagnostic_lines(diagnostics) |
|
|
| if opts.virtual_text.current_line == true then |
| api.nvim_create_autocmd('CursorMoved', { |
| buffer = bufnr, |
| group = ns.user_data.virt_text_augroup, |
| callback = function() |
| local lnum = api.nvim_win_get_cursor(0)[1] - 1 |
| render_virtual_text( |
| ns.user_data.virt_text_ns, |
| bufnr, |
| { [lnum] = diagnostics_at_cursor(line_diagnostics) }, |
| opts.virtual_text |
| ) |
| end, |
| }) |
| |
| local lnum = api.nvim_win_get_cursor(0)[1] - 1 |
| render_virtual_text( |
| ns.user_data.virt_text_ns, |
| bufnr, |
| { [lnum] = diagnostics_at_cursor(line_diagnostics) }, |
| opts.virtual_text |
| ) |
| else |
| render_virtual_text(ns.user_data.virt_text_ns, bufnr, line_diagnostics, opts.virtual_text) |
| end |
|
|
| save_extmarks(ns.user_data.virt_text_ns, bufnr) |
| end, |
| hide = function(namespace, bufnr) |
| local ns = M.get_namespace(namespace) |
| if ns.user_data.virt_text_ns then |
| diagnostic_cache_extmarks[bufnr][ns.user_data.virt_text_ns] = {} |
| if api.nvim_buf_is_valid(bufnr) then |
| api.nvim_buf_clear_namespace(bufnr, ns.user_data.virt_text_ns, 0, -1) |
| api.nvim_clear_autocmds({ group = ns.user_data.virt_text_augroup, buffer = bufnr }) |
| end |
| end |
| end, |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| local function distance_between_cols(bufnr, lnum, start_col, end_col) |
| return api.nvim_buf_call(bufnr, function() |
| local s = vim.fn.virtcol({ lnum + 1, start_col }) |
| local e = vim.fn.virtcol({ lnum + 1, end_col + 1 }) |
| return e - 1 - s |
| end) |
| end |
|
|
| |
| |
| |
| local function render_virtual_lines(namespace, bufnr, diagnostics) |
| table.sort(diagnostics, function(d1, d2) |
| if d1.lnum == d2.lnum then |
| return d1.col < d2.col |
| else |
| return d1.lnum < d2.lnum |
| end |
| end) |
|
|
| api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1) |
|
|
| if not next(diagnostics) then |
| return |
| end |
|
|
| |
| |
| local ElementType = { Space = 1, Diagnostic = 2, Overlap = 3, Blank = 4 } |
| local line_stacks = {} |
| local prev_lnum = -1 |
| local prev_col = 0 |
| for _, diag in ipairs(diagnostics) do |
| if not line_stacks[diag.lnum] then |
| line_stacks[diag.lnum] = {} |
| end |
|
|
| local stack = line_stacks[diag.lnum] |
|
|
| if diag.lnum ~= prev_lnum then |
| table.insert(stack, { |
| ElementType.Space, |
| string.rep(' ', distance_between_cols(bufnr, diag.lnum, 0, diag.col)), |
| }) |
| elseif diag.col ~= prev_col then |
| table.insert(stack, { |
| ElementType.Space, |
| string.rep( |
| ' ', |
| |
| distance_between_cols(bufnr, diag.lnum, prev_col + 1, diag.col) |
| ), |
| }) |
| else |
| table.insert(stack, { ElementType.Overlap, diag.severity }) |
| end |
|
|
| if diag.message:find('^%s*$') then |
| table.insert(stack, { ElementType.Blank, diag }) |
| else |
| table.insert(stack, { ElementType.Diagnostic, diag }) |
| end |
|
|
| prev_lnum, prev_col = diag.lnum, diag.col |
| end |
|
|
| local chars = { |
| cross = '┼', |
| horizontal = '─', |
| horizontal_up = '┴', |
| up_right = '└', |
| vertical = '│', |
| vertical_right = '├', |
| } |
|
|
| for lnum, stack in pairs(line_stacks) do |
| local virt_lines = {} |
|
|
| |
| for i = #stack, 1, -1 do |
| if stack[i][1] == ElementType.Diagnostic then |
| local diagnostic = stack[i][2] |
| local left = {} |
| local overlap = false |
| local multi = false |
|
|
| |
| for j = 1, i - 1 do |
| local type = stack[j][1] |
| local data = stack[j][2] |
| if type == ElementType.Space then |
| if multi then |
| |
| table.insert(left, { |
| string.rep(chars.horizontal, data:len()), |
| virtual_lines_highlight_map[diagnostic.severity], |
| }) |
| else |
| table.insert(left, { data, '' }) |
| end |
| elseif type == ElementType.Diagnostic then |
| |
| if stack[j + 1][1] ~= ElementType.Overlap then |
| table.insert(left, { chars.vertical, virtual_lines_highlight_map[data.severity] }) |
| end |
| overlap = false |
| elseif type == ElementType.Blank then |
| if multi then |
| table.insert( |
| left, |
| { chars.horizontal_up, virtual_lines_highlight_map[data.severity] } |
| ) |
| else |
| table.insert(left, { chars.up_right, virtual_lines_highlight_map[data.severity] }) |
| end |
| multi = true |
| elseif type == ElementType.Overlap then |
| overlap = true |
| end |
| end |
|
|
| local center_char |
| if overlap and multi then |
| center_char = chars.cross |
| elseif overlap then |
| center_char = chars.vertical_right |
| elseif multi then |
| center_char = chars.horizontal_up |
| else |
| center_char = chars.up_right |
| end |
| local center = { |
| { |
| string.format('%s%s', center_char, string.rep(chars.horizontal, 4) .. ' '), |
| virtual_lines_highlight_map[diagnostic.severity], |
| }, |
| } |
|
|
| |
| |
| |
| |
| |
| for msg_line in diagnostic.message:gmatch('([^\n]+)') do |
| local vline = {} |
| vim.list_extend(vline, left) |
| vim.list_extend(vline, center) |
| vim.list_extend(vline, { { msg_line, virtual_lines_highlight_map[diagnostic.severity] } }) |
|
|
| table.insert(virt_lines, vline) |
|
|
| |
| if overlap then |
| center = { |
| { chars.vertical, virtual_lines_highlight_map[diagnostic.severity] }, |
| { ' ', '' }, |
| } |
| else |
| center = { { ' ', '' } } |
| end |
| end |
| end |
| end |
|
|
| api.nvim_buf_set_extmark(bufnr, namespace, lnum, 0, { |
| virt_lines_overflow = 'scroll', |
| virt_lines = virt_lines, |
| }) |
| end |
| end |
|
|
| |
| |
| local function format_virtual_lines(diagnostic) |
| if diagnostic.code then |
| return string.format('%s: %s', diagnostic.code, diagnostic.message) |
| else |
| return diagnostic.message |
| end |
| end |
|
|
| M.handlers.virtual_lines = { |
| show = function(namespace, bufnr, diagnostics, opts) |
| vim.validate('namespace', namespace, 'number') |
| vim.validate('bufnr', bufnr, 'number') |
| vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') |
| vim.validate('opts', opts, 'table', true) |
|
|
| bufnr = vim._resolve_bufnr(bufnr) |
| opts = opts or {} |
|
|
| if not api.nvim_buf_is_loaded(bufnr) then |
| return |
| end |
|
|
| local ns = M.get_namespace(namespace) |
| if not ns.user_data.virt_lines_ns then |
| ns.user_data.virt_lines_ns = |
| api.nvim_create_namespace(string.format('nvim.%s.diagnostic.virtual_lines', ns.name)) |
| end |
| if not ns.user_data.virt_lines_augroup then |
| ns.user_data.virt_lines_augroup = api.nvim_create_augroup( |
| string.format('nvim.%s.diagnostic.virt_lines', ns.name), |
| { clear = true } |
| ) |
| end |
|
|
| api.nvim_clear_autocmds({ group = ns.user_data.virt_lines_augroup, buffer = bufnr }) |
|
|
| diagnostics = |
| reformat_diagnostics(opts.virtual_lines.format or format_virtual_lines, diagnostics) |
|
|
| if opts.virtual_lines.current_line == true then |
| |
| |
| local line_diagnostics = diagnostic_lines(diagnostics) |
| api.nvim_create_autocmd('CursorMoved', { |
| buffer = bufnr, |
| group = ns.user_data.virt_lines_augroup, |
| callback = function() |
| render_virtual_lines( |
| ns.user_data.virt_lines_ns, |
| bufnr, |
| diagnostics_at_cursor(line_diagnostics) |
| ) |
| end, |
| }) |
| |
| render_virtual_lines( |
| ns.user_data.virt_lines_ns, |
| bufnr, |
| diagnostics_at_cursor(line_diagnostics) |
| ) |
| else |
| render_virtual_lines(ns.user_data.virt_lines_ns, bufnr, diagnostics) |
| end |
|
|
| save_extmarks(ns.user_data.virt_lines_ns, bufnr) |
| end, |
| hide = function(namespace, bufnr) |
| local ns = M.get_namespace(namespace) |
| if ns.user_data.virt_lines_ns then |
| diagnostic_cache_extmarks[bufnr][ns.user_data.virt_lines_ns] = {} |
| if api.nvim_buf_is_valid(bufnr) then |
| api.nvim_buf_clear_namespace(bufnr, ns.user_data.virt_lines_ns, 0, -1) |
| api.nvim_clear_autocmds({ group = ns.user_data.virt_lines_augroup, buffer = bufnr }) |
| end |
| end |
| end, |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function M._get_virt_text_chunks(line_diags, opts) |
| if #line_diags == 0 then |
| return nil |
| end |
|
|
| opts = opts or {} |
| local prefix = opts.prefix or '■' |
| local suffix = opts.suffix or '' |
| local spacing = opts.spacing or 4 |
|
|
| |
| local virt_texts = { { string.rep(' ', spacing) } } |
|
|
| for i = 1, #line_diags do |
| local resolved_prefix = prefix |
| if type(prefix) == 'function' then |
| resolved_prefix = prefix(line_diags[i], i, #line_diags) or '' |
| end |
| table.insert( |
| virt_texts, |
| { resolved_prefix, virtual_text_highlight_map[line_diags[i].severity] } |
| ) |
| end |
| local last = line_diags[#line_diags] |
|
|
| |
| |
| if last.message then |
| if type(suffix) == 'function' then |
| suffix = suffix(last) or '' |
| end |
| table.insert(virt_texts, { |
| string.format(' %s%s', last.message:gsub('\r', ''):gsub('\n', ' '), suffix), |
| virtual_text_highlight_map[last.severity], |
| }) |
|
|
| return virt_texts |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.hide(namespace, bufnr) |
| vim.validate('namespace', namespace, 'number', true) |
| vim.validate('bufnr', bufnr, 'number', true) |
|
|
| local buffers = bufnr and { vim._resolve_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache) |
| for _, iter_bufnr in ipairs(buffers) do |
| local namespaces = namespace and { namespace } or vim.tbl_keys(diagnostic_cache[iter_bufnr]) |
| for _, iter_namespace in ipairs(namespaces) do |
| for _, handler in pairs(M.handlers) do |
| if handler.hide then |
| handler.hide(iter_namespace, iter_bufnr) |
| end |
| end |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| function M.is_enabled(filter) |
| filter = filter or {} |
| if filter.ns_id and M.get_namespace(filter.ns_id).disabled then |
| return false |
| elseif filter.bufnr == nil then |
| |
| return vim.tbl_isempty(diagnostic_disabled) and not diagnostic_disabled[1] |
| end |
|
|
| local bufnr = vim._resolve_bufnr(filter.bufnr) |
| if type(diagnostic_disabled[bufnr]) == 'table' then |
| return not diagnostic_disabled[bufnr][filter.ns_id] |
| end |
|
|
| return diagnostic_disabled[bufnr] == nil |
| end |
|
|
| |
| function M.is_disabled(bufnr, namespace) |
| vim.deprecate('vim.diagnostic.is_disabled()', 'vim.diagnostic.is_enabled()', '0.12') |
| return not M.is_enabled { bufnr = bufnr or 0, ns_id = namespace } |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.show(namespace, bufnr, diagnostics, opts) |
| vim.validate('namespace', namespace, 'number', true) |
| vim.validate('bufnr', bufnr, 'number', true) |
| vim.validate('diagnostics', diagnostics, vim.islist, true, 'a list of diagnostics') |
| vim.validate('opts', opts, 'table', true) |
|
|
| if not bufnr or not namespace then |
| assert(not diagnostics, 'Cannot show diagnostics without a buffer and namespace') |
| if not bufnr then |
| for iter_bufnr in pairs(diagnostic_cache) do |
| M.show(namespace, iter_bufnr, nil, opts) |
| end |
| else |
| |
| bufnr = vim._resolve_bufnr(bufnr) |
| for iter_namespace in pairs(diagnostic_cache[bufnr]) do |
| M.show(iter_namespace, bufnr, nil, opts) |
| end |
| end |
| return |
| end |
|
|
| if not M.is_enabled { bufnr = bufnr or 0, ns_id = namespace } then |
| return |
| end |
|
|
| M.hide(namespace, bufnr) |
|
|
| diagnostics = diagnostics or get_diagnostics(bufnr, { namespace = namespace }, true) |
|
|
| if vim.tbl_isempty(diagnostics) then |
| return |
| end |
|
|
| local opts_res = get_resolved_options(opts, namespace, bufnr) |
|
|
| if opts_res.update_in_insert then |
| clear_scheduled_display(namespace, bufnr) |
| else |
| local mode = api.nvim_get_mode() |
| if string.sub(mode.mode, 1, 1) == 'i' then |
| schedule_display(namespace, bufnr, opts_res) |
| return |
| end |
| end |
|
|
| if if_nil(opts_res.severity_sort, false) then |
| if type(opts_res.severity_sort) == 'table' and opts_res.severity_sort.reverse then |
| table.sort(diagnostics, function(a, b) |
| return a.severity < b.severity |
| end) |
| else |
| table.sort(diagnostics, function(a, b) |
| return a.severity > b.severity |
| end) |
| end |
| end |
|
|
| for handler_name, handler in pairs(M.handlers) do |
| if handler.show and opts_res[handler_name] then |
| local filtered = filter_by_severity(opts_res[handler_name].severity, diagnostics) |
| handler.show(namespace, bufnr, filtered, opts_res) |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| function M.open_float(opts, ...) |
| |
| local bufnr |
| if opts == nil or type(opts) == 'number' then |
| bufnr = opts |
| opts = ... |
| else |
| vim.validate('opts', opts, 'table', true) |
| end |
|
|
| opts = opts or {} |
| bufnr = vim._resolve_bufnr(bufnr or opts.bufnr) |
|
|
| do |
| |
| |
| |
| |
| |
| local t = global_diagnostic_options.float |
| local float_opts = vim.tbl_extend('keep', opts, type(t) == 'table' and t or {}) |
| opts = get_resolved_options({ float = float_opts }, nil, bufnr).float |
| end |
|
|
| local scope = ({ l = 'line', c = 'cursor', b = 'buffer' })[opts.scope] or opts.scope or 'line' |
| local lnum, col |
| local opts_pos = opts.pos |
| if scope == 'line' or scope == 'cursor' then |
| if not opts_pos then |
| local pos = api.nvim_win_get_cursor(0) |
| lnum = pos[1] - 1 |
| col = pos[2] |
| elseif type(opts_pos) == 'number' then |
| lnum = opts_pos |
| elseif type(opts_pos) == 'table' then |
| lnum, col = opts_pos[1], opts_pos[2] |
| else |
| error("Invalid value for option 'pos'") |
| end |
| elseif scope ~= 'buffer' then |
| error("Invalid value for option 'scope'") |
| end |
|
|
| local diagnostics = get_diagnostics(bufnr, opts , true) |
|
|
| if scope == 'line' then |
| |
| diagnostics = vim.tbl_filter(function(d) |
| return lnum >= d.lnum |
| and lnum <= d.end_lnum |
| and (d.lnum == d.end_lnum or lnum ~= d.end_lnum or d.end_col ~= 0) |
| end, diagnostics) |
| elseif scope == 'cursor' then |
| |
| local line_length = #api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1] |
| |
| diagnostics = vim.tbl_filter(function(d) |
| return lnum >= d.lnum |
| and lnum <= d.end_lnum |
| and (lnum ~= d.lnum or col >= math.min(d.col, line_length - 1)) |
| and ((d.lnum == d.end_lnum and d.col == d.end_col) or lnum ~= d.end_lnum or col < d.end_col) |
| end, diagnostics) |
| end |
|
|
| if vim.tbl_isempty(diagnostics) then |
| return |
| end |
|
|
| local severity_sort = if_nil(opts.severity_sort, global_diagnostic_options.severity_sort) |
| if severity_sort then |
| if type(severity_sort) == 'table' and severity_sort.reverse then |
| table.sort(diagnostics, function(a, b) |
| return a.severity > b.severity |
| end) |
| else |
| table.sort(diagnostics, function(a, b) |
| return a.severity < b.severity |
| end) |
| end |
| end |
|
|
| local lines = {} |
| local highlights = {} |
| local header = if_nil(opts.header, 'Diagnostics:') |
| if header then |
| vim.validate('header', header, { 'string', 'table' }, "'string' or 'table'") |
| if type(header) == 'table' then |
| |
| if string.len(if_nil(header[1], '')) > 0 then |
| table.insert(lines, header[1]) |
| table.insert(highlights, { hlname = header[2] or 'Bold' }) |
| end |
| elseif #header > 0 then |
| table.insert(lines, header) |
| table.insert(highlights, { hlname = 'Bold' }) |
| end |
| end |
|
|
| if opts.format then |
| diagnostics = reformat_diagnostics(opts.format, diagnostics) |
| end |
|
|
| if opts.source and (opts.source ~= 'if_many' or count_sources(bufnr) > 1) then |
| diagnostics = prefix_source(diagnostics) |
| end |
|
|
| local prefix_opt = |
| if_nil(opts.prefix, (scope == 'cursor' and #diagnostics <= 1) and '' or function(_, i) |
| return string.format('%d. ', i) |
| end) |
|
|
| local prefix, prefix_hl_group |
| if prefix_opt then |
| vim.validate( |
| 'prefix', |
| prefix_opt, |
| { 'string', 'table', 'function' }, |
| "'string' or 'table' or 'function'" |
| ) |
| if type(prefix_opt) == 'string' then |
| prefix, prefix_hl_group = prefix_opt, 'NormalFloat' |
| elseif type(prefix_opt) == 'table' then |
| prefix, prefix_hl_group = prefix_opt[1] or '', prefix_opt[2] or 'NormalFloat' |
| end |
| end |
|
|
| local suffix_opt = if_nil(opts.suffix, function(diagnostic) |
| return diagnostic.code and string.format(' [%s]', diagnostic.code) or '' |
| end) |
|
|
| local suffix, suffix_hl_group |
| if suffix_opt then |
| vim.validate( |
| 'suffix', |
| suffix_opt, |
| { 'string', 'table', 'function' }, |
| "'string' or 'table' or 'function'" |
| ) |
| if type(suffix_opt) == 'string' then |
| suffix, suffix_hl_group = suffix_opt, 'NormalFloat' |
| elseif type(suffix_opt) == 'table' then |
| suffix, suffix_hl_group = suffix_opt[1] or '', suffix_opt[2] or 'NormalFloat' |
| end |
| end |
|
|
| for i, diagnostic in ipairs(diagnostics) do |
| if type(prefix_opt) == 'function' then |
| |
| local prefix0, prefix_hl_group0 = prefix_opt(diagnostic, i, #diagnostics) |
| prefix, prefix_hl_group = prefix0 or '', prefix_hl_group0 or 'NormalFloat' |
| end |
| if type(suffix_opt) == 'function' then |
| |
| local suffix0, suffix_hl_group0 = suffix_opt(diagnostic, i, #diagnostics) |
| suffix, suffix_hl_group = suffix0 or '', suffix_hl_group0 or 'NormalFloat' |
| end |
| |
| local hiname = floating_highlight_map[assert(diagnostic.severity)] |
| local message_lines = vim.split(diagnostic.message, '\n') |
| for j = 1, #message_lines do |
| local pre = j == 1 and prefix or string.rep(' ', #prefix) |
| local suf = j == #message_lines and suffix or '' |
| table.insert(lines, pre .. message_lines[j] .. suf) |
| table.insert(highlights, { |
| hlname = hiname, |
| prefix = { |
| length = j == 1 and #prefix or 0, |
| hlname = prefix_hl_group, |
| }, |
| suffix = { |
| length = j == #message_lines and #suffix or 0, |
| hlname = suffix_hl_group, |
| }, |
| }) |
| end |
| end |
|
|
| |
| if not opts.focus_id then |
| opts.focus_id = scope |
| end |
|
|
| |
| local float_bufnr, winnr = vim.lsp.util.open_floating_preview(lines, 'plaintext', opts) |
| vim.bo[float_bufnr].path = vim.bo[bufnr].path |
|
|
| |
| local add_highlight = api.nvim_buf_add_highlight |
|
|
| for i, hl in ipairs(highlights) do |
| local line = lines[i] |
| local prefix_len = hl.prefix and hl.prefix.length or 0 |
| local suffix_len = hl.suffix and hl.suffix.length or 0 |
| if prefix_len > 0 then |
| add_highlight(float_bufnr, -1, hl.prefix.hlname, i - 1, 0, prefix_len) |
| end |
| add_highlight(float_bufnr, -1, hl.hlname, i - 1, prefix_len, #line - suffix_len) |
| if suffix_len > 0 then |
| add_highlight(float_bufnr, -1, hl.suffix.hlname, i - 1, #line - suffix_len, -1) |
| end |
| end |
|
|
| return float_bufnr, winnr |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.reset(namespace, bufnr) |
| vim.validate('namespace', namespace, 'number', true) |
| vim.validate('bufnr', bufnr, 'number', true) |
|
|
| local buffers = bufnr and { vim._resolve_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache) |
| for _, iter_bufnr in ipairs(buffers) do |
| local namespaces = namespace and { namespace } or vim.tbl_keys(diagnostic_cache[iter_bufnr]) |
| for _, iter_namespace in ipairs(namespaces) do |
| diagnostic_cache[iter_bufnr][iter_namespace] = nil |
| M.hide(iter_namespace, iter_bufnr) |
| end |
|
|
| if api.nvim_buf_is_valid(iter_bufnr) then |
| api.nvim_exec_autocmds('DiagnosticChanged', { |
| modeline = false, |
| buffer = iter_bufnr, |
| data = { diagnostics = {} }, |
| }) |
| else |
| diagnostic_cache[iter_bufnr] = nil |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| function M.setqflist(opts) |
| set_list(false, opts) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| function M.setloclist(opts) |
| set_list(true, opts) |
| end |
|
|
| |
| function M.disable(bufnr, namespace) |
| vim.deprecate('vim.diagnostic.disable()', 'vim.diagnostic.enable(false, …)', '0.12') |
| M.enable(false, { bufnr = bufnr, ns_id = namespace }) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.enable(enable, filter) |
| |
| local legacy = (enable or filter) |
| and vim.tbl_contains({ 'number', 'nil' }, type(enable)) |
| and vim.tbl_contains({ 'number', 'nil' }, type(filter)) |
|
|
| if legacy then |
| vim.deprecate( |
| 'vim.diagnostic.enable(buf:number, namespace:number)', |
| 'vim.diagnostic.enable(enable:boolean, filter:table)', |
| '0.12' |
| ) |
|
|
| vim.validate('enable', enable, 'number', true) |
| vim.validate('filter', filter, 'number', true) |
|
|
| local ns_id = type(filter) == 'number' and filter or nil |
| filter = {} |
| filter.ns_id = ns_id |
| filter.bufnr = type(enable) == 'number' and enable or nil |
| enable = true |
| else |
| filter = filter or {} |
| vim.validate('enable', enable, 'boolean', true) |
| vim.validate('filter', filter, 'table', true) |
| end |
|
|
| enable = enable == nil and true or enable |
| local bufnr = filter.bufnr |
| local ns_id = filter.ns_id |
|
|
| if not bufnr then |
| if not ns_id then |
| diagnostic_disabled = ( |
| enable |
| |
| and {} |
| |
| |
| or setmetatable({}, { |
| __index = function() |
| return true |
| end, |
| }) |
| ) |
| else |
| local ns = M.get_namespace(ns_id) |
| ns.disabled = not enable |
| end |
| else |
| bufnr = vim._resolve_bufnr(bufnr) |
| if not ns_id then |
| diagnostic_disabled[bufnr] = (not enable) and true or nil |
| else |
| if type(diagnostic_disabled[bufnr]) ~= 'table' then |
| if enable then |
| return |
| else |
| diagnostic_disabled[bufnr] = {} |
| end |
| end |
| diagnostic_disabled[bufnr][ns_id] = (not enable) and true or nil |
| end |
| end |
|
|
| if enable then |
| M.show(ns_id, bufnr) |
| else |
| M.hide(ns_id, bufnr) |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.match(str, pat, groups, severity_map, defaults) |
| vim.validate('str', str, 'string') |
| vim.validate('pat', pat, 'string') |
| vim.validate('groups', groups, 'table') |
| vim.validate('severity_map', severity_map, 'table', true) |
| vim.validate('defaults', defaults, 'table', true) |
|
|
| |
| severity_map = severity_map or M.severity |
|
|
| local matches = { str:match(pat) } |
| if vim.tbl_isempty(matches) then |
| return |
| end |
|
|
| local diagnostic = {} |
|
|
| for i, match in ipairs(matches) do |
| local field = groups[i] |
| if field == 'severity' then |
| match = severity_map[match] |
| elseif field == 'lnum' or field == 'end_lnum' or field == 'col' or field == 'end_col' then |
| match = assert(tonumber(match)) - 1 |
| end |
| diagnostic[field] = match |
| end |
|
|
| diagnostic = vim.tbl_extend('keep', diagnostic, defaults or {}) |
| diagnostic.severity = diagnostic.severity or M.severity.ERROR |
| diagnostic.col = diagnostic.col or 0 |
| diagnostic.end_lnum = diagnostic.end_lnum or diagnostic.lnum |
| diagnostic.end_col = diagnostic.end_col or diagnostic.col |
| return diagnostic |
| end |
|
|
| local errlist_type_map = { |
| [M.severity.ERROR] = 'E', |
| [M.severity.WARN] = 'W', |
| [M.severity.INFO] = 'I', |
| [M.severity.HINT] = 'N', |
| } |
|
|
| |
| |
| |
| |
| |
| function M.toqflist(diagnostics) |
| vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') |
|
|
| local list = {} |
| for _, v in ipairs(diagnostics) do |
| local item = { |
| bufnr = v.bufnr, |
| lnum = v.lnum + 1, |
| col = v.col and (v.col + 1) or nil, |
| end_lnum = v.end_lnum and (v.end_lnum + 1) or nil, |
| end_col = v.end_col and (v.end_col + 1) or nil, |
| text = v.message, |
| type = errlist_type_map[v.severity] or 'E', |
| } |
| table.insert(list, item) |
| end |
| table.sort(list, function(a, b) |
| if a.bufnr == b.bufnr then |
| if a.lnum == b.lnum then |
| return a.col < b.col |
| else |
| return a.lnum < b.lnum |
| end |
| else |
| return a.bufnr < b.bufnr |
| end |
| end) |
| return list |
| end |
|
|
| |
| |
| |
| |
| function M.fromqflist(list) |
| vim.validate('list', list, 'table') |
|
|
| local diagnostics = {} |
| for _, item in ipairs(list) do |
| if item.valid == 1 then |
| local lnum = math.max(0, item.lnum - 1) |
| local col = math.max(0, item.col - 1) |
| local end_lnum = item.end_lnum > 0 and (item.end_lnum - 1) or lnum |
| local end_col = item.end_col > 0 and (item.end_col - 1) or col |
| local severity = item.type ~= '' and M.severity[item.type] or M.severity.ERROR |
| diagnostics[#diagnostics + 1] = { |
| bufnr = item.bufnr, |
| lnum = lnum, |
| col = col, |
| end_lnum = end_lnum, |
| end_col = end_col, |
| severity = severity, |
| message = item.text, |
| } |
| end |
| end |
| return diagnostics |
| end |
|
|
| return M |
|
|