blob: 6d3aaa3be0870c1e1f785eaf79964383b075ea5b [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 Moolenaar98aefe72018-12-13 22:20:09 +01009func Test_proptype_global()
10 call prop_type_add('comment', {'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
11 let proptypes = prop_type_list()
12 call assert_equal(1, len(proptypes))
13 call assert_equal('comment', proptypes[0])
14
15 let proptype = prop_type_get('comment')
16 call assert_equal('Directory', proptype['highlight'])
17 call assert_equal(123, proptype['priority'])
18 call assert_equal(1, proptype['start_incl'])
19 call assert_equal(1, proptype['end_incl'])
20
21 call prop_type_delete('comment')
22 call assert_equal(0, len(prop_type_list()))
23
24 call prop_type_add('one', {})
25 call assert_equal(1, len(prop_type_list()))
Bram Moolenaara5a78822019-09-04 21:57:18 +020026 let proptype = 'one'->prop_type_get()
Bram Moolenaar98aefe72018-12-13 22:20:09 +010027 call assert_false(has_key(proptype, 'highlight'))
28 call assert_equal(0, proptype['priority'])
29 call assert_equal(0, proptype['start_incl'])
30 call assert_equal(0, proptype['end_incl'])
31
32 call prop_type_add('two', {})
33 call assert_equal(2, len(prop_type_list()))
34 call prop_type_delete('one')
35 call assert_equal(1, len(prop_type_list()))
36 call prop_type_delete('two')
37 call assert_equal(0, len(prop_type_list()))
38endfunc
39
40func Test_proptype_buf()
41 let bufnr = bufnr('')
42 call prop_type_add('comment', {'bufnr': bufnr, 'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
43 let proptypes = prop_type_list({'bufnr': bufnr})
44 call assert_equal(1, len(proptypes))
45 call assert_equal('comment', proptypes[0])
46
47 let proptype = prop_type_get('comment', {'bufnr': bufnr})
48 call assert_equal('Directory', proptype['highlight'])
49 call assert_equal(123, proptype['priority'])
50 call assert_equal(1, proptype['start_incl'])
51 call assert_equal(1, proptype['end_incl'])
52
53 call prop_type_delete('comment', {'bufnr': bufnr})
Bram Moolenaara5a78822019-09-04 21:57:18 +020054 call assert_equal(0, len({'bufnr': bufnr}->prop_type_list()))
Bram Moolenaar98aefe72018-12-13 22:20:09 +010055
56 call prop_type_add('one', {'bufnr': bufnr})
57 let proptype = prop_type_get('one', {'bufnr': bufnr})
58 call assert_false(has_key(proptype, 'highlight'))
59 call assert_equal(0, proptype['priority'])
60 call assert_equal(0, proptype['start_incl'])
61 call assert_equal(0, proptype['end_incl'])
62
63 call prop_type_add('two', {'bufnr': bufnr})
64 call assert_equal(2, len(prop_type_list({'bufnr': bufnr})))
65 call prop_type_delete('one', {'bufnr': bufnr})
66 call assert_equal(1, len(prop_type_list({'bufnr': bufnr})))
67 call prop_type_delete('two', {'bufnr': bufnr})
68 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
Bram Moolenaarf0884c52019-05-24 21:22:29 +020069
70 call assert_fails("call prop_type_add('one', {'bufnr': 98764})", "E158:")
Bram Moolenaar98aefe72018-12-13 22:20:09 +010071endfunc
72
73func AddPropTypes()
74 call prop_type_add('one', {})
75 call prop_type_add('two', {})
76 call prop_type_add('three', {})
77 call prop_type_add('whole', {})
78endfunc
79
80func DeletePropTypes()
81 call prop_type_delete('one')
82 call prop_type_delete('two')
83 call prop_type_delete('three')
84 call prop_type_delete('whole')
85endfunc
86
87func SetupPropsInFirstLine()
88 call setline(1, 'one two three')
89 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
Bram Moolenaara5a78822019-09-04 21:57:18 +020090 eval 1->prop_add(5, {'length': 3, 'id': 12, 'type': 'two'})
Bram Moolenaar4164bb22019-01-04 23:09:49 +010091 call prop_add(1, 9, {'length': 5, 'id': 13, 'type': 'three'})
Bram Moolenaar98aefe72018-12-13 22:20:09 +010092 call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
93endfunc
94
Bram Moolenaar4164bb22019-01-04 23:09:49 +010095func Get_expected_props()
96 return [
97 \ {'col': 1, 'length': 13, 'id': 14, 'type': 'whole', 'start': 1, 'end': 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +010098 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
99 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100100 \ {'col': 9, 'length': 5, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100101 \ ]
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100102endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100103
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100104func Test_prop_find()
105 new
106 call setline(1, ['one one one', 'twotwo', 'three', 'fourfour', 'five', 'sixsix'])
107
108 " Add two text props on lines 1 and 5, and one spanning lines 2 to 4.
109 call prop_type_add('prop_name', {'highlight': 'Directory'})
110 call prop_add(1, 5, {'type': 'prop_name', 'id': 10, 'length': 3})
111 call prop_add(2, 4, {'type': 'prop_name', 'id': 11, 'end_lnum': 4, 'end_col': 9})
112 call prop_add(5, 4, {'type': 'prop_name', 'id': 12, 'length': 1})
113
114 let expected = [
115 \ {'lnum': 1, 'col': 5, 'length': 3, 'id': 10, 'type': 'prop_name', 'start': 1, 'end': 1},
116 \ {'lnum': 2, 'col': 4, 'id': 11, 'type': 'prop_name', 'start': 1, 'end': 0},
117 \ {'lnum': 5, 'col': 4, 'length': 1, 'id': 12, 'type': 'prop_name', 'start': 1, 'end': 1}
118 \ ]
119
120 " Starting at line 5 col 1 this should find the prop at line 5 col 4.
121 call cursor(5,1)
122 let result = prop_find({'type': 'prop_name'}, 'f')
123 call assert_equal(expected[2], result)
124
125 " With skipstart left at false (default), this should find the prop at line
126 " 5 col 4.
127 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4}, 'b')
128 call assert_equal(expected[2], result)
129
130 " With skipstart set to true, this should skip the prop at line 5 col 4.
131 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4, 'skipstart': 1}, 'b')
132 unlet result.length
133 call assert_equal(expected[1], result)
134
135 " Search backwards from line 1 col 10 to find the prop on the same line.
136 let result = prop_find({'type': 'prop_name', 'lnum': 1, 'col': 10}, 'b')
137 call assert_equal(expected[0], result)
138
139 " with skipstart set to false, if the start position is anywhere between the
140 " start and end lines of a text prop (searching forward or backward), the
141 " result should be the prop on the first line (the line with 'start' set to 1).
142 call cursor(3,1)
143 let result = prop_find({'type': 'prop_name'}, 'f')
144 unlet result.length
145 call assert_equal(expected[1], result)
146 let result = prop_find({'type': 'prop_name'}, 'b')
147 unlet result.length
148 call assert_equal(expected[1], result)
149
150 " with skipstart set to true, if the start position is anywhere between the
151 " start and end lines of a text prop (searching forward or backward), all lines
152 " of the prop will be skipped.
153 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'b')
154 call assert_equal(expected[0], result)
155 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'f')
156 call assert_equal(expected[2], result)
157
158 " Use skipstart to search through all props with type name 'prop_name'.
159 " First forward...
160 let lnum = 1
161 let col = 1
162 let i = 0
163 for exp in expected
164 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'f')
165 if !has_key(exp, "length")
166 unlet result.length
167 endif
168 call assert_equal(exp, result)
169 let lnum = result.lnum
170 let col = result.col
171 let i = i + 1
172 endfor
173
174 " ...then backwards.
175 let lnum = 6
176 let col = 4
177 let i = 2
178 while i >= 0
179 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'b')
180 if !has_key(expected[i], "length")
181 unlet result.length
182 endif
183 call assert_equal(expected[i], result)
184 let lnum = result.lnum
185 let col = result.col
186 let i = i - 1
187 endwhile
188
189 " Starting from line 6 col 1 search backwards for prop with id 10.
190 call cursor(6,1)
191 let result = prop_find({'id': 10, 'skipstart': 1}, 'b')
192 call assert_equal(expected[0], result)
193
194 " Starting from line 1 col 1 search forwards for prop with id 12.
195 call cursor(1,1)
196 let result = prop_find({'id': 12}, 'f')
197 call assert_equal(expected[2], result)
198
199 " Search for a prop with an unknown id.
200 let result = prop_find({'id': 999}, 'f')
201 call assert_equal({}, result)
202
203 " Search backwards from the proceeding position of the prop with id 11
204 " (at line num 2 col 4). This should return an empty dict.
205 let result = prop_find({'id': 11, 'lnum': 2, 'col': 3}, 'b')
206 call assert_equal({}, result)
207
208 " When lnum is given and col is omitted, use column 1.
209 let result = prop_find({'type': 'prop_name', 'lnum': 1}, 'f')
210 call assert_equal(expected[0], result)
211
212 call prop_clear(1,6)
213 call prop_type_delete('prop_name')
214endfunc
215
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100216func Test_prop_add()
217 new
218 call AddPropTypes()
219 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100220 let expected_props = Get_expected_props()
221 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100222 call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:')
223 call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:')
224
225 " Insert a line above, text props must still be there.
226 call append(0, 'empty')
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100227 call assert_equal(expected_props, prop_list(2))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100228 " Delete a line above, text props must still be there.
229 1del
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100230 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100231
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100232 " Prop without length or end column is zero length
233 call prop_clear(1)
Bram Moolenaar12f20032020-02-26 22:06:00 +0100234 call prop_type_add('included', {'start_incl': 1, 'end_incl': 1})
235 call prop_add(1, 5, #{type: 'included'})
236 let expected = [#{col: 5, length: 0, type: 'included', id: 0, start: 1, end: 1}]
237 call assert_equal(expected, prop_list(1))
238
239 " Inserting text makes the prop bigger.
240 exe "normal 5|ixx\<Esc>"
241 let expected = [#{col: 5, length: 2, type: 'included', id: 0, start: 1, end: 1}]
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100242 call assert_equal(expected, prop_list(1))
243
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200244 call assert_fails("call prop_add(1, 5, {'type': 'two', 'bufnr': 234343})", 'E158:')
245
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100246 call DeletePropTypes()
Bram Moolenaar12f20032020-02-26 22:06:00 +0100247 call prop_type_delete('included')
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100248 bwipe!
249endfunc
250
251func Test_prop_remove()
252 new
253 call AddPropTypes()
254 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100255 let props = Get_expected_props()
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100256 call assert_equal(props, prop_list(1))
257
258 " remove by id
Bram Moolenaara5a78822019-09-04 21:57:18 +0200259 call assert_equal(1, {'id': 12}->prop_remove(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100260 unlet props[2]
261 call assert_equal(props, prop_list(1))
262
263 " remove by type
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200264 call assert_equal(1, prop_remove({'type': 'one'}, 1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100265 unlet props[1]
266 call assert_equal(props, prop_list(1))
267
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200268 " remove from unknown buffer
269 call assert_fails("call prop_remove({'type': 'one', 'bufnr': 123456}, 1)", 'E158:')
270
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100271 call DeletePropTypes()
272 bwipe!
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100273
274 new
275 call AddPropTypes()
276 call SetupPropsInFirstLine()
277 call prop_add(1, 6, {'length': 2, 'id': 11, 'type': 'three'})
278 let props = Get_expected_props()
279 call insert(props, {'col': 6, 'length': 2, 'id': 11, 'type': 'three', 'start': 1, 'end': 1}, 3)
280 call assert_equal(props, prop_list(1))
281 call assert_equal(1, prop_remove({'type': 'three', 'id': 11, 'both': 1, 'all': 1}, 1))
282 unlet props[3]
283 call assert_equal(props, prop_list(1))
284
285 call assert_fails("call prop_remove({'id': 11, 'both': 1})", 'E860')
286 call assert_fails("call prop_remove({'type': 'three', 'both': 1})", 'E860')
287
288 call DeletePropTypes()
289 bwipe!
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100290endfunc
291
Bram Moolenaar196d1572019-01-02 23:47:18 +0100292func SetupOneLine()
293 call setline(1, 'xonex xtwoxx')
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200294 normal gg0
Bram Moolenaar196d1572019-01-02 23:47:18 +0100295 call AddPropTypes()
296 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
297 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
298 let expected = [
299 \ {'col': 2, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
300 \ {'col': 8, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
301 \]
302 call assert_equal(expected, prop_list(1))
303 return expected
304endfunc
305
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100306func Test_prop_add_remove_buf()
307 new
308 let bufnr = bufnr('')
309 call AddPropTypes()
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100310 for lnum in range(1, 4)
311 call setline(lnum, 'one two three')
312 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100313 wincmd w
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100314 for lnum in range(1, 4)
315 call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
316 call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
317 call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
318 endfor
319
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100320 let props = [
321 \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1},
322 \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1},
323 \ {'col': 11, 'length': 3, 'id': 13, 'type': 'three', 'start': 1, 'end': 1},
324 \]
325 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100326
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100327 " remove by id
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100328 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100329 unlet props[1]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100330
331 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100332 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100333 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
334 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
335 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
336
337 call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4)
338 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
339 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
340 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
341 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
342
343 call prop_remove({'id': 12, 'bufnr': bufnr})
344 for lnum in range(1, 4)
345 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
346 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100347
348 " remove by type
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100349 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100350 unlet props[0]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100351
352 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100353 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100354 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
355 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
356 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
357
358 call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4)
359 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
360 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
361 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
362 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
363
364 call prop_remove({'type': 'one', 'bufnr': bufnr})
365 for lnum in range(1, 4)
366 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
367 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100368
369 call DeletePropTypes()
370 wincmd w
371 bwipe!
372endfunc
373
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100374func Test_prop_backspace()
375 new
376 set bs=2
Bram Moolenaar196d1572019-01-02 23:47:18 +0100377 let expected = SetupOneLine() " 'xonex xtwoxx'
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100378
379 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
380 call assert_equal('one xtwoxx', getline(1))
381 let expected[0].col = 1
382 let expected[1].col = 6
383 call assert_equal(expected, prop_list(1))
384
385 call DeletePropTypes()
386 bwipe!
387 set bs&
388endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100389
Bram Moolenaar196d1572019-01-02 23:47:18 +0100390func Test_prop_replace()
391 new
392 set bs=2
393 let expected = SetupOneLine() " 'xonex xtwoxx'
394
395 exe "normal 0Ryyy\<Esc>"
396 call assert_equal('yyyex xtwoxx', getline(1))
397 call assert_equal(expected, prop_list(1))
398
399 exe "normal ftRyy\<BS>"
400 call assert_equal('yyyex xywoxx', getline(1))
401 call assert_equal(expected, prop_list(1))
402
403 exe "normal 0fwRyy\<BS>"
404 call assert_equal('yyyex xyyoxx', getline(1))
405 call assert_equal(expected, prop_list(1))
406
407 exe "normal 0foRyy\<BS>\<BS>"
408 call assert_equal('yyyex xyyoxx', getline(1))
409 call assert_equal(expected, prop_list(1))
410
411 call DeletePropTypes()
412 bwipe!
413 set bs&
414endfunc
415
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200416func Test_prop_open_line()
417 new
418
419 " open new line, props stay in top line
420 let expected = SetupOneLine() " 'xonex xtwoxx'
421 exe "normal o\<Esc>"
422 call assert_equal('xonex xtwoxx', getline(1))
423 call assert_equal('', getline(2))
424 call assert_equal(expected, prop_list(1))
425 call DeletePropTypes()
426
427 " move all props to next line
428 let expected = SetupOneLine() " 'xonex xtwoxx'
429 exe "normal 0i\<CR>\<Esc>"
430 call assert_equal('', getline(1))
431 call assert_equal('xonex xtwoxx', getline(2))
432 call assert_equal(expected, prop_list(2))
433 call DeletePropTypes()
434
435 " split just before prop, move all props to next line
436 let expected = SetupOneLine() " 'xonex xtwoxx'
437 exe "normal 0li\<CR>\<Esc>"
438 call assert_equal('x', getline(1))
439 call assert_equal('onex xtwoxx', getline(2))
440 let expected[0].col -= 1
441 let expected[1].col -= 1
442 call assert_equal(expected, prop_list(2))
443 call DeletePropTypes()
444
445 " split inside prop, split first prop
446 let expected = SetupOneLine() " 'xonex xtwoxx'
447 exe "normal 0lli\<CR>\<Esc>"
448 call assert_equal('xo', getline(1))
449 call assert_equal('nex xtwoxx', getline(2))
450 let exp_first = [deepcopy(expected[0])]
451 let exp_first[0].length = 1
452 call assert_equal(exp_first, prop_list(1))
453 let expected[0].col = 1
454 let expected[0].length = 2
455 let expected[1].col -= 2
456 call assert_equal(expected, prop_list(2))
457 call DeletePropTypes()
458
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200459 " split just after first prop, second prop move to next line
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200460 let expected = SetupOneLine() " 'xonex xtwoxx'
461 exe "normal 0fea\<CR>\<Esc>"
462 call assert_equal('xone', getline(1))
463 call assert_equal('x xtwoxx', getline(2))
464 let exp_first = expected[0:0]
465 call assert_equal(exp_first, prop_list(1))
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200466 let expected = expected[1:1]
467 let expected[0].col -= 4
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200468 call assert_equal(expected, prop_list(2))
469 call DeletePropTypes()
470
471 bwipe!
472 set bs&
473endfunc
474
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100475func Test_prop_clear()
476 new
477 call AddPropTypes()
478 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100479 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100480
Bram Moolenaara5a78822019-09-04 21:57:18 +0200481 eval 1->prop_clear()
482 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100483
484 call DeletePropTypes()
485 bwipe!
486endfunc
487
488func Test_prop_clear_buf()
489 new
490 call AddPropTypes()
491 call SetupPropsInFirstLine()
492 let bufnr = bufnr('')
493 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100494 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100495
496 call prop_clear(1, 1, {'bufnr': bufnr})
497 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
498
499 wincmd w
500 call DeletePropTypes()
501 bwipe!
502endfunc
503
Bram Moolenaar21b50382019-01-04 18:07:24 +0100504func Test_prop_setline()
505 new
506 call AddPropTypes()
507 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100508 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100509
510 call setline(1, 'foobar')
511 call assert_equal([], prop_list(1))
512
513 call DeletePropTypes()
514 bwipe!
515endfunc
516
517func Test_prop_setbufline()
518 new
519 call AddPropTypes()
520 call SetupPropsInFirstLine()
521 let bufnr = bufnr('')
522 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100523 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100524
525 call setbufline(bufnr, 1, 'foobar')
526 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
527
528 wincmd w
529 call DeletePropTypes()
530 bwipe!
531endfunc
532
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100533func Test_prop_substitute()
534 new
535 " Set first line to 'one two three'
536 call AddPropTypes()
537 call SetupPropsInFirstLine()
538 let expected_props = Get_expected_props()
539 call assert_equal(expected_props, prop_list(1))
540
541 " Change "n" in "one" to XX: 'oXXe two three'
542 s/n/XX/
543 let expected_props[0].length += 1
544 let expected_props[1].length += 1
545 let expected_props[2].col += 1
546 let expected_props[3].col += 1
547 call assert_equal(expected_props, prop_list(1))
548
549 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
550 s/t//g
551 let expected_props[0].length -= 2
552 let expected_props[2].length -= 1
553 let expected_props[3].length -= 1
554 let expected_props[3].col -= 1
555 call assert_equal(expected_props, prop_list(1))
556
557 " Split the line by changing w to line break: 'oXXe ', 'o hree'
558 " The long prop is split and spans both lines.
559 " The props on "two" and "three" move to the next line.
560 s/w/\r/
561 let new_props = [
562 \ copy(expected_props[0]),
563 \ copy(expected_props[2]),
564 \ copy(expected_props[3]),
565 \ ]
566 let expected_props[0].length = 5
567 unlet expected_props[3]
568 unlet expected_props[2]
569 call assert_equal(expected_props, prop_list(1))
570
571 let new_props[0].length = 6
572 let new_props[1].col = 1
573 let new_props[1].length = 1
574 let new_props[2].col = 3
575 call assert_equal(new_props, prop_list(2))
576
577 call DeletePropTypes()
578 bwipe!
579endfunc
580
Bram Moolenaar663bc892019-01-08 23:07:24 +0100581func Test_prop_change_indent()
582 call prop_type_add('comment', {'highlight': 'Directory'})
583 new
584 call setline(1, [' xxx', 'yyyyy'])
585 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
586 let expect = {'col': 2, 'length': 2, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
587 call assert_equal([expect], prop_list(2))
588
589 set shiftwidth=3
590 normal 2G>>
591 call assert_equal(' yyyyy', getline(2))
592 let expect.col += 3
593 call assert_equal([expect], prop_list(2))
594
595 normal 2G==
596 call assert_equal(' yyyyy', getline(2))
597 let expect.col = 6
598 call assert_equal([expect], prop_list(2))
599
600 call prop_clear(2)
601 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
602 let expect.col = 2
603 let expect.length = 5
604 call assert_equal([expect], prop_list(2))
605
606 normal 2G<<
607 call assert_equal(' yyyyy', getline(2))
608 let expect.length = 2
609 call assert_equal([expect], prop_list(2))
610
611 set shiftwidth&
612 call prop_type_delete('comment')
613endfunc
614
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100615" Setup a three line prop in lines 2 - 4.
616" Add short props in line 1 and 5.
617func Setup_three_line_prop()
618 new
619 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
620 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
621 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
622 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
623endfunc
624
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100625func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200626 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100627 new
628 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
629
630 " start halfway line 1, end halfway line 3
631 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
632 let expect1 = {'col': 3, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
633 call assert_equal([expect1], prop_list(1))
634 let expect2 = {'col': 1, 'length': 10, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
635 call assert_equal([expect2], prop_list(2))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100636 let expect3 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100637 call assert_equal([expect3], prop_list(3))
638 call prop_clear(1, 3)
639
640 " include all three lines
641 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
642 let expect1.col = 1
643 let expect1.length = 8
644 call assert_equal([expect1], prop_list(1))
645 call assert_equal([expect2], prop_list(2))
646 let expect3.length = 9
647 call assert_equal([expect3], prop_list(3))
648 call prop_clear(1, 3)
649
650 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100651
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100652 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100653 call Setup_three_line_prop()
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100654 let expect_short = {'col': 2, 'length': 1, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0}
655 call assert_equal([expect_short], prop_list(1))
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100656 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
657 call assert_equal([expect2], prop_list(2))
658 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100659 call assert_equal([expect_short], prop_list(1))
660 let expect2 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
661 call assert_equal([expect2], prop_list(2))
662 bwipe!
663
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100664 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100665 call Setup_three_line_prop()
666 let expect3 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
667 call assert_equal([expect3], prop_list(3))
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100668 let expect4 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100669 call assert_equal([expect4], prop_list(4))
670 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100671 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100672 call assert_equal([expect3], prop_list(3))
673 call assert_equal([expect_short], prop_list(4))
674 bwipe!
675
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100676 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100677 call Setup_three_line_prop()
678 let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0}
679 call assert_equal([expect2], prop_list(2))
680 call append(2, "new line")
681 call assert_equal([expect2], prop_list(2))
682 let expect3 = {'col': 1, 'length': 9, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0}
683 call assert_equal([expect3], prop_list(3))
684 bwipe!
685
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100686 call prop_type_delete('comment')
687endfunc
688
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100689func Test_prop_line2byte()
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100690 call prop_type_add('comment', {'highlight': 'Directory'})
691 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100692 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100693 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100694 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100695 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100696 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100697
698 bwipe!
699 call prop_type_delete('comment')
700endfunc
701
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100702func Test_prop_byte2line()
703 new
704 set ff=unix
705 call setline(1, ['one one', 'two two', 'three three', 'four four', 'five'])
706 call assert_equal(4, byte2line(line2byte(4)))
707 call assert_equal(5, byte2line(line2byte(5)))
708
709 call prop_type_add('prop', {'highlight': 'Directory'})
710 call prop_add(3, 1, {'length': 5, 'type': 'prop'})
711 call assert_equal(4, byte2line(line2byte(4)))
712 call assert_equal(5, byte2line(line2byte(5)))
713
714 bwipe!
715 call prop_type_delete('prop')
716endfunc
717
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100718func Test_prop_undo()
719 new
720 call prop_type_add('comment', {'highlight': 'Directory'})
721 call setline(1, ['oneone', 'twotwo', 'three'])
722 " Set 'undolevels' to break changes into undo-able pieces.
723 set ul&
724
725 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
726 let expected = [{'col': 3, 'length': 2, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
727 call assert_equal(expected, prop_list(1))
728
729 " Insert a character, then undo.
730 exe "normal 0lllix\<Esc>"
731 set ul&
732 let expected[0].length = 3
733 call assert_equal(expected, prop_list(1))
734 undo
735 let expected[0].length = 2
736 call assert_equal(expected, prop_list(1))
737
738 " Delete a character, then undo
739 exe "normal 0lllx"
740 set ul&
741 let expected[0].length = 1
742 call assert_equal(expected, prop_list(1))
743 undo
744 let expected[0].length = 2
745 call assert_equal(expected, prop_list(1))
746
747 " Delete the line, then undo
748 1d
749 set ul&
750 call assert_equal([], prop_list(1))
751 undo
752 call assert_equal(expected, prop_list(1))
753
754 " Insert a character, delete two characters, then undo with "U"
755 exe "normal 0lllix\<Esc>"
756 set ul&
757 let expected[0].length = 3
758 call assert_equal(expected, prop_list(1))
759 exe "normal 0lllxx"
760 set ul&
761 let expected[0].length = 1
762 call assert_equal(expected, prop_list(1))
763 normal U
764 let expected[0].length = 2
765 call assert_equal(expected, prop_list(1))
766
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200767 " substitute a word, then undo
768 call setline(1, 'the number 123 is highlighted.')
769 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
770 let expected = [{'col': 12, 'length': 3, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
771 call assert_equal(expected, prop_list(1))
772 set ul&
773 1s/number/foo
774 let expected[0].col = 9
775 call assert_equal(expected, prop_list(1))
776 undo
777 let expected[0].col = 12
778 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200779 call prop_clear(1)
780
781 " substitute with backslash
782 call setline(1, 'the number 123 is highlighted.')
783 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
784 let expected = [{'col': 12, 'length': 3, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
785 call assert_equal(expected, prop_list(1))
786 1s/the/\The
787 call assert_equal(expected, prop_list(1))
788 1s/^/\\
789 let expected[0].col += 1
790 call assert_equal(expected, prop_list(1))
791 1s/^/\~
792 let expected[0].col += 1
793 call assert_equal(expected, prop_list(1))
794 1s/123/12\\3
795 let expected[0].length += 1
796 call assert_equal(expected, prop_list(1))
797 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200798
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100799 bwipe!
800 call prop_type_delete('comment')
801endfunc
802
Bram Moolenaarecafcc12019-11-16 20:41:51 +0100803func Test_prop_delete_text()
804 new
805 call prop_type_add('comment', {'highlight': 'Directory'})
806 call setline(1, ['oneone', 'twotwo', 'three'])
807
808 " zero length property
809 call prop_add(1, 3, {'type': 'comment'})
810 let expected = [{'col': 3, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
811 call assert_equal(expected, prop_list(1))
812
813 " delete one char moves the property
814 normal! x
815 let expected = [{'col': 2, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
816 call assert_equal(expected, prop_list(1))
817
818 " delete char of the property has no effect
819 normal! lx
820 let expected = [{'col': 2, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
821 call assert_equal(expected, prop_list(1))
822
823 " delete more chars moves property to first column, is not deleted
824 normal! 0xxxx
825 let expected = [{'col': 1, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ]
826 call assert_equal(expected, prop_list(1))
827
828 bwipe!
829 call prop_type_delete('comment')
830endfunc
831
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100832" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +0200833func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +0200834 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +0100835 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +0200836 if g:orig_encoding != 'utf-8'
837 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100838 endif
839 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +0200840 \ "call setline(1, ["
841 \ .. "'One two',"
842 \ .. "'Numbér 123 änd thœn 4¾7.',"
843 \ .. "'--aa--bb--cc--dd--',"
844 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200845 \ .. "'first line',"
846 \ .. "' second line ',"
847 \ .. "'third line',"
848 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +0200849 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100850 \ "hi NumberProp ctermfg=blue",
851 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200852 \ "hi BackgroundProp ctermbg=lightgrey",
853 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100854 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +0200855 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
856 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100857 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
858 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
859 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +0100860 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
861 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
862 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +0100863 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100864 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100865 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
866 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100867 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
868 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
869 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
870 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +0100871 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
872 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200873 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200874 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
875 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
876 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
877 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +0200878 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100879 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100880 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200881 \ "syn match Comment '//.*'",
882 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100883 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100884 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200885 \ "normal 6G0i\<BS>\<Esc>",
886 \ "normal 3J",
887 \ "normal 3G",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100888 \], 'XtestProp')
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200889 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100890 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100891
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100892 " clean up
893 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +0100894 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100895endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +0200896
897func RunTestVisualBlock(width, dump)
898 call writefile([
899 \ "call setline(1, ["
900 \ .. "'xxxxxxxxx 123 x',"
901 \ .. "'xxxxxxxx 123 x',"
902 \ .. "'xxxxxxx 123 x',"
903 \ .. "'xxxxxx 123 x',"
904 \ .. "'xxxxx 123 x',"
905 \ .. "'xxxx 123 xx',"
906 \ .. "'xxx 123 xxx',"
907 \ .. "'xx 123 xxxx',"
908 \ .. "'x 123 xxxxx',"
909 \ .. "' 123 xxxxxx',"
910 \ .. "])",
911 \ "hi SearchProp ctermbg=yellow",
912 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
913 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
914 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
915 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
916 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
917 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
918 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
919 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
920 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
921 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
922 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
923 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
924 \], 'XtestPropVis')
925 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
926 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
927
928 " clean up
929 call StopVimInTerminal(buf)
930 call delete('XtestPropVis')
931endfunc
932
933" screenshot test with Visual block mode operations
934func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +0200935 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +0200936
937 " Delete two columns while text props are three chars wide.
938 call RunTestVisualBlock(2, '01')
939
940 " Same, but delete four columns
941 call RunTestVisualBlock(4, '02')
942endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +0200943
Bram Moolenaara956bf62019-06-19 17:34:24 +0200944func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +0200945 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +0200946
Bram Moolenaara956bf62019-06-19 17:34:24 +0200947 let lines =<< trim END
948 call setline(1, [
949 \ "\txxx",
950 \ "x\txxx",
951 \ ])
952 hi SearchProp ctermbg=yellow
953 call prop_type_add('search', {'highlight': 'SearchProp'})
954 call prop_add(1, 2, {'length': 3, 'type': 'search'})
955 call prop_add(2, 3, {'length': 3, 'type': 'search'})
956 END
957 call writefile(lines, 'XtestPropTab')
958 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
959 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
960
961 " clean up
962 call StopVimInTerminal(buf)
963 call delete('XtestPropTab')
964endfunc
965
Bram Moolenaar34390282019-10-16 14:38:26 +0200966func Test_textprop_with_syntax()
967 CheckScreendump
968
969 let lines =<< trim END
970 call setline(1, [
971 \ "(abc)",
972 \ ])
973 syn match csParens "[()]" display
974 hi! link csParens MatchParen
975
976 call prop_type_add('TPTitle', #{ highlight: 'Title' })
977 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
978 END
979 call writefile(lines, 'XtestPropSyn')
980 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
981 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
982
983 " clean up
984 call StopVimInTerminal(buf)
985 call delete('XtestPropSyn')
986endfunc
987
Bram Moolenaard79eef22019-05-24 20:41:55 +0200988" Adding a text property to a new buffer should not fail
989func Test_textprop_empty_buffer()
990 call prop_type_add('comment', {'highlight': 'Search'})
991 new
992 call prop_add(1, 1, {'type': 'comment'})
993 close
Bram Moolenaaradfde112019-05-25 22:11:45 +0200994 call prop_type_delete('comment')
995endfunc
996
Bram Moolenaard74af422019-06-28 21:38:00 +0200997" Adding a text property with invalid highlight should be ignored.
998func Test_textprop_invalid_highlight()
999 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
1000 new
1001 call setline(1, ['asdf','asdf'])
1002 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
1003 redraw
1004 bwipe!
1005 call prop_type_delete('dni')
1006endfunc
1007
Bram Moolenaaradfde112019-05-25 22:11:45 +02001008" Adding a text property to an empty buffer and then editing another
1009func Test_textprop_empty_buffer_next()
1010 call prop_type_add("xxx", {})
1011 call prop_add(1, 1, {"type": "xxx"})
1012 next X
1013 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +02001014endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001015
1016func Test_textprop_remove_from_buf()
1017 new
1018 let buf = bufnr('')
1019 call prop_type_add('one', {'bufnr': buf})
1020 call prop_add(1, 1, {'type': 'one', 'id': 234})
1021 file x
1022 edit y
1023 call prop_remove({'id': 234, 'bufnr': buf}, 1)
1024 call prop_type_delete('one', {'bufnr': buf})
1025 bwipe! x
1026 close
1027endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +02001028
1029func Test_textprop_in_unloaded_buf()
1030 edit Xaaa
1031 call setline(1, 'aaa')
1032 write
1033 edit Xbbb
1034 call setline(1, 'bbb')
1035 write
1036 let bnr = bufnr('')
1037 edit Xaaa
1038
1039 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
1040 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
1041 exe 'buf ' .. bnr
1042 call assert_equal('bbb', getline(1))
1043 call assert_equal(0, prop_list(1)->len())
1044
1045 bwipe! Xaaa
1046 bwipe! Xbbb
1047 cal delete('Xaaa')
1048 cal delete('Xbbb')
1049endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001050
1051func Test_proptype_substitute2()
1052 new
1053 " text_prop.vim
1054 call setline(1, [
1055 \ 'The num 123 is smaller than 4567.',
1056 \ '123 The number 123 is smaller than 4567.',
1057 \ '123 The number 123 is smaller than 4567.'])
1058
1059 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1060
1061 call prop_add(1, 12, {'length': 3, 'type': 'number'})
1062 call prop_add(2, 1, {'length': 3, 'type': 'number'})
1063 call prop_add(3, 36, {'length': 4, 'type': 'number'})
1064 set ul&
1065 let expected = [{'id': 0, 'col': 13, 'end': 1, 'type': 'number', 'length': 3, 'start': 1},
1066 \ {'id': 0, 'col': 1, 'end': 1, 'type': 'number', 'length': 3, 'start': 1},
1067 \ {'id': 0, 'col': 50, 'end': 1, 'type': 'number', 'length': 4, 'start': 1}]
1068 " Add some text in between
1069 %s/\s\+/ /g
1070 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
1071
1072 " remove some text
1073 :1s/[a-z]\{3\}//g
1074 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
1075 call assert_equal(expected, prop_list(1))
1076 bwipe!
1077endfunc
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001078
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001079func SaveOptions()
1080 let d = #{tabstop: &tabstop,
1081 \ softtabstop: &softtabstop,
1082 \ shiftwidth: &shiftwidth,
1083 \ expandtab: &expandtab,
1084 \ foldmethod: '"' .. &foldmethod .. '"',
1085 \ }
1086 return d
1087endfunc
1088
1089func RestoreOptions(dict)
1090 for name in keys(a:dict)
1091 exe 'let &' .. name .. ' = ' .. a:dict[name]
1092 endfor
1093endfunc
1094
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001095func Test_textprop_noexpandtab()
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001096 new
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001097 let save_dict = SaveOptions()
1098
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001099 set tabstop=8
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001100 set softtabstop=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001101 set shiftwidth=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001102 set noexpandtab
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001103 set foldmethod=marker
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001104
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001105 call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx")
1106 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1107 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1108 call feedkeys("0i\<tab>", "tx")
1109 call prop_remove({'type': 'test'})
1110 call prop_add(1, 2, {'end_col': 3, 'type': 'test'})
1111 call feedkeys("A\<left>\<tab>", "tx")
1112 call prop_remove({'type': 'test'})
1113 try
1114 " It is correct that this does not pass
1115 call prop_add(1, 6, {'end_col': 7, 'type': 'test'})
1116 " Has already collapsed here, start_col:6 does not result in an error
1117 call feedkeys("A\<left>\<tab>", "tx")
1118 catch /^Vim\%((\a\+)\)\=:E964/
1119 endtry
1120 call prop_remove({'type': 'test'})
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001121 call prop_type_delete('test')
1122
1123 call RestoreOptions(save_dict)
1124 bwipe!
1125endfunc
1126
1127func Test_textprop_noexpandtab_redraw()
1128 new
1129 let save_dict = SaveOptions()
1130
1131 set tabstop=8
1132 set softtabstop=4
1133 set shiftwidth=4
1134 set noexpandtab
1135 set foldmethod=marker
1136
1137 call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx")
1138 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1139 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1140 call feedkeys("0i\<tab>", "tx")
1141 " Internally broken at the next line
1142 call feedkeys("A\<left>\<tab>", "tx")
1143 redraw
1144 " Index calculation failed internally on next line
1145 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1146 call prop_remove({'type': 'test', 'all': v:true})
1147 call prop_type_delete('test')
1148 call prop_type_delete('test')
1149
1150 call RestoreOptions(save_dict)
1151 bwipe!
1152endfunc
1153
1154func Test_textprop_ins_str()
1155 new
1156 call setline(1, 'just some text')
1157 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1158 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1159 call assert_equal([{'id': 0, 'col': 1, 'end': 1, 'type': 'test', 'length': 1, 'start': 1}], prop_list(1))
1160
1161 call feedkeys("foi\<F8>\<Esc>", "tx")
1162 call assert_equal('just s<F8>ome text', getline(1))
1163 call assert_equal([{'id': 0, 'col': 1, 'end': 1, 'type': 'test', 'length': 1, 'start': 1}], prop_list(1))
1164
1165 bwipe!
1166 call prop_remove({'type': 'test'})
1167 call prop_type_delete('test')
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001168endfunc
Bram Moolenaar66b98852020-03-11 19:15:52 +01001169
1170func Test_find_prop_later_in_line()
1171 new
1172 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1173 call setline(1, 'just some text')
1174 call prop_add(1, 1, {'length': 4, 'type': 'test'})
1175 call prop_add(1, 10, {'length': 3, 'type': 'test'})
1176
1177 call assert_equal({'id': 0, 'lnum': 1, 'col': 10, 'end': 1, 'type': 'test', 'length': 3, 'start': 1},
1178 \ prop_find(#{type: 'test', lnum: 1, col: 6}))
1179
1180 bwipe!
1181 call prop_type_delete('test')
1182endfunc
1183
1184func Test_find_zerowidth_prop_sol()
1185 new
1186 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1187 call setline(1, 'just some text')
1188 call prop_add(1, 1, {'length': 0, 'type': 'test'})
1189
1190 call assert_equal({'id': 0, 'lnum': 1, 'col': 1, 'end': 1, 'type': 'test', 'length': 0, 'start': 1},
1191 \ prop_find(#{type: 'test', lnum: 1}))
1192
1193 bwipe!
1194 call prop_type_delete('test')
1195endfunc