robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
18.3 KiB750 linesraw
1-- Test suite for testing interactions with the incremental sync algorithms powering the LSP client
2local t = require('test.testutil')
3local n = require('test.functional.testnvim')()
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local api = n.api
7local clear = n.clear
8local eq = t.eq
9local exec_lua = n.exec_lua
10local feed = n.feed
11
12before_each(function()
13  clear()
14  exec_lua(function()
15    local sync = require('vim.lsp.sync')
16    local events = {}
17
18    -- local format_line_ending = {
19    --   ["unix"] = '\n',
20    --   ["dos"] = '\r\n',
21    --   ["mac"] = '\r',
22    -- }
23
24    -- local line_ending = format_line_ending[vim.api.nvim_get_option_value('fileformat', {})]
25
26    --- @diagnostic disable-next-line:duplicate-set-field
27    function _G.test_register(bufnr, id, position_encoding, line_ending)
28      local prev_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
29
30      local function callback(_, bufnr0, _changedtick, firstline, lastline, new_lastline)
31        if _G.test_unreg == id then
32          return true
33        end
34
35        local curr_lines = vim.api.nvim_buf_get_lines(bufnr0, 0, -1, true)
36        local incremental_change = sync.compute_diff(
37          prev_lines,
38          curr_lines,
39          firstline,
40          lastline,
41          new_lastline,
42          position_encoding,
43          line_ending
44        )
45
46        table.insert(events, incremental_change)
47        prev_lines = curr_lines
48      end
49      local opts = { on_lines = callback, on_detach = callback, on_reload = callback }
50      vim.api.nvim_buf_attach(bufnr, false, opts)
51    end
52
53    --- @diagnostic disable-next-line:duplicate-set-field
54    function _G.get_events()
55      local ret_events = events
56      events = {}
57      return ret_events
58    end
59  end)
60end)
61
62--- @param edit_operations string[]
63local function test_edit(
64  prev_buffer,
65  edit_operations,
66  expected_text_changes,
67  position_encoding,
68  line_ending
69)
70  position_encoding = position_encoding or 'utf-16'
71  line_ending = line_ending or '\n'
72
73  api.nvim_buf_set_lines(0, 0, -1, true, prev_buffer)
74  exec_lua(function()
75    return _G.test_register(0, 'test1', position_encoding, line_ending)
76  end)
77
78  for _, edit in ipairs(edit_operations) do
79    feed(edit)
80  end
81  eq(
82    expected_text_changes,
83    exec_lua(function()
84      return _G.get_events()
85    end)
86  )
87  exec_lua(function()
88    _G.test_unreg = 'test1'
89  end)
90end
91
92describe('incremental synchronization', function()
93  describe('single line edit', function()
94    it('inserting a character in an empty buffer', function()
95      local expected_text_changes = {
96        {
97          range = {
98            ['start'] = {
99              character = 0,
100              line = 0,
101            },
102            ['end'] = {
103              character = 0,
104              line = 0,
105            },
106          },
107          rangeLength = 0,
108          text = 'a',
109        },
110      }
111      test_edit({ '' }, { 'ia' }, expected_text_changes, 'utf-16', '\n')
112    end)
113    it('inserting a character in the middle of a the first line', function()
114      local expected_text_changes = {
115        {
116          range = {
117            ['start'] = {
118              character = 1,
119              line = 0,
120            },
121            ['end'] = {
122              character = 1,
123              line = 0,
124            },
125          },
126          rangeLength = 0,
127          text = 'a',
128        },
129      }
130      test_edit({ 'ab' }, { 'lia' }, expected_text_changes, 'utf-16', '\n')
131    end)
132    it('deleting the only character in a buffer', function()
133      local expected_text_changes = {
134        {
135          range = {
136            ['start'] = {
137              character = 0,
138              line = 0,
139            },
140            ['end'] = {
141              character = 1,
142              line = 0,
143            },
144          },
145          rangeLength = 1,
146          text = '',
147        },
148      }
149      test_edit({ 'a' }, { 'x' }, expected_text_changes, 'utf-16', '\n')
150    end)
151    it('deleting a character in the middle of the line', function()
152      local expected_text_changes = {
153        {
154          range = {
155            ['start'] = {
156              character = 1,
157              line = 0,
158            },
159            ['end'] = {
160              character = 2,
161              line = 0,
162            },
163          },
164          rangeLength = 1,
165          text = '',
166        },
167      }
168      test_edit({ 'abc' }, { 'lx' }, expected_text_changes, 'utf-16', '\n')
169    end)
170    it('replacing a character', function()
171      local expected_text_changes = {
172        {
173          range = {
174            ['start'] = {
175              character = 0,
176              line = 0,
177            },
178            ['end'] = {
179              character = 1,
180              line = 0,
181            },
182          },
183          rangeLength = 1,
184          text = 'b',
185        },
186      }
187      test_edit({ 'a' }, { 'rb' }, expected_text_changes, 'utf-16', '\n')
188    end)
189    it('deleting the first line', function()
190      local expected_text_changes = {
191        {
192          range = {
193            ['start'] = {
194              character = 0,
195              line = 0,
196            },
197            ['end'] = {
198              character = 0,
199              line = 1,
200            },
201          },
202          rangeLength = 6,
203          text = '',
204        },
205      }
206      test_edit({ 'hello', 'world' }, { 'ggdd' }, expected_text_changes, 'utf-16', '\n')
207    end)
208    it('deleting the last line', function()
209      local expected_text_changes = {
210        {
211          range = {
212            ['start'] = {
213              character = 0,
214              line = 1,
215            },
216            ['end'] = {
217              character = 0,
218              line = 2,
219            },
220          },
221          rangeLength = 6,
222          text = '',
223        },
224      }
225      test_edit({ 'hello', 'world' }, { '2ggdd' }, expected_text_changes, 'utf-16', '\n')
226    end)
227    it('deleting all lines', function()
228      local expected_text_changes = {
229        {
230          range = {
231            ['start'] = {
232              character = 0,
233              line = 0,
234            },
235            ['end'] = {
236              character = 5,
237              line = 1,
238            },
239          },
240          rangeLength = 11,
241          text = '',
242        },
243      }
244      test_edit({ 'hello', 'world' }, { 'ggdG' }, expected_text_changes, 'utf-16', '\n')
245    end)
246    it('deleting an empty line', function()
247      local expected_text_changes = {
248        {
249          range = {
250            ['start'] = {
251              character = 0,
252              line = 1,
253            },
254            ['end'] = {
255              character = 0,
256              line = 2,
257            },
258          },
259          rangeLength = 1,
260          text = '',
261        },
262      }
263      test_edit({ 'hello world', '' }, { 'jdd' }, expected_text_changes, 'utf-16', '\n')
264    end)
265    it('adding a line', function()
266      local expected_text_changes = {
267        {
268          range = {
269            ['start'] = {
270              character = 11,
271              line = 0,
272            },
273            ['end'] = {
274              character = 0,
275              line = 1,
276            },
277          },
278          rangeLength = 1,
279          text = '\nhello world\n',
280        },
281      }
282      test_edit({ 'hello world' }, { 'yyp' }, expected_text_changes, 'utf-16', '\n')
283    end)
284    it('adding an empty line', function()
285      local expected_text_changes = {
286        {
287          range = {
288            ['start'] = {
289              character = 11,
290              line = 0,
291            },
292            ['end'] = {
293              character = 0,
294              line = 1,
295            },
296          },
297          rangeLength = 1,
298          text = '\n\n',
299        },
300      }
301      test_edit({ 'hello world' }, { 'o' }, expected_text_changes, 'utf-16', '\n')
302    end)
303    it('adding a line to an empty buffer', function()
304      local expected_text_changes = {
305        {
306          range = {
307            ['start'] = {
308              character = 0,
309              line = 0,
310            },
311            ['end'] = {
312              character = 0,
313              line = 1,
314            },
315          },
316          rangeLength = 1,
317          text = '\n\n',
318        },
319      }
320      test_edit({ '' }, { 'o' }, expected_text_changes, 'utf-16', '\n')
321    end)
322    it('insert a line above the current line', function()
323      local expected_text_changes = {
324        {
325          range = {
326            ['start'] = {
327              character = 0,
328              line = 0,
329            },
330            ['end'] = {
331              character = 0,
332              line = 0,
333            },
334          },
335          rangeLength = 0,
336          text = '\n',
337        },
338      }
339      test_edit({ '' }, { 'O' }, expected_text_changes, 'utf-16', '\n')
340    end)
341  end)
342  describe('multi line edit', function()
343    it('deletion and insertion', function()
344      local expected_text_changes = {
345        -- delete "_fsda" from end of line 1
346        {
347          range = {
348            ['start'] = {
349              character = 4,
350              line = 1,
351            },
352            ['end'] = {
353              character = 9,
354              line = 1,
355            },
356          },
357          rangeLength = 5,
358          text = '',
359        },
360        -- delete "hello world\n" from line 2
361        {
362          range = {
363            ['start'] = {
364              character = 0,
365              line = 2,
366            },
367            ['end'] = {
368              character = 0,
369              line = 3,
370            },
371          },
372          rangeLength = 12,
373          text = '',
374        },
375        -- delete "1234" from beginning of line 2
376        {
377          range = {
378            ['start'] = {
379              character = 0,
380              line = 2,
381            },
382            ['end'] = {
383              character = 4,
384              line = 2,
385            },
386          },
387          rangeLength = 4,
388          text = '',
389        },
390        -- add " asdf" to end of line 1
391        {
392          range = {
393            ['start'] = {
394              character = 4,
395              line = 1,
396            },
397            ['end'] = {
398              character = 4,
399              line = 1,
400            },
401          },
402          rangeLength = 0,
403          text = ' asdf',
404        },
405        -- delete " asdf\n" from line 2
406        {
407          range = {
408            ['start'] = {
409              character = 0,
410              line = 2,
411            },
412            ['end'] = {
413              character = 0,
414              line = 3,
415            },
416          },
417          rangeLength = 6,
418          text = '',
419        },
420        -- undo entire deletion
421        {
422          range = {
423            ['start'] = {
424              character = 4,
425              line = 1,
426            },
427            ['end'] = {
428              character = 9,
429              line = 1,
430            },
431          },
432          rangeLength = 5,
433          text = '_fdsa\nhello world\n1234 asdf',
434        },
435        -- redo entire deletion
436        {
437          range = {
438            ['start'] = {
439              character = 4,
440              line = 1,
441            },
442            ['end'] = {
443              character = 9,
444              line = 3,
445            },
446          },
447          rangeLength = 27,
448          text = ' asdf',
449        },
450      }
451      local original_lines = {
452        '\\begin{document}',
453        'test_fdsa',
454        'hello world',
455        '1234 asdf',
456        '\\end{document}',
457      }
458      test_edit(original_lines, { 'jf_vejjbhhdu<C-R>' }, expected_text_changes, 'utf-16', '\n')
459    end)
460  end)
461
462  describe('multi-operation edits', function()
463    it('mult-line substitution', function()
464      local expected_text_changes = {
465        {
466          range = {
467            ['end'] = {
468              character = 11,
469              line = 2,
470            },
471            ['start'] = {
472              character = 10,
473              line = 2,
474            },
475          },
476          rangeLength = 1,
477          text = '',
478        },
479        {
480          range = {
481            ['end'] = {
482              character = 10,
483              line = 2,
484            },
485            start = {
486              character = 10,
487              line = 2,
488            },
489          },
490          rangeLength = 0,
491          text = '2',
492        },
493        {
494          range = {
495            ['end'] = {
496              character = 11,
497              line = 3,
498            },
499            ['start'] = {
500              character = 10,
501              line = 3,
502            },
503          },
504          rangeLength = 1,
505          text = '',
506        },
507        {
508          range = {
509            ['end'] = {
510              character = 10,
511              line = 3,
512            },
513            ['start'] = {
514              character = 10,
515              line = 3,
516            },
517          },
518          rangeLength = 0,
519          text = '3',
520        },
521        {
522          range = {
523            ['end'] = {
524              character = 0,
525              line = 3,
526            },
527            ['start'] = {
528              character = 12,
529              line = 2,
530            },
531          },
532          rangeLength = 1,
533          text = '\n',
534        },
535      }
536      local original_lines = {
537        '\\begin{document}',
538        '\\section*{1}',
539        '\\section*{1}',
540        '\\section*{1}',
541        '\\end{document}',
542      }
543      test_edit(original_lines, { '3gg$h<C-V>jg<C-A>' }, expected_text_changes, 'utf-16', '\n')
544    end)
545    it('join and undo', function()
546      local expected_text_changes = {
547        {
548          range = {
549            ['start'] = {
550              character = 11,
551              line = 0,
552            },
553            ['end'] = {
554              character = 11,
555              line = 0,
556            },
557          },
558          rangeLength = 0,
559          text = ' test3',
560        },
561        {
562          range = {
563            ['start'] = {
564              character = 0,
565              line = 1,
566            },
567            ['end'] = {
568              character = 0,
569              line = 2,
570            },
571          },
572          rangeLength = 6,
573          text = '',
574        },
575        {
576          range = {
577            ['start'] = {
578              character = 11,
579              line = 0,
580            },
581            ['end'] = {
582              character = 17,
583              line = 0,
584            },
585          },
586          rangeLength = 6,
587          text = '\ntest3',
588        },
589      }
590      test_edit({ 'test1 test2', 'test3' }, { 'J', 'u' }, expected_text_changes, 'utf-16', '\n')
591    end)
592  end)
593
594  describe('multi-byte edits', function()
595    it('deleting a multibyte character', function()
596      local expected_text_changes = {
597        {
598          range = {
599            ['start'] = {
600              character = 0,
601              line = 0,
602            },
603            ['end'] = {
604              character = 2,
605              line = 0,
606            },
607          },
608          rangeLength = 2,
609          text = '',
610        },
611      }
612      test_edit({ '🔥' }, { 'x' }, expected_text_changes, 'utf-16', '\n')
613    end)
614    it('replacing a multibyte character with matching prefix', function()
615      local expected_text_changes = {
616        {
617          range = {
618            ['start'] = {
619              character = 0,
620              line = 1,
621            },
622            ['end'] = {
623              character = 1,
624              line = 1,
625            },
626          },
627          rangeLength = 1,
628          text = '⟩',
629        },
630      }
631      -- ⟨ is e29fa8, ⟩ is e29fa9
632      local original_lines = {
633        '\\begin{document}',
634        '⟨',
635        '\\end{document}',
636      }
637      test_edit(original_lines, { 'jr⟩' }, expected_text_changes, 'utf-16', '\n')
638    end)
639    it('replacing a multibyte character with matching suffix', function()
640      local expected_text_changes = {
641        {
642          range = {
643            ['start'] = {
644              character = 0,
645              line = 1,
646            },
647            ['end'] = {
648              character = 1,
649              line = 1,
650            },
651          },
652          rangeLength = 1,
653          text = 'ḟ',
654        },
655      }
656      -- ฟ is e0b89f, ḟ is e1b89f
657      local original_lines = {
658        '\\begin{document}',
659        'ฟ',
660        '\\end{document}',
661      }
662      test_edit(original_lines, { 'jrḟ' }, expected_text_changes, 'utf-16', '\n')
663    end)
664    it('inserting before a multibyte character', function()
665      local expected_text_changes = {
666        {
667          range = {
668            ['start'] = {
669              character = 0,
670              line = 1,
671            },
672            ['end'] = {
673              character = 0,
674              line = 1,
675            },
676          },
677          rangeLength = 0,
678          text = ' ',
679        },
680      }
681      local original_lines = {
682        '\\begin{document}',
683        '→',
684        '\\end{document}',
685      }
686      test_edit(original_lines, { 'ji ' }, expected_text_changes, 'utf-16', '\n')
687    end)
688    it('deleting a multibyte character from a long line', function()
689      local expected_text_changes = {
690        {
691          range = {
692            ['start'] = {
693              character = 85,
694              line = 1,
695            },
696            ['end'] = {
697              character = 86,
698              line = 1,
699            },
700          },
701          rangeLength = 1,
702          text = '',
703        },
704      }
705      local original_lines = {
706        '\\begin{document}',
707        '→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→',
708        '\\end{document}',
709      }
710      test_edit(original_lines, { 'jx' }, expected_text_changes, 'utf-16', '\n')
711    end)
712    it('deleting multiple lines containing multibyte characters', function()
713      local expected_text_changes = {
714        {
715          range = {
716            ['start'] = {
717              character = 0,
718              line = 1,
719            },
720            ['end'] = {
721              character = 0,
722              line = 3,
723            },
724          },
725          --utf 16 len of 🔥 is 2
726          rangeLength = 8,
727          text = '',
728        },
729      }
730      test_edit(
731        { 'a🔥', 'b🔥', 'c🔥', 'd🔥' },
732        { 'j2dd' },
733        expected_text_changes,
734        'utf-16',
735        '\n'
736      )
737    end)
738  end)
739end)
740
741-- TODO(mjlbach): Add additional tests
742-- deleting single lone line
743-- 2 lines -> 2 line delete -> undo -> redo
744-- describe('future tests', function()
745--   -- This test is currently wrong, ask bjorn why dd on an empty line triggers on_lines
746--   it('deleting an empty line', function()
747--     local expected_text_changes = {{ }}
748--     test_edit({""}, {"ggdd"}, expected_text_changes, 'utf-16', '\n')
749--   end)
750-- end)