robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
23.4 KiB820 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local api = n.api
6local clear = n.clear
7local eq = t.eq
8local exec_capture = n.exec_capture
9local exec_lua = n.exec_lua
10local feed = n.feed
11
12-- Reference text
13-- aa
14--  aa
15--   aa
16--
17--   aa
18--  aa
19-- aa
20local example_lines = { 'aa', ' aa', '  aa', '', '  aa', ' aa', 'aa' }
21
22local set_commentstring = function(commentstring)
23  api.nvim_set_option_value('commentstring', commentstring, { buf = 0 })
24end
25
26local get_lines = function(from, to)
27  from, to = from or 0, to or -1
28  return api.nvim_buf_get_lines(0, from, to, false)
29end
30
31local set_lines = function(lines, from, to)
32  from, to = from or 0, to or -1
33  api.nvim_buf_set_lines(0, from, to, false, lines)
34end
35
36local set_cursor = function(row, col)
37  api.nvim_win_set_cursor(0, { row, col })
38end
39
40local get_cursor = function()
41  return api.nvim_win_get_cursor(0)
42end
43
44local setup_treesitter = function()
45  -- NOTE: This leverages bundled Vimscript and Lua tree-sitter parsers
46  api.nvim_set_option_value('filetype', 'vim', { buf = 0 })
47  exec_lua('vim.treesitter.start()')
48end
49
50before_each(function()
51  -- avoid options, but we still need TS parsers
52  clear({ args_rm = { '--cmd' }, args = { '--clean', '--cmd', n.runtime_set } })
53end)
54
55describe('commenting', function()
56  before_each(function()
57    set_lines(example_lines)
58    set_commentstring('# %s')
59  end)
60
61  describe('toggle_lines()', function()
62    local toggle_lines = function(...)
63      exec_lua('require("vim._comment").toggle_lines(...)', ...)
64    end
65
66    it('works', function()
67      toggle_lines(3, 5)
68      eq({ '  # aa', '  #', '  # aa' }, get_lines(2, 5))
69
70      toggle_lines(3, 5)
71      eq({ '  aa', '', '  aa' }, get_lines(2, 5))
72    end)
73
74    it("works with different 'commentstring' options", function()
75      local validate = function(lines_before, lines_after, lines_again)
76        set_lines(lines_before)
77        toggle_lines(1, #lines_before)
78        eq(lines_after, get_lines())
79        toggle_lines(1, #lines_before)
80        eq(lines_again or lines_before, get_lines())
81      end
82
83      -- Single whitespace inside comment parts (main case)
84      set_commentstring('# %s #')
85      -- - General case
86      validate(
87        { 'aa', '  aa', 'aa  ', '  aa  ' },
88        { '# aa #', '#   aa #', '# aa   #', '#   aa   #' }
89      )
90      -- - Tabs
91      validate(
92        { 'aa', '\taa', 'aa\t', '\taa\t' },
93        { '# aa #', '# \taa #', '# aa\t #', '# \taa\t #' }
94      )
95      -- - With indent
96      validate({ ' aa', '  aa' }, { ' # aa #', ' #  aa #' })
97      -- - With blank/empty lines
98      validate(
99        { '  aa', '', '  ', '\t' },
100        { '  # aa #', '  ##', '  ##', '  ##' },
101        { '  aa', '', '', '' }
102      )
103
104      set_commentstring('# %s')
105      validate({ 'aa', '  aa', 'aa  ', '  aa  ' }, { '# aa', '#   aa', '# aa  ', '#   aa  ' })
106      validate({ 'aa', '\taa', 'aa\t', '\taa\t' }, { '# aa', '# \taa', '# aa\t', '# \taa\t' })
107      validate({ ' aa', '  aa' }, { ' # aa', ' #  aa' })
108      validate(
109        { '  aa', '', '  ', '\t' },
110        { '  # aa', '  #', '  #', '  #' },
111        { '  aa', '', '', '' }
112      )
113
114      set_commentstring('%s #')
115      validate({ 'aa', '  aa', 'aa  ', '  aa  ' }, { 'aa #', '  aa #', 'aa   #', '  aa   #' })
116      validate({ 'aa', '\taa', 'aa\t', '\taa\t' }, { 'aa #', '\taa #', 'aa\t #', '\taa\t #' })
117      validate({ ' aa', '  aa' }, { ' aa #', '  aa #' })
118      validate(
119        { '  aa', '', '  ', '\t' },
120        { '  aa #', '  #', '  #', '  #' },
121        { '  aa', '', '', '' }
122      )
123
124      -- No whitespace in parts
125      set_commentstring('#%s#')
126      validate({ 'aa', '  aa', 'aa  ', '  aa  ' }, { '#aa#', '#  aa#', '#aa  #', '#  aa  #' })
127      validate({ 'aa', '\taa', 'aa\t', '\taa\t' }, { '#aa#', '#\taa#', '#aa\t#', '#\taa\t#' })
128      validate({ ' aa', '  aa' }, { ' #aa#', ' # aa#' })
129      validate(
130        { '  aa', '', '  ', '\t' },
131        { '  #aa#', '  ##', '  ##', '  ##' },
132        { '  aa', '', '', '' }
133      )
134
135      set_commentstring('#%s')
136      validate({ 'aa', '  aa', 'aa  ', '  aa  ' }, { '#aa', '#  aa', '#aa  ', '#  aa  ' })
137      validate({ 'aa', '\taa', 'aa\t', '\taa\t' }, { '#aa', '#\taa', '#aa\t', '#\taa\t' })
138      validate({ ' aa', '  aa' }, { ' #aa', ' # aa' })
139      validate({ '  aa', '', '  ', '\t' }, { '  #aa', '  #', '  #', '  #' }, { '  aa', '', '', '' })
140
141      set_commentstring('%s#')
142      validate({ 'aa', '  aa', 'aa  ', '  aa  ' }, { 'aa#', '  aa#', 'aa  #', '  aa  #' })
143      validate({ 'aa', '\taa', 'aa\t', '\taa\t' }, { 'aa#', '\taa#', 'aa\t#', '\taa\t#' })
144      validate({ ' aa', '  aa' }, { ' aa#', '  aa#' })
145      validate({ '  aa', '', '  ', '\t' }, { '  aa#', '  #', '  #', '  #' }, { '  aa', '', '', '' })
146
147      -- Extra whitespace inside comment parts
148      set_commentstring('#  %s  #')
149      validate(
150        { 'aa', '  aa', 'aa  ', '  aa  ' },
151        { '#  aa  #', '#    aa  #', '#  aa    #', '#    aa    #' }
152      )
153      validate(
154        { 'aa', '\taa', 'aa\t', '\taa\t' },
155        { '#  aa  #', '#  \taa  #', '#  aa\t  #', '#  \taa\t  #' }
156      )
157      validate({ ' aa', '  aa' }, { ' #  aa  #', ' #   aa  #' })
158      validate(
159        { '  aa', '', '  ', '\t' },
160        { '  #  aa  #', '  ##', '  ##', '  ##' },
161        { '  aa', '', '', '' }
162      )
163
164      set_commentstring('#  %s')
165      validate({ 'aa', '  aa', 'aa  ', '  aa  ' }, { '#  aa', '#    aa', '#  aa  ', '#    aa  ' })
166      validate({ 'aa', '\taa', 'aa\t', '\taa\t' }, { '#  aa', '#  \taa', '#  aa\t', '#  \taa\t' })
167      validate({ ' aa', '  aa' }, { ' #  aa', ' #   aa' })
168      validate(
169        { '  aa', '', '  ', '\t' },
170        { '  #  aa', '  #', '  #', '  #' },
171        { '  aa', '', '', '' }
172      )
173
174      set_commentstring('%s  #')
175      validate({ 'aa', '  aa', 'aa  ', '  aa  ' }, { 'aa  #', '  aa  #', 'aa    #', '  aa    #' })
176      validate({ 'aa', '\taa', 'aa\t', '\taa\t' }, { 'aa  #', '\taa  #', 'aa\t  #', '\taa\t  #' })
177      validate({ ' aa', '  aa' }, { ' aa  #', '  aa  #' })
178      validate(
179        { '  aa', '', '  ', '\t' },
180        { '  aa  #', '  #', '  #', '  #' },
181        { '  aa', '', '', '' }
182      )
183
184      -- Whitespace outside of comment parts
185      set_commentstring(' # %s # ')
186      validate(
187        { 'aa', '  aa', 'aa  ', '  aa  ' },
188        { ' # aa # ', ' #   aa # ', ' # aa   # ', ' #   aa   # ' }
189      )
190      validate(
191        { 'aa', '\taa', 'aa\t', '\taa\t' },
192        { ' # aa # ', ' # \taa # ', ' # aa\t # ', ' # \taa\t # ' }
193      )
194      validate({ ' aa', '  aa' }, { '  # aa # ', '  #  aa # ' })
195      validate(
196        { '  aa', '', '  ', '\t' },
197        { '   # aa # ', '  ##', '  ##', '  ##' },
198        { '  aa', '', '', '' }
199      )
200
201      set_commentstring(' # %s ')
202      validate(
203        { 'aa', '  aa', 'aa  ', '  aa  ' },
204        { ' # aa ', ' #   aa ', ' # aa   ', ' #   aa   ' }
205      )
206      validate(
207        { 'aa', '\taa', 'aa\t', '\taa\t' },
208        { ' # aa ', ' # \taa ', ' # aa\t ', ' # \taa\t ' }
209      )
210      validate({ ' aa', '  aa' }, { '  # aa ', '  #  aa ' })
211      validate(
212        { '  aa', '', '  ', '\t' },
213        { '   # aa ', '  #', '  #', '  #' },
214        { '  aa', '', '', '' }
215      )
216
217      set_commentstring(' %s # ')
218      validate(
219        { 'aa', '  aa', 'aa  ', '  aa  ' },
220        { ' aa # ', '   aa # ', ' aa   # ', '   aa   # ' }
221      )
222      validate(
223        { 'aa', '\taa', 'aa\t', '\taa\t' },
224        { ' aa # ', ' \taa # ', ' aa\t # ', ' \taa\t # ' }
225      )
226      validate({ ' aa', '  aa' }, { '  aa # ', '   aa # ' })
227      validate(
228        { '  aa', '', '  ', '\t' },
229        { '   aa # ', '  #', '  #', '  #' },
230        { '  aa', '', '', '' }
231      )
232
233      -- LaTeX
234      set_commentstring('% %s')
235      validate({ 'aa', '  aa', 'aa  ', '  aa  ' }, { '% aa', '%   aa', '% aa  ', '%   aa  ' })
236      validate({ 'aa', '\taa', 'aa\t', '\taa\t' }, { '% aa', '% \taa', '% aa\t', '% \taa\t' })
237      validate({ ' aa', '  aa' }, { ' % aa', ' %  aa' })
238      validate(
239        { '  aa', '', '  ', '\t' },
240        { '  % aa', '  %', '  %', '  %' },
241        { '  aa', '', '', '' }
242      )
243    end)
244
245    it('respects tree-sitter injections', function()
246      setup_treesitter()
247
248      local lines = {
249        'set background=dark',
250        'lua << EOF',
251        'print(1)',
252        'vim.api.nvim_exec2([[',
253        '    set background=light',
254        ']])',
255        'EOF',
256      }
257
258      -- Single line comments
259      local validate = function(line, ref_output)
260        set_lines(lines)
261        toggle_lines(line, line)
262        eq(ref_output, get_lines(line - 1, line)[1])
263      end
264
265      validate(1, '"set background=dark')
266      validate(2, '"lua << EOF')
267      validate(3, '-- print(1)')
268      validate(4, '-- vim.api.nvim_exec2([[')
269      validate(5, '    "set background=light')
270      validate(6, '-- ]])')
271      validate(7, '"EOF')
272
273      -- Multiline comments should be computed based on first line 'commentstring'
274      set_lines(lines)
275      toggle_lines(1, 3)
276      local out_lines = get_lines()
277      eq('"set background=dark', out_lines[1])
278      eq('"lua << EOF', out_lines[2])
279      eq('"print(1)', out_lines[3])
280    end)
281
282    it('correctly computes indent', function()
283      toggle_lines(2, 4)
284      eq({ ' # aa', ' #  aa', ' #' }, get_lines(1, 4))
285    end)
286
287    it('correctly detects comment/uncomment', function()
288      local validate = function(from, to, ref_lines)
289        set_lines({ '', 'aa', '# aa', '# aa', 'aa', '' })
290        toggle_lines(from, to)
291        eq(ref_lines, get_lines())
292      end
293
294      -- It should uncomment only if all non-blank lines are comments
295      validate(3, 4, { '', 'aa', 'aa', 'aa', 'aa', '' })
296      validate(2, 4, { '', '# aa', '# # aa', '# # aa', 'aa', '' })
297      validate(3, 5, { '', 'aa', '# # aa', '# # aa', '# aa', '' })
298      validate(1, 6, { '#', '# aa', '# # aa', '# # aa', '# aa', '#' })
299
300      -- Blank lines should be ignored when making a decision
301      set_lines({ '# aa', '', '  ', '\t', '# aa' })
302      toggle_lines(1, 5)
303      eq({ 'aa', '', '  ', '\t', 'aa' }, get_lines())
304    end)
305
306    it('correctly matches comment parts during checking and uncommenting', function()
307      local validate = function(from, to, ref_lines)
308        set_lines({ '/*aa*/', '/* aa */', '/*  aa  */' })
309        toggle_lines(from, to)
310        eq(ref_lines, get_lines())
311      end
312
313      -- Should first try to match 'commentstring' parts exactly with their
314      -- whitespace, with fallback on trimmed parts
315      set_commentstring('/*%s*/')
316      validate(1, 3, { 'aa', ' aa ', '  aa  ' })
317      validate(2, 3, { '/*aa*/', ' aa ', '  aa  ' })
318      validate(3, 3, { '/*aa*/', '/* aa */', '  aa  ' })
319
320      set_commentstring('/* %s */')
321      validate(1, 3, { 'aa', 'aa', ' aa ' })
322      validate(2, 3, { '/*aa*/', 'aa', ' aa ' })
323      validate(3, 3, { '/*aa*/', '/* aa */', ' aa ' })
324
325      set_commentstring('/*  %s  */')
326      validate(1, 3, { 'aa', ' aa ', 'aa' })
327      validate(2, 3, { '/*aa*/', ' aa ', 'aa' })
328      validate(3, 3, { '/*aa*/', '/* aa */', 'aa' })
329
330      set_commentstring(' /*%s*/ ')
331      validate(1, 3, { 'aa', ' aa ', '  aa  ' })
332      validate(2, 3, { '/*aa*/', ' aa ', '  aa  ' })
333      validate(3, 3, { '/*aa*/', '/* aa */', '  aa  ' })
334    end)
335
336    it('uncomments on inconsistent indent levels', function()
337      set_lines({ '# aa', ' # aa', '  # aa' })
338      toggle_lines(1, 3)
339      eq({ 'aa', ' aa', '  aa' }, get_lines())
340    end)
341
342    it('respects tabs', function()
343      api.nvim_set_option_value('expandtab', false, { buf = 0 })
344      set_lines({ '\t\taa', '\t\taa' })
345
346      toggle_lines(1, 2)
347      eq({ '\t\t# aa', '\t\t# aa' }, get_lines())
348
349      toggle_lines(1, 2)
350      eq({ '\t\taa', '\t\taa' }, get_lines())
351    end)
352
353    it('works with trailing whitespace', function()
354      -- Without right-hand side
355      set_commentstring('# %s')
356      set_lines({ ' aa', ' aa  ', '  ' })
357      toggle_lines(1, 3)
358      eq({ ' # aa', ' # aa  ', ' #' }, get_lines())
359      toggle_lines(1, 3)
360      eq({ ' aa', ' aa  ', '' }, get_lines())
361
362      -- With right-hand side
363      set_commentstring('%s #')
364      set_lines({ ' aa', ' aa  ', '  ' })
365      toggle_lines(1, 3)
366      eq({ ' aa #', ' aa   #', ' #' }, get_lines())
367      toggle_lines(1, 3)
368      eq({ ' aa', ' aa  ', '' }, get_lines())
369
370      -- Trailing whitespace after right side should be preserved for non-blanks
371      set_commentstring('%s #')
372      set_lines({ ' aa #  ', ' aa #\t', ' #  ', ' #\t' })
373      toggle_lines(1, 4)
374      eq({ ' aa  ', ' aa\t', '', '' }, get_lines())
375    end)
376  end)
377
378  describe('Operator', function()
379    it('works in Normal mode', function()
380      set_cursor(2, 2)
381      feed('gc', 'ap')
382      eq({ '# aa', '#  aa', '#   aa', '#', '  aa', ' aa', 'aa' }, get_lines())
383      -- Cursor moves to start line
384      eq({ 1, 0 }, get_cursor())
385
386      -- Supports `v:count`
387      set_lines(example_lines)
388      set_cursor(2, 0)
389      feed('2gc', 'ap')
390      eq({ '# aa', '#  aa', '#   aa', '#', '#   aa', '#  aa', '# aa' }, get_lines())
391    end)
392
393    it('allows dot-repeat in Normal mode', function()
394      local doubly_commented = { '# # aa', '# #  aa', '# #   aa', '# #', '#   aa', '#  aa', '# aa' }
395
396      set_lines(example_lines)
397      set_cursor(2, 2)
398      feed('gc', 'ap')
399      feed('.')
400      eq(doubly_commented, get_lines())
401
402      -- Not immediate dot-repeat
403      set_lines(example_lines)
404      set_cursor(2, 2)
405      feed('gc', 'ap')
406      set_cursor(7, 0)
407      feed('.')
408      eq(doubly_commented, get_lines())
409    end)
410
411    it('works in Visual mode', function()
412      set_cursor(2, 2)
413      feed('v', 'ap', 'gc')
414      eq({ '# aa', '#  aa', '#   aa', '#', '  aa', ' aa', 'aa' }, get_lines())
415
416      -- Cursor moves to start line
417      eq({ 1, 0 }, get_cursor())
418    end)
419
420    it('allows dot-repeat after initial Visual mode', function()
421      -- local example_lines = { 'aa', ' aa', '  aa', '', '  aa', ' aa', 'aa' }
422
423      set_lines(example_lines)
424      set_cursor(2, 2)
425      feed('vip', 'gc')
426      eq({ '# aa', '#  aa', '#   aa', '', '  aa', ' aa', 'aa' }, get_lines())
427      eq({ 1, 0 }, get_cursor())
428
429      -- Dot-repeat after first application in Visual mode applies to the paragraph at cursor (not
430      -- a fixed-size region).
431      feed('.')
432      eq(example_lines, get_lines())
433
434      set_cursor(3, 0)
435      feed('.')
436      eq({ '# aa', '#  aa', '#   aa', '', '  aa', ' aa', 'aa' }, get_lines())
437    end)
438
439    it("respects 'commentstring'", function()
440      set_commentstring('/*%s*/')
441      set_cursor(2, 2)
442      feed('gc', 'ap')
443      eq({ '/*aa*/', '/* aa*/', '/*  aa*/', '/**/', '  aa', ' aa', 'aa' }, get_lines())
444    end)
445
446    it("works with empty 'commentstring'", function()
447      set_commentstring('')
448      set_cursor(2, 2)
449      feed('gc', 'ap')
450      eq(example_lines, get_lines())
451      eq([[Option 'commentstring' is empty.]], exec_capture('1messages'))
452    end)
453
454    it('respects tree-sitter injections', function()
455      setup_treesitter()
456
457      local lines = {
458        'set background=dark',
459        'lua << EOF',
460        'print(1)',
461        'vim.api.nvim_exec2([[',
462        '    set background=light',
463        ']])',
464        'EOF',
465      }
466
467      -- Single line comments
468      local validate = function(line, ref_output)
469        set_lines(lines)
470        set_cursor(line, 0)
471        feed('gc_')
472        eq(ref_output, get_lines(line - 1, line)[1])
473      end
474
475      validate(1, '"set background=dark')
476      validate(2, '"lua << EOF')
477      validate(3, '-- print(1)')
478      validate(4, '-- vim.api.nvim_exec2([[')
479      validate(5, '    "set background=light')
480      validate(6, '-- ]])')
481      validate(7, '"EOF')
482
483      -- Has proper dot-repeat which recomputes 'commentstring'
484      set_lines(lines)
485
486      set_cursor(1, 0)
487      feed('gc_')
488      eq('"set background=dark', get_lines()[1])
489
490      set_cursor(3, 0)
491      feed('.')
492      eq('-- print(1)', get_lines()[3])
493
494      -- Multiline comments should be computed based on cursor position
495      -- which in case of Visual selection means its left part
496      set_lines(lines)
497      set_cursor(1, 0)
498      feed('v2j', 'gc')
499      local out_lines = get_lines()
500      eq('"set background=dark', out_lines[1])
501      eq('"lua << EOF', out_lines[2])
502      eq('"print(1)', out_lines[3])
503    end)
504
505    it("recomputes local 'commentstring' based on cursor position", function()
506      setup_treesitter()
507      local lines = {
508        '  print(1)',
509        'lua << EOF',
510        '  print(1)',
511        'EOF',
512      }
513      set_lines(lines)
514
515      set_cursor(1, 1)
516      feed('gc_')
517      eq('  "print(1)', get_lines()[1])
518
519      set_lines(lines)
520      set_cursor(3, 2)
521      feed('.')
522      eq('  -- print(1)', get_lines()[3])
523    end)
524
525    it('preserves marks', function()
526      set_cursor(2, 0)
527      -- Set '`<' and '`>' marks
528      feed('VV')
529      feed('gc', 'ip')
530      eq({ 2, 0 }, api.nvim_buf_get_mark(0, '<'))
531      eq({ 2, 2147483647 }, api.nvim_buf_get_mark(0, '>'))
532    end)
533  end)
534
535  describe('Current line', function()
536    it('works', function()
537      set_lines(example_lines)
538      set_cursor(1, 1)
539      feed('gcc')
540      eq({ '# aa', ' aa' }, get_lines(0, 2))
541
542      -- Does not comment empty line
543      set_lines(example_lines)
544      set_cursor(4, 0)
545      feed('gcc')
546      eq({ '  aa', '', '  aa' }, get_lines(2, 5))
547
548      -- Supports `v:count`
549      set_lines(example_lines)
550      set_cursor(2, 0)
551      feed('2gcc')
552      eq({ 'aa', ' # aa', ' #  aa' }, get_lines(0, 3))
553    end)
554
555    it('allows dot-repeat', function()
556      set_lines(example_lines)
557      set_cursor(1, 1)
558      feed('gcc')
559      feed('.')
560      eq(example_lines, get_lines())
561
562      -- Not immediate dot-repeat
563      set_lines(example_lines)
564      set_cursor(1, 1)
565      feed('gcc')
566      set_cursor(7, 0)
567      feed('.')
568      eq({ '# aa' }, get_lines(6, 7))
569    end)
570
571    it('respects tree-sitter injections', function()
572      setup_treesitter()
573
574      local lines = {
575        'set background=dark',
576        'lua << EOF',
577        'print(1)',
578        'EOF',
579      }
580      set_lines(lines)
581
582      set_cursor(1, 0)
583      feed('gcc')
584      eq({ '"set background=dark', 'lua << EOF', 'print(1)', 'EOF' }, get_lines())
585
586      -- Should work with dot-repeat
587      set_cursor(3, 0)
588      feed('.')
589      eq({ '"set background=dark', 'lua << EOF', '-- print(1)', 'EOF' }, get_lines())
590    end)
591
592    it('respects tree-sitter commentstring metadata', function()
593      exec_lua [=[
594        vim.treesitter.query.set('vim', 'highlights', [[
595          ((list) @_list (#set! @_list bo.commentstring "!! %s"))
596        ]])
597      ]=]
598      setup_treesitter()
599
600      local lines = {
601        'set background=dark',
602        'let mylist = [',
603        [[  \"a",]],
604        [[  \"b",]],
605        [[  \"c",]],
606        '  \\]',
607      }
608      set_lines(lines)
609
610      set_cursor(1, 0)
611      feed('gcc')
612      eq(
613        { '"set background=dark', 'let mylist = [', [[  \"a",]], [[  \"b",]], [[  \"c",]], '  \\]' },
614        get_lines()
615      )
616
617      -- Should work with dot-repeat
618      set_cursor(4, 0)
619      feed('.')
620      eq({
621        '"set background=dark',
622        'let mylist = [',
623        [[  \"a",]],
624        [[  !! \"b",]],
625        [[  \"c",]],
626        '  \\]',
627      }, get_lines())
628    end)
629
630    it('only applies the innermost tree-sitter commentstring metadata', function()
631      exec_lua [=[
632        vim.treesitter.query.set('vim', 'highlights', [[
633          ((list) @_list (#gsub! @_list "(.*)" "%1") (#set! bo.commentstring "!! %s"))
634          ((script_file) @_src (#set! @_src bo.commentstring "## %s"))
635        ]])
636      ]=]
637      setup_treesitter()
638
639      local lines = {
640        'set background=dark',
641        'let mylist = [',
642        [[  \"a",]],
643        [[  \"b",]],
644        [[  \"c",]],
645        '  \\]',
646      }
647      set_lines(lines)
648
649      set_cursor(1, 0)
650      feed('gcc')
651      eq({
652        '## set background=dark',
653        'let mylist = [',
654        [[  \"a",]],
655        [[  \"b",]],
656        [[  \"c",]],
657        '  \\]',
658      }, get_lines())
659
660      -- Should work with dot-repeat
661      set_cursor(4, 0)
662      feed('.')
663      eq({
664        '## set background=dark',
665        'let mylist = [',
666        [[  \"a",]],
667        [[  !! \"b",]],
668        [[  \"c",]],
669        '  \\]',
670      }, get_lines())
671    end)
672
673    it('respects injected tree-sitter commentstring metadata', function()
674      exec_lua [=[
675        vim.treesitter.query.set('lua', 'highlights', [[
676          ((string) @string (#set! @string bo.commentstring "; %s"))
677        ]])
678      ]=]
679      setup_treesitter()
680
681      local lines = {
682        'set background=dark',
683        'lua << EOF',
684        'print[[',
685        'Inside string',
686        ']]',
687        'EOF',
688      }
689      set_lines(lines)
690
691      set_cursor(1, 0)
692      feed('gcc')
693      eq({
694        '"set background=dark',
695        'lua << EOF',
696        'print[[',
697        'Inside string',
698        ']]',
699        'EOF',
700      }, get_lines())
701
702      -- Should work with dot-repeat
703      set_cursor(4, 0)
704      feed('.')
705      eq({
706        '"set background=dark',
707        'lua << EOF',
708        'print[[',
709        '; Inside string',
710        ']]',
711        'EOF',
712      }, get_lines())
713
714      set_cursor(3, 0)
715      feed('.')
716      eq({
717        '"set background=dark',
718        'lua << EOF',
719        '-- print[[',
720        '; Inside string',
721        ']]',
722        'EOF',
723      }, get_lines())
724    end)
725
726    it('works across combined injections #30799', function()
727      exec_lua [=[
728        vim.treesitter.query.set('lua', 'injections', [[
729          ((function_call
730            name: (_) @_vimcmd_identifier
731            arguments: (arguments
732              (string
733                content: _ @injection.content)))
734            (#eq? @_vimcmd_identifier "vim.cmd")
735            (#set! injection.language "vim")
736            (#set! injection.combined))
737        ]])
738      ]=]
739
740      api.nvim_set_option_value('filetype', 'lua', { buf = 0 })
741      exec_lua('vim.treesitter.start()')
742
743      local lines = {
744        'vim.cmd([[" some text]])',
745        'local a = 123',
746        'vim.cmd([[" some more text]])',
747      }
748      set_lines(lines)
749
750      set_cursor(2, 0)
751      feed('gcc')
752      eq({
753        'vim.cmd([[" some text]])',
754        '-- local a = 123',
755        'vim.cmd([[" some more text]])',
756      }, get_lines())
757    end)
758  end)
759
760  describe('Textobject', function()
761    it('works', function()
762      set_lines({ 'aa', '# aa', '# aa', 'aa' })
763      set_cursor(2, 0)
764      feed('d', 'gc')
765      eq({ 'aa', 'aa' }, get_lines())
766    end)
767
768    it('allows dot-repeat', function()
769      set_lines({ 'aa', '# aa', '# aa', 'aa', '# aa' })
770      set_cursor(2, 0)
771      feed('d', 'gc')
772      set_cursor(3, 0)
773      feed('.')
774      eq({ 'aa', 'aa' }, get_lines())
775    end)
776
777    it('does nothing when not inside textobject', function()
778      -- Builtin operators
779      feed('d', 'gc')
780      eq(example_lines, get_lines())
781
782      -- Comment operator
783      local validate_no_action = function(line, col)
784        set_lines(example_lines)
785        set_cursor(line, col)
786        feed('gc', 'gc')
787        eq(example_lines, get_lines())
788      end
789
790      validate_no_action(1, 1)
791      validate_no_action(2, 2)
792
793      -- Doesn't work (but should) because both `[` and `]` are set to (1, 0)
794      -- (instead of more reasonable (1, -1) or (0, 2147483647)).
795      -- validate_no_action(1, 0)
796    end)
797
798    it('respects tree-sitter injections', function()
799      setup_treesitter()
800      local lines = {
801        '"set background=dark',
802        '"set termguicolors',
803        'lua << EOF',
804        '-- print(1)',
805        '-- print(2)',
806        'EOF',
807      }
808      set_lines(lines)
809
810      set_cursor(1, 0)
811      feed('dgc')
812      eq({ 'lua << EOF', '-- print(1)', '-- print(2)', 'EOF' }, get_lines())
813
814      -- Should work with dot-repeat
815      set_cursor(2, 0)
816      feed('.')
817      eq({ 'lua << EOF', 'EOF' }, get_lines())
818    end)
819  end)
820end)