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