robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
28.0 KiB988 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local t_lsp = require('test.functional.plugin.lsp.testutil')
5
6local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
7local clear = n.clear
8local exec_lua = n.exec_lua
9local eq = t.eq
10local neq = t.neq
11local matches = t.matches
12local pcall_err = t.pcall_err
13
14local create_server_definition = t_lsp.create_server_definition
15
16describe('vim.lsp.diagnostic', function()
17  local fake_uri --- @type string
18  local client_id --- @type integer
19  local diagnostic_bufnr --- @type integer
20
21  before_each(function()
22    clear { env = {
23      NVIM_LUA_NOTRACK = '1',
24      VIMRUNTIME = os.getenv 'VIMRUNTIME',
25    } }
26
27    exec_lua(function()
28      require('vim.lsp')
29
30      _G.make_range = function(x1, y1, x2, y2)
31        return { start = { line = x1, character = y1 }, ['end'] = { line = x2, character = y2 } }
32      end
33
34      _G.make_error = function(msg, x1, y1, x2, y2)
35        return {
36          range = _G.make_range(x1, y1, x2, y2),
37          message = msg,
38          severity = 1,
39        }
40      end
41
42      _G.make_warning = function(msg, x1, y1, x2, y2)
43        return {
44          range = _G.make_range(x1, y1, x2, y2),
45          message = msg,
46          severity = 2,
47        }
48      end
49
50      _G.make_information = function(msg, x1, y1, x2, y2)
51        return {
52          range = _G.make_range(x1, y1, x2, y2),
53          message = msg,
54          severity = 3,
55        }
56      end
57
58      function _G.get_extmarks(bufnr, client_id0)
59        local namespace = vim.lsp.diagnostic.get_namespace(client_id0)
60        local ns = vim.diagnostic.get_namespace(namespace)
61        local extmarks = {}
62        if ns.user_data.virt_text_ns then
63          for _, e in
64            pairs(
65              vim.api.nvim_buf_get_extmarks(
66                bufnr,
67                ns.user_data.virt_text_ns,
68                0,
69                -1,
70                { details = true }
71              )
72            )
73          do
74            table.insert(extmarks, e)
75          end
76        end
77        if ns.user_data.underline_ns then
78          for _, e in
79            pairs(
80              vim.api.nvim_buf_get_extmarks(
81                bufnr,
82                ns.user_data.underline_ns,
83                0,
84                -1,
85                { details = true }
86              )
87            )
88          do
89            table.insert(extmarks, e)
90          end
91        end
92        return extmarks
93      end
94    end)
95
96    fake_uri = 'file:///fake/uri'
97
98    exec_lua(function()
99      diagnostic_bufnr = vim.uri_to_bufnr(fake_uri)
100      local lines = { '1st line', '2nd line of text', 'wow', 'cool', 'more', 'lines' }
101      vim.fn.bufload(diagnostic_bufnr)
102      vim.api.nvim_buf_set_lines(diagnostic_bufnr, 0, 1, false, lines)
103      vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
104    end)
105  end)
106
107  describe('vim.lsp.diagnostic.on_publish_diagnostics', function()
108    before_each(function()
109      exec_lua(create_server_definition)
110      exec_lua(function()
111        _G.requests = 0
112        _G.server = _G._create_server({
113          capabilities = {
114            diagnosticProvider = {},
115          },
116        })
117
118        client_id = assert(vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd }))
119      end)
120    end)
121
122    after_each(function()
123      exec_lua(function()
124        local client = vim.lsp.get_client_by_id(client_id)
125        if client then
126          client:stop()
127        end
128
129        vim.api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
130      end)
131    end)
132
133    it('correctly handles UTF-16 offsets', function()
134      local line = 'All 💼 and no 🎉 makes Jack a dull 👦'
135      local result = exec_lua(function()
136        vim.api.nvim_buf_set_lines(diagnostic_bufnr, 0, -1, false, { line })
137
138        vim.lsp.diagnostic.on_publish_diagnostics(nil, {
139          uri = fake_uri,
140          diagnostics = {
141            _G.make_error('UTF-16 Diagnostic', 0, 7, 0, 8),
142          },
143        }, { client_id = client_id })
144
145        local diags = vim.diagnostic.get(diagnostic_bufnr)
146        return diags
147      end)
148      eq(1, #result)
149      eq(
150        exec_lua(function()
151          return vim.str_byteindex(line, 'utf-16', 7)
152        end),
153        result[1].col
154      )
155      eq(
156        exec_lua(function()
157          return vim.str_byteindex(line, 'utf-16', 8)
158        end),
159        result[1].end_col
160      )
161    end)
162
163    it('does not create buffer on empty diagnostics', function()
164      -- No buffer is created without diagnostics
165      eq(
166        -1,
167        exec_lua(function()
168          vim.lsp.diagnostic.on_publish_diagnostics(nil, {
169            uri = 'file:///fake/uri2',
170            diagnostics = {},
171          }, { client_id = client_id })
172          return vim.fn.bufnr(vim.uri_to_fname('file:///fake/uri2'))
173        end)
174      )
175
176      -- Create buffer on diagnostics
177      neq(
178        -1,
179        exec_lua(function()
180          vim.lsp.diagnostic.on_publish_diagnostics(nil, {
181            uri = 'file:///fake/uri2',
182            diagnostics = {
183              _G.make_error('Diagnostic', 0, 0, 0, 0),
184            },
185          }, { client_id = client_id })
186          return vim.fn.bufnr(vim.uri_to_fname('file:///fake/uri2'))
187        end)
188      )
189      eq(
190        1,
191        exec_lua(function()
192          return #vim.diagnostic.get(_G.bufnr)
193        end)
194      )
195
196      -- Clear diagnostics after buffer was created
197      neq(
198        -1,
199        exec_lua(function()
200          vim.lsp.diagnostic.on_publish_diagnostics(nil, {
201            uri = 'file:///fake/uri2',
202            diagnostics = {},
203          }, { client_id = client_id })
204          return vim.fn.bufnr(vim.uri_to_fname('file:///fake/uri2'))
205        end)
206      )
207      eq(
208        0,
209        exec_lua(function()
210          return #vim.diagnostic.get(_G.bufnr)
211        end)
212      )
213    end)
214
215    it('clears diagnostics for the client namespace on empty publish', function()
216      local before_clear, after_clear = exec_lua(function()
217        local ns = vim.lsp.diagnostic.get_namespace(client_id)
218
219        -- Publish diagnostics
220        vim.lsp.diagnostic.on_publish_diagnostics(nil, {
221          uri = fake_uri,
222          diagnostics = {
223            _G.make_error('Diagnostic', 0, 0, 0, 0),
224          },
225        }, { client_id = client_id })
226
227        local before_clear = vim.diagnostic.get(diagnostic_bufnr, { namespace = ns })
228
229        -- Publish empty diagnostics
230        vim.lsp.diagnostic.on_publish_diagnostics(nil, {
231          uri = fake_uri,
232          diagnostics = {},
233        }, { client_id = client_id })
234
235        local after_clear = vim.diagnostic.get(diagnostic_bufnr, { namespace = ns })
236
237        return before_clear, after_clear
238      end)
239
240      eq(1, #before_clear)
241      eq(0, #after_clear)
242    end)
243
244    it('clears diagnostics when buffer is deleted', function()
245      local before_delete, after_delete = exec_lua(function()
246        local ns = vim.lsp.diagnostic.get_namespace(client_id)
247
248        -- Publish diagnostics
249        vim.lsp.diagnostic.on_publish_diagnostics(nil, {
250          uri = fake_uri,
251          diagnostics = {
252            _G.make_error('Diagnostic', 0, 0, 0, 0),
253          },
254        }, { client_id = client_id })
255
256        local before_delete = vim.diagnostic.get(diagnostic_bufnr, { namespace = ns })
257
258        -- Avoid deleting the currently displayed buffer (Windows teardown edge case)
259        local scratch = vim.api.nvim_create_buf(false, true)
260        vim.api.nvim_win_set_buf(0, scratch)
261
262        -- Delete the buffer deterministically
263        vim.api.nvim_buf_delete(diagnostic_bufnr, { force = true })
264
265        -- Query remaining diagnostics via valid access path
266        -- (deleted buffer cannot be queried directly)
267        local after_delete = vim.diagnostic.get(nil, { namespace = ns })
268
269        return before_delete, after_delete
270      end)
271
272      eq(1, #before_delete)
273      eq(0, #after_delete)
274    end)
275
276    it('clears diagnostics when client is detached', function()
277      exec_lua(function()
278        vim.lsp.diagnostic.on_publish_diagnostics(nil, {
279          uri = fake_uri,
280          diagnostics = {
281            _G.make_error('Diagnostic', 0, 0, 0, 0),
282          },
283        }, { client_id = client_id })
284      end)
285
286      eq(
287        1,
288        exec_lua(function()
289          local ns = vim.lsp.diagnostic.get_namespace(client_id)
290          return #vim.diagnostic.get(diagnostic_bufnr, { namespace = ns })
291        end)
292      )
293
294      exec_lua(function()
295        vim.lsp.get_client_by_id(client_id):stop()
296      end)
297
298      eq(
299        0,
300        exec_lua(function()
301          local ns = vim.lsp.diagnostic.get_namespace(client_id)
302          return #vim.diagnostic.get(diagnostic_bufnr, { namespace = ns })
303        end)
304      )
305    end)
306
307    it('errors when diagnostic tags is null', function()
308      matches(
309        'server response has invalid %(null%) tags',
310        pcall_err(exec_lua, function()
311          vim.lsp.diagnostic.on_publish_diagnostics(nil, {
312            uri = fake_uri,
313            diagnostics = {
314              vim.tbl_extend('force', _G.make_error('Diagnostic', 0, 0, 0, 0), { tags = vim.NIL }),
315            },
316          }, { client_id = client_id })
317        end)
318      )
319    end)
320  end)
321
322  describe('vim.lsp.diagnostic.on_diagnostic', function()
323    before_each(function()
324      exec_lua(create_server_definition)
325      exec_lua(function()
326        _G.requests = 0
327        _G.server = _G._create_server({
328          capabilities = {
329            diagnosticProvider = {
330              documentSelector = vim.NIL,
331              interFileDependencies = false,
332              workspaceDiagnostics = false,
333            },
334          },
335          handlers = {
336            ['textDocument/diagnostic'] = function(_, params)
337              _G.params = params
338              _G.requests = _G.requests + 1
339            end,
340          },
341        })
342
343        function _G.get_extmarks(bufnr, client_id0)
344          local namespace = vim.lsp.diagnostic.get_namespace(client_id0, true)
345          local ns = vim.diagnostic.get_namespace(namespace)
346          local extmarks = {}
347          if ns.user_data.virt_text_ns then
348            for _, e in
349              pairs(
350                vim.api.nvim_buf_get_extmarks(
351                  bufnr,
352                  ns.user_data.virt_text_ns,
353                  0,
354                  -1,
355                  { details = true }
356                )
357              )
358            do
359              table.insert(extmarks, e)
360            end
361          end
362          if ns.user_data.underline_ns then
363            for _, e in
364              pairs(
365                vim.api.nvim_buf_get_extmarks(
366                  bufnr,
367                  ns.user_data.underline_ns,
368                  0,
369                  -1,
370                  { details = true }
371                )
372              )
373            do
374              table.insert(extmarks, e)
375            end
376          end
377          return extmarks
378        end
379
380        client_id = assert(vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd }))
381      end)
382    end)
383
384    it('adds diagnostics to vim.diagnostics', function()
385      local diags = exec_lua(function()
386        vim.lsp.diagnostic.on_diagnostic(nil, {
387          kind = 'full',
388          items = {
389            _G.make_error('Pull Diagnostic', 4, 4, 4, 4),
390          },
391        }, {
392          params = {
393            textDocument = { uri = fake_uri },
394          },
395          uri = fake_uri,
396          client_id = client_id,
397          bufnr = diagnostic_bufnr,
398        }, {})
399
400        return vim.diagnostic.get(diagnostic_bufnr)
401      end)
402      eq(1, #diags)
403      eq('Pull Diagnostic', diags[1].message)
404    end)
405
406    it('preserves push diagnostics when pull diagnostics are empty', function()
407      local push_ns_count, pull_ns_count, all_diags_count, push_ns, pull_ns = exec_lua(function()
408        vim.lsp.diagnostic.on_publish_diagnostics(nil, {
409          uri = fake_uri,
410          diagnostics = {
411            _G.make_error('Push Diagnostic', 0, 0, 0, 0),
412          },
413        }, { client_id = client_id })
414
415        vim.lsp.diagnostic.on_diagnostic(nil, {
416          kind = 'full',
417          items = {},
418        }, {
419          params = {
420            textDocument = { uri = fake_uri },
421          },
422          uri = fake_uri,
423          client_id = client_id,
424          bufnr = diagnostic_bufnr,
425        }, {})
426
427        local push_ns = vim.lsp.diagnostic.get_namespace(client_id, false)
428        local pull_ns = vim.lsp.diagnostic.get_namespace(client_id, true)
429
430        return #vim.diagnostic.get(diagnostic_bufnr, { namespace = push_ns }),
431          #vim.diagnostic.get(diagnostic_bufnr, { namespace = pull_ns }),
432          #vim.diagnostic.get(diagnostic_bufnr),
433          push_ns,
434          pull_ns
435      end)
436
437      eq(1, push_ns_count)
438      eq(0, pull_ns_count)
439      eq(1, all_diags_count)
440      neq(push_ns, pull_ns)
441    end)
442
443    it('uses pull_id to isolate pull diagnostic namespaces', function()
444      local first_count, second_count, total_count, first_ns, second_ns = exec_lua(function()
445        vim.lsp.diagnostic.on_diagnostic(nil, {
446          kind = 'full',
447          items = {
448            _G.make_error('Pull Diagnostic A', 0, 0, 0, 0),
449          },
450        }, {
451          params = {
452            identifier = 'provider-a',
453            textDocument = { uri = fake_uri },
454          },
455          uri = fake_uri,
456          client_id = client_id,
457          bufnr = diagnostic_bufnr,
458        }, {})
459
460        vim.lsp.diagnostic.on_diagnostic(nil, {
461          kind = 'full',
462          items = {},
463        }, {
464          params = {
465            identifier = 'provider-b',
466            textDocument = { uri = fake_uri },
467          },
468          uri = fake_uri,
469          client_id = client_id,
470          bufnr = diagnostic_bufnr,
471        }, {})
472
473        local first_ns = vim.lsp.diagnostic.get_namespace(client_id, true, 'provider-a')
474        local second_ns = vim.lsp.diagnostic.get_namespace(client_id, true, 'provider-b')
475
476        return #vim.diagnostic.get(diagnostic_bufnr, { namespace = first_ns }),
477          #vim.diagnostic.get(diagnostic_bufnr, { namespace = second_ns }),
478          #vim.diagnostic.get(diagnostic_bufnr),
479          first_ns,
480          second_ns
481      end)
482
483      eq(1, first_count)
484      eq(0, second_count)
485      eq(1, total_count)
486      neq(first_ns, second_ns)
487    end)
488
489    it('handles multiline diagnostic ranges #33782', function()
490      local diags = exec_lua(function()
491        vim.lsp.diagnostic.on_diagnostic(nil, {
492          kind = 'full',
493          items = {
494            _G.make_error('Pull Diagnostic', 0, 6, 1, 10),
495          },
496        }, {
497          params = {
498            textDocument = { uri = fake_uri },
499          },
500          uri = fake_uri,
501          client_id = client_id,
502          bufnr = diagnostic_bufnr,
503        }, {})
504
505        return vim.diagnostic.get(diagnostic_bufnr)
506      end)
507      local lines = exec_lua(function()
508        return vim.api.nvim_buf_get_lines(diagnostic_bufnr, 0, -1, false)
509      end)
510      -- This test case must be run over a multiline diagnostic in which the start line is shorter
511      -- than the end line, and the end_col exceeds the start line's length.
512      eq(8, #lines[1])
513      eq(16, #lines[2])
514      eq(1, #diags)
515      eq(6, diags[1].col)
516      eq(10, diags[1].end_col)
517    end)
518
519    it('severity defaults to error if missing', function()
520      ---@type vim.Diagnostic[]
521      local diagnostics = exec_lua(function()
522        vim.lsp.diagnostic.on_diagnostic(nil, {
523          kind = 'full',
524          items = {
525            {
526              range = _G.make_range(4, 4, 4, 4),
527              message = 'bad!',
528            },
529          },
530        }, {
531          params = {
532            textDocument = { uri = fake_uri },
533          },
534          uri = fake_uri,
535          client_id = client_id,
536          bufnr = diagnostic_bufnr,
537        }, {})
538        return vim.diagnostic.get(diagnostic_bufnr)
539      end)
540      eq(1, #diagnostics)
541      eq(1, diagnostics[1].severity)
542    end)
543
544    it('clears pull diagnostics when client detaches', function()
545      exec_lua(function()
546        vim.lsp.diagnostic.on_diagnostic(nil, {
547          kind = 'full',
548          items = {
549            _G.make_error('Pull Diagnostic', 4, 4, 4, 4),
550          },
551        }, {
552          params = {
553            textDocument = { uri = fake_uri },
554          },
555          uri = fake_uri,
556          client_id = client_id,
557          bufnr = diagnostic_bufnr,
558        }, {})
559      end)
560
561      eq(
562        1,
563        exec_lua(function()
564          return #vim.diagnostic.get(diagnostic_bufnr)
565        end)
566      )
567
568      exec_lua(function()
569        vim.lsp.get_client_by_id(client_id):stop()
570      end)
571
572      eq(
573        0,
574        exec_lua(function()
575          return #vim.diagnostic.get(diagnostic_bufnr)
576        end)
577      )
578    end)
579
580    it('keeps diagnostics when one client detaches and others still are attached', function()
581      exec_lua(function()
582        _G.client_id2 = assert(vim.lsp.start({ name = 'dummy2', cmd = _G.server.cmd }))
583
584        vim.lsp.diagnostic.on_diagnostic(nil, {
585          kind = 'full',
586          items = {
587            _G.make_error('Pull Diagnostic', 4, 4, 4, 4),
588          },
589        }, {
590          params = {
591            textDocument = { uri = fake_uri },
592          },
593          uri = fake_uri,
594          client_id = client_id,
595          bufnr = diagnostic_bufnr,
596        }, {})
597      end)
598
599      eq(
600        1,
601        exec_lua(function()
602          return #vim.diagnostic.get(diagnostic_bufnr)
603        end)
604      )
605
606      exec_lua(function()
607        vim.lsp.get_client_by_id(_G.client_id2):stop()
608      end)
609
610      eq(
611        1,
612        exec_lua(function()
613          return #vim.diagnostic.get(diagnostic_bufnr)
614        end)
615      )
616    end)
617
618    it('handles server cancellation', function()
619      eq(
620        2,
621        exec_lua(function()
622          vim.lsp.diagnostic.on_diagnostic({
623            code = vim.lsp.protocol.ErrorCodes.ServerCancelled,
624            -- Empty data defaults to retriggering request
625            data = {},
626            message = '',
627          }, {}, {
628            method = 'textDocument/diagnostic',
629            client_id = client_id,
630            bufnr = diagnostic_bufnr,
631          })
632
633          return _G.requests
634        end)
635      )
636
637      eq(
638        3,
639        exec_lua(function()
640          vim.lsp.diagnostic.on_diagnostic({
641            code = vim.lsp.protocol.ErrorCodes.ServerCancelled,
642            data = { retriggerRequest = true },
643            message = '',
644          }, {}, {
645            method = 'textDocument/diagnostic',
646            client_id = client_id,
647            bufnr = diagnostic_bufnr,
648          })
649
650          return _G.requests
651        end)
652      )
653
654      eq(
655        3,
656        exec_lua(function()
657          vim.lsp.diagnostic.on_diagnostic({
658            code = vim.lsp.protocol.ErrorCodes.ServerCancelled,
659            data = { retriggerRequest = false },
660            message = '',
661          }, {}, {
662            method = 'textDocument/diagnostic',
663            client_id = client_id,
664            bufnr = diagnostic_bufnr,
665          })
666
667          return _G.requests
668        end)
669      )
670    end)
671
672    it('supports dynamic registration', function()
673      exec_lua(create_server_definition)
674      exec_lua(function()
675        _G.server2 = _G._create_server({
676          diagnosticProvider = {
677            documentSelector = vim.NIL,
678          },
679          handlers = {
680            ['textDocument/diagnostic'] = function(_, _, callback)
681              callback(nil, {
682                kind = 'full',
683                items = {
684                  _G.make_error('Dynamic Diagnostic', 4, 4, 4, 4),
685                },
686              })
687            end,
688          },
689        })
690
691        local client_id2 = assert(vim.lsp.start({ name = 'dummy2', cmd = _G.server2.cmd }))
692
693        vim.lsp.handlers['client/registerCapability'](nil, {
694          registrations = {
695            { id = 'diagnostic', method = 'textDocument/diagnostic' },
696          },
697        }, { client_id = client_id2, method = 'client/registerCapability' })
698      end)
699
700      eq(
701        1,
702        exec_lua(function()
703          return #vim.diagnostic.get(diagnostic_bufnr)
704        end)
705      )
706    end)
707
708    it('requests with the `previousResultId`', function()
709      -- Full reports
710      eq(
711        'dummy_server',
712        exec_lua(function()
713          vim.lsp.diagnostic.on_diagnostic(nil, {
714            kind = 'full',
715            resultId = 'dummy_server',
716            items = {
717              _G.make_error('Pull Diagnostic', 4, 4, 4, 4),
718            },
719          }, {
720            method = 'textDocument/diagnostic',
721            params = {
722              textDocument = { uri = fake_uri },
723            },
724            client_id = client_id,
725            bufnr = diagnostic_bufnr,
726          })
727          vim.api.nvim_exec_autocmds('LspNotify', {
728            buf = diagnostic_bufnr,
729            data = {
730              method = 'textDocument/didChange',
731              client_id = client_id,
732            },
733          })
734          return _G.params.previousResultId
735        end)
736      )
737
738      -- Unchanged reports
739      eq(
740        'squidward',
741        exec_lua(function()
742          vim.lsp.diagnostic.on_diagnostic(nil, {
743            kind = 'unchanged',
744            resultId = 'squidward',
745          }, {
746            method = 'textDocument/diagnostic',
747            params = {
748              textDocument = { uri = fake_uri },
749            },
750            client_id = client_id,
751            bufnr = diagnostic_bufnr,
752          })
753          vim.api.nvim_exec_autocmds('LspNotify', {
754            buf = diagnostic_bufnr,
755            data = {
756              method = 'textDocument/didChange',
757              client_id = client_id,
758            },
759          })
760          return _G.params.previousResultId
761        end)
762      )
763    end)
764
765    it('handles relatedDocuments diagnostics', function()
766      local fake_uri_2 = 'file:///fake/uri2'
767      ---@type vim.Diagnostic[], vim.Diagnostic[], string?
768      local diagnostics, related_diagnostics, relatedPreviousResultId = exec_lua(function()
769        local second_buf = vim.uri_to_bufnr(fake_uri_2)
770        vim.fn.bufload(second_buf)
771
772        -- Attach the client to both buffers.
773        vim.api.nvim_win_set_buf(0, second_buf)
774        vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
775
776        vim.lsp.diagnostic.on_diagnostic(nil, {
777          kind = 'full',
778          relatedDocuments = {
779            [fake_uri_2] = {
780              kind = 'full',
781              resultId = 'spongebob',
782              items = {
783                {
784                  range = _G.make_range(4, 4, 4, 4),
785                  message = 'related bad!',
786                },
787              },
788            },
789          },
790          items = {},
791        }, {
792          params = {
793            textDocument = { uri = fake_uri },
794          },
795          uri = fake_uri,
796          client_id = client_id,
797          bufnr = diagnostic_bufnr,
798        }, {})
799
800        vim.api.nvim_exec_autocmds('LspNotify', {
801          buf = second_buf,
802          data = {
803            method = 'textDocument/didChange',
804            client_id = client_id,
805          },
806        })
807
808        return vim.diagnostic.get(diagnostic_bufnr),
809          vim.diagnostic.get(second_buf),
810          _G.params.previousResultId
811      end)
812      eq(0, #diagnostics)
813      eq(1, #related_diagnostics)
814      eq('related bad!', related_diagnostics[1].message)
815      eq('spongebob', relatedPreviousResultId)
816    end)
817  end)
818
819  describe('vim.lsp.diagnostic.on_refresh', function()
820    it('refreshes diagnostics on server-to-client request', function()
821      exec_lua(create_server_definition)
822      exec_lua(function()
823        _G.requests = 0
824        _G.server = _G._create_server({
825          capabilities = {
826            diagnosticProvider = {
827              documentSelector = vim.NIL,
828              interFileDependencies = false,
829              workspaceDiagnostics = false,
830            },
831          },
832          handlers = {
833            ['textDocument/diagnostic'] = function(_, _, callback)
834              _G.requests = _G.requests + 1
835              callback(nil, {
836                kind = 'full',
837                items = {
838                  _G.make_warning('Pull Diagnostic', 4, 4, 4, 4),
839                },
840              })
841            end,
842          },
843        })
844        client_id = assert(vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd }))
845      end)
846
847      local diags = exec_lua(function()
848        vim.lsp.diagnostic.on_diagnostic(nil, {
849          kind = 'full',
850          items = {
851            _G.make_error('Pull Diagnostic', 4, 4, 4, 4),
852          },
853        }, {
854          params = {
855            textDocument = { uri = fake_uri },
856          },
857          uri = fake_uri,
858          client_id = client_id,
859          bufnr = diagnostic_bufnr,
860        }, {})
861
862        return vim.diagnostic.get(diagnostic_bufnr)
863      end)
864      eq(1, #diags)
865      eq(1, diags[1].severity)
866
867      local requests, refreshed_diags = exec_lua(function()
868        vim.lsp.diagnostic.on_refresh(nil, nil, {
869          method = 'workspace/diagnostic/refresh',
870          client_id = client_id,
871        })
872
873        return _G.requests, vim.diagnostic.get(diagnostic_bufnr)
874      end)
875      eq(2, requests)
876      eq(1, #refreshed_diags)
877      eq(2, refreshed_diags[1].severity)
878    end)
879
880    it('refreshes workspace diagnostics', function()
881      local fake_uri_3 = 'file:///fake/uri3'
882      exec_lua(create_server_definition)
883      exec_lua(function()
884        _G.workspace_requests = 0
885        _G.doc_requests = 0
886        _G.server = _G._create_server({
887          capabilities = {
888            diagnosticProvider = {
889              documentSelector = vim.NIL,
890              interFileDependencies = false,
891              workspaceDiagnostics = true,
892            },
893          },
894          handlers = {
895            ['textDocument/diagnostic'] = function(_, _, callback)
896              _G.doc_requests = _G.doc_requests + 1
897              callback(nil, {
898                kind = 'full',
899                items = {
900                  _G.make_warning('Refreshed Diagnostic', 4, 4, 4, 4),
901                },
902              })
903            end,
904            ['workspace/diagnostic'] = function(_, _, callback)
905              _G.workspace_requests = _G.workspace_requests + 1
906              callback(nil, {
907                items = {
908                  {
909                    kind = 'full',
910                    uri = fake_uri_3,
911                    items = {
912                      _G.make_error('Workspace Diagnostic', 4, 4, 4, 4),
913                    },
914                  },
915                },
916              })
917            end,
918          },
919        })
920        client_id = assert(vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd }))
921      end)
922
923      -- Seed the opened buffer with a document diagnostic so on_refresh must
924      -- re-request it even when workspace diagnostics are supported.
925      exec_lua(function()
926        vim.lsp.diagnostic.on_diagnostic(nil, {
927          kind = 'full',
928          items = {
929            _G.make_error('Initial Diagnostic', 4, 4, 4, 4),
930          },
931        }, {
932          params = {
933            textDocument = { uri = fake_uri },
934          },
935          uri = fake_uri,
936          client_id = client_id,
937          bufnr = diagnostic_bufnr,
938        }, {})
939      end)
940
941      eq(
942        1,
943        exec_lua(function()
944          return #vim.diagnostic.get()
945        end)
946      )
947
948      eq(
949        { vim.NIL },
950        exec_lua(function()
951          local client = vim.lsp.get_client_by_id(client_id)
952          assert(client)
953          local result = {}
954          client:_provider_foreach('workspace/diagnostic', function(cap)
955            table.insert(result, cap.identifier or vim.NIL)
956          end)
957          return result
958        end)
959      )
960
961      exec_lua(function()
962        _G.doc_requests = 0
963      end)
964
965      local workspace_requests, doc_requests, diags, opened_diags, workspace_diags = exec_lua(
966        function()
967          vim.lsp.diagnostic.on_refresh(nil, nil, {
968            method = 'workspace/diagnostic/refresh',
969            client_id = client_id,
970          })
971
972          return _G.workspace_requests,
973            _G.doc_requests,
974            vim.diagnostic.get(),
975            vim.diagnostic.get(diagnostic_bufnr),
976            vim.diagnostic.get(vim.uri_to_bufnr(fake_uri_3))
977        end
978      )
979      eq(1, workspace_requests)
980      eq(1, doc_requests)
981      eq(2, #diags)
982      eq(1, #opened_diags)
983      eq(2, opened_diags[1].severity)
984      eq(1, #workspace_diags)
985      eq(1, workspace_diags[1].severity)
986    end)
987  end)
988end)