blob: 16953fc32c7b224ddfe057ac763fb264b4e7ddcd [file] [log] [blame]
Bram Moolenaar429ab172019-01-11 15:54:45 +01001" Tests for 'conceal'.
Bram Moolenaar429ab172019-01-11 15:54:45 +01002
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02003source check.vim
4CheckFeature conceal
Bram Moolenaar429ab172019-01-11 15:54:45 +01005
6source screendump.vim
Bram Moolenaarb9081882022-07-09 04:56:24 +01007source view_util.vim
Bram Moolenaar429ab172019-01-11 15:54:45 +01008
9func Test_conceal_two_windows()
zeertzjq20691912022-01-23 12:42:55 +000010 CheckScreendump
11
Bram Moolenaarc79745a2019-05-20 22:12:34 +020012 let code =<< trim [CODE]
13 let lines = ["one one one one one", "two |hidden| here", "three |hidden| three"]
14 call setline(1, lines)
15 syntax match test /|hidden|/ conceal
16 set conceallevel=2
17 set concealcursor=
18 exe "normal /here\r"
19 new
20 call setline(1, lines)
21 call setline(4, "Second window")
22 syntax match test /|hidden|/ conceal
23 set conceallevel=2
24 set concealcursor=nc
25 exe "normal /here\r"
26 [CODE]
27
Bram Moolenaar45bbaef2022-09-08 16:39:22 +010028 call writefile(code, 'XTest_conceal', 'D')
Bram Moolenaar429ab172019-01-11 15:54:45 +010029 " Check that cursor line is concealed
30 let buf = RunVimInTerminal('-S XTest_conceal', {})
31 call VerifyScreenDump(buf, 'Test_conceal_two_windows_01', {})
32
33 " Check that with concealed text vertical cursor movement is correct.
34 call term_sendkeys(buf, "k")
35 call VerifyScreenDump(buf, 'Test_conceal_two_windows_02', {})
36
37 " Check that with cursor line is not concealed
38 call term_sendkeys(buf, "j")
39 call term_sendkeys(buf, ":set concealcursor=\r")
40 call VerifyScreenDump(buf, 'Test_conceal_two_windows_03', {})
41
42 " Check that with cursor line is not concealed when moving cursor down
43 call term_sendkeys(buf, "j")
44 call VerifyScreenDump(buf, 'Test_conceal_two_windows_04', {})
45
46 " Check that with cursor line is not concealed when switching windows
47 call term_sendkeys(buf, "\<C-W>\<C-W>")
48 call VerifyScreenDump(buf, 'Test_conceal_two_windows_05', {})
49
50 " Check that with cursor line is only concealed in Normal mode
51 call term_sendkeys(buf, ":set concealcursor=n\r")
52 call VerifyScreenDump(buf, 'Test_conceal_two_windows_06n', {})
53 call term_sendkeys(buf, "a")
54 call VerifyScreenDump(buf, 'Test_conceal_two_windows_06i', {})
55 call term_sendkeys(buf, "\<Esc>/e")
56 call VerifyScreenDump(buf, 'Test_conceal_two_windows_06c', {})
57 call term_sendkeys(buf, "\<Esc>v")
58 call VerifyScreenDump(buf, 'Test_conceal_two_windows_06v', {})
59 call term_sendkeys(buf, "\<Esc>")
60
61 " Check that with cursor line is only concealed in Insert mode
62 call term_sendkeys(buf, ":set concealcursor=i\r")
63 call VerifyScreenDump(buf, 'Test_conceal_two_windows_07n', {})
Bram Moolenaar644b49f2021-09-16 22:32:15 +020064 call term_sendkeys(buf, "14|a")
Bram Moolenaar429ab172019-01-11 15:54:45 +010065 call VerifyScreenDump(buf, 'Test_conceal_two_windows_07i', {})
Bram Moolenaar644b49f2021-09-16 22:32:15 +020066 call term_sendkeys(buf, "\<Esc>")
67 call VerifyScreenDump(buf, 'Test_conceal_two_windows_07in', {})
68 call term_sendkeys(buf, "/e")
Bram Moolenaar429ab172019-01-11 15:54:45 +010069 call VerifyScreenDump(buf, 'Test_conceal_two_windows_07c', {})
70 call term_sendkeys(buf, "\<Esc>v")
71 call VerifyScreenDump(buf, 'Test_conceal_two_windows_07v', {})
72 call term_sendkeys(buf, "\<Esc>")
73
74 " Check that with cursor line is only concealed in Command mode
75 call term_sendkeys(buf, ":set concealcursor=c\r")
76 call VerifyScreenDump(buf, 'Test_conceal_two_windows_08n', {})
77 call term_sendkeys(buf, "a")
78 call VerifyScreenDump(buf, 'Test_conceal_two_windows_08i', {})
79 call term_sendkeys(buf, "\<Esc>/e")
80 call VerifyScreenDump(buf, 'Test_conceal_two_windows_08c', {})
81 call term_sendkeys(buf, "\<Esc>v")
82 call VerifyScreenDump(buf, 'Test_conceal_two_windows_08v', {})
83 call term_sendkeys(buf, "\<Esc>")
84
85 " Check that with cursor line is only concealed in Visual mode
86 call term_sendkeys(buf, ":set concealcursor=v\r")
87 call VerifyScreenDump(buf, 'Test_conceal_two_windows_09n', {})
88 call term_sendkeys(buf, "a")
89 call VerifyScreenDump(buf, 'Test_conceal_two_windows_09i', {})
90 call term_sendkeys(buf, "\<Esc>/e")
91 call VerifyScreenDump(buf, 'Test_conceal_two_windows_09c', {})
92 call term_sendkeys(buf, "\<Esc>v")
93 call VerifyScreenDump(buf, 'Test_conceal_two_windows_09v', {})
94 call term_sendkeys(buf, "\<Esc>")
95
Bram Moolenaar465e8b52019-01-11 20:42:28 +010096 " Check moving the cursor while in insert mode.
97 call term_sendkeys(buf, ":set concealcursor=\r")
98 call term_sendkeys(buf, "a")
99 call VerifyScreenDump(buf, 'Test_conceal_two_windows_10', {})
100 call term_sendkeys(buf, "\<Down>")
101 call VerifyScreenDump(buf, 'Test_conceal_two_windows_11', {})
102 call term_sendkeys(buf, "\<Esc>")
103
104 " Check the "o" command
105 call VerifyScreenDump(buf, 'Test_conceal_two_windows_12', {})
106 call term_sendkeys(buf, "o")
107 call VerifyScreenDump(buf, 'Test_conceal_two_windows_13', {})
108 call term_sendkeys(buf, "\<Esc>")
109
Bram Moolenaar429ab172019-01-11 15:54:45 +0100110 " clean up
111 call StopVimInTerminal(buf)
Bram Moolenaar429ab172019-01-11 15:54:45 +0100112endfunc
Bram Moolenaarbbee8d52019-01-14 21:51:40 +0100113
114func Test_conceal_with_cursorline()
zeertzjq20691912022-01-23 12:42:55 +0000115 CheckScreendump
116
Bram Moolenaarbbee8d52019-01-14 21:51:40 +0100117 " Opens a help window, where 'conceal' is set, switches to the other window
118 " where 'cursorline' needs to be updated when the cursor moves.
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200119 let code =<< trim [CODE]
120 set cursorline
121 normal othis is a test
122 new
123 call setline(1, ["one", "two", "three", "four", "five"])
124 set ft=help
125 normal M
126 [CODE]
127
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100128 call writefile(code, 'XTest_conceal_cul', 'D')
Bram Moolenaarbbee8d52019-01-14 21:51:40 +0100129 let buf = RunVimInTerminal('-S XTest_conceal_cul', {})
130 call VerifyScreenDump(buf, 'Test_conceal_cul_01', {})
131
132 call term_sendkeys(buf, ":wincmd w\r")
133 call VerifyScreenDump(buf, 'Test_conceal_cul_02', {})
134
135 call term_sendkeys(buf, "k")
136 call VerifyScreenDump(buf, 'Test_conceal_cul_03', {})
137
138 " clean up
139 call StopVimInTerminal(buf)
Bram Moolenaarbbee8d52019-01-14 21:51:40 +0100140endfunc
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200141
Alexey Radkovaaa16b02023-01-04 11:15:30 +0000142func Test_conceal_with_cursorcolumn()
143 CheckScreendump
144
145 " Check that cursorcolumn and colorcolumn don't get broken in presence of
146 " wrapped lines containing concealed text
147 let code =<< trim [CODE]
148 let lines = ["one one one |hidden| one one one one one one one one",
149 \ "two two two two |hidden| here two two",
150 \ "three |hidden| three three three three three three three three"]
151 call setline(1, lines)
152 set wrap linebreak
153 set showbreak=\ >>>\
154 syntax match test /|hidden|/ conceal
155 set conceallevel=2
156 set concealcursor=
157 exe "normal /here\r"
158 set cursorcolumn
159 set colorcolumn=50
160 [CODE]
161
162 call writefile(code, 'XTest_conceal_cuc', 'D')
163 let buf = RunVimInTerminal('-S XTest_conceal_cuc', {'rows': 10, 'cols': 40})
164 call VerifyScreenDump(buf, 'Test_conceal_cuc_01', {})
165
166 " move cursor to the end of line (the cursor jumps to the next screen line)
167 call term_sendkeys(buf, "$")
168 call VerifyScreenDump(buf, 'Test_conceal_cuc_02', {})
169
170 " clean up
171 call StopVimInTerminal(buf)
172endfunc
173
zeertzjq21b0a3d2024-03-13 20:06:34 +0100174" Check that 'cursorline' and 'wincolor' apply to the whole line in presence
175" of wrapped lines containing concealed text.
176func Test_conceal_wrapped_cursorline_wincolor()
177 CheckScreendump
178
179 let code =<< trim [CODE]
180 call setline(1, 'one one one |hidden| one one one one one one one one')
181 syntax match test /|hidden|/ conceal
182 set conceallevel=2 concealcursor=n cursorline
183 normal! g$
184 [CODE]
185
186 call writefile(code, 'XTest_conceal_cul_wcr', 'D')
187 let buf = RunVimInTerminal('-S XTest_conceal_cul_wcr', {'rows': 4, 'cols': 40})
188 call VerifyScreenDump(buf, 'Test_conceal_cul_wcr_01', {})
189
190 call term_sendkeys(buf, ":set wincolor=ErrorMsg\n")
191 call VerifyScreenDump(buf, 'Test_conceal_cul_wcr_02', {})
192
193 call term_sendkeys(buf, ":set nocursorline\n")
194 call VerifyScreenDump(buf, 'Test_conceal_cul_wcr_03', {})
195
196 " clean up
197 call StopVimInTerminal(buf)
198endfunc
199
200" Same as Test_conceal_wrapped_cursorline_wincolor(), but with 'rightleft'.
201func Test_conceal_wrapped_cursorline_wincolor_rightleft()
202 CheckScreendump
203
204 let code =<< trim [CODE]
205 call setline(1, 'one one one |hidden| one one one one one one one one')
206 syntax match test /|hidden|/ conceal
207 set conceallevel=2 concealcursor=n cursorline rightleft
208 normal! g$
209 [CODE]
210
211 call writefile(code, 'XTest_conceal_cul_wcr_rl', 'D')
212 let buf = RunVimInTerminal('-S XTest_conceal_cul_wcr_rl', {'rows': 4, 'cols': 40})
213 call VerifyScreenDump(buf, 'Test_conceal_cul_wcr_rl_01', {})
214
215 call term_sendkeys(buf, ":set wincolor=ErrorMsg\n")
216 call VerifyScreenDump(buf, 'Test_conceal_cul_wcr_rl_02', {})
217
218 call term_sendkeys(buf, ":set nocursorline\n")
219 call VerifyScreenDump(buf, 'Test_conceal_cul_wcr_rl_03', {})
220
221 " clean up
222 call StopVimInTerminal(buf)
223endfunc
224
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200225func Test_conceal_resize_term()
zeertzjq20691912022-01-23 12:42:55 +0000226 CheckScreendump
227
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200228 let code =<< trim [CODE]
229 call setline(1, '`one` `two` `three` `four` `five`, the backticks should be concealed')
230 setl cocu=n cole=3
231 syn region CommentCodeSpan matchgroup=Comment start=/`/ end=/`/ concealends
232 normal fb
233 [CODE]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100234 call writefile(code, 'XTest_conceal_resize', 'D')
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200235 let buf = RunVimInTerminal('-S XTest_conceal_resize', {'rows': 6})
236 call VerifyScreenDump(buf, 'Test_conceal_resize_01', {})
237
238 call win_execute(buf->win_findbuf()[0], 'wincmd +')
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200239 call VerifyScreenDump(buf, 'Test_conceal_resize_02', {})
240
241 " clean up
242 call StopVimInTerminal(buf)
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200243endfunc
Bram Moolenaar213ed002019-12-11 20:12:26 +0100244
Bram Moolenaar75008662022-10-04 22:40:56 +0100245func Test_conceal_linebreak()
246 CheckScreendump
247
248 let code =<< trim [CODE]
249 vim9script
250 &wrap = true
251 &conceallevel = 2
252 &concealcursor = 'nc'
253 &linebreak = true
254 &showbreak = '+ '
255 var line: string = 'a`a`a`a`'
256 .. 'a'->repeat(&columns - 15)
257 .. ' b`b`'
258 .. 'b'->repeat(&columns - 10)
259 .. ' cccccc'
260 ['x'->repeat(&columns), '', line]->setline(1)
261 syntax region CodeSpan matchgroup=Delimiter start=/\z(`\+\)/ end=/\z1/ concealends
262 [CODE]
263 call writefile(code, 'XTest_conceal_linebreak', 'D')
264 let buf = RunVimInTerminal('-S XTest_conceal_linebreak', {'rows': 8})
265 call VerifyScreenDump(buf, 'Test_conceal_linebreak_1', {})
266
267 " clean up
268 call StopVimInTerminal(buf)
269endfunc
270
Bram Moolenaar213ed002019-12-11 20:12:26 +0100271" Tests for correct display (cursor column position) with +conceal and
272" tabulators. Need to run this test in a separate Vim instance. Otherwise the
273" screen is not updated (lazy redraw) and the cursor position is wrong.
274func Test_conceal_cursor_pos()
275 let code =<< trim [CODE]
276 :let l = ['start:', '.concealed. text', "|concealed|\ttext"]
277 :let l += ['', "\t.concealed.\ttext", "\t|concealed|\ttext", '']
278 :let l += [".a.\t.b.\t.c.\t.d.", "|a|\t|b|\t|c|\t|d|"]
279 :call append(0, l)
280 :call cursor(1, 1)
281 :" Conceal settings.
282 :set conceallevel=2
283 :set concealcursor=nc
284 :syntax match test /|/ conceal
285 :" Save current cursor position. Only works in <expr> mode, can't be used
286 :" with :normal because it moves the cursor to the command line. Thanks
287 :" to ZyX <zyx.vim@gmail.com> for the idea to use an <expr> mapping.
288 :let curpos = []
289 :nnoremap <expr> GG ":let curpos += ['".screenrow().":".screencol()."']\n"
290 :normal ztj
291 GGk
292 :" We should end up in the same column when running these commands on the
293 :" two lines.
294 :normal ft
295 GGk
296 :normal $
297 GGk
298 :normal 0j
299 GGk
300 :normal ft
301 GGk
302 :normal $
303 GGk
304 :normal 0j0j
305 GGk
306 :" Same for next test block.
307 :normal ft
308 GGk
309 :normal $
310 GGk
311 :normal 0j
312 GGk
313 :normal ft
314 GGk
315 :normal $
316 GGk
317 :normal 0j0j
318 GGk
319 :" And check W with multiple tabs and conceals in a line.
320 :normal W
321 GGk
322 :normal W
323 GGk
324 :normal W
325 GGk
326 :normal $
327 GGk
328 :normal 0j
329 GGk
330 :normal W
331 GGk
332 :normal W
333 GGk
334 :normal W
335 GGk
336 :normal $
337 GGk
338 :set lbr
339 :normal $
340 GGk
341 :set list listchars=tab:>-
342 :normal 0
343 GGk
344 :normal W
345 GGk
346 :normal W
347 GGk
348 :normal W
349 GGk
350 :normal $
351 GGk
352 :call writefile(curpos, 'Xconceal_curpos.out')
353 :q!
354
355 [CODE]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100356 call writefile(code, 'XTest_conceal_curpos', 'D')
Bram Moolenaar213ed002019-12-11 20:12:26 +0100357
358 if RunVim([], [], '-s XTest_conceal_curpos')
359 call assert_equal([
360 \ '2:1', '2:17', '2:20', '3:1', '3:17', '3:20', '5:8', '5:25',
361 \ '5:28', '6:8', '6:25', '6:28', '8:1', '8:9', '8:17', '8:25',
362 \ '8:27', '9:1', '9:9', '9:17', '9:25', '9:26', '9:26', '9:1',
363 \ '9:9', '9:17', '9:25', '9:26'], readfile('Xconceal_curpos.out'))
364 endif
365
366 call delete('Xconceal_curpos.out')
Bram Moolenaar213ed002019-12-11 20:12:26 +0100367endfunc
368
Bram Moolenaarfc838d62020-06-25 22:23:48 +0200369func Test_conceal_eol()
370 new!
371 setlocal concealcursor=n conceallevel=1
372 call setline(1, ["x", ""])
373 call matchaddpos('Conceal', [[2, 1, 1]], 2, -1, {'conceal': 1})
374 redraw!
375
376 call assert_notequal(screenchar(1, 1), screenchar(2, 2))
377 call assert_equal(screenattr(1, 1), screenattr(1, 2))
378 call assert_equal(screenattr(1, 2), screenattr(2, 2))
379 call assert_equal(screenattr(2, 1), screenattr(2, 2))
380
381 set list
382 redraw!
383
384 call assert_equal(screenattr(1, 1), screenattr(2, 2))
385 call assert_notequal(screenattr(1, 1), screenattr(1, 2))
386 call assert_notequal(screenattr(1, 2), screenattr(2, 1))
387
388 set nolist
389endfunc
390
Bram Moolenaarb9081882022-07-09 04:56:24 +0100391func Test_conceal_mouse_click()
392 enew!
393 set mouse=a
394 setlocal conceallevel=2 concealcursor=nc
395 syn match Concealed "this" conceal
396 hi link Concealed Search
397 call setline(1, 'conceal this click here')
398 redraw
399 call assert_equal(['conceal click here '], ScreenLines(1, 20))
400
zeertzjqfc305842023-08-19 13:27:03 +0200401 " click on the space between "this" and "click" puts cursor there
402 call test_setmouse(1, 9)
403 call feedkeys("\<LeftMouse>", "tx")
404 call assert_equal([0, 1, 13, 0, 13], getcurpos())
Bram Moolenaarb9081882022-07-09 04:56:24 +0100405 " click on 'h' of "here" puts cursor there
406 call test_setmouse(1, 16)
407 call feedkeys("\<LeftMouse>", "tx")
408 call assert_equal([0, 1, 20, 0, 20], getcurpos())
zeertzjqe500ae82023-08-17 22:35:26 +0200409 " click on 'e' of "here" puts cursor there
410 call test_setmouse(1, 19)
411 call feedkeys("\<LeftMouse>", "tx")
412 call assert_equal([0, 1, 23, 0, 23], getcurpos())
413 " click after end of line puts cursor on 'e' without 'virtualedit'
414 call test_setmouse(1, 20)
415 call feedkeys("\<LeftMouse>", "tx")
416 call assert_equal([0, 1, 23, 0, 24], getcurpos())
417 call test_setmouse(1, 21)
418 call feedkeys("\<LeftMouse>", "tx")
419 call assert_equal([0, 1, 23, 0, 25], getcurpos())
420 call test_setmouse(1, 22)
421 call feedkeys("\<LeftMouse>", "tx")
422 call assert_equal([0, 1, 23, 0, 26], getcurpos())
423 call test_setmouse(1, 31)
424 call feedkeys("\<LeftMouse>", "tx")
425 call assert_equal([0, 1, 23, 0, 35], getcurpos())
426 call test_setmouse(1, 32)
427 call feedkeys("\<LeftMouse>", "tx")
428 call assert_equal([0, 1, 23, 0, 36], getcurpos())
429
430 set virtualedit=all
zeertzjqfc305842023-08-19 13:27:03 +0200431 redraw
432 " click on the space between "this" and "click" puts cursor there
433 call test_setmouse(1, 9)
434 call feedkeys("\<LeftMouse>", "tx")
435 call assert_equal([0, 1, 13, 0, 13], getcurpos())
zeertzjqe500ae82023-08-17 22:35:26 +0200436 " click on 'h' of "here" puts cursor there
437 call test_setmouse(1, 16)
438 call feedkeys("\<LeftMouse>", "tx")
439 call assert_equal([0, 1, 20, 0, 20], getcurpos())
440 " click on 'e' of "here" puts cursor there
441 call test_setmouse(1, 19)
442 call feedkeys("\<LeftMouse>", "tx")
443 call assert_equal([0, 1, 23, 0, 23], getcurpos())
444 " click after end of line puts cursor there without 'virtualedit'
445 call test_setmouse(1, 20)
446 call feedkeys("\<LeftMouse>", "tx")
447 call assert_equal([0, 1, 24, 0, 24], getcurpos())
448 call test_setmouse(1, 21)
449 call feedkeys("\<LeftMouse>", "tx")
450 call assert_equal([0, 1, 24, 1, 25], getcurpos())
451 call test_setmouse(1, 22)
452 call feedkeys("\<LeftMouse>", "tx")
453 call assert_equal([0, 1, 24, 2, 26], getcurpos())
454 call test_setmouse(1, 31)
455 call feedkeys("\<LeftMouse>", "tx")
456 call assert_equal([0, 1, 24, 11, 35], getcurpos())
457 call test_setmouse(1, 32)
458 call feedkeys("\<LeftMouse>", "tx")
459 call assert_equal([0, 1, 24, 12, 36], getcurpos())
Bram Moolenaarb9081882022-07-09 04:56:24 +0100460
461 bwipe!
zeertzjqe500ae82023-08-17 22:35:26 +0200462 set mouse& virtualedit&
Bram Moolenaarb9081882022-07-09 04:56:24 +0100463endfunc
464
Bram Moolenaar213ed002019-12-11 20:12:26 +0100465" vim: shiftwidth=2 sts=2 expandtab