-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathtest_ai.lua
More file actions
3556 lines (2863 loc) · 126 KB
/
Copy pathtest_ai.lua
File metadata and controls
3556 lines (2863 loc) · 126 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local helpers = dofile('tests/helpers.lua')
local child = helpers.new_child_neovim()
local expect, eq = helpers.expect, helpers.expect.equality
local new_set = MiniTest.new_set
-- Helpers with child processes
local load_module = function(config) child.mini_load('ai', config) end
local unload_module = function() child.mini_unload('ai') end
local reload_module = function(config) child.mini_reload('ai', config) end
local set_cursor = function(...) return child.set_cursor(...) end
local get_cursor = function(...) return child.get_cursor(...) end
local set_lines = function(...) return child.set_lines(...) end
local get_lines = function(...) return child.get_lines(...) end
local type_keys = function(...) return child.type_keys(...) end
local sleep = function(ms) helpers.sleep(ms, child) end
local validate_edit = function(...) return child.validate_edit(...) end
local validate_edit1d = function(...) return child.validate_edit1d(...) end
local get_latest_message = function() return child.cmd_capture('1messages') end
local get_mode = function() return child.api.nvim_get_mode()['mode'] end
local validate_next_region = function(keys, next_region)
type_keys(keys)
-- Should put cursor at the right edge of selection (as it is done for
-- built-in textobjects in Visual mode)
eq({ { child.fn.line('v'), child.fn.col('v') }, { child.fn.line('.'), child.fn.col('.') } }, next_region)
end
local validate_next_region1d = function(keys, next_region)
type_keys(keys)
eq({ child.fn.col('v'), child.fn.col('.') }, next_region)
end
local validate_tobj = function(lines, cursor, keys, expected, vis_mode)
vis_mode = vim.api.nvim_replace_termcodes(vis_mode or 'v', true, true, true)
child.ensure_normal_mode()
set_lines(lines)
set_cursor(unpack(cursor))
type_keys(vis_mode, keys)
eq(get_mode(), vis_mode)
-- Allow supplying number items to verify linewise selection
local expected_from = type(expected[1]) == 'number' and expected[1] or { expected[1][1], expected[1][2] - 1 }
local expected_to = type(expected[2]) == 'number' and expected[2] or { expected[2][1], expected[2][2] - 1 }
child.expect_visual_marks(expected_from, expected_to)
end
local validate_tobj1d = function(line, column, keys, expected)
validate_tobj({ line }, { 1, column }, keys, { { 1, expected[1] }, { 1, expected[2] } })
end
local validate_no_tobj = function(lines, cursor, keys, vis_mode)
vis_mode = vim.api.nvim_replace_termcodes(vis_mode or 'v', true, true, true)
child.ensure_normal_mode()
set_lines(lines)
set_cursor(unpack(cursor))
type_keys(vis_mode, keys)
eq(get_mode(), 'n')
eq(get_cursor(), cursor)
expect.match(get_latest_message(), 'No textobject')
end
local validate_no_tobj1d = function(line, column, keys) validate_no_tobj({ line }, { 1, column }, keys) end
-- Time constants
local reminder_delay = 1000
local small_time = helpers.get_time_const(10)
-- Output test set
local T = new_set({
hooks = {
pre_case = function()
child.setup()
load_module()
end,
post_once = child.stop,
},
n_retry = helpers.get_n_retry(1),
})
-- Unit tests =================================================================
T['setup()'] = new_set()
T['setup()']['creates side effects'] = function()
-- Global variable
eq(child.lua_get('type(_G.MiniAi)'), 'table')
end
T['setup()']['creates `config` field'] = function()
eq(child.lua_get('type(_G.MiniAi.config)'), 'table')
-- Check default values
local expect_config = function(field, value) eq(child.lua_get('MiniAi.config.' .. field), value) end
-- Check default values
expect_config('custom_textobjects', {})
expect_config('mappings.around', 'a')
expect_config('mappings.inside', 'i')
expect_config('mappings.around_next', 'an')
expect_config('mappings.inside_next', 'in')
expect_config('mappings.around_last', 'al')
expect_config('mappings.inside_last', 'il')
expect_config('mappings.goto_left', 'g[')
expect_config('mappings.goto_right', 'g]')
expect_config('n_lines', 50)
expect_config('search_method', 'cover_or_next')
expect_config('silent', false)
end
T['setup()']['respects `config` argument'] = function()
unload_module()
load_module({ n_lines = 10 })
eq(child.lua_get('MiniAi.config.n_lines'), 10)
end
T['setup()']['validates `config` argument'] = function()
unload_module()
local expect_config_error = function(config, name, target_type)
expect.error(function() load_module(config) end, vim.pesc(name) .. '.*' .. vim.pesc(target_type))
end
expect_config_error('a', 'config', 'table')
expect_config_error({ custom_textobjects = 'a' }, 'custom_textobjects', 'table')
expect_config_error({ mappings = 'a' }, 'mappings', 'table')
expect_config_error({ mappings = { around = 1 } }, 'mappings.around', 'string')
expect_config_error({ mappings = { inside = 1 } }, 'mappings.inside', 'string')
expect_config_error({ mappings = { around_next = 1 } }, 'mappings.around_next', 'string')
expect_config_error({ mappings = { inside_next = 1 } }, 'mappings.inside_next', 'string')
expect_config_error({ mappings = { around_last = 1 } }, 'mappings.around_last', 'string')
expect_config_error({ mappings = { inside_last = 1 } }, 'mappings.inside_last', 'string')
expect_config_error({ mappings = { goto_left = 1 } }, 'mappings.goto_left', 'string')
expect_config_error({ mappings = { goto_right = 1 } }, 'mappings.goto_right', 'string')
expect_config_error({ n_lines = 'a' }, 'n_lines', 'number')
expect_config_error({ search_method = 1 }, 'search_method', 'one of')
expect_config_error({ silent = 1 }, 'silent', 'boolean')
end
T['setup()']['properly handles `config.mappings`'] = function()
local has_map = function(lhs, pattern) return child.cmd_capture('xmap ' .. lhs):find(pattern) ~= nil end
eq(has_map('a', 'Around'), true)
unload_module()
child.api.nvim_del_keymap('x', 'a')
child.api.nvim_del_keymap('x', 'an')
child.api.nvim_del_keymap('x', 'al')
-- Supplying empty string should mean "don't create keymap"
load_module({ mappings = { around = '', around_next = '', around_last = '' } })
eq(has_map('a', 'Around'), false)
end
local find_textobject = function(...) return child.lua_get('MiniAi.find_textobject(...)', { ... }) end
local validate_find = function(lines, cursor, args, expected)
set_lines(lines)
set_cursor(cursor[1], cursor[2])
local new_expected
if expected == nil then
new_expected = vim.NIL
else
new_expected = {
from = { line = expected[1][1], col = expected[1][2] },
to = { line = expected[2][1], col = expected[2][2] },
vis_mode = expected.vis_mode,
}
end
eq(find_textobject(unpack(args)), new_expected)
end
local validate_find1d = function(line, column, args, expected)
local new_expected
if expected ~= nil then new_expected = { { 1, expected[1] }, { 1, expected[2] } } end
validate_find({ line }, { 1, column }, args, new_expected)
end
T['find_textobject()'] = new_set()
T['find_textobject()']['works'] = function()
validate_find1d('aa(bb)cc', 3, { 'a', ')' }, { 3, 6 })
-- Should use current selection in Visual mode as default reference region
set_lines({ '((aa)(bb))' })
set_cursor(1, 3)
type_keys('v')
set_cursor(1, 6)
eq(find_textobject('a', ')'), { from = { line = 1, col = 1 }, to = { line = 1, col = 10 } })
end
T['find_textobject()']['respects `id` argument'] = function() validate_find1d('(aa[bb]cc)', 4, { 'a', ']' }, { 4, 7 }) end
T['find_textobject()']['respects `ai_type` argument'] = function()
validate_find1d('aa(bb)cc', 3, { 'i', ')' }, { 4, 5 })
end
T['find_textobject()']['respects `opts.n_lines`'] = function()
local lines = { '(', '', 'a', '', ')' }
validate_find(lines, { 3, 1 }, { 'a', ')', { n_lines = 1 } }, nil)
validate_find(lines, { 3, 1 }, { 'a', ')', { n_lines = 2 } }, { { 1, 1 }, { 5, 1 } })
-- Should handle 0
validate_find(lines, { 3, 1 }, { 'a', ')', { n_lines = 0 } }, nil)
end
T['find_textobject()']['respects `opts.n_times`'] = function()
local line, column = '(aa(bb)cc)', 4
validate_find1d(line, column, { 'a', ')', { n_times = 1 } }, { 4, 7 })
validate_find1d(line, column, { 'a', ')', { n_times = 2 } }, { 1, 10 })
validate_find1d(line, column, { 'i', ')', { n_times = 2 } }, { 2, 9 })
-- Should handle 0
validate_find1d(line, 0, { 'a', ')', { n_times = 0 } }, nil)
end
T['find_textobject()']['respects `opts.reference_region`'] = function()
local line = 'aa(bb(cc)dd)ee'
local new_opts = function(from, to)
return { reference_region = { from = { line = 1, col = from }, to = { line = 1, col = to } } }
end
validate_find1d(line, 0, { 'a', ')', new_opts(7, 7) }, { 6, 9 })
validate_find1d(line, 0, { 'a', ')', new_opts(7, 9) }, { 6, 9 })
validate_find1d(line, 0, { 'a', ')', new_opts(6, 8) }, { 6, 9 })
-- Even if reference region is a valid text object, it should select another
-- one. This enables evolving of textobjects during consecutive calls.
validate_find1d(line, 0, { 'a', ')', new_opts(6, 9) }, { 3, 12 })
validate_find1d(line, 0, { 'a', ')', new_opts(3, 12) }, nil)
-- Allows empty reference region
local empty_opts = { reference_region = { from = { line = 1, col = 6 } } }
validate_find1d(line, 0, { 'a', ')', empty_opts }, { 6, 9 })
end
T['find_textobject()']['respects `opts.search_method`'] = function()
local line = '(aa)bbb(cc)'
local new_opts = function(search_method) return { search_method = search_method } end
-- By default should be 'cover_or_next'
validate_find1d(line, 4, { 'a', ')' }, { 8, 11 })
validate_find1d(line, 1, { 'a', ')', new_opts('cover_or_next') }, { 1, 4 })
validate_find1d(line, 4, { 'a', ')', new_opts('cover_or_next') }, { 8, 11 })
validate_find1d(line, 8, { 'a', ')', new_opts('cover_or_next') }, { 8, 11 })
validate_find1d(line, 1, { 'a', ')', new_opts('cover') }, { 1, 4 })
validate_find1d(line, 4, { 'a', ')', new_opts('cover') }, nil)
validate_find1d(line, 8, { 'a', ')', new_opts('cover') }, { 8, 11 })
validate_find1d(line, 1, { 'a', ')', new_opts('cover_or_prev') }, { 1, 4 })
validate_find1d(line, 4, { 'a', ')', new_opts('cover_or_prev') }, { 1, 4 })
validate_find1d(line, 8, { 'a', ')', new_opts('cover_or_prev') }, { 8, 11 })
validate_find1d(line, 1, { 'a', ')', new_opts('cover_or_nearest') }, { 1, 4 })
validate_find1d(line, 4, { 'a', ')', new_opts('cover_or_nearest') }, { 1, 4 })
validate_find1d(line, 5, { 'a', ')', new_opts('cover_or_nearest') }, { 1, 4 })
validate_find1d(line, 6, { 'a', ')', new_opts('cover_or_nearest') }, { 8, 11 })
validate_find1d(line, 8, { 'a', ')', new_opts('cover_or_nearest') }, { 8, 11 })
-- Should validate `opts.search_method`
expect.error(function() find_textobject('a', ')', { search_method = 'aaa' }) end, 'one of')
end
T['find_textobject()']['respects custom textobjects'] = function()
local line, column = 'aabbcc', 0
validate_find1d(line, column, { 'a', 'c' }, nil)
child.lua([[MiniAi.config.custom_textobjects = { c = { '()c()' } }]])
validate_find1d(line, column, { 'a', 'c' }, { 5, 5 })
end
T['find_textobject()']['works on multiple lines'] = function()
local lines, cursor = { '(aa', '(bb', 'cc', 'dd)', 'ee)' }, { 3, 0 }
validate_find(lines, cursor, { 'a', ')' }, { { 2, 1 }, { 4, 3 } })
validate_find(lines, cursor, { 'i', ')' }, { { 2, 2 }, { 4, 2 } })
validate_find(lines, cursor, { 'a', ')', { n_times = 2 } }, { { 1, 1 }, { 5, 3 } })
validate_find(lines, cursor, { 'i', ')', { n_times = 2 } }, { { 1, 2 }, { 5, 2 } })
-- Region over multiple lines is not empty (it has newline)
validate_find({ 'aa(', ')' }, { 1, 1 }, { 'i', ')' }, { { 1, 4 }, { 1, 4 } })
end
T['find_textobject()']['may return position after line end'] = function()
-- This powers multiline collapsing (calling `di)` leads to '()' single line)
validate_find({ '(', 'aa', ')' }, { 2, 0 }, { 'i', ')' }, { { 1, 2 }, { 2, 3 } })
end
T['find_textobject()']['works with multibyte characters'] = function()
-- Each multibyte character takes two column counts
local line = '(ыы)ффф(ыы)'
validate_find1d(line, 1, { 'a', ')' }, { 1, 6 })
validate_find1d(line, 1, { 'a', ')', { n_times = 2 } }, { 13, 18 })
validate_find1d(line, 6, { 'a', ')' }, { 13, 18 })
end
T['find_textobject()']['handles cursor on textobject edge'] = function()
validate_find1d('aa(bb)cc', 2, { 'a', ')' }, { 3, 6 })
validate_find1d('aa(bb)cc', 2, { 'i', ')' }, { 4, 5 })
validate_find1d('aa(bb)cc', 5, { 'a', ')' }, { 3, 6 })
validate_find1d('aa(bb)cc', 5, { 'i', ')' }, { 4, 5 })
end
T['find_textobject()']['first searches within current line'] = function()
local lines, cursor = { '(', 'aa(bb)', ')' }, { 2, 0 }
validate_find(lines, cursor, { 'a', ')' }, { { 2, 3 }, { 2, 6 } })
validate_find(lines, cursor, { 'a', ')', { search_method = 'cover' } }, { { 1, 1 }, { 3, 1 } })
end
T['find_textobject()']['handles `n_times > 1` with matches on current line'] = function()
local lines, cursor = { '((', 'aa(bb)cc(dd)', '))' }, { 2, 0 }
validate_find(lines, cursor, { 'a', ')', { n_times = 1 } }, { { 2, 3 }, { 2, 6 } })
validate_find(lines, cursor, { 'a', ')', { n_times = 2 } }, { { 2, 9 }, { 2, 12 } })
validate_find(lines, cursor, { 'a', ')', { n_times = 3 } }, { { 1, 2 }, { 3, 1 } })
validate_find(lines, cursor, { 'a', ')', { n_times = 4 } }, { { 1, 1 }, { 3, 2 } })
end
T['find_textobject()']['allows empty output region'] = function()
set_lines({ 'aa()bb(cc)' })
for i = 1, 2 do
set_cursor(1, i)
eq(find_textobject('i', ')'), { from = { line = 1, col = 4 } })
eq(find_textobject('i', ')', { n_times = 2 }), { from = { line = 1, col = 8 }, to = { line = 1, col = 9 } })
end
end
T['find_textobject()']['ensures that output is not covered by reference'] = function()
set_lines({ 'aa()bb(cc)dd(ee)' })
set_cursor(1, 0)
-- Non-empty reference
eq(
find_textobject('i', ')', { reference_region = { from = { line = 1, col = 8 }, to = { line = 1, col = 9 } } }),
{ from = { line = 1, col = 14 }, to = { line = 1, col = 15 } }
)
-- Empty region can cover nothing
eq(
find_textobject('i', ')', { reference_region = { from = { line = 1, col = 4 } } }),
{ from = { line = 1, col = 4 } }
)
eq(
find_textobject('i', ')', { reference_region = { from = { line = 1, col = 4 } }, n_times = 2 }),
{ from = { line = 1, col = 8 }, to = { line = 1, col = 9 } }
)
end
T['find_textobject()']['handles function as textobject spec'] = new_set()
T['find_textobject()']['handles function as textobject spec']['returns composed pattern'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
x = function(...) _G.args = {...}; return {'x()x()x'} end
}]])
validate_find1d('aaxxxbb', 0, { 'a', 'x' }, { 3, 5 })
-- Should be called with arguments after expanding defaults
--stylua: ignore
eq(
child.lua_get('_G.args'),
{
'a', 'x',
{
n_lines = 50,
n_times = 1,
reference_region = { from = { line = 1, col = 1 } },
search_method = 'cover_or_next',
},
}
)
end
T['find_textobject()']['handles function as textobject spec']['returns region'] = function()
-- Should take arguments from corresponding `find_textobject()` call
child.lua([[_G.full_buffer = function(ai_type, id, opts)
local from = { line = 1, col = 1 }
local to = { line = vim.fn.line('$'), col = vim.fn.getline('$'):len() }
if ai_type == 'i' then to.col = to.col - 1 end
return { from = from, to = to }
end]])
child.lua([[MiniAi.config.custom_textobjects = { g = _G.full_buffer }]])
validate_find({ 'aaaaa', 'bbbb', 'ccc' }, { 2, 0 }, { 'a', 'g' }, { { 1, 1 }, { 3, 3 } })
validate_find({ 'aaaaa', 'bbbb', 'ccc' }, { 2, 0 }, { 'i', 'g' }, { { 1, 1 }, { 3, 2 } })
-- Should return region as is (taking precedence over options)
child.lua('MiniAi.config.n_lines = 0')
child.lua([[MiniAi.config.search_method = 'next']])
validate_find({ 'aaaaa', 'bbbb', 'ccc' }, { 2, 0 }, { 'a', 'g' }, { { 1, 1 }, { 3, 3 } })
end
T['find_textobject()']['handles function as textobject spec']['returns array of regions'] = function()
-- Should take arguments from corresponding `find_textobject()` call
child.lua([[_G.fixed_regions = function(_, _, _)
local res = {}
res[1] = { from = { line = 1, col = 1 }, to = { line = 1, col = 2 } }
res[2] = { from = { line = 1, col = 1 }, to = { line = 1, col = 4 } }
res[3] = { from = { line = 2, col = 1 }, to = { line = 2, col = 2 } }
res[4] = { from = { line = 2, col = 1 }, to = { line = 2, col = 4 } }
return res
end]])
child.lua([[MiniAi.config.custom_textobjects = { r = _G.fixed_regions }]])
local lines = { 'aaaaa', 'bbbb', 'ccc' }
validate_find(lines, { 1, 0 }, { 'a', 'r' }, { { 1, 1 }, { 1, 2 } })
validate_find(lines, { 1, 3 }, { 'a', 'r' }, { { 1, 1 }, { 1, 4 } })
validate_find(lines, { 1, 4 }, { 'a', 'r' }, { { 2, 1 }, { 2, 2 } })
validate_find(lines, { 3, 0 }, { 'a', 'r' }, nil)
-- Should pick best region according to options
child.lua([[MiniAi.config.n_lines = 0]])
validate_find(lines, { 1, 4 }, { 'a', 'r' }, nil)
child.lua([[MiniAi.config.n_lines = 10]])
child.lua([[MiniAi.config.search_method = 'cover']])
validate_find(lines, { 1, 4 }, { 'a', 'r' }, nil)
-- Should account for region `vis_mode`
child.lua([[_G.line_regions = function(_, _, _)
return {
{ from = { line = 1, col = 1 }, to = { line = 2, col = 1 }, vis_mode = 'V' },
{ from = { line = 3, col = 1 }, to = { line = 4, col = 1 }, vis_mode = 'V' },
}
end]])
child.lua([[MiniAi.config.custom_textobjects = { m = _G.line_regions }]])
local ref_region_1 = { { 1, 1 }, { 2, 1 }, vis_mode = 'V' }
validate_find({ 'aaa', 'bbb', 'ccc', 'ddd' }, { 1, 0 }, { 'a', 'm' }, ref_region_1)
local ref_region_2 = { { 3, 1 }, { 4, 1 }, vis_mode = 'V' }
validate_find({ 'aaa', 'bbb', 'ccc', 'ddd' }, { 3, 0 }, { 'a', 'm' }, ref_region_2)
end
T['find_textobject()']['handles function as specification item'] = function()
child.lua([[_G.c_spec = {
'%b()',
function(s, init) if init > 1 then return end; return 2, s:len() end,
'^().*().$'
}]])
child.lua([[MiniAi.config.custom_textobjects = { c = _G.c_spec }]])
validate_find1d('aa(bb)', 0, { 'a', 'c' }, { 4, 6 })
validate_find1d('aa(bb)', 0, { 'i', 'c' }, { 4, 5 })
end
T['find_textobject()']['handles callable table'] = new_set()
T['find_textobject()']['handles callable table']['as textobject spec'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
x = setmetatable({}, {__call = function() return {'x()x()x'} end})
}]])
validate_find1d('aaxxxbb', 0, { 'a', 'x' }, { 3, 5 })
end
T['find_textobject()']['handles callable table']['as specification item'] = function()
child.lua([[_G.c_spec = {
'%b()',
setmetatable({}, {__call = function(_, s, init) if init > 1 then return end; return 2, s:len() end}),
'^().*().$'
}]])
child.lua([[MiniAi.config.custom_textobjects = { c = _G.c_spec }]])
validate_find1d('aa(bb)', 0, { 'a', 'c' }, { 4, 6 })
validate_find1d('aa(bb)', 0, { 'i', 'c' }, { 4, 5 })
end
T['find_textobject()']['shows message if no region is found'] = function()
-- Make all showed messages full width
child.o.cmdheight = 10
local validate = function(msg, args)
child.cmd('messages clear')
validate_find1d('aa', 0, args, nil)
eq(get_latest_message(), msg)
end
validate(
[[(mini.ai) No textobject "a)" found covering region within 50 lines and `search_method = 'cover_or_next'`.]],
{ 'a', ')' }
)
validate(
[[(mini.ai) No textobject "i]" found covering region 2 times within 1 line and `search_method = 'cover'`.]],
{ 'i', ']', { n_times = 2, n_lines = 1, search_method = 'cover' } }
)
validate(
[[(mini.ai) No textobject "i]" found covering region 0 times within 0 lines and `search_method = 'cover_or_next'`.]],
{ 'i', ']', { n_times = 0, n_lines = 0 } }
)
end
T['find_textobject()']['respects `config.silent`'] = function()
child.lua('MiniAi.config.silent = true')
validate_find1d('aa', 0, { 'a', ')' }, nil)
eq(get_latest_message(), '')
end
T['find_textobject()']['respects `vim.b.miniai_config`'] = function()
child.b.miniai_config = { search_method = 'cover' }
validate_find1d('aa(bb)', 0, { 'a', ')' }, nil)
end
local validate_move = function(lines, cursor, args, expected)
set_lines(lines)
set_cursor(cursor[1], cursor[2])
child.lua([[MiniAi.move_cursor(...)]], args)
eq(get_cursor(), { expected[1], expected[2] })
end
local validate_move1d = function(line, column, args, expected)
validate_move({ line }, { 1, column }, args, { 1, expected })
end
T['move_cursor()'] = new_set()
T['move_cursor()']['works'] = function() validate_move1d('aa(bbb)', 4, { 'left', 'a', ')' }, 2) end
T['move_cursor()']['respects `side` argument'] = function()
local line = '(aa)bb(cc)'
validate_move1d(line, 1, { 'left', 'a', ')' }, 0)
validate_move1d(line, 1, { 'right', 'a', ')' }, 3)
validate_move1d(line, 4, { 'left', 'a', ')' }, 6)
validate_move1d(line, 4, { 'right', 'a', ')' }, 9)
validate_move1d(line, 7, { 'left', 'a', ')' }, 6)
validate_move1d(line, 7, { 'right', 'a', ')' }, 9)
-- It should validate `side` argument
expect.error(
function() child.lua([[MiniAi.move_cursor('leftright', 'a', ')')]]) end,
vim.pesc([[(mini.ai) `side` should be one of 'left' or 'right'.]])
)
end
T['move_cursor()']['respects `ai_type` argument'] = function()
validate_move1d('aa(bbb)', 4, { 'left', 'i', ')' }, 3)
validate_move1d('aa(bbb)', 4, { 'right', 'i', ')' }, 5)
end
T['move_cursor()']['respects `id` argument'] = function() validate_move1d('aa[bbb]', 4, { 'left', 'a', ']' }, 2) end
T['move_cursor()']['respects `opts` argument'] = function()
validate_move1d('aa(bbb)cc(ddd)', 4, { 'left', 'a', ')', { n_times = 2 } }, 9)
end
T['move_cursor()']['always jumps exactly `opts.n_times` times'] = function()
-- It can be not that way if cursor is on edge of one of target textobjects
local line = 'aa(bb)cc(dd)ee(ff)'
validate_move1d(line, 0, { 'left', 'a', ')', { n_times = 2 } }, 8) -- 0->2->8
validate_move1d(line, 2, { 'left', 'a', ')', { n_times = 2 } }, 14) -- 2->8->14
validate_move1d(line, 3, { 'left', 'a', ')', { n_times = 2 } }, 8) -- 3->2->8
validate_move1d(line, 5, { 'left', 'a', ')', { n_times = 2 } }, 8) -- 5->2->8
validate_move1d(line, 0, { 'right', 'a', ')', { n_times = 2 } }, 11) -- 0->5->11
validate_move1d(line, 2, { 'right', 'a', ')', { n_times = 2 } }, 11) -- 2->5->11
validate_move1d(line, 3, { 'right', 'a', ')', { n_times = 2 } }, 11) -- 3->5->11
validate_move1d(line, 5, { 'right', 'a', ')', { n_times = 2 } }, 17) -- 5->11->17
end
T['move_cursor()']['opens just enough folds'] = function()
set_lines({ '(aa', 'b)', 'c', 'd' })
-- Manually create two nested closed folds
set_cursor(3, 0)
type_keys('zf', 'G')
type_keys('zf', 'gg')
eq(child.fn.foldlevel(1), 1)
eq(child.fn.foldlevel(3), 2)
eq(child.fn.foldclosed(2), 1)
eq(child.fn.foldclosed(3), 1)
-- Moving cursor should open just enough folds
set_cursor(1, 1)
child.lua([[MiniAi.move_cursor('right', 'a', ')')]])
eq(get_cursor(), { 2, 1 })
eq(child.fn.foldclosed(2), -1)
eq(child.fn.foldclosed(3), 3)
end
T['move_cursor()']['adds jumplist entry'] = function()
set_lines({ '(aa', 'b)' })
local validate = function(init_pos, direction, ref_pos)
set_cursor(unpack(init_pos))
child.lua('MiniAi.move_cursor("' .. direction .. '", "a", ")")')
eq(get_cursor(), ref_pos)
type_keys('``')
eq(get_cursor(), init_pos)
end
validate({ 1, 1 }, 'right', { 2, 1 })
validate({ 2, 1 }, 'left', { 1, 0 })
end
T['move_cursor()']['handles function as textobject spec'] = function()
-- Should call it only once
child.lua('_G.n = 0')
child.lua([[MiniAi.config.custom_textobjects = { c = function() _G.n = _G.n + 1; return { '()c()' } end }]])
validate_move1d('aabbcc', 0, { 'left', 'a', 'c' }, 4)
eq(child.lua_get('_G.n'), 1)
end
T['move_cursor()']['works with empty region'] = function()
validate_move1d('f()', 0, { 'left', 'i', ')' }, 2)
validate_move1d('f()', 0, { 'right', 'i', ')' }, 2)
end
T['move_cursor()']['works with multibyte characters'] = function()
validate_move1d(' (ыыы) ', 0, { 'left', 'a', ')' }, 1)
validate_move1d(' (ыыы) ', 0, { 'right', 'a', ')' }, 8)
end
T['gen_spec'] = new_set()
T['gen_spec']['argument()'] = new_set()
T['gen_spec']['argument()']['works'] = function()
child.lua([[MiniAi.config.custom_textobjects = { A = MiniAi.gen_spec.argument() }]])
validate_find1d('f(aa, bb)', 0, { 'a', 'A' }, { 3, 5 })
validate_find1d('f(aa, bb)', 0, { 'i', 'A' }, { 3, 4 })
end
T['gen_spec']['argument()']['respects `opts.brackets`'] = function()
child.lua([[MiniAi.config.custom_textobjects = { A = MiniAi.gen_spec.argument({ brackets = { '%b()' } }) }]])
validate_find1d('(aa, bb)', 0, { 'a', 'A' }, { 2, 4 })
validate_find1d('[aa, bb]', 0, { 'a', 'A' }, nil)
validate_find1d('{aa, bb}', 0, { 'a', 'A' }, nil)
end
T['gen_spec']['argument()']['respects `opts.separator`'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
A = MiniAi.gen_spec.argument({ separator = ';' }),
B = MiniAi.gen_spec.argument({ separator = '[,;]' }),
C = MiniAi.gen_spec.argument({ separator = '%s*,%s*' }),
D = MiniAi.gen_spec.argument({ separator = ', ,' }),
}]])
validate_find1d('(aa, bb; cc, dd)', 0, { 'a', 'A', { n_times = 1 } }, { 2, 8 })
validate_find1d('(aa, bb; cc, dd)', 0, { 'a', 'A', { n_times = 2 } }, { 8, 15 })
validate_find1d('(aa, bb; cc, dd)', 0, { 'a', 'B', { n_times = 1 } }, { 2, 4 })
validate_find1d('(aa, bb; cc, dd)', 0, { 'a', 'B', { n_times = 2 } }, { 4, 7 })
validate_find1d('(aa, bb; cc, dd)', 0, { 'a', 'B', { n_times = 3 } }, { 8, 11 })
validate_find1d('(aa, bb; cc, dd)', 0, { 'a', 'B', { n_times = 4 } }, { 12, 15 })
validate_find1d('(aa , bb , cc)', 0, { 'a', 'C', { n_times = 1 } }, { 2, 6 })
validate_find1d('(aa , bb , cc)', 0, { 'a', 'C', { n_times = 2 } }, { 4, 8 })
validate_find1d('(aa , bb , cc)', 0, { 'a', 'C', { n_times = 3 } }, { 9, 13 })
validate_find1d('(aa , bb , cc)', 0, { 'i', 'C', { n_times = 1 } }, { 2, 3 })
validate_find1d('(aa , bb , cc)', 0, { 'i', 'C', { n_times = 2 } }, { 7, 8 })
validate_find1d('(aa , bb , cc)', 0, { 'i', 'C', { n_times = 3 } }, { 12, 13 })
validate_find1d('(aa, bb , , cc, dd)', 0, { 'a', 'D', { n_times = 1 } }, { 2, 11 })
validate_find1d('(aa, bb , , cc, dd)', 0, { 'a', 'D', { n_times = 2 } }, { 9, 18 })
validate_find1d('(aa, bb , , cc, dd)', 0, { 'i', 'D', { n_times = 1 } }, { 2, 7 })
validate_find1d('(aa, bb , , cc, dd)', 0, { 'i', 'D', { n_times = 2 } }, { 13, 18 })
end
T['gen_spec']['argument()']['respects `opts.exclude_regions`'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
A = MiniAi.gen_spec.argument({ exclude_regions = { '".-"' } }),
}]])
-- Comma inside `()` should not be excluded
validate_find1d('(aa, (bb, cc))', 4, { 'a', 'A' }, { 4, 8 })
end
T['gen_spec']['function_call()'] = new_set()
T['gen_spec']['function_call()']['works'] = function()
child.lua([[MiniAi.config.custom_textobjects = { F = MiniAi.gen_spec.function_call() }]])
validate_find1d('f.g_h(x)', 0, { 'a', 'F' }, { 1, 8 })
validate_find1d('f.g_h(x)', 0, { 'i', 'F' }, { 7, 7 })
end
T['gen_spec']['function_call()']['respects `opts.name_pattern`'] = function()
child.lua([[MiniAi.config.custom_textobjects = { F = MiniAi.gen_spec.function_call({ name_pattern = '[%w_]' }) }]])
validate_find1d('f.g_h(x)', 0, { 'a', 'F' }, { 3, 8 })
validate_find1d('f.g_h(x)', 0, { 'i', 'F' }, { 7, 7 })
end
T['gen_spec']['pair()'] = new_set()
T['gen_spec']['pair()']['works'] = function()
child.lua([[MiniAi.config.custom_textobjects = { x = MiniAi.gen_spec.pair('x', 'y') }]])
validate_find1d('aaxbbycc', 3, { 'a', 'x' }, { 3, 6 })
validate_find1d('aaxbbycc', 3, { 'i', 'x' }, { 4, 5 })
end
--stylua: ignore
T['gen_spec']['pair()']['validates arguments'] = function()
expect.error(function() child.lua([[MiniAi.gen_spec.pair(1, 'y')]]) end, '`left`.*string')
expect.error(function() child.lua([[MiniAi.gen_spec.pair('x', 1)]]) end, '`right`.*string')
-- For balanced and greedy types arguments should be single character
expect.error(function() child.lua([[MiniAi.gen_spec.pair('xx', 'y', { type = 'balanced' })]]) end, '`left`.*single.*balanced')
expect.error(function() child.lua([[MiniAi.gen_spec.pair('x', 'yy', { type = 'balanced' })]]) end, '`left`.*single.*balanced')
expect.error(function() child.lua([[MiniAi.gen_spec.pair('xx', 'y', { type = 'greedy' })]]) end, '`left`.*single.*greedy')
expect.error(function() child.lua([[MiniAi.gen_spec.pair('x', 'yy', { type = 'greedy' })]]) end, '`left`.*single.*greedy')
-- `opts.type` should be one of pre-defined characters
expect.error(function() child.lua([[MiniAi.gen_spec.pair('x', 'y', { type = 'aaa' })]]) end, 'opts.type.*one of')
end
T['gen_spec']['pair()']['respects `opts.type`'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
_ = MiniAi.gen_spec.pair('_', '_', { type = 'non-balanced' }),
x = MiniAi.gen_spec.pair('_', '_', { type = 'balanced' }),
b = MiniAi.gen_spec.pair('(', ')', { type = 'balanced' }),
y = MiniAi.gen_spec.pair('_', '_', { type = 'greedy' }),
}]])
-- Non-balanced pair
validate_find1d('_a_b_c_', 0, { 'a', '_', { n_times = 1 } }, { 1, 3 })
validate_find1d('_a_b_c_', 0, { 'a', '_', { n_times = 2 } }, { 3, 5 })
validate_find1d('_a_b_c_', 0, { 'a', '_', { n_times = 3 } }, { 5, 7 })
validate_find1d('_a_b_c_', 0, { 'i', '_' }, { 2, 2 })
-- Balanced pair
validate_find1d('_a_b_c_', 0, { 'a', 'x', { n_times = 1 } }, { 1, 3 })
validate_find1d('_a_b_c_', 0, { 'a', 'x', { n_times = 2 } }, { 5, 7 })
validate_find1d('_a_b_c_', 0, { 'i', 'x' }, { 2, 2 })
-- Should also work with "special" characters
validate_find1d('((aa))', 2, { 'a', 'b', { n_times = 1 } }, { 2, 5 })
validate_find1d('((aa))', 2, { 'a', 'b', { n_times = 2 } }, { 1, 6 })
-- Greedy pair
validate_find1d('__a__b_', 0, { 'a', 'y', { n_times = 1 } }, { 1, 5 })
validate_find1d('__a__b_', 0, { 'a', 'y', { n_times = 2 } }, { 4, 7 })
validate_find1d('__a__b_', 0, { 'i', 'y' }, { 3, 3 })
end
T['gen_spec']['treesitter()'] = new_set({
hooks = {
pre_case = function()
-- Mock tree-sitter queries
child.cmd('noautocmd set rtp+=tests/mock-treesitter')
-- Start editing reference file
child.cmd('edit tests/mock-treesitter/lua-file.lua')
child.lua('vim.treesitter.start()')
-- Define "function definition" textobject
child.lua([[MiniAi.config.custom_textobjects = {
F = MiniAi.gen_spec.treesitter({ a = '@function.outer', i = '@function.inner' })
}]])
end,
},
})
T['gen_spec']['treesitter()']['works'] = function()
local lines = get_lines()
validate_find(lines, { 1, 0 }, { 'a', 'F' }, { { 3, 1 }, { 5, 3 } })
validate_find(lines, { 1, 0 }, { 'i', 'F' }, { { 4, 3 }, { 4, 38 } })
validate_find(lines, { 13, 0 }, { 'a', 'F' }, nil)
-- Should prefer match on current line over multiline covering
validate_find(lines, { 4, 0 }, { 'a', 'F' }, { { 4, 10 }, { 4, 38 } })
-- Should prefer range from metadata instead of node itself. This is useful,
-- for example, with `#offset!` directive to create more precise captures.
validate_find(lines, { 9, 0 }, { 'i', 'F' }, { { 8, 3 }, { 10, 13 } })
end
T['gen_spec']['treesitter()']['allows array of captures'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
o = MiniAi.gen_spec.treesitter({ a = { '@function.outer', '@return.outer' }, i = { '@function.inner', '@return.inner' } })
}]])
local lines = get_lines()
validate_find(lines, { 10, 2 }, { 'a', 'o' }, { { 10, 3 }, { 10, 13 } })
validate_find(lines, { 10, 2 }, { 'i', 'o' }, { { 10, 10 }, { 10, 13 } })
validate_find(lines, { 10, 2 }, { 'a', 'o', { n_times = 2 } }, { { 7, 7 }, { 11, 3 } })
validate_find(lines, { 10, 2 }, { 'i', 'o', { n_times = 2 } }, { { 8, 3 }, { 10, 13 } })
validate_find(lines, { 13, 0 }, { 'a', 'o' }, { { 13, 1 }, { 13, 8 } })
end
T['gen_spec']['treesitter()']['respects `opts.use_nvim_treesitter`'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
F = MiniAi.gen_spec.treesitter({ a = '@function.outer', i = '@function.inner' }),
o = MiniAi.gen_spec.treesitter({ a = '@plugin_return', i = '@plugin_return' }),
O = MiniAi.gen_spec.treesitter(
{ a = '@plugin_return', i = '@plugin_return' },
{ use_nvim_treesitter = true }
)
}]])
local lines = get_lines()
-- By default it should be `false`
validate_find(lines, { 1, 0 }, { 'a', 'F' }, { { 3, 1 }, { 5, 3 } })
validate_find(lines, { 1, 0 }, { 'a', 'o' }, nil)
validate_find(lines, { 1, 0 }, { 'a', 'O' }, nil)
child.cmd('noautocmd set rtp+=tests/dir-ai/mock-nvim-treesitter')
validate_find(lines, { 1, 0 }, { 'a', 'F' }, { { 3, 1 }, { 5, 3 } })
validate_find(lines, { 1, 0 }, { 'a', 'o' }, nil)
validate_find(lines, { 1, 0 }, { 'a', 'O' }, { { 4, 3 }, { 4, 38 } })
-- Should prefer range from metadata instead of node itself. This is useful,
-- for example, with `#offset!` directive to create more precise captures.
validate_find(lines, { 9, 0 }, { 'i', 'F' }, { { 8, 3 }, { 10, 13 } })
end
T['gen_spec']['treesitter()']['works with directives'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
S = MiniAi.gen_spec.treesitter({ a = '@string', i = '@string_offset' }),
}]])
local lines = get_lines()
validate_find(lines, { 9, 9 }, { 'i', 'S' }, { { 9, 10 }, { 9, 16 } })
end
T['gen_spec']['treesitter()']['works with quantified captures'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
P = MiniAi.gen_spec.treesitter({ a = '@parameter.outer', i = '@parameter.inner' }),
}]])
-- "Around" parameter should include whitespace and comma
local lines = get_lines()
validate_find(lines, { 3, 0 }, { 'i', 'P' }, { { 3, 14 }, { 3, 14 } })
validate_find(lines, { 3, 0 }, { 'a', 'P' }, { { 3, 14 }, { 3, 15 } })
validate_find(lines, { 3, 0 }, { 'i', 'P', { n_times = 2 } }, { { 3, 17 }, { 3, 18 } })
validate_find(lines, { 3, 0 }, { 'a', 'P', { n_times = 2 } }, { { 3, 15 }, { 3, 18 } })
validate_find(lines, { 3, 0 }, { 'i', 'P', { n_times = 3 } }, { { 3, 21 }, { 3, 23 } })
validate_find(lines, { 3, 0 }, { 'a', 'P', { n_times = 3 } }, { { 3, 19 }, { 3, 23 } })
end
T['gen_spec']['treesitter()']['works with parent of injected language'] = function()
local lines = {
'local foo = function()',
' vim.cmd([[',
'set cursorline',
']])',
'end',
}
validate_find(lines, { 3, 0 }, { 'a', 'F' }, { { 1, 13 }, { 5, 3 } })
end
T['gen_spec']['treesitter()']['works with injected child language'] = function()
local lines = {
'vim.cmd([[',
'set cursorline',
'lua local a = function() return true end',
']])',
}
validate_find(lines, { 1, 0 }, { 'a', 'F' }, { { 3, 15 }, { 3, 40 } })
end
T['gen_spec']['treesitter()']['works with row-exclusive, col-0 end range'] = function()
child.lua([[MiniAi.config.custom_textobjects = {
c = MiniAi.gen_spec.treesitter({ a = '@chunk.outer', i = '@chunk.outer' }),
}]])
local lines = get_lines()
validate_find(lines, { 1, 0 }, { 'a', 'c' }, { { 1, 1 }, { 13, 9 } })
end
T['gen_spec']['treesitter()']['respects plugin options'] = function()
local lines = get_lines()
-- `opts.n_lines`
child.lua('MiniAi.config.n_lines = 0')
validate_find(lines, { 9, 0 }, { 'a', 'F' }, nil)
-- `opts.search_method`
child.lua('MiniAi.config.n_lines = 50')
child.lua('MiniAi.config.search_method = "next"')
validate_find(lines, { 9, 0 }, { 'a', 'F' }, nil)
end
T['gen_spec']['treesitter()']['validates `ai_captures` argument'] = function()
local validate = function(args)
expect.error(function() child.lua('MiniAi.gen_spec.treesitter(...)', { args }) end, 'ai_captures')
end
validate('a')
validate({})
-- Each `a` and `i` should be a string starting with '@'
validate({ a = 1 })
validate({ a = 'function.outer' })
validate({ a = { 1 } })
validate({ a = { 'function.outer' } })
validate({ i = 1 })
validate({ i = 'function.inner' })
validate({ i = { 1 } })
validate({ i = { 'function.inner' } })
end
T['gen_spec']['treesitter()']['validates builtin treesitter presence'] = function()
-- Query
child.bo.filetype = 'vim'
expect.error(
function() child.lua('MiniAi.find_textobject("a", "F")') end,
'%(mini%.ai%) Can not get query for buffer 1 and language "vim"%.'
)
-- - Should show local language in error message
child.cmd('edit tmp.lua')
set_lines({ 'vim.cmd([[', 'setlocal cursorline', ']])' })
set_cursor(2, 0)
expect.error(
function() child.lua('MiniAi.find_textobject("a", "F")') end,
'%(mini%.ai%) Can not get query for buffer 2 and language "vim"%.'
)
-- Parser
child.cmd('enew')
child.bo.filetype = 'aaa'
expect.error(
function() child.lua('MiniAi.find_textobject("a", "F")') end,
'%(mini%.ai%) Can not get parser for buffer 3 and language "aaa"%.'
)
-- - Should respect registered language for a filetype
child.lua('vim.treesitter.language.register("my_aaa", "aaa")')
expect.error(
function() child.lua('MiniAi.find_textobject("a", "F")') end,
'%(mini%.ai%) Can not get parser for buffer 3 and language "my_aaa"%.'
)
-- - Should show each language
child.cmd('enew')
child.bo.filetype = 'help'
set_lines({ '>vim', ' set cursorline', '<' })
set_cursor(2, 0)
expect.error(
function() child.lua('MiniAi.find_textobject("a", "F")') end,
'%(mini%.ai%) Can not get query for buffer 3 and languages "vim", "vimdoc"%.'
)
-- - Should show each language once
child.cmd('edit tmp.vim')
set_lines({ 'set indentexpr=VimIndent()' })
set_cursor(1, 0)
expect.error(
function() child.lua('MiniAi.find_textobject("a", "F")') end,
'%(mini%.ai%) Can not get query for buffer 4 and language "vim"%.'
)
end
T['gen_spec']['user_prompt()'] = new_set()
-- More tests are done in `T['Builtin']['User prompt']`
T['gen_spec']['user_prompt()']['works'] = function()
-- Define custom "user prompt" textobject
child.lua('MiniAi.config.custom_textobjects = { ["/"] = MiniAi.gen_spec.user_prompt() }')
-- Single character edges
validate_tobj1d('__e__o__', 0, 'a/e<CR>o<CR>', { 3, 6 })
validate_tobj1d('__e__o__', 0, 'i/e<CR>o<CR>', { 4, 5 })
-- Multiple character edges
validate_tobj1d('__ef__op__', 0, 'a/ef<CR>op<CR>', { 3, 8 })
validate_tobj1d('__ef__op__', 0, 'i/ef<CR>op<CR>', { 5, 6 })
-- Supports dot-repeat
set_lines({ '_e--o_', '+e---o+' })
set_cursor(1, 0)
type_keys('da', '/', 'e<CR>', 'o<CR>')
eq(get_lines(), { '__', '+e---o+' })
type_keys('j', '.')
eq(get_lines(), { '__', '++' })
set_lines({ '_r--t_' })
set_cursor(1, 0)
type_keys('di', '/', 'r<CR>', 't<CR>')
eq(get_lines(), { '_rt_' })
end
local validate_select = function(lines, cursor, args, expected)
child.ensure_normal_mode()
set_lines(lines)
set_cursor(unpack(cursor))
child.lua([[MiniAi.select_textobject(...)]], args)
local expected_mode = (args[3] or {}).vis_mode or 'v'
eq(get_mode(), vim.api.nvim_replace_termcodes(expected_mode, true, true, true))
-- Allow supplying number items to verify linewise selection
local expected_from = type(expected[1]) == 'number' and expected[1] or { expected[1][1], expected[1][2] - 1 }
local expected_to = type(expected[2]) == 'number' and expected[2] or { expected[2][1], expected[2][2] - 1 }
child.expect_visual_marks(expected_from, expected_to)
end
local validate_select1d = function(line, column, args, expected)
validate_select({ line }, { 1, column }, args, { { 1, expected[1] }, { 1, expected[2] } })
end