blob: be2f458287d8f85a3622b153fdc7a5f436034575 [file] [log] [blame]
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001" Test for syntax and syntax iskeyword option
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature syntax
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005
Bram Moolenaar4d785892017-06-22 22:00:50 +02006source view_util.vim
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01007source screendump.vim
Bram Moolenaar4d785892017-06-22 22:00:50 +02008
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01009func GetSyntaxItem(pat)
10 let c = ''
11 let a = ['a', getreg('a'), getregtype('a')]
12 0
13 redraw!
14 call search(a:pat, 'W')
15 let synid = synID(line('.'), col('.'), 1)
16 while synid == synID(line('.'), col('.'), 1)
17 norm! v"ay
18 " stop at whitespace
19 if @a =~# '\s'
20 break
21 endif
22 let c .= @a
23 norm! l
24 endw
25 call call('setreg', a)
26 0
27 return c
28endfunc
29
Bram Moolenaarb46f57e2020-11-29 14:11:41 +010030func AssertHighlightGroups(lnum, startcol, expected, trans = 1, msg = "")
31 " Assert that the characters starting at a given (line, col)
32 " sequentially match the expected highlight groups.
33 " If groups are provided as a string, each character is assumed to be a
34 " group and spaces represent no group, useful for visually describing tests.
35 let l:expectedGroups = type(a:expected) == v:t_string
36 \ ? a:expected->split('\zs')->map({_, v -> trim(v)})
37 \ : a:expected
38 let l:errors = 0
39 let l:msg = (a:msg->empty() ? "" : a:msg .. ": ")
40 \ .. "Wrong highlight group at " .. a:lnum .. ","
41
42 for l:i in range(a:startcol, a:startcol + l:expectedGroups->len() - 1)
43 let l:errors += synID(a:lnum, l:i, a:trans)
44 \ ->synIDattr("name")
45 \ ->assert_equal(l:expectedGroups[l:i - 1],
46 \ l:msg .. l:i)
47 endfor
48endfunc
49
Bram Moolenaarb8060fe2016-01-19 22:29:28 +010050func Test_syn_iskeyword()
51 new
52 call setline(1, [
53 \ 'CREATE TABLE FOOBAR(',
54 \ ' DLTD_BY VARCHAR2(100)',
55 \ ');',
Bram Moolenaar037c54f2019-04-20 23:47:46 +020056 \ ''])
Bram Moolenaarb8060fe2016-01-19 22:29:28 +010057
58 syntax on
59 set ft=sql
60 syn match SYN /C\k\+\>/
61 hi link SYN ErrorMsg
62 call assert_equal('DLTD_BY', GetSyntaxItem('DLTD'))
63 /\<D\k\+\>/:norm! ygn
64 call assert_equal('DLTD_BY', @0)
65 redir @c
66 syn iskeyword
67 redir END
68 call assert_equal("\nsyntax iskeyword not set", @c)
69
70 syn iskeyword @,48-57,_,192-255
71 redir @c
72 syn iskeyword
73 redir END
74 call assert_equal("\nsyntax iskeyword @,48-57,_,192-255", @c)
75
76 setlocal isk-=_
77 call assert_equal('DLTD_BY', GetSyntaxItem('DLTD'))
78 /\<D\k\+\>/:norm! ygn
Bram Moolenaar73b484c2016-12-11 15:11:17 +010079 let b2 = @0
Bram Moolenaarb8060fe2016-01-19 22:29:28 +010080 call assert_equal('DLTD', @0)
81
82 syn iskeyword clear
83 redir @c
84 syn iskeyword
85 redir END
86 call assert_equal("\nsyntax iskeyword not set", @c)
87
88 quit!
89endfunc
Bram Moolenaarc3691332016-04-20 12:49:49 +020090
91func Test_syntax_after_reload()
92 split Xsomefile
93 call setline(1, ['hello', 'there'])
94 w!
95 only!
96 setl filetype=hello
97 au FileType hello let g:gotit = 1
98 call assert_false(exists('g:gotit'))
99 edit other
100 buf Xsomefile
101 call assert_equal('hello', &filetype)
102 call assert_true(exists('g:gotit'))
103 call delete('Xsomefile')
104endfunc
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100105
106func Test_syntime()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200107 CheckFeature profile
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100108
109 syntax on
110 syntime on
111 let a = execute('syntime report')
112 call assert_equal("\nNo Syntax items defined for this buffer", a)
113
Dominique Pelle6d37e8e2021-05-06 17:36:55 +0200114 let a = execute('syntime clear')
115 call assert_equal("\nNo Syntax items defined for this buffer", a)
116
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100117 view ../memfile_test.c
118 setfiletype cpp
119 redraw
120 let a = execute('syntime report')
121 call assert_match('^ TOTAL *COUNT *MATCH *SLOWEST *AVERAGE *NAME *PATTERN', a)
122 call assert_match(' \d*\.\d* \+[^0]\d* .* cppRawString ', a)
123 call assert_match(' \d*\.\d* \+[^0]\d* .* cppNumber ', a)
124
125 syntime off
126 syntime clear
127 let a = execute('syntime report')
128 call assert_match('^ TOTAL *COUNT *MATCH *SLOWEST *AVERAGE *NAME *PATTERN', a)
129 call assert_notmatch('.* cppRawString *', a)
130 call assert_notmatch('.* cppNumber*', a)
131 call assert_notmatch('[1-9]', a)
132
Bram Moolenaare2e40752020-09-04 21:18:46 +0200133 call assert_fails('syntime abc', 'E475:')
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100134
135 syntax clear
136 let a = execute('syntime report')
137 call assert_equal("\nNo Syntax items defined for this buffer", a)
138
139 bd
140endfunc
141
Bram Moolenaarb513d302018-12-02 14:55:08 +0100142func Test_syntime_completion()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200143 CheckFeature profile
Bram Moolenaarb513d302018-12-02 14:55:08 +0100144
145 call feedkeys(":syntime \<C-A>\<C-B>\"\<CR>", 'tx')
146 call assert_equal('"syntime clear off on report', @:)
147endfunc
148
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100149func Test_syntax_list()
150 syntax on
151 let a = execute('syntax list')
152 call assert_equal("\nNo Syntax items defined for this buffer", a)
153
154 view ../memfile_test.c
155 setfiletype c
156
157 let a = execute('syntax list')
158 call assert_match('cInclude*', a)
159 call assert_match('cDefine', a)
160
161 let a = execute('syntax list cDefine')
162 call assert_notmatch('cInclude*', a)
163 call assert_match('cDefine', a)
164 call assert_match(' links to Macro$', a)
165
166 call assert_fails('syntax list ABCD', 'E28:')
167 call assert_fails('syntax list @ABCD', 'E392:')
168
169 syntax clear
170 let a = execute('syntax list')
171 call assert_equal("\nNo Syntax items defined for this buffer", a)
172
Bram Moolenaar08f41572020-04-20 16:50:00 +0200173 syntax keyword Type int containedin=g1 skipwhite skipempty skipnl nextgroup=Abc
174 let exp = "Type xxx containedin=g1 nextgroup=Abc skipnl skipwhite skipempty int"
175 call assert_equal(exp, split(execute("syntax list"), "\n")[1])
176
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100177 bd
178endfunc
179
180func Test_syntax_completion()
181 call feedkeys(":syn \<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaare35a52a2020-05-31 19:48:53 +0200182 call assert_equal('"syn case clear cluster conceal enable foldlevel include iskeyword keyword list manual match off on region reset spell sync', @:)
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100183
184 call feedkeys(":syn case \<C-A>\<C-B>\"\<CR>", 'tx')
185 call assert_equal('"syn case ignore match', @:)
186
Bram Moolenaar2d028392017-01-08 18:28:22 +0100187 call feedkeys(":syn spell \<C-A>\<C-B>\"\<CR>", 'tx')
188 call assert_equal('"syn spell default notoplevel toplevel', @:)
189
190 call feedkeys(":syn sync \<C-A>\<C-B>\"\<CR>", 'tx')
191 call assert_equal('"syn sync ccomment clear fromstart linebreaks= linecont lines= match maxlines= minlines= region', @:)
192
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100193 " Check that clearing "Aap" avoids it showing up before Boolean.
194 hi Aap ctermfg=blue
195 call feedkeys(":syn list \<C-A>\<C-B>\"\<CR>", 'tx')
196 call assert_match('^"syn list Aap Boolean Character ', @:)
197 hi clear Aap
198
Bram Moolenaar73b484c2016-12-11 15:11:17 +0100199 call feedkeys(":syn list \<C-A>\<C-B>\"\<CR>", 'tx')
200 call assert_match('^"syn list Boolean Character ', @:)
201
202 call feedkeys(":syn match \<C-A>\<C-B>\"\<CR>", 'tx')
203 call assert_match('^"syn match Boolean Character ', @:)
204endfunc
Bram Moolenaarde318c52017-01-17 16:27:10 +0100205
Bram Moolenaar297610b2019-12-27 17:20:55 +0100206func Test_echohl_completion()
207 call feedkeys(":echohl no\<C-A>\<C-B>\"\<CR>", 'tx')
208 call assert_equal('"echohl NonText Normal none', @:)
209endfunc
210
Bram Moolenaarde318c52017-01-17 16:27:10 +0100211func Test_syntax_arg_skipped()
212 syn clear
213 syntax case ignore
214 if 0
215 syntax case match
216 endif
217 call assert_match('case ignore', execute('syntax case'))
218
219 syn keyword Foo foo
220 call assert_match('Foo', execute('syntax'))
221 syn clear
222 call assert_match('case match', execute('syntax case'))
223 call assert_notmatch('Foo', execute('syntax'))
224
225 if has('conceal')
226 syn clear
227 syntax conceal on
228 if 0
229 syntax conceal off
230 endif
231 call assert_match('conceal on', execute('syntax conceal'))
232 syn clear
233 call assert_match('conceal off', execute('syntax conceal'))
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100234
235 syntax conceal on
236 syntax conceal off
237 call assert_match('conceal off', execute('syntax conceal'))
Bram Moolenaarde318c52017-01-17 16:27:10 +0100238 endif
239
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100240 syntax region Bar start=/</ end=/>/
Bram Moolenaarde318c52017-01-17 16:27:10 +0100241 if 0
242 syntax region NotTest start=/</ end=/>/ contains=@Spell
243 endif
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100244 call assert_match('Bar', execute('syntax'))
Bram Moolenaarde318c52017-01-17 16:27:10 +0100245 call assert_notmatch('NotTest', execute('syntax'))
246 call assert_notmatch('Spell', execute('syntax'))
247
248 hi Foo ctermfg=blue
249 let a = execute('hi Foo')
250 if 0
251 syntax rest
252 endif
253 call assert_equal(a, execute('hi Foo'))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100254 hi clear Bar
255 hi clear Foo
Bram Moolenaarde318c52017-01-17 16:27:10 +0100256
257 set ft=tags
258 syn off
259 if 0
260 syntax enable
261 endif
262 call assert_match('No Syntax items defined', execute('syntax'))
263 syntax enable
264 call assert_match('tagComment', execute('syntax'))
265 set ft=
266
267 syn clear
268 if 0
269 syntax include @Spell nothing
270 endif
271 call assert_notmatch('Spell', execute('syntax'))
272
273 syn clear
274 syn iskeyword 48-57,$,_
275 call assert_match('48-57,$,_', execute('syntax iskeyword'))
276 if 0
277 syn clear
278 syn iskeyword clear
279 endif
280 call assert_match('48-57,$,_', execute('syntax iskeyword'))
281 syn iskeyword clear
282 call assert_match('not set', execute('syntax iskeyword'))
283 syn iskeyword 48-57,$,_
284 syn clear
285 call assert_match('not set', execute('syntax iskeyword'))
286
287 syn clear
288 syn keyword Foo foo
289 if 0
290 syn keyword NotAdded bar
291 endif
292 call assert_match('Foo', execute('syntax'))
293 call assert_notmatch('NotAdded', execute('highlight'))
294
295 syn clear
296 syn keyword Foo foo
297 call assert_match('Foo', execute('syntax'))
298 call assert_match('Foo', execute('syntax list'))
299 call assert_notmatch('Foo', execute('if 0 | syntax | endif'))
300 call assert_notmatch('Foo', execute('if 0 | syntax list | endif'))
301
302 syn clear
303 syn match Fopi /asdf/
304 if 0
305 syn match Fopx /asdf/
306 endif
307 call assert_match('Fopi', execute('syntax'))
308 call assert_notmatch('Fopx', execute('syntax'))
309
310 syn clear
311 syn spell toplevel
312 call assert_match('spell toplevel', execute('syntax spell'))
313 if 0
314 syn spell notoplevel
315 endif
316 call assert_match('spell toplevel', execute('syntax spell'))
317 syn spell notoplevel
318 call assert_match('spell notoplevel', execute('syntax spell'))
319 syn spell default
320 call assert_match('spell default', execute('syntax spell'))
321
322 syn clear
323 if 0
324 syntax cluster Spell
325 endif
326 call assert_notmatch('Spell', execute('syntax'))
327
328 syn clear
329 syn keyword Foo foo
330 syn sync ccomment
331 syn sync maxlines=5
332 if 0
333 syn sync maxlines=11
334 endif
335 call assert_match('on C-style comments', execute('syntax sync'))
336 call assert_match('maximal 5 lines', execute('syntax sync'))
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100337 syn sync clear
Bram Moolenaarde318c52017-01-17 16:27:10 +0100338 if 0
339 syn sync ccomment
340 endif
341 call assert_notmatch('on C-style comments', execute('syntax sync'))
Bram Moolenaar99502802020-11-18 16:53:23 +0100342 syn sync fromstart
343 call assert_match('syncing starts at the first line', execute('syntax sync'))
Bram Moolenaarde318c52017-01-17 16:27:10 +0100344
345 syn clear
346endfunc
347
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200348" Check for an error. Used when multiple errors are thrown and we are checking
349" for an earliest error.
350func AssertFails(cmd, errcode)
351 let save_exception = ''
352 try
353 exe a:cmd
354 catch
355 let save_exception = v:exception
356 endtry
357 call assert_match(a:errcode, save_exception)
358endfunc
359
Bram Moolenaarea588152017-04-10 22:45:30 +0200360func Test_syntax_invalid_arg()
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100361 call assert_fails('syntax case asdf', 'E390:')
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100362 if has('conceal')
363 call assert_fails('syntax conceal asdf', 'E390:')
364 endif
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100365 call assert_fails('syntax spell asdf', 'E390:')
Bram Moolenaarea588152017-04-10 22:45:30 +0200366 call assert_fails('syntax clear @ABCD', 'E391:')
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200367 call assert_fails('syntax include random_file', 'E484:')
368 call assert_fails('syntax include <afile>', 'E495:')
Bram Moolenaarea588152017-04-10 22:45:30 +0200369 call assert_fails('syntax sync x', 'E404:')
370 call assert_fails('syntax keyword Abc a[', 'E789:')
371 call assert_fails('syntax keyword Abc a[bc]d', 'E890:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200372 call assert_fails('syntax cluster Abc add=A add=', 'E406:')
Bram Moolenaar476a6132020-04-08 19:48:56 +0200373
374 " Test for too many \z\( and unmatched \z\(
375 " Not able to use assert_fails() here because both E50:/E879: and E475:
376 " messages are emitted.
377 set regexpengine=1
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200378 call AssertFails("syntax region MyRegion start='\\z\\(' end='\\*/'", 'E52:')
Bram Moolenaar476a6132020-04-08 19:48:56 +0200379
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200380 let cmd = "syntax region MyRegion start='"
381 let cmd ..= repeat("\\z\\(.\\)", 10) .. "' end='\*/'"
382 call AssertFails(cmd, 'E50:')
Bram Moolenaar476a6132020-04-08 19:48:56 +0200383
384 set regexpengine=2
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200385 call AssertFails("syntax region MyRegion start='\\z\\(' end='\\*/'", 'E54:')
Bram Moolenaar476a6132020-04-08 19:48:56 +0200386
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200387 let cmd = "syntax region MyRegion start='"
388 let cmd ..= repeat("\\z\\(.\\)", 10) .. "' end='\*/'"
389 call AssertFails(cmd, 'E879:')
Bram Moolenaar476a6132020-04-08 19:48:56 +0200390 set regexpengine&
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200391
392 call AssertFails('syntax keyword cMyItem grouphere G1', 'E393:')
393 call AssertFails('syntax sync match Abc grouphere MyItem "abc"', 'E394:')
394 call AssertFails('syn keyword Type contains int', 'E395:')
395 call assert_fails('syntax include @Xxx', 'E397:')
396 call AssertFails('syntax region X start', 'E398:')
397 call assert_fails('syntax region X start="{"', 'E399:')
398 call AssertFails('syntax cluster contains=Abc', 'E400:')
399 call AssertFails("syntax match Character /'.'", 'E401:')
400 call AssertFails("syntax match Character /'.'/a", 'E402:')
Bram Moolenaar531be472020-09-23 22:38:05 +0200401 call assert_fails('syntax sync linecont /\%(/', 'E53:')
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200402 call assert_fails('syntax sync linecont /pat', 'E404:')
403 call assert_fails('syntax sync linecont', 'E404:')
404 call assert_fails('syntax sync linecont /pat1/ linecont /pat2/', 'E403:')
405 call assert_fails('syntax sync minlines=a', 'E404:')
406 call AssertFails('syntax match ABC /x/ contains=', 'E406:')
407 call AssertFails("syntax match Character contains /'.'/", 'E405:')
408 call AssertFails('syntax match ccFoo "Foo" nextgroup=ALLBUT,F', 'E407:')
409 call AssertFails('syntax region Block start="{" contains=F,ALLBUT', 'E408:')
410 call AssertFails("syntax match Characters contains=a.*x /'.'/", 'E409:')
Bram Moolenaar531be472020-09-23 22:38:05 +0200411 call assert_fails('syntax match Search /abc/ contains=ALLBUT,/\%(/', 'E53:')
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100412endfunc
413
414func Test_syn_sync()
415 syntax region HereGroup start=/this/ end=/that/
416 syntax sync match SyncHere grouphere HereGroup "pattern"
417 call assert_match('SyncHere', execute('syntax sync'))
418 syn sync clear
419 call assert_notmatch('SyncHere', execute('syntax sync'))
420 syn clear
421endfunc
422
423func Test_syn_clear()
424 syntax keyword Foo foo
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100425 syntax keyword Bar tar
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100426 call assert_match('Foo', execute('syntax'))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100427 call assert_match('Bar', execute('syntax'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200428 call assert_equal('Foo', synIDattr(hlID("Foo"), "name"))
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100429 syn clear Foo
430 call assert_notmatch('Foo', execute('syntax'))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100431 call assert_match('Bar', execute('syntax'))
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200432 call assert_equal('Foo', synIDattr(hlID("Foo"), "name"))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100433 syn clear Foo Bar
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100434 call assert_notmatch('Foo', execute('syntax'))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100435 call assert_notmatch('Bar', execute('syntax'))
436 hi clear Foo
Bram Moolenaarc96272e2017-03-26 13:50:09 +0200437 call assert_equal('Foo', synIDattr(hlID("Foo"), "name"))
Bram Moolenaard61e8aa2017-01-17 17:44:46 +0100438 hi clear Bar
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200439 call assert_fails('syntax clear invalid_syngroup', 'E28:')
Bram Moolenaar58f60ca2017-01-17 17:19:00 +0100440endfunc
Bram Moolenaar4007ed42017-01-17 18:14:54 +0100441
442func Test_invalid_name()
443 syn clear
444 syn keyword Nop yes
445 call assert_fails("syntax keyword Wr\x17ong bar", 'E669:')
446 syntax keyword @Wrong bar
447 call assert_match('W18:', execute('1messages'))
448 syn clear
449 hi clear Nop
450 hi clear @Wrong
451endfunc
Bram Moolenaarf8ec9982017-04-09 15:41:31 +0200452
453func Test_ownsyntax()
454 new Xfoo
455 call setline(1, '#define FOO')
456 syntax on
457 set filetype=c
Bram Moolenaard1f76af2020-09-13 22:37:34 +0200458
Bram Moolenaarf8ec9982017-04-09 15:41:31 +0200459 ownsyntax perl
Bram Moolenaard1f76af2020-09-13 22:37:34 +0200460 " this should not crash
461 set
462
Bram Moolenaarf8ec9982017-04-09 15:41:31 +0200463 call assert_equal('perlComment', synIDattr(synID(line('.'), col('.'), 1), 'name'))
464 call assert_equal('c', b:current_syntax)
465 call assert_equal('perl', w:current_syntax)
466
467 " A new split window should have the original syntax.
468 split
469 call assert_equal('cDefine', synIDattr(synID(line('.'), col('.'), 1), 'name'))
470 call assert_equal('c', b:current_syntax)
471 call assert_equal(0, exists('w:current_syntax'))
472
473 wincmd x
474 call assert_equal('perlComment', synIDattr(synID(line("."), col("."), 1), "name"))
475
476 syntax off
477 set filetype&
478 %bw!
479endfunc
480
481func Test_ownsyntax_completion()
482 call feedkeys(":ownsyntax java\<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaarea7a08a2019-08-26 22:38:22 +0200483 call assert_equal('"ownsyntax java javacc javascript javascriptreact', @:)
Bram Moolenaarf8ec9982017-04-09 15:41:31 +0200484endfunc
Bram Moolenaarea588152017-04-10 22:45:30 +0200485
486func Test_highlight_invalid_arg()
487 if has('gui_running')
488 call assert_fails('hi XXX guifg=xxx', 'E254:')
489 endif
490 call assert_fails('hi DoesNotExist', 'E411:')
491 call assert_fails('hi link', 'E412:')
492 call assert_fails('hi link a', 'E412:')
493 call assert_fails('hi link a b c', 'E413:')
494 call assert_fails('hi XXX =', 'E415:')
495 call assert_fails('hi XXX cterm', 'E416:')
496 call assert_fails('hi XXX cterm=', 'E417:')
497 call assert_fails('hi XXX cterm=DoesNotExist', 'E418:')
498 call assert_fails('hi XXX ctermfg=DoesNotExist', 'E421:')
499 call assert_fails('hi XXX xxx=White', 'E423:')
500endfunc
501
Bram Moolenaar1615b362017-06-04 21:06:09 +0200502func Test_bg_detection()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200503 CheckNotGui
504
Dominique Pelle923dce22021-11-21 11:36:04 +0000505 " auto-detection of &bg, make sure it isn't set anywhere before this test
Bram Moolenaar1615b362017-06-04 21:06:09 +0200506 hi Normal ctermbg=0
507 call assert_equal('dark', &bg)
508 hi Normal ctermbg=4
509 call assert_equal('dark', &bg)
510 hi Normal ctermbg=12
511 call assert_equal('light', &bg)
512 hi Normal ctermbg=15
513 call assert_equal('light', &bg)
514
Bram Moolenaar0b2eef22017-06-27 15:43:49 +0200515 " manually-set &bg takes precedence over auto-detection
Bram Moolenaar1615b362017-06-04 21:06:09 +0200516 set bg=light
517 hi Normal ctermbg=4
518 call assert_equal('light', &bg)
519 set bg=dark
520 hi Normal ctermbg=12
521 call assert_equal('dark', &bg)
Bram Moolenaar6acadda2018-02-24 16:51:32 +0100522
523 hi Normal ctermbg=NONE
Bram Moolenaar1615b362017-06-04 21:06:09 +0200524endfunc
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200525
526func Test_syntax_hangs()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200527 CheckFunction reltimefloat
528 CheckFeature syntax
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200529
530 " This pattern takes a long time to match, it should timeout.
531 new
532 call setline(1, ['aaa', repeat('abc ', 1000), 'ccc'])
533 let start = reltime()
534 set nolazyredraw redrawtime=101
535 syn match Error /\%#=1a*.*X\@<=b*/
536 redraw
537 let elapsed = reltimefloat(reltime(start))
538 call assert_true(elapsed > 0.1)
539 call assert_true(elapsed < 1.0)
540
541 " second time syntax HL is disabled
542 let start = reltime()
543 redraw
544 let elapsed = reltimefloat(reltime(start))
545 call assert_true(elapsed < 0.1)
546
547 " after CTRL-L the timeout flag is reset
548 let start = reltime()
549 exe "normal \<C-L>"
550 redraw
551 let elapsed = reltimefloat(reltime(start))
552 call assert_true(elapsed > 0.1)
553 call assert_true(elapsed < 1.0)
554
555 set redrawtime&
556 bwipe!
557endfunc
Bram Moolenaar4d785892017-06-22 22:00:50 +0200558
Bram Moolenaar4d785892017-06-22 22:00:50 +0200559func Test_conceal()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200560 CheckFeature conceal
Bram Moolenaar4d785892017-06-22 22:00:50 +0200561
562 new
563 call setline(1, ['', '123456'])
564 syn match test23 "23" conceal cchar=X
565 syn match test45 "45" conceal
566
567 set conceallevel=0
568 call assert_equal('123456 ', ScreenLines(2, 7)[0])
Bram Moolenaarcc0750d2017-06-24 22:29:24 +0200569 call assert_equal([[0, '', 0], [0, '', 0], [0, '', 0], [0, '', 0], [0, '', 0], [0, '', 0]], map(range(1, 6), 'synconcealed(2, v:val)'))
Bram Moolenaar4d785892017-06-22 22:00:50 +0200570
571 set conceallevel=1
572 call assert_equal('1X 6 ', ScreenLines(2, 7)[0])
Bram Moolenaarcc0750d2017-06-24 22:29:24 +0200573 call assert_equal([[0, '', 0], [1, 'X', 1], [1, 'X', 1], [1, ' ', 2], [1, ' ', 2], [0, '', 0]], map(range(1, 6), 'synconcealed(2, v:val)'))
Bram Moolenaar4d785892017-06-22 22:00:50 +0200574
575 set conceallevel=1
576 set listchars=conceal:Y
Bram Moolenaarcc0750d2017-06-24 22:29:24 +0200577 call assert_equal([[0, '', 0], [1, 'X', 1], [1, 'X', 1], [1, 'Y', 2], [1, 'Y', 2], [0, '', 0]], map(range(1, 6), 'synconcealed(2, v:val)'))
Bram Moolenaar4d785892017-06-22 22:00:50 +0200578 call assert_equal('1XY6 ', ScreenLines(2, 7)[0])
579
580 set conceallevel=2
581 call assert_match('1X6 ', ScreenLines(2, 7)[0])
Bram Moolenaarcc0750d2017-06-24 22:29:24 +0200582 call assert_equal([[0, '', 0], [1, 'X', 1], [1, 'X', 1], [1, '', 2], [1, '', 2], [0, '', 0]], map(range(1, 6), 'synconcealed(2, v:val)'))
Bram Moolenaar4d785892017-06-22 22:00:50 +0200583
584 set conceallevel=3
585 call assert_match('16 ', ScreenLines(2, 7)[0])
Bram Moolenaarcc0750d2017-06-24 22:29:24 +0200586 call assert_equal([[0, '', 0], [1, '', 1], [1, '', 1], [1, '', 2], [1, '', 2], [0, '', 0]], map(range(1, 6), 'synconcealed(2, v:val)'))
Bram Moolenaar4d785892017-06-22 22:00:50 +0200587
Bram Moolenaarfbf21222020-04-19 18:31:25 +0200588 call AssertFails("syntax match Entity '&amp;' conceal cchar=\<Tab>", 'E844:')
589
Bram Moolenaar4d785892017-06-22 22:00:50 +0200590 syn clear
591 set conceallevel&
592 bw!
593endfunc
Bram Moolenaar0b2eef22017-06-27 15:43:49 +0200594
Bram Moolenaarda650582018-02-20 15:51:40 +0100595func Test_synstack_synIDtrans()
Bram Moolenaar0b2eef22017-06-27 15:43:49 +0200596 new
597 setfiletype c
598 syntax on
599 call setline(1, ' /* A comment with a TODO */')
600
601 call assert_equal([], synstack(1, 1))
602
603 norm f/
Bram Moolenaara74e4942019-08-04 17:35:53 +0200604 eval synstack(line("."), col("."))->map('synIDattr(v:val, "name")')->assert_equal(['cComment', 'cCommentStart'])
605 eval synstack(line("."), col("."))->map('synIDattr(synIDtrans(v:val), "name")')->assert_equal(['Comment', 'Comment'])
Bram Moolenaar0b2eef22017-06-27 15:43:49 +0200606
607 norm fA
608 call assert_equal(['cComment'], map(synstack(line("."), col(".")), 'synIDattr(v:val, "name")'))
609 call assert_equal(['Comment'], map(synstack(line("."), col(".")), 'synIDattr(synIDtrans(v:val), "name")'))
610
611 norm fT
612 call assert_equal(['cComment', 'cTodo'], map(synstack(line("."), col(".")), 'synIDattr(v:val, "name")'))
613 call assert_equal(['Comment', 'Todo'], map(synstack(line("."), col(".")), 'synIDattr(synIDtrans(v:val), "name")'))
614
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100615 call assert_fails("let n=synIDtrans([])", 'E745:')
616
Bram Moolenaar0b2eef22017-06-27 15:43:49 +0200617 syn clear
618 bw!
619endfunc
Bram Moolenaarda650582018-02-20 15:51:40 +0100620
621" Check highlighting for a small piece of C code with a screen dump.
622func Test_syntax_c()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200623 CheckRunVimInTerminal
Bram Moolenaarda650582018-02-20 15:51:40 +0100624 call writefile([
625 \ '/* comment line at the top */',
Bram Moolenaar83e9a1c2019-10-20 14:51:23 +0200626 \ 'int main(int argc, char **argv) { // another comment',
Bram Moolenaarda650582018-02-20 15:51:40 +0100627 \ '#if 0',
628 \ ' int not_used;',
629 \ '#else',
630 \ ' int used;',
631 \ '#endif',
632 \ ' printf("Just an example piece of C code\n");',
633 \ ' return 0x0ff;',
634 \ '}',
Bram Moolenaar82260af2019-10-20 13:16:22 +0200635 \ "\t\t ",
Bram Moolenaarda650582018-02-20 15:51:40 +0100636 \ ' static void',
637 \ 'myFunction(const double count, struct nothing, long there) {',
Bram Moolenaar84590062019-10-18 23:12:20 +0200638 \ "\t// 123: nothing to endif here",
Bram Moolenaar9115c612019-10-16 16:57:06 +0200639 \ "\tfor (int i = 0; i < count; ++i) {",
640 \ "\t break;",
641 \ "\t}",
Bram Moolenaarbbfd1562019-10-19 20:38:15 +0200642 \ "\tNote: asdf",
Bram Moolenaarda650582018-02-20 15:51:40 +0100643 \ '}',
644 \ ], 'Xtest.c')
Bram Moolenaarb7ea7cb2018-02-24 14:38:51 +0100645
646 " This makes the default for 'background' use "dark", check that the
647 " response to t_RB corrects it to "light".
648 let $COLORFGBG = '15;0'
649
Bram Moolenaar83e9a1c2019-10-20 14:51:23 +0200650 let buf = RunVimInTerminal('Xtest.c', {})
Bram Moolenaarbbfd1562019-10-19 20:38:15 +0200651 call term_sendkeys(buf, ":syn keyword Search Note\r")
Bram Moolenaar82260af2019-10-20 13:16:22 +0200652 call term_sendkeys(buf, ":syn match Error /^\\s\\+$/\r")
Bram Moolenaar84590062019-10-18 23:12:20 +0200653 call term_sendkeys(buf, ":set hlsearch\r")
654 call term_sendkeys(buf, "/endif\r")
655 call term_sendkeys(buf, "vjfC")
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +0100656 call VerifyScreenDump(buf, 'Test_syntax_c_01', {})
Bram Moolenaar84590062019-10-18 23:12:20 +0200657
658 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarda650582018-02-20 15:51:40 +0100659 call StopVimInTerminal(buf)
660
Bram Moolenaarb7ea7cb2018-02-24 14:38:51 +0100661 let $COLORFGBG = ''
Bram Moolenaarda650582018-02-20 15:51:40 +0100662 call delete('Xtest.c')
663endfun
Bram Moolenaarbcf94422018-06-23 14:21:42 +0200664
Dominique Pelle354b23a2021-12-17 17:32:29 +0000665" Test \z(...) along with \z1
666func Test_syn_zsub()
667 new
668 syntax on
669 call setline(1, 'xxx start foo xxx not end foo xxx end foo xxx')
670 let l:expected = ' ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ '
671
672 for l:re in [0, 1, 2]
673 " Example taken from :help :syn-ext-match
674 syntax region Z start="start \z(\I\i*\)" skip="not end \z1" end="end \z1"
675 eval AssertHighlightGroups(1, 1, l:expected, 1, 'regexp=' .. l:re)
676 syntax clear Z
677 endfor
678
679 set re&
680 bw!
681endfunc
682
Bram Moolenaarbcf94422018-06-23 14:21:42 +0200683" Using \z() in a region with NFA failing should not crash.
684func Test_syn_wrong_z_one()
685 new
686 call setline(1, ['just some text', 'with foo and bar to match with'])
687 syn region FooBar start="foo\z(.*\)bar" end="\z1"
688 call test_override("nfa_fail", 1)
689 redraw!
690 redraw!
691 call test_override("ALL", 0)
692 bwipe!
693endfunc
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200694
695func Test_syntax_after_bufdo()
696 call writefile(['/* aaa comment */'], 'Xaaa.c')
697 call writefile(['/* bbb comment */'], 'Xbbb.c')
698 call writefile(['/* ccc comment */'], 'Xccc.c')
699 call writefile(['/* ddd comment */'], 'Xddd.c')
700
701 let bnr = bufnr('%')
702 new Xaaa.c
703 badd Xbbb.c
704 badd Xccc.c
705 badd Xddd.c
706 exe "bwipe " . bnr
707 let l = []
708 bufdo call add(l, bufnr('%'))
709 call assert_equal(4, len(l))
710
711 syntax on
712
713 " This used to only enable syntax HL in the last buffer.
714 bufdo tab split
715 tabrewind
716 for tab in range(1, 4)
717 norm fm
718 call assert_equal(['cComment'], map(synstack(line("."), col(".")), 'synIDattr(v:val, "name")'))
719 tabnext
720 endfor
721
722 bwipe! Xaaa.c
723 bwipe! Xbbb.c
724 bwipe! Xccc.c
725 bwipe! Xddd.c
726 syntax off
727 call delete('Xaaa.c')
728 call delete('Xbbb.c')
729 call delete('Xccc.c')
730 call delete('Xddd.c')
731endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100732
Bram Moolenaare35a52a2020-05-31 19:48:53 +0200733func Test_syntax_foldlevel()
734 new
735 call setline(1, [
736 \ 'void f(int a)',
737 \ '{',
738 \ ' if (a == 1) {',
739 \ ' a = 0;',
740 \ ' } else if (a == 2) {',
741 \ ' a = 1;',
742 \ ' } else {',
743 \ ' a = 2;',
744 \ ' }',
745 \ ' if (a > 0) {',
746 \ ' if (a == 1) {',
747 \ ' a = 0;',
748 \ ' } /* missing newline */ } /* end of outer if */ else {',
749 \ ' a = 1;',
750 \ ' }',
751 \ ' if (a == 1)',
752 \ ' {',
753 \ ' a = 0;',
754 \ ' }',
755 \ ' else if (a == 2)',
756 \ ' {',
757 \ ' a = 1;',
758 \ ' }',
759 \ ' else',
760 \ ' {',
761 \ ' a = 2;',
762 \ ' }',
763 \ '}',
764 \ ])
765 setfiletype c
766 syntax on
767 set foldmethod=syntax
768
Bram Moolenaare2e40752020-09-04 21:18:46 +0200769 call assert_fails('syn foldlevel start start', 'E390:')
770 call assert_fails('syn foldlevel not_an_option', 'E390:')
Bram Moolenaare35a52a2020-05-31 19:48:53 +0200771
772 set foldlevel=1
773
774 syn foldlevel start
775 redir @c
776 syn foldlevel
777 redir END
778 call assert_equal("\nsyntax foldlevel start", @c)
779 syn sync fromstart
Bram Moolenaar99502802020-11-18 16:53:23 +0100780 call assert_match('from the first line$', execute('syn sync'))
Bram Moolenaare35a52a2020-05-31 19:48:53 +0200781 let a = map(range(3,9), 'foldclosed(v:val)')
782 call assert_equal([3,3,3,3,3,3,3], a) " attached cascade folds together
783 let a = map(range(10,15), 'foldclosed(v:val)')
784 call assert_equal([10,10,10,10,10,10], a) " over-attached 'else' hidden
785 let a = map(range(16,27), 'foldclosed(v:val)')
786 let unattached_results = [-1,17,17,17,-1,21,21,21,-1,25,25,25]
787 call assert_equal(unattached_results, a) " unattached cascade folds separately
788
789 syn foldlevel minimum
790 redir @c
791 syn foldlevel
792 redir END
793 call assert_equal("\nsyntax foldlevel minimum", @c)
794 syn sync fromstart
795 let a = map(range(3,9), 'foldclosed(v:val)')
796 call assert_equal([3,3,5,5,7,7,7], a) " attached cascade folds separately
797 let a = map(range(10,15), 'foldclosed(v:val)')
798 call assert_equal([10,10,10,13,13,13], a) " over-attached 'else' visible
799 let a = map(range(16,27), 'foldclosed(v:val)')
800 call assert_equal(unattached_results, a) " unattached cascade folds separately
801
802 set foldlevel=2
803
804 syn foldlevel start
805 syn sync fromstart
806 let a = map(range(11,14), 'foldclosed(v:val)')
807 call assert_equal([11,11,11,-1], a) " over-attached 'else' hidden
808
809 syn foldlevel minimum
810 syn sync fromstart
811 let a = map(range(11,14), 'foldclosed(v:val)')
812 call assert_equal([11,11,-1,-1], a) " over-attached 'else' visible
813
814 quit!
815endfunc
816
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200817func Test_search_syntax_skip()
818 new
819 let lines =<< trim END
820
821 /* This is VIM */
822 Another Text for VIM
823 let a = "VIM"
824 END
825 call setline(1, lines)
826 syntax on
827 syntax match Comment "^/\*.*\*/"
828 syntax match String '".*"'
829
830 " Skip argument using string evaluation.
831 1
832 call search('VIM', 'w', '', 0, 'synIDattr(synID(line("."), col("."), 1), "name") =~? "comment"')
833 call assert_equal('Another Text for VIM', getline('.'))
834 1
835 call search('VIM', 'w', '', 0, 'synIDattr(synID(line("."), col("."), 1), "name") !~? "string"')
836 call assert_equal(' let a = "VIM"', getline('.'))
837
838 " Skip argument using Lambda.
839 1
840 call search('VIM', 'w', '', 0, { -> synIDattr(synID(line("."), col("."), 1), "name") =~? "comment"})
841 call assert_equal('Another Text for VIM', getline('.'))
842
843 1
844 call search('VIM', 'w', '', 0, { -> synIDattr(synID(line("."), col("."), 1), "name") !~? "string"})
845 call assert_equal(' let a = "VIM"', getline('.'))
846
847 " Skip argument using funcref.
848 func InComment()
849 return synIDattr(synID(line("."), col("."), 1), "name") =~? "comment"
850 endfunc
851 func InString()
852 return synIDattr(synID(line("."), col("."), 1), "name") !~? "string"
853 endfunc
854 1
855 call search('VIM', 'w', '', 0, function('InComment'))
856 call assert_equal('Another Text for VIM', getline('.'))
857
858 1
859 call search('VIM', 'w', '', 0, function('InString'))
860 call assert_equal(' let a = "VIM"', getline('.'))
861
862 delfunc InComment
863 delfunc InString
864 bwipe!
865endfunc
866
Bram Moolenaarb46f57e2020-11-29 14:11:41 +0100867func Test_syn_contained_transparent()
868 " Comments starting with "Regression:" show the result when the highlighting
869 " span of the containing item is assigned to the contained region.
870 syntax on
871
872 let l:case = "Transparent region contained in region"
873 new
874 syntax region X start=/\[/ end=/\]/ contained transparent
875 syntax region Y start=/(/ end=/)/ contains=X
876
877 call setline(1, "==(--[~~]--)==")
878 let l:expected = " YYYYYYYYYY "
879 eval AssertHighlightGroups(1, 1, l:expected, 1, l:case)
880 syntax clear Y X
881 bw!
882
883 let l:case = "Transparent region extends region"
884 new
885 syntax region X start=/\[/ end=/\]/ contained transparent
886 syntax region Y start=/(/ end=/)/ end=/e/ contains=X
887
888 call setline(1, "==(--[~~e~~]--)==")
889 let l:expected = " YYYYYYYYYYYYY "
890 " Regression: " YYYYYYY YYY "
891 eval AssertHighlightGroups(1, 1, l:expected, 1, l:case)
892 syntax clear Y X
893 bw!
894
895 let l:case = "Nested transparent regions extend region"
896 new
897 syntax region X start=/\[/ end=/\]/ contained transparent
898 syntax region Y start=/(/ end=/)/ end=/e/ contains=X
899
900 call setline(1, "==(--[~~e~~[~~e~~]~~e~~]--)==")
901 let l:expected = " YYYYYYYYYYYYYYYYYYYYYYYYY "
902 " Regression: " YYYYYYY YYYYYYYYY "
903 eval AssertHighlightGroups(1, 1, l:expected, 1, l:case)
904 syntax clear Y X
905 bw!
906
907 let l:case = "Transparent region contained in match"
908 new
909 syntax region X start=/\[/ end=/\]/ contained transparent
910 syntax match Y /(.\{-})/ contains=X
911
912 call setline(1, "==(--[~~]--)==")
913 let l:expected = " YYYYYYYYYY "
914 eval AssertHighlightGroups(1, 1, l:expected, 1, l:case)
915 syntax clear Y X
916 bw!
917
918 let l:case = "Transparent region extends match"
919 new
920 syntax region X start=/\[/ end=/\]/ contained transparent
921 syntax match Y /(.\{-}[e)]/ contains=X
922
923 call setline(1, "==(--[~~e~~]--)==")
924 let l:expected = " YYYYYYYYYY "
925 " Regression: " YYYYYYY "
926 eval AssertHighlightGroups(1, 1, l:expected, 1, l:case)
927 syntax clear Y X
928 bw!
929
930 let l:case = "Nested transparent regions extend match"
931 new
932 syntax region X start=/\[/ end=/\]/ contained transparent
933 syntax match Y /(.\{-}[e)]/ contains=X
934
935 call setline(1, "==(--[~~e~~[~~e~~]~~e~~]--)==")
936 let l:expected = " YYYYYYYYYYYYYYYYYYYYYY "
937 " Regression: " YYYYYYY YYYYYY "
938 eval AssertHighlightGroups(1, 1, l:expected, 1, l:case)
939 syntax clear Y X
940 bw!
941endfunc
942
Bram Moolenaar2e240bd2021-04-14 11:15:08 +0200943func Test_syn_include_contains_TOP()
944 let l:case = "TOP in included syntax means its group list name"
945 new
946 syntax include @INCLUDED syntax/c.vim
947 syntax region FencedCodeBlockC start=/```c/ end=/```/ contains=@INCLUDED
948
949 call setline(1, ['```c', '#if 0', 'int', '#else', 'int', '#endif', '```' ])
950 let l:expected = ["cCppOutIf2"]
951 eval AssertHighlightGroups(3, 1, l:expected, 1)
952 " cCppOutElse has contains=TOP
953 let l:expected = ["cType"]
954 eval AssertHighlightGroups(5, 1, l:expected, 1, l:case)
955 syntax clear
956 bw!
957endfunc
958
zeertzjqca7e86c2022-04-16 16:49:24 +0100959" This was using freed memory
960func Test_WinEnter_synstack_synID()
961 autocmd WinEnter * call synstack(line("."), col("."))
962 autocmd WinEnter * call synID(line('.'), col('.') - 1, 1)
963 call setline(1, 'aaaaa')
964 normal! $
965 new
966 close
967
968 au! WinEnter
969 bw!
970endfunc
971
Bram Moolenaar2e240bd2021-04-14 11:15:08 +0200972
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100973" vim: shiftwidth=2 sts=2 expandtab