blob: 6cc38d56634770da65cb9057eebcd73ef36dc141 [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
4if !has('textprop')
5 finish
6endif
7
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01008source screendump.vim
9
Bram Moolenaarb9c67a52019-01-01 19:49:20 +010010" test length zero
11
Bram Moolenaar98aefe72018-12-13 22:20:09 +010012func Test_proptype_global()
13 call prop_type_add('comment', {'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
14 let proptypes = prop_type_list()
15 call assert_equal(1, len(proptypes))
16 call assert_equal('comment', proptypes[0])
17
18 let proptype = prop_type_get('comment')
19 call assert_equal('Directory', proptype['highlight'])
20 call assert_equal(123, proptype['priority'])
21 call assert_equal(1, proptype['start_incl'])
22 call assert_equal(1, proptype['end_incl'])
23
24 call prop_type_delete('comment')
25 call assert_equal(0, len(prop_type_list()))
26
27 call prop_type_add('one', {})
28 call assert_equal(1, len(prop_type_list()))
29 let proptype = prop_type_get('one')
30 call assert_false(has_key(proptype, 'highlight'))
31 call assert_equal(0, proptype['priority'])
32 call assert_equal(0, proptype['start_incl'])
33 call assert_equal(0, proptype['end_incl'])
34
35 call prop_type_add('two', {})
36 call assert_equal(2, len(prop_type_list()))
37 call prop_type_delete('one')
38 call assert_equal(1, len(prop_type_list()))
39 call prop_type_delete('two')
40 call assert_equal(0, len(prop_type_list()))
41endfunc
42
43func Test_proptype_buf()
44 let bufnr = bufnr('')
45 call prop_type_add('comment', {'bufnr': bufnr, 'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
46 let proptypes = prop_type_list({'bufnr': bufnr})
47 call assert_equal(1, len(proptypes))
48 call assert_equal('comment', proptypes[0])
49
50 let proptype = prop_type_get('comment', {'bufnr': bufnr})
51 call assert_equal('Directory', proptype['highlight'])
52 call assert_equal(123, proptype['priority'])
53 call assert_equal(1, proptype['start_incl'])
54 call assert_equal(1, proptype['end_incl'])
55
56 call prop_type_delete('comment', {'bufnr': bufnr})
57 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
58
59 call prop_type_add('one', {'bufnr': bufnr})
60 let proptype = prop_type_get('one', {'bufnr': bufnr})
61 call assert_false(has_key(proptype, 'highlight'))
62 call assert_equal(0, proptype['priority'])
63 call assert_equal(0, proptype['start_incl'])
64 call assert_equal(0, proptype['end_incl'])
65
66 call prop_type_add('two', {'bufnr': bufnr})
67 call assert_equal(2, len(prop_type_list({'bufnr': bufnr})))
68 call prop_type_delete('one', {'bufnr': bufnr})
69 call assert_equal(1, len(prop_type_list({'bufnr': bufnr})))
70 call prop_type_delete('two', {'bufnr': bufnr})
71 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
72endfunc
73
74func AddPropTypes()
75 call prop_type_add('one', {})
76 call prop_type_add('two', {})
77 call prop_type_add('three', {})
78 call prop_type_add('whole', {})
79endfunc
80
81func DeletePropTypes()
82 call prop_type_delete('one')
83 call prop_type_delete('two')
84 call prop_type_delete('three')
85 call prop_type_delete('whole')
86endfunc
87
88func SetupPropsInFirstLine()
89 call setline(1, 'one two three')
90 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
91 call prop_add(1, 5, {'length': 3, 'id': 12, 'type': 'two'})
Bram Moolenaar4164bb22019-01-04 23:09:49 +010092 call prop_add(1, 9, {'length': 5, 'id': 13, 'type': 'three'})
Bram Moolenaar98aefe72018-12-13 22:20:09 +010093 call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
94endfunc
95
Bram Moolenaar4164bb22019-01-04 23:09:49 +010096func Get_expected_props()
97 return [
98 \ {'col': 1, 'length': 13, 'id': 14, 'type': 'whole', 'start': 1, 'end': 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +010099 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
100 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100101 \ {'col': 9, 'length': 5, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100102 \ ]
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100103endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100104
105func Test_prop_add()
106 new
107 call AddPropTypes()
108 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100109 let expected_props = Get_expected_props()
110 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100111 call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:')
112 call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:')
113
114 " Insert a line above, text props must still be there.
115 call append(0, 'empty')
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100116 call assert_equal(expected_props, prop_list(2))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100117 " Delete a line above, text props must still be there.
118 1del
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100119 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100120
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100121 " Prop without length or end column is zero length
122 call prop_clear(1)
123 call prop_add(1, 5, {'type': 'two'})
124 let expected = [{'col': 5, 'length': 0, 'type': 'two', 'id': 0, 'start': 1, 'end': 1}]
125 call assert_equal(expected, prop_list(1))
126
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100127 call DeletePropTypes()
128 bwipe!
129endfunc
130
131func Test_prop_remove()
132 new
133 call AddPropTypes()
134 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100135 let props = Get_expected_props()
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100136 call assert_equal(props, prop_list(1))
137
138 " remove by id
139 call prop_remove({'id': 12}, 1)
140 unlet props[2]
141 call assert_equal(props, prop_list(1))
142
143 " remove by type
144 call prop_remove({'type': 'one'}, 1)
145 unlet props[1]
146 call assert_equal(props, prop_list(1))
147
148 call DeletePropTypes()
149 bwipe!
150endfunc
151
Bram Moolenaar196d1572019-01-02 23:47:18 +0100152func SetupOneLine()
153 call setline(1, 'xonex xtwoxx')
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200154 normal gg0
Bram Moolenaar196d1572019-01-02 23:47:18 +0100155 call AddPropTypes()
156 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
157 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
158 let expected = [
159 \ {'col': 2, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
160 \ {'col': 8, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
161 \]
162 call assert_equal(expected, prop_list(1))
163 return expected
164endfunc
165
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100166func Test_prop_add_remove_buf()
167 new
168 let bufnr = bufnr('')
169 call AddPropTypes()
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100170 for lnum in range(1, 4)
171 call setline(lnum, 'one two three')
172 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100173 wincmd w
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100174 for lnum in range(1, 4)
175 call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
176 call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
177 call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
178 endfor
179
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100180 let props = [
181 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
182 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
183 \ {'col': 11, 'length': 3, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
184 \]
185 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100186
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100187 " remove by id
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100188 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100189 unlet props[1]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100190
191 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100192 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100193 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
194 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
195 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
196
197 call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4)
198 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
199 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
200 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
201 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
202
203 call prop_remove({'id': 12, 'bufnr': bufnr})
204 for lnum in range(1, 4)
205 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
206 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100207
208 " remove by type
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100209 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100210 unlet props[0]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100211
212 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100213 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100214 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
215 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
216 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
217
218 call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4)
219 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
220 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
221 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
222 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
223
224 call prop_remove({'type': 'one', 'bufnr': bufnr})
225 for lnum in range(1, 4)
226 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
227 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100228
229 call DeletePropTypes()
230 wincmd w
231 bwipe!
232endfunc
233
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100234func Test_prop_backspace()
235 new
236 set bs=2
Bram Moolenaar196d1572019-01-02 23:47:18 +0100237 let expected = SetupOneLine() " 'xonex xtwoxx'
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100238
239 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
240 call assert_equal('one xtwoxx', getline(1))
241 let expected[0].col = 1
242 let expected[1].col = 6
243 call assert_equal(expected, prop_list(1))
244
245 call DeletePropTypes()
246 bwipe!
247 set bs&
248endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100249
Bram Moolenaar196d1572019-01-02 23:47:18 +0100250func Test_prop_replace()
251 new
252 set bs=2
253 let expected = SetupOneLine() " 'xonex xtwoxx'
254
255 exe "normal 0Ryyy\<Esc>"
256 call assert_equal('yyyex xtwoxx', getline(1))
257 call assert_equal(expected, prop_list(1))
258
259 exe "normal ftRyy\<BS>"
260 call assert_equal('yyyex xywoxx', getline(1))
261 call assert_equal(expected, prop_list(1))
262
263 exe "normal 0fwRyy\<BS>"
264 call assert_equal('yyyex xyyoxx', getline(1))
265 call assert_equal(expected, prop_list(1))
266
267 exe "normal 0foRyy\<BS>\<BS>"
268 call assert_equal('yyyex xyyoxx', getline(1))
269 call assert_equal(expected, prop_list(1))
270
271 call DeletePropTypes()
272 bwipe!
273 set bs&
274endfunc
275
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200276func Test_prop_open_line()
277 new
278
279 " open new line, props stay in top line
280 let expected = SetupOneLine() " 'xonex xtwoxx'
281 exe "normal o\<Esc>"
282 call assert_equal('xonex xtwoxx', getline(1))
283 call assert_equal('', getline(2))
284 call assert_equal(expected, prop_list(1))
285 call DeletePropTypes()
286
287 " move all props to next line
288 let expected = SetupOneLine() " 'xonex xtwoxx'
289 exe "normal 0i\<CR>\<Esc>"
290 call assert_equal('', getline(1))
291 call assert_equal('xonex xtwoxx', getline(2))
292 call assert_equal(expected, prop_list(2))
293 call DeletePropTypes()
294
295 " split just before prop, move all props to next line
296 let expected = SetupOneLine() " 'xonex xtwoxx'
297 exe "normal 0li\<CR>\<Esc>"
298 call assert_equal('x', getline(1))
299 call assert_equal('onex xtwoxx', getline(2))
300 let expected[0].col -= 1
301 let expected[1].col -= 1
302 call assert_equal(expected, prop_list(2))
303 call DeletePropTypes()
304
305 " split inside prop, split first prop
306 let expected = SetupOneLine() " 'xonex xtwoxx'
307 exe "normal 0lli\<CR>\<Esc>"
308 call assert_equal('xo', getline(1))
309 call assert_equal('nex xtwoxx', getline(2))
310 let exp_first = [deepcopy(expected[0])]
311 let exp_first[0].length = 1
312 call assert_equal(exp_first, prop_list(1))
313 let expected[0].col = 1
314 let expected[0].length = 2
315 let expected[1].col -= 2
316 call assert_equal(expected, prop_list(2))
317 call DeletePropTypes()
318
319 " split just after first prop, empty prop and second prop move to next line
320 let expected = SetupOneLine() " 'xonex xtwoxx'
321 exe "normal 0fea\<CR>\<Esc>"
322 call assert_equal('xone', getline(1))
323 call assert_equal('x xtwoxx', getline(2))
324 let exp_first = expected[0:0]
325 call assert_equal(exp_first, prop_list(1))
326 let expected[0].col = 1
327 let expected[0].length = 0
328 let expected[1].col -= 4
329 call assert_equal(expected, prop_list(2))
330 call DeletePropTypes()
331
332 bwipe!
333 set bs&
334endfunc
335
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100336func Test_prop_clear()
337 new
338 call AddPropTypes()
339 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100340 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100341
342 call prop_clear(1)
343 call assert_equal([], prop_list(1))
344
345 call DeletePropTypes()
346 bwipe!
347endfunc
348
349func Test_prop_clear_buf()
350 new
351 call AddPropTypes()
352 call SetupPropsInFirstLine()
353 let bufnr = bufnr('')
354 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100355 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100356
357 call prop_clear(1, 1, {'bufnr': bufnr})
358 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
359
360 wincmd w
361 call DeletePropTypes()
362 bwipe!
363endfunc
364
Bram Moolenaar21b50382019-01-04 18:07:24 +0100365func Test_prop_setline()
366 new
367 call AddPropTypes()
368 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100369 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100370
371 call setline(1, 'foobar')
372 call assert_equal([], prop_list(1))
373
374 call DeletePropTypes()
375 bwipe!
376endfunc
377
378func Test_prop_setbufline()
379 new
380 call AddPropTypes()
381 call SetupPropsInFirstLine()
382 let bufnr = bufnr('')
383 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100384 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100385
386 call setbufline(bufnr, 1, 'foobar')
387 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
388
389 wincmd w
390 call DeletePropTypes()
391 bwipe!
392endfunc
393
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100394func Test_prop_substitute()
395 new
396 " Set first line to 'one two three'
397 call AddPropTypes()
398 call SetupPropsInFirstLine()
399 let expected_props = Get_expected_props()
400 call assert_equal(expected_props, prop_list(1))
401
402 " Change "n" in "one" to XX: 'oXXe two three'
403 s/n/XX/
404 let expected_props[0].length += 1
405 let expected_props[1].length += 1
406 let expected_props[2].col += 1
407 let expected_props[3].col += 1
408 call assert_equal(expected_props, prop_list(1))
409
410 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
411 s/t//g
412 let expected_props[0].length -= 2
413 let expected_props[2].length -= 1
414 let expected_props[3].length -= 1
415 let expected_props[3].col -= 1
416 call assert_equal(expected_props, prop_list(1))
417
418 " Split the line by changing w to line break: 'oXXe ', 'o hree'
419 " The long prop is split and spans both lines.
420 " The props on "two" and "three" move to the next line.
421 s/w/\r/
422 let new_props = [
423 \ copy(expected_props[0]),
424 \ copy(expected_props[2]),
425 \ copy(expected_props[3]),
426 \ ]
427 let expected_props[0].length = 5
428 unlet expected_props[3]
429 unlet expected_props[2]
430 call assert_equal(expected_props, prop_list(1))
431
432 let new_props[0].length = 6
433 let new_props[1].col = 1
434 let new_props[1].length = 1
435 let new_props[2].col = 3
436 call assert_equal(new_props, prop_list(2))
437
438 call DeletePropTypes()
439 bwipe!
440endfunc
441
Bram Moolenaar663bc892019-01-08 23:07:24 +0100442func Test_prop_change_indent()
443 call prop_type_add('comment', {'highlight': 'Directory'})
444 new
445 call setline(1, [' xxx', 'yyyyy'])
446 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
447 let expect = {'col': 2, 'length': 2, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
448 call assert_equal([expect], prop_list(2))
449
450 set shiftwidth=3
451 normal 2G>>
452 call assert_equal(' yyyyy', getline(2))
453 let expect.col += 3
454 call assert_equal([expect], prop_list(2))
455
456 normal 2G==
457 call assert_equal(' yyyyy', getline(2))
458 let expect.col = 6
459 call assert_equal([expect], prop_list(2))
460
461 call prop_clear(2)
462 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
463 let expect.col = 2
464 let expect.length = 5
465 call assert_equal([expect], prop_list(2))
466
467 normal 2G<<
468 call assert_equal(' yyyyy', getline(2))
469 let expect.length = 2
470 call assert_equal([expect], prop_list(2))
471
472 set shiftwidth&
473 call prop_type_delete('comment')
474endfunc
475
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100476" Setup a three line prop in lines 2 - 4.
477" Add short props in line 1 and 5.
478func Setup_three_line_prop()
479 new
480 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
481 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
482 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
483 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
484endfunc
485
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100486func Test_prop_multiline()
487 call prop_type_add('comment', {'highlight': 'Directory'})
488 new
489 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
490
491 " start halfway line 1, end halfway line 3
492 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
493 let expect1 = {'col': 3, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
494 call assert_equal([expect1], prop_list(1))
495 let expect2 = {'col': 1, 'length': 10, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
496 call assert_equal([expect2], prop_list(2))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100497 let expect3 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100498 call assert_equal([expect3], prop_list(3))
499 call prop_clear(1, 3)
500
501 " include all three lines
502 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
503 let expect1.col = 1
504 let expect1.length = 8
505 call assert_equal([expect1], prop_list(1))
506 call assert_equal([expect2], prop_list(2))
507 let expect3.length = 9
508 call assert_equal([expect3], prop_list(3))
509 call prop_clear(1, 3)
510
511 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100512
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100513 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100514 call Setup_three_line_prop()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100515 let expect_short = {'col': 2, 'length': 1, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
516 call assert_equal([expect_short], prop_list(1))
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100517 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
518 call assert_equal([expect2], prop_list(2))
519 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100520 call assert_equal([expect_short], prop_list(1))
521 let expect2 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
522 call assert_equal([expect2], prop_list(2))
523 bwipe!
524
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100525 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100526 call Setup_three_line_prop()
527 let expect3 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
528 call assert_equal([expect3], prop_list(3))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100529 let expect4 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100530 call assert_equal([expect4], prop_list(4))
531 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100532 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100533 call assert_equal([expect3], prop_list(3))
534 call assert_equal([expect_short], prop_list(4))
535 bwipe!
536
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100537 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100538 call Setup_three_line_prop()
539 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
540 call assert_equal([expect2], prop_list(2))
541 call append(2, "new line")
542 call assert_equal([expect2], prop_list(2))
543 let expect3 = {'col': 1, 'length': 9, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
544 call assert_equal([expect3], prop_list(3))
545 bwipe!
546
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100547 call prop_type_delete('comment')
548endfunc
549
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100550func Test_prop_byteoff()
551 call prop_type_add('comment', {'highlight': 'Directory'})
552 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100553 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100554 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100555 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100556 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100557 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100558
559 bwipe!
560 call prop_type_delete('comment')
561endfunc
562
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100563func Test_prop_undo()
564 new
565 call prop_type_add('comment', {'highlight': 'Directory'})
566 call setline(1, ['oneone', 'twotwo', 'three'])
567 " Set 'undolevels' to break changes into undo-able pieces.
568 set ul&
569
570 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
571 let expected = [{'col': 3, 'length': 2, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
572 call assert_equal(expected, prop_list(1))
573
574 " Insert a character, then undo.
575 exe "normal 0lllix\<Esc>"
576 set ul&
577 let expected[0].length = 3
578 call assert_equal(expected, prop_list(1))
579 undo
580 let expected[0].length = 2
581 call assert_equal(expected, prop_list(1))
582
583 " Delete a character, then undo
584 exe "normal 0lllx"
585 set ul&
586 let expected[0].length = 1
587 call assert_equal(expected, prop_list(1))
588 undo
589 let expected[0].length = 2
590 call assert_equal(expected, prop_list(1))
591
592 " Delete the line, then undo
593 1d
594 set ul&
595 call assert_equal([], prop_list(1))
596 undo
597 call assert_equal(expected, prop_list(1))
598
599 " Insert a character, delete two characters, then undo with "U"
600 exe "normal 0lllix\<Esc>"
601 set ul&
602 let expected[0].length = 3
603 call assert_equal(expected, prop_list(1))
604 exe "normal 0lllxx"
605 set ul&
606 let expected[0].length = 1
607 call assert_equal(expected, prop_list(1))
608 normal U
609 let expected[0].length = 2
610 call assert_equal(expected, prop_list(1))
611
612 bwipe!
613 call prop_type_delete('comment')
614endfunc
615
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100616" screenshot test with textprop highlighting
617funct Test_textprop_screenshots()
Bram Moolenaared79d1e2019-02-22 14:38:58 +0100618 " The Vim running in the terminal needs to use utf-8.
619 if !CanRunVimInTerminal() || g:orig_encoding != 'utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100620 return
621 endif
622 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +0200623 \ "call setline(1, ["
624 \ .. "'One two',"
625 \ .. "'Numbér 123 änd thœn 4¾7.',"
626 \ .. "'--aa--bb--cc--dd--',"
627 \ .. "'// comment with error in it',"
628 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100629 \ "hi NumberProp ctermfg=blue",
630 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200631 \ "hi BackgroundProp ctermbg=lightgrey",
632 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100633 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
634 \ "call prop_type_add('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100635 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
636 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
637 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200638 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 1})",
639 \ "call prop_type_add('error', {'highlight': 'UnderlineProp', 'combine': 1})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100640 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100641 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
642 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100643 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
644 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
645 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
646 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200647 \ "call prop_add(4, 12, {'length': 10, 'type': 'background'})",
648 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100649 \ "set number",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100650 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100651 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200652 \ "syn match Comment '//.*'",
653 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100654 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100655 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100656 \], 'XtestProp')
Bram Moolenaarde24a872019-05-05 15:48:00 +0200657 let buf = RunVimInTerminal('-S XtestProp', {'rows': 7})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100658 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100659
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100660 " clean up
661 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +0100662 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100663endfunc