feature(diagnostic): add vim.diagnostic.count() (#26807)

feat(diagnostic): add `vim.diagnostic.count()`

Problem: Getting diagnostic count based on the output of
  `vim.diagnostic.get()` might become costly as number of diagnostic
  entries grows. This is because it returns a copy of diagnostic cache
  entries (so as to not allow users to change them in place).
  Getting information about diagnostic count is frequently used in
  statusline, so it is important to be as fast as reasonbly possible.

Solution: Add `vim.diagnostic.count()` which computes severity
  counts without making copies.
This commit is contained in:
Evgeni Chasnovski
2024-01-01 23:03:50 +02:00
committed by GitHub
parent 164f1ea06d
commit 4ee656e4f3
5 changed files with 164 additions and 3 deletions

View File

@@ -363,7 +363,6 @@ 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
@@ -374,6 +373,7 @@ 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)
@@ -756,7 +756,31 @@ function M.get(bufnr, opts)
opts = { opts, 't', true },
})
return get_diagnostics(bufnr, opts, false)
return vim.deepcopy(get_diagnostics(bufnr, opts, false))
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.