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  "             1234567890123456
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, 2), screenattr(lnum, 7))
77  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
78  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
79  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 16))
80
81  " more matchadd()
82  "             1234567890123456
83  let expect = '#Thisisa Test'
84
85  call matchadd('ErrorMsg', '\%2l Test', 20, -1, {'conceal': 'X'})
86  redraw!
87  call assert_equal(expect, Screenline(lnum))
88  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 2))
89  call assert_equal(screenattr(lnum, 2) , screenattr(lnum, 7))
90  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 10))
91  call assert_equal(screenattr(lnum, 10), screenattr(lnum, 12))
92  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 16))
93  call assert_notequal(screenattr(lnum, 10), screenattr(lnum, 16))
94
95  syntax off
96  quit!
97endfunc
98
99func Test_default_conceal_char()
100  new
101  setlocal concealcursor=n conceallevel=1
102
103  1put='# This is a Test'
104  "             1234567890123456
105  let expect = '# This is a Test'
106
107  call cursor(1, 1)
108  call matchadd('Conceal', '\%2l ', 10, -1, {})
109  redraw!
110  let lnum = 2
111  call assert_equal(expect, Screenline(lnum))
112  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
113  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
114  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
115  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
116  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
117
118  "             1234567890123456
119  let expect = '#+This+is+a+Test'
120  let listchars_save = &listchars
121  set listchars=conceal:+
122  redraw!
123
124  call assert_equal(expect, Screenline(lnum))
125  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
126  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
127  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
128  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
129  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
130
131  let &listchars = listchars_save
132  quit!
133endfunc
134
135func Test_syn_and_match_conceal()
136  new
137  setlocal concealcursor=n conceallevel=1
138
139  1put='# This is a Test'
140  "             1234567890123456
141  let expect = '#ZThisZisZaZTest'
142
143  call cursor(1, 1)
144  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'Z'})
145  syntax match MyConceal /\%2l / conceal containedin=ALL cchar=*
146  redraw!
147  let lnum = 2
148  call assert_equal(expect, Screenline(lnum))
149  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
150  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
151  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
152  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
153  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
154
155  "             1234567890123456
156  let expect = '#*This*is*a*Test'
157  call clearmatches()
158  redraw!
159
160  call assert_equal(expect, Screenline(lnum))
161  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
162  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
163  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
164  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
165  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
166
167  syntax off
168  quit!
169endfunc
170
171func Test_clearmatches()
172  new
173  setlocal concealcursor=n conceallevel=1
174
175  1put='# This is a Test'
176  "             1234567890123456
177  let expect = '# This is a Test'
178
179  call cursor(1, 1)
180  call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'Z'})
181  let a = getmatches()
182  call clearmatches()
183  redraw!
184
185  let lnum = 2
186  call assert_equal(expect, Screenline(lnum))
187  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2))
188  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
189  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
190  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
191  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
192
193  " reset match using setmatches()
194  "             1234567890123456
195  let expect = '#ZThisZisZaZTest'
196  call setmatches(a)
197  redraw!
198
199  call assert_equal(expect, Screenline(lnum))
200  call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
201  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
202  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
203  call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
204  call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
205  call assert_equal({'group': 'Conceal', 'pattern': '\%2l ', 'priority': 10, 'id': a[0].id, 'conceal': 'Z'}, a[0])
206
207  quit!
208endfunc
209
210func Test_using_matchaddpos()
211  new
212  setlocal concealcursor=n conceallevel=1
213  " set filetype and :syntax on to change screenattr()
214  setlocal filetype=conf
215  syntax on
216
217  1put='# This is a Test'
218  "             1234567890123456
219  let expect = '#Pis a Test'
220
221  call cursor(1, 1)
222  call matchaddpos('Conceal', [[2,2,6]], 10, -1, {'conceal': 'P'})
223  let a = getmatches()
224  redraw!
225
226  let lnum = 2
227  call assert_equal(expect, Screenline(lnum))
228  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 2))
229  call assert_notequal(screenattr(lnum, 2) , screenattr(lnum, 7))
230  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 7))
231  call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 10))
232  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 12))
233  call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 16))
234  call assert_equal(screenattr(lnum, 12), screenattr(lnum, 16))
235  call assert_equal({'group': 'Conceal', 'id': a[0].id, 'priority': 10, 'pos1': [2, 2, 6], 'conceal': 'P'}, a[0])
236
237  syntax off
238  quit!
239endfunc
240
241func Test_matchadd_repeat_conceal_with_syntax_off()
242  new
243
244  " To test targets in the same line string is replaced with conceal char
245  " correctly, repeat 'TARGET'
246  1put ='TARGET_TARGETTARGET'
247  call cursor(1, 1)
248  redraw
249  call assert_equal('TARGET_TARGETTARGET', Screenline(2))
250
251  setlocal conceallevel=2
252  call matchadd('Conceal', 'TARGET', 10, -1, {'conceal': 't'})
253
254  redraw
255  call assert_equal('t_tt', Screenline(2))
256
257  quit!
258endfunc
259
260func Test_matchadd_and_syn_conceal()
261  new
262  let cnt='Inductive bool : Type := | true : bool | false : bool.'
263  let expect = 'Inductive - : Type := | true : - | false : -.'
264  0put =cnt
265  " set filetype and :syntax on to change screenattr()
266  set cole=1 cocu=nv
267  hi link CheckedByCoq WarningMsg
268  syntax on
269  syntax keyword coqKwd bool conceal cchar=-
270  redraw!
271  call assert_equal(expect, Screenline(1))
272  call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
273  call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
274  call assert_equal(screenattr(1, 11) , screenattr(1, 32))
275  call matchadd('CheckedByCoq', '\%<2l\%>9c\%<16c')
276  redraw!
277  call assert_equal(expect, Screenline(1))
278  call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
279  call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
280  call assert_equal(screenattr(1, 11) , screenattr(1, 32))
281endfunc
282
283func Test_cursor_column_in_concealed_line_after_window_scroll()
284  CheckRunVimInTerminal
285
286  " Test for issue #5012 fix.
287  " For a concealed line with cursor, there should be no window's cursor
288  " position invalidation during win_update() after scrolling attempt that is
289  " not successful and no real topline change happens. The invalidation would
290  " cause a window's cursor position recalc outside of win_line() where it's
291  " not possible to take conceal into account.
292  let lines =<< trim END
293    3split
294    let m = matchadd('Conceal', '=')
295    setl conceallevel=2 concealcursor=nc
296    normal gg
297    "==expr==
298  END
299  call writefile(lines, 'Xcolesearch')
300  let buf = RunVimInTerminal('Xcolesearch', {})
301  call TermWait(buf, 50)
302
303  " Jump to something that is beyond the bottom of the window,
304  " so there's a scroll down.
305  call term_sendkeys(buf, ":so %\<CR>")
306  call TermWait(buf, 50)
307  call term_sendkeys(buf, "/expr\<CR>")
308  call TermWait(buf, 50)
309
310  " Are the concealed parts of the current line really hidden?
311  let cursor_row = term_scrape(buf, '.')->map({_, e -> e.chars})->join('')
312  call assert_equal('"expr', cursor_row)
313
314  " BugFix check: Is the window's cursor column properly updated for hidden
315  " parts of the current line?
316  call assert_equal(2, term_getcursor(buf)[1])
317
318  call StopVimInTerminal(buf)
319  call delete('Xcolesearch')
320endfunc
321
322func Test_cursor_column_in_concealed_line_after_leftcol_change()
323  CheckRunVimInTerminal
324
325  " Test for issue #5214 fix.
326  let lines =<< trim END
327    0put = 'ab' .. repeat('-', &columns) .. 'c'
328    call matchadd('Conceal', '-')
329    set nowrap ss=0 cole=3 cocu=n
330  END
331  call writefile(lines, 'Xcurs-columns')
332  let buf = RunVimInTerminal('-S Xcurs-columns', {})
333
334  " Go to the end of the line (3 columns beyond the end of the screen).
335  " Horizontal scroll would center the cursor in the screen line, but conceal
336  " makes it go to screen column 1.
337  call term_sendkeys(buf, "$")
338  call TermWait(buf)
339
340  " Are the concealed parts of the current line really hidden?
341  call WaitForAssert({-> assert_equal('c', term_getline(buf, '.'))})
342
343  " BugFix check: Is the window's cursor column properly updated for conceal?
344  call assert_equal(1, term_getcursor(buf)[1])
345
346  call StopVimInTerminal(buf)
347  call delete('Xcurs-columns')
348endfunc
349