blob: b6a6695d17212a29991d223664bb7e85ccf107a5 [file] [log] [blame]
Bram Moolenaarf9660b52016-04-16 22:19:15 +02001" Test commands that jump somewhere.
2
Bram Moolenaar226630a2016-10-08 19:21:31 +02003" Create a new buffer using "lines" and place the cursor on the word after the
4" first occurrence of return and invoke "cmd". The cursor should now be
5" positioned at the given line and col.
6func XTest_goto_decl(cmd, lines, line, col)
Bram Moolenaarf9660b52016-04-16 22:19:15 +02007 new
Bram Moolenaar226630a2016-10-08 19:21:31 +02008 call setline(1, a:lines)
9 /return/
10 normal! W
11 execute 'norm! ' . a:cmd
12 call assert_equal(a:line, line('.'))
13 call assert_equal(a:col, col('.'))
Bram Moolenaarf9660b52016-04-16 22:19:15 +020014 quit!
15endfunc
Bram Moolenaar23c60f22016-06-15 22:03:48 +020016
Bram Moolenaar226630a2016-10-08 19:21:31 +020017func Test_gD()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020018 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 int x;
Bram Moolenaar94722c52023-01-28 19:19:03 +000020
Bram Moolenaare7eb9272019-06-24 00:58:07 +020021 int func(void)
22 {
23 return x;
24 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020025 [CODE]
26
Bram Moolenaar226630a2016-10-08 19:21:31 +020027 call XTest_goto_decl('gD', lines, 1, 5)
28endfunc
29
30func Test_gD_too()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020031 let lines =<< trim [CODE]
Bram Moolenaarc79745a2019-05-20 22:12:34 +020032 Filename x;
Bram Moolenaar94722c52023-01-28 19:19:03 +000033
Bram Moolenaare7eb9272019-06-24 00:58:07 +020034 int Filename
35 int func() {
36 Filename x;
37 return x;
Bram Moolenaarc79745a2019-05-20 22:12:34 +020038 [CODE]
39
Bram Moolenaar226630a2016-10-08 19:21:31 +020040 call XTest_goto_decl('gD', lines, 1, 10)
41endfunc
42
43func Test_gD_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020044 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020045 /* int x; */
46 int x;
Bram Moolenaar94722c52023-01-28 19:19:03 +000047
Bram Moolenaare7eb9272019-06-24 00:58:07 +020048 int func(void)
49 {
50 return x;
51 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020052 [CODE]
53
Bram Moolenaar226630a2016-10-08 19:21:31 +020054 call XTest_goto_decl('gD', lines, 2, 5)
55endfunc
56
57func Test_gD_inline_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020058 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020059 int y /* , x */;
60 int x;
Bram Moolenaar94722c52023-01-28 19:19:03 +000061
Bram Moolenaare7eb9272019-06-24 00:58:07 +020062 int func(void)
63 {
64 return x;
65 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020066 [CODE]
67
Bram Moolenaar226630a2016-10-08 19:21:31 +020068 call XTest_goto_decl('gD', lines, 2, 5)
69endfunc
70
71func Test_gD_string()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020072 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020073 char *s[] = "x";
74 int x = 1;
Bram Moolenaar94722c52023-01-28 19:19:03 +000075
Bram Moolenaare7eb9272019-06-24 00:58:07 +020076 int func(void)
77 {
78 return x;
79 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020080 [CODE]
81
Bram Moolenaar226630a2016-10-08 19:21:31 +020082 call XTest_goto_decl('gD', lines, 2, 5)
83endfunc
84
85func Test_gD_string_same_line()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020086 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +020087 char *s[] = "x", int x = 1;
Bram Moolenaar94722c52023-01-28 19:19:03 +000088
Bram Moolenaare7eb9272019-06-24 00:58:07 +020089 int func(void)
90 {
91 return x;
92 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +020093 [CODE]
94
Bram Moolenaar226630a2016-10-08 19:21:31 +020095 call XTest_goto_decl('gD', lines, 1, 22)
96endfunc
97
98func Test_gD_char()
Bram Moolenaarc79745a2019-05-20 22:12:34 +020099 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200100 char c = 'x';
101 int x = 1;
Bram Moolenaar94722c52023-01-28 19:19:03 +0000102
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200103 int func(void)
104 {
105 return x;
106 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200107 [CODE]
108
Bram Moolenaar226630a2016-10-08 19:21:31 +0200109 call XTest_goto_decl('gD', lines, 2, 5)
110endfunc
111
112func Test_gd()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200113 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200114 int x;
Bram Moolenaar94722c52023-01-28 19:19:03 +0000115
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200116 int func(int x)
117 {
118 return x;
119 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200120 [CODE]
121
Bram Moolenaar226630a2016-10-08 19:21:31 +0200122 call XTest_goto_decl('gd', lines, 3, 14)
123endfunc
124
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200125" Using gd to jump to a declaration in a fold
126func Test_gd_with_fold()
127 new
128 let lines =<< trim END
129 #define ONE 1
130 #define TWO 2
131 #define THREE 3
132
133 TWO
134 END
135 call setline(1, lines)
136 1,3fold
137 call feedkeys('Ggd', 'xt')
138 call assert_equal(2, line('.'))
139 call assert_equal(-1, foldclosedend(2))
140 bw!
141endfunc
142
Bram Moolenaar226630a2016-10-08 19:21:31 +0200143func Test_gd_not_local()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200144 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200145 int func1(void)
146 {
147 return x;
148 }
Bram Moolenaar94722c52023-01-28 19:19:03 +0000149
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200150 int func2(int x)
151 {
152 return x;
153 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200154 [CODE]
155
Bram Moolenaar226630a2016-10-08 19:21:31 +0200156 call XTest_goto_decl('gd', lines, 3, 10)
157endfunc
158
159func Test_gd_kr_style()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200160 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200161 int func(x)
162 int x;
163 {
164 return x;
165 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200166 [CODE]
167
Bram Moolenaar226630a2016-10-08 19:21:31 +0200168 call XTest_goto_decl('gd', lines, 2, 7)
169endfunc
170
171func Test_gd_missing_braces()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200172 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200173 def func1(a)
174 a + 1
175 end
Bram Moolenaar94722c52023-01-28 19:19:03 +0000176
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200177 a = 1
Bram Moolenaar94722c52023-01-28 19:19:03 +0000178
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200179 def func2()
180 return a
181 end
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200182 [CODE]
183
Bram Moolenaar226630a2016-10-08 19:21:31 +0200184 call XTest_goto_decl('gd', lines, 1, 11)
185endfunc
186
187func Test_gd_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200188 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200189 int func(void)
190 {
191 /* int x; */
192 int x;
193 return x;
194 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200195 [CODE]
196
Bram Moolenaar226630a2016-10-08 19:21:31 +0200197 call XTest_goto_decl('gd', lines, 4, 7)
198endfunc
199
200func Test_gd_comment_in_string()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200201 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200202 int func(void)
203 {
204 char *s ="//"; int x;
205 int x;
206 return x;
207 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200208 [CODE]
209
Bram Moolenaar226630a2016-10-08 19:21:31 +0200210 call XTest_goto_decl('gd', lines, 3, 22)
211endfunc
212
213func Test_gd_string_in_comment()
214 set comments=
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200215 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200216 int func(void)
217 {
218 /* " */ int x;
219 int x;
220 return x;
221 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200222 [CODE]
223
Bram Moolenaar226630a2016-10-08 19:21:31 +0200224 call XTest_goto_decl('gd', lines, 3, 15)
225 set comments&
226endfunc
227
228func Test_gd_inline_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200229 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200230 int func(/* x is an int */ int x)
231 {
232 return x;
233 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200234 [CODE]
235
Bram Moolenaar226630a2016-10-08 19:21:31 +0200236 call XTest_goto_decl('gd', lines, 1, 32)
237endfunc
238
239func Test_gd_inline_comment_only()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200240 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200241 int func(void) /* one lonely x */
242 {
243 return x;
244 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200245 [CODE]
246
Bram Moolenaar226630a2016-10-08 19:21:31 +0200247 call XTest_goto_decl('gd', lines, 3, 10)
248endfunc
249
250func Test_gd_inline_comment_body()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200251 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200252 int func(void)
253 {
254 int y /* , x */;
Bram Moolenaar94722c52023-01-28 19:19:03 +0000255
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200256 for (/* int x = 0 */; y < 2; y++);
Bram Moolenaar94722c52023-01-28 19:19:03 +0000257
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200258 int x = 0;
Bram Moolenaar94722c52023-01-28 19:19:03 +0000259
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200260 return x;
261 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200262 [CODE]
263
Bram Moolenaar226630a2016-10-08 19:21:31 +0200264 call XTest_goto_decl('gd', lines, 7, 7)
265endfunc
266
267func Test_gd_trailing_multiline_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200268 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200269 int func(int x) /* x is an int */
270 {
271 return x;
272 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200273 [CODE]
274
Bram Moolenaar226630a2016-10-08 19:21:31 +0200275 call XTest_goto_decl('gd', lines, 1, 14)
276endfunc
277
278func Test_gd_trailing_comment()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200279 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200280 int func(int x) // x is an int
281 {
282 return x;
283 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200284 [CODE]
285
Bram Moolenaar226630a2016-10-08 19:21:31 +0200286 call XTest_goto_decl('gd', lines, 1, 14)
287endfunc
288
289func Test_gd_string()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200290 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200291 int func(void)
292 {
293 char *s = "x";
294 int x = 1;
Bram Moolenaar94722c52023-01-28 19:19:03 +0000295
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200296 return x;
297 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200298 [CODE]
zeertzjqac4ce9e2024-07-17 19:25:38 +0200299
Bram Moolenaar226630a2016-10-08 19:21:31 +0200300 call XTest_goto_decl('gd', lines, 4, 7)
301endfunc
302
303func Test_gd_string_only()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200304 let lines =<< trim [CODE]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200305 int func(void)
306 {
307 char *s = "x";
Bram Moolenaar94722c52023-01-28 19:19:03 +0000308
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200309 return x;
310 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200311 [CODE]
312
Bram Moolenaar226630a2016-10-08 19:21:31 +0200313 call XTest_goto_decl('gd', lines, 5, 10)
Bram Moolenaar23c60f22016-06-15 22:03:48 +0200314endfunc
Bram Moolenaara2477fd2016-12-03 15:13:20 +0100315
zeertzjqfcaed6a2024-02-18 09:33:54 +0100316" Check that setting some options does not change curswant
317func Test_set_options_keep_col()
Bram Moolenaara2477fd2016-12-03 15:13:20 +0100318 new
319 call setline(1, ['long long long line', 'short line'])
320 normal ggfi
321 let pos = getcurpos()
322 normal j
zeertzjqfcaed6a2024-02-18 09:33:54 +0100323 set invhlsearch spell spelllang=en,cjk spelloptions=camel textwidth=80
zeertzjqac4ce9e2024-07-17 19:25:38 +0200324 set cursorline cursorcolumn cursorlineopt=line colorcolumn=+1 winfixbuf
zeertzjqb026a292024-08-10 09:35:20 +0200325 set comments=:# commentstring=#%s define=function
zeertzjqfcaed6a2024-02-18 09:33:54 +0100326 set background=dark
327 set background=light
Bram Moolenaara2477fd2016-12-03 15:13:20 +0100328 normal k
329 call assert_equal(pos, getcurpos())
330 bwipe!
zeertzjqfcaed6a2024-02-18 09:33:54 +0100331 set hlsearch& spell& spelllang& spelloptions& textwidth&
zeertzjqac4ce9e2024-07-17 19:25:38 +0200332 set cursorline& cursorcolumn& cursorlineopt& colorcolumn& winfixbuf&
zeertzjqb026a292024-08-10 09:35:20 +0200333 set comments& commentstring& define&
zeertzjqfcaed6a2024-02-18 09:33:54 +0100334 set background&
Bram Moolenaara2477fd2016-12-03 15:13:20 +0100335endfunc
336
Bram Moolenaar60402d62017-04-20 18:54:50 +0200337func Test_gd_local_block()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200338 let lines =<< trim [CODE]
339 int main()
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200340 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200341 char *a = "NOT NULL";
342 if(a)
343 {
344 char *b = a;
345 printf("%s\n", b);
346 }
347 else
348 {
349 char *b = "NULL";
350 return b;
351 }
Bram Moolenaar94722c52023-01-28 19:19:03 +0000352
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200353 return 0;
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200354 }
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200355 [CODE]
356
Bram Moolenaar60402d62017-04-20 18:54:50 +0200357 call XTest_goto_decl('1gd', lines, 11, 11)
358endfunc
Bram Moolenaar6b69e5c2018-05-26 18:39:32 +0200359
360func Test_motion_if_elif_else_endif()
361 new
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200362 let lines =<< trim END
363 /* Test pressing % on #if, #else #elsif and #endif,
364 * with nested #if
365 */
366 #if FOO
367 /* ... */
368 # if BAR
369 /* ... */
370 # endif
371 #elif BAR
372 /* ... */
373 #else
374 /* ... */
375 #endif
376
377 #define FOO 1
378 END
379 call setline(1, lines)
Bram Moolenaar6b69e5c2018-05-26 18:39:32 +0200380 /#if FOO
381 norm %
382 call assert_equal([9, 1], getpos('.')[1:2])
383 norm %
384 call assert_equal([11, 1], getpos('.')[1:2])
385 norm %
386 call assert_equal([13, 1], getpos('.')[1:2])
387 norm %
388 call assert_equal([4, 1], getpos('.')[1:2])
389 /# if BAR
390 norm $%
391 call assert_equal([8, 1], getpos('.')[1:2])
392 norm $%
393 call assert_equal([6, 1], getpos('.')[1:2])
394
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200395 " Test for [# and ]# command
396 call cursor(5, 1)
397 normal [#
398 call assert_equal([4, 1], getpos('.')[1:2])
399 call cursor(5, 1)
400 normal ]#
401 call assert_equal([9, 1], getpos('.')[1:2])
402 call cursor(10, 1)
403 normal [#
404 call assert_equal([9, 1], getpos('.')[1:2])
405 call cursor(10, 1)
406 normal ]#
407 call assert_equal([11, 1], getpos('.')[1:2])
408
409 " Finding a match before the first line or after the last line should fail
410 normal gg
411 call assert_beeps('normal [#')
412 normal G
413 call assert_beeps('normal ]#')
414
415 " Finding a match for a macro definition (#define) should fail
416 normal G
417 call assert_beeps('normal %')
418
Bram Moolenaar6b69e5c2018-05-26 18:39:32 +0200419 bw!
420endfunc
421
422func Test_motion_c_comment()
423 new
424 a
425/*
426 * Test pressing % on beginning/end
427 * of C comments.
428 */
429/* Another comment */
430.
431 norm gg0%
432 call assert_equal([4, 3], getpos('.')[1:2])
433 norm %
434 call assert_equal([1, 1], getpos('.')[1:2])
435 norm gg0l%
436 call assert_equal([4, 3], getpos('.')[1:2])
437 norm h%
438 call assert_equal([1, 1], getpos('.')[1:2])
439
440 norm G^
441 norm %
442 call assert_equal([5, 21], getpos('.')[1:2])
443 norm %
444 call assert_equal([5, 1], getpos('.')[1:2])
445
446 bw!
447endfunc
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200448
449" vim: shiftwidth=2 sts=2 expandtab