|
| 1 | +local protocol = vim.lsp.protocol |
| 2 | +local if_nil = vim.F.if_nil |
| 3 | + |
| 4 | +local DiagnosticSeverity = protocol.DiagnosticSeverity |
| 5 | + |
| 6 | +local loclist_type_map = { |
| 7 | + [DiagnosticSeverity.Error] = 'E', |
| 8 | + [DiagnosticSeverity.Warning] = 'W', |
| 9 | + [DiagnosticSeverity.Information] = 'I', |
| 10 | + [DiagnosticSeverity.Hint] = 'I', |
| 11 | +} |
| 12 | + |
| 13 | +local to_severity = function(severity) |
| 14 | + if not severity then return nil end |
| 15 | + return type(severity) == 'string' and DiagnosticSeverity[severity] or severity |
| 16 | +end |
| 17 | + |
| 18 | +local filter_to_severity_limit = function(severity, diagnostics) |
| 19 | + local filter_level = to_severity(severity) |
| 20 | + if not filter_level then |
| 21 | + return diagnostics |
| 22 | + end |
| 23 | + |
| 24 | + return vim.tbl_filter(function(t) return t.severity == filter_level end, diagnostics) |
| 25 | +end |
| 26 | + |
| 27 | +local filter_by_severity_limit = function(severity_limit, diagnostics) |
| 28 | + local filter_level = to_severity(severity_limit) |
| 29 | + if not filter_level then |
| 30 | + return diagnostics |
| 31 | + end |
| 32 | + |
| 33 | + return vim.tbl_filter(function(t) return t.severity <= filter_level end, diagnostics) |
| 34 | +end |
| 35 | + |
| 36 | +-- Keep it as a global so it stays between reloads, caches and exposed to view. |
| 37 | +_LspExtensionsWorkspaceCache = _LspExtensionsWorkspaceCache or {} |
| 38 | + |
| 39 | +local M = {} |
| 40 | + |
| 41 | +-- { client: stuff } |
| 42 | +M.diagnostic_cache = _LspExtensionsWorkspaceCache |
| 43 | + |
| 44 | +M.handler = function(err, method, result, client_id, bufnr, config) |
| 45 | + vim.lsp.diagnostic.on_publish_diagnostics(err, method, result, client_id, bufnr, config) |
| 46 | + |
| 47 | + if not result then return end |
| 48 | + |
| 49 | + bufnr = bufnr or vim.uri_to_bufnr(result.uri) |
| 50 | + |
| 51 | + if not M.diagnostic_cache[client_id] then |
| 52 | + M.diagnostic_cache[client_id] = {} |
| 53 | + end |
| 54 | + |
| 55 | + local diagnostics = result.diagnostics |
| 56 | + |
| 57 | + local counts = {} |
| 58 | + for _, severity in ipairs(DiagnosticSeverity) do |
| 59 | + counts[to_severity(severity)] = vim.lsp.diagnostic.get_count(bufnr, severity, client_id) |
| 60 | + end |
| 61 | + |
| 62 | + |
| 63 | + M.diagnostic_cache[client_id][bufnr] = { |
| 64 | + diagnostics = diagnostics, |
| 65 | + counts = counts, |
| 66 | + } |
| 67 | +end |
| 68 | + |
| 69 | +M.get_count = function(bufnr, severity) |
| 70 | + if bufnr == 0 or not bufnr then |
| 71 | + bufnr = vim.api.nvim_get_current_buf() |
| 72 | + end |
| 73 | + |
| 74 | + severity = to_severity(severity) |
| 75 | + |
| 76 | + local count = 0 |
| 77 | + local clients = vim.lsp.buf_get_clients(bufnr) |
| 78 | + |
| 79 | + for client_id, _ in pairs(clients) do |
| 80 | + for _, diagnostic_cache in pairs(M.diagnostic_cache[client_id] or {}) do |
| 81 | + if diagnostic_cache.counts then |
| 82 | + count = count + diagnostic_cache.counts[severity] |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + return count |
| 88 | +end |
| 89 | + |
| 90 | +M.set_qf_list = function(opts) |
| 91 | + opts = opts or {} |
| 92 | + |
| 93 | + local open_qflist = if_nil(opts.open_qflist, true) |
| 94 | + |
| 95 | + local bufnr = vim.api.nvim_get_current_buf() |
| 96 | + local diags_by_buffer = M.get(bufnr) |
| 97 | + |
| 98 | + for diag_bufnr, diags in pairs(diags_by_buffer) do |
| 99 | + if opts.severity then |
| 100 | + diags_by_buffer[diag_bufnr] = filter_to_severity_limit(opts.severity, diags) |
| 101 | + elseif opts.severity_limit then |
| 102 | + diags_by_buffer[diag_bufnr] = filter_by_severity_limit(opts.severity_limit, diags) |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + -- P(diags_by_buffer) |
| 107 | + -- if true then return end |
| 108 | + |
| 109 | + local item_list = {} |
| 110 | + |
| 111 | + local insert_diag = function(diag_bufnr, diag) |
| 112 | + local pos = diag.range.start |
| 113 | + local row = pos.line |
| 114 | + |
| 115 | + local col_ok, col = pcall(vim.lsp.util.character_offset, bufnr, row, pos.character) |
| 116 | + if not col_ok then |
| 117 | + col = pos.character |
| 118 | + end |
| 119 | + |
| 120 | + local line = (vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false) or {""})[1] or "" |
| 121 | + |
| 122 | + table.insert(item_list, { |
| 123 | + bufnr = diag_bufnr, |
| 124 | + lnum = row + 1, |
| 125 | + col = col + 1, |
| 126 | + text = line .. " | " .. diag.message, |
| 127 | + type = loclist_type_map[diag.severity or DiagnosticSeverity.Error] or 'E', |
| 128 | + }) |
| 129 | + end |
| 130 | + |
| 131 | + for diag_bufnr, diags in pairs(diags_by_buffer) do |
| 132 | + for _, v in ipairs(diags) do |
| 133 | + insert_diag(diag_bufnr, v) |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + vim.fn.setqflist({}, 'r', { title = 'LSP Diagnostics'; items = item_list; }) |
| 138 | + |
| 139 | + if open_qflist then |
| 140 | + vim.cmd [[copen]] |
| 141 | + end |
| 142 | +end |
| 143 | + |
| 144 | +function M.get(bufnr) |
| 145 | + local result = {} |
| 146 | + |
| 147 | + for client_id, _ in pairs(vim.lsp.buf_get_clients(bufnr)) do |
| 148 | + for diag_bufnr, diag_cache in pairs(M.diagnostic_cache[client_id] or {}) do |
| 149 | + if not result[diag_bufnr] then |
| 150 | + result[diag_bufnr] = {} |
| 151 | + end |
| 152 | + |
| 153 | + vim.list_extend(result[diag_bufnr], diag_cache.diagnostics or {}) |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + return result |
| 158 | +end |
| 159 | + |
| 160 | +return M |
0 commit comments