blob: 123bdf066193853e196bf46034d998d84a0cdcb3 [file] [log] [blame]
Bram Moolenaar4d585022016-04-14 19:50:22 +02001" Test for matchadd() and conceal feature
2if !has('conceal')
3 finish
4endif
5
6if !has('gui_running') && has('unix')
7 set term=ansi
8endif
9
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010010source shared.vim
Bram Moolenaar4d585022016-04-14 19:50:22 +020011
12function! Test_simple_matchadd()
13 new
14
15 1put='# This is a Test'
16 " 1234567890123456
17 let expect = '# This is a Test'
18
19 call cursor(1, 1)
20 call matchadd('Conceal', '\%2l ')
21 redraw!
22 let lnum = 2
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010023 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +020024 call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
25 call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
26 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
27 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
28 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
29 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
30
31 quit!
32endfunction
33
34function! Test_simple_matchadd_and_conceal()
35 new
36 setlocal concealcursor=n conceallevel=1
37
38 1put='# This is a Test'
39 " 1234567890123456
40 let expect = '#XThisXisXaXTest'
41
42 call cursor(1, 1)
43 call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'})
44 redraw!
45 let lnum = 2
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010046 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +020047 call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
48 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
49 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
50 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
51 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
52
53 quit!
54endfunction
55
56function! Test_matchadd_and_conceallevel_3()
57 new
58
59 setlocal conceallevel=3
60 " set filetype and :syntax on to change screenattr()
61 setlocal filetype=conf
62 syntax on
63
64 1put='# This is a Test'
65 " 1234567890123456
66 let expect = '#ThisisaTest'
67
68 call cursor(1, 1)
69 call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'})
70 redraw!
71 let lnum = 2
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010072 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +020073 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2))
74 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
75 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
76 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
77 call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 16))
78
79 " more matchadd()
80 " 1234567890123456
81 let expect = '#Thisisa Test'
82
83 call matchadd('ErrorMsg', '\%2l Test', 20, -1, {'conceal': 'X'})
84 redraw!
Bram Moolenaar5f73ef82018-02-27 21:09:30 +010085 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +020086 call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 2))
87 call assert_equal(screenattr(lnum, 2) , screenattr(lnum, 7))
88 call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 10))
89 call assert_equal(screenattr(lnum, 10), screenattr(lnum, 12))
90 call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 16))
91 call assert_notequal(screenattr(lnum, 10), screenattr(lnum, 16))
92
93 syntax off
94 quit!
95endfunction
96
97function! Test_default_conceal_char()
98 new
99 setlocal concealcursor=n conceallevel=1
100
101 1put='# This is a Test'
102 " 1234567890123456
103 let expect = '# This is a Test'
104
105 call cursor(1, 1)
106 call matchadd('Conceal', '\%2l ', 10, -1, {})
107 redraw!
108 let lnum = 2
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100109 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +0200110 call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
111 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
112 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
113 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
114 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
115
116 " 1234567890123456
117 let expect = '#+This+is+a+Test'
118 let listchars_save = &listchars
119 set listchars=conceal:+
120 redraw!
121
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100122 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +0200123 call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
124 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
125 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
126 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
127 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
128
129 let &listchars = listchars_save
130 quit!
131endfunction
132
133function! Test_syn_and_match_conceal()
134 new
135 setlocal concealcursor=n conceallevel=1
136
137 1put='# This is a Test'
138 " 1234567890123456
139 let expect = '#ZThisZisZaZTest'
140
141 call cursor(1, 1)
142 call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'Z'})
143 syntax match MyConceal /\%2l / conceal containedin=ALL cchar=*
144 redraw!
145 let lnum = 2
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100146 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +0200147 call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
148 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
149 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
150 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
151 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
152
153 " 1234567890123456
154 let expect = '#*This*is*a*Test'
155 call clearmatches()
156 redraw!
157
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100158 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +0200159 call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
160 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
161 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
162 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
163 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
164
165 syntax off
166 quit!
167endfunction
168
169function! Test_clearmatches()
170 new
171 setlocal concealcursor=n conceallevel=1
172
173 1put='# This is a Test'
174 " 1234567890123456
175 let expect = '# This is a Test'
176
177 call cursor(1, 1)
178 call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'Z'})
179 let a = getmatches()
180 call clearmatches()
181 redraw!
182
183 let lnum = 2
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100184 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +0200185 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2))
186 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
187 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
188 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
189 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
190
191 " reset match using setmatches()
192 " 1234567890123456
193 let expect = '#ZThisZisZaZTest'
194 call setmatches(a)
195 redraw!
196
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100197 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +0200198 call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
199 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
200 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
201 call assert_equal(screenattr(lnum, 2), screenattr(lnum, 12))
202 call assert_equal(screenattr(lnum, 1), screenattr(lnum, 16))
203 call assert_equal({'group': 'Conceal', 'pattern': '\%2l ', 'priority': 10, 'id': a[0].id, 'conceal': 'Z'}, a[0])
204
205 quit!
206endfunction
207
208function! Test_using_matchaddpos()
209 new
210 setlocal concealcursor=n conceallevel=1
211 " set filetype and :syntax on to change screenattr()
212 setlocal filetype=conf
213 syntax on
214
215 1put='# This is a Test'
216 " 1234567890123456
217 let expect = '#Pis a Test'
218
219 call cursor(1, 1)
220 call matchaddpos('Conceal', [[2,2,6]], 10, -1, {'conceal': 'P'})
221 let a = getmatches()
222 redraw!
223
224 let lnum = 2
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100225 call assert_equal(expect, Screenline(lnum))
Bram Moolenaar4d585022016-04-14 19:50:22 +0200226 call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 2))
227 call assert_notequal(screenattr(lnum, 2) , screenattr(lnum, 7))
228 call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 7))
229 call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 10))
230 call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 12))
231 call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 16))
232 call assert_equal(screenattr(lnum, 12), screenattr(lnum, 16))
233 call assert_equal({'group': 'Conceal', 'id': a[0].id, 'priority': 10, 'pos1': [2, 2, 6], 'conceal': 'P'}, a[0])
234
235 syntax off
236 quit!
237endfunction
238
239function! Test_matchadd_repeat_conceal_with_syntax_off()
240 new
241
242 " To test targets in the same line string is replaced with conceal char
243 " correctly, repeat 'TARGET'
244 1put ='TARGET_TARGETTARGET'
245 call cursor(1, 1)
246 redraw
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100247 call assert_equal('TARGET_TARGETTARGET', Screenline(2))
Bram Moolenaar4d585022016-04-14 19:50:22 +0200248
249 setlocal conceallevel=2
250 call matchadd('Conceal', 'TARGET', 10, -1, {'conceal': 't'})
251
252 redraw
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100253 call assert_equal('t_tt', Screenline(2))
Bram Moolenaar4d585022016-04-14 19:50:22 +0200254
255 quit!
256endfunction
Bram Moolenaar2f979122016-10-27 17:27:44 +0200257
258function! Test_matchadd_and_syn_conceal()
259 new
260 let cnt='Inductive bool : Type := | true : bool | false : bool.'
261 let expect = 'Inductive - : Type := | true : - | false : -.'
262 0put =cnt
263 " set filetype and :syntax on to change screenattr()
264 set cole=1 cocu=nv
265 hi link CheckedByCoq WarningMsg
266 syntax on
267 syntax keyword coqKwd bool conceal cchar=-
268 redraw!
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100269 call assert_equal(expect, Screenline(1))
Bram Moolenaar2f979122016-10-27 17:27:44 +0200270 call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
271 call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
272 call assert_equal(screenattr(1, 11) , screenattr(1, 32))
273 call matchadd('CheckedByCoq', '\%<2l\%>9c\%<16c')
Bram Moolenaar35a1f592016-10-28 22:10:27 +0200274 redraw!
Bram Moolenaar5f73ef82018-02-27 21:09:30 +0100275 call assert_equal(expect, Screenline(1))
Bram Moolenaar2f979122016-10-27 17:27:44 +0200276 call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
277 call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
278 call assert_equal(screenattr(1, 11) , screenattr(1, 32))
279endfunction