blob: ca6fd746e6da5893b3a538df8960b7894820bcb2 [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
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100106func Test_prop_find()
107 new
108 call setline(1, ['one one one', 'twotwo', 'three', 'fourfour', 'five', 'sixsix'])
109
110 " Add two text props on lines 1 and 5, and one spanning lines 2 to 4.
111 call prop_type_add('prop_name', {'highlight': 'Directory'})
112 call prop_add(1, 5, {'type': 'prop_name', 'id': 10, 'length': 3})
113 call prop_add(2, 4, {'type': 'prop_name', 'id': 11, 'end_lnum': 4, 'end_col': 9})
114 call prop_add(5, 4, {'type': 'prop_name', 'id': 12, 'length': 1})
115
116 let expected = [
117 \ {'lnum': 1, 'col': 5, 'length': 3, 'id': 10, 'type': 'prop_name', 'start': 1, 'end': 1},
118 \ {'lnum': 2, 'col': 4, 'id': 11, 'type': 'prop_name', 'start': 1, 'end': 0},
119 \ {'lnum': 5, 'col': 4, 'length': 1, 'id': 12, 'type': 'prop_name', 'start': 1, 'end': 1}
120 \ ]
121
122 " Starting at line 5 col 1 this should find the prop at line 5 col 4.
123 call cursor(5,1)
124 let result = prop_find({'type': 'prop_name'}, 'f')
125 call assert_equal(expected[2], result)
126
127 " With skipstart left at false (default), this should find the prop at line
128 " 5 col 4.
129 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4}, 'b')
130 call assert_equal(expected[2], result)
131
132 " With skipstart set to true, this should skip the prop at line 5 col 4.
133 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4, 'skipstart': 1}, 'b')
134 unlet result.length
135 call assert_equal(expected[1], result)
136
137 " Search backwards from line 1 col 10 to find the prop on the same line.
138 let result = prop_find({'type': 'prop_name', 'lnum': 1, 'col': 10}, 'b')
139 call assert_equal(expected[0], result)
140
141 " with skipstart set to false, if the start position is anywhere between the
142 " start and end lines of a text prop (searching forward or backward), the
143 " result should be the prop on the first line (the line with 'start' set to 1).
144 call cursor(3,1)
145 let result = prop_find({'type': 'prop_name'}, 'f')
146 unlet result.length
147 call assert_equal(expected[1], result)
148 let result = prop_find({'type': 'prop_name'}, 'b')
149 unlet result.length
150 call assert_equal(expected[1], result)
151
152 " with skipstart set to true, if the start position is anywhere between the
153 " start and end lines of a text prop (searching forward or backward), all lines
154 " of the prop will be skipped.
155 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'b')
156 call assert_equal(expected[0], result)
157 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'f')
158 call assert_equal(expected[2], result)
159
160 " Use skipstart to search through all props with type name 'prop_name'.
161 " First forward...
162 let lnum = 1
163 let col = 1
164 let i = 0
165 for exp in expected
166 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'f')
167 if !has_key(exp, "length")
168 unlet result.length
169 endif
170 call assert_equal(exp, result)
171 let lnum = result.lnum
172 let col = result.col
173 let i = i + 1
174 endfor
175
176 " ...then backwards.
177 let lnum = 6
178 let col = 4
179 let i = 2
180 while i >= 0
181 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'b')
182 if !has_key(expected[i], "length")
183 unlet result.length
184 endif
185 call assert_equal(expected[i], result)
186 let lnum = result.lnum
187 let col = result.col
188 let i = i - 1
189 endwhile
190
191 " Starting from line 6 col 1 search backwards for prop with id 10.
192 call cursor(6,1)
193 let result = prop_find({'id': 10, 'skipstart': 1}, 'b')
194 call assert_equal(expected[0], result)
195
196 " Starting from line 1 col 1 search forwards for prop with id 12.
197 call cursor(1,1)
198 let result = prop_find({'id': 12}, 'f')
199 call assert_equal(expected[2], result)
200
201 " Search for a prop with an unknown id.
202 let result = prop_find({'id': 999}, 'f')
203 call assert_equal({}, result)
204
205 " Search backwards from the proceeding position of the prop with id 11
206 " (at line num 2 col 4). This should return an empty dict.
207 let result = prop_find({'id': 11, 'lnum': 2, 'col': 3}, 'b')
208 call assert_equal({}, result)
209
210 " When lnum is given and col is omitted, use column 1.
211 let result = prop_find({'type': 'prop_name', 'lnum': 1}, 'f')
212 call assert_equal(expected[0], result)
213
214 call prop_clear(1,6)
215 call prop_type_delete('prop_name')
216endfunc
217
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100218func Test_prop_add()
219 new
220 call AddPropTypes()
221 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100222 let expected_props = Get_expected_props()
223 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100224 call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:')
225 call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:')
226
227 " Insert a line above, text props must still be there.
228 call append(0, 'empty')
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100229 call assert_equal(expected_props, prop_list(2))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100230 " Delete a line above, text props must still be there.
231 1del
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100232 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100233
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100234 " Prop without length or end column is zero length
235 call prop_clear(1)
236 call prop_add(1, 5, {'type': 'two'})
237 let expected = [{'col': 5, 'length': 0, 'type': 'two', 'id': 0, 'start': 1, 'end': 1}]
238 call assert_equal(expected, prop_list(1))
239
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200240 call assert_fails("call prop_add(1, 5, {'type': 'two', 'bufnr': 234343})", 'E158:')
241
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100242 call DeletePropTypes()
243 bwipe!
244endfunc
245
246func Test_prop_remove()
247 new
248 call AddPropTypes()
249 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100250 let props = Get_expected_props()
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100251 call assert_equal(props, prop_list(1))
252
253 " remove by id
Bram Moolenaara5a78822019-09-04 21:57:18 +0200254 call assert_equal(1, {'id': 12}->prop_remove(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100255 unlet props[2]
256 call assert_equal(props, prop_list(1))
257
258 " remove by type
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200259 call assert_equal(1, prop_remove({'type': 'one'}, 1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100260 unlet props[1]
261 call assert_equal(props, prop_list(1))
262
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200263 " remove from unknown buffer
264 call assert_fails("call prop_remove({'type': 'one', 'bufnr': 123456}, 1)", 'E158:')
265
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100266 call DeletePropTypes()
267 bwipe!
268endfunc
269
Bram Moolenaar196d1572019-01-02 23:47:18 +0100270func SetupOneLine()
271 call setline(1, 'xonex xtwoxx')
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200272 normal gg0
Bram Moolenaar196d1572019-01-02 23:47:18 +0100273 call AddPropTypes()
274 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
275 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
276 let expected = [
277 \ {'col': 2, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
278 \ {'col': 8, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
279 \]
280 call assert_equal(expected, prop_list(1))
281 return expected
282endfunc
283
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100284func Test_prop_add_remove_buf()
285 new
286 let bufnr = bufnr('')
287 call AddPropTypes()
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100288 for lnum in range(1, 4)
289 call setline(lnum, 'one two three')
290 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100291 wincmd w
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100292 for lnum in range(1, 4)
293 call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
294 call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
295 call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
296 endfor
297
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100298 let props = [
299 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
300 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
301 \ {'col': 11, 'length': 3, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
302 \]
303 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100304
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100305 " remove by id
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100306 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100307 unlet props[1]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100308
309 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100310 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100311 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
312 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
313 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
314
315 call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4)
316 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
317 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
318 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
319 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
320
321 call prop_remove({'id': 12, 'bufnr': bufnr})
322 for lnum in range(1, 4)
323 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
324 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100325
326 " remove by type
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100327 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100328 unlet props[0]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100329
330 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100331 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100332 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
333 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
334 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
335
336 call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4)
337 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
338 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
339 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
340 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
341
342 call prop_remove({'type': 'one', 'bufnr': bufnr})
343 for lnum in range(1, 4)
344 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
345 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100346
347 call DeletePropTypes()
348 wincmd w
349 bwipe!
350endfunc
351
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100352func Test_prop_backspace()
353 new
354 set bs=2
Bram Moolenaar196d1572019-01-02 23:47:18 +0100355 let expected = SetupOneLine() " 'xonex xtwoxx'
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100356
357 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
358 call assert_equal('one xtwoxx', getline(1))
359 let expected[0].col = 1
360 let expected[1].col = 6
361 call assert_equal(expected, prop_list(1))
362
363 call DeletePropTypes()
364 bwipe!
365 set bs&
366endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100367
Bram Moolenaar196d1572019-01-02 23:47:18 +0100368func Test_prop_replace()
369 new
370 set bs=2
371 let expected = SetupOneLine() " 'xonex xtwoxx'
372
373 exe "normal 0Ryyy\<Esc>"
374 call assert_equal('yyyex xtwoxx', getline(1))
375 call assert_equal(expected, prop_list(1))
376
377 exe "normal ftRyy\<BS>"
378 call assert_equal('yyyex xywoxx', getline(1))
379 call assert_equal(expected, prop_list(1))
380
381 exe "normal 0fwRyy\<BS>"
382 call assert_equal('yyyex xyyoxx', getline(1))
383 call assert_equal(expected, prop_list(1))
384
385 exe "normal 0foRyy\<BS>\<BS>"
386 call assert_equal('yyyex xyyoxx', getline(1))
387 call assert_equal(expected, prop_list(1))
388
389 call DeletePropTypes()
390 bwipe!
391 set bs&
392endfunc
393
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200394func Test_prop_open_line()
395 new
396
397 " open new line, props stay in top line
398 let expected = SetupOneLine() " 'xonex xtwoxx'
399 exe "normal o\<Esc>"
400 call assert_equal('xonex xtwoxx', getline(1))
401 call assert_equal('', getline(2))
402 call assert_equal(expected, prop_list(1))
403 call DeletePropTypes()
404
405 " move all props to next line
406 let expected = SetupOneLine() " 'xonex xtwoxx'
407 exe "normal 0i\<CR>\<Esc>"
408 call assert_equal('', getline(1))
409 call assert_equal('xonex xtwoxx', getline(2))
410 call assert_equal(expected, prop_list(2))
411 call DeletePropTypes()
412
413 " split just before prop, move all props to next line
414 let expected = SetupOneLine() " 'xonex xtwoxx'
415 exe "normal 0li\<CR>\<Esc>"
416 call assert_equal('x', getline(1))
417 call assert_equal('onex xtwoxx', getline(2))
418 let expected[0].col -= 1
419 let expected[1].col -= 1
420 call assert_equal(expected, prop_list(2))
421 call DeletePropTypes()
422
423 " split inside prop, split first prop
424 let expected = SetupOneLine() " 'xonex xtwoxx'
425 exe "normal 0lli\<CR>\<Esc>"
426 call assert_equal('xo', getline(1))
427 call assert_equal('nex xtwoxx', getline(2))
428 let exp_first = [deepcopy(expected[0])]
429 let exp_first[0].length = 1
430 call assert_equal(exp_first, prop_list(1))
431 let expected[0].col = 1
432 let expected[0].length = 2
433 let expected[1].col -= 2
434 call assert_equal(expected, prop_list(2))
435 call DeletePropTypes()
436
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200437 " split just after first prop, second prop move to next line
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200438 let expected = SetupOneLine() " 'xonex xtwoxx'
439 exe "normal 0fea\<CR>\<Esc>"
440 call assert_equal('xone', getline(1))
441 call assert_equal('x xtwoxx', getline(2))
442 let exp_first = expected[0:0]
443 call assert_equal(exp_first, prop_list(1))
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200444 let expected = expected[1:1]
445 let expected[0].col -= 4
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200446 call assert_equal(expected, prop_list(2))
447 call DeletePropTypes()
448
449 bwipe!
450 set bs&
451endfunc
452
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100453func Test_prop_clear()
454 new
455 call AddPropTypes()
456 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100457 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100458
Bram Moolenaara5a78822019-09-04 21:57:18 +0200459 eval 1->prop_clear()
460 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100461
462 call DeletePropTypes()
463 bwipe!
464endfunc
465
466func Test_prop_clear_buf()
467 new
468 call AddPropTypes()
469 call SetupPropsInFirstLine()
470 let bufnr = bufnr('')
471 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100472 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100473
474 call prop_clear(1, 1, {'bufnr': bufnr})
475 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
476
477 wincmd w
478 call DeletePropTypes()
479 bwipe!
480endfunc
481
Bram Moolenaar21b50382019-01-04 18:07:24 +0100482func Test_prop_setline()
483 new
484 call AddPropTypes()
485 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100486 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100487
488 call setline(1, 'foobar')
489 call assert_equal([], prop_list(1))
490
491 call DeletePropTypes()
492 bwipe!
493endfunc
494
495func Test_prop_setbufline()
496 new
497 call AddPropTypes()
498 call SetupPropsInFirstLine()
499 let bufnr = bufnr('')
500 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100501 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100502
503 call setbufline(bufnr, 1, 'foobar')
504 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
505
506 wincmd w
507 call DeletePropTypes()
508 bwipe!
509endfunc
510
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100511func Test_prop_substitute()
512 new
513 " Set first line to 'one two three'
514 call AddPropTypes()
515 call SetupPropsInFirstLine()
516 let expected_props = Get_expected_props()
517 call assert_equal(expected_props, prop_list(1))
518
519 " Change "n" in "one" to XX: 'oXXe two three'
520 s/n/XX/
521 let expected_props[0].length += 1
522 let expected_props[1].length += 1
523 let expected_props[2].col += 1
524 let expected_props[3].col += 1
525 call assert_equal(expected_props, prop_list(1))
526
527 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
528 s/t//g
529 let expected_props[0].length -= 2
530 let expected_props[2].length -= 1
531 let expected_props[3].length -= 1
532 let expected_props[3].col -= 1
533 call assert_equal(expected_props, prop_list(1))
534
535 " Split the line by changing w to line break: 'oXXe ', 'o hree'
536 " The long prop is split and spans both lines.
537 " The props on "two" and "three" move to the next line.
538 s/w/\r/
539 let new_props = [
540 \ copy(expected_props[0]),
541 \ copy(expected_props[2]),
542 \ copy(expected_props[3]),
543 \ ]
544 let expected_props[0].length = 5
545 unlet expected_props[3]
546 unlet expected_props[2]
547 call assert_equal(expected_props, prop_list(1))
548
549 let new_props[0].length = 6
550 let new_props[1].col = 1
551 let new_props[1].length = 1
552 let new_props[2].col = 3
553 call assert_equal(new_props, prop_list(2))
554
555 call DeletePropTypes()
556 bwipe!
557endfunc
558
Bram Moolenaar663bc892019-01-08 23:07:24 +0100559func Test_prop_change_indent()
560 call prop_type_add('comment', {'highlight': 'Directory'})
561 new
562 call setline(1, [' xxx', 'yyyyy'])
563 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
564 let expect = {'col': 2, 'length': 2, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
565 call assert_equal([expect], prop_list(2))
566
567 set shiftwidth=3
568 normal 2G>>
569 call assert_equal(' yyyyy', getline(2))
570 let expect.col += 3
571 call assert_equal([expect], prop_list(2))
572
573 normal 2G==
574 call assert_equal(' yyyyy', getline(2))
575 let expect.col = 6
576 call assert_equal([expect], prop_list(2))
577
578 call prop_clear(2)
579 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
580 let expect.col = 2
581 let expect.length = 5
582 call assert_equal([expect], prop_list(2))
583
584 normal 2G<<
585 call assert_equal(' yyyyy', getline(2))
586 let expect.length = 2
587 call assert_equal([expect], prop_list(2))
588
589 set shiftwidth&
590 call prop_type_delete('comment')
591endfunc
592
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100593" Setup a three line prop in lines 2 - 4.
594" Add short props in line 1 and 5.
595func Setup_three_line_prop()
596 new
597 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
598 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
599 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
600 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
601endfunc
602
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100603func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200604 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100605 new
606 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
607
608 " start halfway line 1, end halfway line 3
609 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
610 let expect1 = {'col': 3, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
611 call assert_equal([expect1], prop_list(1))
612 let expect2 = {'col': 1, 'length': 10, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
613 call assert_equal([expect2], prop_list(2))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100614 let expect3 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100615 call assert_equal([expect3], prop_list(3))
616 call prop_clear(1, 3)
617
618 " include all three lines
619 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
620 let expect1.col = 1
621 let expect1.length = 8
622 call assert_equal([expect1], prop_list(1))
623 call assert_equal([expect2], prop_list(2))
624 let expect3.length = 9
625 call assert_equal([expect3], prop_list(3))
626 call prop_clear(1, 3)
627
628 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100629
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100630 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100631 call Setup_three_line_prop()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100632 let expect_short = {'col': 2, 'length': 1, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
633 call assert_equal([expect_short], prop_list(1))
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100634 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
635 call assert_equal([expect2], prop_list(2))
636 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100637 call assert_equal([expect_short], prop_list(1))
638 let expect2 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
639 call assert_equal([expect2], prop_list(2))
640 bwipe!
641
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100642 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100643 call Setup_three_line_prop()
644 let expect3 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
645 call assert_equal([expect3], prop_list(3))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100646 let expect4 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100647 call assert_equal([expect4], prop_list(4))
648 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100649 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100650 call assert_equal([expect3], prop_list(3))
651 call assert_equal([expect_short], prop_list(4))
652 bwipe!
653
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100654 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100655 call Setup_three_line_prop()
656 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
657 call assert_equal([expect2], prop_list(2))
658 call append(2, "new line")
659 call assert_equal([expect2], prop_list(2))
660 let expect3 = {'col': 1, 'length': 9, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
661 call assert_equal([expect3], prop_list(3))
662 bwipe!
663
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100664 call prop_type_delete('comment')
665endfunc
666
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100667func Test_prop_byteoff()
668 call prop_type_add('comment', {'highlight': 'Directory'})
669 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100670 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100671 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100672 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100673 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100674 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100675
676 bwipe!
677 call prop_type_delete('comment')
678endfunc
679
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100680func Test_prop_undo()
681 new
682 call prop_type_add('comment', {'highlight': 'Directory'})
683 call setline(1, ['oneone', 'twotwo', 'three'])
684 " Set 'undolevels' to break changes into undo-able pieces.
685 set ul&
686
687 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
688 let expected = [{'col': 3, 'length': 2, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
689 call assert_equal(expected, prop_list(1))
690
691 " Insert a character, then undo.
692 exe "normal 0lllix\<Esc>"
693 set ul&
694 let expected[0].length = 3
695 call assert_equal(expected, prop_list(1))
696 undo
697 let expected[0].length = 2
698 call assert_equal(expected, prop_list(1))
699
700 " Delete a character, then undo
701 exe "normal 0lllx"
702 set ul&
703 let expected[0].length = 1
704 call assert_equal(expected, prop_list(1))
705 undo
706 let expected[0].length = 2
707 call assert_equal(expected, prop_list(1))
708
709 " Delete the line, then undo
710 1d
711 set ul&
712 call assert_equal([], prop_list(1))
713 undo
714 call assert_equal(expected, prop_list(1))
715
716 " Insert a character, delete two characters, then undo with "U"
717 exe "normal 0lllix\<Esc>"
718 set ul&
719 let expected[0].length = 3
720 call assert_equal(expected, prop_list(1))
721 exe "normal 0lllxx"
722 set ul&
723 let expected[0].length = 1
724 call assert_equal(expected, prop_list(1))
725 normal U
726 let expected[0].length = 2
727 call assert_equal(expected, prop_list(1))
728
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200729 " substitute a word, then undo
730 call setline(1, 'the number 123 is highlighted.')
731 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
732 let expected = [{'col': 12, 'length': 3, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
733 call assert_equal(expected, prop_list(1))
734 set ul&
735 1s/number/foo
736 let expected[0].col = 9
737 call assert_equal(expected, prop_list(1))
738 undo
739 let expected[0].col = 12
740 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200741 call prop_clear(1)
742
743 " substitute with backslash
744 call setline(1, 'the number 123 is highlighted.')
745 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
746 let expected = [{'col': 12, 'length': 3, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
747 call assert_equal(expected, prop_list(1))
748 1s/the/\The
749 call assert_equal(expected, prop_list(1))
750 1s/^/\\
751 let expected[0].col += 1
752 call assert_equal(expected, prop_list(1))
753 1s/^/\~
754 let expected[0].col += 1
755 call assert_equal(expected, prop_list(1))
756 1s/123/12\\3
757 let expected[0].length += 1
758 call assert_equal(expected, prop_list(1))
759 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200760
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100761 bwipe!
762 call prop_type_delete('comment')
763endfunc
764
Bram Moolenaarecafcc12019-11-16 20:41:51 +0100765func Test_prop_delete_text()
766 new
767 call prop_type_add('comment', {'highlight': 'Directory'})
768 call setline(1, ['oneone', 'twotwo', 'three'])
769
770 " zero length property
771 call prop_add(1, 3, {'type': 'comment'})
772 let expected = [{'col': 3, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
773 call assert_equal(expected, prop_list(1))
774
775 " delete one char moves the property
776 normal! x
777 let expected = [{'col': 2, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
778 call assert_equal(expected, prop_list(1))
779
780 " delete char of the property has no effect
781 normal! lx
782 let expected = [{'col': 2, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
783 call assert_equal(expected, prop_list(1))
784
785 " delete more chars moves property to first column, is not deleted
786 normal! 0xxxx
787 let expected = [{'col': 1, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
788 call assert_equal(expected, prop_list(1))
789
790 bwipe!
791 call prop_type_delete('comment')
792endfunc
793
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100794" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +0200795func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +0200796 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +0100797 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +0200798 if g:orig_encoding != 'utf-8'
799 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100800 endif
801 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +0200802 \ "call setline(1, ["
803 \ .. "'One two',"
804 \ .. "'Numbér 123 änd thœn 4¾7.',"
805 \ .. "'--aa--bb--cc--dd--',"
806 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200807 \ .. "'first line',"
808 \ .. "' second line ',"
809 \ .. "'third line',"
810 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +0200811 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100812 \ "hi NumberProp ctermfg=blue",
813 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200814 \ "hi BackgroundProp ctermbg=lightgrey",
815 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100816 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +0200817 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
818 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100819 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
820 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
821 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +0100822 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
823 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
824 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +0100825 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100826 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100827 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
828 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100829 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
830 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
831 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
832 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +0100833 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
834 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200835 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200836 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
837 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
838 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
839 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +0200840 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100841 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100842 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200843 \ "syn match Comment '//.*'",
844 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100845 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100846 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200847 \ "normal 6G0i\<BS>\<Esc>",
848 \ "normal 3J",
849 \ "normal 3G",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100850 \], 'XtestProp')
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200851 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100852 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100853
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100854 " clean up
855 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +0100856 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100857endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +0200858
859func RunTestVisualBlock(width, dump)
860 call writefile([
861 \ "call setline(1, ["
862 \ .. "'xxxxxxxxx 123 x',"
863 \ .. "'xxxxxxxx 123 x',"
864 \ .. "'xxxxxxx 123 x',"
865 \ .. "'xxxxxx 123 x',"
866 \ .. "'xxxxx 123 x',"
867 \ .. "'xxxx 123 xx',"
868 \ .. "'xxx 123 xxx',"
869 \ .. "'xx 123 xxxx',"
870 \ .. "'x 123 xxxxx',"
871 \ .. "' 123 xxxxxx',"
872 \ .. "])",
873 \ "hi SearchProp ctermbg=yellow",
874 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
875 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
876 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
877 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
878 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
879 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
880 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
881 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
882 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
883 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
884 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
885 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
886 \], 'XtestPropVis')
887 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
888 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
889
890 " clean up
891 call StopVimInTerminal(buf)
892 call delete('XtestPropVis')
893endfunc
894
895" screenshot test with Visual block mode operations
896func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +0200897 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +0200898
899 " Delete two columns while text props are three chars wide.
900 call RunTestVisualBlock(2, '01')
901
902 " Same, but delete four columns
903 call RunTestVisualBlock(4, '02')
904endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +0200905
Bram Moolenaara956bf62019-06-19 17:34:24 +0200906func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +0200907 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +0200908
Bram Moolenaara956bf62019-06-19 17:34:24 +0200909 let lines =<< trim END
910 call setline(1, [
911 \ "\txxx",
912 \ "x\txxx",
913 \ ])
914 hi SearchProp ctermbg=yellow
915 call prop_type_add('search', {'highlight': 'SearchProp'})
916 call prop_add(1, 2, {'length': 3, 'type': 'search'})
917 call prop_add(2, 3, {'length': 3, 'type': 'search'})
918 END
919 call writefile(lines, 'XtestPropTab')
920 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
921 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
922
923 " clean up
924 call StopVimInTerminal(buf)
925 call delete('XtestPropTab')
926endfunc
927
Bram Moolenaar34390282019-10-16 14:38:26 +0200928func Test_textprop_with_syntax()
929 CheckScreendump
930
931 let lines =<< trim END
932 call setline(1, [
933 \ "(abc)",
934 \ ])
935 syn match csParens "[()]" display
936 hi! link csParens MatchParen
937
938 call prop_type_add('TPTitle', #{ highlight: 'Title' })
939 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
940 END
941 call writefile(lines, 'XtestPropSyn')
942 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
943 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
944
945 " clean up
946 call StopVimInTerminal(buf)
947 call delete('XtestPropSyn')
948endfunc
949
Bram Moolenaard79eef22019-05-24 20:41:55 +0200950" Adding a text property to a new buffer should not fail
951func Test_textprop_empty_buffer()
952 call prop_type_add('comment', {'highlight': 'Search'})
953 new
954 call prop_add(1, 1, {'type': 'comment'})
955 close
Bram Moolenaaradfde112019-05-25 22:11:45 +0200956 call prop_type_delete('comment')
957endfunc
958
Bram Moolenaard74af422019-06-28 21:38:00 +0200959" Adding a text property with invalid highlight should be ignored.
960func Test_textprop_invalid_highlight()
961 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
962 new
963 call setline(1, ['asdf','asdf'])
964 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
965 redraw
966 bwipe!
967 call prop_type_delete('dni')
968endfunc
969
Bram Moolenaaradfde112019-05-25 22:11:45 +0200970" Adding a text property to an empty buffer and then editing another
971func Test_textprop_empty_buffer_next()
972 call prop_type_add("xxx", {})
973 call prop_add(1, 1, {"type": "xxx"})
974 next X
975 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +0200976endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200977
978func Test_textprop_remove_from_buf()
979 new
980 let buf = bufnr('')
981 call prop_type_add('one', {'bufnr': buf})
982 call prop_add(1, 1, {'type': 'one', 'id': 234})
983 file x
984 edit y
985 call prop_remove({'id': 234, 'bufnr': buf}, 1)
986 call prop_type_delete('one', {'bufnr': buf})
987 bwipe! x
988 close
989endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +0200990
991func Test_textprop_in_unloaded_buf()
992 edit Xaaa
993 call setline(1, 'aaa')
994 write
995 edit Xbbb
996 call setline(1, 'bbb')
997 write
998 let bnr = bufnr('')
999 edit Xaaa
1000
1001 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
1002 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
1003 exe 'buf ' .. bnr
1004 call assert_equal('bbb', getline(1))
1005 call assert_equal(0, prop_list(1)->len())
1006
1007 bwipe! Xaaa
1008 bwipe! Xbbb
1009 cal delete('Xaaa')
1010 cal delete('Xbbb')
1011endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001012
1013func Test_proptype_substitute2()
1014 new
1015 " text_prop.vim
1016 call setline(1, [
1017 \ 'The num 123 is smaller than 4567.',
1018 \ '123 The number 123 is smaller than 4567.',
1019 \ '123 The number 123 is smaller than 4567.'])
1020
1021 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1022
1023 call prop_add(1, 12, {'length': 3, 'type': 'number'})
1024 call prop_add(2, 1, {'length': 3, 'type': 'number'})
1025 call prop_add(3, 36, {'length': 4, 'type': 'number'})
1026 set ul&
1027 let expected = [{'id': 0, 'col': 13, 'end': 1, 'type': 'number', 'length': 3, 'start': 1},
1028 \ {'id': 0, 'col': 1, 'end': 1, 'type': 'number', 'length': 3, 'start': 1},
1029 \ {'id': 0, 'col': 50, 'end': 1, 'type': 'number', 'length': 4, 'start': 1}]
1030 " Add some text in between
1031 %s/\s\+/ /g
1032 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
1033
1034 " remove some text
1035 :1s/[a-z]\{3\}//g
1036 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
1037 call assert_equal(expected, prop_list(1))
1038 bwipe!
1039endfunc
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001040
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001041func SaveOptions()
1042 let d = #{tabstop: &tabstop,
1043 \ softtabstop: &softtabstop,
1044 \ shiftwidth: &shiftwidth,
1045 \ expandtab: &expandtab,
1046 \ foldmethod: '"' .. &foldmethod .. '"',
1047 \ }
1048 return d
1049endfunc
1050
1051func RestoreOptions(dict)
1052 for name in keys(a:dict)
1053 exe 'let &' .. name .. ' = ' .. a:dict[name]
1054 endfor
1055endfunc
1056
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001057func Test_textprop_noexpandtab()
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001058 new
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001059 let save_dict = SaveOptions()
1060
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001061 set tabstop=8
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001062 set softtabstop=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001063 set shiftwidth=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001064 set noexpandtab
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001065 set foldmethod=marker
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001066
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001067 call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx")
1068 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1069 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1070 call feedkeys("0i\<tab>", "tx")
1071 call prop_remove({'type': 'test'})
1072 call prop_add(1, 2, {'end_col': 3, 'type': 'test'})
1073 call feedkeys("A\<left>\<tab>", "tx")
1074 call prop_remove({'type': 'test'})
1075 try
1076 " It is correct that this does not pass
1077 call prop_add(1, 6, {'end_col': 7, 'type': 'test'})
1078 " Has already collapsed here, start_col:6 does not result in an error
1079 call feedkeys("A\<left>\<tab>", "tx")
1080 catch /^Vim\%((\a\+)\)\=:E964/
1081 endtry
1082 call prop_remove({'type': 'test'})
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001083 call prop_type_delete('test')
1084
1085 call RestoreOptions(save_dict)
1086 bwipe!
1087endfunc
1088
1089func Test_textprop_noexpandtab_redraw()
1090 new
1091 let save_dict = SaveOptions()
1092
1093 set tabstop=8
1094 set softtabstop=4
1095 set shiftwidth=4
1096 set noexpandtab
1097 set foldmethod=marker
1098
1099 call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx")
1100 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1101 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1102 call feedkeys("0i\<tab>", "tx")
1103 " Internally broken at the next line
1104 call feedkeys("A\<left>\<tab>", "tx")
1105 redraw
1106 " Index calculation failed internally on next line
1107 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1108 call prop_remove({'type': 'test', 'all': v:true})
1109 call prop_type_delete('test')
1110 call prop_type_delete('test')
1111
1112 call RestoreOptions(save_dict)
1113 bwipe!
1114endfunc
1115
1116func Test_textprop_ins_str()
1117 new
1118 call setline(1, 'just some text')
1119 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1120 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1121 call assert_equal([{'id': 0, 'col': 1, 'end': 1, 'type': 'test', 'length': 1, 'start': 1}], prop_list(1))
1122
1123 call feedkeys("foi\<F8>\<Esc>", "tx")
1124 call assert_equal('just s<F8>ome text', getline(1))
1125 call assert_equal([{'id': 0, 'col': 1, 'end': 1, 'type': 'test', 'length': 1, 'start': 1}], prop_list(1))
1126
1127 bwipe!
1128 call prop_remove({'type': 'test'})
1129 call prop_type_delete('test')
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001130endfunc