blob: 0ac4e898fc3f7e24b7a38d86cd267483a2609afe [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('')
Martin Tournoije2390c72021-07-28 13:30:16 +020042 call prop_type_add('comment', #{bufnr: bufnr, highlight: 'Directory', priority: 123, start_incl: 1, end_incl: 1})
Bram Moolenaar98aefe72018-12-13 22:20:09 +010043 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
Martin Tournoije2390c72021-07-28 13:30:16 +020073def Test_proptype_buf_list()
74 new
75 var bufnr = bufnr('')
76 try
77 prop_type_add('global', {})
78 prop_type_add('local', {bufnr: bufnr})
79
80 prop_add(1, 1, {type: 'global'})
81 prop_add(1, 1, {type: 'local'})
82
83 assert_equal([
84 {type: 'local', type_bufnr: bufnr, id: 0, col: 1, end: 1, length: 0, start: 1},
85 {type: 'global', type_bufnr: 0, id: 0, col: 1, end: 1, length: 0, start: 1},
86 ], prop_list(1))
87 assert_equal(
88 {lnum: 1, id: 0, col: 1, type_bufnr: bufnr, end: 1, type: 'local', length: 0, start: 1},
89 prop_find({lnum: 1, type: 'local'}))
90 assert_equal(
91 {lnum: 1, id: 0, col: 1, type_bufnr: 0, end: 1, type: 'global', length: 0, start: 1},
92 prop_find({lnum: 1, type: 'global'}))
93
94 prop_remove({type: 'global'}, 1)
95 prop_remove({type: 'local'}, 1)
96 finally
97 prop_type_delete('global')
98 prop_type_delete('local', {bufnr: bufnr})
99 bwipe!
100 endtry
101enddef
102
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100103func AddPropTypes()
104 call prop_type_add('one', {})
105 call prop_type_add('two', {})
106 call prop_type_add('three', {})
107 call prop_type_add('whole', {})
108endfunc
109
110func DeletePropTypes()
111 call prop_type_delete('one')
112 call prop_type_delete('two')
113 call prop_type_delete('three')
114 call prop_type_delete('whole')
115endfunc
116
117func SetupPropsInFirstLine()
118 call setline(1, 'one two three')
119 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
Bram Moolenaara5a78822019-09-04 21:57:18 +0200120 eval 1->prop_add(5, {'length': 3, 'id': 12, 'type': 'two'})
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100121 call prop_add(1, 9, {'length': 5, 'id': 13, 'type': 'three'})
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100122 call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
123endfunc
124
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100125func Get_expected_props()
126 return [
Martin Tournoije2390c72021-07-28 13:30:16 +0200127 \ #{type_bufnr: 0, col: 1, length: 13, id: 14, type: 'whole', start: 1, end: 1},
128 \ #{type_bufnr: 0, col: 1, length: 3, id: 11, type: 'one', start: 1, end: 1},
129 \ #{type_bufnr: 0, col: 5, length: 3, id: 12, type: 'two', start: 1, end: 1},
130 \ #{type_bufnr: 0, col: 9, length: 5, id: 13, type: 'three', start: 1, end: 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100131 \ ]
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100132endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100133
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100134func Test_prop_find()
135 new
136 call setline(1, ['one one one', 'twotwo', 'three', 'fourfour', 'five', 'sixsix'])
Martin Tournoije2390c72021-07-28 13:30:16 +0200137
138 " Add two text props on lines 1 and 5, and one spanning lines 2 to 4.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100139 call prop_type_add('prop_name', {'highlight': 'Directory'})
140 call prop_add(1, 5, {'type': 'prop_name', 'id': 10, 'length': 3})
141 call prop_add(2, 4, {'type': 'prop_name', 'id': 11, 'end_lnum': 4, 'end_col': 9})
142 call prop_add(5, 4, {'type': 'prop_name', 'id': 12, 'length': 1})
143
144 let expected = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200145 \ #{type_bufnr: 0, lnum: 1, col: 5, length: 3, id: 10, type: 'prop_name', start: 1, end: 1},
146 \ #{type_bufnr: 0, lnum: 2, col: 4, id: 11, type: 'prop_name', start: 1, end: 0},
147 \ #{type_bufnr: 0, lnum: 5, col: 4, length: 1, id: 12, type: 'prop_name', start: 1, end: 1}
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100148 \ ]
149
150 " Starting at line 5 col 1 this should find the prop at line 5 col 4.
151 call cursor(5,1)
152 let result = prop_find({'type': 'prop_name'}, 'f')
153 call assert_equal(expected[2], result)
154
155 " With skipstart left at false (default), this should find the prop at line
156 " 5 col 4.
157 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4}, 'b')
158 call assert_equal(expected[2], result)
159
160 " With skipstart set to true, this should skip the prop at line 5 col 4.
161 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4, 'skipstart': 1}, 'b')
162 unlet result.length
163 call assert_equal(expected[1], result)
164
165 " Search backwards from line 1 col 10 to find the prop on the same line.
166 let result = prop_find({'type': 'prop_name', 'lnum': 1, 'col': 10}, 'b')
167 call assert_equal(expected[0], result)
168
169 " with skipstart set to false, if the start position is anywhere between the
170 " start and end lines of a text prop (searching forward or backward), the
171 " result should be the prop on the first line (the line with 'start' set to 1).
172 call cursor(3,1)
173 let result = prop_find({'type': 'prop_name'}, 'f')
174 unlet result.length
175 call assert_equal(expected[1], result)
176 let result = prop_find({'type': 'prop_name'}, 'b')
177 unlet result.length
178 call assert_equal(expected[1], result)
179
180 " with skipstart set to true, if the start position is anywhere between the
181 " start and end lines of a text prop (searching forward or backward), all lines
182 " of the prop will be skipped.
183 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'b')
184 call assert_equal(expected[0], result)
185 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'f')
186 call assert_equal(expected[2], result)
187
188 " Use skipstart to search through all props with type name 'prop_name'.
189 " First forward...
190 let lnum = 1
191 let col = 1
192 let i = 0
193 for exp in expected
194 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'f')
195 if !has_key(exp, "length")
196 unlet result.length
197 endif
198 call assert_equal(exp, result)
199 let lnum = result.lnum
200 let col = result.col
201 let i = i + 1
202 endfor
203
204 " ...then backwards.
205 let lnum = 6
206 let col = 4
207 let i = 2
208 while i >= 0
209 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'b')
210 if !has_key(expected[i], "length")
211 unlet result.length
212 endif
213 call assert_equal(expected[i], result)
214 let lnum = result.lnum
215 let col = result.col
216 let i = i - 1
Martin Tournoije2390c72021-07-28 13:30:16 +0200217 endwhile
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100218
219 " Starting from line 6 col 1 search backwards for prop with id 10.
220 call cursor(6,1)
221 let result = prop_find({'id': 10, 'skipstart': 1}, 'b')
222 call assert_equal(expected[0], result)
223
224 " Starting from line 1 col 1 search forwards for prop with id 12.
225 call cursor(1,1)
226 let result = prop_find({'id': 12}, 'f')
227 call assert_equal(expected[2], result)
228
229 " Search for a prop with an unknown id.
230 let result = prop_find({'id': 999}, 'f')
231 call assert_equal({}, result)
232
233 " Search backwards from the proceeding position of the prop with id 11
234 " (at line num 2 col 4). This should return an empty dict.
235 let result = prop_find({'id': 11, 'lnum': 2, 'col': 3}, 'b')
236 call assert_equal({}, result)
237
238 " When lnum is given and col is omitted, use column 1.
239 let result = prop_find({'type': 'prop_name', 'lnum': 1}, 'f')
240 call assert_equal(expected[0], result)
241
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200242 " When ID is -1 it's like prop is not found.
243 call assert_equal({}, prop_find({'id': -1}))
244
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100245 call prop_clear(1,6)
246 call prop_type_delete('prop_name')
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200247
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200248 bwipe!
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100249endfunc
250
Bram Moolenaareb245562020-09-03 22:33:44 +0200251def Test_prop_find2()
252 # Multiple props per line, start on the first, should find the second.
253 new
254 ['the quikc bronw fox jumsp over the layz dog']->repeat(2)->setline(1)
Bram Moolenaare0de1712020-12-02 17:36:54 +0100255 prop_type_add('misspell', {highlight: 'ErrorMsg'})
Bram Moolenaareb245562020-09-03 22:33:44 +0200256 for lnum in [1, 2]
257 for col in [8, 14, 24, 38]
Bram Moolenaare0de1712020-12-02 17:36:54 +0100258 prop_add(lnum, col, {type: 'misspell', length: 2})
Bram Moolenaareb245562020-09-03 22:33:44 +0200259 endfor
260 endfor
261 cursor(1, 8)
Martin Tournoije2390c72021-07-28 13:30:16 +0200262 var expected = {type_bufnr: 0, lnum: 1, id: 0, col: 14, end: 1, type: 'misspell', length: 2, start: 1}
Bram Moolenaare0de1712020-12-02 17:36:54 +0100263 var result = prop_find({type: 'misspell', skipstart: true}, 'f')
Bram Moolenaareb245562020-09-03 22:33:44 +0200264 assert_equal(expected, result)
265
266 prop_type_delete('misspell')
267 bwipe!
268enddef
269
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100270func Test_prop_find_smaller_len_than_match_col()
271 new
272 call prop_type_add('test', {'highlight': 'ErrorMsg'})
273 call setline(1, ['xxxx', 'x'])
274 call prop_add(1, 4, {'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200275 call assert_equal(
276 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 4, type: 'test', length: 0, start: 1, end: 1},
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100277 \ prop_find({'type': 'test', 'lnum': 2, 'col': 1}, 'b'))
278 bwipe!
279 call prop_type_delete('test')
280endfunc
281
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100282func Test_prop_find_with_both_option_enabled()
283 " Initialize
284 new
285 call AddPropTypes()
286 call SetupPropsInFirstLine()
287 let props = Get_expected_props()->map({_, v -> extend(v, {'lnum': 1})})
288 " Test
289 call assert_fails("call prop_find({'both': 1})", 'E968:')
290 call assert_fails("call prop_find({'id': 11, 'both': 1})", 'E860:')
291 call assert_fails("call prop_find({'type': 'three', 'both': 1})", 'E860:')
292 call assert_equal({}, prop_find({'id': 11, 'type': 'three', 'both': 1}))
293 call assert_equal({}, prop_find({'id': 130000, 'type': 'one', 'both': 1}))
294 call assert_equal(props[2], prop_find({'id': 12, 'type': 'two', 'both': 1}))
295 call assert_equal(props[0], prop_find({'id': 14, 'type': 'whole', 'both': 1}))
296 " Clean up
297 call DeletePropTypes()
298 bwipe!
299endfunc
300
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100301func Test_prop_add()
302 new
303 call AddPropTypes()
304 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100305 let expected_props = Get_expected_props()
306 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100307 call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:')
308 call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:')
Martin Tournoije2390c72021-07-28 13:30:16 +0200309
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100310 " Insert a line above, text props must still be there.
311 call append(0, 'empty')
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100312 call assert_equal(expected_props, prop_list(2))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100313 " Delete a line above, text props must still be there.
314 1del
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100315 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100316
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100317 " Prop without length or end column is zero length
318 call prop_clear(1)
Bram Moolenaar12f20032020-02-26 22:06:00 +0100319 call prop_type_add('included', {'start_incl': 1, 'end_incl': 1})
320 call prop_add(1, 5, #{type: 'included'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200321 let expected = [#{type_bufnr: 0, col: 5, length: 0, type: 'included', id: 0, start: 1, end: 1}]
Bram Moolenaar12f20032020-02-26 22:06:00 +0100322 call assert_equal(expected, prop_list(1))
323
324 " Inserting text makes the prop bigger.
325 exe "normal 5|ixx\<Esc>"
Martin Tournoije2390c72021-07-28 13:30:16 +0200326 let expected = [#{type_bufnr: 0, col: 5, length: 2, type: 'included', id: 0, start: 1, end: 1}]
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100327 call assert_equal(expected, prop_list(1))
328
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200329 call assert_fails("call prop_add(1, 5, {'type': 'two', 'bufnr': 234343})", 'E158:')
330
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100331 call DeletePropTypes()
Bram Moolenaar12f20032020-02-26 22:06:00 +0100332 call prop_type_delete('included')
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100333 bwipe!
334endfunc
335
336func Test_prop_remove()
337 new
338 call AddPropTypes()
339 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100340 let props = Get_expected_props()
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100341 call assert_equal(props, prop_list(1))
342
343 " remove by id
Bram Moolenaara5a78822019-09-04 21:57:18 +0200344 call assert_equal(1, {'id': 12}->prop_remove(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100345 unlet props[2]
346 call assert_equal(props, prop_list(1))
347
348 " remove by type
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200349 call assert_equal(1, prop_remove({'type': 'one'}, 1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100350 unlet props[1]
351 call assert_equal(props, prop_list(1))
352
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200353 " remove from unknown buffer
354 call assert_fails("call prop_remove({'type': 'one', 'bufnr': 123456}, 1)", 'E158:')
355
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100356 call DeletePropTypes()
357 bwipe!
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100358
359 new
360 call AddPropTypes()
361 call SetupPropsInFirstLine()
362 call prop_add(1, 6, {'length': 2, 'id': 11, 'type': 'three'})
363 let props = Get_expected_props()
Martin Tournoije2390c72021-07-28 13:30:16 +0200364 call insert(props, #{type_bufnr: 0, col: 6, length: 2, id: 11, type: 'three', start: 1, end: 1}, 3)
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100365 call assert_equal(props, prop_list(1))
366 call assert_equal(1, prop_remove({'type': 'three', 'id': 11, 'both': 1, 'all': 1}, 1))
367 unlet props[3]
368 call assert_equal(props, prop_list(1))
369
Bram Moolenaare2e40752020-09-04 21:18:46 +0200370 call assert_fails("call prop_remove({'id': 11, 'both': 1})", 'E860:')
371 call assert_fails("call prop_remove({'type': 'three', 'both': 1})", 'E860:')
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100372
373 call DeletePropTypes()
374 bwipe!
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100375endfunc
376
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200377def Test_prop_add_vim9()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100378 prop_type_add('comment', {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200379 highlight: 'Directory',
380 priority: 123,
381 start_incl: true,
382 end_incl: true,
383 combine: false,
384 })
385 prop_type_delete('comment')
386enddef
387
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200388def Test_prop_remove_vim9()
389 new
Bram Moolenaard2c61702020-09-06 15:58:36 +0200390 AddPropTypes()
391 SetupPropsInFirstLine()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100392 assert_equal(1, prop_remove({type: 'three', id: 13, both: true, all: true}))
Bram Moolenaard2c61702020-09-06 15:58:36 +0200393 DeletePropTypes()
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200394 bwipe!
395enddef
396
Bram Moolenaar196d1572019-01-02 23:47:18 +0100397func SetupOneLine()
398 call setline(1, 'xonex xtwoxx')
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200399 normal gg0
Bram Moolenaar196d1572019-01-02 23:47:18 +0100400 call AddPropTypes()
401 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
402 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
403 let expected = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200404 \ #{type_bufnr: 0, col: 2, length: 3, id: 11, type: 'one', start: 1, end: 1},
405 \ #{type_bufnr: 0, col: 8, length: 3, id: 12, type: 'two', start: 1, end: 1},
Bram Moolenaar196d1572019-01-02 23:47:18 +0100406 \]
407 call assert_equal(expected, prop_list(1))
408 return expected
409endfunc
410
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100411func Test_prop_add_remove_buf()
412 new
413 let bufnr = bufnr('')
414 call AddPropTypes()
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100415 for lnum in range(1, 4)
416 call setline(lnum, 'one two three')
417 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100418 wincmd w
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100419 for lnum in range(1, 4)
420 call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
421 call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
422 call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
423 endfor
424
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100425 let props = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200426 \ #{type_bufnr: 0, col: 1, length: 3, id: 11, type: 'one', start: 1, end: 1},
427 \ #{type_bufnr: 0, col: 5, length: 3, id: 12, type: 'two', start: 1, end: 1},
428 \ #{type_bufnr: 0, col: 11, length: 3, id: 13, type: 'three', start: 1, end: 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100429 \]
430 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100431
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100432 " remove by id
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100433 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100434 unlet props[1]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100435
436 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100437 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100438 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
439 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
440 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
441
442 call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4)
443 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
444 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
445 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
446 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
447
448 call prop_remove({'id': 12, 'bufnr': bufnr})
449 for lnum in range(1, 4)
450 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
451 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100452
453 " remove by type
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100454 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100455 unlet props[0]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100456
457 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100458 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100459 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
460 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
461 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
462
463 call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4)
464 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
465 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
466 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
467 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
468
469 call prop_remove({'type': 'one', 'bufnr': bufnr})
470 for lnum in range(1, 4)
471 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
472 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100473
474 call DeletePropTypes()
475 wincmd w
476 bwipe!
477endfunc
478
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100479func Test_prop_backspace()
480 new
481 set bs=2
Bram Moolenaar196d1572019-01-02 23:47:18 +0100482 let expected = SetupOneLine() " 'xonex xtwoxx'
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100483
484 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
485 call assert_equal('one xtwoxx', getline(1))
486 let expected[0].col = 1
487 let expected[1].col = 6
488 call assert_equal(expected, prop_list(1))
489
490 call DeletePropTypes()
491 bwipe!
492 set bs&
493endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100494
Bram Moolenaar196d1572019-01-02 23:47:18 +0100495func Test_prop_replace()
496 new
497 set bs=2
498 let expected = SetupOneLine() " 'xonex xtwoxx'
499
500 exe "normal 0Ryyy\<Esc>"
501 call assert_equal('yyyex xtwoxx', getline(1))
502 call assert_equal(expected, prop_list(1))
503
504 exe "normal ftRyy\<BS>"
505 call assert_equal('yyyex xywoxx', getline(1))
506 call assert_equal(expected, prop_list(1))
507
508 exe "normal 0fwRyy\<BS>"
509 call assert_equal('yyyex xyyoxx', getline(1))
510 call assert_equal(expected, prop_list(1))
511
512 exe "normal 0foRyy\<BS>\<BS>"
513 call assert_equal('yyyex xyyoxx', getline(1))
514 call assert_equal(expected, prop_list(1))
515
516 call DeletePropTypes()
517 bwipe!
518 set bs&
519endfunc
520
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200521func Test_prop_open_line()
522 new
523
524 " open new line, props stay in top line
525 let expected = SetupOneLine() " 'xonex xtwoxx'
526 exe "normal o\<Esc>"
527 call assert_equal('xonex xtwoxx', getline(1))
528 call assert_equal('', getline(2))
529 call assert_equal(expected, prop_list(1))
530 call DeletePropTypes()
531
532 " move all props to next line
533 let expected = SetupOneLine() " 'xonex xtwoxx'
534 exe "normal 0i\<CR>\<Esc>"
535 call assert_equal('', getline(1))
536 call assert_equal('xonex xtwoxx', getline(2))
537 call assert_equal(expected, prop_list(2))
538 call DeletePropTypes()
539
540 " split just before prop, move all props to next line
541 let expected = SetupOneLine() " 'xonex xtwoxx'
542 exe "normal 0li\<CR>\<Esc>"
543 call assert_equal('x', getline(1))
544 call assert_equal('onex xtwoxx', getline(2))
545 let expected[0].col -= 1
546 let expected[1].col -= 1
547 call assert_equal(expected, prop_list(2))
548 call DeletePropTypes()
549
550 " split inside prop, split first prop
551 let expected = SetupOneLine() " 'xonex xtwoxx'
552 exe "normal 0lli\<CR>\<Esc>"
553 call assert_equal('xo', getline(1))
554 call assert_equal('nex xtwoxx', getline(2))
555 let exp_first = [deepcopy(expected[0])]
556 let exp_first[0].length = 1
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200557 let exp_first[0].end = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200558 call assert_equal(exp_first, prop_list(1))
559 let expected[0].col = 1
560 let expected[0].length = 2
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200561 let expected[0].start = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200562 let expected[1].col -= 2
563 call assert_equal(expected, prop_list(2))
564 call DeletePropTypes()
565
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200566 " split just after first prop, second prop move to next line
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200567 let expected = SetupOneLine() " 'xonex xtwoxx'
568 exe "normal 0fea\<CR>\<Esc>"
569 call assert_equal('xone', getline(1))
570 call assert_equal('x xtwoxx', getline(2))
571 let exp_first = expected[0:0]
572 call assert_equal(exp_first, prop_list(1))
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200573 let expected = expected[1:1]
574 let expected[0].col -= 4
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200575 call assert_equal(expected, prop_list(2))
576 call DeletePropTypes()
577
578 bwipe!
579 set bs&
580endfunc
581
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100582func Test_prop_clear()
583 new
584 call AddPropTypes()
585 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100586 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100587
Bram Moolenaara5a78822019-09-04 21:57:18 +0200588 eval 1->prop_clear()
589 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100590
591 call DeletePropTypes()
592 bwipe!
593endfunc
594
595func Test_prop_clear_buf()
596 new
597 call AddPropTypes()
598 call SetupPropsInFirstLine()
599 let bufnr = bufnr('')
600 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100601 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100602
603 call prop_clear(1, 1, {'bufnr': bufnr})
604 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
605
606 wincmd w
607 call DeletePropTypes()
608 bwipe!
609endfunc
610
Bram Moolenaar21b50382019-01-04 18:07:24 +0100611func Test_prop_setline()
612 new
613 call AddPropTypes()
614 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100615 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100616
617 call setline(1, 'foobar')
618 call assert_equal([], prop_list(1))
619
620 call DeletePropTypes()
621 bwipe!
622endfunc
623
624func Test_prop_setbufline()
625 new
626 call AddPropTypes()
627 call SetupPropsInFirstLine()
628 let bufnr = bufnr('')
629 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100630 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100631
632 call setbufline(bufnr, 1, 'foobar')
633 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
634
635 wincmd w
636 call DeletePropTypes()
637 bwipe!
638endfunc
639
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100640func Test_prop_substitute()
641 new
642 " Set first line to 'one two three'
643 call AddPropTypes()
644 call SetupPropsInFirstLine()
645 let expected_props = Get_expected_props()
646 call assert_equal(expected_props, prop_list(1))
647
648 " Change "n" in "one" to XX: 'oXXe two three'
649 s/n/XX/
650 let expected_props[0].length += 1
651 let expected_props[1].length += 1
652 let expected_props[2].col += 1
653 let expected_props[3].col += 1
654 call assert_equal(expected_props, prop_list(1))
655
656 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
657 s/t//g
658 let expected_props[0].length -= 2
659 let expected_props[2].length -= 1
660 let expected_props[3].length -= 1
661 let expected_props[3].col -= 1
662 call assert_equal(expected_props, prop_list(1))
663
664 " Split the line by changing w to line break: 'oXXe ', 'o hree'
665 " The long prop is split and spans both lines.
666 " The props on "two" and "three" move to the next line.
667 s/w/\r/
668 let new_props = [
669 \ copy(expected_props[0]),
670 \ copy(expected_props[2]),
671 \ copy(expected_props[3]),
672 \ ]
673 let expected_props[0].length = 5
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200674 let expected_props[0].end = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100675 unlet expected_props[3]
676 unlet expected_props[2]
677 call assert_equal(expected_props, prop_list(1))
678
679 let new_props[0].length = 6
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200680 let new_props[0].start = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100681 let new_props[1].col = 1
682 let new_props[1].length = 1
683 let new_props[2].col = 3
684 call assert_equal(new_props, prop_list(2))
685
686 call DeletePropTypes()
687 bwipe!
688endfunc
689
Bram Moolenaar663bc892019-01-08 23:07:24 +0100690func Test_prop_change_indent()
691 call prop_type_add('comment', {'highlight': 'Directory'})
692 new
693 call setline(1, [' xxx', 'yyyyy'])
694 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200695 let expect = #{type_bufnr: 0, col: 2, length: 2, type: 'comment', start: 1, end: 1, id: 0}
Bram Moolenaar663bc892019-01-08 23:07:24 +0100696 call assert_equal([expect], prop_list(2))
697
698 set shiftwidth=3
699 normal 2G>>
700 call assert_equal(' yyyyy', getline(2))
701 let expect.col += 3
702 call assert_equal([expect], prop_list(2))
703
704 normal 2G==
705 call assert_equal(' yyyyy', getline(2))
706 let expect.col = 6
707 call assert_equal([expect], prop_list(2))
708
709 call prop_clear(2)
710 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
711 let expect.col = 2
712 let expect.length = 5
713 call assert_equal([expect], prop_list(2))
714
715 normal 2G<<
716 call assert_equal(' yyyyy', getline(2))
717 let expect.length = 2
718 call assert_equal([expect], prop_list(2))
719
720 set shiftwidth&
721 call prop_type_delete('comment')
722endfunc
723
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100724" Setup a three line prop in lines 2 - 4.
725" Add short props in line 1 and 5.
726func Setup_three_line_prop()
727 new
728 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
729 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
730 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
731 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
732endfunc
733
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100734func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200735 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100736 new
737 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
738
739 " start halfway line 1, end halfway line 3
740 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200741 let expect1 = #{type_bufnr: 0, col: 3, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100742 call assert_equal([expect1], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200743 let expect2 = #{type_bufnr: 0, col: 1, length: 10, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100744 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200745 let expect3 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100746 call assert_equal([expect3], prop_list(3))
747 call prop_clear(1, 3)
748
749 " include all three lines
750 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
751 let expect1.col = 1
752 let expect1.length = 8
753 call assert_equal([expect1], prop_list(1))
754 call assert_equal([expect2], prop_list(2))
755 let expect3.length = 9
756 call assert_equal([expect3], prop_list(3))
757 call prop_clear(1, 3)
758
759 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100760
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100761 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100762 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200763 let expect_short = #{type_bufnr: 0, col: 2, length: 1, type: 'comment', start: 1, end: 1, id: 0}
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100764 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200765 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100766 call assert_equal([expect2], prop_list(2))
767 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100768 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200769 let expect2 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100770 call assert_equal([expect2], prop_list(2))
771 bwipe!
772
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100773 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100774 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200775 let expect3 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100776 call assert_equal([expect3], prop_list(3))
Martin Tournoije2390c72021-07-28 13:30:16 +0200777 let expect4 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100778 call assert_equal([expect4], prop_list(4))
779 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100780 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100781 call assert_equal([expect3], prop_list(3))
782 call assert_equal([expect_short], prop_list(4))
783 bwipe!
784
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100785 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100786 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200787 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100788 call assert_equal([expect2], prop_list(2))
789 call append(2, "new line")
790 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200791 let expect3 = #{type_bufnr: 0, col: 1, length: 9, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100792 call assert_equal([expect3], prop_list(3))
793 bwipe!
794
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100795 call prop_type_delete('comment')
796endfunc
797
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100798func Test_prop_line2byte()
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100799 call prop_type_add('comment', {'highlight': 'Directory'})
800 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100801 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100802 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100803 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100804 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100805 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100806
807 bwipe!
808 call prop_type_delete('comment')
809endfunc
810
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100811func Test_prop_byte2line()
812 new
813 set ff=unix
814 call setline(1, ['one one', 'two two', 'three three', 'four four', 'five'])
815 call assert_equal(4, byte2line(line2byte(4)))
816 call assert_equal(5, byte2line(line2byte(5)))
817
818 call prop_type_add('prop', {'highlight': 'Directory'})
819 call prop_add(3, 1, {'length': 5, 'type': 'prop'})
820 call assert_equal(4, byte2line(line2byte(4)))
821 call assert_equal(5, byte2line(line2byte(5)))
822
823 bwipe!
824 call prop_type_delete('prop')
825endfunc
826
Bram Moolenaar59ff6402021-01-30 17:16:28 +0100827func Test_prop_goto_byte()
828 new
829 call setline(1, '')
830 call setline(2, 'two three')
831 call setline(3, '')
832 call setline(4, 'four five')
833
834 call prop_type_add('testprop', {'highlight': 'Directory'})
835 call search('^two')
836 call prop_add(line('.'), col('.'), {
837 \ 'length': len('two'),
838 \ 'type': 'testprop'
839 \ })
840
841 call search('two \zsthree')
842 let expected_pos = line2byte(line('.')) + col('.') - 1
843 exe expected_pos .. 'goto'
844 let actual_pos = line2byte(line('.')) + col('.') - 1
845 eval actual_pos->assert_equal(expected_pos)
846
847 call search('four \zsfive')
848 let expected_pos = line2byte(line('.')) + col('.') - 1
849 exe expected_pos .. 'goto'
850 let actual_pos = line2byte(line('.')) + col('.') - 1
851 eval actual_pos->assert_equal(expected_pos)
852
853 call prop_type_delete('testprop')
854 bwipe!
855endfunc
856
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100857func Test_prop_undo()
858 new
859 call prop_type_add('comment', {'highlight': 'Directory'})
860 call setline(1, ['oneone', 'twotwo', 'three'])
861 " Set 'undolevels' to break changes into undo-able pieces.
862 set ul&
863
864 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200865 let expected = [#{type_bufnr: 0, col: 3, length: 2, id: 0, type: 'comment', start: 1, end: 1}]
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100866 call assert_equal(expected, prop_list(1))
867
868 " Insert a character, then undo.
869 exe "normal 0lllix\<Esc>"
870 set ul&
871 let expected[0].length = 3
872 call assert_equal(expected, prop_list(1))
873 undo
874 let expected[0].length = 2
875 call assert_equal(expected, prop_list(1))
876
877 " Delete a character, then undo
878 exe "normal 0lllx"
879 set ul&
880 let expected[0].length = 1
881 call assert_equal(expected, prop_list(1))
882 undo
883 let expected[0].length = 2
884 call assert_equal(expected, prop_list(1))
885
886 " Delete the line, then undo
887 1d
888 set ul&
889 call assert_equal([], prop_list(1))
890 undo
891 call assert_equal(expected, prop_list(1))
892
893 " Insert a character, delete two characters, then undo with "U"
894 exe "normal 0lllix\<Esc>"
895 set ul&
896 let expected[0].length = 3
897 call assert_equal(expected, prop_list(1))
898 exe "normal 0lllxx"
899 set ul&
900 let expected[0].length = 1
901 call assert_equal(expected, prop_list(1))
902 normal U
903 let expected[0].length = 2
904 call assert_equal(expected, prop_list(1))
905
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200906 " substitute a word, then undo
907 call setline(1, 'the number 123 is highlighted.')
908 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200909 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200910 call assert_equal(expected, prop_list(1))
911 set ul&
912 1s/number/foo
913 let expected[0].col = 9
914 call assert_equal(expected, prop_list(1))
915 undo
916 let expected[0].col = 12
917 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200918 call prop_clear(1)
919
920 " substitute with backslash
921 call setline(1, 'the number 123 is highlighted.')
922 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200923 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200924 call assert_equal(expected, prop_list(1))
925 1s/the/\The
926 call assert_equal(expected, prop_list(1))
927 1s/^/\\
928 let expected[0].col += 1
929 call assert_equal(expected, prop_list(1))
930 1s/^/\~
931 let expected[0].col += 1
932 call assert_equal(expected, prop_list(1))
933 1s/123/12\\3
934 let expected[0].length += 1
935 call assert_equal(expected, prop_list(1))
936 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200937
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100938 bwipe!
939 call prop_type_delete('comment')
940endfunc
941
Bram Moolenaarecafcc12019-11-16 20:41:51 +0100942func Test_prop_delete_text()
943 new
944 call prop_type_add('comment', {'highlight': 'Directory'})
945 call setline(1, ['oneone', 'twotwo', 'three'])
946
947 " zero length property
948 call prop_add(1, 3, {'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200949 let expected = [#{type_bufnr: 0, col: 3, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +0100950 call assert_equal(expected, prop_list(1))
951
952 " delete one char moves the property
953 normal! x
Martin Tournoije2390c72021-07-28 13:30:16 +0200954 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +0100955 call assert_equal(expected, prop_list(1))
956
957 " delete char of the property has no effect
958 normal! lx
Martin Tournoije2390c72021-07-28 13:30:16 +0200959 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +0100960 call assert_equal(expected, prop_list(1))
961
962 " delete more chars moves property to first column, is not deleted
963 normal! 0xxxx
Martin Tournoije2390c72021-07-28 13:30:16 +0200964 let expected = [#{type_bufnr: 0, col: 1, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +0100965 call assert_equal(expected, prop_list(1))
966
967 bwipe!
968 call prop_type_delete('comment')
969endfunc
970
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100971" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +0200972func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +0200973 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +0100974 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +0200975 if g:orig_encoding != 'utf-8'
976 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100977 endif
978 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +0200979 \ "call setline(1, ["
980 \ .. "'One two',"
981 \ .. "'Numbér 123 änd thœn 4¾7.',"
982 \ .. "'--aa--bb--cc--dd--',"
983 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +0200984 \ .. "'first line',"
985 \ .. "' second line ',"
986 \ .. "'third line',"
987 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +0200988 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100989 \ "hi NumberProp ctermfg=blue",
990 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +0200991 \ "hi BackgroundProp ctermbg=lightgrey",
992 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +0100993 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +0200994 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
995 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +0100996 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
997 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
998 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +0100999 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
1000 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
1001 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +01001002 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001003 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001004 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
1005 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001006 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
1007 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
1008 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
1009 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001010 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
1011 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001012 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001013 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
1014 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
1015 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
1016 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +02001017 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001018 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001019 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001020 \ "syn match Comment '//.*'",
1021 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001022 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01001023 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001024 \ "normal 6G0i\<BS>\<Esc>",
1025 \ "normal 3J",
1026 \ "normal 3G",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001027 \], 'XtestProp')
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001028 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001029 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +01001030
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001031 " clean up
1032 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +01001033 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001034endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +02001035
1036func RunTestVisualBlock(width, dump)
1037 call writefile([
1038 \ "call setline(1, ["
1039 \ .. "'xxxxxxxxx 123 x',"
1040 \ .. "'xxxxxxxx 123 x',"
1041 \ .. "'xxxxxxx 123 x',"
1042 \ .. "'xxxxxx 123 x',"
1043 \ .. "'xxxxx 123 x',"
1044 \ .. "'xxxx 123 xx',"
1045 \ .. "'xxx 123 xxx',"
1046 \ .. "'xx 123 xxxx',"
1047 \ .. "'x 123 xxxxx',"
1048 \ .. "' 123 xxxxxx',"
1049 \ .. "])",
1050 \ "hi SearchProp ctermbg=yellow",
1051 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
1052 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
1053 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
1054 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
1055 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
1056 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
1057 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
1058 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
1059 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
1060 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
1061 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
1062 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
1063 \], 'XtestPropVis')
1064 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
1065 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
1066
1067 " clean up
1068 call StopVimInTerminal(buf)
1069 call delete('XtestPropVis')
1070endfunc
1071
1072" screenshot test with Visual block mode operations
1073func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +02001074 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +02001075
1076 " Delete two columns while text props are three chars wide.
1077 call RunTestVisualBlock(2, '01')
1078
1079 " Same, but delete four columns
1080 call RunTestVisualBlock(4, '02')
1081endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +02001082
Bram Moolenaara956bf62019-06-19 17:34:24 +02001083func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +02001084 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +02001085
Bram Moolenaara956bf62019-06-19 17:34:24 +02001086 let lines =<< trim END
1087 call setline(1, [
1088 \ "\txxx",
1089 \ "x\txxx",
1090 \ ])
1091 hi SearchProp ctermbg=yellow
1092 call prop_type_add('search', {'highlight': 'SearchProp'})
1093 call prop_add(1, 2, {'length': 3, 'type': 'search'})
1094 call prop_add(2, 3, {'length': 3, 'type': 'search'})
1095 END
1096 call writefile(lines, 'XtestPropTab')
1097 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
1098 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
1099
1100 " clean up
1101 call StopVimInTerminal(buf)
1102 call delete('XtestPropTab')
1103endfunc
1104
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001105func Test_textprop_nowrap_scrolled()
1106 CheckScreendump
1107
1108 let lines =<< trim END
1109 vim9script
1110 set nowrap
1111 setline(1, 'The number 123 is smaller than 4567.' .. repeat('X', &columns))
1112 prop_type_add('number', {'highlight': 'ErrorMsg'})
1113 prop_add(1, 12, {'length': 3, 'type': 'number'})
1114 prop_add(1, 32, {'length': 4, 'type': 'number'})
1115 feedkeys('gg20zl', 'nxt')
1116 END
1117 call writefile(lines, 'XtestNowrap')
1118 let buf = RunVimInTerminal('-S XtestNowrap', {'rows': 6})
1119 call VerifyScreenDump(buf, 'Test_textprop_nowrap_01', {})
1120
1121 call term_sendkeys(buf, "$")
1122 call VerifyScreenDump(buf, 'Test_textprop_nowrap_02', {})
1123
1124 " clean up
1125 call StopVimInTerminal(buf)
1126 call delete('XtestNowrap')
1127endfunc
1128
Bram Moolenaar34390282019-10-16 14:38:26 +02001129func Test_textprop_with_syntax()
1130 CheckScreendump
1131
1132 let lines =<< trim END
1133 call setline(1, [
1134 \ "(abc)",
1135 \ ])
1136 syn match csParens "[()]" display
1137 hi! link csParens MatchParen
1138
1139 call prop_type_add('TPTitle', #{ highlight: 'Title' })
1140 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
1141 END
1142 call writefile(lines, 'XtestPropSyn')
1143 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
1144 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
1145
1146 " clean up
1147 call StopVimInTerminal(buf)
1148 call delete('XtestPropSyn')
1149endfunc
1150
Bram Moolenaard79eef22019-05-24 20:41:55 +02001151" Adding a text property to a new buffer should not fail
1152func Test_textprop_empty_buffer()
1153 call prop_type_add('comment', {'highlight': 'Search'})
1154 new
1155 call prop_add(1, 1, {'type': 'comment'})
1156 close
Bram Moolenaaradfde112019-05-25 22:11:45 +02001157 call prop_type_delete('comment')
1158endfunc
1159
Bram Moolenaard74af422019-06-28 21:38:00 +02001160" Adding a text property with invalid highlight should be ignored.
1161func Test_textprop_invalid_highlight()
1162 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
1163 new
1164 call setline(1, ['asdf','asdf'])
1165 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
1166 redraw
1167 bwipe!
1168 call prop_type_delete('dni')
1169endfunc
1170
Bram Moolenaaradfde112019-05-25 22:11:45 +02001171" Adding a text property to an empty buffer and then editing another
1172func Test_textprop_empty_buffer_next()
1173 call prop_type_add("xxx", {})
1174 call prop_add(1, 1, {"type": "xxx"})
1175 next X
1176 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +02001177endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001178
1179func Test_textprop_remove_from_buf()
1180 new
1181 let buf = bufnr('')
1182 call prop_type_add('one', {'bufnr': buf})
1183 call prop_add(1, 1, {'type': 'one', 'id': 234})
1184 file x
1185 edit y
1186 call prop_remove({'id': 234, 'bufnr': buf}, 1)
1187 call prop_type_delete('one', {'bufnr': buf})
1188 bwipe! x
1189 close
1190endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +02001191
1192func Test_textprop_in_unloaded_buf()
1193 edit Xaaa
1194 call setline(1, 'aaa')
1195 write
1196 edit Xbbb
1197 call setline(1, 'bbb')
1198 write
1199 let bnr = bufnr('')
1200 edit Xaaa
1201
1202 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
1203 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
1204 exe 'buf ' .. bnr
1205 call assert_equal('bbb', getline(1))
1206 call assert_equal(0, prop_list(1)->len())
1207
1208 bwipe! Xaaa
1209 bwipe! Xbbb
1210 cal delete('Xaaa')
1211 cal delete('Xbbb')
1212endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001213
1214func Test_proptype_substitute2()
1215 new
1216 " text_prop.vim
1217 call setline(1, [
1218 \ 'The num 123 is smaller than 4567.',
1219 \ '123 The number 123 is smaller than 4567.',
1220 \ '123 The number 123 is smaller than 4567.'])
1221
1222 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1223
1224 call prop_add(1, 12, {'length': 3, 'type': 'number'})
1225 call prop_add(2, 1, {'length': 3, 'type': 'number'})
1226 call prop_add(3, 36, {'length': 4, 'type': 'number'})
1227 set ul&
Martin Tournoije2390c72021-07-28 13:30:16 +02001228 let expected = [
1229 \ #{type_bufnr: 0, id: 0, col: 13, end: 1, type: 'number', length: 3, start: 1},
1230 \ #{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'number', length: 3, start: 1},
1231 \ #{type_bufnr: 0, id: 0, col: 50, end: 1, type: 'number', length: 4, start: 1}]
1232
1233 " TODO
1234 return
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001235 " Add some text in between
1236 %s/\s\+/ /g
Martin Tournoije2390c72021-07-28 13:30:16 +02001237 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001238
1239 " remove some text
1240 :1s/[a-z]\{3\}//g
1241 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
1242 call assert_equal(expected, prop_list(1))
1243 bwipe!
1244endfunc
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001245
Bram Moolenaar8902b312020-09-20 21:04:35 +02001246" This was causing property corruption.
1247func Test_proptype_substitute3()
1248 new
1249 call setline(1, ['abcxxx', 'def'])
1250 call prop_type_add("test", {"highlight": "Search"})
1251 call prop_add(1, 2, {"end_lnum": 2, "end_col": 2, "type": "test"})
1252 %s/x\+$//
1253 redraw
1254
1255 call prop_type_delete('test')
1256 bwipe!
1257endfunc
1258
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001259func SaveOptions()
1260 let d = #{tabstop: &tabstop,
1261 \ softtabstop: &softtabstop,
1262 \ shiftwidth: &shiftwidth,
1263 \ expandtab: &expandtab,
1264 \ foldmethod: '"' .. &foldmethod .. '"',
1265 \ }
1266 return d
1267endfunc
1268
1269func RestoreOptions(dict)
1270 for name in keys(a:dict)
1271 exe 'let &' .. name .. ' = ' .. a:dict[name]
1272 endfor
1273endfunc
1274
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001275func Test_textprop_noexpandtab()
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001276 new
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001277 let save_dict = SaveOptions()
1278
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001279 set tabstop=8
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001280 set softtabstop=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001281 set shiftwidth=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001282 set noexpandtab
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001283 set foldmethod=marker
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001284
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001285 call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx")
1286 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1287 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1288 call feedkeys("0i\<tab>", "tx")
1289 call prop_remove({'type': 'test'})
1290 call prop_add(1, 2, {'end_col': 3, 'type': 'test'})
1291 call feedkeys("A\<left>\<tab>", "tx")
1292 call prop_remove({'type': 'test'})
1293 try
1294 " It is correct that this does not pass
1295 call prop_add(1, 6, {'end_col': 7, 'type': 'test'})
1296 " Has already collapsed here, start_col:6 does not result in an error
1297 call feedkeys("A\<left>\<tab>", "tx")
1298 catch /^Vim\%((\a\+)\)\=:E964/
1299 endtry
1300 call prop_remove({'type': 'test'})
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001301 call prop_type_delete('test')
1302
1303 call RestoreOptions(save_dict)
1304 bwipe!
1305endfunc
1306
1307func Test_textprop_noexpandtab_redraw()
1308 new
1309 let save_dict = SaveOptions()
1310
1311 set tabstop=8
1312 set softtabstop=4
1313 set shiftwidth=4
1314 set noexpandtab
1315 set foldmethod=marker
1316
1317 call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx")
1318 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1319 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1320 call feedkeys("0i\<tab>", "tx")
1321 " Internally broken at the next line
1322 call feedkeys("A\<left>\<tab>", "tx")
1323 redraw
1324 " Index calculation failed internally on next line
1325 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1326 call prop_remove({'type': 'test', 'all': v:true})
1327 call prop_type_delete('test')
1328 call prop_type_delete('test')
1329
1330 call RestoreOptions(save_dict)
1331 bwipe!
1332endfunc
1333
1334func Test_textprop_ins_str()
1335 new
1336 call setline(1, 'just some text')
1337 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1338 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001339 call assert_equal([#{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'test', length: 1, start: 1}], prop_list(1))
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001340
1341 call feedkeys("foi\<F8>\<Esc>", "tx")
1342 call assert_equal('just s<F8>ome text', getline(1))
Martin Tournoije2390c72021-07-28 13:30:16 +02001343 call assert_equal([#{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'test', length: 1, start: 1}], prop_list(1))
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001344
1345 bwipe!
1346 call prop_remove({'type': 'test'})
1347 call prop_type_delete('test')
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001348endfunc
Bram Moolenaar66b98852020-03-11 19:15:52 +01001349
1350func Test_find_prop_later_in_line()
1351 new
1352 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1353 call setline(1, 'just some text')
1354 call prop_add(1, 1, {'length': 4, 'type': 'test'})
1355 call prop_add(1, 10, {'length': 3, 'type': 'test'})
1356
Martin Tournoije2390c72021-07-28 13:30:16 +02001357 call assert_equal(
1358 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 10, end: 1, type: 'test', length: 3, start: 1},
1359 \ prop_find(#{type: 'test', lnum: 1, col: 6}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001360
1361 bwipe!
1362 call prop_type_delete('test')
1363endfunc
1364
1365func Test_find_zerowidth_prop_sol()
1366 new
1367 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1368 call setline(1, 'just some text')
1369 call prop_add(1, 1, {'length': 0, 'type': 'test'})
1370
Martin Tournoije2390c72021-07-28 13:30:16 +02001371 call assert_equal(
1372 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 1, end: 1, type: 'test', length: 0, start: 1},
1373 \ prop_find(#{type: 'test', lnum: 1}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001374
1375 bwipe!
1376 call prop_type_delete('test')
1377endfunc
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001378
1379" Test for passing invalid arguments to prop_xxx() functions
1380func Test_prop_func_invalid_args()
1381 call assert_fails('call prop_clear(1, 2, [])', 'E715:')
1382 call assert_fails('call prop_clear(-1, 2)', 'E16:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001383 call assert_fails('call prop_find(test_null_dict())', 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001384 call assert_fails('call prop_find({"bufnr" : []})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001385 call assert_fails('call prop_find({})', 'E968:')
1386 call assert_fails('call prop_find({}, "x")', 'E474:')
1387 call assert_fails('call prop_find({"lnum" : -2})', 'E16:')
1388 call assert_fails('call prop_list(1, [])', 'E715:')
Bram Moolenaar9d489562020-07-30 20:08:50 +02001389 call assert_fails('call prop_list(-1, {})', 'E16:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001390 call assert_fails('call prop_remove([])', 'E474:')
1391 call assert_fails('call prop_remove({}, -2)', 'E16:')
1392 call assert_fails('call prop_remove({})', 'E968:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001393 call assert_fails('call prop_type_add([], {})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001394 call assert_fails("call prop_type_change('long', {'xyz' : 10})", 'E971:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001395 call assert_fails("call prop_type_delete([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001396 call assert_fails("call prop_type_delete('xyz', [])", 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001397 call assert_fails("call prop_type_get([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001398 call assert_fails("call prop_type_get('', [])", 'E474:')
1399 call assert_fails("call prop_type_list([])", 'E715:')
Bram Moolenaar3dc34742021-03-02 13:36:47 +01001400 call assert_fails("call prop_type_add('yyy', 'not_a_dict')", 'E715:')
1401 call assert_fails("call prop_add(1, 5, {'type':'missing_type', 'length':1})", 'E971:')
1402 call assert_fails("call prop_add(1, 5, {'type': ''})", 'E971:')
1403 call assert_fails('call prop_add(1, 1, 0)', 'E715:')
1404
1405 new
1406 call setline(1, ['first', 'second'])
1407 call prop_type_add('xxx', {})
1408
1409 call assert_fails("call prop_type_add('xxx', {})", 'E969:')
1410 call assert_fails("call prop_add(2, 0, {'type': 'xxx'})", 'E964:')
1411 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':1})", 'E475:')
1412 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':3})", 'E966:')
1413 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'length':-1})", 'E475:')
1414 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_col':0})", 'E475:')
1415 call assert_fails("call prop_add(2, 3, {'length':1})", 'E965:')
1416
1417 call prop_type_delete('xxx')
1418 bwipe!
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001419endfunc
1420
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001421func Test_prop_split_join()
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001422 new
1423 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1424 call setline(1, 'just some text')
1425 call prop_add(1, 6, {'length': 4, 'type': 'test'})
1426
1427 " Split in middle of "some"
1428 execute "normal! 8|i\<CR>"
Martin Tournoije2390c72021-07-28 13:30:16 +02001429 call assert_equal(
1430 \ [#{type_bufnr: 0, id: 0, col: 6, end: 0, type: 'test', length: 2, start: 1}],
1431 \ prop_list(1))
1432 call assert_equal(
1433 \ [#{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'test', length: 2, start: 0}],
1434 \ prop_list(2))
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001435
1436 " Join the two lines back together
1437 normal! 1GJ
Martin Tournoije2390c72021-07-28 13:30:16 +02001438 call assert_equal([#{type_bufnr: 0, id: 0, col: 6, end: 1, type: 'test', length: 5, start: 1}], prop_list(1))
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001439
1440 bwipe!
1441 call prop_type_delete('test')
1442endfunc
1443
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001444func Test_prop_increment_decrement()
1445 new
1446 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1447 call setline(1, 'its 998 times')
1448 call prop_add(1, 5, {'length': 3, 'type': 'test'})
1449
1450 exe "normal! 0f9\<C-A>"
1451 eval getline(1)->assert_equal('its 999 times')
1452 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001453 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 3, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001454
1455 exe "normal! 0f9\<C-A>"
1456 eval getline(1)->assert_equal('its 1000 times')
1457 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001458 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 4, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001459
1460 bwipe!
1461 call prop_type_delete('test')
1462endfunc
1463
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001464func Test_prop_block_insert()
1465 new
1466 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1467 call setline(1, ['one ', 'two '])
1468 call prop_add(1, 1, {'length': 3, 'type': 'test'})
1469 call prop_add(2, 1, {'length': 3, 'type': 'test'})
1470
1471 " insert "xx" in the first column of both lines
1472 exe "normal! gg0\<C-V>jIxx\<Esc>"
1473 eval getline(1, 2)->assert_equal(['xxone ', 'xxtwo '])
Martin Tournoije2390c72021-07-28 13:30:16 +02001474 let expected = [#{type_bufnr: 0, id: 0, col: 3, end: 1, type: 'test', length: 3, start: 1}]
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001475 eval prop_list(1)->assert_equal(expected)
1476 eval prop_list(2)->assert_equal(expected)
1477
1478 " insert "yy" inside the text props to make them longer
1479 exe "normal! gg03l\<C-V>jIyy\<Esc>"
1480 eval getline(1, 2)->assert_equal(['xxoyyne ', 'xxtyywo '])
1481 let expected[0].length = 5
1482 eval prop_list(1)->assert_equal(expected)
1483 eval prop_list(2)->assert_equal(expected)
1484
1485 " insert "zz" after the text props, text props don't change
1486 exe "normal! gg07l\<C-V>jIzz\<Esc>"
1487 eval getline(1, 2)->assert_equal(['xxoyynezz ', 'xxtyywozz '])
1488 eval prop_list(1)->assert_equal(expected)
1489 eval prop_list(2)->assert_equal(expected)
1490
1491 bwipe!
1492 call prop_type_delete('test')
1493endfunc
1494
Bram Moolenaar23999d72020-12-23 14:36:00 +01001495" this was causing an ml_get error because w_botline was wrong
1496func Test_prop_one_line_window()
1497 enew
1498 call range(2)->setline(1)
1499 call prop_type_add('testprop', {})
1500 call prop_add(1, 1, {'type': 'testprop'})
1501 call popup_create('popup', {'textprop': 'testprop'})
1502 $
1503 new
1504 wincmd _
1505 call feedkeys("\r", 'xt')
1506 redraw
1507
1508 call popup_clear()
1509 call prop_type_delete('testprop')
1510 close
1511 bwipe!
1512endfunc
1513
Bram Moolenaar840f91f2021-05-26 22:32:10 +02001514" This was calling ml_append_int() and copy a text property from a previous
1515" line at the wrong moment. Exact text length matters.
1516def Test_prop_splits_data_block()
1517 new
1518 var lines: list<string> = [repeat('x', 35)]->repeat(41)
1519 + [repeat('!', 35)]
1520 + [repeat('x', 35)]->repeat(56)
1521 lines->setline(1)
1522 prop_type_add('someprop', {highlight: 'ErrorMsg'})
1523 prop_add(1, 27, {end_lnum: 1, end_col: 70, type: 'someprop'})
1524 prop_remove({type: 'someprop'}, 1)
1525 prop_add(35, 22, {end_lnum: 43, end_col: 43, type: 'someprop'})
1526 prop_remove({type: 'someprop'}, 35, 43)
1527 assert_equal([], prop_list(42))
1528
1529 bwipe!
1530 prop_type_delete('someprop')
1531enddef
1532
Bram Moolenaar4cd5c522021-06-27 13:04:00 +02001533" This was calling ml_delete_int() and try to change text properties.
1534def Test_prop_add_delete_line()
1535 new
1536 var a = 10
1537 var b = 20
1538 repeat([''], a)->append('$')
1539 prop_type_add('Test', {highlight: 'ErrorMsg'})
1540 for lnum in range(1, a)
1541 for col in range(1, b)
1542 prop_add(1, 1, {end_lnum: lnum, end_col: col, type: 'Test'})
1543 endfor
1544 endfor
1545
1546 # check deleting lines is OK
1547 :5del
1548 :1del
1549 :$del
1550
1551 prop_type_delete('Test')
1552 bwipe!
1553enddef
1554
Martin Tournoije2390c72021-07-28 13:30:16 +02001555" Buffer number of 0 should be ignored, as if the parameter wasn't passed.
1556def Test_prop_bufnr_zero()
1557 new
1558 try
1559 var bufnr = bufnr('')
1560 setline(1, 'hello')
1561 prop_type_add('bufnr-global', {highlight: 'ErrorMsg'})
1562 prop_type_add('bufnr-buffer', {highlight: 'StatusLine', bufnr: bufnr})
1563
1564 prop_add(1, 1, {type: 'bufnr-global', length: 1})
1565 prop_add(1, 2, {type: 'bufnr-buffer', length: 1})
1566
1567 var list = prop_list(1)
1568 assert_equal([
1569 {id: 0, col: 1, type_bufnr: 0, end: 1, type: 'bufnr-global', length: 1, start: 1},
1570 {id: 0, col: 2, type_bufnr: bufnr, end: 1, type: 'bufnr-buffer', length: 1, start: 1},
1571 ], list)
1572
1573 assert_equal(
1574 {highlight: 'ErrorMsg', end_incl: 0, start_incl: 0, priority: 0, combine: 1},
1575 prop_type_get('bufnr-global', {bufnr: list[0].type_bufnr}))
1576
1577 assert_equal(
1578 {highlight: 'StatusLine', end_incl: 0, start_incl: 0, priority: 0, bufnr: bufnr, combine: 1},
1579 prop_type_get('bufnr-buffer', {bufnr: list[1].type_bufnr}))
1580 finally
1581 bwipe!
1582 prop_type_delete('bufnr-global')
1583 endtry
1584enddef
1585
1586
Bram Moolenaar23999d72020-12-23 14:36:00 +01001587
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001588" vim: shiftwidth=2 sts=2 expandtab