robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
148.2 KiB4576 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local command = n.command
7local clear = n.clear
8local exec_lua = n.exec_lua
9local eq = t.eq
10local neq = t.neq
11local matches = t.matches
12local retry = t.retry
13local api = n.api
14local pcall_err = t.pcall_err
15local fn = n.fn
16
17describe('vim.diagnostic', function()
18  before_each(function()
19    clear()
20
21    exec_lua(function()
22      require('vim.diagnostic')
23
24      local function make_diagnostic(msg, lnum, col, end_lnum, end_col, severity, source, code)
25        return {
26          lnum = lnum,
27          col = col,
28          end_lnum = end_lnum,
29          end_col = end_col,
30          message = msg,
31          severity = severity,
32          source = source,
33          code = code,
34        }
35      end
36
37      function _G.make_error(msg, lnum, col, end_lnum, end_col, source, code)
38        return make_diagnostic(
39          msg,
40          lnum,
41          col,
42          end_lnum,
43          end_col,
44          vim.diagnostic.severity.ERROR,
45          source,
46          code
47        )
48      end
49
50      function _G.make_warning(msg, lnum, col, end_lnum, end_col, source, code)
51        return make_diagnostic(
52          msg,
53          lnum,
54          col,
55          end_lnum,
56          end_col,
57          vim.diagnostic.severity.WARN,
58          source,
59          code
60        )
61      end
62
63      function _G.make_info(msg, lnum, col, end_lnum, end_col, source, code)
64        return make_diagnostic(
65          msg,
66          lnum,
67          col,
68          end_lnum,
69          end_col,
70          vim.diagnostic.severity.INFO,
71          source,
72          code
73        )
74      end
75
76      function _G.make_hint(msg, lnum, col, end_lnum, end_col, source, code)
77        return make_diagnostic(
78          msg,
79          lnum,
80          col,
81          end_lnum,
82          end_col,
83          vim.diagnostic.severity.HINT,
84          source,
85          code
86        )
87      end
88
89      function _G.count_diagnostics(bufnr, severity, namespace)
90        return #vim.diagnostic.get(bufnr, { severity = severity, namespace = namespace })
91      end
92
93      function _G.count_extmarks(bufnr, namespace)
94        local ns = vim.diagnostic.get_namespace(namespace)
95        local extmarks = 0
96        if ns.user_data.virt_text_ns then
97          extmarks = extmarks
98            + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.virt_text_ns, 0, -1, {})
99        end
100        if ns.user_data.underline_ns then
101          extmarks = extmarks
102            + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.underline_ns, 0, -1, {})
103        end
104        return extmarks
105      end
106
107      function _G.get_virt_text_extmarks(ns)
108        ns = vim.diagnostic.get_namespace(ns)
109        local virt_text_ns = ns.user_data.virt_text_ns
110        return vim.api.nvim_buf_get_extmarks(
111          _G.diagnostic_bufnr,
112          virt_text_ns,
113          0,
114          -1,
115          { details = true }
116        )
117      end
118
119      function _G.get_virt_lines_extmarks(ns)
120        ns = vim.diagnostic.get_namespace(ns)
121        local virt_lines_ns = ns.user_data.virt_lines_ns
122        return vim.api.nvim_buf_get_extmarks(
123          _G.diagnostic_bufnr,
124          virt_lines_ns,
125          0,
126          -1,
127          { details = true }
128        )
129      end
130
131      ---@param ns integer
132      function _G.get_underline_extmarks(ns)
133        ---@type integer
134        local underline_ns = vim.diagnostic.get_namespace(ns).user_data.underline_ns
135        return vim.api.nvim_buf_get_extmarks(
136          _G.diagnostic_bufnr,
137          underline_ns,
138          0,
139          -1,
140          { details = true }
141        )
142      end
143    end)
144
145    exec_lua(function()
146      _G.diagnostic_ns = vim.api.nvim_create_namespace('diagnostic_spec')
147      _G.other_ns = vim.api.nvim_create_namespace('other_namespace')
148      _G.diagnostic_bufnr = vim.api.nvim_create_buf(true, false)
149      local lines = { '1st line of text', '2nd line of text', 'wow', 'cool', 'more', 'lines' }
150      vim.fn.bufload(_G.diagnostic_bufnr)
151      vim.api.nvim_buf_set_lines(_G.diagnostic_bufnr, 0, 1, false, lines)
152    end)
153  end)
154
155  it('creates highlight groups', function()
156    command('runtime plugin/diagnostic.vim')
157    eq({
158      'DiagnosticDeprecated',
159      'DiagnosticError',
160      'DiagnosticFloatingError',
161      'DiagnosticFloatingHint',
162      'DiagnosticFloatingInfo',
163      'DiagnosticFloatingOk',
164      'DiagnosticFloatingWarn',
165      'DiagnosticHint',
166      'DiagnosticInfo',
167      'DiagnosticOk',
168      'DiagnosticSignError',
169      'DiagnosticSignHint',
170      'DiagnosticSignInfo',
171      'DiagnosticSignOk',
172      'DiagnosticSignWarn',
173      'DiagnosticUnderlineError',
174      'DiagnosticUnderlineHint',
175      'DiagnosticUnderlineInfo',
176      'DiagnosticUnderlineOk',
177      'DiagnosticUnderlineWarn',
178      'DiagnosticUnnecessary',
179      'DiagnosticVirtualLinesError',
180      'DiagnosticVirtualLinesHint',
181      'DiagnosticVirtualLinesInfo',
182      'DiagnosticVirtualLinesOk',
183      'DiagnosticVirtualLinesWarn',
184      'DiagnosticVirtualTextError',
185      'DiagnosticVirtualTextHint',
186      'DiagnosticVirtualTextInfo',
187      'DiagnosticVirtualTextOk',
188      'DiagnosticVirtualTextWarn',
189      'DiagnosticWarn',
190    }, fn.getcompletion('Diagnostic', 'highlight'))
191  end)
192
193  it('retrieves diagnostics from all buffers and namespaces', function()
194    local result = exec_lua(function()
195      local other_bufnr = vim.api.nvim_create_buf(true, false)
196      local lines = vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, -1, true)
197      vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines)
198
199      vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
200        _G.make_error('Diagnostic #1', 1, 1, 1, 1),
201        _G.make_error('Diagnostic #2', 2, 1, 2, 1),
202      })
203      vim.diagnostic.set(_G.other_ns, other_bufnr, {
204        _G.make_error('Diagnostic #3', 3, 1, 3, 1),
205      })
206      return vim.diagnostic.get()
207    end)
208    eq(3, #result)
209    eq(
210      2,
211      exec_lua(function()
212        return #vim.tbl_filter(function(d)
213          return d.bufnr == _G.diagnostic_bufnr
214        end, result)
215      end)
216    )
217    eq('Diagnostic #1', result[1].message)
218  end)
219
220  it('removes diagnostics from the cache when a buffer is removed', function()
221    eq(
222      2,
223      exec_lua(function()
224        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
225        local other_bufnr = vim.fn.bufadd('test | test')
226        local lines = vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, -1, true)
227        vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines)
228        vim.cmd('bunload! ' .. other_bufnr)
229
230        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
231          _G.make_error('Diagnostic #1', 1, 1, 1, 1),
232          _G.make_error('Diagnostic #2', 2, 1, 2, 1),
233        })
234        vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, {
235          _G.make_error('Diagnostic #3', 3, 1, 3, 1),
236        })
237        vim.api.nvim_set_current_buf(other_bufnr)
238        vim.opt_local.buflisted = true
239        vim.cmd('bwipeout!')
240        return #vim.diagnostic.get()
241      end)
242    )
243    eq(
244      2,
245      exec_lua(function()
246        vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
247        vim.opt_local.buflisted = false
248        return #vim.diagnostic.get()
249      end)
250    )
251    eq(
252      0,
253      exec_lua(function()
254        vim.cmd('bwipeout!')
255        return #vim.diagnostic.get()
256      end)
257    )
258  end)
259
260  it('removes diagnostic from stale cache on reset', function()
261    local diagnostics = exec_lua(function()
262      vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
263        _G.make_error('Diagnostic #1', 1, 1, 1, 1),
264        _G.make_error('Diagnostic #2', 2, 1, 2, 1),
265      })
266      vim.fn.bufadd('test | test')
267      vim.cmd('noautocmd bwipeout! ' .. _G.diagnostic_bufnr)
268      return vim.diagnostic.get(_G.diagnostic_bufnr)
269    end)
270    eq(2, #diagnostics)
271    diagnostics = exec_lua(function()
272      vim.diagnostic.reset()
273      return vim.diagnostic.get()
274    end)
275    eq(0, #diagnostics)
276  end)
277
278  it('always returns a copy of diagnostic tables', function()
279    local result = exec_lua(function()
280      vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
281        _G.make_error('Diagnostic #1', 1, 1, 1, 1),
282      })
283      local diag = vim.diagnostic.get()
284      diag[1].col = 10000
285      return vim.diagnostic.get()[1].col == 10000
286    end)
287    eq(false, result)
288  end)
289
290  it('resolves buffer number 0 to the current buffer', function()
291    eq(
292      2,
293      exec_lua(function()
294        vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
295        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
296          _G.make_error('Diagnostic #1', 1, 1, 1, 1),
297          _G.make_error('Diagnostic #2', 2, 1, 2, 1),
298        })
299        return #vim.diagnostic.get(0)
300      end)
301    )
302  end)
303
304  it('saves and count a single error', function()
305    eq(
306      1,
307      exec_lua(function()
308        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
309          _G.make_error('Diagnostic #1', 1, 1, 1, 1),
310        })
311        return _G.count_diagnostics(
312          _G.diagnostic_bufnr,
313          vim.diagnostic.severity.ERROR,
314          _G.diagnostic_ns
315        )
316      end)
317    )
318  end)
319
320  it('saves and count multiple errors', function()
321    eq(
322      2,
323      exec_lua(function()
324        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
325          _G.make_error('Diagnostic #1', 1, 1, 1, 1),
326          _G.make_error('Diagnostic #2', 2, 1, 2, 1),
327        })
328        return _G.count_diagnostics(
329          _G.diagnostic_bufnr,
330          vim.diagnostic.severity.ERROR,
331          _G.diagnostic_ns
332        )
333      end)
334    )
335  end)
336
337  it('saves and count from multiple namespaces', function()
338    eq(
339      { 1, 1, 2 },
340      exec_lua(function()
341        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
342          _G.make_error('Diagnostic From Server 1', 1, 1, 1, 1),
343        })
344        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
345          _G.make_error('Diagnostic From Server 2', 1, 1, 1, 1),
346        })
347        return {
348          -- First namespace
349          _G.count_diagnostics(
350            _G.diagnostic_bufnr,
351            vim.diagnostic.severity.ERROR,
352            _G.diagnostic_ns
353          ),
354          -- Second namespace
355          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.other_ns),
356          -- All namespaces
357          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR),
358        }
359      end)
360    )
361  end)
362
363  it('saves and count from multiple namespaces with respect to severity', function()
364    eq(
365      { 3, 0, 3 },
366      exec_lua(function()
367        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
368          _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
369          _G.make_error('Diagnostic From Server 1:2', 2, 2, 2, 2),
370          _G.make_error('Diagnostic From Server 1:3', 2, 3, 3, 2),
371        })
372        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
373          _G.make_warning('Warning From Server 2', 3, 3, 3, 3),
374        })
375        return {
376          -- Namespace 1
377          _G.count_diagnostics(
378            _G.diagnostic_bufnr,
379            vim.diagnostic.severity.ERROR,
380            _G.diagnostic_ns
381          ),
382          -- Namespace 2
383          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.other_ns),
384          -- All namespaces
385          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR),
386        }
387      end)
388    )
389  end)
390
391  it('handles one namespace clearing highlights while the other still has highlights', function()
392    exec_lua(function()
393      vim.diagnostic.config({ virtual_text = true })
394    end)
395    -- 1 Error (1)
396    -- 1 Warning (2)
397    -- 1 Warning (2) + 1 Warning (1)
398    -- 2 highlights and 2 underlines (since error)
399    -- 1 highlight + 1 underline
400    local all_highlights = { 1, 1, 2, 4, 2 }
401    eq(
402      all_highlights,
403      exec_lua(function()
404        local ns_1_diags = {
405          _G.make_error('Error 1', 1, 1, 1, 5),
406          _G.make_warning('Warning on Server 1', 2, 1, 2, 3),
407        }
408        local ns_2_diags = {
409          _G.make_warning('Warning 1', 2, 1, 2, 3),
410        }
411
412        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
413        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
414
415        return {
416          _G.count_diagnostics(
417            _G.diagnostic_bufnr,
418            vim.diagnostic.severity.ERROR,
419            _G.diagnostic_ns
420          ),
421          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
422          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
423          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
424          _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
425        }
426      end)
427    )
428
429    -- Clear diagnostics from namespace 1, and make sure we have the right amount of stuff for namespace 2
430    eq(
431      { 1, 1, 2, 0, 2 },
432      exec_lua(function()
433        vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
434        return {
435          _G.count_diagnostics(
436            _G.diagnostic_bufnr,
437            vim.diagnostic.severity.ERROR,
438            _G.diagnostic_ns
439          ),
440          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
441          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
442          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
443          _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
444        }
445      end)
446    )
447
448    -- Show diagnostics from namespace 1 again
449    eq(
450      all_highlights,
451      exec_lua(function()
452        vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
453        return {
454          _G.count_diagnostics(
455            _G.diagnostic_bufnr,
456            vim.diagnostic.severity.ERROR,
457            _G.diagnostic_ns
458          ),
459          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
460          _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
461          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
462          _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
463        }
464      end)
465    )
466  end)
467
468  it('does not display diagnostics when disabled', function()
469    exec_lua(function()
470      vim.diagnostic.config({ virtual_text = true })
471    end)
472
473    eq(
474      { 0, 2 },
475      exec_lua(function()
476        local ns_1_diags = {
477          _G.make_error('Error 1', 1, 1, 1, 5),
478          _G.make_warning('Warning on Server 1', 2, 1, 2, 3),
479        }
480        local ns_2_diags = {
481          _G.make_warning('Warning 1', 2, 1, 2, 3),
482        }
483
484        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
485        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
486
487        vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
488
489        return {
490          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
491          _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
492        }
493      end)
494    )
495
496    eq(
497      { 4, 0 },
498      exec_lua(function()
499        vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
500        vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.other_ns })
501
502        return {
503          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
504          _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
505        }
506      end)
507    )
508  end)
509
510  describe('show() and hide()', function()
511    it('works', function()
512      local result = exec_lua(function()
513        local other_bufnr = vim.api.nvim_create_buf(true, false)
514
515        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
516
517        local result = {}
518
519        vim.diagnostic.config({ underline = false, virtual_text = true })
520
521        local ns_1_diags = {
522          _G.make_error('Error 1', 1, 1, 1, 5),
523          _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
524        }
525        local ns_2_diags = {
526          _G.make_warning('Warning 1', 2, 1, 2, 5),
527        }
528        local other_buffer_diags = {
529          _G.make_info('This is interesting', 0, 0, 0, 0),
530        }
531
532        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
533        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
534        vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
535
536        -- All buffers and namespaces
537        table.insert(
538          result,
539          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
540            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
541            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
542        )
543
544        -- Hide one namespace
545        vim.diagnostic.hide(_G.diagnostic_ns)
546        table.insert(
547          result,
548          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
549            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
550            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
551        )
552
553        -- Show one namespace
554        vim.diagnostic.show(_G.diagnostic_ns)
555        table.insert(
556          result,
557          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
558            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
559            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
560        )
561
562        -- Hide one buffer
563        vim.diagnostic.hide(nil, other_bufnr)
564        table.insert(
565          result,
566          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
567            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
568            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
569        )
570
571        -- Hide everything
572        vim.diagnostic.hide()
573        table.insert(
574          result,
575          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
576            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
577            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
578        )
579
580        -- Show one buffer
581        vim.diagnostic.show(nil, _G.diagnostic_bufnr)
582        table.insert(
583          result,
584          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
585            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
586            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
587        )
588
589        return result
590      end)
591
592      eq(4, result[1])
593      eq(1, result[2])
594      eq(4, result[3])
595      eq(3, result[4])
596      eq(0, result[5])
597      eq(3, result[6])
598    end)
599
600    it("doesn't error after bwipeout on buffer", function()
601      exec_lua(function()
602        vim.diagnostic.set(
603          _G.diagnostic_ns,
604          _G.diagnostic_bufnr,
605          { { message = '', lnum = 0, end_lnum = 0, col = 0, end_col = 0 } }
606        )
607        vim.cmd('bwipeout! ' .. _G.diagnostic_bufnr)
608
609        vim.diagnostic.show(_G.diagnostic_ns)
610        vim.diagnostic.hide(_G.diagnostic_ns)
611      end)
612    end)
613
614    it('handles diagnostics without extmark_id', function()
615      exec_lua(function()
616        vim.diagnostic.config({ virtual_text = true })
617
618        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
619          _G.make_error('Error message', 0, 0, 0, 5),
620        })
621
622        local diags = vim.diagnostic.get(_G.diagnostic_bufnr, { namespace = _G.diagnostic_ns })
623        diags[1]._extmark_id = nil
624
625        vim.diagnostic.show(_G.diagnostic_ns, _G.diagnostic_bufnr, diags)
626      end)
627    end)
628  end)
629
630  describe('enable() and disable()', function()
631    it('validation', function()
632      matches('expected boolean, got table', pcall_err(exec_lua, [[vim.diagnostic.enable({})]]))
633      matches(
634        'filter: expected table, got string',
635        pcall_err(exec_lua, [[vim.diagnostic.enable(false, '')]])
636      )
637      matches(
638        'Invalid buffer id: 42',
639        pcall_err(exec_lua, [[vim.diagnostic.enable(true, { bufnr = 42 })]])
640      )
641      matches(
642        'expected boolean, got number',
643        pcall_err(exec_lua, [[vim.diagnostic.enable(42, {})]])
644      )
645      matches('expected boolean, got table', pcall_err(exec_lua, [[vim.diagnostic.enable({}, 42)]]))
646    end)
647
648    it('without arguments', function()
649      local result = exec_lua(function()
650        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
651
652        local result = {}
653
654        vim.diagnostic.config({ underline = false, virtual_text = true })
655
656        local ns_1_diags = {
657          _G.make_error('Error 1', 1, 1, 1, 5),
658          _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
659        }
660        local ns_2_diags = {
661          _G.make_warning('Warning 1', 2, 1, 2, 5),
662        }
663
664        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
665        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
666
667        table.insert(
668          result,
669          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
670            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
671        )
672
673        vim.diagnostic.enable(false)
674
675        table.insert(
676          result,
677          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
678            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
679        )
680
681        -- Create a new buffer
682        local other_bufnr = vim.api.nvim_create_buf(true, false)
683        local other_buffer_diags = {
684          _G.make_info('This is interesting', 0, 0, 0, 0),
685        }
686
687        vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
688
689        table.insert(
690          result,
691          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
692            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
693            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
694        )
695
696        vim.diagnostic.enable()
697
698        table.insert(
699          result,
700          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
701            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
702            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
703        )
704
705        return result
706      end)
707
708      eq(3, result[1])
709      eq(0, result[2])
710      eq(0, result[3])
711      eq(4, result[4])
712    end)
713
714    it('with buffer argument', function()
715      local result = exec_lua(function()
716        local other_bufnr = vim.api.nvim_create_buf(true, false)
717
718        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
719
720        local result = {}
721
722        vim.diagnostic.config({ underline = false, virtual_text = true })
723
724        local ns_1_diags = {
725          _G.make_error('Error 1', 1, 1, 1, 5),
726          _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
727        }
728        local ns_2_diags = {
729          _G.make_warning('Warning 1', 2, 1, 2, 5),
730        }
731        local other_buffer_diags = {
732          _G.make_info('This is interesting', 0, 0, 0, 0),
733        }
734
735        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
736        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
737        vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
738
739        table.insert(
740          result,
741          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
742            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
743            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
744        )
745
746        vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr })
747
748        table.insert(
749          result,
750          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
751            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
752            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
753        )
754
755        vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr })
756
757        table.insert(
758          result,
759          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
760            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
761            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
762        )
763
764        vim.diagnostic.enable(false, { bufnr = other_bufnr })
765
766        table.insert(
767          result,
768          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
769            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
770            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
771        )
772
773        return result
774      end)
775
776      eq(4, result[1])
777      eq(1, result[2])
778      eq(4, result[3])
779      eq(3, result[4])
780    end)
781
782    it('with a namespace argument', function()
783      local result = exec_lua(function()
784        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
785
786        local result = {}
787
788        vim.diagnostic.config({ underline = false, virtual_text = true })
789
790        local ns_1_diags = {
791          _G.make_error('Error 1', 1, 1, 1, 5),
792          _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
793        }
794        local ns_2_diags = {
795          _G.make_warning('Warning 1', 2, 1, 2, 5),
796        }
797
798        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
799        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
800
801        table.insert(
802          result,
803          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
804            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
805        )
806
807        vim.diagnostic.enable(false, { ns_id = _G.diagnostic_ns })
808
809        table.insert(
810          result,
811          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
812            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
813        )
814
815        vim.diagnostic.enable(true, { ns_id = _G.diagnostic_ns })
816
817        table.insert(
818          result,
819          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
820            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
821        )
822
823        vim.diagnostic.enable(false, { ns_id = _G.other_ns })
824
825        table.insert(
826          result,
827          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
828            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
829        )
830
831        return result
832      end)
833
834      eq(3, result[1])
835      eq(1, result[2])
836      eq(3, result[3])
837      eq(2, result[4])
838    end)
839
840    --- @return table
841    local function test_enable()
842      return exec_lua(function()
843        local other_bufnr = vim.api.nvim_create_buf(true, false)
844
845        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
846
847        local result = {}
848
849        vim.diagnostic.config({ underline = false, virtual_text = true })
850
851        local ns_1_diags = {
852          _G.make_error('Error 1', 1, 1, 1, 5),
853          _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
854        }
855        local ns_2_diags = {
856          _G.make_warning('Warning 1', 2, 1, 2, 5),
857        }
858        local other_buffer_diags = {
859          _G.make_info('This is interesting', 0, 0, 0, 0),
860        }
861
862        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
863        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
864        vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
865
866        table.insert(
867          result,
868          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
869            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
870            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
871        )
872
873        vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
874
875        table.insert(
876          result,
877          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
878            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
879            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
880        )
881
882        vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.other_ns })
883
884        table.insert(
885          result,
886          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
887            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
888            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
889        )
890
891        vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
892
893        table.insert(
894          result,
895          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
896            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
897            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
898        )
899
900        -- Should have no effect
901        vim.diagnostic.enable(false, { bufnr = other_bufnr, ns_id = _G.other_ns })
902
903        table.insert(
904          result,
905          _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
906            + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
907            + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
908        )
909
910        return result
911      end)
912    end
913
914    it('with both buffer and namespace arguments', function()
915      local result = test_enable()
916      eq(4, result[1])
917      eq(2, result[2])
918      eq(1, result[3])
919      eq(3, result[4])
920      eq(3, result[5])
921    end)
922  end)
923
924  describe('reset()', function()
925    it('diagnostic count is 0 and displayed diagnostics are 0 after call', function()
926      exec_lua(function()
927        vim.diagnostic.config({ virtual_text = true })
928      end)
929
930      -- 1 Error (1)
931      -- 1 Warning (2)
932      -- 1 Warning (2) + 1 Warning (1)
933      -- 2 highlights and 2 underlines (since error)
934      -- 1 highlight + 1 underline
935      local all_highlights = { 1, 1, 2, 4, 2 }
936      eq(
937        all_highlights,
938        exec_lua(function()
939          local ns_1_diags = {
940            _G.make_error('Error 1', 1, 1, 1, 5),
941            _G.make_warning('Warning on Server 1', 2, 1, 2, 3),
942          }
943          local ns_2_diags = {
944            _G.make_warning('Warning 1', 2, 1, 2, 3),
945          }
946
947          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
948          vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
949
950          return {
951            _G.count_diagnostics(
952              _G.diagnostic_bufnr,
953              vim.diagnostic.severity.ERROR,
954              _G.diagnostic_ns
955            ),
956            _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
957            _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
958            _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
959            _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
960          }
961        end)
962      )
963
964      -- Reset diagnostics from namespace 1
965      exec_lua([[ vim.diagnostic.reset( _G.diagnostic_ns) ]])
966
967      -- Make sure we have the right diagnostic count
968      eq(
969        { 0, 1, 1, 0, 2 },
970        exec_lua(function()
971          local diagnostic_count = {}
972          vim.wait(100, function()
973            diagnostic_count = {
974              _G.count_diagnostics(
975                _G.diagnostic_bufnr,
976                vim.diagnostic.severity.ERROR,
977                _G.diagnostic_ns
978              ),
979              _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
980              _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
981              _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
982              _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
983            }
984          end)
985          return diagnostic_count
986        end)
987      )
988
989      -- Reset diagnostics from namespace 2
990      exec_lua([[ vim.diagnostic.reset(_G.other_ns) ]])
991
992      -- Make sure we have the right diagnostic count
993      eq(
994        { 0, 0, 0, 0, 0 },
995        exec_lua(function()
996          local diagnostic_count = {}
997          vim.wait(100, function()
998            diagnostic_count = {
999              _G.count_diagnostics(
1000                _G.diagnostic_bufnr,
1001                vim.diagnostic.severity.ERROR,
1002                _G.diagnostic_ns
1003              ),
1004              _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
1005              _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
1006              _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
1007              _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
1008            }
1009          end)
1010          return diagnostic_count
1011        end)
1012      )
1013    end)
1014
1015    it("doesn't error after bwipeout called on buffer", function()
1016      exec_lua(function()
1017        vim.diagnostic.set(
1018          _G.diagnostic_ns,
1019          _G.diagnostic_bufnr,
1020          { { message = '', lnum = 0, end_lnum = 0, col = 0, end_col = 0 } }
1021        )
1022        vim.cmd('bwipeout! ' .. _G.diagnostic_bufnr)
1023
1024        vim.diagnostic.reset(_G.diagnostic_ns)
1025      end)
1026    end)
1027  end)
1028
1029  describe('get_next()', function()
1030    it('can find the next pos with only one namespace', function()
1031      eq(
1032        { 1, 1 },
1033        exec_lua(function()
1034          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1035            _G.make_error('Diagnostic #1', 1, 1, 1, 1),
1036          })
1037          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1038          local next = vim.diagnostic.get_next()
1039          return { next.lnum, next.col }
1040        end)
1041      )
1042    end)
1043
1044    it('can find next pos with two errors', function()
1045      eq(
1046        { 4, 4 },
1047        exec_lua(function()
1048          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1049            _G.make_error('Diagnostic #1', 1, 1, 1, 1),
1050            _G.make_error('Diagnostic #2', 4, 4, 4, 4),
1051          })
1052          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1053          vim.api.nvim_win_set_cursor(0, { 3, 1 })
1054          local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
1055          return { next.lnum, next.col }
1056        end)
1057      )
1058    end)
1059
1060    it('can cycle when position is past error', function()
1061      eq(
1062        { 1, 1 },
1063        exec_lua(function()
1064          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1065            _G.make_error('Diagnostic #1', 1, 1, 1, 1),
1066          })
1067          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1068          vim.api.nvim_win_set_cursor(0, { 3, 1 })
1069          local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
1070          return { next.lnum, next.col }
1071        end)
1072      )
1073    end)
1074
1075    it('will not cycle when wrap is off', function()
1076      eq(
1077        nil,
1078        exec_lua(function()
1079          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1080            _G.make_error('Diagnostic #1', 1, 1, 1, 1),
1081          })
1082          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1083          vim.api.nvim_win_set_cursor(0, { 3, 1 })
1084          local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns, wrap = false })
1085          return next
1086        end)
1087      )
1088    end)
1089
1090    it('can cycle even from the last line', function()
1091      eq(
1092        { 4, 4 },
1093        exec_lua(function()
1094          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1095            _G.make_error('Diagnostic #2', 4, 4, 4, 4),
1096          })
1097          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1098          vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(0), 1 })
1099          local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns })
1100          return { prev.lnum, prev.col }
1101        end)
1102      )
1103    end)
1104
1105    it('works with diagnostics past the end of the line #16349', function()
1106      eq(
1107        { 4, 0 },
1108        exec_lua(function()
1109          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1110            _G.make_error('Diagnostic #1', 3, 9001, 3, 9001),
1111            _G.make_error('Diagnostic #2', 4, 0, 4, 0),
1112          })
1113          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1114          vim.api.nvim_win_set_cursor(0, { 1, 1 })
1115          vim.diagnostic.jump({ count = 1 })
1116          local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
1117          return { next.lnum, next.col }
1118        end)
1119      )
1120    end)
1121
1122    it('works with diagnostics before the start of the line', function()
1123      eq(
1124        { 4, 0 },
1125        exec_lua(function()
1126          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1127            _G.make_error('Diagnostic #1', 3, 9001, 3, 9001),
1128            _G.make_error('Diagnostic #2', 4, -1, 4, -1),
1129          })
1130          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1131          vim.api.nvim_win_set_cursor(0, { 1, 1 })
1132          vim.diagnostic.jump({ count = 1 })
1133          local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
1134          return { next.lnum, next.col }
1135        end)
1136      )
1137    end)
1138
1139    it('jumps to diagnostic with highest severity', function()
1140      exec_lua(function()
1141        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1142          _G.make_info('Info', 1, 0, 1, 1),
1143          _G.make_error('Error', 2, 0, 2, 1),
1144          _G.make_warning('Warning', 3, 0, 3, 1),
1145          _G.make_error('Error', 4, 0, 4, 1),
1146        })
1147
1148        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1149        vim.api.nvim_win_set_cursor(0, { 1, 0 })
1150      end)
1151
1152      eq(
1153        { 3, 0 },
1154        exec_lua(function()
1155          vim.diagnostic.jump({ count = 1, _highest = true })
1156          return vim.api.nvim_win_get_cursor(0)
1157        end)
1158      )
1159
1160      eq(
1161        { 5, 0 },
1162        exec_lua(function()
1163          vim.diagnostic.jump({ count = 1, _highest = true })
1164          return vim.api.nvim_win_get_cursor(0)
1165        end)
1166      )
1167
1168      exec_lua(function()
1169        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1170          _G.make_info('Info', 1, 0, 1, 1),
1171          _G.make_hint('Hint', 2, 0, 2, 1),
1172          _G.make_warning('Warning', 3, 0, 3, 1),
1173          _G.make_hint('Hint', 4, 0, 4, 1),
1174          _G.make_warning('Warning', 5, 0, 5, 1),
1175        })
1176
1177        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1178        vim.api.nvim_win_set_cursor(0, { 1, 0 })
1179      end)
1180
1181      eq(
1182        { 4, 0 },
1183        exec_lua(function()
1184          vim.diagnostic.jump({ count = 1, _highest = true })
1185          return vim.api.nvim_win_get_cursor(0)
1186        end)
1187      )
1188
1189      eq(
1190        { 6, 0 },
1191        exec_lua(function()
1192          vim.diagnostic.jump({ count = 1, _highest = true })
1193          return vim.api.nvim_win_get_cursor(0)
1194        end)
1195      )
1196    end)
1197
1198    it('jumps to next diagnostic if severity is non-nil', function()
1199      exec_lua(function()
1200        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1201          _G.make_info('Info', 1, 0, 1, 1),
1202          _G.make_error('Error', 2, 0, 2, 1),
1203          _G.make_warning('Warning', 3, 0, 3, 1),
1204          _G.make_error('Error', 4, 0, 4, 1),
1205        })
1206
1207        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1208        vim.api.nvim_win_set_cursor(0, { 1, 0 })
1209      end)
1210
1211      eq(
1212        { 2, 0 },
1213        exec_lua(function()
1214          vim.diagnostic.jump({ count = 1 })
1215          return vim.api.nvim_win_get_cursor(0)
1216        end)
1217      )
1218
1219      eq(
1220        { 3, 0 },
1221        exec_lua(function()
1222          vim.diagnostic.jump({ count = 1 })
1223          return vim.api.nvim_win_get_cursor(0)
1224        end)
1225      )
1226
1227      eq(
1228        { 4, 0 },
1229        exec_lua(function()
1230          vim.diagnostic.jump({ count = 1 })
1231          return vim.api.nvim_win_get_cursor(0)
1232        end)
1233      )
1234    end)
1235  end)
1236
1237  describe('get_prev()', function()
1238    it('can find the previous diagnostic with only one namespace', function()
1239      eq(
1240        { 1, 1 },
1241        exec_lua(function()
1242          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1243            _G.make_error('Diagnostic #1', 1, 1, 1, 1),
1244          })
1245          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1246          vim.api.nvim_win_set_cursor(0, { 3, 1 })
1247          local prev = vim.diagnostic.get_prev()
1248          return { prev.lnum, prev.col }
1249        end)
1250      )
1251    end)
1252
1253    it('can find the previous diagnostic with two errors', function()
1254      eq(
1255        { 1, 1 },
1256        exec_lua(function()
1257          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1258            _G.make_error('Diagnostic #1', 1, 1, 1, 1),
1259            _G.make_error('Diagnostic #2', 4, 4, 4, 4),
1260          })
1261          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1262          vim.api.nvim_win_set_cursor(0, { 3, 1 })
1263          local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns })
1264          return { prev.lnum, prev.col }
1265        end)
1266      )
1267    end)
1268
1269    it('can cycle when position is past error', function()
1270      eq(
1271        { 4, 4 },
1272        exec_lua(function()
1273          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1274            _G.make_error('Diagnostic #2', 4, 4, 4, 4),
1275          })
1276          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1277          vim.api.nvim_win_set_cursor(0, { 3, 1 })
1278          local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns })
1279          return { prev.lnum, prev.col }
1280        end)
1281      )
1282    end)
1283
1284    it('respects wrap parameter', function()
1285      eq(
1286        nil,
1287        exec_lua(function()
1288          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1289            _G.make_error('Diagnostic #2', 4, 4, 4, 4),
1290          })
1291          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1292          vim.api.nvim_win_set_cursor(0, { 3, 1 })
1293          local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns, wrap = false })
1294          return prev
1295        end)
1296      )
1297    end)
1298
1299    it('works on blank line #28397', function()
1300      eq(
1301        { 0, 2 },
1302        exec_lua(function()
1303          local test_bufnr = vim.api.nvim_create_buf(true, false)
1304          vim.api.nvim_buf_set_lines(test_bufnr, 0, -1, false, {
1305            'first line',
1306            '',
1307            '',
1308            'end line',
1309          })
1310          vim.diagnostic.set(_G.diagnostic_ns, test_bufnr, {
1311            _G.make_info('Diagnostic #1', 0, 2, 0, 2),
1312            _G.make_info('Diagnostic #2', 2, 0, 2, 0),
1313            _G.make_info('Diagnostic #3', 2, 0, 2, 0),
1314          })
1315          vim.api.nvim_win_set_buf(0, test_bufnr)
1316          vim.api.nvim_win_set_cursor(0, { 3, 0 })
1317          return vim.diagnostic.get_prev_pos { namespace = _G.diagnostic_ns }
1318        end)
1319      )
1320    end)
1321  end)
1322
1323  describe('jump()', function()
1324    before_each(function()
1325      exec_lua(function()
1326        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1327          _G.make_error('Diagnostic #1', 0, 0, 0, 2),
1328          _G.make_error('Diagnostic #2', 1, 1, 1, 4),
1329          _G.make_warning('Diagnostic #3', 2, -1, 2, -1),
1330          _G.make_info('Diagnostic #4', 3, 0, 3, 3),
1331        })
1332        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
1333      end)
1334    end)
1335
1336    it('can move forward', function()
1337      eq(
1338        { 2, 1 },
1339        exec_lua(function()
1340          vim.api.nvim_win_set_cursor(0, { 1, 0 })
1341          vim.diagnostic.jump({ count = 1 })
1342          return vim.api.nvim_win_get_cursor(0)
1343        end)
1344      )
1345
1346      eq(
1347        { 4, 0 },
1348        exec_lua(function()
1349          vim.api.nvim_win_set_cursor(0, { 1, 0 })
1350          vim.diagnostic.jump({ count = 3 })
1351          return vim.api.nvim_win_get_cursor(0)
1352        end)
1353      )
1354
1355      eq(
1356        { 4, 0 },
1357        exec_lua(function()
1358          vim.api.nvim_win_set_cursor(0, { 1, 0 })
1359          vim.diagnostic.jump({ count = math.huge, wrap = false })
1360          return vim.api.nvim_win_get_cursor(0)
1361        end)
1362      )
1363    end)
1364
1365    it('can move backward', function()
1366      eq(
1367        { 3, 0 },
1368        exec_lua(function()
1369          vim.api.nvim_win_set_cursor(0, { 4, 0 })
1370          vim.diagnostic.jump({ count = -1 })
1371          return vim.api.nvim_win_get_cursor(0)
1372        end)
1373      )
1374
1375      eq(
1376        { 1, 0 },
1377        exec_lua(function()
1378          vim.api.nvim_win_set_cursor(0, { 4, 0 })
1379          vim.diagnostic.jump({ count = -3 })
1380          return vim.api.nvim_win_get_cursor(0)
1381        end)
1382      )
1383
1384      eq(
1385        { 1, 0 },
1386        exec_lua(function()
1387          vim.api.nvim_win_set_cursor(0, { 4, 0 })
1388          vim.diagnostic.jump({ count = -math.huge, wrap = false })
1389          return vim.api.nvim_win_get_cursor(0)
1390        end)
1391      )
1392    end)
1393
1394    it('can filter by severity', function()
1395      eq(
1396        { 3, 0 },
1397        exec_lua(function()
1398          vim.api.nvim_win_set_cursor(0, { 1, 0 })
1399          vim.diagnostic.jump({ count = 1, severity = vim.diagnostic.severity.WARN })
1400          return vim.api.nvim_win_get_cursor(0)
1401        end)
1402      )
1403
1404      eq(
1405        { 3, 0 },
1406        exec_lua(function()
1407          vim.api.nvim_win_set_cursor(0, { 1, 0 })
1408          vim.diagnostic.jump({ count = 9999, severity = vim.diagnostic.severity.WARN })
1409          return vim.api.nvim_win_get_cursor(0)
1410        end)
1411      )
1412    end)
1413
1414    it('can wrap', function()
1415      eq(
1416        { 1, 0 },
1417        exec_lua(function()
1418          vim.api.nvim_win_set_cursor(0, { 4, 0 })
1419          vim.diagnostic.jump({ count = 1, wrap = true })
1420          return vim.api.nvim_win_get_cursor(0)
1421        end)
1422      )
1423
1424      eq(
1425        { 4, 0 },
1426        exec_lua(function()
1427          vim.api.nvim_win_set_cursor(0, { 1, 0 })
1428          vim.diagnostic.jump({ count = -1, wrap = true })
1429          return vim.api.nvim_win_get_cursor(0)
1430        end)
1431      )
1432    end)
1433
1434    it('supports on_jump() handler', function()
1435      exec_lua(function()
1436        _G.jumped = false
1437
1438        vim.diagnostic.jump({
1439          count = 1,
1440          on_jump = function()
1441            _G.jumped = true
1442          end,
1443        })
1444      end)
1445
1446      retry(nil, nil, function()
1447        eq(true, exec_lua('return _G.jumped'))
1448      end)
1449    end)
1450
1451    describe('after inserting text before diagnostic position', function()
1452      before_each(function()
1453        api.nvim_buf_set_text(0, 3, 0, 3, 0, { 'new line', 'new ' })
1454      end)
1455
1456      it('finds next diagnostic at a logical location', function()
1457        eq(
1458          { 5, 4 },
1459          exec_lua(function()
1460            vim.api.nvim_win_set_cursor(0, { 3, 1 })
1461            vim.diagnostic.jump({ count = 1 })
1462            return vim.api.nvim_win_get_cursor(0)
1463          end)
1464        )
1465      end)
1466
1467      it('finds previous diagnostic at a logical location', function()
1468        eq(
1469          { 5, 4 },
1470          exec_lua(function()
1471            vim.api.nvim_win_set_cursor(0, { 6, 4 })
1472            vim.diagnostic.jump({ count = -1 })
1473            return vim.api.nvim_win_get_cursor(0)
1474          end)
1475        )
1476      end)
1477    end)
1478
1479    describe('if diagnostic is set after last character in line', function()
1480      before_each(function()
1481        exec_lua(function()
1482          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1483            _G.make_error('Diagnostic #1', 2, 3, 3, 4),
1484          })
1485        end)
1486      end)
1487
1488      it('finds next diagnostic at the end of the line', function()
1489        eq(
1490          { 3, 2 },
1491          exec_lua(function()
1492            vim.api.nvim_win_set_cursor(0, { 3, 0 })
1493            vim.diagnostic.jump({ count = 1 })
1494            return vim.api.nvim_win_get_cursor(0)
1495          end)
1496        )
1497      end)
1498
1499      it('finds previous diagnostic at the end of the line', function()
1500        eq(
1501          { 3, 2 },
1502          exec_lua(function()
1503            vim.api.nvim_win_set_cursor(0, { 4, 2 })
1504            vim.diagnostic.jump({ count = -1 })
1505            return vim.api.nvim_win_get_cursor(0)
1506          end)
1507        )
1508      end)
1509    end)
1510
1511    describe('after entire text range with a diagnostic was deleted', function()
1512      before_each(function()
1513        api.nvim_buf_set_text(0, 1, 1, 1, 4, {})
1514      end)
1515
1516      it('does not find next diagnostic inside the deleted range', function()
1517        eq(
1518          { 3, 0 },
1519          exec_lua(function()
1520            vim.api.nvim_win_set_cursor(0, { 1, 0 })
1521            vim.diagnostic.jump({ count = 1 })
1522            return vim.api.nvim_win_get_cursor(0)
1523          end)
1524        )
1525      end)
1526
1527      it('does not find previous diagnostic inside the deleted range', function()
1528        eq(
1529          { 1, 0 },
1530          exec_lua(function()
1531            vim.api.nvim_win_set_cursor(0, { 3, 0 })
1532            vim.diagnostic.jump({ count = -1 })
1533            return vim.api.nvim_win_get_cursor(0)
1534          end)
1535        )
1536      end)
1537    end)
1538  end)
1539
1540  describe('get()', function()
1541    it('returns an empty table when no diagnostics are present', function()
1542      eq(
1543        {},
1544        exec_lua [[return vim.diagnostic.get( _G.diagnostic_bufnr, {namespace=diagnostic_ns})]]
1545      )
1546    end)
1547
1548    it('returns all diagnostics when no severity is supplied', function()
1549      eq(
1550        2,
1551        exec_lua(function()
1552          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1553            _G.make_error('Error 1', 1, 1, 1, 5),
1554            _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
1555          })
1556
1557          return #vim.diagnostic.get(_G.diagnostic_bufnr)
1558        end)
1559      )
1560    end)
1561
1562    it('returns only requested diagnostics when severity range is supplied', function()
1563      eq(
1564        { 2, 3, 2 },
1565        exec_lua(function()
1566          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1567            _G.make_error('Error 1', 1, 1, 1, 5),
1568            _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
1569            _G.make_info('Ignored information', 1, 1, 2, 3),
1570            _G.make_hint("Here's a hint", 1, 1, 2, 3),
1571          })
1572
1573          return {
1574            #vim.diagnostic.get(
1575              _G.diagnostic_bufnr,
1576              { severity = { min = vim.diagnostic.severity.WARN } }
1577            ),
1578            #vim.diagnostic.get(
1579              _G.diagnostic_bufnr,
1580              { severity = { max = vim.diagnostic.severity.WARN } }
1581            ),
1582            #vim.diagnostic.get(_G.diagnostic_bufnr, {
1583              severity = {
1584                min = vim.diagnostic.severity.INFO,
1585                max = vim.diagnostic.severity.WARN,
1586              },
1587            }),
1588          }
1589        end)
1590      )
1591    end)
1592
1593    it('returns only requested diagnostics when severities are supplied', function()
1594      eq(
1595        { 1, 1, 2 },
1596        exec_lua(function()
1597          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1598            _G.make_error('Error 1', 1, 1, 1, 5),
1599            _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
1600            _G.make_info('Ignored information', 1, 1, 2, 3),
1601            _G.make_hint("Here's a hint", 1, 1, 2, 3),
1602          })
1603
1604          return {
1605            #vim.diagnostic.get(
1606              _G.diagnostic_bufnr,
1607              { severity = { vim.diagnostic.severity.WARN } }
1608            ),
1609            #vim.diagnostic.get(
1610              _G.diagnostic_bufnr,
1611              { severity = { vim.diagnostic.severity.ERROR } }
1612            ),
1613            #vim.diagnostic.get(_G.diagnostic_bufnr, {
1614              severity = {
1615                vim.diagnostic.severity.INFO,
1616                vim.diagnostic.severity.WARN,
1617              },
1618            }),
1619          }
1620        end)
1621      )
1622    end)
1623
1624    it('allows filtering by line', function()
1625      eq(
1626        2,
1627        exec_lua(function()
1628          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1629            _G.make_error('Error 1', 1, 1, 1, 5),
1630            _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
1631            _G.make_info('Ignored information', 1, 1, 2, 3),
1632            _G.make_error('Error On Other Line', 3, 1, 3, 5),
1633          })
1634
1635          return #vim.diagnostic.get(_G.diagnostic_bufnr, { lnum = 2 })
1636        end)
1637      )
1638    end)
1639
1640    it('allows filtering by enablement', function()
1641      eq(
1642        { 3, 1, 2 },
1643        exec_lua(function()
1644          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1645            _G.make_error('Error 1', 1, 1, 1, 5),
1646          })
1647          vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
1648            _G.make_error('Error 2', 1, 1, 1, 5),
1649            _G.make_error('Error 3', 3, 1, 3, 5),
1650          })
1651
1652          vim.diagnostic.enable(false, { ns_id = _G.other_ns })
1653
1654          return {
1655            #vim.diagnostic.get(_G.diagnostic_bufnr),
1656            #vim.diagnostic.get(_G.diagnostic_bufnr, { enabled = true }),
1657            #vim.diagnostic.get(_G.diagnostic_bufnr, { enabled = false }),
1658          }
1659        end)
1660      )
1661    end)
1662  end)
1663
1664  describe('count', function()
1665    it('returns actually present severity counts', function()
1666      eq(
1667        exec_lua(function()
1668          return {
1669            [vim.diagnostic.severity.ERROR] = 4,
1670            [vim.diagnostic.severity.WARN] = 3,
1671            [vim.diagnostic.severity.INFO] = 2,
1672            [vim.diagnostic.severity.HINT] = 1,
1673          }
1674        end),
1675        exec_lua(function()
1676          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1677            _G.make_error('Error 1', 1, 1, 1, 2),
1678            _G.make_error('Error 2', 1, 3, 1, 4),
1679            _G.make_error('Error 3', 1, 5, 1, 6),
1680            _G.make_error('Error 4', 1, 7, 1, 8),
1681            _G.make_warning('Warning 1', 2, 1, 2, 2),
1682            _G.make_warning('Warning 2', 2, 3, 2, 4),
1683            _G.make_warning('Warning 3', 2, 5, 2, 6),
1684            _G.make_info('Info 1', 3, 1, 3, 2),
1685            _G.make_info('Info 2', 3, 3, 3, 4),
1686            _G.make_hint('Hint 1', 4, 1, 4, 2),
1687          })
1688          return vim.diagnostic.count(_G.diagnostic_bufnr)
1689        end)
1690      )
1691      eq(
1692        exec_lua(function()
1693          return {
1694            [vim.diagnostic.severity.ERROR] = 2,
1695            [vim.diagnostic.severity.INFO] = 1,
1696          }
1697        end),
1698        exec_lua(function()
1699          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1700            _G.make_error('Error 1', 1, 1, 1, 2),
1701            _G.make_error('Error 2', 1, 3, 1, 4),
1702            _G.make_info('Info 1', 3, 1, 3, 2),
1703          })
1704          return vim.diagnostic.count(_G.diagnostic_bufnr)
1705        end)
1706      )
1707    end)
1708
1709    it('returns only requested diagnostics count when severity range is supplied', function()
1710      eq(
1711        exec_lua(function()
1712          return {
1713            { [vim.diagnostic.severity.ERROR] = 1, [vim.diagnostic.severity.WARN] = 1 },
1714            {
1715              [vim.diagnostic.severity.WARN] = 1,
1716              [vim.diagnostic.severity.INFO] = 1,
1717              [vim.diagnostic.severity.HINT] = 1,
1718            },
1719            { [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1 },
1720          }
1721        end),
1722        exec_lua(function()
1723          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1724            _G.make_error('Error 1', 1, 1, 1, 5),
1725            _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
1726            _G.make_info('Ignored information', 1, 1, 2, 3),
1727            _G.make_hint("Here's a hint", 1, 1, 2, 3),
1728          })
1729
1730          return {
1731            vim.diagnostic.count(
1732              _G.diagnostic_bufnr,
1733              { severity = { min = vim.diagnostic.severity.WARN } }
1734            ),
1735            vim.diagnostic.count(
1736              _G.diagnostic_bufnr,
1737              { severity = { max = vim.diagnostic.severity.WARN } }
1738            ),
1739            vim.diagnostic.count(_G.diagnostic_bufnr, {
1740              severity = {
1741                min = vim.diagnostic.severity.INFO,
1742                max = vim.diagnostic.severity.WARN,
1743              },
1744            }),
1745          }
1746        end)
1747      )
1748    end)
1749
1750    it('returns only requested diagnostics when severities are supplied', function()
1751      eq(
1752        exec_lua(function()
1753          return {
1754            { [vim.diagnostic.severity.WARN] = 1 },
1755            { [vim.diagnostic.severity.ERROR] = 1 },
1756            { [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1 },
1757          }
1758        end),
1759        exec_lua(function()
1760          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1761            _G.make_error('Error 1', 1, 1, 1, 5),
1762            _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
1763            _G.make_info('Ignored information', 1, 1, 2, 3),
1764            _G.make_hint("Here's a hint", 1, 1, 2, 3),
1765          })
1766
1767          return {
1768            vim.diagnostic.count(
1769              _G.diagnostic_bufnr,
1770              { severity = { vim.diagnostic.severity.WARN } }
1771            ),
1772            vim.diagnostic.count(
1773              _G.diagnostic_bufnr,
1774              { severity = { vim.diagnostic.severity.ERROR } }
1775            ),
1776            vim.diagnostic.count(_G.diagnostic_bufnr, {
1777              severity = {
1778                vim.diagnostic.severity.INFO,
1779                vim.diagnostic.severity.WARN,
1780              },
1781            }),
1782          }
1783        end)
1784      )
1785    end)
1786
1787    it('allows filtering by line', function()
1788      eq(
1789        exec_lua(function()
1790          return {
1791            [vim.diagnostic.severity.WARN] = 1,
1792            [vim.diagnostic.severity.INFO] = 1,
1793          }
1794        end),
1795        exec_lua(function()
1796          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1797            _G.make_error('Error 1', 1, 1, 1, 5),
1798            _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
1799            _G.make_info('Ignored information', 1, 1, 2, 3),
1800            _G.make_error('Error On Other Line', 3, 1, 3, 5),
1801          })
1802
1803          return vim.diagnostic.count(_G.diagnostic_bufnr, { lnum = 2 })
1804        end)
1805      )
1806    end)
1807  end)
1808
1809  describe('config()', function()
1810    it('works with global, namespace, and ephemeral options', function()
1811      eq(
1812        1,
1813        exec_lua(function()
1814          vim.diagnostic.config({
1815            virtual_text = false,
1816          })
1817
1818          vim.diagnostic.config({
1819            virtual_text = true,
1820            underline = false,
1821          }, _G.diagnostic_ns)
1822
1823          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1824            _G.make_error('Some Error', 4, 4, 4, 4),
1825          })
1826
1827          return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
1828        end)
1829      )
1830
1831      eq(
1832        1,
1833        exec_lua(function()
1834          vim.diagnostic.config({
1835            virtual_text = false,
1836          })
1837
1838          vim.diagnostic.config({
1839            virtual_text = false,
1840            underline = false,
1841          }, _G.diagnostic_ns)
1842
1843          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1844            _G.make_error('Some Error', 4, 4, 4, 4),
1845          }, { virtual_text = true })
1846
1847          return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
1848        end)
1849      )
1850
1851      eq(
1852        0,
1853        exec_lua(function()
1854          vim.diagnostic.config({
1855            virtual_text = false,
1856          })
1857
1858          vim.diagnostic.config({
1859            virtual_text = { severity = vim.diagnostic.severity.ERROR },
1860            underline = false,
1861          }, _G.diagnostic_ns)
1862
1863          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1864            _G.make_warning('Some Warning', 4, 4, 4, 4),
1865          }, { virtual_text = true })
1866
1867          return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
1868        end)
1869      )
1870
1871      eq(
1872        1,
1873        exec_lua(function()
1874          vim.diagnostic.config({
1875            virtual_text = false,
1876          })
1877
1878          vim.diagnostic.config({
1879            virtual_text = { severity = vim.diagnostic.severity.ERROR },
1880            underline = false,
1881          }, _G.diagnostic_ns)
1882
1883          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1884            _G.make_warning('Some Warning', 4, 4, 4, 4),
1885          }, {
1886            virtual_text = {}, -- An empty table uses default values
1887          })
1888
1889          return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
1890        end)
1891      )
1892    end)
1893
1894    it('can use functions for config values', function()
1895      exec_lua(function()
1896        vim.diagnostic.config({
1897          virtual_text = function()
1898            return true
1899          end,
1900        }, _G.diagnostic_ns)
1901        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1902          _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
1903        })
1904      end)
1905
1906      eq(
1907        1,
1908        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
1909      )
1910      eq(2, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
1911
1912      -- Now, don't enable virtual text.
1913      -- We should have one less extmark displayed.
1914      exec_lua(function()
1915        vim.diagnostic.config({
1916          virtual_text = function()
1917            return false
1918          end,
1919        }, _G.diagnostic_ns)
1920      end)
1921
1922      eq(
1923        1,
1924        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
1925      )
1926      eq(1, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
1927    end)
1928
1929    it('allows filtering by severity', function()
1930      local get_extmark_count_with_severity = function(min_severity)
1931        return exec_lua(function()
1932          vim.diagnostic.config({
1933            underline = false,
1934            virtual_text = {
1935              severity = { min = min_severity },
1936            },
1937          })
1938
1939          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1940            _G.make_warning('Delayed Diagnostic', 4, 4, 4, 4),
1941          })
1942
1943          return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
1944        end)
1945      end
1946
1947      -- No messages with Error or higher
1948      eq(0, get_extmark_count_with_severity('ERROR'))
1949
1950      -- But now we don't filter it
1951      eq(1, get_extmark_count_with_severity('WARN'))
1952      eq(1, get_extmark_count_with_severity('HINT'))
1953    end)
1954
1955    it('allows sorting by severity', function()
1956      exec_lua(function()
1957        vim.diagnostic.config({
1958          underline = false,
1959          signs = true,
1960          virtual_text = true,
1961        })
1962
1963        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
1964          _G.make_warning('Warning', 4, 4, 4, 4),
1965          _G.make_error('Error', 4, 4, 4, 4),
1966          _G.make_info('Info', 4, 4, 4, 4),
1967        })
1968
1969        function _G.get_highest_underline_hl(severity_sort)
1970          vim.diagnostic.config({
1971            underline = true,
1972            severity_sort = severity_sort,
1973          })
1974
1975          local extmarks = _G.get_underline_extmarks(_G.diagnostic_ns)
1976
1977          table.sort(extmarks, function(a, b)
1978            return a[4].priority > b[4].priority
1979          end)
1980
1981          return extmarks[1][4].hl_group
1982        end
1983
1984        function _G.get_virt_text_and_signs(severity_sort)
1985          vim.diagnostic.config({
1986            severity_sort = severity_sort,
1987          })
1988
1989          local virt_text = _G.get_virt_text_extmarks(_G.diagnostic_ns)[1][4].virt_text
1990
1991          local virt_texts = {}
1992          for i = 2, #virt_text - 1 do
1993            table.insert(virt_texts, (string.gsub(virt_text[i][2], 'DiagnosticVirtualText', '')))
1994          end
1995
1996          local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
1997          local sign_ns = ns.user_data.sign_ns
1998          local signs = {}
1999          local all_signs = vim.api.nvim_buf_get_extmarks(
2000            _G.diagnostic_bufnr,
2001            sign_ns,
2002            0,
2003            -1,
2004            { type = 'sign', details = true }
2005          )
2006          table.sort(all_signs, function(a, b)
2007            return a[1] > b[1]
2008          end)
2009
2010          for _, v in ipairs(all_signs) do
2011            local s = v[4].sign_hl_group:gsub('DiagnosticSign', '')
2012            if not vim.tbl_contains(signs, s) then
2013              signs[#signs + 1] = s
2014            end
2015          end
2016
2017          return { virt_texts, signs }
2018        end
2019      end)
2020
2021      local result = exec_lua [[return _G.get_virt_text_and_signs(false)]]
2022
2023      -- Virt texts are defined lowest priority to highest, signs from
2024      -- highest to lowest
2025      eq({ 'Warn', 'Error', 'Info' }, result[1])
2026      eq({ 'Info', 'Error', 'Warn' }, result[2])
2027
2028      result = exec_lua [[return _G.get_virt_text_and_signs(true)]]
2029      eq({ 'Info', 'Warn', 'Error' }, result[1])
2030      eq({ 'Error', 'Warn', 'Info' }, result[2])
2031
2032      result = exec_lua [[return _G.get_virt_text_and_signs({ reverse = true })]]
2033      eq({ 'Error', 'Warn', 'Info' }, result[1])
2034      eq({ 'Info', 'Warn', 'Error' }, result[2])
2035
2036      local underline_hl = exec_lua [[return _G.get_highest_underline_hl(true)]]
2037      eq('DiagnosticUnderlineError', underline_hl)
2038
2039      underline_hl = exec_lua [[return _G.get_highest_underline_hl({ reverse = true })]]
2040      eq('DiagnosticUnderlineInfo', underline_hl)
2041    end)
2042
2043    it(
2044      'shows deprecated and unnecessary highlights in addition to severity-based highlights',
2045      function()
2046        local screen = Screen.new(50, 3)
2047        screen:set_default_attr_ids({
2048          --- DiagnosticUnderlineError + DiagnosticUnnecessary + DiagnosticDeprecated combined
2049          [1] = {
2050            background = Screen.colors.Red1,
2051            strikethrough = true,
2052            underline = true,
2053            special = Screen.colors.Red1,
2054            bold = true,
2055          },
2056        })
2057
2058        command('hi DiagnosticUnderlineError guibg=Red gui=underline guisp=Red')
2059        command('hi DiagnosticUnnecessary gui=bold')
2060        command('hi DiagnosticDeprecated gui=strikethrough')
2061
2062        exec_lua(function()
2063          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
2064          vim.diagnostic.config({
2065            signs = false,
2066          })
2067
2068          local diagnostic = _G.make_error('Some error', 0, 0, 0, 3, 'source x')
2069          diagnostic._tags = {
2070            deprecated = true,
2071            unnecessary = true,
2072          }
2073          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { diagnostic })
2074        end)
2075
2076        screen:expect([[
2077          {1:^1st} line of text                                  |
2078          2nd line of text                                  |
2079                                                            |
2080        ]])
2081      end
2082    )
2083
2084    it('can show diagnostic sources in virtual text', function()
2085      local result = exec_lua(function()
2086        local diagnostics = {
2087          _G.make_error('Some error', 0, 0, 0, 0, 'source x'),
2088        }
2089
2090        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
2091          underline = false,
2092          virtual_text = {
2093            prefix = '',
2094            source = 'always',
2095          },
2096        })
2097
2098        local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2099        local virt_text = extmarks[1][4].virt_text[3][1]
2100        return virt_text
2101      end)
2102      eq(' source x: Some error', result)
2103
2104      result = exec_lua(function()
2105        vim.diagnostic.config({
2106          underline = false,
2107          virtual_text = {
2108            prefix = '',
2109            source = 'if_many',
2110          },
2111        }, _G.diagnostic_ns)
2112
2113        local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2114        local virt_text = extmarks[1][4].virt_text[3][1]
2115        return virt_text
2116      end)
2117      eq(' Some error', result)
2118
2119      result = exec_lua(function()
2120        local diagnostics = {
2121          _G.make_error('Some error', 0, 0, 0, 0, 'source x'),
2122          _G.make_error('Another error', 1, 1, 1, 1, 'source y'),
2123        }
2124
2125        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
2126          underline = false,
2127          virtual_text = {
2128            prefix = '',
2129            source = 'if_many',
2130          },
2131        })
2132
2133        local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2134        local virt_text = { extmarks[1][4].virt_text[3][1], extmarks[2][4].virt_text[3][1] }
2135        return virt_text
2136      end)
2137      eq(' source x: Some error', result[1])
2138      eq(' source y: Another error', result[2])
2139    end)
2140
2141    it('supports a format function for diagnostic messages', function()
2142      local result = exec_lua(function()
2143        vim.diagnostic.config({
2144          underline = false,
2145          virtual_text = {
2146            prefix = '',
2147            format = function(diagnostic)
2148              if diagnostic.severity == vim.diagnostic.severity.ERROR then
2149                return string.format('🔥 %s', diagnostic.message)
2150              end
2151              return string.format('👀 %s', diagnostic.message)
2152            end,
2153          },
2154        })
2155
2156        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2157          _G.make_warning('Warning', 0, 0, 0, 0),
2158          _G.make_error('Error', 1, 0, 1, 0),
2159        })
2160
2161        local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2162        return { extmarks[1][4].virt_text, extmarks[2][4].virt_text }
2163      end)
2164      eq(' 👀 Warning', result[1][3][1])
2165      eq(' 🔥 Error', result[2][3][1])
2166    end)
2167
2168    it('includes source for formatted diagnostics', function()
2169      local result = exec_lua(function()
2170        vim.diagnostic.config({
2171          underline = false,
2172          virtual_text = {
2173            prefix = '',
2174            source = 'always',
2175            format = function(diagnostic)
2176              if diagnostic.severity == vim.diagnostic.severity.ERROR then
2177                return string.format('🔥 %s', diagnostic.message)
2178              end
2179              return string.format('👀 %s', diagnostic.message)
2180            end,
2181          },
2182        })
2183
2184        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2185          _G.make_warning('Warning', 0, 0, 0, 0, 'some_linter'),
2186          _G.make_error('Error', 1, 0, 1, 0, 'another_linter'),
2187        })
2188
2189        local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2190        return { extmarks[1][4].virt_text, extmarks[2][4].virt_text }
2191      end)
2192      eq(' some_linter: 👀 Warning', result[1][3][1])
2193      eq(' another_linter: 🔥 Error', result[2][3][1])
2194    end)
2195
2196    it('can add a prefix to virtual text', function()
2197      eq(
2198        'E Some error',
2199        exec_lua(function()
2200          local diagnostics = {
2201            _G.make_error('Some error', 0, 0, 0, 0),
2202          }
2203
2204          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
2205            underline = false,
2206            virtual_text = {
2207              prefix = 'E',
2208              suffix = '',
2209            },
2210          })
2211
2212          local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2213          local prefix = extmarks[1][4].virt_text[2][1]
2214          local message = extmarks[1][4].virt_text[3][1]
2215          return prefix .. message
2216        end)
2217      )
2218
2219      eq(
2220        '[(1/1) err-code] Some error',
2221        exec_lua(function()
2222          local diagnostics = {
2223            _G.make_error('Some error', 0, 0, 0, 0, nil, 'err-code'),
2224          }
2225
2226          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
2227            underline = false,
2228            virtual_text = {
2229              prefix = function(diag, i, total)
2230                return string.format('[(%d/%d) %s]', i, total, diag.code)
2231              end,
2232              suffix = '',
2233            },
2234          })
2235
2236          local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2237          local prefix = extmarks[1][4].virt_text[2][1]
2238          local message = extmarks[1][4].virt_text[3][1]
2239          return prefix .. message
2240        end)
2241      )
2242    end)
2243
2244    it('can add a suffix to virtual text', function()
2245      eq(
2246        ' Some error ✘',
2247        exec_lua(function()
2248          local diagnostics = {
2249            _G.make_error('Some error', 0, 0, 0, 0),
2250          }
2251
2252          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
2253            underline = false,
2254            virtual_text = {
2255              prefix = '',
2256              suffix = ' ✘',
2257            },
2258          })
2259
2260          local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2261          local virt_text = extmarks[1][4].virt_text[3][1]
2262          return virt_text
2263        end)
2264      )
2265
2266      eq(
2267        ' Some error [err-code]',
2268        exec_lua(function()
2269          local diagnostics = {
2270            _G.make_error('Some error', 0, 0, 0, 0, nil, 'err-code'),
2271          }
2272
2273          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
2274            underline = false,
2275            virtual_text = {
2276              prefix = '',
2277              suffix = function(diag)
2278                return string.format(' [%s]', diag.code)
2279              end,
2280            },
2281          })
2282
2283          local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2284          local virt_text = extmarks[1][4].virt_text[3][1]
2285          return virt_text
2286        end)
2287      )
2288    end)
2289
2290    it('can filter diagnostics by returning nil when formatting', function()
2291      local result = exec_lua(function()
2292        vim.diagnostic.config {
2293          virtual_text = {
2294            format = function(diagnostic)
2295              if diagnostic.code == 'foo_err' then
2296                return nil
2297              end
2298              return diagnostic.message
2299            end,
2300          },
2301        }
2302
2303        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2304          _G.make_error('An error here!', 0, 0, 0, 0, 'foo_server', 'foo_err'),
2305          _G.make_error('An error there!', 1, 1, 1, 1, 'bar_server', 'bar_err'),
2306        })
2307
2308        local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2309        return extmarks
2310      end)
2311
2312      eq(1, #result)
2313      eq(' An error there!', result[1][4].virt_text[3][1])
2314    end)
2315
2316    it('can only show virtual_text for the current line', function()
2317      local result = exec_lua(function()
2318        vim.api.nvim_win_set_cursor(0, { 1, 0 })
2319
2320        vim.diagnostic.config({ virtual_text = { current_line = true } })
2321
2322        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2323          _G.make_error('Error here!', 0, 0, 0, 0, 'foo_server'),
2324          _G.make_error('Another error there!', 1, 0, 1, 0, 'foo_server'),
2325        })
2326
2327        local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2328        return extmarks
2329      end)
2330
2331      eq(1, #result)
2332      eq(' Error here!', result[1][4].virt_text[3][1])
2333    end)
2334
2335    it('can hide virtual_text for the current line', function()
2336      local result = exec_lua(function()
2337        vim.api.nvim_win_set_cursor(0, { 1, 0 })
2338
2339        vim.diagnostic.config({ virtual_text = { current_line = false } })
2340
2341        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2342          _G.make_error('Error here!', 0, 0, 0, 0, 'foo_server'),
2343          _G.make_error('Another error there!', 1, 0, 1, 0, 'foo_server'),
2344        })
2345
2346        local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
2347        return extmarks
2348      end)
2349
2350      eq(1, #result)
2351      eq(' Another error there!', result[1][4].virt_text[3][1])
2352    end)
2353
2354    it('only renders virtual_text diagnostics within buffer length', function()
2355      local result = exec_lua(function()
2356        vim.api.nvim_win_set_cursor(0, { 1, 0 })
2357
2358        vim.diagnostic.config({ virtual_text = { current_line = false } })
2359
2360        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2361          _G.make_error('Hidden Error here!', 0, 0, 0, 0, 'foo_server'),
2362          _G.make_error('First Error here!', 1, 0, 1, 0, 'foo_server'),
2363          _G.make_error('Second Error here!', 2, 0, 2, 0, 'foo_server'),
2364          _G.make_error('First Ignored Error here!', 3, 0, 3, 0, 'foo_server'),
2365          _G.make_error('Second Ignored Error here!', 6, 0, 6, 0, 'foo_server'),
2366          _G.make_error('Third Ignored Error here!', 8, 0, 8, 0, 'foo_server'),
2367        })
2368
2369        vim.api.nvim_buf_set_lines(_G.diagnostic_bufnr, 2, 5, false, {})
2370        vim.api.nvim_exec_autocmds('CursorHold', { buf = _G.diagnostic_bufnr })
2371        return _G.get_virt_text_extmarks(_G.diagnostic_ns)
2372      end)
2373
2374      eq(2, #result)
2375      eq(' First Error here!', result[1][4].virt_text[3][1])
2376      eq(' Second Error here!', result[2][4].virt_text[3][1])
2377    end)
2378  end)
2379
2380  describe('handlers.virtual_lines', function()
2381    it('includes diagnostic code and message', function()
2382      local result = exec_lua(function()
2383        vim.diagnostic.config({ virtual_lines = true })
2384
2385        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2386          _G.make_error('Missed symbol `,`', 0, 0, 0, 0, 'lua_ls', 'miss-symbol'),
2387        })
2388
2389        local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
2390        return extmarks
2391      end)
2392
2393      eq(1, #result)
2394      eq('auto', result[1][4].virt_lines_overflow)
2395      eq('miss-symbol: Missed symbol `,`', result[1][4].virt_lines[1][3][1])
2396    end)
2397
2398    it('adds space to the left of the diagnostic', function()
2399      local error_offset = 5
2400      local result = exec_lua(function()
2401        vim.diagnostic.config({ virtual_lines = true })
2402
2403        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2404          _G.make_error('Error here!', 0, error_offset, 0, error_offset, 'foo_server'),
2405        })
2406
2407        local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
2408        return extmarks[1][4].virt_lines
2409      end)
2410
2411      eq(error_offset, result[1][1][1]:len())
2412    end)
2413
2414    it('highlights diagnostics in multiple lines by default #38685', function()
2415      local result = exec_lua(function()
2416        vim.diagnostic.config({ virtual_lines = true })
2417
2418        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2419          _G.make_error('Error here!', 0, 0, 0, 0, 'foo_server'),
2420          _G.make_error('Another error there!', 1, 0, 3, 0, 'foo_server'),
2421        })
2422
2423        local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
2424        return extmarks
2425      end)
2426
2427      eq(2, #result)
2428      eq('Error here!', result[1][4].virt_lines[1][3][1])
2429      eq('Another error there!', result[2][4].virt_lines[1][3][1])
2430      eq(0, result[1][2])
2431      eq(3, result[2][2])
2432    end)
2433
2434    it('highlights multiple diagnostics in a single line by default', function()
2435      local result = exec_lua(function()
2436        vim.api.nvim_buf_set_lines(
2437          _G.diagnostic_bufnr,
2438          0,
2439          -1,
2440          false,
2441          { 'def foo(x: int, /, y: str, *, z: float) -> None: ...' }
2442        )
2443
2444        vim.diagnostic.config({ virtual_lines = true })
2445
2446        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2447          _G.make_error('Error here!', 0, 8, 0, 9, 'foo_server'),
2448          _G.make_error('Another error there!', 0, 19, 0, 20, 'foo_server'),
2449          _G.make_error('And another one!', 0, 30, 0, 31, 'foo_server'),
2450        })
2451
2452        local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
2453        return extmarks
2454      end)
2455
2456      --[[
2457      |def foo(x: int, /, y: str, *, z: float) -> None: ...
2458      |        │          │          └──── And another one!
2459      |        │          └──── Another error there!
2460      |        └──── Error here!
2461      |        ^ col=8
2462      |                   ^ col=19
2463      |                              ^ col=30
2464
2465      11 cols between each diagnostic after the first one (10 spaces + |)
2466      ]]
2467
2468      eq(1, #result)
2469      local virt_lines = result[1][4].virt_lines
2470      eq(8, virt_lines[1][1][1]:len()) -- first space
2471      eq(10, virt_lines[1][3][1]:len()) -- second space
2472      eq(10, virt_lines[1][5][1]:len()) -- third space
2473      eq('And another one!', virt_lines[1][7][1])
2474      eq(8, virt_lines[2][1][1]:len()) -- first space
2475      eq(10, virt_lines[2][3][1]:len()) -- second space
2476      eq('Another error there!', virt_lines[2][5][1])
2477      eq(8, virt_lines[3][1][1]:len()) -- first space
2478      eq('Error here!', virt_lines[3][3][1])
2479    end)
2480
2481    it('highlights overlapping diagnostics on a single line', function()
2482      local result = exec_lua(function()
2483        vim.diagnostic.config({ virtual_lines = true })
2484
2485        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2486          _G.make_error('Error here!', 0, 10, 0, 11, 'foo_server'),
2487          _G.make_error('Another error here!', 0, 10, 0, 11, 'foo_server'),
2488        })
2489
2490        local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
2491        return extmarks
2492      end)
2493
2494      --[[
2495      |1234567890x
2496      |          ├──── Another error here!
2497      |          └──── Error here!
2498      ]]
2499
2500      eq(1, #result)
2501      local virt_lines = result[1][4].virt_lines
2502      eq(10, virt_lines[1][1][1]:len()) -- first space
2503      eq('├──── ', virt_lines[1][2][1])
2504      eq('Another error here!', virt_lines[1][3][1])
2505      eq(10, virt_lines[2][1][1]:len()) -- second space
2506      eq('└──── ', virt_lines[2][2][1])
2507      eq('Error here!', virt_lines[2][3][1])
2508    end)
2509
2510    it('handles multi-line diagnostic message', function()
2511      local result = exec_lua(function()
2512        vim.diagnostic.config({ virtual_lines = true })
2513
2514        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2515          _G.make_error('Error here!\ngot another line', 0, 10, 0, 11, 'foo_server'),
2516        })
2517
2518        local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
2519        return extmarks
2520      end)
2521
2522      --[[
2523      |1234567890x
2524      |          └──── Error here!
2525      |                got another line
2526      ]]
2527
2528      eq(1, #result)
2529      local virt_lines = result[1][4].virt_lines
2530      eq(10, virt_lines[1][1][1]:len()) -- first space
2531      eq('└──── ', virt_lines[1][2][1])
2532      eq('Error here!', virt_lines[1][3][1])
2533      eq(10, virt_lines[2][1][1]:len()) -- second line space
2534      eq(6, virt_lines[2][2][1]:len()) -- extra padding
2535      eq('got another line', virt_lines[2][3][1])
2536    end)
2537
2538    it('can highlight diagnostics only in the current line', function()
2539      local result = exec_lua(function()
2540        vim.api.nvim_win_set_cursor(0, { 1, 0 })
2541
2542        vim.diagnostic.config({ virtual_lines = { current_line = true } })
2543
2544        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2545          _G.make_error('Error here!', 0, 0, 0, 0, 'foo_server'),
2546          _G.make_error('Another error there!', 1, 0, 1, 0, 'foo_server'),
2547        })
2548
2549        vim.api.nvim_exec_autocmds('CursorHold', { buf = _G.diagnostic_bufnr })
2550        local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
2551        return extmarks
2552      end)
2553
2554      eq(1, #result)
2555      eq('Error here!', result[1][4].virt_lines[1][3][1])
2556    end)
2557
2558    it('supports a format function for diagnostic messages', function()
2559      local result = exec_lua(function()
2560        vim.diagnostic.config({
2561          virtual_lines = {
2562            format = function()
2563              return 'Error here!'
2564            end,
2565          },
2566        })
2567        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2568          _G.make_error('Invalid syntax', 0, 0, 0, 0),
2569        })
2570        local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
2571        return extmarks[1][4].virt_lines
2572      end)
2573      eq('Error here!', result[1][3][1])
2574    end)
2575
2576    it('sorts by severity with stable tiebreaker #37137', function()
2577      local result = exec_lua(function()
2578        vim.diagnostic.config({ severity_sort = true, virtual_lines = { current_line = true } })
2579        local m = 100
2580        local diagnostics = {
2581          { end_col = m, lnum = 0, message = 'a', severity = 2 },
2582          { end_col = m, lnum = 0, message = 'b', severity = 2 },
2583          { end_col = m, lnum = 0, message = 'c', severity = 2 },
2584          { end_col = m, lnum = 2, message = 'd', severity = 2 },
2585          { end_col = m, lnum = 2, message = 'e', severity = 2 },
2586          { end_col = m, lnum = 2, message = 'f', severity = 2 },
2587        }
2588        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {})
2589        vim.diagnostic.show(_G.diagnostic_ns, _G.diagnostic_bufnr)
2590        vim.api.nvim_win_set_cursor(0, { 1, 0 })
2591        vim.api.nvim_exec_autocmds('CursorHold', { buf = _G.diagnostic_bufnr })
2592        local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
2593        local result = {}
2594        for _, d in ipairs(extmarks[1][4].virt_lines) do
2595          table.insert(result, d[3][1])
2596        end
2597        return result
2598      end)
2599      eq({ 'c', 'b', 'a' }, result)
2600    end)
2601  end)
2602
2603  describe('set()', function()
2604    it('validation', function()
2605      matches(
2606        'expected a list of diagnostics',
2607        pcall_err(exec_lua, [[vim.diagnostic.set(1, 0, {lnum = 1, col = 2})]])
2608      )
2609    end)
2610
2611    it('does not accumulate BufRead autocmds for unloaded buffers', function()
2612      local counts = exec_lua(function()
2613        local bufnr = vim.fn.bufadd(vim.fn.tempname())
2614        local function count_autocmds()
2615          return #vim.api.nvim_get_autocmds({ event = 'BufRead', buffer = bufnr })
2616        end
2617        vim.diagnostic.set(_G.diagnostic_ns, bufnr, { _G.make_error('Error 1', 0, 0, 0, 0) })
2618        local first = count_autocmds()
2619        for i = 2, 5 do
2620          vim.diagnostic.set(_G.diagnostic_ns, bufnr, { _G.make_error('Error ' .. i, 0, 0, 0, 0) })
2621        end
2622        return { first = first, last = count_autocmds() }
2623      end)
2624      eq(counts.first, counts.last)
2625    end)
2626
2627    it('computes positions from the last set() when the buffer loads', function()
2628      eq(
2629        { autocmds = 0, extmarks = { { 2, 1 } } },
2630        exec_lua(function()
2631          local path = vim.fn.tempname()
2632          vim.fn.writefile({ 'one', 'two', 'three' }, path)
2633          local bufnr = vim.fn.bufadd(path)
2634          vim.diagnostic.set(_G.diagnostic_ns, bufnr, { _G.make_error('Old', 0, 0, 0, 0) })
2635          vim.diagnostic.set(_G.diagnostic_ns, bufnr, { _G.make_error('New', 2, 1, 2, 2) })
2636          vim.fn.bufload(bufnr)
2637          local location_ns = vim.diagnostic.get_namespace(_G.diagnostic_ns).user_data.location_ns
2638          local extmarks = {} --- @type [integer, integer][]
2639          for _, m in ipairs(vim.api.nvim_buf_get_extmarks(bufnr, location_ns, 0, -1, {})) do
2640            extmarks[#extmarks + 1] = { m[2], m[3] }
2641          end
2642          return {
2643            autocmds = #vim.api.nvim_get_autocmds({ event = 'BufRead', buffer = bufnr }),
2644            extmarks = extmarks,
2645          }
2646        end)
2647      )
2648    end)
2649
2650    it('can perform updates after insert_leave', function()
2651      exec_lua(function()
2652        vim.diagnostic.config({ virtual_text = true })
2653        vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
2654      end)
2655
2656      api.nvim_input('o')
2657      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2658
2659      -- Save the diagnostics
2660      exec_lua(function()
2661        vim.diagnostic.config({
2662          update_in_insert = false,
2663        })
2664        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2665          _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
2666        })
2667      end)
2668
2669      -- No diagnostics displayed yet.
2670      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2671      eq(
2672        1,
2673        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
2674      )
2675      eq(0, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
2676
2677      api.nvim_input('<esc>')
2678      eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
2679
2680      eq(
2681        1,
2682        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
2683      )
2684      eq(2, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
2685    end)
2686
2687    it('does not perform updates when not needed', function()
2688      exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]]
2689      api.nvim_input('o')
2690      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2691
2692      -- Save the diagnostics
2693      exec_lua(function()
2694        vim.diagnostic.config({
2695          update_in_insert = false,
2696          virtual_text = true,
2697        })
2698
2699        _G.DisplayCount = 0
2700        local set_virtual_text = vim.diagnostic.handlers.virtual_text.show
2701        vim.diagnostic.handlers.virtual_text.show = function(...)
2702          _G.DisplayCount = _G.DisplayCount + 1
2703          return set_virtual_text(...)
2704        end
2705
2706        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2707          _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
2708        })
2709      end)
2710
2711      -- No diagnostics displayed yet.
2712      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2713      eq(
2714        1,
2715        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
2716      )
2717      eq(0, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
2718      eq(0, exec_lua [[return _G.DisplayCount]])
2719
2720      api.nvim_input('<esc>')
2721      eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
2722
2723      eq(
2724        1,
2725        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
2726      )
2727      eq(2, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
2728      eq(1, exec_lua [[return _G.DisplayCount]])
2729
2730      -- Go in and out of insert mode one more time.
2731      api.nvim_input('o')
2732      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2733
2734      api.nvim_input('<esc>')
2735      eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
2736
2737      -- Should not have set the virtual text again.
2738      eq(1, exec_lua [[return _G.DisplayCount]])
2739    end)
2740
2741    it('never sets virtual text, in combination with insert leave', function()
2742      exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]]
2743      api.nvim_input('o')
2744      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2745
2746      -- Save the diagnostics
2747      exec_lua(function()
2748        vim.diagnostic.config({
2749          update_in_insert = false,
2750          virtual_text = false,
2751        })
2752
2753        _G.DisplayCount = 0
2754        local set_virtual_text = vim.diagnostic.handlers.virtual_text.show
2755        vim.diagnostic.handlers.virtual_text.show = function(...)
2756          _G.DisplayCount = _G.DisplayCount + 1
2757          return set_virtual_text(...)
2758        end
2759
2760        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2761          _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
2762        })
2763      end)
2764
2765      -- No diagnostics displayed yet.
2766      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2767      eq(
2768        1,
2769        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
2770      )
2771      eq(0, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
2772      eq(0, exec_lua [[return _G.DisplayCount]])
2773
2774      api.nvim_input('<esc>')
2775      eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
2776
2777      eq(
2778        1,
2779        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
2780      )
2781      eq(1, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
2782      eq(0, exec_lua [[return _G.DisplayCount]])
2783
2784      -- Go in and out of insert mode one more time.
2785      api.nvim_input('o')
2786      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2787
2788      api.nvim_input('<esc>')
2789      eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
2790
2791      -- Should not have set the virtual text still.
2792      eq(0, exec_lua [[return _G.DisplayCount]])
2793    end)
2794
2795    it('can perform updates while in insert mode, if desired', function()
2796      exec_lua(function()
2797        vim.diagnostic.config({ virtual_text = true })
2798        vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
2799      end)
2800      api.nvim_input('o')
2801      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2802
2803      -- Save the diagnostics
2804      exec_lua(function()
2805        vim.diagnostic.config({
2806          update_in_insert = true,
2807        })
2808
2809        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2810          _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
2811        })
2812      end)
2813
2814      -- Diagnostics are displayed, because the user wanted them that way!
2815      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2816      eq(
2817        1,
2818        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
2819      )
2820      eq(2, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
2821
2822      api.nvim_input('<esc>')
2823      eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
2824
2825      eq(
2826        1,
2827        exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR,  _G.diagnostic_ns)]]
2828      )
2829      eq(2, exec_lua [[return  _G.count_extmarks( _G.diagnostic_bufnr,  _G.diagnostic_ns)]])
2830    end)
2831
2832    it('can set diagnostics without displaying them', function()
2833      exec_lua(function()
2834        vim.diagnostic.config({ virtual_text = true })
2835      end)
2836
2837      eq(
2838        0,
2839        exec_lua(function()
2840          vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
2841          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2842            _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
2843          })
2844          return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
2845        end)
2846      )
2847
2848      eq(
2849        2,
2850        exec_lua(function()
2851          vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
2852          return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
2853        end)
2854      )
2855    end)
2856
2857    it('can set display options', function()
2858      eq(
2859        0,
2860        exec_lua(function()
2861          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2862            _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
2863          }, { virtual_text = false, underline = false })
2864          return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
2865        end)
2866      )
2867
2868      eq(
2869        1,
2870        exec_lua(function()
2871          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
2872            _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
2873          }, { virtual_text = true, underline = false })
2874          return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
2875        end)
2876      )
2877    end)
2878
2879    it('sets and clears signs #26193 #26555', function()
2880      do
2881        local result = exec_lua(function()
2882          vim.diagnostic.config({
2883            signs = true,
2884          })
2885
2886          local diagnostics = {
2887            _G.make_error('Error', 1, 1, 1, 2),
2888            _G.make_warning('Warning', 3, 3, 3, 3),
2889          }
2890
2891          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
2892
2893          local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
2894          local sign_ns = ns.user_data.sign_ns
2895
2896          local signs = vim.api.nvim_buf_get_extmarks(
2897            _G.diagnostic_bufnr,
2898            sign_ns,
2899            0,
2900            -1,
2901            { type = 'sign', details = true }
2902          )
2903          local result = {}
2904          for _, s in ipairs(signs) do
2905            result[#result + 1] = { lnum = s[2] + 1, name = s[4].sign_hl_group }
2906          end
2907          return result
2908        end)
2909
2910        eq({ 2, 'DiagnosticSignError' }, { result[1].lnum, result[1].name })
2911        eq({ 4, 'DiagnosticSignWarn' }, { result[2].lnum, result[2].name })
2912      end
2913
2914      do
2915        local result = exec_lua(function()
2916          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {})
2917
2918          local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
2919          local sign_ns = ns.user_data.sign_ns
2920
2921          return vim.api.nvim_buf_get_extmarks(
2922            _G.diagnostic_bufnr,
2923            sign_ns,
2924            0,
2925            -1,
2926            { type = 'sign', details = true }
2927          )
2928        end)
2929
2930        eq({}, result)
2931      end
2932    end)
2933
2934    it('always passes a table to DiagnosticChanged autocommand', function()
2935      local result = exec_lua(function()
2936        local changed_diags --- @type vim.Diagnostic[]?
2937        vim.api.nvim_create_autocmd('DiagnosticChanged', {
2938          buf = _G.diagnostic_bufnr,
2939          callback = function(ev)
2940            --- @type vim.Diagnostic[]
2941            changed_diags = ev.data.diagnostics
2942          end,
2943        })
2944        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {})
2945        return changed_diags
2946      end)
2947      eq('table', type(result))
2948      eq(0, #result)
2949    end)
2950  end)
2951
2952  describe('open_float()', function()
2953    it('can display a header', function()
2954      eq(
2955        { 'Diagnostics:', '1. Syntax error' },
2956        exec_lua(function()
2957          local diagnostics = {
2958            _G.make_error('Syntax error', 0, 1, 0, 3),
2959          }
2960          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
2961          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
2962          local float_bufnr, winnr = vim.diagnostic.open_float()
2963          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
2964          vim.api.nvim_win_close(winnr, true)
2965          return lines
2966        end)
2967      )
2968
2969      eq(
2970        { "We're no strangers to love...", '1. Syntax error' },
2971        exec_lua(function()
2972          local diagnostics = {
2973            _G.make_error('Syntax error', 0, 1, 0, 3),
2974          }
2975          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
2976          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
2977          local float_bufnr, winnr =
2978            vim.diagnostic.open_float({ header = "We're no strangers to love..." })
2979          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
2980          vim.api.nvim_win_close(winnr, true)
2981          return lines
2982        end)
2983      )
2984
2985      eq(
2986        { 'You know the rules', '1. Syntax error' },
2987        exec_lua(function()
2988          local diagnostics = {
2989            _G.make_error('Syntax error', 0, 1, 0, 3),
2990          }
2991          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
2992          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
2993          local float_bufnr, winnr =
2994            vim.diagnostic.open_float({ header = { 'You know the rules', 'Search' } })
2995          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
2996          vim.api.nvim_win_close(winnr, true)
2997          return lines
2998        end)
2999      )
3000    end)
3001
3002    it('can show diagnostics from the whole buffer', function()
3003      eq(
3004        { '1. Syntax error', '2. Some warning' },
3005        exec_lua(function()
3006          local diagnostics = {
3007            _G.make_error('Syntax error', 0, 1, 0, 3),
3008            _G.make_warning('Some warning', 1, 1, 1, 3),
3009          }
3010          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3011          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3012          local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
3013          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3014          vim.api.nvim_win_close(winnr, true)
3015          return lines
3016        end)
3017      )
3018    end)
3019
3020    it('can show diagnostics from a single line', function()
3021      -- Using cursor position
3022      eq(
3023        { '1. Some warning' },
3024        exec_lua(function()
3025          local diagnostics = {
3026            _G.make_error('Syntax error', 0, 1, 0, 3),
3027            _G.make_warning('Some warning', 1, 1, 1, 3),
3028          }
3029          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3030          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3031          vim.api.nvim_win_set_cursor(0, { 2, 1 })
3032          local float_bufnr, winnr = vim.diagnostic.open_float({ header = false })
3033          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3034          vim.api.nvim_win_close(winnr, true)
3035          return lines
3036        end)
3037      )
3038
3039      -- With specified position
3040      eq(
3041        { '1. Some warning' },
3042        exec_lua(function()
3043          local diagnostics = {
3044            _G.make_error('Syntax error', 0, 1, 0, 3),
3045            _G.make_warning('Some warning', 1, 1, 1, 3),
3046          }
3047          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3048          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3049          vim.api.nvim_win_set_cursor(0, { 1, 1 })
3050          local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, pos = 1 })
3051          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3052          vim.api.nvim_win_close(winnr, true)
3053          return lines
3054        end)
3055      )
3056
3057      -- End position is exclusive
3058      eq(
3059        nil,
3060        exec_lua(function()
3061          local diagnostics = {
3062            _G.make_error('Syntax error', 1, 1, 2, 0),
3063          }
3064          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3065          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3066          vim.api.nvim_win_set_cursor(0, { 1, 1 })
3067          local _, winnr = vim.diagnostic.open_float(0, { header = false, pos = { 2, 0 } })
3068          return winnr
3069        end)
3070      )
3071
3072      -- Works when width == 0
3073      eq(
3074        { '1. Syntax error' },
3075        exec_lua(function()
3076          local diagnostics = {
3077            _G.make_error('Syntax error', 2, 0, 2, 0),
3078          }
3079          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3080          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3081          vim.api.nvim_win_set_cursor(0, { 1, 1 })
3082          local float_bufnr, winnr =
3083            vim.diagnostic.open_float(0, { header = false, pos = { 2, 1 } })
3084          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3085          vim.api.nvim_win_close(winnr, true)
3086          return lines
3087        end)
3088      )
3089    end)
3090
3091    it('can show diagnostics from a specific position', function()
3092      -- Using cursor position
3093      eq(
3094        { 'Syntax error' },
3095        exec_lua(function()
3096          local diagnostics = {
3097            _G.make_error('Syntax error', 1, 1, 1, 3),
3098            _G.make_warning('Some warning', 1, 3, 1, 4),
3099          }
3100          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3101          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3102          vim.api.nvim_win_set_cursor(0, { 2, 2 })
3103          local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'cursor' })
3104          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3105          vim.api.nvim_win_close(winnr, true)
3106          return lines
3107        end)
3108      )
3109
3110      -- With specified position
3111      eq(
3112        { 'Some warning' },
3113        exec_lua(function()
3114          local diagnostics = {
3115            _G.make_error('Syntax error', 1, 1, 1, 3),
3116            _G.make_warning('Some warning', 1, 3, 1, 4),
3117          }
3118          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3119          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3120          vim.api.nvim_win_set_cursor(0, { 1, 1 })
3121          local float_bufnr, winnr =
3122            vim.diagnostic.open_float({ header = false, scope = 'cursor', pos = { 1, 3 } })
3123          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3124          vim.api.nvim_win_close(winnr, true)
3125          return lines
3126        end)
3127      )
3128
3129      -- With column position past the end of the line. #16062
3130      eq(
3131        { 'Syntax error' },
3132        exec_lua(function()
3133          local first_line_len = #vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, 1, true)[1]
3134          local diagnostics = {
3135            _G.make_error('Syntax error', 0, first_line_len + 1, 1, 0),
3136          }
3137          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3138          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3139          vim.api.nvim_win_set_cursor(0, { 1, 1 })
3140          local float_bufnr, winnr = vim.diagnostic.open_float({
3141            header = false,
3142            scope = 'cursor',
3143            pos = { 0, first_line_len - 1 },
3144          })
3145          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3146          vim.api.nvim_win_close(winnr, true)
3147          return lines
3148        end)
3149      )
3150
3151      -- End position is exclusive
3152      eq(
3153        nil,
3154        exec_lua(function()
3155          local diagnostics = {
3156            _G.make_error('Syntax error', 1, 1, 1, 3),
3157          }
3158          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3159          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3160          vim.api.nvim_win_set_cursor(0, { 1, 1 })
3161          local _, winnr =
3162            vim.diagnostic.open_float(0, { header = false, scope = 'cursor', pos = { 1, 3 } })
3163          return winnr
3164        end)
3165      )
3166
3167      -- Works when width == 0
3168      eq(
3169        { 'Syntax error' },
3170        exec_lua(function()
3171          local diagnostics = {
3172            _G.make_error('Syntax error', 2, 0, 2, 0),
3173          }
3174          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3175          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3176          vim.api.nvim_win_set_cursor(0, { 1, 1 })
3177          local float_bufnr, winnr =
3178            vim.diagnostic.open_float({ header = false, scope = 'cursor', pos = { 2, 0 } })
3179          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3180          vim.api.nvim_win_close(winnr, true)
3181          return lines
3182        end)
3183      )
3184    end)
3185
3186    it(
3187      'creates floating window and returns float bufnr and winnr if current line contains diagnostics',
3188      function()
3189        -- Two lines:
3190        --    Diagnostic:
3191        --    1. <msg>
3192        eq(
3193          2,
3194          exec_lua(function()
3195            local diagnostics = {
3196              _G.make_error('Syntax error', 0, 1, 0, 3),
3197            }
3198            vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3199            vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3200            local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr)
3201            local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3202            vim.api.nvim_win_close(winnr, true)
3203            return #lines
3204          end)
3205        )
3206      end
3207    )
3208
3209    it('only reports diagnostics from the current buffer when bufnr is omitted #15710', function()
3210      eq(
3211        2,
3212        exec_lua(function()
3213          local other_bufnr = vim.api.nvim_create_buf(true, false)
3214          local buf_1_diagnostics = {
3215            _G.make_error('Syntax error', 0, 1, 0, 3),
3216          }
3217          local buf_2_diagnostics = {
3218            _G.make_warning('Some warning', 0, 1, 0, 3),
3219          }
3220          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3221          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, buf_1_diagnostics)
3222          vim.diagnostic.set(_G.other_ns, other_bufnr, buf_2_diagnostics)
3223          local float_bufnr, winnr = vim.diagnostic.open_float()
3224          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3225          vim.api.nvim_win_close(winnr, true)
3226          return #lines
3227        end)
3228      )
3229    end)
3230
3231    it('allows filtering by namespace', function()
3232      eq(
3233        { 'Diagnostics:', '1. Syntax error' },
3234        exec_lua(function()
3235          local ns_1_diagnostics = {
3236            _G.make_error('Syntax error', 0, 1, 0, 3),
3237          }
3238          local ns_2_diagnostics = {
3239            _G.make_warning('Some warning', 0, 1, 0, 3),
3240          }
3241          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3242          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diagnostics)
3243          vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diagnostics)
3244          local float_bufnr, winnr =
3245            vim.diagnostic.open_float(_G.diagnostic_bufnr, { namespace = _G.diagnostic_ns })
3246          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3247          vim.api.nvim_win_close(winnr, true)
3248          return lines
3249        end)
3250      )
3251    end)
3252
3253    it('allows filtering by multiple namespaces', function()
3254      eq(
3255        { 'Diagnostics:', '1. Syntax error', '2. Some warning' },
3256        exec_lua(function()
3257          local ns_1_diagnostics = {
3258            _G.make_error('Syntax error', 0, 1, 0, 3),
3259          }
3260          local ns_2_diagnostics = {
3261            _G.make_warning('Some warning', 0, 1, 0, 3),
3262          }
3263          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3264          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diagnostics)
3265          vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diagnostics)
3266          local float_bufnr, winnr = vim.diagnostic.open_float(
3267            _G.diagnostic_bufnr,
3268            { namespace = { _G.diagnostic_ns, _G.other_ns } }
3269          )
3270          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3271          vim.api.nvim_win_close(winnr, true)
3272          return lines
3273        end)
3274      )
3275    end)
3276
3277    it('creates floating window without header and supports function-based float config', function()
3278      -- One line (since no header):
3279      --    1. <msg>
3280      eq(
3281        { 1, '╭', '┌' },
3282        exec_lua(function()
3283          local diagnostics = {
3284            _G.make_error('Syntax error', 0, 1, 0, 3),
3285          }
3286          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3287          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3288          local float_bufnr, winnr =
3289            vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
3290          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3291          vim.api.nvim_win_close(winnr, true)
3292          vim.diagnostic.config({
3293            float = function(_, bufnr)
3294              return { border = bufnr == _G.diagnostic_bufnr and 'rounded' or 'single' }
3295            end,
3296          })
3297          _, winnr = vim.diagnostic.open_float()
3298          local border1 = vim.api.nvim_win_get_config(winnr).border
3299          vim.api.nvim_win_close(winnr, true)
3300
3301          local other_bufnr = vim.api.nvim_create_buf(true, true)
3302          vim.api.nvim_win_set_buf(0, other_bufnr)
3303          vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, diagnostics)
3304          local _, winnr1 = vim.diagnostic.open_float()
3305          local border2 = vim.api.nvim_win_get_config(winnr1).border
3306          vim.api.nvim_win_close(winnr1, true)
3307          return { #lines, border1[1], border2[1] }
3308        end)
3309      )
3310    end)
3311
3312    it('clamps diagnostic line numbers within the valid range', function()
3313      eq(
3314        1,
3315        exec_lua(function()
3316          local diagnostics = {
3317            _G.make_error('Syntax error', 6, 0, 6, 0),
3318          }
3319          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3320          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3321          local float_bufnr, winnr =
3322            vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false, pos = 5 })
3323          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3324          vim.api.nvim_win_close(winnr, true)
3325          return #lines
3326        end)
3327      )
3328    end)
3329
3330    it('can show diagnostic source', function()
3331      exec_lua [[vim.api.nvim_win_set_buf(0,  _G.diagnostic_bufnr)]]
3332
3333      eq(
3334        { '1. Syntax error' },
3335        exec_lua(function()
3336          local diagnostics = {
3337            _G.make_error('Syntax error', 0, 1, 0, 3, 'source x'),
3338          }
3339          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3340          local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, {
3341            header = false,
3342            source = 'if_many',
3343          })
3344          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3345          vim.api.nvim_win_close(winnr, true)
3346          return lines
3347        end)
3348      )
3349
3350      eq(
3351        { '1. source x: Syntax error' },
3352        exec_lua(function()
3353          local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, {
3354            header = false,
3355            source = 'always',
3356          })
3357          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3358          vim.api.nvim_win_close(winnr, true)
3359          return lines
3360        end)
3361      )
3362
3363      eq(
3364        { '1. source x: Syntax error', '2. source y: Another error' },
3365        exec_lua(function()
3366          local diagnostics = {
3367            _G.make_error('Syntax error', 0, 1, 0, 3, 'source x'),
3368            _G.make_error('Another error', 0, 1, 0, 3, 'source y'),
3369          }
3370          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3371          local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, {
3372            header = false,
3373            source = 'if_many',
3374          })
3375          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3376          vim.api.nvim_win_close(winnr, true)
3377          return lines
3378        end)
3379      )
3380    end)
3381
3382    it('respects severity_sort', function()
3383      exec_lua [[vim.api.nvim_win_set_buf(0,  _G.diagnostic_bufnr)]]
3384
3385      eq(
3386        { '1. Syntax error', '2. Info', '3. Error', '4. Warning' },
3387        exec_lua(function()
3388          local diagnostics = {
3389            _G.make_error('Syntax error', 0, 1, 0, 3),
3390            _G.make_info('Info', 0, 3, 0, 4),
3391            _G.make_error('Error', 0, 2, 0, 2),
3392            _G.make_warning('Warning', 0, 0, 0, 1),
3393          }
3394
3395          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3396
3397          vim.diagnostic.config({ severity_sort = false })
3398
3399          local float_bufnr, winnr =
3400            vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
3401          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3402          vim.api.nvim_win_close(winnr, true)
3403          return lines
3404        end)
3405      )
3406
3407      eq(
3408        { '1. Syntax error', '2. Error', '3. Warning', '4. Info' },
3409        exec_lua(function()
3410          vim.diagnostic.config({ severity_sort = true })
3411          local float_bufnr, winnr =
3412            vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
3413          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3414          vim.api.nvim_win_close(winnr, true)
3415          return lines
3416        end)
3417      )
3418
3419      eq(
3420        { '1. Info', '2. Warning', '3. Error', '4. Syntax error' },
3421        exec_lua(function()
3422          vim.diagnostic.config({ severity_sort = { reverse = true } })
3423          local float_bufnr, winnr =
3424            vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
3425          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3426          vim.api.nvim_win_close(winnr, true)
3427          return lines
3428        end)
3429      )
3430    end)
3431
3432    it('can filter by severity', function()
3433      local count_diagnostics_with_severity = function(min_severity, max_severity)
3434        return exec_lua(function()
3435          vim.diagnostic.config({
3436            float = {
3437              severity = { min = min_severity, max = max_severity },
3438            },
3439          })
3440
3441          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
3442            _G.make_error('Syntax error', 0, 1, 0, 3),
3443            _G.make_info('Info', 0, 3, 0, 4),
3444            _G.make_error('Error', 0, 2, 0, 2),
3445            _G.make_warning('Warning', 0, 0, 0, 1),
3446          })
3447
3448          local float_bufnr, winnr =
3449            vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
3450          if not float_bufnr then
3451            return 0
3452          end
3453
3454          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3455          vim.api.nvim_win_close(winnr, true)
3456          return #lines
3457        end)
3458      end
3459
3460      eq(2, count_diagnostics_with_severity('ERROR'))
3461      eq(3, count_diagnostics_with_severity('WARN'))
3462      eq(1, count_diagnostics_with_severity('WARN', 'WARN'))
3463      eq(4, count_diagnostics_with_severity('HINT'))
3464      eq(0, count_diagnostics_with_severity('HINT', 'HINT'))
3465    end)
3466
3467    it('can add a prefix to diagnostics', function()
3468      -- Default is to add a number
3469      eq(
3470        { '1. Syntax error', '2. Some warning' },
3471        exec_lua(function()
3472          local diagnostics = {
3473            _G.make_error('Syntax error', 0, 1, 0, 3),
3474            _G.make_warning('Some warning', 1, 1, 1, 3),
3475          }
3476          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3477          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3478          local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
3479          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3480          vim.api.nvim_win_close(winnr, true)
3481          return lines
3482        end)
3483      )
3484
3485      eq(
3486        { 'Syntax error', 'Some warning' },
3487        exec_lua(function()
3488          local diagnostics = {
3489            _G.make_error('Syntax error', 0, 1, 0, 3),
3490            _G.make_warning('Some warning', 1, 1, 1, 3),
3491          }
3492          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3493          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3494          local float_bufnr, winnr =
3495            vim.diagnostic.open_float({ header = false, scope = 'buffer', prefix = '' })
3496          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3497          vim.api.nvim_win_close(winnr, true)
3498          return lines
3499        end)
3500      )
3501
3502      eq(
3503        { '1. Syntax error', '2. Some warning' },
3504        exec_lua(function()
3505          local diagnostics = {
3506            _G.make_error('Syntax error', 0, 1, 0, 3),
3507            _G.make_warning('Some warning', 0, 1, 0, 3),
3508          }
3509          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3510          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3511          local float_bufnr, winnr = vim.diagnostic.open_float({
3512            header = false,
3513            prefix = function(_, i, total)
3514              -- Only show a number if there is more than one diagnostic
3515              if total > 1 then
3516                return string.format('%d. ', i)
3517              end
3518              return ''
3519            end,
3520          })
3521          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3522          vim.api.nvim_win_close(winnr, true)
3523          return lines
3524        end)
3525      )
3526
3527      eq(
3528        { 'Syntax error' },
3529        exec_lua(function()
3530          local diagnostics = {
3531            _G.make_error('Syntax error', 0, 1, 0, 3),
3532          }
3533          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3534          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3535          local float_bufnr, winnr = vim.diagnostic.open_float({
3536            header = false,
3537            prefix = function(_, i, total)
3538              -- Only show a number if there is more than one diagnostic
3539              if total > 1 then
3540                return string.format('%d. ', i)
3541              end
3542              return ''
3543            end,
3544          })
3545          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3546          vim.api.nvim_win_close(winnr, true)
3547          return lines
3548        end)
3549      )
3550
3551      eq(
3552        '.../_float.lua:0: prefix: expected string|table|function, got number',
3553        pcall_err(exec_lua, [[ vim.diagnostic.open_float({ prefix = 42 }) ]])
3554      )
3555    end)
3556
3557    it('can add a suffix to diagnostics', function()
3558      -- Default is to render the diagnostic error code
3559      eq(
3560        { '1. Syntax error [code-x]', '2. Some warning [code-y]' },
3561        exec_lua(function()
3562          local diagnostics = {
3563            _G.make_error('Syntax error', 0, 1, 0, 3, nil, 'code-x'),
3564            _G.make_warning('Some warning', 1, 1, 1, 3, nil, 'code-y'),
3565          }
3566          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3567          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3568          local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
3569          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3570          vim.api.nvim_win_close(winnr, true)
3571          return lines
3572        end)
3573      )
3574
3575      eq(
3576        { '1. Syntax error', '2. Some warning' },
3577        exec_lua(function()
3578          local diagnostics = {
3579            _G.make_error('Syntax error', 0, 1, 0, 3, nil, 'code-x'),
3580            _G.make_warning('Some warning', 1, 1, 1, 3, nil, 'code-y'),
3581          }
3582          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3583          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3584          local float_bufnr, winnr =
3585            vim.diagnostic.open_float({ header = false, scope = 'buffer', suffix = '' })
3586          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3587          vim.api.nvim_win_close(winnr, true)
3588          return lines
3589        end)
3590      )
3591
3592      -- Suffix is rendered on the last line of a multiline diagnostic
3593      eq(
3594        { '1. Syntax error', '   More context [code-x]' },
3595        exec_lua(function()
3596          local diagnostics = {
3597            _G.make_error('Syntax error\nMore context', 0, 1, 0, 3, nil, 'code-x'),
3598          }
3599          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3600          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3601          local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
3602          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3603          vim.api.nvim_win_close(winnr, true)
3604          return lines
3605        end)
3606      )
3607
3608      eq(
3609        '.../_float.lua:0: suffix: expected string|table|function, got number',
3610        pcall_err(exec_lua, [[ vim.diagnostic.open_float({ suffix = 42 }) ]])
3611      )
3612    end)
3613
3614    it('can add LSP related information to a diagnostic', function()
3615      local related_info_uri = 'file:///fake/uri'
3616
3617      -- Populate the related info buffer.
3618      exec_lua(function()
3619        local fake_bufnr = vim.uri_to_bufnr(related_info_uri)
3620        vim.fn.bufload(fake_bufnr)
3621        vim.api.nvim_buf_set_lines(fake_bufnr, 0, 1, false, {
3622          'O, the Pelican,',
3623          'so smoothly doth he crest.',
3624          'a wind god!',
3625        })
3626        vim.api.nvim_win_set_buf(0, fake_bufnr)
3627      end)
3628
3629      -- Displays related info.
3630      eq(
3631        {
3632          '1. Some warning',
3633          '   uri:2:1: Some extra info',
3634          '   uri:3:4: Some more extra info',
3635        },
3636        exec_lua(function()
3637          ---@type vim.Diagnostic
3638          local diagnostic = _G.make_warning('Some warning', 1, 1, 1, 3)
3639          diagnostic.user_data = {
3640            -- Related information comes from LSP user_data
3641            lsp = {
3642              relatedInformation = {
3643                {
3644                  message = 'Some extra info',
3645                  location = {
3646                    uri = related_info_uri,
3647                    range = {
3648                      start = {
3649                        line = 1,
3650                        character = 0,
3651                      },
3652                      ['end'] = {
3653                        line = 1,
3654                        character = 1,
3655                      },
3656                    },
3657                  },
3658                },
3659                {
3660                  message = 'Some more extra info',
3661                  location = {
3662                    uri = related_info_uri,
3663                    range = {
3664                      start = {
3665                        line = 2,
3666                        character = 3,
3667                      },
3668                      ['end'] = {
3669                        line = 4,
3670                        character = 5,
3671                      },
3672                    },
3673                  },
3674                },
3675              },
3676            },
3677          }
3678          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3679          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { diagnostic })
3680          local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
3681          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3682          -- Put the cursor on a line with related info
3683          vim.api.nvim_tabpage_set_win(0, winnr)
3684          vim.api.nvim_win_set_cursor(0, { 2, 0 })
3685          return lines
3686        end)
3687      )
3688
3689      -- Jumps to related info.
3690      eq(
3691        'so smoothly doth he crest.',
3692        exec_lua(function()
3693          vim.cmd.norm('gf')
3694          vim.wait(20, function() end)
3695          return vim.api.nvim_get_current_line()
3696        end)
3697      )
3698    end)
3699
3700    it('errors when LSP relatedInformation is null', function()
3701      matches(
3702        'server response has invalid %(null%) relatedInformation',
3703        pcall_err(
3704          exec_lua,
3705          [[
3706          local diagnostic = _G.make_warning('Some warning', 1, 1, 1, 3)
3707          diagnostic.user_data = { lsp = { relatedInformation = vim.NIL } }
3708          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3709          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { diagnostic })
3710          vim.diagnostic.open_float({ header = false, scope = 'buffer' })
3711        ]]
3712        )
3713      )
3714    end)
3715
3716    it('works with the old signature', function()
3717      eq(
3718        { '1. Syntax error' },
3719        exec_lua(function()
3720          local diagnostics = {
3721            _G.make_error('Syntax error', 0, 1, 0, 3),
3722          }
3723          vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3724          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3725          local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false })
3726          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3727          vim.api.nvim_win_close(winnr, true)
3728          return lines
3729        end)
3730      )
3731    end)
3732
3733    it('works for multi-line diagnostics #21949', function()
3734      -- create diagnostic
3735      exec_lua(function()
3736        local diagnostics = {
3737          _G.make_error('Error in two lines lnum is 1 and end_lnum is 2', 1, 1, 2, 3),
3738        }
3739        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3740        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
3741      end)
3742
3743      -- open float failed non diagnostic lnum
3744      eq(
3745        nil,
3746        exec_lua(function()
3747          vim.api.nvim_win_set_cursor(0, { 1, 0 })
3748          local _, winnr = vim.diagnostic.open_float(0, { header = false })
3749          return winnr
3750        end)
3751      )
3752      eq(
3753        nil,
3754        exec_lua(function()
3755          vim.api.nvim_win_set_cursor(0, { 1, 0 })
3756          local _, winnr = vim.diagnostic.open_float(0, { header = false, scope = 'cursor' })
3757          return winnr
3758        end)
3759      )
3760
3761      -- can open a float window on lnum 1
3762      eq(
3763        { '1. Error in two lines lnum is 1 and end_lnum is 2' },
3764        exec_lua(function()
3765          vim.api.nvim_win_set_cursor(0, { 2, 0 })
3766          local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false })
3767          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3768          vim.api.nvim_win_close(winnr, true)
3769          return lines
3770        end)
3771      )
3772
3773      -- can open a cursor-scoped float window on lnum 1
3774      eq(
3775        { 'Error in two lines lnum is 1 and end_lnum is 2' },
3776        exec_lua(function()
3777          vim.api.nvim_win_set_cursor(0, { 2, 1 })
3778          local float_bufnr, winnr =
3779            vim.diagnostic.open_float(0, { header = false, scope = 'cursor' })
3780          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3781          vim.api.nvim_win_close(winnr, true)
3782          return lines
3783        end)
3784      )
3785
3786      -- can open a float window on end_lnum 2
3787      eq(
3788        { '1. Error in two lines lnum is 1 and end_lnum is 2' },
3789        exec_lua(function()
3790          vim.api.nvim_win_set_cursor(0, { 3, 0 })
3791          local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false })
3792          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3793          vim.api.nvim_win_close(winnr, true)
3794          return lines
3795        end)
3796      )
3797
3798      -- can open a cursor-scoped float window on end_lnum 2
3799      eq(
3800        { 'Error in two lines lnum is 1 and end_lnum is 2' },
3801        exec_lua(function()
3802          vim.api.nvim_win_set_cursor(0, { 3, 2 })
3803          local float_bufnr, winnr =
3804            vim.diagnostic.open_float(0, { header = false, scope = 'cursor' })
3805          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3806          vim.api.nvim_win_close(winnr, true)
3807          return lines
3808        end)
3809      )
3810    end)
3811
3812    it('shows diagnostics at their logical locations after text changes before', function()
3813      exec_lua(function()
3814        vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
3815
3816        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
3817          _G.make_error('Diagnostic #1', 1, 4, 1, 7),
3818          _G.make_error('Diagnostic #2', 3, 0, 3, 3),
3819        })
3820      end)
3821
3822      api.nvim_buf_set_text(0, 3, 0, 3, 0, { 'new line', 'new ' })
3823
3824      eq(
3825        { 'Diagnostic #1' },
3826        exec_lua(function()
3827          vim.api.nvim_win_set_cursor(0, { 2, 4 })
3828          local float_bufnr, winnr = vim.diagnostic.open_float({ header = '', scope = 'cursor' })
3829          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3830          vim.api.nvim_win_close(winnr, true)
3831          return lines
3832        end)
3833      )
3834
3835      eq(
3836        { 'Diagnostic #2' },
3837        exec_lua(function()
3838          vim.api.nvim_win_set_cursor(0, { 5, 4 })
3839          local float_bufnr, winnr = vim.diagnostic.open_float({ header = '', scope = 'cursor' })
3840          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3841          vim.api.nvim_win_close(winnr, true)
3842          return lines
3843        end)
3844      )
3845    end)
3846
3847    it('shows diagnostics at their logical locations after text changes inside', function()
3848      exec_lua(function()
3849        vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
3850
3851        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
3852          _G.make_error('Diagnostic #1', 1, 0, 1, 7),
3853        })
3854      end)
3855
3856      api.nvim_buf_set_text(0, 1, 4, 1, 4, { 'new ' })
3857
3858      eq(
3859        { 'Diagnostic #1' },
3860        exec_lua(function()
3861          vim.api.nvim_win_set_cursor(0, { 2, 10 })
3862          local float_bufnr, winnr = vim.diagnostic.open_float({ header = '', scope = 'cursor' })
3863          local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3864          vim.api.nvim_win_close(winnr, true)
3865          return lines
3866        end)
3867      )
3868    end)
3869
3870    it(
3871      'shows diagnostics at the end of the line if diagnostic is set after last character in line',
3872      function()
3873        exec_lua(function()
3874          vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
3875
3876          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
3877            _G.make_error('Diagnostic #1', 2, 3, 3, 4),
3878          })
3879        end)
3880
3881        eq(
3882          { 'Diagnostic #1' },
3883          exec_lua(function()
3884            vim.api.nvim_win_set_cursor(0, { 3, 2 })
3885            local float_bufnr, winnr = vim.diagnostic.open_float({ header = '', scope = 'cursor' })
3886            local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
3887            vim.api.nvim_win_close(winnr, true)
3888            return lines
3889          end)
3890        )
3891      end
3892    )
3893  end)
3894
3895  describe('setloclist()', function()
3896    it('sets diagnostics in lnum order', function()
3897      local loc_list = exec_lua(function()
3898        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3899
3900        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
3901          _G.make_error('Farther Diagnostic', 4, 4, 4, 4),
3902          _G.make_error('Lower Diagnostic', 1, 1, 1, 1),
3903        })
3904
3905        vim.diagnostic.setloclist()
3906
3907        return vim.fn.getloclist(0)
3908      end)
3909
3910      assert(loc_list[1].lnum < loc_list[2].lnum)
3911    end)
3912
3913    it('sets diagnostics in lnum order, regardless of namespace', function()
3914      local loc_list = exec_lua(function()
3915        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3916
3917        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
3918          _G.make_error('Lower Diagnostic', 1, 1, 1, 1),
3919        })
3920
3921        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
3922          _G.make_warning('Farther Diagnostic', 4, 4, 4, 4),
3923        })
3924
3925        vim.diagnostic.setloclist()
3926
3927        return vim.fn.getloclist(0)
3928      end)
3929
3930      assert(loc_list[1].lnum < loc_list[2].lnum)
3931    end)
3932
3933    it('sets diagnostics from the specified namespaces', function()
3934      local loc_list = exec_lua(function()
3935        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3936
3937        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
3938          _G.make_error('Error here!', 1, 1, 1, 1),
3939        })
3940        vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
3941          _G.make_warning('Error there!', 2, 2, 2, 2),
3942        })
3943
3944        vim.diagnostic.setloclist({ namespace = { _G.diagnostic_ns } })
3945
3946        return vim.fn.getloclist(0)
3947      end)
3948
3949      eq(1, #loc_list)
3950      eq('Error here!', loc_list[1].text)
3951    end)
3952
3953    it('supports format function', function()
3954      local loc_list = exec_lua(function()
3955        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3956
3957        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
3958          _G.make_error('Error', 1, 1, 1, 1, 'foo_ls'),
3959          _G.make_error('Another error', 2, 2, 2, 2, 'foo_ls'),
3960        })
3961
3962        vim.diagnostic.setloclist({
3963          format = function(diagnostic)
3964            if diagnostic.lnum > 1 then
3965              return nil
3966            end
3967
3968            return string.format('%s: %s', diagnostic.source, diagnostic.message)
3969          end,
3970        })
3971
3972        return vim.fn.getloclist(0)
3973      end)
3974
3975      eq(1, #loc_list)
3976      eq('foo_ls: Error', loc_list[1].text)
3977    end)
3978  end)
3979
3980  describe('setqflist()', function()
3981    it('updates existing diagnostics quickfix if one already exists', function()
3982      local result = exec_lua(function()
3983        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
3984
3985        vim.fn.setqflist({}, ' ', { title = 'Diagnostics' })
3986        local diagnostics_qf_id = vim.fn.getqflist({ id = 0 }).id
3987
3988        vim.diagnostic.setqflist({ title = 'Diagnostics' })
3989        local qf_id = vim.fn.getqflist({ id = 0, nr = '$' }).id
3990
3991        return { diagnostics_qf_id, qf_id }
3992      end)
3993
3994      eq(result[1], result[2])
3995    end)
3996
3997    it('navigates to existing diagnostics quickfix if one already exists and open=true', function()
3998      local result = exec_lua(function()
3999        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
4000
4001        vim.fn.setqflist({}, ' ', { title = 'Diagnostics' })
4002        local diagnostics_qf_id = vim.fn.getqflist({ id = 0 }).id
4003
4004        vim.fn.setqflist({}, ' ', { title = 'Other' })
4005
4006        vim.diagnostic.setqflist({ title = 'Diagnostics', open = true })
4007        local qf_id = vim.fn.getqflist({ id = 0 }).id
4008
4009        return { diagnostics_qf_id, qf_id }
4010      end)
4011
4012      eq(result[1], result[2])
4013    end)
4014
4015    it('sets new diagnostics quickfix as active when open=true', function()
4016      local result = exec_lua(function()
4017        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
4018
4019        vim.fn.setqflist({}, ' ', { title = 'Other' })
4020        local other_qf_id = vim.fn.getqflist({ id = 0 }).id
4021
4022        vim.diagnostic.setqflist({ title = 'Diagnostics', open = true })
4023        local qf_id = vim.fn.getqflist({ id = 0 }).id
4024
4025        return { other_qf_id, qf_id }
4026      end)
4027
4028      neq(result[1], result[2])
4029    end)
4030
4031    it('opens quickfix window when open=true', function()
4032      local qf_winid = exec_lua(function()
4033        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
4034
4035        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
4036          _G.make_error('Error', 1, 1, 1, 1),
4037        })
4038
4039        vim.diagnostic.setqflist({ open = true })
4040
4041        return vim.fn.getqflist({ winid = 0 }).winid
4042      end)
4043
4044      neq(0, qf_winid)
4045    end)
4046
4047    it('supports format function', function()
4048      local qf_list = exec_lua(function()
4049        vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
4050
4051        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
4052          _G.make_error('Error', 1, 1, 1, 1, 'foo_ls'),
4053          _G.make_error('Another error', 2, 2, 2, 2, 'foo_ls'),
4054        })
4055
4056        vim.diagnostic.setqflist({
4057          format = function(diagnostic)
4058            if diagnostic.lnum > 1 then
4059              return nil
4060            end
4061
4062            return string.format('%s: %s', diagnostic.source, diagnostic.message)
4063          end,
4064        })
4065
4066        return vim.fn.getqflist()
4067      end)
4068
4069      eq(1, #qf_list)
4070      eq('foo_ls: Error', qf_list[1].text)
4071    end)
4072  end)
4073
4074  describe('match()', function()
4075    it('matches a string', function()
4076      local msg = 'ERROR: george.txt:19:84:Two plus two equals five'
4077      local diagnostic = {
4078        severity = exec_lua [[return vim.diagnostic.severity.ERROR]],
4079        lnum = 18,
4080        col = 83,
4081        end_lnum = 18,
4082        end_col = 83,
4083        message = 'Two plus two equals five',
4084      }
4085      eq(
4086        diagnostic,
4087        exec_lua(function()
4088          return vim.diagnostic.match(
4089            msg,
4090            '^(%w+): [^:]+:(%d+):(%d+):(.+)$',
4091            { 'severity', 'lnum', 'col', 'message' }
4092          )
4093        end)
4094      )
4095    end)
4096
4097    it('returns nil if the pattern fails to match', function()
4098      eq(
4099        nil,
4100        exec_lua(function()
4101          local msg = 'The answer to life, the universe, and everything is'
4102          return vim.diagnostic.match(msg, 'This definitely will not match', {})
4103        end)
4104      )
4105    end)
4106
4107    it('respects default values', function()
4108      local msg = 'anna.txt:1:Happy families are all alike'
4109      local diagnostic = {
4110        severity = exec_lua [[return vim.diagnostic.severity.INFO]],
4111        lnum = 0,
4112        col = 0,
4113        end_lnum = 0,
4114        end_col = 0,
4115        message = 'Happy families are all alike',
4116      }
4117      eq(
4118        diagnostic,
4119        exec_lua(function()
4120          return vim.diagnostic.match(
4121            msg,
4122            '^[^:]+:(%d+):(.+)$',
4123            { 'lnum', 'message' },
4124            nil,
4125            { severity = vim.diagnostic.severity.INFO }
4126          )
4127        end)
4128      )
4129    end)
4130
4131    it('accepts a severity map', function()
4132      local msg = '46:FATAL:Et tu, Brute?'
4133      local diagnostic = {
4134        severity = exec_lua [[return vim.diagnostic.severity.ERROR]],
4135        lnum = 45,
4136        col = 0,
4137        end_lnum = 45,
4138        end_col = 0,
4139        message = 'Et tu, Brute?',
4140      }
4141      eq(
4142        diagnostic,
4143        exec_lua(function()
4144          return vim.diagnostic.match(
4145            msg,
4146            '^(%d+):(%w+):(.+)$',
4147            { 'lnum', 'severity', 'message' },
4148            { FATAL = vim.diagnostic.severity.ERROR }
4149          )
4150        end)
4151      )
4152    end)
4153  end)
4154
4155  describe('toqflist(), fromqflist()', function()
4156    it('works', function()
4157      local result = exec_lua(function()
4158        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
4159          _G.make_error('Error 1', 0, 1, 0, 1),
4160          _G.make_error('Error 2', 1, 1, 1, 1),
4161          _G.make_warning('Warning', 2, 2, 2, 2),
4162        })
4163
4164        local diagnostics = vim.diagnostic.get(_G.diagnostic_bufnr)
4165        vim.fn.setqflist(vim.diagnostic.toqflist(diagnostics))
4166        local list = vim.fn.getqflist()
4167        local new_diagnostics = vim.diagnostic.fromqflist(list)
4168
4169        -- Remove extra properties not present in the return value of fromlist()
4170        for _, v in ipairs(diagnostics) do
4171          v._extmark_id = nil
4172          v.namespace = nil
4173        end
4174
4175        return { diagnostics, new_diagnostics }
4176      end)
4177      eq(result[1], result[2])
4178    end)
4179
4180    it('uses the start position when quickfix coordinates are absent', function()
4181      eq(
4182        {
4183          {
4184            lnum = 0,
4185            col = 0,
4186            end_lnum = 0,
4187            end_col = 0,
4188            severity = vim.diagnostic.severity.ERROR,
4189            message = 'missing coordinates',
4190          },
4191          {
4192            lnum = 3,
4193            col = 2,
4194            end_lnum = 3,
4195            end_col = 2,
4196            severity = vim.diagnostic.severity.ERROR,
4197            message = 'missing end coordinates',
4198          },
4199        },
4200        exec_lua(function()
4201          return vim.diagnostic.fromqflist({
4202            { valid = 1, nr = 0, text = 'missing coordinates' },
4203            { valid = 1, nr = 0, lnum = 4, col = 3, text = 'missing end coordinates' },
4204          })
4205        end)
4206      )
4207    end)
4208
4209    it('merge_lines=true merges continuation lines', function()
4210      local function get_fromqflist(merge_lines)
4211        return exec_lua(function(merge_lines_)
4212          local qflist = {
4213            {
4214              bufnr = 1,
4215              lnum = 10,
4216              col = 5,
4217              end_lnum = 10,
4218              end_col = 10,
4219              text = 'error: [GHC-83865]',
4220              type = 'E',
4221              nr = 0,
4222              valid = 1,
4223            },
4224            {
4225              bufnr = 1,
4226              lnum = 0,
4227              col = 0,
4228              end_lnum = 0,
4229              end_col = 0,
4230              text = "    Couldn't match expected type",
4231              type = '',
4232              nr = 0,
4233              valid = 0,
4234            },
4235            {
4236              bufnr = 1,
4237              lnum = 0,
4238              col = 0,
4239              end_lnum = 0,
4240              end_col = 0,
4241              text = '    with actual type',
4242              type = '',
4243              nr = 0,
4244              valid = 0,
4245            },
4246            {
4247              bufnr = 1,
4248              lnum = 20,
4249              col = 1,
4250              end_lnum = 20,
4251              end_col = 5,
4252              text = 'warning: unused',
4253              type = 'W',
4254              nr = 0,
4255              valid = 1,
4256            },
4257          }
4258          return vim.diagnostic.fromqflist(qflist, { merge_lines = merge_lines_ })
4259        end, merge_lines)
4260      end
4261
4262      -- merge_lines=true
4263      local result = get_fromqflist(true)
4264      eq(2, #result)
4265      eq(
4266        "error: [GHC-83865]\n    Couldn't match expected type\n    with actual type",
4267        result[1].message
4268      )
4269      eq('warning: unused', result[2].message)
4270
4271      -- merge_lines=false
4272      result = get_fromqflist(false)
4273      eq(2, #result)
4274      eq('error: [GHC-83865]', result[1].message)
4275    end)
4276  end)
4277
4278  describe('status()', function()
4279    it('returns empty string if no diagnostics', function()
4280      local result = exec_lua(function()
4281        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {})
4282        return vim.diagnostic.status()
4283      end)
4284
4285      eq('', result)
4286    end)
4287
4288    it('returns count for each diagnostic kind', function()
4289      local result = exec_lua(function()
4290        vim.diagnostic.set(_G.diagnostic_ns, 0, {
4291          _G.make_error('Error 1', 0, 1, 0, 1),
4292
4293          _G.make_warning('Warning 1', 2, 2, 2, 2),
4294          _G.make_warning('Warning 2', 2, 2, 2, 2),
4295
4296          _G.make_info('Info 1', 3, 3, 3, 3),
4297          _G.make_info('Info 2', 3, 3, 3, 3),
4298          _G.make_info('Info 3', 3, 3, 3, 3),
4299
4300          _G.make_hint('Hint 1', 4, 4, 4, 4),
4301          _G.make_hint('Hint 2', 4, 4, 4, 4),
4302          _G.make_hint('Hint 3', 4, 4, 4, 4),
4303          _G.make_hint('Hint 4', 4, 4, 4, 4),
4304        })
4305        return vim.diagnostic.status()
4306      end)
4307
4308      eq(
4309        '%#(%$DiagnosticSignError$E:1%#) %#(%$DiagnosticSignWarn$W:2%#) %#(%$DiagnosticSignInfo$I:3%#) %#(%$DiagnosticSignHint$H:4%#)',
4310        result
4311      )
4312
4313      exec_lua('vim.cmd.enew()')
4314
4315      -- Empty diagnostics for a buffer without diagnostics
4316      eq(
4317        '',
4318        exec_lua(function()
4319          return vim.diagnostic.status()
4320        end)
4321      )
4322    end)
4323
4324    it('uses text from diagnostic.config().signs.text[severity]', function()
4325      local result = exec_lua(function()
4326        vim.diagnostic.config({
4327          signs = {
4328            text = {
4329              [vim.diagnostic.severity.ERROR] = '⨯',
4330              [vim.diagnostic.severity.WARN] = '⚠︎',
4331            },
4332          },
4333        })
4334
4335        vim.diagnostic.set(_G.diagnostic_ns, 0, {
4336          _G.make_error('Error 1', 0, 1, 0, 1),
4337          _G.make_warning('Warning 1', 2, 2, 2, 2),
4338        })
4339
4340        return vim.diagnostic.status()
4341      end)
4342
4343      eq('%#(%$DiagnosticSignError$⨯:1%#) %#(%$DiagnosticSignWarn$⚠︎:1%#)', result)
4344    end)
4345
4346    it('works when signs are disabled', function()
4347      local result = exec_lua(function()
4348        vim.diagnostic.config({ signs = false })
4349
4350        vim.diagnostic.set(_G.diagnostic_ns, 0, {
4351          _G.make_error('Error 1', 0, 1, 0, 1),
4352          _G.make_warning('Warning 1', 2, 2, 2, 2),
4353        })
4354
4355        return vim.diagnostic.status()
4356      end)
4357
4358      eq('%#(%$DiagnosticSignError$E:1%#) %#(%$DiagnosticSignWarn$W:1%#)', result)
4359    end)
4360
4361    it('uses format function diagnostic.config().status.format', function()
4362      local result = exec_lua(function()
4363        local signs = {
4364          [vim.diagnostic.severity.ERROR] = 'EE',
4365          [vim.diagnostic.severity.WARN] = 'WW',
4366          [vim.diagnostic.severity.INFO] = 'II',
4367          [vim.diagnostic.severity.HINT] = 'HH',
4368        }
4369        local hl_map = {
4370          [vim.diagnostic.severity.ERROR] = 'ERROR',
4371          [vim.diagnostic.severity.WARN] = 'WARN',
4372          [vim.diagnostic.severity.INFO] = 'INFO',
4373          [vim.diagnostic.severity.HINT] = 'HINT',
4374        }
4375        vim.diagnostic.config({
4376          status = {
4377            format = function(counts)
4378              local items = {}
4379              for severity, sign in ipairs(signs) do
4380                local count = counts[severity] or 0
4381                local hl = hl_map[severity]
4382                table.insert(items, ('%%#%s#%s %s'):format(hl, sign, count))
4383              end
4384              return table.concat(items, ' ')
4385            end,
4386          },
4387        })
4388
4389        vim.diagnostic.set(_G.diagnostic_ns, 0, {
4390          _G.make_error('Error 1', 0, 1, 0, 1),
4391          _G.make_warning('Warning 1', 2, 2, 2, 2),
4392        })
4393
4394        return vim.diagnostic.status()
4395      end)
4396
4397      eq('%#(%#ERROR#EE 1 %#WARN#WW 1 %#INFO#II 0 %#HINT#HH 0%#)', result)
4398    end)
4399  end)
4400
4401  describe('handlers', function()
4402    it('checks that a new handler is a table', function()
4403      matches(
4404        [[.*handler: expected table, got string.*]],
4405        pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = "bar" ]])
4406      )
4407      matches(
4408        [[.*handler: expected table, got function.*]],
4409        pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = function() end ]])
4410      )
4411    end)
4412
4413    it('can add new handlers', function()
4414      eq(
4415        true,
4416        exec_lua(function()
4417          local handler_called = false
4418          vim.diagnostic.handlers.test = {
4419            show = function(namespace, bufnr, diagnostics, opts)
4420              assert(namespace == _G.diagnostic_ns)
4421              assert(bufnr == _G.diagnostic_bufnr)
4422              assert(#diagnostics == 1)
4423              assert(opts.test.some_opt == 42)
4424              handler_called = true
4425            end,
4426          }
4427
4428          vim.diagnostic.config({ test = { some_opt = 42 } })
4429          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
4430            _G.make_warning('Warning', 0, 0, 0, 0),
4431          })
4432          return handler_called
4433        end)
4434      )
4435    end)
4436
4437    it('can disable handlers by setting the corresponding option to false', function()
4438      eq(
4439        false,
4440        exec_lua(function()
4441          local handler_called = false
4442          vim.diagnostic.handlers.test = {
4443            show = function(_, _, _, _)
4444              handler_called = true
4445            end,
4446          }
4447
4448          vim.diagnostic.config({ test = false })
4449          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
4450            _G.make_warning('Warning', 0, 0, 0, 0),
4451          })
4452          return handler_called
4453        end)
4454      )
4455    end)
4456
4457    it("always calls a handler's hide function if defined", function()
4458      eq(
4459        { false, true },
4460        exec_lua(function()
4461          local hide_called = false
4462          local show_called = false
4463          vim.diagnostic.handlers.test = {
4464            show = function(_, _, _, _)
4465              show_called = true
4466            end,
4467            hide = function(namespace, bufnr)
4468              assert(namespace == _G.diagnostic_ns)
4469              assert(bufnr == _G.diagnostic_bufnr)
4470              hide_called = true
4471            end,
4472          }
4473
4474          vim.diagnostic.config({ test = false })
4475          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
4476            _G.make_warning('Warning', 0, 0, 0, 0),
4477          })
4478          vim.diagnostic.hide(_G.diagnostic_ns, _G.diagnostic_bufnr)
4479          return { show_called, hide_called }
4480        end)
4481      )
4482    end)
4483
4484    it('triggers the autocommand when diagnostics are set', function()
4485      eq(
4486        { true, true },
4487        exec_lua(function()
4488          -- Set a different buffer as current to test that <abuf> is being set properly in
4489          -- DiagnosticChanged callbacks
4490          local tmp = vim.api.nvim_create_buf(false, true)
4491          vim.api.nvim_set_current_buf(tmp)
4492
4493          local triggered = {}
4494          vim.api.nvim_create_autocmd('DiagnosticChanged', {
4495            callback = function(ev)
4496              triggered = { ev.buf, #ev.data.diagnostics }
4497            end,
4498          })
4499          vim.api.nvim_buf_set_name(_G.diagnostic_bufnr, 'test | test')
4500          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
4501            _G.make_error('Diagnostic', 0, 0, 0, 0),
4502          })
4503          return {
4504            triggered[1] == _G.diagnostic_bufnr,
4505            triggered[2] == 1,
4506          }
4507        end)
4508      )
4509    end)
4510
4511    it('triggers the autocommand when diagnostics are cleared', function()
4512      eq(
4513        true,
4514        exec_lua(function()
4515          local tmp = vim.api.nvim_create_buf(false, true)
4516          vim.api.nvim_set_current_buf(tmp)
4517          vim.g.diagnostic_autocmd_triggered = 0
4518          vim.cmd(
4519            'autocmd DiagnosticChanged * let g:diagnostic_autocmd_triggered = +expand("<abuf>")'
4520          )
4521          vim.api.nvim_buf_set_name(_G.diagnostic_bufnr, 'test | test')
4522          vim.diagnostic.reset(_G.diagnostic_ns, _G.diagnostic_bufnr)
4523          return vim.g.diagnostic_autocmd_triggered == _G.diagnostic_bufnr
4524        end)
4525      )
4526    end)
4527
4528    it('is_enabled', function()
4529      eq(
4530        { false, false, false, false, false },
4531        exec_lua(function()
4532          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
4533            _G.make_error('Diagnostic #1', 1, 1, 1, 1),
4534          })
4535          vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
4536          vim.diagnostic.enable(false)
4537          return {
4538            vim.diagnostic.is_enabled(),
4539            vim.diagnostic.is_enabled { bufnr = 0 },
4540            vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr },
4541            vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns },
4542            vim.diagnostic.is_enabled { bufnr = 0, ns_id = _G.diagnostic_ns },
4543          }
4544        end)
4545      )
4546
4547      eq(
4548        { true, true, true, true, true },
4549        exec_lua(function()
4550          vim.diagnostic.enable()
4551          return {
4552            vim.diagnostic.is_enabled(),
4553            vim.diagnostic.is_enabled { bufnr = 0 },
4554            vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr },
4555            vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns },
4556            vim.diagnostic.is_enabled { bufnr = 0, ns_id = _G.diagnostic_ns },
4557          }
4558        end)
4559      )
4560    end)
4561
4562    it('does not redraw for buffer not in window', function()
4563      local did_status = exec_lua(function()
4564        _G.Status = function()
4565          _G.did_status = (_G.did_status or 0) + 1
4566        end
4567        vim.o.laststatus, vim.o.statusline = 2, '%!v:lua._G.Status()'
4568        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
4569          _G.make_error('Diagnostic #1', 1, 1, 1, 1),
4570        })
4571        return _G.did_status
4572      end)
4573      eq(nil, did_status)
4574    end)
4575  end)
4576end)