blob: 36eeb709b8af3f580659a0a61c08b824786980d1 [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()))
28 let proptype = prop_type_get('one')
29 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})
56 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
57
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'})
92 call prop_add(1, 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 Moolenaarf0884c52019-05-24 21:22:29 +0200142 call assert_equal(1, prop_remove({'id': 12}, 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
347 call prop_clear(1)
348 call assert_equal([], prop_list(1))
349
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()
492 call prop_type_add('comment', {'highlight': 'Directory'})
493 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 Moolenaared79d1e2019-02-22 14:38:58 +0100655 " The Vim running in the terminal needs to use utf-8.
656 if !CanRunVimInTerminal() || g:orig_encoding != 'utf-8'
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200657 throw 'Skipped: cannot make screendumps or not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100658 endif
659 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +0200660 \ "call setline(1, ["
661 \ .. "'One two',"
662 \ .. "'Numbér 123 änd thœn 4¾7.',"
663 \ .. "'--aa--bb--cc--dd--',"
664 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200665 \ .. "'first line',"
666 \ .. "' second line ',"
667 \ .. "'third line',"
668 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +0200669 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100670 \ "hi NumberProp ctermfg=blue",
671 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200672 \ "hi BackgroundProp ctermbg=lightgrey",
673 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100674 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
675 \ "call prop_type_add('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100676 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
677 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
678 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200679 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 1})",
680 \ "call prop_type_add('error', {'highlight': 'UnderlineProp', 'combine': 1})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100681 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100682 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
683 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100684 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
685 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
686 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
687 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200688 \ "call prop_add(4, 12, {'length': 10, 'type': 'background'})",
689 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200690 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
691 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
692 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
693 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +0200694 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100695 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100696 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200697 \ "syn match Comment '//.*'",
698 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100699 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100700 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200701 \ "normal 6G0i\<BS>\<Esc>",
702 \ "normal 3J",
703 \ "normal 3G",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100704 \], 'XtestProp')
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200705 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100706 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100707
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100708 " clean up
709 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +0100710 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100711endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +0200712
713func RunTestVisualBlock(width, dump)
714 call writefile([
715 \ "call setline(1, ["
716 \ .. "'xxxxxxxxx 123 x',"
717 \ .. "'xxxxxxxx 123 x',"
718 \ .. "'xxxxxxx 123 x',"
719 \ .. "'xxxxxx 123 x',"
720 \ .. "'xxxxx 123 x',"
721 \ .. "'xxxx 123 xx',"
722 \ .. "'xxx 123 xxx',"
723 \ .. "'xx 123 xxxx',"
724 \ .. "'x 123 xxxxx',"
725 \ .. "' 123 xxxxxx',"
726 \ .. "])",
727 \ "hi SearchProp ctermbg=yellow",
728 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
729 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
730 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
731 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
732 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
733 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
734 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
735 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
736 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
737 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
738 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
739 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
740 \], 'XtestPropVis')
741 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
742 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
743
744 " clean up
745 call StopVimInTerminal(buf)
746 call delete('XtestPropVis')
747endfunc
748
749" screenshot test with Visual block mode operations
750func Test_textprop_screenshot_visual()
751 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200752 throw 'Skipped: cannot make screendumps'
Bram Moolenaar8055d172019-05-17 22:57:26 +0200753 endif
754
755 " Delete two columns while text props are three chars wide.
756 call RunTestVisualBlock(2, '01')
757
758 " Same, but delete four columns
759 call RunTestVisualBlock(4, '02')
760endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +0200761
Bram Moolenaara956bf62019-06-19 17:34:24 +0200762func Test_textprop_after_tab()
763 let lines =<< trim END
764 call setline(1, [
765 \ "\txxx",
766 \ "x\txxx",
767 \ ])
768 hi SearchProp ctermbg=yellow
769 call prop_type_add('search', {'highlight': 'SearchProp'})
770 call prop_add(1, 2, {'length': 3, 'type': 'search'})
771 call prop_add(2, 3, {'length': 3, 'type': 'search'})
772 END
773 call writefile(lines, 'XtestPropTab')
774 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
775 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
776
777 " clean up
778 call StopVimInTerminal(buf)
779 call delete('XtestPropTab')
780endfunc
781
Bram Moolenaard79eef22019-05-24 20:41:55 +0200782" Adding a text property to a new buffer should not fail
783func Test_textprop_empty_buffer()
784 call prop_type_add('comment', {'highlight': 'Search'})
785 new
786 call prop_add(1, 1, {'type': 'comment'})
787 close
Bram Moolenaaradfde112019-05-25 22:11:45 +0200788 call prop_type_delete('comment')
789endfunc
790
791" Adding a text property to an empty buffer and then editing another
792func Test_textprop_empty_buffer_next()
793 call prop_type_add("xxx", {})
794 call prop_add(1, 1, {"type": "xxx"})
795 next X
796 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +0200797endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200798
799func Test_textprop_remove_from_buf()
800 new
801 let buf = bufnr('')
802 call prop_type_add('one', {'bufnr': buf})
803 call prop_add(1, 1, {'type': 'one', 'id': 234})
804 file x
805 edit y
806 call prop_remove({'id': 234, 'bufnr': buf}, 1)
807 call prop_type_delete('one', {'bufnr': buf})
808 bwipe! x
809 close
810endfunc