robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
68.5 KiB2045 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
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 command = n.command
8local dedent = t.dedent
9local eq = t.eq
10local exec_lua = n.exec_lua
11local feed = n.feed
12local insert = n.insert
13local api = n.api
14
15local clear_notrace = t_lsp.clear_notrace
16local create_server_definition = t_lsp.create_server_definition
17
18local function create_start_server()
19  function _G._start_server(server)
20    return vim.lsp.start({
21      name = 'dummy',
22      cmd = server.cmd,
23      flags = {
24        allow_incremental_sync = false,
25        debounce_text_changes = 0, -- disable debounce to make tests faster
26      },
27    })
28  end
29end
30
31before_each(function()
32  clear_notrace()
33  exec_lua(create_server_definition)
34  exec_lua(create_start_server)
35  exec_lua(function()
36    vim.lsp.semantic_tokens.__STHighlighter.debounce = 0 -- disable internal debounce to make tests faster
37  end)
38end)
39
40after_each(function()
41  api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
42end)
43
44describe('semantic token highlighting', function()
45  local screen --- @type test.functional.ui.screen
46  before_each(function()
47    screen = Screen.new(40, 16)
48    screen:set_default_attr_ids {
49      [1] = { bold = true, foreground = Screen.colors.Blue1 },
50      [2] = { foreground = Screen.colors.DarkCyan },
51      [3] = { foreground = Screen.colors.SlateBlue },
52      [4] = { bold = true, foreground = Screen.colors.SeaGreen },
53      [5] = { foreground = tonumber('0x6a0dad') },
54      [6] = { foreground = Screen.colors.Blue1 },
55      [7] = { bold = true, foreground = Screen.colors.DarkCyan },
56      [8] = { bold = true, foreground = Screen.colors.SlateBlue },
57      [9] = { bold = true, foreground = tonumber('0x6a0dad') },
58      [10] = { bold = true, foreground = Screen.colors.Brown },
59      [11] = { foreground = Screen.colors.Magenta1 },
60    }
61    command([[ hi link @lsp.type.namespace Type ]])
62    command([[ hi link @lsp.type.function Special ]])
63    command([[ hi link @lsp.type.comment Comment ]])
64    command([[ hi @lsp.mod.declaration gui=bold ]])
65  end)
66
67  describe('general', function()
68    local text = dedent([[
69    #include <iostream>
70
71    int main()
72    {
73        int x;
74    #ifdef __cplusplus
75        std::cout << x << "\n";
76    #else
77        printf("%d\n", x);
78    #endif
79    }
80    }]])
81
82    local legend = [[{
83      "tokenTypes": [
84        "variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment"
85      ],
86      "tokenModifiers": [
87        "declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope"
88      ]
89    }]]
90
91    local response = [[{
92      "data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025, 1, 7, 11, 19, 8192, 1, 4, 3, 15, 8448, 0, 5, 4, 0, 8448, 0, 8, 1, 1, 1024, 1, 0, 5, 20, 0, 1, 0, 22, 20, 0, 1, 0, 6, 20, 0 ],
93      "resultId": "1"
94    }]]
95
96    local range_response = [[{
97      "data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025 ],
98      "resultId": "2"
99    }]]
100
101    local edit_response = [[{
102      "edits": [ {"data": [ 2, 8, 1, 3, 8193, 1, 7, 11, 19, 8192, 1, 4, 3, 15, 8448, 0, 5, 4, 0, 8448, 0, 8, 1, 3, 8192 ], "deleteCount": 25, "start": 5 } ],
103      "resultId": "3"
104    }]]
105
106    before_each(function()
107      exec_lua(function()
108        _G.server = _G._create_server({
109          capabilities = {
110            textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
111            semanticTokensProvider = {
112              full = { delta = true },
113              range = false,
114              legend = vim.fn.json_decode(legend),
115            },
116          },
117          handlers = {
118            ['textDocument/semanticTokens/full'] = function(_, _, callback)
119              callback(nil, vim.fn.json_decode(response))
120            end,
121            ['textDocument/semanticTokens/full/delta'] = function(_, _, callback)
122              callback(nil, vim.fn.json_decode(edit_response))
123            end,
124          },
125        })
126      end)
127    end)
128
129    it('buffer is highlighted when attached', function()
130      insert(text)
131
132      exec_lua(function()
133        local bufnr = vim.api.nvim_get_current_buf()
134        vim.api.nvim_win_set_buf(0, bufnr)
135        vim.bo[bufnr].filetype = 'some-filetype'
136        _G._start_server(_G.server)
137      end)
138
139      screen:expect {
140        grid = [[
141        #include <iostream>                     |
142                                                |
143        int {8:main}()                              |
144        {                                       |
145            int {7:x};                              |
146        #ifdef {5:__cplusplus}                      |
147            {4:std}::{2:cout} << {2:x} << "\n";             |
148        {6:#else}                                   |
149        {6:    printf("%d\n", x);}                  |
150        {6:#endif}                                  |
151        }                                       |
152        ^}                                       |
153        {1:~                                       }|*3
154                                                |
155      ]],
156      }
157    end)
158
159    it('buffer is highlighted with multiline tokens', function()
160      insert(text)
161
162      exec_lua(function()
163        _G.server2 = _G._create_server({
164          capabilities = {
165            textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
166            semanticTokensProvider = {
167              full = { delta = false },
168              legend = vim.fn.json_decode(legend),
169            },
170          },
171          handlers = {
172            ['textDocument/semanticTokens/full'] = function(_, _, callback)
173              callback(nil, {
174                data = { 5, 0, 82, 0, 0 },
175                resultId = 1,
176              })
177            end,
178          },
179        })
180      end, legend, response, edit_response)
181      exec_lua(function()
182        local bufnr = vim.api.nvim_get_current_buf()
183        vim.api.nvim_win_set_buf(0, bufnr)
184        vim.bo[bufnr].fileformat = 'unix'
185        vim.bo[bufnr].filetype = 'some-filetype'
186        _G.test_client_id = _G._start_server(_G.server2)
187      end)
188
189      screen:expect {
190        grid = [[
191        #include <iostream>                     |
192                                                |
193        int main()                              |
194        {                                       |
195            int x;                              |
196        {2:#ifdef __cplusplus}                      |
197        {2:    std::cout << x << "\n";}             |
198        {2:#else}                                   |
199        {2:    printf("%d\n", x);}                  |
200        {2:#endif}                                  |
201        }                                       |
202        ^}                                       |
203        {1:~                                       }|*3
204                                                |
205      ]],
206      }
207
208      -- The same token reaches four characters less far in a 'dos' buffer, because each
209      -- of the four line endings it spans takes two characters instead of one.
210      exec_lua(function()
211        vim.lsp.get_client_by_id(assert(_G.test_client_id)):stop()
212        vim.bo[vim.api.nvim_get_current_buf()].fileformat = 'dos'
213        _G._start_server(_G.server2)
214      end)
215
216      screen:expect {
217        grid = [[
218        #include <iostream>                     |
219                                                |
220        int main()                              |
221        {                                       |
222            int x;                              |
223        {2:#ifdef __cplusplus}                      |
224        {2:    std::cout << x << "\n";}             |
225        {2:#else}                                   |
226        {2:    printf("%d\n", x);}                  |
227        {2:#e}ndif                                  |
228        }                                       |
229        ^}                                       |
230        {1:~                                       }|*3
231                                                |
232      ]],
233      }
234    end)
235
236    it('calls both range and full when range is supported', function()
237      insert(text)
238
239      exec_lua(function()
240        _G.server_range = _G._create_server({
241          capabilities = {
242            semanticTokensProvider = {
243              full = { delta = false },
244              range = true,
245              legend = vim.fn.json_decode(legend),
246            },
247          },
248          handlers = {
249            ['textDocument/semanticTokens/range'] = function(_, _, callback)
250              callback(nil, vim.fn.json_decode(range_response))
251            end,
252            ['textDocument/semanticTokens/full'] = function(_, _, callback)
253              callback(nil, vim.fn.json_decode(response))
254            end,
255          },
256        })
257      end)
258      exec_lua(function()
259        local bufnr = vim.api.nvim_get_current_buf()
260        vim.api.nvim_win_set_buf(0, bufnr)
261        _G._start_server(_G.server_range)
262      end)
263
264      screen:expect {
265        grid = [[
266        #include <iostream>                     |
267                                                |
268        int {8:main}()                              |
269        {                                       |
270            int {7:x};                              |
271        #ifdef {5:__cplusplus}                      |
272            {4:std}::{2:cout} << {2:x} << "\n";             |
273        {6:#else}                                   |
274        {6:    printf("%d\n", x);}                  |
275        {6:#endif}                                  |
276        }                                       |
277        ^}                                       |
278        {1:~                                       }|*3
279                                                |
280      ]],
281      }
282
283      local messages = exec_lua('return _G.server_range.messages')
284      local called_range = false
285      local called_full = false
286      for _, m in ipairs(messages) do
287        if m.method == 'textDocument/semanticTokens/range' then
288          called_range = true
289        end
290        if m.method == 'textDocument/semanticTokens/full' then
291          called_full = true
292        end
293      end
294      eq(true, called_range)
295      eq(true, called_full)
296    end)
297
298    it('does not call range when only full is supported', function()
299      insert(text)
300
301      exec_lua(function()
302        _G.server_full = _G._create_server({
303          capabilities = {
304            semanticTokensProvider = {
305              full = { delta = false },
306              range = false,
307              legend = vim.fn.json_decode(legend),
308            },
309          },
310          handlers = {
311            ['textDocument/semanticTokens/full'] = function(_, _, callback)
312              callback(nil, vim.fn.json_decode(response))
313            end,
314            ['textDocument/semanticTokens/range'] = function(_, _, callback)
315              callback(nil, vim.fn.json_decode(range_response))
316            end,
317          },
318        })
319        return _G._start_server(_G.server_full)
320      end)
321
322      local messages = exec_lua('return _G.server_full.messages')
323      local called_full = false
324      local called_range = false
325      for _, m in ipairs(messages) do
326        if m.method == 'textDocument/semanticTokens/full' then
327          called_full = true
328        end
329        if m.method == 'textDocument/semanticTokens/range' then
330          called_range = true
331        end
332      end
333      eq(true, called_full)
334      eq(false, called_range)
335    end)
336
337    it('does not call range after full request received', function()
338      insert(text)
339
340      exec_lua(function()
341        _G.server_full = _G._create_server({
342          capabilities = {
343            textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
344            semanticTokensProvider = {
345              full = { delta = false },
346              range = true,
347              legend = vim.fn.json_decode(legend),
348            },
349          },
350          handlers = {
351            ['textDocument/semanticTokens/full'] = function(_, _, callback)
352              callback(nil, vim.fn.json_decode(response))
353            end,
354            ['textDocument/semanticTokens/range'] = function(_, _, callback)
355              callback(nil, vim.fn.json_decode(range_response))
356            end,
357          },
358        })
359        return _G._start_server(_G.server_full)
360      end)
361
362      -- ensure initial semantic token requests have been sent before feeding input
363      n.poke_eventloop()
364      -- modify the buffer
365      feed('o<ESC>')
366
367      n.poke_eventloop()
368
369      local messages = exec_lua('return _G.server_full.messages')
370      local called_full = 0
371      local called_range = 0
372      for _, m in ipairs(messages) do
373        if m.method == 'textDocument/semanticTokens/full' then
374          called_full = called_full + 1
375        end
376        if m.method == 'textDocument/semanticTokens/range' then
377          called_range = called_range + 1
378        end
379      end
380      eq(2, called_full)
381      eq(1, called_range)
382    end)
383
384    it('range requests preserve highlights outside updated range', function()
385      screen:try_resize(40, 6)
386      insert(text)
387      feed('gg')
388
389      local client_id, bufnr = exec_lua(function()
390        _G.response = [[{
391          "data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025 ]
392        }]]
393        _G.server2 = _G._create_server({
394          capabilities = {
395            textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
396            semanticTokensProvider = {
397              range = true,
398              legend = vim.fn.json_decode(legend),
399            },
400          },
401          handlers = {
402            ['textDocument/semanticTokens/range'] = function(_, _, callback)
403              callback(nil, vim.fn.json_decode(_G.response))
404            end,
405          },
406        })
407        local bufnr = vim.api.nvim_get_current_buf()
408        local client_id = assert(_G._start_server(_G.server2))
409        return client_id, bufnr
410      end)
411
412      screen:expect {
413        grid = [[
414        ^#include <iostream>                     |
415                                                |
416        int {8:main}()                              |
417        {                                       |
418            int {7:x};                              |
419                                                |
420      ]],
421      }
422
423      eq(
424        2,
425        exec_lua(function()
426          return #vim.lsp.semantic_tokens.__STHighlighter.active[bufnr].client_state[client_id].current_result.highlights
427        end)
428      )
429
430      exec_lua(function()
431        _G.response = [[{
432        "data": [ 7, 0, 5, 20, 0, 1, 0, 22, 20, 0, 1, 0, 6, 20, 0 ]
433      }]]
434      end)
435
436      feed('G')
437
438      screen:expect {
439        grid = [[
440        {6:#else}                                   |
441        {6:    printf("%d\n", x);}                  |
442        {6:#endif}                                  |
443        }                                       |
444        ^}                                       |
445                                                |
446      ]],
447      }
448
449      eq(
450        5,
451        exec_lua(function()
452          return #vim.lsp.semantic_tokens.__STHighlighter.active[bufnr].client_state[client_id].current_result.highlights
453        end)
454      )
455
456      exec_lua(function()
457        _G.response = [[{
458        "data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025, 1, 7, 11, 19, 8192 ]
459      }]]
460      end)
461      feed('ggLj0')
462
463      screen:expect {
464        grid = [[
465                                                |
466        int {8:main}()                              |
467        {                                       |
468            int {7:x};                              |
469        ^#ifdef {5:__cplusplus}                      |
470                                                |
471      ]],
472      }
473
474      eq(
475        6,
476        exec_lua(function()
477          return #vim.lsp.semantic_tokens.__STHighlighter.active[bufnr].client_state[client_id].current_result.highlights
478        end)
479      )
480
481      eq(
482        {
483          {
484            line = 2,
485            end_line = 2,
486            start_col = 4,
487            end_col = 8,
488            marked = true,
489            modifiers = {
490              declaration = true,
491              globalScope = true,
492            },
493            type = 'function',
494          },
495          {
496            line = 4,
497            end_line = 4,
498            start_col = 8,
499            end_col = 9,
500            marked = true,
501            modifiers = {
502              declaration = true,
503              functionScope = true,
504            },
505            type = 'variable',
506          },
507          {
508            line = 5,
509            end_line = 5,
510            start_col = 7,
511            end_col = 18,
512            marked = true,
513            modifiers = {
514              globalScope = true,
515            },
516            type = 'macro',
517          },
518          {
519            line = 7,
520            end_line = 7,
521            start_col = 0,
522            end_col = 5,
523            marked = true,
524            modifiers = {},
525            type = 'comment',
526          },
527          {
528            line = 8,
529            end_line = 8,
530            start_col = 0,
531            end_col = 22,
532            marked = true,
533            modifiers = {},
534            type = 'comment',
535          },
536          {
537            line = 9,
538            end_line = 9,
539            start_col = 0,
540            end_col = 6,
541            marked = true,
542            modifiers = {},
543            type = 'comment',
544          },
545        },
546        exec_lua(function()
547          return vim.lsp.semantic_tokens.__STHighlighter.active[bufnr].client_state[client_id].current_result.highlights
548        end)
549      )
550    end)
551
552    it('use LspTokenUpdate and highlight_token', function()
553      insert(text)
554
555      exec_lua(function()
556        vim.api.nvim_create_autocmd('LspTokenUpdate', {
557          callback = function(ev)
558            local token = ev.data.token --- @type STTokenRange
559            if token.type == 'function' and token.modifiers.declaration then
560              vim.lsp.semantic_tokens.highlight_token(token, ev.buf, ev.data.client_id, 'Macro')
561            end
562          end,
563        })
564        local bufnr = vim.api.nvim_get_current_buf()
565        vim.api.nvim_win_set_buf(0, bufnr)
566        _G._start_server(_G.server)
567      end)
568
569      screen:expect {
570        grid = [[
571        #include <iostream>                     |
572                                                |
573        int {9:main}()                              |
574        {                                       |
575            int {7:x};                              |
576        #ifdef {5:__cplusplus}                      |
577            {4:std}::{2:cout} << {2:x} << "\n";             |
578        {6:#else}                                   |
579        {6:    printf("%d\n", x);}                  |
580        {6:#endif}                                  |
581        }                                       |
582        ^}                                       |
583        {1:~                                       }|*3
584                                                |
585      ]],
586      }
587    end)
588
589    it('buffer is unhighlighted when client is detached', function()
590      insert(text)
591
592      local bufnr = n.api.nvim_get_current_buf()
593      local client_id = exec_lua(function()
594        return _G._start_server(_G.server)
595      end)
596
597      screen:expect {
598        grid = [[
599        #include <iostream>                     |
600                                                |
601        int {8:main}()                              |
602        {                                       |
603            int {7:x};                              |
604        #ifdef {5:__cplusplus}                      |
605            {4:std}::{2:cout} << {2:x} << "\n";             |
606        {6:#else}                                   |
607        {6:    printf("%d\n", x);}                  |
608        {6:#endif}                                  |
609        }                                       |
610        ^}                                       |
611        {1:~                                       }|*3
612                                                |
613      ]],
614      }
615
616      exec_lua(function()
617        --- @diagnostic disable-next-line:duplicate-set-field
618        vim.notify = function() end
619        vim.lsp.buf_detach_client(bufnr, client_id)
620      end)
621
622      screen:expect {
623        grid = [[
624        #include <iostream>                     |
625                                                |
626        int main()                              |
627        {                                       |
628            int x;                              |
629        #ifdef __cplusplus                      |
630            std::cout << x << "\n";             |
631        #else                                   |
632            printf("%d\n", x);                  |
633        #endif                                  |
634        }                                       |
635        ^}                                       |
636        {1:~                                       }|*3
637                                                |
638      ]],
639      }
640    end)
641
642    it(
643      'buffer is highlighted and unhighlighted when semantic token highlighting is enabled and disabled',
644      function()
645        insert(text)
646
647        exec_lua(function()
648          return _G._start_server(_G.server)
649        end)
650
651        screen:expect {
652          grid = [[
653          #include <iostream>                     |
654                                                  |
655          int {8:main}()                              |
656          {                                       |
657              int {7:x};                              |
658          #ifdef {5:__cplusplus}                      |
659              {4:std}::{2:cout} << {2:x} << "\n";             |
660          {6:#else}                                   |
661          {6:    printf("%d\n", x);}                  |
662          {6:#endif}                                  |
663          }                                       |
664          ^}                                       |
665          {1:~                                       }|*3
666                                                  |
667        ]],
668        }
669
670        exec_lua(function()
671          --- @diagnostic disable-next-line:duplicate-set-field
672          vim.notify = function() end
673          vim.lsp.semantic_tokens.enable(false)
674        end)
675
676        screen:expect {
677          grid = [[
678          #include <iostream>                     |
679                                                  |
680          int main()                              |
681          {                                       |
682              int x;                              |
683          #ifdef __cplusplus                      |
684              std::cout << x << "\n";             |
685          #else                                   |
686              printf("%d\n", x);                  |
687          #endif                                  |
688          }                                       |
689          ^}                                       |
690          {1:~                                       }|*3
691                                                  |
692      ]],
693        }
694
695        exec_lua(function()
696          vim.lsp.semantic_tokens.enable(true)
697        end)
698
699        screen:expect {
700          grid = [[
701        #include <iostream>                     |
702                                                |
703        int {8:main}()                              |
704        {                                       |
705            int {7:x};                              |
706        #ifdef {5:__cplusplus}                      |
707            {4:std}::{2:cout} << {2:x} << "\n";             |
708        {6:#else}                                   |
709        {6:    printf("%d\n", x);}                  |
710        {6:#endif}                                  |
711        }                                       |
712        ^}                                       |
713        {1:~                                       }|*3
714                                                |
715      ]],
716        }
717      end
718    )
719
720    it('highlights start and stop when using "0" for current buffer', function()
721      insert(text)
722
723      exec_lua(function()
724        return _G._start_server(_G.server)
725      end)
726
727      screen:expect {
728        grid = [[
729        #include <iostream>                     |
730                                                |
731        int {8:main}()                              |
732        {                                       |
733            int {7:x};                              |
734        #ifdef {5:__cplusplus}                      |
735            {4:std}::{2:cout} << {2:x} << "\n";             |
736        {6:#else}                                   |
737        {6:    printf("%d\n", x);}                  |
738        {6:#endif}                                  |
739        }                                       |
740        ^}                                       |
741        {1:~                                       }|*3
742                                                |
743      ]],
744      }
745
746      exec_lua(function()
747        --- @diagnostic disable-next-line:duplicate-set-field
748        vim.notify = function() end
749        vim.lsp.semantic_tokens.enable(false, { bufnr = 0 })
750      end)
751
752      screen:expect {
753        grid = [[
754        #include <iostream>                     |
755                                                |
756        int main()                              |
757        {                                       |
758            int x;                              |
759        #ifdef __cplusplus                      |
760            std::cout << x << "\n";             |
761        #else                                   |
762            printf("%d\n", x);                  |
763        #endif                                  |
764        }                                       |
765        ^}                                       |
766        {1:~                                       }|*3
767                                                |
768      ]],
769      }
770
771      exec_lua(function()
772        vim.lsp.semantic_tokens.enable(true, { bufnr = 0 })
773      end)
774
775      screen:expect {
776        grid = [[
777        #include <iostream>                     |
778                                                |
779        int {8:main}()                              |
780        {                                       |
781            int {7:x};                              |
782        #ifdef {5:__cplusplus}                      |
783            {4:std}::{2:cout} << {2:x} << "\n";             |
784        {6:#else}                                   |
785        {6:    printf("%d\n", x);}                  |
786        {6:#endif}                                  |
787        }                                       |
788        ^}                                       |
789        {1:~                                       }|*3
790                                                |
791      ]],
792      }
793    end)
794
795    it('buffer is re-highlighted when force refreshed', function()
796      insert(text)
797
798      exec_lua(function()
799        return _G._start_server(_G.server)
800      end)
801
802      screen:expect {
803        grid = [[
804        #include <iostream>                     |
805                                                |
806        int {8:main}()                              |
807        {                                       |
808            int {7:x};                              |
809        #ifdef {5:__cplusplus}                      |
810            {4:std}::{2:cout} << {2:x} << "\n";             |
811        {6:#else}                                   |
812        {6:    printf("%d\n", x);}                  |
813        {6:#endif}                                  |
814        }                                       |
815        ^}                                       |
816        {1:~                                       }|*3
817                                                |
818      ]],
819      }
820
821      exec_lua(function()
822        vim.lsp.semantic_tokens.force_refresh()
823      end)
824
825      screen:expect {
826        grid = [[
827        #include <iostream>                     |
828                                                |
829        int {8:main}()                              |
830        {                                       |
831            int {7:x};                              |
832        #ifdef {5:__cplusplus}                      |
833            {4:std}::{2:cout} << {2:x} << "\n";             |
834        {6:#else}                                   |
835        {6:    printf("%d\n", x);}                  |
836        {6:#endif}                                  |
837        }                                       |
838        ^}                                       |
839        {1:~                                       }|*3
840                                                |
841      ]],
842        unchanged = true,
843      }
844
845      local messages = exec_lua('return server.messages')
846      local token_request_count = 0
847      for _, message in
848        ipairs(messages --[[@as {method:string,params:table}[] ]])
849      do
850        assert(message.method ~= 'textDocument/semanticTokens/full/delta', 'delta request received')
851        if message.method == 'textDocument/semanticTokens/full' then
852          token_request_count = token_request_count + 1
853        end
854      end
855      eq(2, token_request_count)
856    end)
857
858    it('destroys the highlighter if the buffer is deleted', function()
859      insert(text)
860
861      exec_lua(function()
862        return _G._start_server(_G.server)
863      end)
864
865      eq(
866        {},
867        exec_lua(function()
868          local bufnr = vim.api.nvim_get_current_buf()
869          vim.api.nvim_buf_delete(bufnr, { force = true })
870          return vim.lsp.semantic_tokens.__STHighlighter.active
871        end)
872      )
873    end)
874
875    it('updates highlights with delta request on buffer change', function()
876      insert(text)
877
878      exec_lua(function()
879        return _G._start_server(_G.server)
880      end)
881
882      screen:expect {
883        grid = [[
884        #include <iostream>                     |
885                                                |
886        int {8:main}()                              |
887        {                                       |
888            int {7:x};                              |
889        #ifdef {5:__cplusplus}                      |
890            {4:std}::{2:cout} << {2:x} << "\n";             |
891        {6:#else}                                   |
892        {6:    printf("%d\n", x);}                  |
893        {6:#endif}                                  |
894        }                                       |
895        ^}                                       |
896        {1:~                                       }|*3
897                                                |
898      ]],
899      }
900      feed(':%s/int x/int x()/<CR>')
901      feed(':noh<CR>')
902      screen:expect {
903        grid = [[
904        #include <iostream>                     |
905                                                |
906        int {8:main}()                              |
907        {                                       |
908            ^int {8:x}();                            |
909        #ifdef {5:__cplusplus}                      |
910            {4:std}::{2:cout} << {3:x} << "\n";             |
911        {6:#else}                                   |
912        {6:    printf("%d\n", x);}                  |
913        {6:#endif}                                  |
914        }                                       |*2
915        {1:~                                       }|*3
916        :noh                                    |
917      ]],
918      }
919    end)
920
921    it(
922      'opt-out: does not activate semantic token highlighting if disabled in client attach',
923      function()
924        local client_id = exec_lua(function()
925          return vim.lsp.start({
926            name = 'dummy',
927            cmd = _G.server.cmd,
928            --- @param client vim.lsp.Client
929            on_attach = vim.schedule_wrap(function(client, _bufnr)
930              client.server_capabilities.semanticTokensProvider = nil
931            end),
932          })
933        end)
934        eq(true, exec_lua('return vim.lsp.buf_is_attached(0, ...)', client_id))
935
936        insert(text)
937
938        screen:expect {
939          grid = [[
940          #include <iostream>                     |
941                                                  |
942          int main()                              |
943          {                                       |
944              int x;                              |
945          #ifdef __cplusplus                      |
946              std::cout << x << "\n";             |
947          #else                                   |
948              printf("%d\n", x);                  |
949          #endif                                  |
950          }                                       |
951          ^}                                       |
952          {1:~                                       }|*3
953                                                  |
954        ]],
955        }
956
957        screen:expect {
958          grid = [[
959          #include <iostream>                     |
960                                                  |
961          int main()                              |
962          {                                       |
963              int x;                              |
964          #ifdef __cplusplus                      |
965              std::cout << x << "\n";             |
966          #else                                   |
967              printf("%d\n", x);                  |
968          #endif                                  |
969          }                                       |
970          ^}                                       |
971          {1:~                                       }|*3
972                                                  |
973          ]],
974          unchanged = true,
975        }
976      end
977    )
978
979    it('ignores null responses from the server', function()
980      local client_id = exec_lua(function()
981        _G.server2 = _G._create_server({
982          capabilities = {
983            textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
984            semanticTokensProvider = {
985              legend = vim.fn.json_decode(legend),
986              full = { delta = false },
987            },
988          },
989          handlers = {
990            ['textDocument/semanticTokens/full'] = function(_, _, callback)
991              callback(nil, nil)
992            end,
993            ['textDocument/semanticTokens/full/delta'] = function(_, _, callback)
994              callback(nil, nil)
995            end,
996          },
997        })
998        return _G._start_server(_G.server2)
999      end)
1000      eq(
1001        true,
1002        exec_lua(function()
1003          return vim.lsp.buf_is_attached(0, client_id)
1004        end)
1005      )
1006
1007      insert(text)
1008
1009      screen:expect {
1010        grid = [[
1011        #include <iostream>                     |
1012                                                |
1013        int main()                              |
1014        {                                       |
1015            int x;                              |
1016        #ifdef __cplusplus                      |
1017            std::cout << x << "\n";             |
1018        #else                                   |
1019            printf("%d\n", x);                  |
1020        #endif                                  |
1021        }                                       |
1022        ^}                                       |
1023        {1:~                                       }|*3
1024                                                |
1025      ]],
1026      }
1027    end)
1028
1029    it('resets active request after receiving error responses from the server', function()
1030      local error = { code = -32801, message = 'Content modified' }
1031      exec_lua(function()
1032        _G.server2 = _G._create_server({
1033          capabilities = {
1034            textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
1035            semanticTokensProvider = {
1036              legend = vim.fn.json_decode(legend),
1037              full = { delta = false },
1038            },
1039          },
1040          handlers = {
1041            -- There is same logic for handling nil responses and error responses,
1042            -- so keep responses not nil.
1043            --
1044            -- if an error response was not be handled, this test will hang on here.
1045            --- @param callback function
1046            ['textDocument/semanticTokens/full'] = function(_, _, callback)
1047              callback(error, vim.fn.json_decode(response))
1048            end,
1049            --- @param callback function
1050            ['textDocument/semanticTokens/full/delta'] = function(_, _, callback)
1051              callback(error, vim.fn.json_decode(response))
1052            end,
1053          },
1054        })
1055        return _G._start_server(_G.server2)
1056      end)
1057      screen:expect([[
1058        ^                                        |
1059        {1:~                                       }|*14
1060                                                |
1061      ]])
1062    end)
1063
1064    it('does not send delta requests if not supported by server', function()
1065      insert(text)
1066      exec_lua(function()
1067        _G.server2 = _G._create_server({
1068          capabilities = {
1069            textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
1070            semanticTokensProvider = {
1071              full = { delta = false },
1072              legend = vim.fn.json_decode(legend),
1073            },
1074          },
1075          handlers = {
1076            ['textDocument/semanticTokens/full'] = function(_, _, callback)
1077              callback(nil, vim.fn.json_decode(response))
1078            end,
1079            ['textDocument/semanticTokens/full/delta'] = function(_, _, callback)
1080              callback(nil, vim.fn.json_decode(edit_response))
1081            end,
1082          },
1083        })
1084        return _G._start_server(_G.server2)
1085      end)
1086
1087      screen:expect {
1088        grid = [[
1089        #include <iostream>                     |
1090                                                |
1091        int {8:main}()                              |
1092        {                                       |
1093            int {7:x};                              |
1094        #ifdef {5:__cplusplus}                      |
1095            {4:std}::{2:cout} << {2:x} << "\n";             |
1096        {6:#else}                                   |
1097        {6:    printf("%d\n", x);}                  |
1098        {6:#endif}                                  |
1099        }                                       |
1100        ^}                                       |
1101        {1:~                                       }|*3
1102                                                |
1103      ]],
1104      }
1105      feed(':%s/int x/int x()/<CR>')
1106      feed(':noh<CR>')
1107
1108      -- the highlights don't change because our fake server sent the exact
1109      -- same result for the same method (the full request). "x" would have
1110      -- changed to highlight index 3 had we sent a delta request
1111      screen:expect {
1112        grid = [[
1113        #include <iostream>                     |
1114                                                |
1115        int {8:main}()                              |
1116        {                                       |
1117            ^int {7:x}();                            |
1118        #ifdef {5:__cplusplus}                      |
1119            {4:std}::{2:cout} << {2:x} << "\n";             |
1120        {6:#else}                                   |
1121        {6:    printf("%d\n", x);}                  |
1122        {6:#endif}                                  |
1123        }                                       |*2
1124        {1:~                                       }|*3
1125        :noh                                    |
1126      ]],
1127      }
1128      local messages = exec_lua('return server2.messages')
1129      local token_request_count = 0
1130      for _, message in
1131        ipairs(messages --[[@as {method:string,params:table}[] ]])
1132      do
1133        assert(message.method ~= 'textDocument/semanticTokens/full/delta', 'delta request received')
1134        if message.method == 'textDocument/semanticTokens/full' then
1135          token_request_count = token_request_count + 1
1136        end
1137      end
1138      eq(2, token_request_count)
1139    end)
1140  end)
1141
1142  describe('token array decoding', function()
1143    for _, test in ipairs({
1144      {
1145        it = 'clangd-15 on C',
1146        text = [[char* foo = "\n";]],
1147        response = [[{"data": [0, 6, 3, 0, 8193], "resultId": "1"}]],
1148        legend = [[{
1149          "tokenTypes": [
1150            "variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment"
1151          ],
1152          "tokenModifiers": [
1153            "declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope"
1154          ]
1155        }]],
1156        expected = {
1157          {
1158            line = 0,
1159            end_line = 0,
1160            modifiers = { declaration = true, globalScope = true },
1161            start_col = 6,
1162            end_col = 9,
1163            type = 'variable',
1164            marked = true,
1165          },
1166        },
1167        expected_screen = function()
1168          screen:expect {
1169            grid = [[
1170            char* {7:foo} = "\n"^;                       |
1171            {1:~                                       }|*14
1172                                                    |
1173          ]],
1174          }
1175        end,
1176      },
1177      {
1178        it = 'clangd-15 on C (bad response data)',
1179        text = [[char* foo = "\n";]],
1180        response = [[{"data": [0, 6, 4294967295, 0, 8193], "resultId": "1"}]],
1181        legend = [[{
1182          "tokenTypes": [
1183            "variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment"
1184          ],
1185          "tokenModifiers": [
1186            "declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope"
1187          ]
1188        }]],
1189        expected = {
1190          {
1191            line = 0,
1192            end_line = 0,
1193            modifiers = { declaration = true, globalScope = true },
1194            start_col = 6,
1195            end_col = 17,
1196            type = 'variable',
1197            marked = true,
1198          },
1199        },
1200        expected_screen = function()
1201          screen:expect {
1202            grid = [[
1203            char* {7:foo = "\n"^;}                       |
1204            {1:~                                       }|*14
1205                                                    |
1206          ]],
1207          }
1208        end,
1209      },
1210      {
1211        it = 'clangd-15 on C++',
1212        text = [[#include <iostream>
1213int main()
1214{
1215  #ifdef __cplusplus
1216  const int x = 1;
1217  std::cout << x << std::endl;
1218  #else
1219    comment
1220  #endif
1221}]],
1222        response = [[{"data": [1, 4, 4, 3, 8193, 2, 9, 11, 19, 8192, 1, 12, 1, 1, 1033, 1, 2, 3, 15, 8448, 0, 5, 4, 0, 8448, 0, 8, 1, 1, 1032, 0, 5, 3, 15, 8448, 0, 5, 4, 3, 8448, 1, 0, 7, 20, 0, 1, 0, 11, 20, 0, 1, 0, 8, 20, 0], "resultId": "1"}]],
1223        legend = [[{
1224          "tokenTypes": [
1225            "variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment"
1226          ],
1227          "tokenModifiers": [
1228            "declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope"
1229          ]
1230        }]],
1231        expected = {
1232          { -- main
1233            line = 1,
1234            end_line = 1,
1235            modifiers = { declaration = true, globalScope = true },
1236            start_col = 4,
1237            end_col = 8,
1238            type = 'function',
1239            marked = true,
1240          },
1241          { --  __cplusplus
1242            line = 3,
1243            end_line = 3,
1244            modifiers = { globalScope = true },
1245            start_col = 9,
1246            end_col = 20,
1247            type = 'macro',
1248            marked = true,
1249          },
1250          { -- x
1251            line = 4,
1252            end_line = 4,
1253            modifiers = { declaration = true, readonly = true, functionScope = true },
1254            start_col = 12,
1255            end_col = 13,
1256            type = 'variable',
1257            marked = true,
1258          },
1259          { -- std
1260            line = 5,
1261            end_line = 5,
1262            modifiers = { defaultLibrary = true, globalScope = true },
1263            start_col = 2,
1264            end_col = 5,
1265            type = 'namespace',
1266            marked = true,
1267          },
1268          { -- cout
1269            line = 5,
1270            end_line = 5,
1271            modifiers = { defaultLibrary = true, globalScope = true },
1272            start_col = 7,
1273            end_col = 11,
1274            type = 'variable',
1275            marked = true,
1276          },
1277          { -- x
1278            line = 5,
1279            end_line = 5,
1280            modifiers = { readonly = true, functionScope = true },
1281            start_col = 15,
1282            end_col = 16,
1283            type = 'variable',
1284            marked = true,
1285          },
1286          { -- std
1287            line = 5,
1288            end_line = 5,
1289            modifiers = { defaultLibrary = true, globalScope = true },
1290            start_col = 20,
1291            end_col = 23,
1292            type = 'namespace',
1293            marked = true,
1294          },
1295          { -- endl
1296            line = 5,
1297            end_line = 5,
1298            modifiers = { defaultLibrary = true, globalScope = true },
1299            start_col = 25,
1300            end_col = 29,
1301            type = 'function',
1302            marked = true,
1303          },
1304          { -- #else comment #endif
1305            line = 6,
1306            end_line = 6,
1307            modifiers = {},
1308            start_col = 0,
1309            end_col = 7,
1310            type = 'comment',
1311            marked = true,
1312          },
1313          {
1314            line = 7,
1315            end_line = 7,
1316            modifiers = {},
1317            start_col = 0,
1318            end_col = 11,
1319            type = 'comment',
1320            marked = true,
1321          },
1322          {
1323            line = 8,
1324            end_line = 8,
1325            modifiers = {},
1326            start_col = 0,
1327            end_col = 8,
1328            type = 'comment',
1329            marked = true,
1330          },
1331        },
1332        expected_screen = function()
1333          screen:expect {
1334            grid = [[
1335            #include <iostream>                     |
1336            int {8:main}()                              |
1337            {                                       |
1338              #ifdef {5:__cplusplus}                    |
1339              const int {7:x} = 1;                      |
1340              {4:std}::{2:cout} << {2:x} << {4:std}::{3:endl};          |
1341            {6:  #else}                                 |
1342            {6:    comment}                             |
1343            {6:  #endif}                                |
1344            ^}                                       |
1345            {1:~                                       }|*5
1346                                                    |
1347          ]],
1348          }
1349        end,
1350      },
1351      {
1352        it = 'sumneko_lua',
1353        text = [[-- comment
1354local a = 1
1355b = "as"]],
1356        response = [[{"data": [0, 0, 10, 17, 0, 1, 6, 1, 8, 1, 1, 0, 1, 8, 8]}]],
1357        legend = [[{
1358          "tokenTypes": [
1359            "namespace", "type", "class", "enum", "interface", "struct", "typeParameter", "parameter", "variable", "property", "enumMember", "event", "function", "method", "macro", "keyword", "modifier", "comment", "string", "number", "regexp", "operator"
1360          ],
1361          "tokenModifiers": [
1362            "declaration", "definition", "readonly", "static", "deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary"
1363          ]
1364        }]],
1365        expected = {
1366          {
1367            line = 0,
1368            end_line = 0,
1369            modifiers = {},
1370            start_col = 0,
1371            end_col = 10,
1372            type = 'comment', -- comment
1373            marked = true,
1374          },
1375          {
1376            line = 1,
1377            end_line = 1,
1378            modifiers = { declaration = true }, -- a
1379            start_col = 6,
1380            end_col = 7,
1381            type = 'variable',
1382            marked = true,
1383          },
1384          {
1385            line = 2,
1386            end_line = 2,
1387            modifiers = { static = true }, -- b (global)
1388            start_col = 0,
1389            end_col = 1,
1390            type = 'variable',
1391            marked = true,
1392          },
1393        },
1394        expected_screen = function()
1395          screen:expect {
1396            grid = [[
1397            {6:-- comment}                              |
1398            local {7:a} = 1                             |
1399            {2:b} = "as^"                                |
1400            {1:~                                       }|*12
1401                                                    |
1402          ]],
1403          }
1404        end,
1405      },
1406      {
1407        it = 'rust-analyzer',
1408        text = [[pub fn main() {
1409    println!("Hello world!");
1410    break rust;
1411    /// what?
1412}
1413]],
1414        response = [[{"data": [0, 0, 3, 1, 0, 0, 4, 2, 1, 0, 0, 3, 4, 14, 524290, 0, 4, 1, 45, 0, 0, 1, 1, 45, 0, 0, 2, 1, 26, 0, 1, 4, 8, 17, 0, 0, 8, 1, 45, 0, 0, 1, 14, 2, 0, 0, 14, 1, 45, 0, 0, 1, 1, 48, 0, 1, 4, 5, 1, 8192, 0, 6, 4, 52, 0, 0, 4, 1, 48, 0, 1, 4, 9, 0, 1, 1, 0, 1, 26, 0 ], "resultId": "1"}]],
1415
1416        legend = [[{
1417        "tokenTypes": [
1418          "comment", "keyword", "string", "number", "regexp", "operator", "namespace", "type", "struct", "class", "interface", "enum", "enumMember", "typeParameter", "function", "method", "property", "macro", "variable",
1419          "parameter", "angle", "arithmetic", "attribute", "attributeBracket", "bitwise", "boolean", "brace", "bracket", "builtinAttribute", "builtinType", "character", "colon", "comma", "comparison", "constParameter", "derive",
1420          "dot", "escapeSequence", "formatSpecifier", "generic", "label", "lifetime", "logical", "macroBang", "operator", "parenthesis", "punctuation", "selfKeyword", "semicolon", "typeAlias", "toolModule", "union", "unresolvedReference"
1421        ],
1422        "tokenModifiers": [
1423          "documentation", "declaration", "definition", "static", "abstract", "deprecated", "readonly", "defaultLibrary", "async", "attribute", "callable", "constant", "consuming", "controlFlow", "crateRoot", "injected", "intraDocLink",
1424          "library", "mutable", "public", "reference", "trait", "unsafe"
1425        ]
1426        }]],
1427        expected = {
1428          {
1429            line = 0,
1430            end_line = 0,
1431            modifiers = {},
1432            start_col = 0,
1433            end_col = 3, -- pub
1434            type = 'keyword',
1435            marked = true,
1436          },
1437          {
1438            line = 0,
1439            end_line = 0,
1440            modifiers = {},
1441            start_col = 4,
1442            end_col = 6, -- fn
1443            type = 'keyword',
1444            marked = true,
1445          },
1446          {
1447            line = 0,
1448            end_line = 0,
1449            modifiers = { declaration = true, public = true },
1450            start_col = 7,
1451            end_col = 11, -- main
1452            type = 'function',
1453            marked = true,
1454          },
1455          {
1456            line = 0,
1457            end_line = 0,
1458            modifiers = {},
1459            start_col = 11,
1460            end_col = 12,
1461            type = 'parenthesis',
1462            marked = true,
1463          },
1464          {
1465            line = 0,
1466            end_line = 0,
1467            modifiers = {},
1468            start_col = 12,
1469            end_col = 13,
1470            type = 'parenthesis',
1471            marked = true,
1472          },
1473          {
1474            line = 0,
1475            end_line = 0,
1476            modifiers = {},
1477            start_col = 14,
1478            end_col = 15,
1479            type = 'brace',
1480            marked = true,
1481          },
1482          {
1483            line = 1,
1484            end_line = 1,
1485            modifiers = {},
1486            start_col = 4,
1487            end_col = 12,
1488            type = 'macro', -- println!
1489            marked = true,
1490          },
1491          {
1492            line = 1,
1493            end_line = 1,
1494            modifiers = {},
1495            start_col = 12,
1496            end_col = 13,
1497            type = 'parenthesis',
1498            marked = true,
1499          },
1500          {
1501            line = 1,
1502            end_line = 1,
1503            modifiers = {},
1504            start_col = 13,
1505            end_col = 27,
1506            type = 'string', -- "Hello world!"
1507            marked = true,
1508          },
1509          {
1510            line = 1,
1511            end_line = 1,
1512            modifiers = {},
1513            start_col = 27,
1514            end_col = 28,
1515            type = 'parenthesis',
1516            marked = true,
1517          },
1518          {
1519            line = 1,
1520            end_line = 1,
1521            modifiers = {},
1522            start_col = 28,
1523            end_col = 29,
1524            type = 'semicolon',
1525            marked = true,
1526          },
1527          {
1528            line = 2,
1529            end_line = 2,
1530            modifiers = { controlFlow = true },
1531            start_col = 4,
1532            end_col = 9, -- break
1533            type = 'keyword',
1534            marked = true,
1535          },
1536          {
1537            line = 2,
1538            end_line = 2,
1539            modifiers = {},
1540            start_col = 10,
1541            end_col = 14, -- rust
1542            type = 'unresolvedReference',
1543            marked = true,
1544          },
1545          {
1546            line = 2,
1547            end_line = 2,
1548            modifiers = {},
1549            start_col = 14,
1550            end_col = 15,
1551            type = 'semicolon',
1552            marked = true,
1553          },
1554          {
1555            line = 3,
1556            end_line = 3,
1557            modifiers = { documentation = true },
1558            start_col = 4,
1559            end_col = 13,
1560            type = 'comment', -- /// what?
1561            marked = true,
1562          },
1563          {
1564            line = 4,
1565            end_line = 4,
1566            modifiers = {},
1567            start_col = 0,
1568            end_col = 1,
1569            type = 'brace',
1570            marked = true,
1571          },
1572        },
1573        expected_screen = function()
1574          screen:expect {
1575            grid = [[
1576            {10:pub} {10:fn} {8:main}() {                         |
1577                {5:println!}({11:"Hello world!"});           |
1578                {10:break} rust;                         |
1579                {6:/// what?}                           |
1580            }                                       |
1581            ^                                        |
1582            {1:~                                       }|*9
1583                                                    |
1584          ]],
1585          }
1586        end,
1587      },
1588    }) do
1589      it(test.it, function()
1590        local client_id = exec_lua(function(legend, resp)
1591          _G.server = _G._create_server({
1592            capabilities = {
1593              textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
1594              semanticTokensProvider = {
1595                full = { delta = false },
1596                legend = vim.fn.json_decode(legend),
1597              },
1598            },
1599            handlers = {
1600              ['textDocument/semanticTokens/full'] = function(_, _, callback)
1601                callback(nil, vim.fn.json_decode(resp))
1602              end,
1603            },
1604          })
1605          return _G._start_server(_G.server)
1606        end, test.legend, test.response)
1607
1608        insert(test.text)
1609
1610        test.expected_screen()
1611
1612        eq(
1613          test.expected,
1614          exec_lua(function()
1615            local bufnr = vim.api.nvim_get_current_buf()
1616            return vim.lsp.semantic_tokens.__STHighlighter.active[bufnr].client_state[client_id].current_result.highlights
1617          end)
1618        )
1619      end)
1620    end
1621  end)
1622
1623  describe('token decoding with deltas', function()
1624    for _, test in ipairs({
1625      {
1626        it = 'semantic_tokens_delta: clangd-15 on C',
1627        legend = [[{
1628          "tokenTypes": [
1629            "variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment"
1630          ],
1631          "tokenModifiers": [
1632            "declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope"
1633          ]
1634        }]],
1635        text1 = [[char* foo = "\n";]],
1636        edit = [[ggO<Esc>]],
1637        response1 = [[{"data": [0, 6, 3, 0, 8193], "resultId": "1"}]],
1638        response2 = [[{"edits": [{ "start": 0, "deleteCount": 1, "data": [1] }], "resultId": "2"}]],
1639        expected1 = {
1640          {
1641            line = 0,
1642            modifiers = {
1643              declaration = true,
1644              globalScope = true,
1645            },
1646            start_col = 6,
1647            end_line = 0,
1648            end_col = 9,
1649            type = 'variable',
1650            marked = true,
1651          },
1652        },
1653        expected2 = {
1654          {
1655            line = 1,
1656            modifiers = {
1657              declaration = true,
1658              globalScope = true,
1659            },
1660            start_col = 6,
1661            end_line = 1,
1662            end_col = 9,
1663            type = 'variable',
1664            marked = true,
1665          },
1666        },
1667        expected_screen1 = function()
1668          screen:expect {
1669            grid = [[
1670          char* {7:foo} = "\n"^;                       |
1671          {1:~                                       }|*14
1672                                                  |
1673        ]],
1674          }
1675        end,
1676        expected_screen2 = function()
1677          screen:expect {
1678            grid = [[
1679            ^                                        |
1680            char* {7:foo} = "\n";                       |
1681            {1:~                                       }|*13
1682                                                    |
1683          ]],
1684          }
1685        end,
1686      },
1687      {
1688        it = 'response with multiple delta edits',
1689        legend = [[{
1690        "tokenTypes": [
1691          "variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment"
1692        ],
1693        "tokenModifiers": [
1694          "declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope"
1695        ]
1696        }]],
1697        text1 = dedent([[
1698        #include <iostream>
1699
1700        int main()
1701        {
1702            int x;
1703        #ifdef __cplusplus
1704            std::cout << x << "\n";
1705        #else
1706            printf("%d\n", x);
1707        #endif
1708        }]]),
1709        text2 = [[#include <iostream>
1710
1711int main()
1712{
1713    int x();
1714    double y;
1715#ifdef __cplusplus
1716    std::cout << x << "\n";
1717#else
1718    printf("%d\n", x);
1719#endif
1720}]],
1721        response1 = [[{
1722        "data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025, 1, 7, 11, 19, 8192, 1, 4, 3, 15, 8448, 0, 5, 4, 0, 8448, 0, 8, 1, 1, 1024, 1, 0, 5, 20, 0, 1, 0, 22, 20, 0, 1, 0, 6, 20, 0 ],
1723        "resultId": 1
1724        }]],
1725        response2 = [[{
1726        "edits": [ {"data": [ 2, 8, 1, 3, 8193, 1, 11, 1, 1, 1025 ], "deleteCount": 5, "start": 5}, {"data": [ 0, 8, 1, 3, 8192 ], "deleteCount": 5, "start": 25 } ],
1727        "resultId":"2"
1728        }]],
1729        expected1 = {
1730          {
1731            line = 2,
1732            end_line = 2,
1733            start_col = 4,
1734            end_col = 8,
1735            modifiers = { declaration = true, globalScope = true },
1736            type = 'function',
1737            marked = true,
1738          },
1739          {
1740            line = 4,
1741            end_line = 4,
1742            start_col = 8,
1743            end_col = 9,
1744            modifiers = { declaration = true, functionScope = true },
1745            type = 'variable',
1746            marked = true,
1747          },
1748          {
1749            line = 5,
1750            end_line = 5,
1751            start_col = 7,
1752            end_col = 18,
1753            modifiers = { globalScope = true },
1754            type = 'macro',
1755            marked = true,
1756          },
1757          {
1758            line = 6,
1759            end_line = 6,
1760            start_col = 4,
1761            end_col = 7,
1762            modifiers = { defaultLibrary = true, globalScope = true },
1763            type = 'namespace',
1764            marked = true,
1765          },
1766          {
1767            line = 6,
1768            end_line = 6,
1769            start_col = 9,
1770            end_col = 13,
1771            modifiers = { defaultLibrary = true, globalScope = true },
1772            type = 'variable',
1773            marked = true,
1774          },
1775          {
1776            line = 6,
1777            end_line = 6,
1778            start_col = 17,
1779            end_col = 18,
1780            marked = true,
1781            modifiers = { functionScope = true },
1782            type = 'variable',
1783          },
1784          {
1785            line = 7,
1786            end_line = 7,
1787            start_col = 0,
1788            end_col = 5,
1789            marked = true,
1790            modifiers = {},
1791            type = 'comment',
1792          },
1793          {
1794            line = 8,
1795            end_line = 8,
1796            end_col = 22,
1797            modifiers = {},
1798            start_col = 0,
1799            type = 'comment',
1800            marked = true,
1801          },
1802          {
1803            line = 9,
1804            end_line = 9,
1805            start_col = 0,
1806            end_col = 6,
1807            modifiers = {},
1808            type = 'comment',
1809            marked = true,
1810          },
1811        },
1812        expected2 = {
1813          {
1814            line = 2,
1815            end_line = 2,
1816            start_col = 4,
1817            end_col = 8,
1818            modifiers = { declaration = true, globalScope = true },
1819            type = 'function',
1820            marked = true,
1821          },
1822          {
1823            line = 4,
1824            end_line = 4,
1825            start_col = 8,
1826            end_col = 9,
1827            modifiers = { declaration = true, globalScope = true },
1828            type = 'function',
1829            marked = true,
1830          },
1831          {
1832            line = 5,
1833            end_line = 5,
1834            end_col = 12,
1835            start_col = 11,
1836            modifiers = { declaration = true, functionScope = true },
1837            type = 'variable',
1838            marked = true,
1839          },
1840          {
1841            line = 6,
1842            end_line = 6,
1843            start_col = 7,
1844            end_col = 18,
1845            modifiers = { globalScope = true },
1846            type = 'macro',
1847            marked = true,
1848          },
1849          {
1850            line = 7,
1851            end_line = 7,
1852            start_col = 4,
1853            end_col = 7,
1854            modifiers = { defaultLibrary = true, globalScope = true },
1855            type = 'namespace',
1856            marked = true,
1857          },
1858          {
1859            line = 7,
1860            end_line = 7,
1861            start_col = 9,
1862            end_col = 13,
1863            modifiers = { defaultLibrary = true, globalScope = true },
1864            type = 'variable',
1865            marked = true,
1866          },
1867          {
1868            line = 7,
1869            end_line = 7,
1870            start_col = 17,
1871            end_col = 18,
1872            marked = true,
1873            modifiers = { globalScope = true },
1874            type = 'function',
1875          },
1876          {
1877            line = 8,
1878            end_line = 8,
1879            start_col = 0,
1880            end_col = 5,
1881            marked = true,
1882            modifiers = {},
1883            type = 'comment',
1884          },
1885          {
1886            line = 9,
1887            end_line = 9,
1888            end_col = 22,
1889            modifiers = {},
1890            start_col = 0,
1891            type = 'comment',
1892            marked = true,
1893          },
1894          {
1895            line = 10,
1896            end_line = 10,
1897            start_col = 0,
1898            end_col = 6,
1899            modifiers = {},
1900            type = 'comment',
1901            marked = true,
1902          },
1903        },
1904        expected_screen1 = function()
1905          screen:expect {
1906            grid = [[
1907            #include <iostream>                     |
1908                                                    |
1909            int {8:main}()                              |
1910            {                                       |
1911                int {7:x};                              |
1912            #ifdef {5:__cplusplus}                      |
1913                {4:std}::{2:cout} << {2:x} << "\n";             |
1914            {6:#else}                                   |
1915            {6:    printf("%d\n", x);}                  |
1916            {6:#endif}                                  |
1917            ^}                                       |
1918            {1:~                                       }|*4
1919                                                    |
1920          ]],
1921          }
1922        end,
1923        expected_screen2 = function()
1924          screen:expect {
1925            grid = [[
1926            #include <iostream>                     |
1927                                                    |
1928            int {8:main}()                              |
1929            {                                       |
1930                int {8:x}();                            |
1931                double {7:y};                           |
1932            #ifdef {5:__cplusplus}                      |
1933                {4:std}::{2:cout} << {3:x} << "\n";             |
1934            {6:#else}                                   |
1935            {6:    printf("%d\n", x);}                  |
1936            {6:^#endif}                                  |
1937            }                                       |
1938            {1:~                                       }|*3
1939                                                    |
1940          ]],
1941          }
1942        end,
1943      },
1944      {
1945        it = 'optional token_edit.data on deletion',
1946        legend = [[{
1947          "tokenTypes": [
1948            "comment", "keyword", "operator", "string", "number", "regexp", "type", "class", "interface", "enum", "enumMember", "typeParameter", "function", "method", "property", "variable", "parameter", "module", "intrinsic", "selfParameter", "clsParameter", "magicFunction", "builtinConstant", "parenthesis", "curlybrace", "bracket", "colon", "semicolon", "arrow"
1949          ],
1950          "tokenModifiers": [
1951            "declaration", "static", "abstract", "async", "documentation", "typeHint", "typeHintComment", "readonly", "decorator", "builtin"
1952          ]
1953        }]],
1954        text1 = [[string = "test"]],
1955        text2 = [[]],
1956        response1 = [[{"data": [0, 0, 6, 15, 1], "resultId": "1"}]],
1957        response2 = [[{"edits": [{ "start": 0, "deleteCount": 5 }], "resultId": "2"}]],
1958        expected1 = {
1959          {
1960            line = 0,
1961            end_line = 0,
1962            modifiers = {
1963              declaration = true,
1964            },
1965            start_col = 0,
1966            end_col = 6,
1967            type = 'variable',
1968            marked = true,
1969          },
1970        },
1971        expected2 = {},
1972        expected_screen1 = function()
1973          screen:expect {
1974            grid = [[
1975            {7:string} = "test^"                         |
1976            {1:~                                       }|*14
1977                                                    |
1978          ]],
1979          }
1980        end,
1981        expected_screen2 = function()
1982          screen:expect {
1983            grid = [[
1984            ^                                        |
1985            {1:~                                       }|*14
1986                                                    |
1987          ]],
1988          }
1989        end,
1990      },
1991    }) do
1992      it(test.it, function()
1993        local bufnr = n.api.nvim_get_current_buf()
1994        insert(test.text1)
1995        local client_id = exec_lua(function(legend, resp1, resp2)
1996          _G.server = _G._create_server({
1997            capabilities = {
1998              textDocumentSync = vim.lsp.protocol.TextDocumentSyncKind.Full,
1999              semanticTokensProvider = {
2000                full = { delta = true },
2001                legend = vim.fn.json_decode(legend),
2002              },
2003            },
2004            handlers = {
2005              ['textDocument/semanticTokens/full'] = function(_, _, callback)
2006                callback(nil, vim.fn.json_decode(resp1))
2007              end,
2008              ['textDocument/semanticTokens/full/delta'] = function(_, _, callback)
2009                callback(nil, vim.fn.json_decode(resp2))
2010              end,
2011            },
2012          })
2013          return assert(_G._start_server(_G.server))
2014        end, test.legend, test.response1, test.response2)
2015
2016        test.expected_screen1()
2017
2018        eq(
2019          test.expected1,
2020          exec_lua(function()
2021            return vim.lsp.semantic_tokens.__STHighlighter.active[bufnr].client_state[client_id].current_result.highlights
2022          end)
2023        )
2024
2025        if test.edit then
2026          feed(test.edit)
2027        else
2028          exec_lua(function(text)
2029            vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.fn.split(text, '\n'))
2030          end, test.text2)
2031        end
2032
2033        feed('<Ignore>')
2034        test.expected_screen2()
2035
2036        eq(
2037          test.expected2,
2038          exec_lua(function()
2039            return vim.lsp.semantic_tokens.__STHighlighter.active[bufnr].client_state[client_id].current_result.highlights
2040          end)
2041        )
2042      end)
2043    end
2044  end)
2045end)