blob: 467d833497ef5b7953a4c559cfac8dc7b9cbf47e [file] [log] [blame]
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001" Tests for defining text property types and adding text properties to the
2" buffer.
3
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02004source check.vim
5CheckFeature textprop
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01007source screendump.vim
8
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01009" test length zero
10
Bram Moolenaar98aefe72018-12-13 22:20:09 +010011func Test_proptype_global()
12 call prop_type_add('comment', {'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
13 let proptypes = prop_type_list()
14 call assert_equal(1, len(proptypes))
15 call assert_equal('comment', proptypes[0])
16
17 let proptype = prop_type_get('comment')
18 call assert_equal('Directory', proptype['highlight'])
19 call assert_equal(123, proptype['priority'])
20 call assert_equal(1, proptype['start_incl'])
21 call assert_equal(1, proptype['end_incl'])
22
23 call prop_type_delete('comment')
24 call assert_equal(0, len(prop_type_list()))
25
26 call prop_type_add('one', {})
27 call assert_equal(1, len(prop_type_list()))
Bram Moolenaara5a78822019-09-04 21:57:18 +020028 let proptype = 'one'->prop_type_get()
Bram Moolenaar98aefe72018-12-13 22:20:09 +010029 call assert_false(has_key(proptype, 'highlight'))
30 call assert_equal(0, proptype['priority'])
31 call assert_equal(0, proptype['start_incl'])
32 call assert_equal(0, proptype['end_incl'])
33
34 call prop_type_add('two', {})
35 call assert_equal(2, len(prop_type_list()))
36 call prop_type_delete('one')
37 call assert_equal(1, len(prop_type_list()))
38 call prop_type_delete('two')
39 call assert_equal(0, len(prop_type_list()))
40endfunc
41
42func Test_proptype_buf()
43 let bufnr = bufnr('')
44 call prop_type_add('comment', {'bufnr': bufnr, 'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
45 let proptypes = prop_type_list({'bufnr': bufnr})
46 call assert_equal(1, len(proptypes))
47 call assert_equal('comment', proptypes[0])
48
49 let proptype = prop_type_get('comment', {'bufnr': bufnr})
50 call assert_equal('Directory', proptype['highlight'])
51 call assert_equal(123, proptype['priority'])
52 call assert_equal(1, proptype['start_incl'])
53 call assert_equal(1, proptype['end_incl'])
54
55 call prop_type_delete('comment', {'bufnr': bufnr})
Bram Moolenaara5a78822019-09-04 21:57:18 +020056 call assert_equal(0, len({'bufnr': bufnr}->prop_type_list()))
Bram Moolenaar98aefe72018-12-13 22:20:09 +010057
58 call prop_type_add('one', {'bufnr': bufnr})
59 let proptype = prop_type_get('one', {'bufnr': bufnr})
60 call assert_false(has_key(proptype, 'highlight'))
61 call assert_equal(0, proptype['priority'])
62 call assert_equal(0, proptype['start_incl'])
63 call assert_equal(0, proptype['end_incl'])
64
65 call prop_type_add('two', {'bufnr': bufnr})
66 call assert_equal(2, len(prop_type_list({'bufnr': bufnr})))
67 call prop_type_delete('one', {'bufnr': bufnr})
68 call assert_equal(1, len(prop_type_list({'bufnr': bufnr})))
69 call prop_type_delete('two', {'bufnr': bufnr})
70 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
Bram Moolenaarf0884c52019-05-24 21:22:29 +020071
72 call assert_fails("call prop_type_add('one', {'bufnr': 98764})", "E158:")
Bram Moolenaar98aefe72018-12-13 22:20:09 +010073endfunc
74
75func AddPropTypes()
76 call prop_type_add('one', {})
77 call prop_type_add('two', {})
78 call prop_type_add('three', {})
79 call prop_type_add('whole', {})
80endfunc
81
82func DeletePropTypes()
83 call prop_type_delete('one')
84 call prop_type_delete('two')
85 call prop_type_delete('three')
86 call prop_type_delete('whole')
87endfunc
88
89func SetupPropsInFirstLine()
90 call setline(1, 'one two three')
91 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
Bram Moolenaara5a78822019-09-04 21:57:18 +020092 eval 1->prop_add(5, {'length': 3, 'id': 12, 'type': 'two'})
Bram Moolenaar4164bb22019-01-04 23:09:49 +010093 call prop_add(1, 9, {'length': 5, 'id': 13, 'type': 'three'})
Bram Moolenaar98aefe72018-12-13 22:20:09 +010094 call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
95endfunc
96
Bram Moolenaar4164bb22019-01-04 23:09:49 +010097func Get_expected_props()
98 return [
99 \ {'col': 1, 'length': 13, 'id': 14, 'type': 'whole', 'start': 1, 'end': 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100100 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
101 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100102 \ {'col': 9, 'length': 5, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100103 \ ]
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100104endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100105
106func Test_prop_add()
107 new
108 call AddPropTypes()
109 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100110 let expected_props = Get_expected_props()
111 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100112 call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:')
113 call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:')
114
115 " Insert a line above, text props must still be there.
116 call append(0, 'empty')
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100117 call assert_equal(expected_props, prop_list(2))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100118 " Delete a line above, text props must still be there.
119 1del
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100120 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100121
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100122 " Prop without length or end column is zero length
123 call prop_clear(1)
124 call prop_add(1, 5, {'type': 'two'})
125 let expected = [{'col': 5, 'length': 0, 'type': 'two', 'id': 0, 'start': 1, 'end': 1}]
126 call assert_equal(expected, prop_list(1))
127
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200128 call assert_fails("call prop_add(1, 5, {'type': 'two', 'bufnr': 234343})", 'E158:')
129
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100130 call DeletePropTypes()
131 bwipe!
132endfunc
133
134func Test_prop_remove()
135 new
136 call AddPropTypes()
137 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100138 let props = Get_expected_props()
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100139 call assert_equal(props, prop_list(1))
140
141 " remove by id
Bram Moolenaara5a78822019-09-04 21:57:18 +0200142 call assert_equal(1, {'id': 12}->prop_remove(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100143 unlet props[2]
144 call assert_equal(props, prop_list(1))
145
146 " remove by type
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200147 call assert_equal(1, prop_remove({'type': 'one'}, 1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100148 unlet props[1]
149 call assert_equal(props, prop_list(1))
150
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200151 " remove from unknown buffer
152 call assert_fails("call prop_remove({'type': 'one', 'bufnr': 123456}, 1)", 'E158:')
153
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100154 call DeletePropTypes()
155 bwipe!
156endfunc
157
Bram Moolenaar196d1572019-01-02 23:47:18 +0100158func SetupOneLine()
159 call setline(1, 'xonex xtwoxx')
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200160 normal gg0
Bram Moolenaar196d1572019-01-02 23:47:18 +0100161 call AddPropTypes()
162 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
163 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
164 let expected = [
165 \ {'col': 2, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
166 \ {'col': 8, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
167 \]
168 call assert_equal(expected, prop_list(1))
169 return expected
170endfunc
171
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100172func Test_prop_add_remove_buf()
173 new
174 let bufnr = bufnr('')
175 call AddPropTypes()
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100176 for lnum in range(1, 4)
177 call setline(lnum, 'one two three')
178 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100179 wincmd w
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100180 for lnum in range(1, 4)
181 call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
182 call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
183 call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
184 endfor
185
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100186 let props = [
187 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
188 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
189 \ {'col': 11, 'length': 3, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
190 \]
191 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100192
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100193 " remove by id
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100194 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100195 unlet props[1]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100196
197 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100198 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100199 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
200 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
201 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
202
203 call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4)
204 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
205 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
206 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
207 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
208
209 call prop_remove({'id': 12, 'bufnr': bufnr})
210 for lnum in range(1, 4)
211 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
212 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100213
214 " remove by type
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100215 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100216 unlet props[0]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100217
218 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100219 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100220 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
221 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
222 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
223
224 call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4)
225 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
226 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
227 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
228 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
229
230 call prop_remove({'type': 'one', 'bufnr': bufnr})
231 for lnum in range(1, 4)
232 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
233 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100234
235 call DeletePropTypes()
236 wincmd w
237 bwipe!
238endfunc
239
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100240func Test_prop_backspace()
241 new
242 set bs=2
Bram Moolenaar196d1572019-01-02 23:47:18 +0100243 let expected = SetupOneLine() " 'xonex xtwoxx'
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100244
245 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
246 call assert_equal('one xtwoxx', getline(1))
247 let expected[0].col = 1
248 let expected[1].col = 6
249 call assert_equal(expected, prop_list(1))
250
251 call DeletePropTypes()
252 bwipe!
253 set bs&
254endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100255
Bram Moolenaar196d1572019-01-02 23:47:18 +0100256func Test_prop_replace()
257 new
258 set bs=2
259 let expected = SetupOneLine() " 'xonex xtwoxx'
260
261 exe "normal 0Ryyy\<Esc>"
262 call assert_equal('yyyex xtwoxx', getline(1))
263 call assert_equal(expected, prop_list(1))
264
265 exe "normal ftRyy\<BS>"
266 call assert_equal('yyyex xywoxx', getline(1))
267 call assert_equal(expected, prop_list(1))
268
269 exe "normal 0fwRyy\<BS>"
270 call assert_equal('yyyex xyyoxx', getline(1))
271 call assert_equal(expected, prop_list(1))
272
273 exe "normal 0foRyy\<BS>\<BS>"
274 call assert_equal('yyyex xyyoxx', getline(1))
275 call assert_equal(expected, prop_list(1))
276
277 call DeletePropTypes()
278 bwipe!
279 set bs&
280endfunc
281
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200282func Test_prop_open_line()
283 new
284
285 " open new line, props stay in top line
286 let expected = SetupOneLine() " 'xonex xtwoxx'
287 exe "normal o\<Esc>"
288 call assert_equal('xonex xtwoxx', getline(1))
289 call assert_equal('', getline(2))
290 call assert_equal(expected, prop_list(1))
291 call DeletePropTypes()
292
293 " move all props to next line
294 let expected = SetupOneLine() " 'xonex xtwoxx'
295 exe "normal 0i\<CR>\<Esc>"
296 call assert_equal('', getline(1))
297 call assert_equal('xonex xtwoxx', getline(2))
298 call assert_equal(expected, prop_list(2))
299 call DeletePropTypes()
300
301 " split just before prop, move all props to next line
302 let expected = SetupOneLine() " 'xonex xtwoxx'
303 exe "normal 0li\<CR>\<Esc>"
304 call assert_equal('x', getline(1))
305 call assert_equal('onex xtwoxx', getline(2))
306 let expected[0].col -= 1
307 let expected[1].col -= 1
308 call assert_equal(expected, prop_list(2))
309 call DeletePropTypes()
310
311 " split inside prop, split first prop
312 let expected = SetupOneLine() " 'xonex xtwoxx'
313 exe "normal 0lli\<CR>\<Esc>"
314 call assert_equal('xo', getline(1))
315 call assert_equal('nex xtwoxx', getline(2))
316 let exp_first = [deepcopy(expected[0])]
317 let exp_first[0].length = 1
318 call assert_equal(exp_first, prop_list(1))
319 let expected[0].col = 1
320 let expected[0].length = 2
321 let expected[1].col -= 2
322 call assert_equal(expected, prop_list(2))
323 call DeletePropTypes()
324
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200325 " split just after first prop, second prop move to next line
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200326 let expected = SetupOneLine() " 'xonex xtwoxx'
327 exe "normal 0fea\<CR>\<Esc>"
328 call assert_equal('xone', getline(1))
329 call assert_equal('x xtwoxx', getline(2))
330 let exp_first = expected[0:0]
331 call assert_equal(exp_first, prop_list(1))
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200332 let expected = expected[1:1]
333 let expected[0].col -= 4
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200334 call assert_equal(expected, prop_list(2))
335 call DeletePropTypes()
336
337 bwipe!
338 set bs&
339endfunc
340
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100341func Test_prop_clear()
342 new
343 call AddPropTypes()
344 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100345 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100346
Bram Moolenaara5a78822019-09-04 21:57:18 +0200347 eval 1->prop_clear()
348 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100349
350 call DeletePropTypes()
351 bwipe!
352endfunc
353
354func Test_prop_clear_buf()
355 new
356 call AddPropTypes()
357 call SetupPropsInFirstLine()
358 let bufnr = bufnr('')
359 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100360 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100361
362 call prop_clear(1, 1, {'bufnr': bufnr})
363 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
364
365 wincmd w
366 call DeletePropTypes()
367 bwipe!
368endfunc
369
Bram Moolenaar21b50382019-01-04 18:07:24 +0100370func Test_prop_setline()
371 new
372 call AddPropTypes()
373 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100374 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100375
376 call setline(1, 'foobar')
377 call assert_equal([], prop_list(1))
378
379 call DeletePropTypes()
380 bwipe!
381endfunc
382
383func Test_prop_setbufline()
384 new
385 call AddPropTypes()
386 call SetupPropsInFirstLine()
387 let bufnr = bufnr('')
388 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100389 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100390
391 call setbufline(bufnr, 1, 'foobar')
392 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
393
394 wincmd w
395 call DeletePropTypes()
396 bwipe!
397endfunc
398
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100399func Test_prop_substitute()
400 new
401 " Set first line to 'one two three'
402 call AddPropTypes()
403 call SetupPropsInFirstLine()
404 let expected_props = Get_expected_props()
405 call assert_equal(expected_props, prop_list(1))
406
407 " Change "n" in "one" to XX: 'oXXe two three'
408 s/n/XX/
409 let expected_props[0].length += 1
410 let expected_props[1].length += 1
411 let expected_props[2].col += 1
412 let expected_props[3].col += 1
413 call assert_equal(expected_props, prop_list(1))
414
415 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
416 s/t//g
417 let expected_props[0].length -= 2
418 let expected_props[2].length -= 1
419 let expected_props[3].length -= 1
420 let expected_props[3].col -= 1
421 call assert_equal(expected_props, prop_list(1))
422
423 " Split the line by changing w to line break: 'oXXe ', 'o hree'
424 " The long prop is split and spans both lines.
425 " The props on "two" and "three" move to the next line.
426 s/w/\r/
427 let new_props = [
428 \ copy(expected_props[0]),
429 \ copy(expected_props[2]),
430 \ copy(expected_props[3]),
431 \ ]
432 let expected_props[0].length = 5
433 unlet expected_props[3]
434 unlet expected_props[2]
435 call assert_equal(expected_props, prop_list(1))
436
437 let new_props[0].length = 6
438 let new_props[1].col = 1
439 let new_props[1].length = 1
440 let new_props[2].col = 3
441 call assert_equal(new_props, prop_list(2))
442
443 call DeletePropTypes()
444 bwipe!
445endfunc
446
Bram Moolenaar663bc892019-01-08 23:07:24 +0100447func Test_prop_change_indent()
448 call prop_type_add('comment', {'highlight': 'Directory'})
449 new
450 call setline(1, [' xxx', 'yyyyy'])
451 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
452 let expect = {'col': 2, 'length': 2, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
453 call assert_equal([expect], prop_list(2))
454
455 set shiftwidth=3
456 normal 2G>>
457 call assert_equal(' yyyyy', getline(2))
458 let expect.col += 3
459 call assert_equal([expect], prop_list(2))
460
461 normal 2G==
462 call assert_equal(' yyyyy', getline(2))
463 let expect.col = 6
464 call assert_equal([expect], prop_list(2))
465
466 call prop_clear(2)
467 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
468 let expect.col = 2
469 let expect.length = 5
470 call assert_equal([expect], prop_list(2))
471
472 normal 2G<<
473 call assert_equal(' yyyyy', getline(2))
474 let expect.length = 2
475 call assert_equal([expect], prop_list(2))
476
477 set shiftwidth&
478 call prop_type_delete('comment')
479endfunc
480
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100481" Setup a three line prop in lines 2 - 4.
482" Add short props in line 1 and 5.
483func Setup_three_line_prop()
484 new
485 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
486 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
487 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
488 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
489endfunc
490
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100491func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200492 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100493 new
494 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
495
496 " start halfway line 1, end halfway line 3
497 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
498 let expect1 = {'col': 3, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
499 call assert_equal([expect1], prop_list(1))
500 let expect2 = {'col': 1, 'length': 10, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
501 call assert_equal([expect2], prop_list(2))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100502 let expect3 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100503 call assert_equal([expect3], prop_list(3))
504 call prop_clear(1, 3)
505
506 " include all three lines
507 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
508 let expect1.col = 1
509 let expect1.length = 8
510 call assert_equal([expect1], prop_list(1))
511 call assert_equal([expect2], prop_list(2))
512 let expect3.length = 9
513 call assert_equal([expect3], prop_list(3))
514 call prop_clear(1, 3)
515
516 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100517
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100518 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100519 call Setup_three_line_prop()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100520 let expect_short = {'col': 2, 'length': 1, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
521 call assert_equal([expect_short], prop_list(1))
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100522 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
523 call assert_equal([expect2], prop_list(2))
524 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100525 call assert_equal([expect_short], prop_list(1))
526 let expect2 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
527 call assert_equal([expect2], prop_list(2))
528 bwipe!
529
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100530 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100531 call Setup_three_line_prop()
532 let expect3 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
533 call assert_equal([expect3], prop_list(3))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100534 let expect4 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100535 call assert_equal([expect4], prop_list(4))
536 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100537 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100538 call assert_equal([expect3], prop_list(3))
539 call assert_equal([expect_short], prop_list(4))
540 bwipe!
541
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100542 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100543 call Setup_three_line_prop()
544 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
545 call assert_equal([expect2], prop_list(2))
546 call append(2, "new line")
547 call assert_equal([expect2], prop_list(2))
548 let expect3 = {'col': 1, 'length': 9, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
549 call assert_equal([expect3], prop_list(3))
550 bwipe!
551
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100552 call prop_type_delete('comment')
553endfunc
554
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100555func Test_prop_byteoff()
556 call prop_type_add('comment', {'highlight': 'Directory'})
557 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100558 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100559 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100560 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100561 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100562 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100563
564 bwipe!
565 call prop_type_delete('comment')
566endfunc
567
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100568func Test_prop_undo()
569 new
570 call prop_type_add('comment', {'highlight': 'Directory'})
571 call setline(1, ['oneone', 'twotwo', 'three'])
572 " Set 'undolevels' to break changes into undo-able pieces.
573 set ul&
574
575 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
576 let expected = [{'col': 3, 'length': 2, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
577 call assert_equal(expected, prop_list(1))
578
579 " Insert a character, then undo.
580 exe "normal 0lllix\<Esc>"
581 set ul&
582 let expected[0].length = 3
583 call assert_equal(expected, prop_list(1))
584 undo
585 let expected[0].length = 2
586 call assert_equal(expected, prop_list(1))
587
588 " Delete a character, then undo
589 exe "normal 0lllx"
590 set ul&
591 let expected[0].length = 1
592 call assert_equal(expected, prop_list(1))
593 undo
594 let expected[0].length = 2
595 call assert_equal(expected, prop_list(1))
596
597 " Delete the line, then undo
598 1d
599 set ul&
600 call assert_equal([], prop_list(1))
601 undo
602 call assert_equal(expected, prop_list(1))
603
604 " Insert a character, delete two characters, then undo with "U"
605 exe "normal 0lllix\<Esc>"
606 set ul&
607 let expected[0].length = 3
608 call assert_equal(expected, prop_list(1))
609 exe "normal 0lllxx"
610 set ul&
611 let expected[0].length = 1
612 call assert_equal(expected, prop_list(1))
613 normal U
614 let expected[0].length = 2
615 call assert_equal(expected, prop_list(1))
616
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200617 " substitute a word, then undo
618 call setline(1, 'the number 123 is highlighted.')
619 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
620 let expected = [{'col': 12, 'length': 3, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
621 call assert_equal(expected, prop_list(1))
622 set ul&
623 1s/number/foo
624 let expected[0].col = 9
625 call assert_equal(expected, prop_list(1))
626 undo
627 let expected[0].col = 12
628 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200629 call prop_clear(1)
630
631 " substitute with backslash
632 call setline(1, 'the number 123 is highlighted.')
633 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
634 let expected = [{'col': 12, 'length': 3, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
635 call assert_equal(expected, prop_list(1))
636 1s/the/\The
637 call assert_equal(expected, prop_list(1))
638 1s/^/\\
639 let expected[0].col += 1
640 call assert_equal(expected, prop_list(1))
641 1s/^/\~
642 let expected[0].col += 1
643 call assert_equal(expected, prop_list(1))
644 1s/123/12\\3
645 let expected[0].length += 1
646 call assert_equal(expected, prop_list(1))
647 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200648
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100649 bwipe!
650 call prop_type_delete('comment')
651endfunc
652
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100653" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +0200654func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +0200655 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +0100656 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +0200657 if g:orig_encoding != 'utf-8'
658 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100659 endif
660 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +0200661 \ "call setline(1, ["
662 \ .. "'One two',"
663 \ .. "'Numbér 123 änd thœn 4¾7.',"
664 \ .. "'--aa--bb--cc--dd--',"
665 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200666 \ .. "'first line',"
667 \ .. "' second line ',"
668 \ .. "'third line',"
669 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +0200670 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100671 \ "hi NumberProp ctermfg=blue",
672 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200673 \ "hi BackgroundProp ctermbg=lightgrey",
674 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100675 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +0200676 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
677 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100678 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
679 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
680 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +0100681 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
682 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
683 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +0100684 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100685 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100686 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
687 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100688 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
689 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
690 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
691 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +0100692 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
693 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200694 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200695 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
696 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
697 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
698 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +0200699 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100700 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100701 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200702 \ "syn match Comment '//.*'",
703 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100704 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100705 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200706 \ "normal 6G0i\<BS>\<Esc>",
707 \ "normal 3J",
708 \ "normal 3G",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100709 \], 'XtestProp')
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200710 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100711 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100712
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100713 " clean up
714 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +0100715 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100716endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +0200717
718func RunTestVisualBlock(width, dump)
719 call writefile([
720 \ "call setline(1, ["
721 \ .. "'xxxxxxxxx 123 x',"
722 \ .. "'xxxxxxxx 123 x',"
723 \ .. "'xxxxxxx 123 x',"
724 \ .. "'xxxxxx 123 x',"
725 \ .. "'xxxxx 123 x',"
726 \ .. "'xxxx 123 xx',"
727 \ .. "'xxx 123 xxx',"
728 \ .. "'xx 123 xxxx',"
729 \ .. "'x 123 xxxxx',"
730 \ .. "' 123 xxxxxx',"
731 \ .. "])",
732 \ "hi SearchProp ctermbg=yellow",
733 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
734 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
735 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
736 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
737 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
738 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
739 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
740 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
741 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
742 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
743 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
744 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
745 \], 'XtestPropVis')
746 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
747 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
748
749 " clean up
750 call StopVimInTerminal(buf)
751 call delete('XtestPropVis')
752endfunc
753
754" screenshot test with Visual block mode operations
755func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +0200756 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +0200757
758 " Delete two columns while text props are three chars wide.
759 call RunTestVisualBlock(2, '01')
760
761 " Same, but delete four columns
762 call RunTestVisualBlock(4, '02')
763endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +0200764
Bram Moolenaara956bf62019-06-19 17:34:24 +0200765func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +0200766 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +0200767
Bram Moolenaara956bf62019-06-19 17:34:24 +0200768 let lines =<< trim END
769 call setline(1, [
770 \ "\txxx",
771 \ "x\txxx",
772 \ ])
773 hi SearchProp ctermbg=yellow
774 call prop_type_add('search', {'highlight': 'SearchProp'})
775 call prop_add(1, 2, {'length': 3, 'type': 'search'})
776 call prop_add(2, 3, {'length': 3, 'type': 'search'})
777 END
778 call writefile(lines, 'XtestPropTab')
779 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
780 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
781
782 " clean up
783 call StopVimInTerminal(buf)
784 call delete('XtestPropTab')
785endfunc
786
Bram Moolenaar34390282019-10-16 14:38:26 +0200787func Test_textprop_with_syntax()
788 CheckScreendump
789
790 let lines =<< trim END
791 call setline(1, [
792 \ "(abc)",
793 \ ])
794 syn match csParens "[()]" display
795 hi! link csParens MatchParen
796
797 call prop_type_add('TPTitle', #{ highlight: 'Title' })
798 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
799 END
800 call writefile(lines, 'XtestPropSyn')
801 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
802 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
803
804 " clean up
805 call StopVimInTerminal(buf)
806 call delete('XtestPropSyn')
807endfunc
808
Bram Moolenaard79eef22019-05-24 20:41:55 +0200809" Adding a text property to a new buffer should not fail
810func Test_textprop_empty_buffer()
811 call prop_type_add('comment', {'highlight': 'Search'})
812 new
813 call prop_add(1, 1, {'type': 'comment'})
814 close
Bram Moolenaaradfde112019-05-25 22:11:45 +0200815 call prop_type_delete('comment')
816endfunc
817
Bram Moolenaard74af422019-06-28 21:38:00 +0200818" Adding a text property with invalid highlight should be ignored.
819func Test_textprop_invalid_highlight()
820 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
821 new
822 call setline(1, ['asdf','asdf'])
823 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
824 redraw
825 bwipe!
826 call prop_type_delete('dni')
827endfunc
828
Bram Moolenaaradfde112019-05-25 22:11:45 +0200829" Adding a text property to an empty buffer and then editing another
830func Test_textprop_empty_buffer_next()
831 call prop_type_add("xxx", {})
832 call prop_add(1, 1, {"type": "xxx"})
833 next X
834 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +0200835endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200836
837func Test_textprop_remove_from_buf()
838 new
839 let buf = bufnr('')
840 call prop_type_add('one', {'bufnr': buf})
841 call prop_add(1, 1, {'type': 'one', 'id': 234})
842 file x
843 edit y
844 call prop_remove({'id': 234, 'bufnr': buf}, 1)
845 call prop_type_delete('one', {'bufnr': buf})
846 bwipe! x
847 close
848endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +0200849
850func Test_textprop_in_unloaded_buf()
851 edit Xaaa
852 call setline(1, 'aaa')
853 write
854 edit Xbbb
855 call setline(1, 'bbb')
856 write
857 let bnr = bufnr('')
858 edit Xaaa
859
860 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
861 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
862 exe 'buf ' .. bnr
863 call assert_equal('bbb', getline(1))
864 call assert_equal(0, prop_list(1)->len())
865
866 bwipe! Xaaa
867 bwipe! Xbbb
868 cal delete('Xaaa')
869 cal delete('Xbbb')
870endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +0200871
872func Test_proptype_substitute2()
873 new
874 " text_prop.vim
875 call setline(1, [
876 \ 'The num 123 is smaller than 4567.',
877 \ '123 The number 123 is smaller than 4567.',
878 \ '123 The number 123 is smaller than 4567.'])
879
880 call prop_type_add('number', {'highlight': 'ErrorMsg'})
881
882 call prop_add(1, 12, {'length': 3, 'type': 'number'})
883 call prop_add(2, 1, {'length': 3, 'type': 'number'})
884 call prop_add(3, 36, {'length': 4, 'type': 'number'})
885 set ul&
886 let expected = [{'id': 0, 'col': 13, 'end': 1, 'type': 'number', 'length': 3, 'start': 1},
887 \ {'id': 0, 'col': 1, 'end': 1, 'type': 'number', 'length': 3, 'start': 1},
888 \ {'id': 0, 'col': 50, 'end': 1, 'type': 'number', 'length': 4, 'start': 1}]
889 " Add some text in between
890 %s/\s\+/ /g
891 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
892
893 " remove some text
894 :1s/[a-z]\{3\}//g
895 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
896 call assert_equal(expected, prop_list(1))
897 bwipe!
898endfunc