robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
5.0 KiB179 linesraw
1local api = vim.api
2
3local store = require('vim.diagnostic._store')
4
5--- @class (private) vim.diagnostic._shared
6local M = {}
7
8--- @param bufnr integer
9--- @return integer
10function M.count_sources(bufnr)
11  local count = 0
12  local seen = {} --- @type table<string, true>
13  for _, diagnostic in ipairs(store.get_diagnostics(bufnr, nil, false)) do
14    local source = diagnostic.source
15    if source and not seen[source] then
16      seen[source] = true
17      count = count + 1
18    end
19  end
20  return count
21end
22
23--- @param diagnostics vim.Diagnostic[]
24--- @return vim.Diagnostic[]
25function M.prefix_source(diagnostics)
26  --- @param diagnostic vim.Diagnostic
27  return vim.tbl_map(function(diagnostic)
28    if not diagnostic.source then
29      return diagnostic
30    end
31
32    local copied = vim.deepcopy(diagnostic, true)
33    copied.message = string.format('%s: %s', diagnostic.source, diagnostic.message)
34    return copied
35  end, diagnostics)
36end
37
38--- Get a position based on an extmark referenced by `_extmark_id` field
39--- @param diagnostic vim.Diagnostic
40--- @return integer lnum
41--- @return integer col
42--- @return integer end_lnum
43--- @return integer end_col
44--- @return boolean valid
45function M.get_logical_pos(diagnostic)
46  if not diagnostic._extmark_id then
47    return diagnostic.lnum, diagnostic.col, diagnostic.end_lnum, diagnostic.end_col, true
48  end
49
50  local ns = vim.diagnostic.get_namespace(assert(diagnostic.namespace))
51  local extmark = api.nvim_buf_get_extmark_by_id(
52    diagnostic.bufnr,
53    ns.user_data.location_ns,
54    diagnostic._extmark_id,
55    { details = true }
56  )
57  if next(extmark) == nil then
58    return diagnostic.lnum, diagnostic.col, diagnostic.end_lnum, diagnostic.end_col, true
59  end
60
61  -- Diagnostic extmarks always have an end position.
62  ---@cast extmark [integer, integer, {end_row: integer, end_col: integer, invalid?: boolean}]
63  return extmark[1], extmark[2], extmark[3].end_row, extmark[3].end_col, not extmark[3].invalid
64end
65
66--- @param diagnostics vim.Diagnostic[]?
67--- @param use_logical_pos boolean
68--- @return table<integer, vim.Diagnostic[]>
69function M.diagnostic_lines(diagnostics, use_logical_pos)
70  if not diagnostics then
71    return {}
72  end
73
74  local diagnostics_by_line = {} --- @type table<integer, vim.Diagnostic[]>
75  for _, diagnostic in ipairs(diagnostics) do
76    local lnum --- @type integer
77    local valid --- @type boolean
78
79    if use_logical_pos then
80      lnum, _, _, _, valid = M.get_logical_pos(diagnostic)
81    else
82      lnum, valid = diagnostic.lnum, true
83    end
84
85    if valid then
86      local line_diagnostics = diagnostics_by_line[lnum]
87      if not line_diagnostics then
88        line_diagnostics = {}
89        diagnostics_by_line[lnum] = line_diagnostics
90      end
91      line_diagnostics[#line_diagnostics + 1] = diagnostic
92    end
93  end
94  return diagnostics_by_line
95end
96
97--- @param diagnostics table<integer, vim.Diagnostic[]>
98--- @return vim.Diagnostic[]
99function M.diagnostics_at_cursor(diagnostics)
100  local lnum = api.nvim_win_get_cursor(0)[1] - 1
101
102  if diagnostics[lnum] ~= nil then
103    return diagnostics[lnum]
104  end
105
106  local cursor_diagnostics = {} --- @type vim.Diagnostic[]
107  for _, line_diags in pairs(diagnostics) do
108    for _, diagnostic in ipairs(line_diags) do
109      if diagnostic.end_lnum and lnum >= diagnostic.lnum and lnum <= diagnostic.end_lnum then
110        cursor_diagnostics[#cursor_diagnostics + 1] = diagnostic
111      end
112    end
113  end
114  return cursor_diagnostics
115end
116
117--- @param a vim.Diagnostic
118--- @param b vim.Diagnostic
119--- @param primary_key string
120--- @param reverse boolean
121--- @param col_fn? fun(diagnostic: vim.Diagnostic): integer
122--- @return boolean
123function M.diagnostic_cmp(a, b, primary_key, reverse, col_fn)
124  local a_val, b_val --- @type integer, integer
125  if col_fn then
126    a_val, b_val = col_fn(a), col_fn(b)
127  else
128    a_val = a[primary_key] --[[@as integer]]
129    b_val = b[primary_key] --[[@as integer]]
130  end
131
132  --- @param x integer
133  --- @param y integer
134  local function cmp(x, y)
135    if reverse then
136      return x > y
137    end
138    return x < y
139  end
140
141  if a_val ~= b_val then
142    return cmp(a_val, b_val)
143  end
144  if a.lnum ~= b.lnum then
145    return cmp(a.lnum, b.lnum)
146  end
147  if a.col ~= b.col then
148    return cmp(a.col, b.col)
149  end
150  if a.end_lnum ~= b.end_lnum then
151    return cmp(a.end_lnum, b.end_lnum)
152  end
153  if a.end_col ~= b.end_col then
154    return cmp(a.end_col, b.end_col)
155  end
156
157  return cmp(a._extmark_id or 0, b._extmark_id or 0)
158end
159
160--- @param format fun(diagnostic: vim.Diagnostic): string?
161--- @param diagnostics vim.Diagnostic[]
162--- @return vim.Diagnostic[]
163function M.reformat_diagnostics(format, diagnostics)
164  vim.validate('format', format, 'function')
165  vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics')
166
167  local formatted = {} --- @type vim.Diagnostic[]
168  for _, diagnostic in ipairs(diagnostics) do
169    local message = format(diagnostic)
170    if message ~= nil then
171      local formatted_diagnostic = vim.deepcopy(diagnostic, true)
172      formatted_diagnostic.message = message
173      formatted[#formatted + 1] = formatted_diagnostic
174    end
175  end
176  return formatted
177end
178
179return M