blob: 09b986f3396477d3912c577baa4a1c89feec9ae6 [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
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200174func Test_conceal_resize_term()
zeertzjq20691912022-01-23 12:42:55 +0000175 CheckScreendump
176
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200177 let code =<< trim [CODE]
178 call setline(1, '`one` `two` `three` `four` `five`, the backticks should be concealed')
179 setl cocu=n cole=3
180 syn region CommentCodeSpan matchgroup=Comment start=/`/ end=/`/ concealends
181 normal fb
182 [CODE]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100183 call writefile(code, 'XTest_conceal_resize', 'D')
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200184 let buf = RunVimInTerminal('-S XTest_conceal_resize', {'rows': 6})
185 call VerifyScreenDump(buf, 'Test_conceal_resize_01', {})
186
187 call win_execute(buf->win_findbuf()[0], 'wincmd +')
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200188 call VerifyScreenDump(buf, 'Test_conceal_resize_02', {})
189
190 " clean up
191 call StopVimInTerminal(buf)
Bram Moolenaar5babc6e2019-09-14 21:55:51 +0200192endfunc
Bram Moolenaar213ed002019-12-11 20:12:26 +0100193
Bram Moolenaar75008662022-10-04 22:40:56 +0100194func Test_conceal_linebreak()
195 CheckScreendump
196
197 let code =<< trim [CODE]
198 vim9script
199 &wrap = true
200 &conceallevel = 2
201 &concealcursor = 'nc'
202 &linebreak = true
203 &showbreak = '+ '
204 var line: string = 'a`a`a`a`'
205 .. 'a'->repeat(&columns - 15)
206 .. ' b`b`'
207 .. 'b'->repeat(&columns - 10)
208 .. ' cccccc'
209 ['x'->repeat(&columns), '', line]->setline(1)
210 syntax region CodeSpan matchgroup=Delimiter start=/\z(`\+\)/ end=/\z1/ concealends
211 [CODE]
212 call writefile(code, 'XTest_conceal_linebreak', 'D')
213 let buf = RunVimInTerminal('-S XTest_conceal_linebreak', {'rows': 8})
214 call VerifyScreenDump(buf, 'Test_conceal_linebreak_1', {})
215
216 " clean up
217 call StopVimInTerminal(buf)
218endfunc
219
Bram Moolenaar213ed002019-12-11 20:12:26 +0100220" Tests for correct display (cursor column position) with +conceal and
221" tabulators. Need to run this test in a separate Vim instance. Otherwise the
222" screen is not updated (lazy redraw) and the cursor position is wrong.
223func Test_conceal_cursor_pos()
224 let code =<< trim [CODE]
225 :let l = ['start:', '.concealed. text', "|concealed|\ttext"]
226 :let l += ['', "\t.concealed.\ttext", "\t|concealed|\ttext", '']
227 :let l += [".a.\t.b.\t.c.\t.d.", "|a|\t|b|\t|c|\t|d|"]
228 :call append(0, l)
229 :call cursor(1, 1)
230 :" Conceal settings.
231 :set conceallevel=2
232 :set concealcursor=nc
233 :syntax match test /|/ conceal
234 :" Save current cursor position. Only works in <expr> mode, can't be used
235 :" with :normal because it moves the cursor to the command line. Thanks
236 :" to ZyX <zyx.vim@gmail.com> for the idea to use an <expr> mapping.
237 :let curpos = []
238 :nnoremap <expr> GG ":let curpos += ['".screenrow().":".screencol()."']\n"
239 :normal ztj
240 GGk
241 :" We should end up in the same column when running these commands on the
242 :" two lines.
243 :normal ft
244 GGk
245 :normal $
246 GGk
247 :normal 0j
248 GGk
249 :normal ft
250 GGk
251 :normal $
252 GGk
253 :normal 0j0j
254 GGk
255 :" Same for next test block.
256 :normal ft
257 GGk
258 :normal $
259 GGk
260 :normal 0j
261 GGk
262 :normal ft
263 GGk
264 :normal $
265 GGk
266 :normal 0j0j
267 GGk
268 :" And check W with multiple tabs and conceals in a line.
269 :normal W
270 GGk
271 :normal W
272 GGk
273 :normal W
274 GGk
275 :normal $
276 GGk
277 :normal 0j
278 GGk
279 :normal W
280 GGk
281 :normal W
282 GGk
283 :normal W
284 GGk
285 :normal $
286 GGk
287 :set lbr
288 :normal $
289 GGk
290 :set list listchars=tab:>-
291 :normal 0
292 GGk
293 :normal W
294 GGk
295 :normal W
296 GGk
297 :normal W
298 GGk
299 :normal $
300 GGk
301 :call writefile(curpos, 'Xconceal_curpos.out')
302 :q!
303
304 [CODE]
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100305 call writefile(code, 'XTest_conceal_curpos', 'D')
Bram Moolenaar213ed002019-12-11 20:12:26 +0100306
307 if RunVim([], [], '-s XTest_conceal_curpos')
308 call assert_equal([
309 \ '2:1', '2:17', '2:20', '3:1', '3:17', '3:20', '5:8', '5:25',
310 \ '5:28', '6:8', '6:25', '6:28', '8:1', '8:9', '8:17', '8:25',
311 \ '8:27', '9:1', '9:9', '9:17', '9:25', '9:26', '9:26', '9:1',
312 \ '9:9', '9:17', '9:25', '9:26'], readfile('Xconceal_curpos.out'))
313 endif
314
315 call delete('Xconceal_curpos.out')
Bram Moolenaar213ed002019-12-11 20:12:26 +0100316endfunc
317
Bram Moolenaarfc838d62020-06-25 22:23:48 +0200318func Test_conceal_eol()
319 new!
320 setlocal concealcursor=n conceallevel=1
321 call setline(1, ["x", ""])
322 call matchaddpos('Conceal', [[2, 1, 1]], 2, -1, {'conceal': 1})
323 redraw!
324
325 call assert_notequal(screenchar(1, 1), screenchar(2, 2))
326 call assert_equal(screenattr(1, 1), screenattr(1, 2))
327 call assert_equal(screenattr(1, 2), screenattr(2, 2))
328 call assert_equal(screenattr(2, 1), screenattr(2, 2))
329
330 set list
331 redraw!
332
333 call assert_equal(screenattr(1, 1), screenattr(2, 2))
334 call assert_notequal(screenattr(1, 1), screenattr(1, 2))
335 call assert_notequal(screenattr(1, 2), screenattr(2, 1))
336
337 set nolist
338endfunc
339
Bram Moolenaarb9081882022-07-09 04:56:24 +0100340func Test_conceal_mouse_click()
341 enew!
342 set mouse=a
343 setlocal conceallevel=2 concealcursor=nc
344 syn match Concealed "this" conceal
345 hi link Concealed Search
346 call setline(1, 'conceal this click here')
347 redraw
348 call assert_equal(['conceal click here '], ScreenLines(1, 20))
349
350 " click on 'h' of "here" puts cursor there
351 call test_setmouse(1, 16)
352 call feedkeys("\<LeftMouse>", "tx")
353 call assert_equal([0, 1, 20, 0, 20], getcurpos())
zeertzjqe500ae82023-08-17 22:35:26 +0200354 " click on 'e' of "here" puts cursor there
355 call test_setmouse(1, 19)
356 call feedkeys("\<LeftMouse>", "tx")
357 call assert_equal([0, 1, 23, 0, 23], getcurpos())
358 " click after end of line puts cursor on 'e' without 'virtualedit'
359 call test_setmouse(1, 20)
360 call feedkeys("\<LeftMouse>", "tx")
361 call assert_equal([0, 1, 23, 0, 24], getcurpos())
362 call test_setmouse(1, 21)
363 call feedkeys("\<LeftMouse>", "tx")
364 call assert_equal([0, 1, 23, 0, 25], getcurpos())
365 call test_setmouse(1, 22)
366 call feedkeys("\<LeftMouse>", "tx")
367 call assert_equal([0, 1, 23, 0, 26], getcurpos())
368 call test_setmouse(1, 31)
369 call feedkeys("\<LeftMouse>", "tx")
370 call assert_equal([0, 1, 23, 0, 35], getcurpos())
371 call test_setmouse(1, 32)
372 call feedkeys("\<LeftMouse>", "tx")
373 call assert_equal([0, 1, 23, 0, 36], getcurpos())
374
375 set virtualedit=all
376 " click on 'h' of "here" puts cursor there
377 call test_setmouse(1, 16)
378 call feedkeys("\<LeftMouse>", "tx")
379 call assert_equal([0, 1, 20, 0, 20], getcurpos())
380 " click on 'e' of "here" puts cursor there
381 call test_setmouse(1, 19)
382 call feedkeys("\<LeftMouse>", "tx")
383 call assert_equal([0, 1, 23, 0, 23], getcurpos())
384 " click after end of line puts cursor there without 'virtualedit'
385 call test_setmouse(1, 20)
386 call feedkeys("\<LeftMouse>", "tx")
387 call assert_equal([0, 1, 24, 0, 24], getcurpos())
388 call test_setmouse(1, 21)
389 call feedkeys("\<LeftMouse>", "tx")
390 call assert_equal([0, 1, 24, 1, 25], getcurpos())
391 call test_setmouse(1, 22)
392 call feedkeys("\<LeftMouse>", "tx")
393 call assert_equal([0, 1, 24, 2, 26], getcurpos())
394 call test_setmouse(1, 31)
395 call feedkeys("\<LeftMouse>", "tx")
396 call assert_equal([0, 1, 24, 11, 35], getcurpos())
397 call test_setmouse(1, 32)
398 call feedkeys("\<LeftMouse>", "tx")
399 call assert_equal([0, 1, 24, 12, 36], getcurpos())
Bram Moolenaarb9081882022-07-09 04:56:24 +0100400
401 bwipe!
zeertzjqe500ae82023-08-17 22:35:26 +0200402 set mouse& virtualedit&
Bram Moolenaarb9081882022-07-09 04:56:24 +0100403endfunc
404
Bram Moolenaar213ed002019-12-11 20:12:26 +0100405" vim: shiftwidth=2 sts=2 expandtab