blob: b605d76f2fe0a54c206442081b6c344f8cf594c1 [file] [log] [blame]
Bram Moolenaar00b24be2016-07-23 22:04:47 +02001" Test for textobjects
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textobjects
Bram Moolenaar00b24be2016-07-23 22:04:47 +02005
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02006func CpoM(line, useM, expected)
Bram Moolenaar00b24be2016-07-23 22:04:47 +02007 new
8
9 if a:useM
10 set cpoptions+=M
11 else
12 set cpoptions-=M
13 endif
14
15 call setline(1, a:line)
16
17 call setreg('"', '')
18 normal! ggfrmavi)y
19 call assert_equal(getreg('"'), a:expected[0])
20
21 call setreg('"', '')
22 normal! `afbmavi)y
23 call assert_equal(getreg('"'), a:expected[1])
24
25 call setreg('"', '')
26 normal! `afgmavi)y
27 call assert_equal(getreg('"'), a:expected[2])
28
29 q!
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020030endfunc
Bram Moolenaar00b24be2016-07-23 22:04:47 +020031
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020032func Test_inner_block_without_cpo_M()
Bram Moolenaar00b24be2016-07-23 22:04:47 +020033 call CpoM('(red \(blue) green)', 0, ['red \(blue', 'red \(blue', ''])
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020034endfunc
Bram Moolenaar00b24be2016-07-23 22:04:47 +020035
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020036func Test_inner_block_with_cpo_M_left_backslash()
Bram Moolenaar00b24be2016-07-23 22:04:47 +020037 call CpoM('(red \(blue) green)', 1, ['red \(blue) green', 'blue', 'red \(blue) green'])
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020038endfunc
Bram Moolenaar00b24be2016-07-23 22:04:47 +020039
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020040func Test_inner_block_with_cpo_M_right_backslash()
Bram Moolenaar00b24be2016-07-23 22:04:47 +020041 call CpoM('(red (blue\) green)', 1, ['red (blue\) green', 'blue\', 'red (blue\) green'])
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020042endfunc
43
LemonBoy53737b52022-05-24 11:49:31 +010044func Test_inner_block_single_char()
45 new
46 call setline(1, "(a)")
47
48 set selection=inclusive
49 let @" = ''
50 call assert_nobeep('norm! 0faviby')
51 call assert_equal('a', @")
52
53 set selection=exclusive
54 let @" = ''
55 call assert_nobeep('norm! 0faviby')
56 call assert_equal('a', @")
57
58 set selection&
59 bwipe!
60endfunc
61
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020062func Test_quote_selection_selection_exclusive()
63 new
64 call setline(1, "a 'bcde' f")
65 set selection=exclusive
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010066
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020067 exe "norm! fdvhi'y"
68 call assert_equal('bcde', @")
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010069
LemonBoy53737b52022-05-24 11:49:31 +010070 let @" = 'dummy'
Bram Moolenaar7170b292019-11-17 17:32:28 +010071 exe "norm! $gevi'y"
72 call assert_equal('bcde', @")
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010073
LemonBoy53737b52022-05-24 11:49:31 +010074 let @" = 'dummy'
Bram Moolenaar94d9f4f2019-11-21 20:55:26 +010075 exe "norm! 0fbhvi'y"
76 call assert_equal('bcde', @")
77
Bram Moolenaarc5e2b042017-06-05 16:37:07 +020078 set selection&vim
79 bw!
80endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +020081
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +010082func Test_quote_selection_selection_exclusive_abort()
83 new
84 set selection=exclusive
85 call setline(1, "'abzzc'")
86 let exp_curs = [0, 1, 6, 0]
87 call cursor(1,1)
88 exe 'norm! fcdvi"'
89 " make sure to end visual mode to have a clear state
90 exe "norm! \<esc>"
91 call assert_equal(exp_curs, getpos('.'))
92 call cursor(1,1)
93 exe 'norm! fcvi"'
94 exe "norm! \<esc>"
95 call assert_equal(exp_curs, getpos('.'))
96 call cursor(1,2)
97 exe 'norm! vfcoi"'
98 exe "norm! \<esc>"
99 let exp_curs = [0, 1, 2, 0]
100 let exp_visu = [0, 1, 7, 0]
101 call assert_equal(exp_curs, getpos('.'))
102 call assert_equal(exp_visu, getpos("'>"))
103 set selection&vim
104 bw!
105endfunc
106
Bram Moolenaardb510072017-09-28 21:52:17 +0200107" Tests for string and html text objects
108func Test_string_html_objects()
Bram Moolenaardb510072017-09-28 21:52:17 +0200109
Dominique Pelleaf631f62021-09-03 16:50:16 +0200110 for e in ['utf-8', 'latin1', 'cp932']
111 enew!
112 exe 'set enc=' .. e
Bram Moolenaardb510072017-09-28 21:52:17 +0200113
Dominique Pelleaf631f62021-09-03 16:50:16 +0200114 let t = '"wo\"rd\\" foo'
115 put =t
116 normal! da"
117 call assert_equal('foo', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200118
Dominique Pelleaf631f62021-09-03 16:50:16 +0200119 let t = "'foo' 'bar' 'piep'"
120 put =t
121 normal! 0va'a'rx
122 call assert_equal("xxxxxxxxxxxx'piep'", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200123
Dominique Pelleaf631f62021-09-03 16:50:16 +0200124 let t = "bla bla `quote` blah"
125 put =t
126 normal! 02f`da`
127 call assert_equal("bla bla blah", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200128
Dominique Pelleaf631f62021-09-03 16:50:16 +0200129 let t = 'out " in "noXno"'
130 put =t
131 normal! 0fXdi"
132 call assert_equal('out " in ""', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200133
Dominique Pelleaf631f62021-09-03 16:50:16 +0200134 let t = "\"'\" 'blah' rep 'buh'"
135 put =t
136 normal! 03f'vi'ry
137 call assert_equal("\"'\" 'blah'yyyyy'buh'", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200138
Dominique Pelleaf631f62021-09-03 16:50:16 +0200139 set quoteescape=+*-
140 let t = "bla `s*`d-`+++`l**` b`la"
141 put =t
142 normal! di`
143 call assert_equal("bla `` b`la", getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200144
Dominique Pelleaf631f62021-09-03 16:50:16 +0200145 let t = 'voo "nah" sdf " asdf" sdf " sdf" sd'
146 put =t
147 normal! $F"va"oha"i"rz
148 call assert_equal('voo "zzzzzzzzzzzzzzzzzzzzzzzzzzzzsd', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200149
Dominique Pelleaf631f62021-09-03 16:50:16 +0200150 let t = "-<b>asdf<i>Xasdf</i>asdf</b>-"
151 put =t
152 normal! fXdit
153 call assert_equal('-<b>asdf<i></i>asdf</b>-', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200154
Dominique Pelleaf631f62021-09-03 16:50:16 +0200155 let t = "-<b>asdX<i>a<i />sdf</i>asdf</b>-"
156 put =t
157 normal! 0fXdit
158 call assert_equal('-<b></b>-', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200159
Dominique Pelleaf631f62021-09-03 16:50:16 +0200160 let t = "-<b>asdf<i>Xasdf</i>asdf</b>-"
161 put =t
162 normal! fXdat
163 call assert_equal('-<b>asdfasdf</b>-', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200164
Dominique Pelleaf631f62021-09-03 16:50:16 +0200165 let t = "-<b>asdX<i>as<b />df</i>asdf</b>-"
166 put =t
167 normal! 0fXdat
168 call assert_equal('--', getline('.'), e)
Bram Moolenaardb510072017-09-28 21:52:17 +0200169
Dominique Pelleaf631f62021-09-03 16:50:16 +0200170 let t = "-<b>\ninnertext object\n</b>"
171 put =t
172 normal! dit
173 call assert_equal('-<b></b>', getline('.'), e)
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200174
Dominique Pelleaf631f62021-09-03 16:50:16 +0200175 " copy the tag block from leading indentation before the start tag
176 let t = " <b>\ntext\n</b>"
177 $put =t
178 normal! 2kvaty
179 call assert_equal("<b>\ntext\n</b>", @", e)
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200180
Dominique Pelleaf631f62021-09-03 16:50:16 +0200181 " copy the tag block from the end tag
182 let t = "<title>\nwelcome\n</title>"
183 $put =t
184 normal! $vaty
185 call assert_equal("<title>\nwelcome\n</title>", @", e)
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200186
Dominique Pelleaf631f62021-09-03 16:50:16 +0200187 " copy the outer tag block from a tag without an end tag
188 let t = "<html>\n<title>welcome\n</html>"
189 $put =t
190 normal! k$vaty
191 call assert_equal("<html>\n<title>welcome\n</html>", @", e)
Bram Moolenaara604ccc2020-10-15 21:23:28 +0200192
Dominique Pelleaf631f62021-09-03 16:50:16 +0200193 " nested tag that has < in a different line from >
194 let t = "<div><div\n></div></div>"
195 $put =t
196 normal! k0vaty
197 call assert_equal("<div><div\n></div></div>", @", e)
Bram Moolenaara604ccc2020-10-15 21:23:28 +0200198
Dominique Pelleaf631f62021-09-03 16:50:16 +0200199 " nested tag with attribute that has < in a different line from >
200 let t = "<div><div\nattr=\"attr\"\n></div></div>"
201 $put =t
202 normal! 2k0vaty
203 call assert_equal("<div><div\nattr=\"attr\"\n></div></div>", @", e)
204
205 set quoteescape&
Bram Moolenaar53a70282022-05-09 13:15:07 +0100206
207 " this was going beyond the end of the line
208 %del
209 sil! norm i"\
210 sil! norm i"\
211 sil! norm i"\
212 call assert_equal('"\', getline(1))
213
214 bwipe!
Dominique Pelleaf631f62021-09-03 16:50:16 +0200215 endfor
216
217 set enc=utf-8
Bram Moolenaardb510072017-09-28 21:52:17 +0200218endfunc
219
Bram Moolenaarb476cb72018-08-16 21:37:50 +0200220func Test_empty_html_tag()
221 new
222 call setline(1, '<div></div>')
223 normal 0citxxx
224 call assert_equal('<div>xxx</div>', getline(1))
225
226 call setline(1, '<div></div>')
227 normal 0f<cityyy
228 call assert_equal('<div>yyy</div>', getline(1))
229
230 call setline(1, '<div></div>')
231 normal 0f<vitsaaa
232 call assert_equal('aaa', getline(1))
233
Dominique Pelle923dce22021-11-21 11:36:04 +0000234 " selecting a tag block in a non-empty blank line should fail
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200235 call setline(1, ' ')
236 call assert_beeps('normal $vaty')
237
Bram Moolenaarb476cb72018-08-16 21:37:50 +0200238 bwipe!
239endfunc
240
Bram Moolenaardb510072017-09-28 21:52:17 +0200241" Tests for match() and matchstr()
242func Test_match()
243 call assert_equal("b", matchstr("abcd", ".", 0, 2))
244 call assert_equal("bc", matchstr("abcd", "..", 0, 2))
245 call assert_equal("c", matchstr("abcd", ".", 2, 0))
246 call assert_equal("a", matchstr("abcd", ".", 0, -1))
247 call assert_equal(-1, match("abcd", ".", 0, 5))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200248 call assert_equal(0, match("abcd", ".", 0, -1))
249 call assert_equal(0, match('abc', '.', 0, 1))
250 call assert_equal(1, match('abc', '.', 0, 2))
251 call assert_equal(2, match('abc', '.', 0, 3))
Bram Moolenaardb510072017-09-28 21:52:17 +0200252 call assert_equal(-1, match('abc', '.', 0, 4))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200253 call assert_equal(1, match('abc', '.', 1, 1))
254 call assert_equal(2, match('abc', '.', 2, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200255 call assert_equal(-1, match('abc', '.', 3, 1))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200256 call assert_equal(3, match('abc', '$', 0, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200257 call assert_equal(-1, match('abc', '$', 0, 2))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200258 call assert_equal(3, match('abc', '$', 1, 1))
259 call assert_equal(3, match('abc', '$', 2, 1))
260 call assert_equal(3, match('abc', '$', 3, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200261 call assert_equal(-1, match('abc', '$', 4, 1))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200262 call assert_equal(0, match('abc', '\zs', 0, 1))
263 call assert_equal(1, match('abc', '\zs', 0, 2))
264 call assert_equal(2, match('abc', '\zs', 0, 3))
265 call assert_equal(3, match('abc', '\zs', 0, 4))
Bram Moolenaardb510072017-09-28 21:52:17 +0200266 call assert_equal(-1, match('abc', '\zs', 0, 5))
Bram Moolenaar9d489562020-07-30 20:08:50 +0200267 call assert_equal(1, match('abc', '\zs', 1, 1))
268 call assert_equal(2, match('abc', '\zs', 2, 1))
269 call assert_equal(3, match('abc', '\zs', 3, 1))
Bram Moolenaardb510072017-09-28 21:52:17 +0200270 call assert_equal(-1, match('abc', '\zs', 4, 1))
271endfunc
Bram Moolenaar82846a02018-02-09 18:09:54 +0100272
273" This was causing an illegal memory access
274func Test_inner_tag()
275 new
276 norm ixxx
277 call feedkeys("v", 'xt')
278 insert
279x
280x
281.
282 norm it
283 q!
284endfunc
Bram Moolenaar85160712018-06-19 18:27:41 +0200285
286func Test_sentence()
287 enew!
288 call setline(1, 'A sentence. A sentence? A sentence!')
289
290 normal yis
291 call assert_equal('A sentence.', @")
292 normal yas
293 call assert_equal('A sentence. ', @")
294
295 normal )
296
297 normal yis
298 call assert_equal('A sentence?', @")
299 normal yas
300 call assert_equal('A sentence? ', @")
301
302 normal )
303
304 normal yis
305 call assert_equal('A sentence!', @")
306 normal yas
307 call assert_equal(' A sentence!', @")
308
309 normal 0
310 normal 2yis
311 call assert_equal('A sentence. ', @")
312 normal 3yis
313 call assert_equal('A sentence. A sentence?', @")
314 normal 2yas
315 call assert_equal('A sentence. A sentence? ', @")
316
317 %delete _
318endfunc
319
320func Test_sentence_with_quotes()
321 enew!
322 call setline(1, 'A "sentence." A sentence.')
323
324 normal yis
325 call assert_equal('A "sentence."', @")
326 normal yas
327 call assert_equal('A "sentence." ', @")
328
329 normal )
330
331 normal yis
332 call assert_equal('A sentence.', @")
333 normal yas
334 call assert_equal(' A sentence.', @")
335
336 %delete _
337endfunc
338
Bram Moolenaar1e115362019-01-09 23:01:02 +0100339func Test_sentence_with_cursor_on_delimiter()
Bram Moolenaar85160712018-06-19 18:27:41 +0200340 enew!
341 call setline(1, "A '([sentence.])' A sentence.")
342
343 normal! 15|yis
344 call assert_equal("A '([sentence.])'", @")
345 normal! 15|yas
346 call assert_equal("A '([sentence.])' ", @")
347
348 normal! 16|yis
349 call assert_equal("A '([sentence.])'", @")
350 normal! 16|yas
351 call assert_equal("A '([sentence.])' ", @")
352
353 normal! 17|yis
354 call assert_equal("A '([sentence.])'", @")
355 normal! 17|yas
356 call assert_equal("A '([sentence.])' ", @")
357
Bram Moolenaar2f03e5a2020-06-18 15:33:25 +0200358 " don't get stuck on a quote at the start of a sentence
359 %delete _
360 call setline(1, ['A sentence.', '"A sentence"?', 'A sentence!'])
361 normal gg))
362 call assert_equal(3, getcurpos()[1])
363
364 %delete _
365 call setline(1, ['A sentence.', "'A sentence'?", 'A sentence!'])
366 normal gg))
367 call assert_equal(3, getcurpos()[1])
368
Bram Moolenaar85160712018-06-19 18:27:41 +0200369 %delete _
370endfunc
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200371
372" Test for the paragraph (ap) text object
373func Test_paragraph()
374 new
375 call setline(1, ['First line.', 'Second line.', 'Third line.'])
376 call cursor(2, 1)
377 normal vapy
378 call assert_equal("First line.\nSecond line.\nThird line.\n", @")
379
380 call cursor(2, 1)
381 call assert_beeps('normal vapapy')
382
383 call setline(1, ['First line.', 'Second line.', ' ', ''])
384 call cursor(1, 1)
385 normal vapy
386 call assert_equal("First line.\nSecond line.\n \n\n", @")
387
388 call setline(1, ['', '', '', 'First line.', 'Second line.'])
389 call cursor(2, 1)
390 normal yap
391 call assert_equal("\n\n\nFirst line.\nSecond line.\n", @")
392 call assert_beeps('normal 3yap')
393 exe "normal \<C-C>"
394
395 %d
396 call setline(1, [' ', ' ', ' '])
397 call cursor(2, 1)
398 normal Vipy
399 call assert_equal(" \n \n \n", @")
400 call cursor(2, 1)
401 call assert_beeps("normal Vipip")
402 exe "normal \<C-C>"
403
404 close!
405endfunc
406
407" Tests for text object aw
408func Test_textobj_a_word()
409 new
410 call append(0, ['foobar,eins,foobar', 'foo,zwei,foo '])
411 " diw
412 norm! 1gg0diw
413 call assert_equal([',eins,foobar', 'foo,zwei,foo ', ''], getline(1,'$'))
414 " daw
415 norm! 2ggEdaw
416 call assert_equal([',eins,foobar', 'foo,zwei,', ''], getline(1, '$'))
417 " daw the last word in a line
418 call setline(1, ['foo bar', 'foo bar', ''])
419 call cursor(1, 5)
420 normal daw
421 call assert_equal('foo', getline(1))
422 " aw in visual mode
423 call cursor(2, 5)
424 normal! vawx
425 call assert_equal('foo', getline(2))
426 %d
427 call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo "])
428 " diW
429 norm! 2ggwd2iW
430 call assert_equal(['foo eins foobar', 'foo foo ', ''], getline(1,'$'))
431 " daW
432 norm! 1ggd2aW
433 call assert_equal(['foobar', 'foo foo ', ''], getline(1,'$'))
434
435 %d
436 call append(0, ["foo\teins\tfoobar", "foo\tzwei\tfoo "])
437 " aw in visual line mode switches to characterwise mode
438 norm! 2gg$Vawd
439 call assert_equal(['foo eins foobar', 'foo zwei foo'], getline(1,'$'))
440 norm! 1gg$Viwd
441 call assert_equal(['foo eins ', 'foo zwei foo'], getline(1,'$'))
442
443 " visually selecting a tab before a word with 'selection' set to 'exclusive'
444 set selection=exclusive
445 normal gg3lvlawy
446 call assert_equal("\teins", @")
447 " visually selecting a tab before a word with 'selection' set to 'inclusive'
448 set selection=inclusive
449 normal gg3lvlawy
450 call assert_equal("\teins\t", @")
451 set selection&
452
453 " selecting a word with no non-space characters in a buffer fails
454 %d
455 call setline(1, ' ')
456 call assert_beeps('normal 3lyaw')
457
458 " visually selecting words backwards with no more words to select
459 call setline(1, 'one two')
460 call assert_beeps('normal 2lvh2aw')
461 exe "normal \<C-C>"
462 call assert_beeps('normal $vh3aw')
463 exe "normal \<C-C>"
464 call setline(1, ['', 'one two'])
465 call assert_beeps('normal 2G2lvh3aw')
466 exe "normal \<C-C>"
467
468 " selecting words forward with no more words to select
469 %d
470 call setline(1, 'one a')
471 call assert_beeps('normal 0y3aw')
472 call setline(1, 'one two ')
473 call assert_beeps('normal 0y3aw')
474 call assert_beeps('normal 03ly2aw')
475
476 " clean up
477 bw!
478endfunc
479
480" Test for is and as text objects
481func Test_textobj_sentence()
482 new
483 call append(0, ['This is a test. With some sentences!', '',
484 \ 'Even with a question? And one more. And no sentence here'])
485 " Test for dis - does not remove trailing whitespace
486 norm! 1gg0dis
487 call assert_equal([' With some sentences!', '',
488 \ 'Even with a question? And one more. And no sentence here', ''],
489 \ getline(1,'$'))
490 " Test for das - removes leading whitespace
491 norm! 3ggf?ldas
492 call assert_equal([' With some sentences!', '',
493 \ 'Even with a question? And no sentence here', ''], getline(1,'$'))
494 " when used in visual mode, is made characterwise
495 norm! 3gg$Visy
496 call assert_equal('v', visualmode())
497 " reset visualmode()
498 norm! 3ggVy
499 norm! 3gg$Vasy
500 call assert_equal('v', visualmode())
501 " basic testing for textobjects a< and at
502 %d
503 call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>',' </div>', ' '])
504 " a<
505 norm! 1gg0da<
506 call assert_equal([' ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
507 norm! 1pj
508 call assert_equal([' <div>', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
509 " at
510 norm! d2at
511 call assert_equal([' '], getline(1,'$'))
512 %d
513 call setline(1, ['<div> ','<a href="foobar" class="foo">xyz</a>',' </div>', ' '])
514 " i<
515 norm! 1gg0di<
516 call assert_equal(['<> ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
517 norm! 1Pj
518 call assert_equal(['<div> ', '<a href="foobar" class="foo">xyz</a>', ' </div>', ' '], getline(1,'$'))
519 norm! d2it
520 call assert_equal(['<div></div>',' '], getline(1,'$'))
521 " basic testing for a[ and i[ text object
522 %d
523 call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
524 norm! 3gg0di[
525 call assert_equal([' ', '[', ']'], getline(1,'$'))
526 call setline(1, [' ', '[', 'one [two]', 'thre', ']'])
527 norm! 3gg0ftd2a[
528 call assert_equal([' '], getline(1,'$'))
529
530 " clean up
531 bw!
532endfunc
533
534" Test for quote (', " and `) textobjects
535func Test_textobj_quote()
536 new
537
538 " Test for i" when cursor is in front of a quoted object
539 call append(0, 'foo "bar"')
540 norm! 1gg0di"
541 call assert_equal(['foo ""', ''], getline(1,'$'))
542
543 " Test for visually selecting an inner quote
544 %d
545 " extend visual selection from one quote to the next
546 call setline(1, 'color "red" color "blue"')
547 call cursor(1, 7)
548 normal v4li"y
549 call assert_equal('"red" color "blue', @")
550
551 " try to extend visual selection from one quote to a non-existing quote
552 call setline(1, 'color "red" color blue')
553 call cursor(1, 7)
554 call feedkeys('v4li"y', 'xt')
555 call assert_equal('"red"', @")
556
557 " try to extend visual selection from one quote to a next partial quote
558 call setline(1, 'color "red" color "blue')
559 call cursor(1, 7)
560 normal v4li"y
561 call assert_equal('"red" color ', @")
562
563 " select a quote backwards in visual mode
564 call cursor(1, 12)
565 normal vhi"y
566 call assert_equal('red" ', @")
567 call assert_equal(8, col('.'))
568
569 " select a quote backwards in visual mode from outside the quote
570 call cursor(1, 17)
571 normal v2hi"y
572 call assert_equal('red', @")
573 call assert_equal(8, col('.'))
574
575 " visually selecting a quote with 'selection' set to 'exclusive'
576 call setline(1, 'He said "How are you?"')
577 set selection=exclusive
578 normal 012lv2li"y
579 call assert_equal('How are you?', @")
580 set selection&
581
582 " try copy a quote object with a single quote in the line
583 call setline(1, "Smith's car")
584 call cursor(1, 6)
585 call assert_beeps("normal yi'")
586 call assert_beeps("normal 2lyi'")
587
588 " selecting space before and after a quoted string
589 call setline(1, "some 'special' string")
590 normal 0ya'
591 call assert_equal("'special' ", @")
592 call setline(1, "some 'special'string")
593 normal 0ya'
594 call assert_equal(" 'special'", @")
595
Dominique Pelleaf631f62021-09-03 16:50:16 +0200596 " quoted string with odd or even number of backslashes.
597 call setline(1, 'char *s = "foo\"bar"')
598 normal $hhyi"
599 call assert_equal('foo\"bar', @")
600 call setline(1, 'char *s = "foo\\"bar"')
601 normal $hhyi"
602 call assert_equal('bar', @")
603 call setline(1, 'char *s = "foo\\\"bar"')
604 normal $hhyi"
605 call assert_equal('foo\\\"bar', @")
606 call setline(1, 'char *s = "foo\\\\"bar"')
607 normal $hhyi"
608 call assert_equal('bar', @")
609
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200610 close!
611endfunc
612
Connor Lane Smithb9115da2021-07-31 13:31:42 +0200613" Test for i(, i<, etc. when cursor is in front of a block
614func Test_textobj_find_paren_forward()
615 new
616
617 " i< and a> when cursor is in front of a block
618 call setline(1, '#include <foo.h>')
619 normal 0yi<
620 call assert_equal('foo.h', @")
621 normal 0ya>
622 call assert_equal('<foo.h>', @")
623
624 " 2i(, 3i( in front of a block enters second/third nested '('
625 call setline(1, 'foo (bar (baz (quux)))')
626 normal 0yi)
627 call assert_equal('bar (baz (quux))', @")
628 normal 02yi)
629 call assert_equal('baz (quux)', @")
630 normal 03yi)
631 call assert_equal('quux', @")
632
633 " 3i( in front of a block doesn't enter third but un-nested '('
634 call setline(1, 'foo (bar (baz) (quux))')
635 normal 03di)
636 call assert_equal('foo (bar (baz) (quux))', getline(1))
637 normal 02di)
638 call assert_equal('foo (bar () (quux))', getline(1))
639 normal 0di)
640 call assert_equal('foo ()', getline(1))
641
642 close!
643endfunc
644
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200645" vim: shiftwidth=2 sts=2 expandtab