1" Test for matchadd() and conceal feature
2
3source check.vim
4CheckFeature conceal
5
6if !has('gui_running') && has('unix')
7  set term=ansi
8endif
9
10source shared.vim
11source term_util.vim
12source view_util.vim
13
14func Test_simple_matchadd()
15  new
16
17  1put='# This is a Test'
18  "             1234567890123456
19  let expect = '# This is a Test'
20
21  call cursor(1, 1)
22  call matchadd('Conceal', '\%2l ')
23  redraw!
24  let lnum = 2
25  call assert_equal(expect, Screenline(lnum))
26  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
27  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
28  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
29  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
30  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
31  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
32
33  quit!
34endfunc
35
36func Test_simple_matchadd_and_conceal()
37  new
38  setlocal concealcursor=n conceallevel=1
39
40  1put='# This is a Test'
41  "             1234567890123456
42  let expect = '#XThisXisXaXTest'
43
44  call cursor(1, 1)
45  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'})
46  redraw!
47  let lnum = 2
48  call assert_equal(expect, Screenline(lnum))
49  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
50  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
51  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
52  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
53  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
54
55  quit!
56endfunc
57
58func Test_matchadd_and_conceallevel_3()
59  new
60
61  setlocal conceallevel=3
62  " set filetype and :syntax on to change screenattr()
63  setlocal filetype=conf
64  syntax on
65
66  1put='# This is a Test  $'
67  "             1234567890123
68  let expect = '#ThisisaTest$'
69
70  call cursor(1, 1)
71  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'})
72  redraw!
73  let lnum = 2
74  call assert_equal(expect, Screenline(lnum))
75  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2))
76  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 7))
77  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 10))
78  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 12))
79  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 13))
80  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 14))
81  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 16))
82
83  " more matchadd()
84  "             12345678901234
85  let expect = '#Thisisa Test$'
86
87  call matchadd('ErrorMsg', '\%2l Test', 20, -1, {'conceal': 'X'})
88  redraw!
89  call assert_equal(expect, Screenline(lnum))
90  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 2))
91  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 7))
92  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 10))
93  call assert_equal(screenattr(lnum, 10), screenattr(lnum, 13))
94  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 14))
95  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 16))
96  call assert_notequal(screenattr(lnum, 10), screenattr(lnum, 16))
97
98  syntax off
99  quit!
100endfunc
101
102func Test_default_conceal_char()
103  new
104  setlocal concealcursor=n conceallevel=1
105
106  1put='# This is a Test'
107  "             1234567890123456
108  let expect = '# This is a Test'
109
110  call cursor(1, 1)
111  call matchadd('Conceal', '\%2l ', 10, -1, {})
112  redraw!
113  let lnum = 2
114  call assert_equal(expect, Screenline(lnum))
115  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
116  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
117  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
118  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
119  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
120
121  "             1234567890123456
122  let expect = '#+This+is+a+Test'
123  let listchars_save = &listchars
124  set listchars=conceal:+
125  redraw!
126
127  call assert_equal(expect, Screenline(lnum))
128  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
129  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
130  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
131  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
132  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
133
134  let &listchars = listchars_save
135  quit!
136endfunc
137
138func Test_syn_and_match_conceal()
139  new
140  setlocal concealcursor=n conceallevel=1
141
142  1put='# This is a Test  '
143
144  let lnum = 2
145  call cursor(1, 1)
146
147  "             123456789012345678
148  let expect = '#ZThisZisZaZTestZZ'
149  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'Z'})
150  syntax match MyConceal /\%2l / conceal containedin=ALL
151  hi MyConceal ctermbg=4 ctermfg=2
152  redraw!
153
154  call assert_equal(expect, Screenline(lnum))
155  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
156  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
157  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
158  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
159  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
160
161  syntax clear MyConceal
162  syntax match MyConceal /\%2l / conceal containedin=ALL cchar=*
163  redraw!
164
165  call assert_equal(expect, Screenline(lnum))
166  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
167  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
168  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
169  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
170  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
171
172  "             123456789012345678
173  let expect = '#*This*is*a*Test**'
174  call clearmatches()
175  redraw!
176
177  call assert_equal(expect, Screenline(lnum))
178  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
179  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
180  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
181  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
182  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
183
184  "             123456789012345678
185  let expect = '#*ThisXis*a*Test**'
186  call matchadd('Conceal', '\%2l\%7c ', 10, -1, {'conceal': 'X'})
187  redraw!
188
189  call assert_equal(expect, Screenline(lnum))
190  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
191  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
192  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
193  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
194  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
195
196  "             123456789012345678
197  let expect = '#*ThisXis*a*Test**'
198  call matchadd('ErrorMsg', '\%2l Test', 20, -1)
199  redraw!
200
201  call assert_equal(expect, Screenline(lnum))
202  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
203  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
204  call assert_notequal(screenattr(lnum, 12), screenattr(lnum, 13))
205  call assert_equal(screenattr(lnum, 13), screenattr(lnum, 16))
206  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 17))
207  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 18))
208  call assert_notequal(screenattr(lnum, 18), screenattr(lnum, 19))
209
210  "             123456789012345678
211  let expect = '# ThisXis a Test'
212  syntax clear MyConceal
213  syntax match MyConceal /\%2l / conceal containedin=ALL
214  redraw!
215
216  call assert_equal(expect, Screenline(lnum))
217  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
218  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
219  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 12))
220  call assert_notequal(screenattr(lnum, 12), screenattr(lnum, 13))
221  call assert_equal(screenattr(lnum, 13), screenattr(lnum, 16))
222  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 17))
223  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 18))
224  call assert_notequal(screenattr(lnum, 18), screenattr(lnum, 19))
225
226  syntax off
227  quit!
228endfunc
229
230func Test_clearmatches()
231  new
232  setlocal concealcursor=n conceallevel=1
233
234  1put='# This is a Test'
235  "             1234567890123456
236  let expect = '# This is a Test'
237
238  call cursor(1, 1)
239  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'Z'})
240  let a = getmatches()
241  call clearmatches()
242  redraw!
243
244  let lnum = 2
245  call assert_equal(expect, Screenline(lnum))
246  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2))
247  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
248  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
249  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
250  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
251
252  " reset match using setmatches()
253  "             1234567890123456
254  let expect = '#ZThisZisZaZTest'
255  call setmatches(a)
256  redraw!
257
258  call assert_equal(expect, Screenline(lnum))
259  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
260  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
261  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
262  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
263  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
264  call assert_equal({'group': 'Conceal', 'pattern': '\%2l ', 'priority': 10, 'id': a[0].id, 'conceal': 'Z'}, a[0])
265
266  quit!
267endfunc
268
269func Test_using_matchaddpos()
270  new
271  setlocal concealcursor=n conceallevel=1
272  " set filetype and :syntax on to change screenattr()
273  setlocal filetype=conf
274  syntax on
275
276  1put='# This is a Test'
277  "             1234567890123456
278  let expect = '#Pis a Test'
279
280  call cursor(1, 1)
281  call matchaddpos('Conceal', [[2,2,6]], 10, -1, {'conceal': 'P'})
282  let a = getmatches()
283  redraw!
284
285  let lnum = 2
286  call assert_equal(expect, Screenline(lnum))
287  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 2))
288  call assert_notequal(screenattr(lnum, 2) , screenattr(lnum, 7))
289  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 7))
290  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 10))
291  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 12))
292  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 16))
293  call assert_equal(screenattr(lnum, 12), screenattr(lnum, 16))
294  call assert_equal({'group': 'Conceal', 'id': a[0].id, 'priority': 10, 'pos1': [2, 2, 6], 'conceal': 'P'}, a[0])
295
296  syntax off
297  quit!
298endfunc
299
300func Test_matchadd_repeat_conceal_with_syntax_off()
301  new
302
303  " To test targets in the same line string is replaced with conceal char
304  " correctly, repeat 'TARGET'
305  1put ='TARGET_TARGETTARGET'
306  call cursor(1, 1)
307  redraw
308  call assert_equal('TARGET_TARGETTARGET', Screenline(2))
309
310  setlocal conceallevel=2
311  call matchadd('Conceal', 'TARGET', 10, -1, {'conceal': 't'})
312
313  redraw
314  call assert_equal('t_tt', Screenline(2))
315
316  quit!
317endfunc
318
319func Test_matchadd_and_syn_conceal()
320  new
321  let cnt='Inductive bool : Type := | true : bool | false : bool.'
322  let expect = 'Inductive - : Type := | true : - | false : -.'
323  0put =cnt
324  " set filetype and :syntax on to change screenattr()
325  set cole=1 cocu=nv
326  hi link CheckedByCoq WarningMsg
327  syntax on
328  syntax keyword coqKwd bool conceal cchar=-
329  redraw!
330  call assert_equal(expect, Screenline(1))
331  call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
332  call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
333  call assert_equal(screenattr(1, 11) , screenattr(1, 32))
334  call matchadd('CheckedByCoq', '\%<2l\%>9c\%<16c')
335  redraw!
336  call assert_equal(expect, Screenline(1))
337  call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
338  call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
339  call assert_equal(screenattr(1, 11) , screenattr(1, 32))
340endfunc
341
342func Test_cursor_column_in_concealed_line_after_window_scroll()
343  CheckRunVimInTerminal
344
345  " Test for issue #5012 fix.
346  " For a concealed line with cursor, there should be no window's cursor
347  " position invalidation during win_update() after scrolling attempt that is
348  " not successful and no real topline change happens. The invalidation would
349  " cause a window's cursor position recalc outside of win_line() where it's
350  " not possible to take conceal into account.
351  let lines =<< trim END
352    3split
353    let m = matchadd('Conceal', '=')
354    setl conceallevel=2 concealcursor=nc
355    normal gg
356    "==expr==
357  END
358  call writefile(lines, 'Xcolesearch')
359  let buf = RunVimInTerminal('Xcolesearch', {})
360  call TermWait(buf, 50)
361
362  " Jump to something that is beyond the bottom of the window,
363  " so there's a scroll down.
364  call term_sendkeys(buf, ":so %\<CR>")
365  call TermWait(buf, 50)
366  call term_sendkeys(buf, "/expr\<CR>")
367  call TermWait(buf, 50)
368
369  " Are the concealed parts of the current line really hidden?
370  let cursor_row = term_scrape(buf, '.')->map({_, e -> e.chars})->join('')
371  call assert_equal('"expr', cursor_row)
372
373  " BugFix check: Is the window's cursor column properly updated for hidden
374  " parts of the current line?
375  call assert_equal(2, term_getcursor(buf)[1])
376
377  call StopVimInTerminal(buf)
378  call delete('Xcolesearch')
379endfunc
380
381func Test_cursor_column_in_concealed_line_after_leftcol_change()
382  CheckRunVimInTerminal
383
384  " Test for issue #5214 fix.
385  let lines =<< trim END
386    0put = 'ab' .. repeat('-', &columns) .. 'c'
387    call matchadd('Conceal', '-')
388    set nowrap ss=0 cole=3 cocu=n
389  END
390  call writefile(lines, 'Xcurs-columns')
391  let buf = RunVimInTerminal('-S Xcurs-columns', {})
392
393  " Go to the end of the line (3 columns beyond the end of the screen).
394  " Horizontal scroll would center the cursor in the screen line, but conceal
395  " makes it go to screen column 1.
396  call term_sendkeys(buf, "$")
397  call TermWait(buf)
398
399  " Are the concealed parts of the current line really hidden?
400  call WaitForAssert({-> assert_equal('c', term_getline(buf, '.'))})
401
402  " BugFix check: Is the window's cursor column properly updated for conceal?
403  call assert_equal(1, term_getcursor(buf)[1])
404
405  call StopVimInTerminal(buf)
406  call delete('Xcurs-columns')
407endfunc
408
409" vim: shiftwidth=2 sts=2 expandtab
410