robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
8.2 KiB294 linesraw
1local api, nonnil = vim.api, vim.nonnil
2local shared = require('vim.diagnostic._shared')
3local store = require('vim.diagnostic._store')
4
5--- @class (private) vim.diagnostic._JumpOpts : vim.diagnostic.JumpOpts
6--- @field _highest? boolean
7--- @field win_id? integer
8--- @field cursor_position? [integer, integer]
9--- @field float? table|boolean
10
11--- @class (private) vim.diagnostic._jump
12local M = {}
13
14--- @param diagnostics vim.Diagnostic[]
15local function filter_highest(diagnostics)
16  table.sort(diagnostics, function(a, b)
17    return shared.diagnostic_cmp(a, b, 'severity', false)
18  end)
19
20  -- Find the first diagnostic where the severity does not match the highest severity, and remove
21  -- that element and all subsequent elements from the array
22  local worst = (diagnostics[1] or {}).severity
23  for i = 2, #diagnostics do
24    if diagnostics[i].severity ~= worst then
25      for j = i, #diagnostics do
26        diagnostics[j] = nil
27      end
28      break
29    end
30  end
31end
32
33--- @param search_forward boolean
34--- @param opts vim.diagnostic.JumpOpts?
35--- @param use_logical_pos boolean
36--- @return vim.Diagnostic?
37local function next_diagnostic(search_forward, opts, use_logical_pos)
38  opts = opts or {}
39  --- @cast opts vim.diagnostic._JumpOpts
40
41  -- Support deprecated win_id alias
42  if opts.win_id then
43    vim.deprecate('opts.win_id', 'opts.winid', '0.13')
44    opts.winid = opts.win_id
45    opts.win_id = nil --- @diagnostic disable-line
46  end
47
48  -- Support deprecated cursor_position alias
49  if opts.cursor_position then
50    vim.deprecate('opts.cursor_position', 'opts.pos', '0.13')
51    opts.pos = opts.cursor_position
52    opts.cursor_position = nil --- @diagnostic disable-line
53  end
54
55  local winid = opts.winid or api.nvim_get_current_win()
56  local bufnr = api.nvim_win_get_buf(winid)
57  local position = opts.pos or api.nvim_win_get_cursor(winid)
58
59  -- Adjust row to be 0-indexed
60  position[1] = position[1] - 1
61
62  local wrap = nonnil(opts.wrap, true)
63  local diagnostics = store.get_diagnostics(bufnr, opts, true)
64
65  if opts._highest then
66    filter_highest(diagnostics)
67  end
68
69  local line_diagnostics = shared.diagnostic_lines(diagnostics, use_logical_pos)
70
71  --- @param diagnostic vim.Diagnostic
72  --- @return integer
73  local function col_fn(diagnostic)
74    return use_logical_pos and select(2, shared.get_logical_pos(diagnostic)) or diagnostic.col
75  end
76
77  local line_count = api.nvim_buf_line_count(bufnr)
78  for i = 0, line_count do
79    local offset = i * (search_forward and 1 or -1)
80    local lnum = position[1] + offset
81    if lnum < 0 or lnum >= line_count then
82      if not wrap then
83        return
84      end
85      lnum = (lnum + line_count) % line_count
86    end
87
88    if line_diagnostics[lnum] and not vim.tbl_isempty(line_diagnostics[lnum]) then
89      local line_length = #api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1]
90      local sort_diagnostics, is_next --- @type function, function
91      if search_forward then
92        --- @param a vim.Diagnostic
93        --- @param b vim.Diagnostic
94        sort_diagnostics = function(a, b)
95          return shared.diagnostic_cmp(a, b, 'col', false, col_fn)
96        end
97        --- @param diagnostic vim.Diagnostic
98        is_next = function(diagnostic)
99          return math.min(col_fn(diagnostic), math.max(line_length - 1, 0)) > position[2]
100        end
101      else
102        --- @param a vim.Diagnostic
103        --- @param b vim.Diagnostic
104        sort_diagnostics = function(a, b)
105          return shared.diagnostic_cmp(a, b, 'col', true, col_fn)
106        end
107        --- @param diagnostic vim.Diagnostic
108        is_next = function(diagnostic)
109          return math.min(col_fn(diagnostic), math.max(line_length - 1, 0)) < position[2]
110        end
111      end
112
113      table.sort(line_diagnostics[lnum], sort_diagnostics)
114      if i == 0 then
115        for _, diagnostic in ipairs(line_diagnostics[lnum]) do
116          if is_next(diagnostic) then
117            return diagnostic
118          end
119        end
120      else
121        return line_diagnostics[lnum][1]
122      end
123    end
124  end
125end
126
127--- @param diagnostic vim.Diagnostic?
128--- @param opts vim.diagnostic.JumpOpts?
129local function goto_diagnostic(diagnostic, opts)
130  if not diagnostic then
131    api.nvim_echo({ { 'No more valid diagnostics to move to', 'WarningMsg' } }, true)
132    return
133  end
134
135  opts = opts or {}
136  --- @cast opts vim.diagnostic._JumpOpts
137
138  -- Support deprecated win_id alias
139  if opts.win_id then
140    vim.deprecate('opts.win_id', 'opts.winid', '0.13')
141    opts.winid = opts.win_id
142    opts.win_id = nil --- @diagnostic disable-line
143  end
144
145  local winid = opts.winid or api.nvim_get_current_win()
146  local lnum, col = shared.get_logical_pos(diagnostic)
147
148  vim._with({ win = winid }, function()
149    -- Save position in the window's jumplist
150    vim.cmd("normal! m'")
151    api.nvim_win_set_cursor(winid, { lnum + 1, col })
152    -- Open folds under the cursor
153    vim.cmd('normal! zv')
154  end)
155
156  if opts.float then
157    vim.deprecate('opts.float', 'opts.on_jump', '0.14')
158    local float_opts = opts.float
159    float_opts = type(float_opts) == 'table' and float_opts or {}
160
161    opts.on_jump = function(_, bufnr)
162      vim.diagnostic.open_float(vim.tbl_extend('keep', float_opts, {
163        bufnr = bufnr,
164        scope = 'cursor',
165        focus = false,
166      }))
167    end
168
169    opts.float = nil --- @diagnostic disable-line
170  end
171
172  if opts.on_jump then
173    vim.schedule(function()
174      opts.on_jump(diagnostic, api.nvim_win_get_buf(winid))
175    end)
176  end
177end
178
179--- @param opts? vim.diagnostic.JumpOpts
180--- @return vim.Diagnostic?
181function M.get_prev(opts)
182  return next_diagnostic(false, opts, false)
183end
184
185--- @param opts? vim.diagnostic.JumpOpts
186--- @return table|false
187function M.get_prev_pos(opts)
188  vim.deprecate(
189    'vim.diagnostic.get_prev_pos()',
190    'access the lnum and col fields from get_prev() instead',
191    '0.13'
192  )
193  local prev = M.get_prev(opts)
194  if not prev then
195    return false
196  end
197
198  return { prev.lnum, prev.col }
199end
200
201--- @param opts? vim.diagnostic.JumpOpts
202function M.goto_prev(opts)
203  vim.deprecate('vim.diagnostic.goto_prev()', 'vim.diagnostic.jump()', '0.13')
204  opts = opts or {}
205  opts.float = nonnil(opts.float, true) --- @diagnostic disable-line
206  goto_diagnostic(M.get_prev(opts), opts)
207end
208
209--- @param opts? vim.diagnostic.JumpOpts
210--- @return vim.Diagnostic?
211function M.get_next(opts)
212  return next_diagnostic(true, opts, false)
213end
214
215--- @param opts? vim.diagnostic.JumpOpts
216--- @return table|false
217function M.get_next_pos(opts)
218  vim.deprecate(
219    'vim.diagnostic.get_next_pos()',
220    'access the lnum and col fields from get_next() instead',
221    '0.13'
222  )
223  local next = M.get_next(opts)
224  if not next then
225    return false
226  end
227
228  return { next.lnum, next.col }
229end
230
231--- @param opts vim.diagnostic.JumpOpts
232--- @return vim.Diagnostic?
233function M.jump(opts)
234  vim.validate('opts', opts, 'table')
235
236  -- One of "diagnostic" or "count" must be provided
237  assert(
238    opts.diagnostic or opts.count,
239    'One of "diagnostic" or "count" must be specified in the options to vim.diagnostic.jump()'
240  )
241
242  -- Apply configuration options from vim.diagnostic.config()
243  local config = vim.diagnostic.config().jump or {}
244  opts = vim.tbl_deep_extend('keep', opts, config)
245  --- @cast opts vim.diagnostic._JumpOpts
246
247  if opts.diagnostic then
248    goto_diagnostic(opts.diagnostic, opts)
249    return opts.diagnostic
250  end
251
252  local count = assert(opts.count)
253  if count == 0 then
254    return nil
255  end
256
257  -- Support deprecated cursor_position alias
258  if opts.cursor_position then
259    vim.deprecate('opts.cursor_position', 'opts.pos', '0.13')
260    opts.pos = opts.cursor_position
261    opts.cursor_position = nil --- @diagnostic disable-line
262  end
263
264  local diagnostic --- @type vim.Diagnostic?
265  while count ~= 0 do
266    local next = next_diagnostic(count > 0, opts, true)
267    if not next then
268      break
269    end
270
271    -- Update cursor position
272    opts.pos = { next.lnum + 1, next.col }
273
274    if count > 0 then
275      count = count - 1
276    else
277      count = count + 1
278    end
279    diagnostic = next
280  end
281
282  goto_diagnostic(diagnostic, opts)
283  return diagnostic
284end
285
286--- @param opts? vim.diagnostic.JumpOpts
287function M.goto_next(opts)
288  vim.deprecate('vim.diagnostic.goto_next()', 'vim.diagnostic.jump()', '0.13')
289  opts = opts or {}
290  opts.float = nonnil(opts.float, true) --- @diagnostic disable-line
291  goto_diagnostic(M.get_next(opts), opts)
292end
293
294return M