blob: a2ba477e7147f3c0b68ce9e507d8054d16901327 [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
Bram Moolenaar62aec932022-01-29 21:45:34 +00008import './vim9.vim' as v9
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01009
Bram Moolenaar98aefe72018-12-13 22:20:09 +010010func Test_proptype_global()
11 call prop_type_add('comment', {'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1})
12 let proptypes = prop_type_list()
13 call assert_equal(1, len(proptypes))
14 call assert_equal('comment', proptypes[0])
15
16 let proptype = prop_type_get('comment')
17 call assert_equal('Directory', proptype['highlight'])
18 call assert_equal(123, proptype['priority'])
19 call assert_equal(1, proptype['start_incl'])
20 call assert_equal(1, proptype['end_incl'])
21
22 call prop_type_delete('comment')
23 call assert_equal(0, len(prop_type_list()))
24
25 call prop_type_add('one', {})
26 call assert_equal(1, len(prop_type_list()))
Bram Moolenaara5a78822019-09-04 21:57:18 +020027 let proptype = 'one'->prop_type_get()
Bram Moolenaar98aefe72018-12-13 22:20:09 +010028 call assert_false(has_key(proptype, 'highlight'))
29 call assert_equal(0, proptype['priority'])
30 call assert_equal(0, proptype['start_incl'])
31 call assert_equal(0, proptype['end_incl'])
32
33 call prop_type_add('two', {})
34 call assert_equal(2, len(prop_type_list()))
35 call prop_type_delete('one')
36 call assert_equal(1, len(prop_type_list()))
37 call prop_type_delete('two')
38 call assert_equal(0, len(prop_type_list()))
39endfunc
40
41func Test_proptype_buf()
42 let bufnr = bufnr('')
Martin Tournoije2390c72021-07-28 13:30:16 +020043 call prop_type_add('comment', #{bufnr: bufnr, highlight: 'Directory', priority: 123, start_incl: 1, end_incl: 1})
Bram Moolenaar98aefe72018-12-13 22:20:09 +010044 let proptypes = prop_type_list({'bufnr': bufnr})
45 call assert_equal(1, len(proptypes))
46 call assert_equal('comment', proptypes[0])
47
48 let proptype = prop_type_get('comment', {'bufnr': bufnr})
49 call assert_equal('Directory', proptype['highlight'])
50 call assert_equal(123, proptype['priority'])
51 call assert_equal(1, proptype['start_incl'])
52 call assert_equal(1, proptype['end_incl'])
53
54 call prop_type_delete('comment', {'bufnr': bufnr})
Bram Moolenaara5a78822019-09-04 21:57:18 +020055 call assert_equal(0, len({'bufnr': bufnr}->prop_type_list()))
Bram Moolenaar98aefe72018-12-13 22:20:09 +010056
57 call prop_type_add('one', {'bufnr': bufnr})
58 let proptype = prop_type_get('one', {'bufnr': bufnr})
59 call assert_false(has_key(proptype, 'highlight'))
60 call assert_equal(0, proptype['priority'])
61 call assert_equal(0, proptype['start_incl'])
62 call assert_equal(0, proptype['end_incl'])
63
64 call prop_type_add('two', {'bufnr': bufnr})
65 call assert_equal(2, len(prop_type_list({'bufnr': bufnr})))
66 call prop_type_delete('one', {'bufnr': bufnr})
67 call assert_equal(1, len(prop_type_list({'bufnr': bufnr})))
68 call prop_type_delete('two', {'bufnr': bufnr})
69 call assert_equal(0, len(prop_type_list({'bufnr': bufnr})))
Bram Moolenaarf0884c52019-05-24 21:22:29 +020070
71 call assert_fails("call prop_type_add('one', {'bufnr': 98764})", "E158:")
Bram Moolenaar98aefe72018-12-13 22:20:09 +010072endfunc
73
Martin Tournoije2390c72021-07-28 13:30:16 +020074def Test_proptype_buf_list()
75 new
76 var bufnr = bufnr('')
77 try
78 prop_type_add('global', {})
79 prop_type_add('local', {bufnr: bufnr})
80
81 prop_add(1, 1, {type: 'global'})
82 prop_add(1, 1, {type: 'local'})
83
84 assert_equal([
85 {type: 'local', type_bufnr: bufnr, id: 0, col: 1, end: 1, length: 0, start: 1},
86 {type: 'global', type_bufnr: 0, id: 0, col: 1, end: 1, length: 0, start: 1},
87 ], prop_list(1))
88 assert_equal(
89 {lnum: 1, id: 0, col: 1, type_bufnr: bufnr, end: 1, type: 'local', length: 0, start: 1},
90 prop_find({lnum: 1, type: 'local'}))
91 assert_equal(
92 {lnum: 1, id: 0, col: 1, type_bufnr: 0, end: 1, type: 'global', length: 0, start: 1},
93 prop_find({lnum: 1, type: 'global'}))
94
95 prop_remove({type: 'global'}, 1)
96 prop_remove({type: 'local'}, 1)
97 finally
98 prop_type_delete('global')
99 prop_type_delete('local', {bufnr: bufnr})
100 bwipe!
101 endtry
102enddef
103
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100104func AddPropTypes()
105 call prop_type_add('one', {})
106 call prop_type_add('two', {})
107 call prop_type_add('three', {})
108 call prop_type_add('whole', {})
109endfunc
110
111func DeletePropTypes()
112 call prop_type_delete('one')
113 call prop_type_delete('two')
114 call prop_type_delete('three')
115 call prop_type_delete('whole')
116endfunc
117
118func SetupPropsInFirstLine()
119 call setline(1, 'one two three')
120 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
Bram Moolenaara5a78822019-09-04 21:57:18 +0200121 eval 1->prop_add(5, {'length': 3, 'id': 12, 'type': 'two'})
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100122 call prop_add(1, 9, {'length': 5, 'id': 13, 'type': 'three'})
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100123 call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
124endfunc
125
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100126func Get_expected_props()
127 return [
Martin Tournoije2390c72021-07-28 13:30:16 +0200128 \ #{type_bufnr: 0, col: 1, length: 13, id: 14, type: 'whole', start: 1, end: 1},
129 \ #{type_bufnr: 0, col: 1, length: 3, id: 11, type: 'one', start: 1, end: 1},
130 \ #{type_bufnr: 0, col: 5, length: 3, id: 12, type: 'two', start: 1, end: 1},
131 \ #{type_bufnr: 0, col: 9, length: 5, id: 13, type: 'three', start: 1, end: 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100132 \ ]
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100133endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100134
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100135func Test_prop_find()
136 new
137 call setline(1, ['one one one', 'twotwo', 'three', 'fourfour', 'five', 'sixsix'])
Martin Tournoije2390c72021-07-28 13:30:16 +0200138
139 " Add two text props on lines 1 and 5, and one spanning lines 2 to 4.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100140 call prop_type_add('prop_name', {'highlight': 'Directory'})
141 call prop_add(1, 5, {'type': 'prop_name', 'id': 10, 'length': 3})
142 call prop_add(2, 4, {'type': 'prop_name', 'id': 11, 'end_lnum': 4, 'end_col': 9})
143 call prop_add(5, 4, {'type': 'prop_name', 'id': 12, 'length': 1})
144
145 let expected = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200146 \ #{type_bufnr: 0, lnum: 1, col: 5, length: 3, id: 10, type: 'prop_name', start: 1, end: 1},
147 \ #{type_bufnr: 0, lnum: 2, col: 4, id: 11, type: 'prop_name', start: 1, end: 0},
148 \ #{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 +0100149 \ ]
150
151 " Starting at line 5 col 1 this should find the prop at line 5 col 4.
152 call cursor(5,1)
153 let result = prop_find({'type': 'prop_name'}, 'f')
154 call assert_equal(expected[2], result)
155
156 " With skipstart left at false (default), this should find the prop at line
157 " 5 col 4.
158 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4}, 'b')
159 call assert_equal(expected[2], result)
160
161 " With skipstart set to true, this should skip the prop at line 5 col 4.
162 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4, 'skipstart': 1}, 'b')
163 unlet result.length
164 call assert_equal(expected[1], result)
165
166 " Search backwards from line 1 col 10 to find the prop on the same line.
167 let result = prop_find({'type': 'prop_name', 'lnum': 1, 'col': 10}, 'b')
168 call assert_equal(expected[0], result)
169
170 " with skipstart set to false, if the start position is anywhere between the
171 " start and end lines of a text prop (searching forward or backward), the
172 " result should be the prop on the first line (the line with 'start' set to 1).
173 call cursor(3,1)
174 let result = prop_find({'type': 'prop_name'}, 'f')
175 unlet result.length
176 call assert_equal(expected[1], result)
177 let result = prop_find({'type': 'prop_name'}, 'b')
178 unlet result.length
179 call assert_equal(expected[1], result)
180
181 " with skipstart set to true, if the start position is anywhere between the
182 " start and end lines of a text prop (searching forward or backward), all lines
183 " of the prop will be skipped.
184 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'b')
185 call assert_equal(expected[0], result)
186 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'f')
187 call assert_equal(expected[2], result)
188
189 " Use skipstart to search through all props with type name 'prop_name'.
190 " First forward...
191 let lnum = 1
192 let col = 1
193 let i = 0
194 for exp in expected
195 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'f')
196 if !has_key(exp, "length")
197 unlet result.length
198 endif
199 call assert_equal(exp, result)
200 let lnum = result.lnum
201 let col = result.col
202 let i = i + 1
203 endfor
204
205 " ...then backwards.
206 let lnum = 6
207 let col = 4
208 let i = 2
209 while i >= 0
210 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'b')
211 if !has_key(expected[i], "length")
212 unlet result.length
213 endif
214 call assert_equal(expected[i], result)
215 let lnum = result.lnum
216 let col = result.col
217 let i = i - 1
Martin Tournoije2390c72021-07-28 13:30:16 +0200218 endwhile
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100219
220 " Starting from line 6 col 1 search backwards for prop with id 10.
221 call cursor(6,1)
222 let result = prop_find({'id': 10, 'skipstart': 1}, 'b')
223 call assert_equal(expected[0], result)
224
225 " Starting from line 1 col 1 search forwards for prop with id 12.
226 call cursor(1,1)
227 let result = prop_find({'id': 12}, 'f')
228 call assert_equal(expected[2], result)
229
230 " Search for a prop with an unknown id.
231 let result = prop_find({'id': 999}, 'f')
232 call assert_equal({}, result)
233
234 " Search backwards from the proceeding position of the prop with id 11
235 " (at line num 2 col 4). This should return an empty dict.
236 let result = prop_find({'id': 11, 'lnum': 2, 'col': 3}, 'b')
237 call assert_equal({}, result)
238
239 " When lnum is given and col is omitted, use column 1.
240 let result = prop_find({'type': 'prop_name', 'lnum': 1}, 'f')
241 call assert_equal(expected[0], result)
242
Bram Moolenaare041dde2021-08-01 21:30:12 +0200243 " Negative ID is possible, just like prop is not found.
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200244 call assert_equal({}, prop_find({'id': -1}))
Bram Moolenaare041dde2021-08-01 21:30:12 +0200245 call assert_equal({}, prop_find({'id': -2}))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200246
Bram Moolenaare041dde2021-08-01 21:30:12 +0200247 call prop_clear(1, 6)
248
249 " Default ID is zero
250 call prop_add(5, 4, {'type': 'prop_name', 'length': 1})
251 call assert_equal(#{lnum: 5, id: 0, col: 4, type_bufnr: 0, end: 1, type: 'prop_name', length: 1, start: 1}, prop_find({'id': 0}))
252
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100253 call prop_type_delete('prop_name')
Bram Moolenaare041dde2021-08-01 21:30:12 +0200254 call prop_clear(1, 6)
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200255 bwipe!
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100256endfunc
257
Bram Moolenaareb245562020-09-03 22:33:44 +0200258def Test_prop_find2()
259 # Multiple props per line, start on the first, should find the second.
260 new
261 ['the quikc bronw fox jumsp over the layz dog']->repeat(2)->setline(1)
Bram Moolenaare0de1712020-12-02 17:36:54 +0100262 prop_type_add('misspell', {highlight: 'ErrorMsg'})
Bram Moolenaareb245562020-09-03 22:33:44 +0200263 for lnum in [1, 2]
264 for col in [8, 14, 24, 38]
Bram Moolenaare0de1712020-12-02 17:36:54 +0100265 prop_add(lnum, col, {type: 'misspell', length: 2})
Bram Moolenaareb245562020-09-03 22:33:44 +0200266 endfor
267 endfor
268 cursor(1, 8)
Martin Tournoije2390c72021-07-28 13:30:16 +0200269 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 +0100270 var result = prop_find({type: 'misspell', skipstart: true}, 'f')
Bram Moolenaareb245562020-09-03 22:33:44 +0200271 assert_equal(expected, result)
272
273 prop_type_delete('misspell')
274 bwipe!
275enddef
276
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100277func Test_prop_find_smaller_len_than_match_col()
278 new
279 call prop_type_add('test', {'highlight': 'ErrorMsg'})
280 call setline(1, ['xxxx', 'x'])
281 call prop_add(1, 4, {'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200282 call assert_equal(
283 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 4, type: 'test', length: 0, start: 1, end: 1},
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100284 \ prop_find({'type': 'test', 'lnum': 2, 'col': 1}, 'b'))
285 bwipe!
286 call prop_type_delete('test')
287endfunc
288
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100289func Test_prop_find_with_both_option_enabled()
290 " Initialize
291 new
292 call AddPropTypes()
293 call SetupPropsInFirstLine()
294 let props = Get_expected_props()->map({_, v -> extend(v, {'lnum': 1})})
295 " Test
296 call assert_fails("call prop_find({'both': 1})", 'E968:')
297 call assert_fails("call prop_find({'id': 11, 'both': 1})", 'E860:')
298 call assert_fails("call prop_find({'type': 'three', 'both': 1})", 'E860:')
299 call assert_equal({}, prop_find({'id': 11, 'type': 'three', 'both': 1}))
300 call assert_equal({}, prop_find({'id': 130000, 'type': 'one', 'both': 1}))
301 call assert_equal(props[2], prop_find({'id': 12, 'type': 'two', 'both': 1}))
302 call assert_equal(props[0], prop_find({'id': 14, 'type': 'whole', 'both': 1}))
303 " Clean up
304 call DeletePropTypes()
305 bwipe!
306endfunc
307
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100308func Test_prop_add()
309 new
310 call AddPropTypes()
311 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100312 let expected_props = Get_expected_props()
313 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100314 call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:')
315 call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:')
Martin Tournoije2390c72021-07-28 13:30:16 +0200316
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100317 " Insert a line above, text props must still be there.
318 call append(0, 'empty')
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100319 call assert_equal(expected_props, prop_list(2))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100320 " Delete a line above, text props must still be there.
321 1del
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100322 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100323
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100324 " Prop without length or end column is zero length
325 call prop_clear(1)
Bram Moolenaar12f20032020-02-26 22:06:00 +0100326 call prop_type_add('included', {'start_incl': 1, 'end_incl': 1})
327 call prop_add(1, 5, #{type: 'included'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200328 let expected = [#{type_bufnr: 0, col: 5, length: 0, type: 'included', id: 0, start: 1, end: 1}]
Bram Moolenaar12f20032020-02-26 22:06:00 +0100329 call assert_equal(expected, prop_list(1))
330
331 " Inserting text makes the prop bigger.
332 exe "normal 5|ixx\<Esc>"
Martin Tournoije2390c72021-07-28 13:30:16 +0200333 let expected = [#{type_bufnr: 0, col: 5, length: 2, type: 'included', id: 0, start: 1, end: 1}]
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100334 call assert_equal(expected, prop_list(1))
335
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200336 call assert_fails("call prop_add(1, 5, {'type': 'two', 'bufnr': 234343})", 'E158:')
337
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100338 call DeletePropTypes()
Bram Moolenaar12f20032020-02-26 22:06:00 +0100339 call prop_type_delete('included')
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100340 bwipe!
341endfunc
342
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200343" Test for the prop_add_list() function
344func Test_prop_add_list()
345 new
346 call AddPropTypes()
347 call setline(1, ['one one one', 'two two two', 'six six six', 'ten ten ten'])
348 call prop_add_list(#{type: 'one', id: 2},
349 \ [[1, 1, 1, 3], [2, 5, 2, 7], [3, 6, 4, 6]])
350 call assert_equal([#{id: 2, col: 1, type_bufnr: 0, end: 1, type: 'one',
351 \ length: 2, start: 1}], prop_list(1))
352 call assert_equal([#{id: 2, col: 5, type_bufnr: 0, end: 1, type: 'one',
353 \ length: 2, start: 1}], prop_list(2))
354 call assert_equal([#{id: 2, col: 6, type_bufnr: 0, end: 0, type: 'one',
355 \ length: 7, start: 1}], prop_list(3))
356 call assert_equal([#{id: 2, col: 1, type_bufnr: 0, end: 1, type: 'one',
357 \ length: 5, start: 0}], prop_list(4))
358 call assert_fails('call prop_add_list([1, 2], [[1, 1, 3]])', 'E1206:')
359 call assert_fails('call prop_add_list({}, {})', 'E1211:')
360 call assert_fails('call prop_add_list({}, [[1, 1, 3]])', 'E965:')
361 call assert_fails('call prop_add_list(#{type: "abc"}, [[1, 1, 1, 3]])', 'E971:')
362 call assert_fails('call prop_add_list(#{type: "one"}, [[]])', 'E474:')
363 call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, 1, 1], {}])', 'E714:')
364 call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, "a"]])', 'E474:')
365 call assert_fails('call prop_add_list(#{type: "one"}, [[2, 2]])', 'E474:')
366 call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, 2], [2, 2]])', 'E474:')
367 call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, 1, 2], [4, 1, 5, 2]])', 'E966:')
368 call assert_fails('call prop_add_list(#{type: "one"}, [[3, 1, 1, 2]])', 'E966:')
369 call assert_fails('call prop_add_list(#{type: "one"}, [[2, 2, 2, 2], [3, 20, 3, 22]])', 'E964:')
370 call assert_fails('eval #{type: "one"}->prop_add_list([[2, 2, 2, 2], [3, 20, 3, 22]])', 'E964:')
371 call assert_fails('call prop_add_list(test_null_dict(), [[2, 2, 2]])', 'E965:')
372 call assert_fails('call prop_add_list(#{type: "one"}, test_null_list())', 'E714:')
373 call assert_fails('call prop_add_list(#{type: "one"}, [test_null_list()])', 'E714:')
374 call DeletePropTypes()
375 bw!
376endfunc
377
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100378func Test_prop_remove()
379 new
380 call AddPropTypes()
381 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100382 let props = Get_expected_props()
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100383 call assert_equal(props, prop_list(1))
384
385 " remove by id
Bram Moolenaara5a78822019-09-04 21:57:18 +0200386 call assert_equal(1, {'id': 12}->prop_remove(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100387 unlet props[2]
388 call assert_equal(props, prop_list(1))
389
390 " remove by type
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200391 call assert_equal(1, prop_remove({'type': 'one'}, 1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100392 unlet props[1]
393 call assert_equal(props, prop_list(1))
394
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200395 " remove from unknown buffer
396 call assert_fails("call prop_remove({'type': 'one', 'bufnr': 123456}, 1)", 'E158:')
397
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100398 call DeletePropTypes()
399 bwipe!
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100400
401 new
402 call AddPropTypes()
403 call SetupPropsInFirstLine()
404 call prop_add(1, 6, {'length': 2, 'id': 11, 'type': 'three'})
405 let props = Get_expected_props()
Martin Tournoije2390c72021-07-28 13:30:16 +0200406 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 +0100407 call assert_equal(props, prop_list(1))
408 call assert_equal(1, prop_remove({'type': 'three', 'id': 11, 'both': 1, 'all': 1}, 1))
409 unlet props[3]
410 call assert_equal(props, prop_list(1))
411
Bram Moolenaare2e40752020-09-04 21:18:46 +0200412 call assert_fails("call prop_remove({'id': 11, 'both': 1})", 'E860:')
413 call assert_fails("call prop_remove({'type': 'three', 'both': 1})", 'E860:')
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100414
415 call DeletePropTypes()
416 bwipe!
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100417endfunc
418
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200419def Test_prop_add_vim9()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100420 prop_type_add('comment', {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200421 highlight: 'Directory',
422 priority: 123,
423 start_incl: true,
424 end_incl: true,
425 combine: false,
426 })
427 prop_type_delete('comment')
428enddef
429
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200430def Test_prop_remove_vim9()
431 new
Bram Moolenaar62aec932022-01-29 21:45:34 +0000432 g:AddPropTypes()
433 g:SetupPropsInFirstLine()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100434 assert_equal(1, prop_remove({type: 'three', id: 13, both: true, all: true}))
Bram Moolenaar62aec932022-01-29 21:45:34 +0000435 g:DeletePropTypes()
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200436 bwipe!
437enddef
438
Bram Moolenaar196d1572019-01-02 23:47:18 +0100439func SetupOneLine()
440 call setline(1, 'xonex xtwoxx')
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200441 normal gg0
Bram Moolenaar196d1572019-01-02 23:47:18 +0100442 call AddPropTypes()
443 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
444 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
445 let expected = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200446 \ #{type_bufnr: 0, col: 2, length: 3, id: 11, type: 'one', start: 1, end: 1},
447 \ #{type_bufnr: 0, col: 8, length: 3, id: 12, type: 'two', start: 1, end: 1},
Bram Moolenaar196d1572019-01-02 23:47:18 +0100448 \]
449 call assert_equal(expected, prop_list(1))
450 return expected
451endfunc
452
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100453func Test_prop_add_remove_buf()
454 new
455 let bufnr = bufnr('')
456 call AddPropTypes()
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100457 for lnum in range(1, 4)
458 call setline(lnum, 'one two three')
459 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100460 wincmd w
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100461 for lnum in range(1, 4)
462 call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
463 call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
464 call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
465 endfor
466
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100467 let props = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200468 \ #{type_bufnr: 0, col: 1, length: 3, id: 11, type: 'one', start: 1, end: 1},
469 \ #{type_bufnr: 0, col: 5, length: 3, id: 12, type: 'two', start: 1, end: 1},
470 \ #{type_bufnr: 0, col: 11, length: 3, id: 13, type: 'three', start: 1, end: 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100471 \]
472 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100473
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100474 " remove by id
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100475 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100476 unlet props[1]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100477
478 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100479 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100480 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
481 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
482 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
483
484 call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4)
485 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
486 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
487 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
488 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
489
490 call prop_remove({'id': 12, 'bufnr': bufnr})
491 for lnum in range(1, 4)
492 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
493 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100494
495 " remove by type
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100496 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100497 unlet props[0]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100498
499 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100500 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100501 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
502 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
503 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
504
505 call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4)
506 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
507 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
508 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
509 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
510
511 call prop_remove({'type': 'one', 'bufnr': bufnr})
512 for lnum in range(1, 4)
513 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
514 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100515
516 call DeletePropTypes()
517 wincmd w
518 bwipe!
519endfunc
520
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100521func Test_prop_backspace()
522 new
523 set bs=2
Bram Moolenaar196d1572019-01-02 23:47:18 +0100524 let expected = SetupOneLine() " 'xonex xtwoxx'
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100525
526 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
527 call assert_equal('one xtwoxx', getline(1))
528 let expected[0].col = 1
529 let expected[1].col = 6
530 call assert_equal(expected, prop_list(1))
531
532 call DeletePropTypes()
533 bwipe!
534 set bs&
535endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100536
LemonBoyd0b1a092022-05-12 18:45:18 +0100537func Test_prop_change()
538 new
539 let expected = SetupOneLine() " 'xonex xtwoxx'
540
541 " Characterwise.
542 exe "normal 7|c$\<Esc>"
543 call assert_equal('xonex ', getline(1))
544 call assert_equal(expected[:0], prop_list(1))
545 " Linewise.
546 exe "normal cc\<Esc>"
547 call assert_equal('', getline(1))
548 call assert_equal([], prop_list(1))
549
550 call DeletePropTypes()
551 bwipe!
552 set bs&
553endfunc
554
Bram Moolenaar196d1572019-01-02 23:47:18 +0100555func Test_prop_replace()
556 new
557 set bs=2
558 let expected = SetupOneLine() " 'xonex xtwoxx'
559
560 exe "normal 0Ryyy\<Esc>"
561 call assert_equal('yyyex xtwoxx', getline(1))
562 call assert_equal(expected, prop_list(1))
563
564 exe "normal ftRyy\<BS>"
565 call assert_equal('yyyex xywoxx', getline(1))
566 call assert_equal(expected, prop_list(1))
567
568 exe "normal 0fwRyy\<BS>"
569 call assert_equal('yyyex xyyoxx', getline(1))
570 call assert_equal(expected, prop_list(1))
571
572 exe "normal 0foRyy\<BS>\<BS>"
573 call assert_equal('yyyex xyyoxx', getline(1))
574 call assert_equal(expected, prop_list(1))
575
576 call DeletePropTypes()
577 bwipe!
578 set bs&
579endfunc
580
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200581func Test_prop_open_line()
582 new
583
584 " open new line, props stay in top line
585 let expected = SetupOneLine() " 'xonex xtwoxx'
586 exe "normal o\<Esc>"
587 call assert_equal('xonex xtwoxx', getline(1))
588 call assert_equal('', getline(2))
589 call assert_equal(expected, prop_list(1))
590 call DeletePropTypes()
591
592 " move all props to next line
593 let expected = SetupOneLine() " 'xonex xtwoxx'
594 exe "normal 0i\<CR>\<Esc>"
595 call assert_equal('', getline(1))
596 call assert_equal('xonex xtwoxx', getline(2))
597 call assert_equal(expected, prop_list(2))
598 call DeletePropTypes()
599
600 " split just before prop, move all props to next line
601 let expected = SetupOneLine() " 'xonex xtwoxx'
602 exe "normal 0li\<CR>\<Esc>"
603 call assert_equal('x', getline(1))
604 call assert_equal('onex xtwoxx', getline(2))
605 let expected[0].col -= 1
606 let expected[1].col -= 1
607 call assert_equal(expected, prop_list(2))
608 call DeletePropTypes()
609
610 " split inside prop, split first prop
611 let expected = SetupOneLine() " 'xonex xtwoxx'
612 exe "normal 0lli\<CR>\<Esc>"
613 call assert_equal('xo', getline(1))
614 call assert_equal('nex xtwoxx', getline(2))
615 let exp_first = [deepcopy(expected[0])]
616 let exp_first[0].length = 1
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200617 let exp_first[0].end = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200618 call assert_equal(exp_first, prop_list(1))
619 let expected[0].col = 1
620 let expected[0].length = 2
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200621 let expected[0].start = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200622 let expected[1].col -= 2
623 call assert_equal(expected, prop_list(2))
624 call DeletePropTypes()
625
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200626 " split just after first prop, second prop move to next line
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200627 let expected = SetupOneLine() " 'xonex xtwoxx'
628 exe "normal 0fea\<CR>\<Esc>"
629 call assert_equal('xone', getline(1))
630 call assert_equal('x xtwoxx', getline(2))
631 let exp_first = expected[0:0]
632 call assert_equal(exp_first, prop_list(1))
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200633 let expected = expected[1:1]
634 let expected[0].col -= 4
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200635 call assert_equal(expected, prop_list(2))
636 call DeletePropTypes()
637
638 bwipe!
639 set bs&
640endfunc
641
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100642func Test_prop_clear()
643 new
644 call AddPropTypes()
645 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100646 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100647
Bram Moolenaara5a78822019-09-04 21:57:18 +0200648 eval 1->prop_clear()
649 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100650
651 call DeletePropTypes()
652 bwipe!
653endfunc
654
655func Test_prop_clear_buf()
656 new
657 call AddPropTypes()
658 call SetupPropsInFirstLine()
659 let bufnr = bufnr('')
660 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100661 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100662
663 call prop_clear(1, 1, {'bufnr': bufnr})
664 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
665
666 wincmd w
667 call DeletePropTypes()
668 bwipe!
669endfunc
670
Bram Moolenaar21b50382019-01-04 18:07:24 +0100671func Test_prop_setline()
672 new
673 call AddPropTypes()
674 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100675 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100676
677 call setline(1, 'foobar')
678 call assert_equal([], prop_list(1))
679
680 call DeletePropTypes()
681 bwipe!
682endfunc
683
684func Test_prop_setbufline()
685 new
686 call AddPropTypes()
687 call SetupPropsInFirstLine()
688 let bufnr = bufnr('')
689 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100690 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100691
692 call setbufline(bufnr, 1, 'foobar')
693 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
694
695 wincmd w
696 call DeletePropTypes()
697 bwipe!
698endfunc
699
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100700func Test_prop_substitute()
701 new
702 " Set first line to 'one two three'
703 call AddPropTypes()
704 call SetupPropsInFirstLine()
705 let expected_props = Get_expected_props()
706 call assert_equal(expected_props, prop_list(1))
707
708 " Change "n" in "one" to XX: 'oXXe two three'
709 s/n/XX/
710 let expected_props[0].length += 1
711 let expected_props[1].length += 1
712 let expected_props[2].col += 1
713 let expected_props[3].col += 1
714 call assert_equal(expected_props, prop_list(1))
715
716 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
717 s/t//g
718 let expected_props[0].length -= 2
719 let expected_props[2].length -= 1
720 let expected_props[3].length -= 1
721 let expected_props[3].col -= 1
722 call assert_equal(expected_props, prop_list(1))
723
724 " Split the line by changing w to line break: 'oXXe ', 'o hree'
725 " The long prop is split and spans both lines.
726 " The props on "two" and "three" move to the next line.
727 s/w/\r/
728 let new_props = [
729 \ copy(expected_props[0]),
730 \ copy(expected_props[2]),
731 \ copy(expected_props[3]),
732 \ ]
733 let expected_props[0].length = 5
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200734 let expected_props[0].end = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100735 unlet expected_props[3]
736 unlet expected_props[2]
737 call assert_equal(expected_props, prop_list(1))
738
739 let new_props[0].length = 6
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200740 let new_props[0].start = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100741 let new_props[1].col = 1
742 let new_props[1].length = 1
743 let new_props[2].col = 3
744 call assert_equal(new_props, prop_list(2))
745
746 call DeletePropTypes()
747 bwipe!
748endfunc
749
Bram Moolenaar663bc892019-01-08 23:07:24 +0100750func Test_prop_change_indent()
751 call prop_type_add('comment', {'highlight': 'Directory'})
752 new
753 call setline(1, [' xxx', 'yyyyy'])
754 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200755 let expect = #{type_bufnr: 0, col: 2, length: 2, type: 'comment', start: 1, end: 1, id: 0}
Bram Moolenaar663bc892019-01-08 23:07:24 +0100756 call assert_equal([expect], prop_list(2))
757
758 set shiftwidth=3
759 normal 2G>>
760 call assert_equal(' yyyyy', getline(2))
761 let expect.col += 3
762 call assert_equal([expect], prop_list(2))
763
764 normal 2G==
765 call assert_equal(' yyyyy', getline(2))
766 let expect.col = 6
767 call assert_equal([expect], prop_list(2))
768
769 call prop_clear(2)
770 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
771 let expect.col = 2
772 let expect.length = 5
773 call assert_equal([expect], prop_list(2))
774
775 normal 2G<<
776 call assert_equal(' yyyyy', getline(2))
777 let expect.length = 2
778 call assert_equal([expect], prop_list(2))
779
780 set shiftwidth&
781 call prop_type_delete('comment')
782endfunc
783
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100784" Setup a three line prop in lines 2 - 4.
785" Add short props in line 1 and 5.
786func Setup_three_line_prop()
787 new
788 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
789 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
790 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
791 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
792endfunc
793
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100794func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200795 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100796 new
797 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
798
799 " start halfway line 1, end halfway line 3
800 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200801 let expect1 = #{type_bufnr: 0, col: 3, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100802 call assert_equal([expect1], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200803 let expect2 = #{type_bufnr: 0, col: 1, length: 10, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100804 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200805 let expect3 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100806 call assert_equal([expect3], prop_list(3))
807 call prop_clear(1, 3)
808
809 " include all three lines
810 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
811 let expect1.col = 1
812 let expect1.length = 8
813 call assert_equal([expect1], prop_list(1))
814 call assert_equal([expect2], prop_list(2))
815 let expect3.length = 9
816 call assert_equal([expect3], prop_list(3))
817 call prop_clear(1, 3)
818
819 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100820
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100821 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100822 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200823 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 +0100824 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200825 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100826 call assert_equal([expect2], prop_list(2))
827 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100828 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200829 let expect2 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100830 call assert_equal([expect2], prop_list(2))
831 bwipe!
832
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100833 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100834 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200835 let expect3 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100836 call assert_equal([expect3], prop_list(3))
Martin Tournoije2390c72021-07-28 13:30:16 +0200837 let expect4 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100838 call assert_equal([expect4], prop_list(4))
839 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100840 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100841 call assert_equal([expect3], prop_list(3))
842 call assert_equal([expect_short], prop_list(4))
843 bwipe!
844
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100845 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100846 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200847 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100848 call assert_equal([expect2], prop_list(2))
849 call append(2, "new line")
850 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200851 let expect3 = #{type_bufnr: 0, col: 1, length: 9, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100852 call assert_equal([expect3], prop_list(3))
853 bwipe!
854
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100855 call prop_type_delete('comment')
856endfunc
857
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100858func Test_prop_line2byte()
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100859 call prop_type_add('comment', {'highlight': 'Directory'})
860 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100861 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100862 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100863 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100864 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100865 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100866 bwipe!
Bram Moolenaar14c75302021-08-15 14:28:40 +0200867
868 new
Bram Moolenaara401bba2021-08-15 15:04:41 +0200869 setlocal ff=unix
Bram Moolenaar14c75302021-08-15 14:28:40 +0200870 call setline(1, range(500))
871 call assert_equal(1491, line2byte(401))
872 call prop_add(2, 1, {'type': 'comment'})
873 call prop_add(222, 1, {'type': 'comment'})
874 call assert_equal(1491, line2byte(401))
875 call prop_remove({'type': 'comment'})
876 call assert_equal(1491, line2byte(401))
877 bwipe!
878
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +0200879 new
Bram Moolenaar49b93042021-08-25 17:02:00 +0200880 setlocal ff=unix
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +0200881 call setline(1, range(520))
882 call assert_equal(1491, line2byte(401))
883 call prop_add(2, 1, {'type': 'comment'})
884 call assert_equal(1491, line2byte(401))
885 2delete
886 call assert_equal(1489, line2byte(400))
887 bwipe!
888
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100889 call prop_type_delete('comment')
890endfunc
891
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100892func Test_prop_byte2line()
893 new
894 set ff=unix
895 call setline(1, ['one one', 'two two', 'three three', 'four four', 'five'])
896 call assert_equal(4, byte2line(line2byte(4)))
897 call assert_equal(5, byte2line(line2byte(5)))
898
899 call prop_type_add('prop', {'highlight': 'Directory'})
900 call prop_add(3, 1, {'length': 5, 'type': 'prop'})
901 call assert_equal(4, byte2line(line2byte(4)))
902 call assert_equal(5, byte2line(line2byte(5)))
903
904 bwipe!
905 call prop_type_delete('prop')
906endfunc
907
Bram Moolenaar59ff6402021-01-30 17:16:28 +0100908func Test_prop_goto_byte()
909 new
910 call setline(1, '')
911 call setline(2, 'two three')
912 call setline(3, '')
913 call setline(4, 'four five')
914
915 call prop_type_add('testprop', {'highlight': 'Directory'})
916 call search('^two')
917 call prop_add(line('.'), col('.'), {
918 \ 'length': len('two'),
919 \ 'type': 'testprop'
920 \ })
921
922 call search('two \zsthree')
923 let expected_pos = line2byte(line('.')) + col('.') - 1
924 exe expected_pos .. 'goto'
925 let actual_pos = line2byte(line('.')) + col('.') - 1
926 eval actual_pos->assert_equal(expected_pos)
927
928 call search('four \zsfive')
929 let expected_pos = line2byte(line('.')) + col('.') - 1
930 exe expected_pos .. 'goto'
931 let actual_pos = line2byte(line('.')) + col('.') - 1
932 eval actual_pos->assert_equal(expected_pos)
933
934 call prop_type_delete('testprop')
935 bwipe!
936endfunc
937
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100938func Test_prop_undo()
939 new
940 call prop_type_add('comment', {'highlight': 'Directory'})
941 call setline(1, ['oneone', 'twotwo', 'three'])
942 " Set 'undolevels' to break changes into undo-able pieces.
943 set ul&
944
945 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200946 let expected = [#{type_bufnr: 0, col: 3, length: 2, id: 0, type: 'comment', start: 1, end: 1}]
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100947 call assert_equal(expected, prop_list(1))
948
949 " Insert a character, then undo.
950 exe "normal 0lllix\<Esc>"
951 set ul&
952 let expected[0].length = 3
953 call assert_equal(expected, prop_list(1))
954 undo
955 let expected[0].length = 2
956 call assert_equal(expected, prop_list(1))
957
958 " Delete a character, then undo
959 exe "normal 0lllx"
960 set ul&
961 let expected[0].length = 1
962 call assert_equal(expected, prop_list(1))
963 undo
964 let expected[0].length = 2
965 call assert_equal(expected, prop_list(1))
966
967 " Delete the line, then undo
968 1d
969 set ul&
970 call assert_equal([], prop_list(1))
971 undo
972 call assert_equal(expected, prop_list(1))
973
974 " Insert a character, delete two characters, then undo with "U"
975 exe "normal 0lllix\<Esc>"
976 set ul&
977 let expected[0].length = 3
978 call assert_equal(expected, prop_list(1))
979 exe "normal 0lllxx"
980 set ul&
981 let expected[0].length = 1
982 call assert_equal(expected, prop_list(1))
983 normal U
984 let expected[0].length = 2
985 call assert_equal(expected, prop_list(1))
986
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200987 " substitute a word, then undo
988 call setline(1, 'the number 123 is highlighted.')
989 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200990 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200991 call assert_equal(expected, prop_list(1))
992 set ul&
993 1s/number/foo
994 let expected[0].col = 9
995 call assert_equal(expected, prop_list(1))
996 undo
997 let expected[0].col = 12
998 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200999 call prop_clear(1)
1000
1001 " substitute with backslash
1002 call setline(1, 'the number 123 is highlighted.')
1003 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001004 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001005 call assert_equal(expected, prop_list(1))
1006 1s/the/\The
1007 call assert_equal(expected, prop_list(1))
1008 1s/^/\\
1009 let expected[0].col += 1
1010 call assert_equal(expected, prop_list(1))
1011 1s/^/\~
1012 let expected[0].col += 1
1013 call assert_equal(expected, prop_list(1))
1014 1s/123/12\\3
1015 let expected[0].length += 1
1016 call assert_equal(expected, prop_list(1))
1017 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001018
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001019 bwipe!
1020 call prop_type_delete('comment')
1021endfunc
1022
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001023func Test_prop_delete_text()
1024 new
1025 call prop_type_add('comment', {'highlight': 'Directory'})
1026 call setline(1, ['oneone', 'twotwo', 'three'])
1027
1028 " zero length property
1029 call prop_add(1, 3, {'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001030 let expected = [#{type_bufnr: 0, col: 3, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001031 call assert_equal(expected, prop_list(1))
1032
1033 " delete one char moves the property
1034 normal! x
Martin Tournoije2390c72021-07-28 13:30:16 +02001035 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001036 call assert_equal(expected, prop_list(1))
1037
1038 " delete char of the property has no effect
1039 normal! lx
Martin Tournoije2390c72021-07-28 13:30:16 +02001040 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001041 call assert_equal(expected, prop_list(1))
1042
1043 " delete more chars moves property to first column, is not deleted
1044 normal! 0xxxx
Martin Tournoije2390c72021-07-28 13:30:16 +02001045 let expected = [#{type_bufnr: 0, col: 1, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001046 call assert_equal(expected, prop_list(1))
1047
1048 bwipe!
1049 call prop_type_delete('comment')
1050endfunc
1051
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001052" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +02001053func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +02001054 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +01001055 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +02001056 if g:orig_encoding != 'utf-8'
1057 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001058 endif
1059 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +02001060 \ "call setline(1, ["
1061 \ .. "'One two',"
1062 \ .. "'Numbér 123 änd thœn 4¾7.',"
1063 \ .. "'--aa--bb--cc--dd--',"
1064 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001065 \ .. "'first line',"
1066 \ .. "' second line ',"
1067 \ .. "'third line',"
1068 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +02001069 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001070 \ "hi NumberProp ctermfg=blue",
1071 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001072 \ "hi BackgroundProp ctermbg=lightgrey",
1073 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001074 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +02001075 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
1076 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001077 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
1078 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
1079 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001080 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
1081 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
1082 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +01001083 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001084 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001085 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
1086 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001087 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
1088 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
1089 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
1090 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001091 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
1092 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001093 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001094 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
1095 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
1096 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
1097 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +02001098 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001099 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001100 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001101 \ "syn match Comment '//.*'",
1102 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001103 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01001104 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001105 \ "normal 6G0i\<BS>\<Esc>",
1106 \ "normal 3J",
1107 \ "normal 3G",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001108 \], 'XtestProp')
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001109 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001110 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +01001111
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001112 " clean up
1113 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +01001114 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001115endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +02001116
1117func RunTestVisualBlock(width, dump)
1118 call writefile([
1119 \ "call setline(1, ["
1120 \ .. "'xxxxxxxxx 123 x',"
1121 \ .. "'xxxxxxxx 123 x',"
1122 \ .. "'xxxxxxx 123 x',"
1123 \ .. "'xxxxxx 123 x',"
1124 \ .. "'xxxxx 123 x',"
1125 \ .. "'xxxx 123 xx',"
1126 \ .. "'xxx 123 xxx',"
1127 \ .. "'xx 123 xxxx',"
1128 \ .. "'x 123 xxxxx',"
1129 \ .. "' 123 xxxxxx',"
1130 \ .. "])",
1131 \ "hi SearchProp ctermbg=yellow",
1132 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
1133 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
1134 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
1135 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
1136 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
1137 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
1138 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
1139 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
1140 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
1141 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
1142 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
1143 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
1144 \], 'XtestPropVis')
1145 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
1146 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
1147
1148 " clean up
1149 call StopVimInTerminal(buf)
1150 call delete('XtestPropVis')
1151endfunc
1152
1153" screenshot test with Visual block mode operations
1154func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +02001155 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +02001156
1157 " Delete two columns while text props are three chars wide.
1158 call RunTestVisualBlock(2, '01')
1159
1160 " Same, but delete four columns
1161 call RunTestVisualBlock(4, '02')
1162endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +02001163
Bram Moolenaara956bf62019-06-19 17:34:24 +02001164func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +02001165 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +02001166
Bram Moolenaara956bf62019-06-19 17:34:24 +02001167 let lines =<< trim END
1168 call setline(1, [
1169 \ "\txxx",
1170 \ "x\txxx",
1171 \ ])
1172 hi SearchProp ctermbg=yellow
1173 call prop_type_add('search', {'highlight': 'SearchProp'})
1174 call prop_add(1, 2, {'length': 3, 'type': 'search'})
1175 call prop_add(2, 3, {'length': 3, 'type': 'search'})
1176 END
1177 call writefile(lines, 'XtestPropTab')
1178 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
1179 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
1180
1181 " clean up
1182 call StopVimInTerminal(buf)
1183 call delete('XtestPropTab')
1184endfunc
1185
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001186func Test_textprop_nowrap_scrolled()
1187 CheckScreendump
1188
1189 let lines =<< trim END
1190 vim9script
1191 set nowrap
1192 setline(1, 'The number 123 is smaller than 4567.' .. repeat('X', &columns))
1193 prop_type_add('number', {'highlight': 'ErrorMsg'})
1194 prop_add(1, 12, {'length': 3, 'type': 'number'})
1195 prop_add(1, 32, {'length': 4, 'type': 'number'})
1196 feedkeys('gg20zl', 'nxt')
1197 END
1198 call writefile(lines, 'XtestNowrap')
1199 let buf = RunVimInTerminal('-S XtestNowrap', {'rows': 6})
1200 call VerifyScreenDump(buf, 'Test_textprop_nowrap_01', {})
1201
1202 call term_sendkeys(buf, "$")
1203 call VerifyScreenDump(buf, 'Test_textprop_nowrap_02', {})
1204
1205 " clean up
1206 call StopVimInTerminal(buf)
1207 call delete('XtestNowrap')
1208endfunc
1209
Bram Moolenaar34390282019-10-16 14:38:26 +02001210func Test_textprop_with_syntax()
1211 CheckScreendump
1212
1213 let lines =<< trim END
1214 call setline(1, [
1215 \ "(abc)",
1216 \ ])
1217 syn match csParens "[()]" display
1218 hi! link csParens MatchParen
1219
1220 call prop_type_add('TPTitle', #{ highlight: 'Title' })
1221 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
1222 END
1223 call writefile(lines, 'XtestPropSyn')
1224 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
1225 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
1226
1227 " clean up
1228 call StopVimInTerminal(buf)
1229 call delete('XtestPropSyn')
1230endfunc
1231
Bram Moolenaard79eef22019-05-24 20:41:55 +02001232" Adding a text property to a new buffer should not fail
1233func Test_textprop_empty_buffer()
1234 call prop_type_add('comment', {'highlight': 'Search'})
1235 new
1236 call prop_add(1, 1, {'type': 'comment'})
1237 close
Bram Moolenaaradfde112019-05-25 22:11:45 +02001238 call prop_type_delete('comment')
1239endfunc
1240
Bram Moolenaard74af422019-06-28 21:38:00 +02001241" Adding a text property with invalid highlight should be ignored.
1242func Test_textprop_invalid_highlight()
1243 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
1244 new
1245 call setline(1, ['asdf','asdf'])
1246 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
1247 redraw
1248 bwipe!
1249 call prop_type_delete('dni')
1250endfunc
1251
Bram Moolenaaradfde112019-05-25 22:11:45 +02001252" Adding a text property to an empty buffer and then editing another
1253func Test_textprop_empty_buffer_next()
1254 call prop_type_add("xxx", {})
1255 call prop_add(1, 1, {"type": "xxx"})
1256 next X
1257 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +02001258endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001259
1260func Test_textprop_remove_from_buf()
1261 new
1262 let buf = bufnr('')
1263 call prop_type_add('one', {'bufnr': buf})
1264 call prop_add(1, 1, {'type': 'one', 'id': 234})
1265 file x
1266 edit y
1267 call prop_remove({'id': 234, 'bufnr': buf}, 1)
1268 call prop_type_delete('one', {'bufnr': buf})
1269 bwipe! x
1270 close
1271endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +02001272
1273func Test_textprop_in_unloaded_buf()
1274 edit Xaaa
1275 call setline(1, 'aaa')
1276 write
1277 edit Xbbb
1278 call setline(1, 'bbb')
1279 write
1280 let bnr = bufnr('')
1281 edit Xaaa
1282
1283 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
1284 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
1285 exe 'buf ' .. bnr
1286 call assert_equal('bbb', getline(1))
1287 call assert_equal(0, prop_list(1)->len())
1288
1289 bwipe! Xaaa
1290 bwipe! Xbbb
1291 cal delete('Xaaa')
1292 cal delete('Xbbb')
1293endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001294
1295func Test_proptype_substitute2()
1296 new
1297 " text_prop.vim
1298 call setline(1, [
1299 \ 'The num 123 is smaller than 4567.',
1300 \ '123 The number 123 is smaller than 4567.',
1301 \ '123 The number 123 is smaller than 4567.'])
1302
1303 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1304
1305 call prop_add(1, 12, {'length': 3, 'type': 'number'})
1306 call prop_add(2, 1, {'length': 3, 'type': 'number'})
1307 call prop_add(3, 36, {'length': 4, 'type': 'number'})
1308 set ul&
Martin Tournoije2390c72021-07-28 13:30:16 +02001309 let expected = [
1310 \ #{type_bufnr: 0, id: 0, col: 13, end: 1, type: 'number', length: 3, start: 1},
1311 \ #{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'number', length: 3, start: 1},
1312 \ #{type_bufnr: 0, id: 0, col: 50, end: 1, type: 'number', length: 4, start: 1}]
1313
1314 " TODO
1315 return
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001316 " Add some text in between
1317 %s/\s\+/ /g
Martin Tournoije2390c72021-07-28 13:30:16 +02001318 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001319
1320 " remove some text
1321 :1s/[a-z]\{3\}//g
1322 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
1323 call assert_equal(expected, prop_list(1))
1324 bwipe!
1325endfunc
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001326
Bram Moolenaar8902b312020-09-20 21:04:35 +02001327" This was causing property corruption.
1328func Test_proptype_substitute3()
1329 new
1330 call setline(1, ['abcxxx', 'def'])
1331 call prop_type_add("test", {"highlight": "Search"})
1332 call prop_add(1, 2, {"end_lnum": 2, "end_col": 2, "type": "test"})
1333 %s/x\+$//
1334 redraw
1335
1336 call prop_type_delete('test')
1337 bwipe!
1338endfunc
1339
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001340func SaveOptions()
1341 let d = #{tabstop: &tabstop,
1342 \ softtabstop: &softtabstop,
1343 \ shiftwidth: &shiftwidth,
1344 \ expandtab: &expandtab,
1345 \ foldmethod: '"' .. &foldmethod .. '"',
1346 \ }
1347 return d
1348endfunc
1349
1350func RestoreOptions(dict)
1351 for name in keys(a:dict)
1352 exe 'let &' .. name .. ' = ' .. a:dict[name]
1353 endfor
1354endfunc
1355
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001356func Test_textprop_noexpandtab()
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001357 new
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001358 let save_dict = SaveOptions()
1359
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001360 set tabstop=8
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001361 set softtabstop=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001362 set shiftwidth=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001363 set noexpandtab
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001364 set foldmethod=marker
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001365
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001366 call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx")
1367 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1368 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1369 call feedkeys("0i\<tab>", "tx")
1370 call prop_remove({'type': 'test'})
1371 call prop_add(1, 2, {'end_col': 3, 'type': 'test'})
1372 call feedkeys("A\<left>\<tab>", "tx")
1373 call prop_remove({'type': 'test'})
1374 try
1375 " It is correct that this does not pass
1376 call prop_add(1, 6, {'end_col': 7, 'type': 'test'})
1377 " Has already collapsed here, start_col:6 does not result in an error
1378 call feedkeys("A\<left>\<tab>", "tx")
1379 catch /^Vim\%((\a\+)\)\=:E964/
1380 endtry
1381 call prop_remove({'type': 'test'})
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001382 call prop_type_delete('test')
1383
1384 call RestoreOptions(save_dict)
1385 bwipe!
1386endfunc
1387
1388func Test_textprop_noexpandtab_redraw()
1389 new
1390 let save_dict = SaveOptions()
1391
1392 set tabstop=8
1393 set softtabstop=4
1394 set shiftwidth=4
1395 set noexpandtab
1396 set foldmethod=marker
1397
1398 call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx")
1399 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1400 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1401 call feedkeys("0i\<tab>", "tx")
1402 " Internally broken at the next line
1403 call feedkeys("A\<left>\<tab>", "tx")
1404 redraw
1405 " Index calculation failed internally on next line
1406 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1407 call prop_remove({'type': 'test', 'all': v:true})
1408 call prop_type_delete('test')
1409 call prop_type_delete('test')
1410
1411 call RestoreOptions(save_dict)
1412 bwipe!
1413endfunc
1414
1415func Test_textprop_ins_str()
1416 new
1417 call setline(1, 'just some text')
1418 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1419 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001420 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 +01001421
1422 call feedkeys("foi\<F8>\<Esc>", "tx")
1423 call assert_equal('just s<F8>ome text', getline(1))
Martin Tournoije2390c72021-07-28 13:30:16 +02001424 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 +01001425
1426 bwipe!
1427 call prop_remove({'type': 'test'})
1428 call prop_type_delete('test')
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001429endfunc
Bram Moolenaar66b98852020-03-11 19:15:52 +01001430
1431func Test_find_prop_later_in_line()
1432 new
1433 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1434 call setline(1, 'just some text')
1435 call prop_add(1, 1, {'length': 4, 'type': 'test'})
1436 call prop_add(1, 10, {'length': 3, 'type': 'test'})
1437
Martin Tournoije2390c72021-07-28 13:30:16 +02001438 call assert_equal(
1439 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 10, end: 1, type: 'test', length: 3, start: 1},
1440 \ prop_find(#{type: 'test', lnum: 1, col: 6}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001441
1442 bwipe!
1443 call prop_type_delete('test')
1444endfunc
1445
1446func Test_find_zerowidth_prop_sol()
1447 new
1448 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1449 call setline(1, 'just some text')
1450 call prop_add(1, 1, {'length': 0, 'type': 'test'})
1451
Martin Tournoije2390c72021-07-28 13:30:16 +02001452 call assert_equal(
1453 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 1, end: 1, type: 'test', length: 0, start: 1},
1454 \ prop_find(#{type: 'test', lnum: 1}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001455
1456 bwipe!
1457 call prop_type_delete('test')
1458endfunc
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001459
1460" Test for passing invalid arguments to prop_xxx() functions
1461func Test_prop_func_invalid_args()
1462 call assert_fails('call prop_clear(1, 2, [])', 'E715:')
1463 call assert_fails('call prop_clear(-1, 2)', 'E16:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001464 call assert_fails('call prop_find(test_null_dict())', 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001465 call assert_fails('call prop_find({"bufnr" : []})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001466 call assert_fails('call prop_find({})', 'E968:')
1467 call assert_fails('call prop_find({}, "x")', 'E474:')
1468 call assert_fails('call prop_find({"lnum" : -2})', 'E16:')
1469 call assert_fails('call prop_list(1, [])', 'E715:')
Bram Moolenaar9d489562020-07-30 20:08:50 +02001470 call assert_fails('call prop_list(-1, {})', 'E16:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001471 call assert_fails('call prop_remove([])', 'E474:')
1472 call assert_fails('call prop_remove({}, -2)', 'E16:')
1473 call assert_fails('call prop_remove({})', 'E968:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001474 call assert_fails('call prop_type_add([], {})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001475 call assert_fails("call prop_type_change('long', {'xyz' : 10})", 'E971:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001476 call assert_fails("call prop_type_delete([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001477 call assert_fails("call prop_type_delete('xyz', [])", 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001478 call assert_fails("call prop_type_get([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001479 call assert_fails("call prop_type_get('', [])", 'E474:')
1480 call assert_fails("call prop_type_list([])", 'E715:')
Bram Moolenaar3dc34742021-03-02 13:36:47 +01001481 call assert_fails("call prop_type_add('yyy', 'not_a_dict')", 'E715:')
1482 call assert_fails("call prop_add(1, 5, {'type':'missing_type', 'length':1})", 'E971:')
1483 call assert_fails("call prop_add(1, 5, {'type': ''})", 'E971:')
1484 call assert_fails('call prop_add(1, 1, 0)', 'E715:')
1485
1486 new
1487 call setline(1, ['first', 'second'])
1488 call prop_type_add('xxx', {})
1489
1490 call assert_fails("call prop_type_add('xxx', {})", 'E969:')
1491 call assert_fails("call prop_add(2, 0, {'type': 'xxx'})", 'E964:')
1492 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':1})", 'E475:')
1493 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':3})", 'E966:')
1494 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'length':-1})", 'E475:')
1495 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_col':0})", 'E475:')
1496 call assert_fails("call prop_add(2, 3, {'length':1})", 'E965:')
1497
1498 call prop_type_delete('xxx')
1499 bwipe!
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001500endfunc
1501
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001502func Test_prop_split_join()
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001503 new
1504 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1505 call setline(1, 'just some text')
1506 call prop_add(1, 6, {'length': 4, 'type': 'test'})
1507
1508 " Split in middle of "some"
1509 execute "normal! 8|i\<CR>"
Martin Tournoije2390c72021-07-28 13:30:16 +02001510 call assert_equal(
1511 \ [#{type_bufnr: 0, id: 0, col: 6, end: 0, type: 'test', length: 2, start: 1}],
1512 \ prop_list(1))
1513 call assert_equal(
1514 \ [#{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'test', length: 2, start: 0}],
1515 \ prop_list(2))
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001516
1517 " Join the two lines back together
1518 normal! 1GJ
Martin Tournoije2390c72021-07-28 13:30:16 +02001519 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 +02001520
1521 bwipe!
1522 call prop_type_delete('test')
1523endfunc
1524
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001525func Test_prop_increment_decrement()
1526 new
1527 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1528 call setline(1, 'its 998 times')
1529 call prop_add(1, 5, {'length': 3, 'type': 'test'})
1530
1531 exe "normal! 0f9\<C-A>"
1532 eval getline(1)->assert_equal('its 999 times')
1533 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001534 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 3, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001535
1536 exe "normal! 0f9\<C-A>"
1537 eval getline(1)->assert_equal('its 1000 times')
1538 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001539 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 4, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001540
1541 bwipe!
1542 call prop_type_delete('test')
1543endfunc
1544
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001545func Test_prop_block_insert()
1546 new
1547 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1548 call setline(1, ['one ', 'two '])
1549 call prop_add(1, 1, {'length': 3, 'type': 'test'})
1550 call prop_add(2, 1, {'length': 3, 'type': 'test'})
1551
1552 " insert "xx" in the first column of both lines
1553 exe "normal! gg0\<C-V>jIxx\<Esc>"
1554 eval getline(1, 2)->assert_equal(['xxone ', 'xxtwo '])
Martin Tournoije2390c72021-07-28 13:30:16 +02001555 let expected = [#{type_bufnr: 0, id: 0, col: 3, end: 1, type: 'test', length: 3, start: 1}]
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001556 eval prop_list(1)->assert_equal(expected)
1557 eval prop_list(2)->assert_equal(expected)
1558
1559 " insert "yy" inside the text props to make them longer
1560 exe "normal! gg03l\<C-V>jIyy\<Esc>"
1561 eval getline(1, 2)->assert_equal(['xxoyyne ', 'xxtyywo '])
1562 let expected[0].length = 5
1563 eval prop_list(1)->assert_equal(expected)
1564 eval prop_list(2)->assert_equal(expected)
1565
1566 " insert "zz" after the text props, text props don't change
1567 exe "normal! gg07l\<C-V>jIzz\<Esc>"
1568 eval getline(1, 2)->assert_equal(['xxoyynezz ', 'xxtyywozz '])
1569 eval prop_list(1)->assert_equal(expected)
1570 eval prop_list(2)->assert_equal(expected)
1571
1572 bwipe!
1573 call prop_type_delete('test')
1574endfunc
1575
Bram Moolenaar23999d72020-12-23 14:36:00 +01001576" this was causing an ml_get error because w_botline was wrong
1577func Test_prop_one_line_window()
1578 enew
1579 call range(2)->setline(1)
1580 call prop_type_add('testprop', {})
1581 call prop_add(1, 1, {'type': 'testprop'})
1582 call popup_create('popup', {'textprop': 'testprop'})
1583 $
1584 new
1585 wincmd _
1586 call feedkeys("\r", 'xt')
1587 redraw
1588
1589 call popup_clear()
1590 call prop_type_delete('testprop')
1591 close
1592 bwipe!
1593endfunc
1594
Bram Moolenaar840f91f2021-05-26 22:32:10 +02001595" This was calling ml_append_int() and copy a text property from a previous
1596" line at the wrong moment. Exact text length matters.
1597def Test_prop_splits_data_block()
1598 new
1599 var lines: list<string> = [repeat('x', 35)]->repeat(41)
1600 + [repeat('!', 35)]
1601 + [repeat('x', 35)]->repeat(56)
1602 lines->setline(1)
1603 prop_type_add('someprop', {highlight: 'ErrorMsg'})
1604 prop_add(1, 27, {end_lnum: 1, end_col: 70, type: 'someprop'})
1605 prop_remove({type: 'someprop'}, 1)
1606 prop_add(35, 22, {end_lnum: 43, end_col: 43, type: 'someprop'})
1607 prop_remove({type: 'someprop'}, 35, 43)
1608 assert_equal([], prop_list(42))
1609
1610 bwipe!
1611 prop_type_delete('someprop')
1612enddef
1613
Bram Moolenaar4cd5c522021-06-27 13:04:00 +02001614" This was calling ml_delete_int() and try to change text properties.
1615def Test_prop_add_delete_line()
1616 new
1617 var a = 10
1618 var b = 20
1619 repeat([''], a)->append('$')
1620 prop_type_add('Test', {highlight: 'ErrorMsg'})
1621 for lnum in range(1, a)
1622 for col in range(1, b)
1623 prop_add(1, 1, {end_lnum: lnum, end_col: col, type: 'Test'})
1624 endfor
1625 endfor
1626
1627 # check deleting lines is OK
1628 :5del
1629 :1del
1630 :$del
1631
1632 prop_type_delete('Test')
1633 bwipe!
1634enddef
1635
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001636func Test_prop_in_linebreak()
1637 CheckRunVimInTerminal
1638
1639 let lines =<< trim END
1640 set breakindent linebreak breakat+=]
1641 call printf('%s]%s', repeat('x', 50), repeat('x', 70))->setline(1)
1642 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1643 call prop_add(1, 51, #{length: 1, type: 'test'})
1644 END
1645 call writefile(lines, 'XscriptPropLinebreak')
1646 let buf = RunVimInTerminal('-S XscriptPropLinebreak', #{rows: 10})
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001647 call VerifyScreenDump(buf, 'Test_prop_linebreak', {})
1648
1649 call StopVimInTerminal(buf)
1650 call delete('XscriptPropLinebreak')
1651endfunc
1652
Bram Moolenaar42eba042021-11-30 20:22:49 +00001653func Test_prop_after_tab()
1654 CheckRunVimInTerminal
1655
1656 let lines =<< trim END
1657 set breakindent linebreak breakat+=]
1658 call setline(1, "\t[xxx]")
1659 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1660 call prop_add(1, 2, #{length: 1, type: 'test'})
1661 END
1662 call writefile(lines, 'XscriptPropAfterTab')
1663 let buf = RunVimInTerminal('-S XscriptPropAfterTab', #{rows: 10})
Bram Moolenaar42eba042021-11-30 20:22:49 +00001664 call VerifyScreenDump(buf, 'Test_prop_after_tab', {})
1665
1666 call StopVimInTerminal(buf)
1667 call delete('XscriptPropAfterTab')
1668endfunc
1669
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001670func Test_prop_after_linebreak()
1671 CheckRunVimInTerminal
1672
1673 let lines =<< trim END
1674 set linebreak wrap
1675 call printf('%s+(%s)', 'x'->repeat(&columns / 2), 'x'->repeat(&columns / 2))->setline(1)
1676 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1677 call prop_add(1, (&columns / 2) + 2, #{length: 1, type: 'test'})
1678 END
1679 call writefile(lines, 'XscriptPropAfterLinebreak')
1680 let buf = RunVimInTerminal('-S XscriptPropAfterLinebreak', #{rows: 10})
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001681 call VerifyScreenDump(buf, 'Test_prop_after_linebreak', {})
1682
1683 call StopVimInTerminal(buf)
1684 call delete('XscriptPropAfterLinebreak')
1685endfunc
1686
Martin Tournoije2390c72021-07-28 13:30:16 +02001687" Buffer number of 0 should be ignored, as if the parameter wasn't passed.
1688def Test_prop_bufnr_zero()
1689 new
1690 try
1691 var bufnr = bufnr('')
1692 setline(1, 'hello')
1693 prop_type_add('bufnr-global', {highlight: 'ErrorMsg'})
1694 prop_type_add('bufnr-buffer', {highlight: 'StatusLine', bufnr: bufnr})
1695
1696 prop_add(1, 1, {type: 'bufnr-global', length: 1})
1697 prop_add(1, 2, {type: 'bufnr-buffer', length: 1})
1698
1699 var list = prop_list(1)
1700 assert_equal([
1701 {id: 0, col: 1, type_bufnr: 0, end: 1, type: 'bufnr-global', length: 1, start: 1},
1702 {id: 0, col: 2, type_bufnr: bufnr, end: 1, type: 'bufnr-buffer', length: 1, start: 1},
1703 ], list)
1704
1705 assert_equal(
1706 {highlight: 'ErrorMsg', end_incl: 0, start_incl: 0, priority: 0, combine: 1},
1707 prop_type_get('bufnr-global', {bufnr: list[0].type_bufnr}))
1708
1709 assert_equal(
1710 {highlight: 'StatusLine', end_incl: 0, start_incl: 0, priority: 0, bufnr: bufnr, combine: 1},
1711 prop_type_get('bufnr-buffer', {bufnr: list[1].type_bufnr}))
1712 finally
1713 bwipe!
1714 prop_type_delete('bufnr-global')
1715 endtry
1716enddef
1717
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001718" Tests for the prop_list() function
1719func Test_prop_list()
1720 let lines =<< trim END
1721 new
Bram Moolenaar62aec932022-01-29 21:45:34 +00001722 call g:AddPropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001723 call setline(1, repeat([repeat('a', 60)], 10))
1724 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1725 call prop_add(1, 5, {'type': 'two', 'id': 10, 'end_col': 7})
1726 call prop_add(3, 12, {'type': 'one', 'id': 20, 'end_col': 14})
1727 call prop_add(3, 13, {'type': 'two', 'id': 10, 'end_col': 15})
1728 call prop_add(5, 20, {'type': 'one', 'id': 10, 'end_col': 22})
1729 call prop_add(5, 21, {'type': 'two', 'id': 20, 'end_col': 23})
1730 call assert_equal([
1731 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1732 \ 'type': 'one', 'length': 2, 'start': 1},
1733 \ {'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
1734 \ 'type': 'two', 'length': 2, 'start': 1}], prop_list(1))
1735 #" text properties between a few lines
1736 call assert_equal([
1737 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1738 \ 'type': 'one', 'length': 2, 'start': 1},
1739 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
1740 \ 'type': 'two', 'length': 2, 'start': 1},
1741 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
1742 \ 'type': 'one', 'length': 2, 'start': 1},
1743 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
1744 \ 'type': 'two', 'length': 2, 'start': 1}],
1745 \ prop_list(2, {'end_lnum': 5}))
1746 #" text properties across all the lines
1747 call assert_equal([
1748 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1749 \ 'type': 'one', 'length': 2, 'start': 1},
1750 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1751 \ 'type': 'one', 'length': 2, 'start': 1},
1752 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
1753 \ 'type': 'one', 'length': 2, 'start': 1}],
1754 \ prop_list(1, {'types': ['one'], 'end_lnum': -1}))
1755 #" text properties with the specified identifier
1756 call assert_equal([
1757 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1758 \ 'type': 'one', 'length': 2, 'start': 1},
1759 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
1760 \ 'type': 'two', 'length': 2, 'start': 1}],
1761 \ prop_list(1, {'ids': [20], 'end_lnum': 10}))
1762 #" text properties of the specified type and id
1763 call assert_equal([
1764 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
1765 \ 'type': 'two', 'length': 2, 'start': 1},
1766 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
1767 \ 'type': 'two', 'length': 2, 'start': 1}],
1768 \ prop_list(1, {'types': ['two'], 'ids': [10], 'end_lnum': 20}))
1769 call assert_equal([], prop_list(1, {'ids': [40, 50], 'end_lnum': 10}))
1770 call assert_equal([], prop_list(6, {'end_lnum': 10}))
1771 call assert_equal([], prop_list(2, {'end_lnum': 2}))
1772 #" error cases
1773 call assert_fails("echo prop_list(1, {'end_lnum': -20})", 'E16:')
1774 call assert_fails("echo prop_list(4, {'end_lnum': 2})", 'E16:')
1775 call assert_fails("echo prop_list(1, {'end_lnum': '$'})", 'E889:')
1776 call assert_fails("echo prop_list(1, {'types': ['blue'], 'end_lnum': 10})",
1777 \ 'E971:')
1778 call assert_fails("echo prop_list(1, {'types': ['one', 'blue'],
1779 \ 'end_lnum': 10})", 'E971:')
1780 call assert_fails("echo prop_list(1, {'types': ['one', 10],
1781 \ 'end_lnum': 10})", 'E928:')
1782 call assert_fails("echo prop_list(1, {'types': ['']})", 'E971:')
1783 call assert_equal([], prop_list(2, {'types': []}))
1784 call assert_equal([], prop_list(2, {'types': test_null_list()}))
1785 call assert_fails("call prop_list(1, {'types': {}})", 'E714:')
1786 call assert_fails("call prop_list(1, {'types': 'one'})", 'E714:')
1787 call assert_equal([], prop_list(2, {'types': ['one'],
1788 \ 'ids': test_null_list()}))
1789 call assert_equal([], prop_list(2, {'types': ['one'], 'ids': []}))
1790 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': {}})",
1791 \ 'E714:')
1792 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': 10})",
1793 \ 'E714:')
1794 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [[]]})",
1795 \ 'E745:')
1796 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [10, []]})",
1797 \ 'E745:')
Martin Tournoije2390c72021-07-28 13:30:16 +02001798
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001799 #" get text properties from a non-current buffer
1800 wincmd w
1801 call assert_equal([
1802 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1803 \ 'type': 'one', 'length': 2, 'start': 1},
1804 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
1805 \ 'type': 'two', 'length': 2, 'start': 1},
1806 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1807 \ 'type': 'one', 'length': 2, 'start': 1},
1808 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
1809 \ 'type': 'two', 'length': 2, 'start': 1}],
1810 \ prop_list(1, {'bufnr': winbufnr(1), 'end_lnum': 4}))
1811 wincmd w
1812
1813 #" get text properties after clearing all the properties
1814 call prop_clear(1, line('$'))
1815 call assert_equal([], prop_list(1, {'end_lnum': 10}))
1816
1817 call prop_add(2, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1818 call prop_add(2, 4, {'type': 'two', 'id': 10, 'end_col': 6})
1819 call prop_add(2, 4, {'type': 'three', 'id': 15, 'end_col': 6})
1820 #" get text properties with a list of types
1821 call assert_equal([
1822 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
1823 \ 'type': 'two', 'length': 2, 'start': 1},
1824 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1825 \ 'type': 'one', 'length': 2, 'start': 1}],
1826 \ prop_list(2, {'types': ['one', 'two']}))
1827 call assert_equal([
1828 \ {'id': 15, 'col': 4, 'type_bufnr': 0, 'end': 1,
1829 \ 'type': 'three', 'length': 2, 'start': 1},
1830 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1831 \ 'type': 'one', 'length': 2, 'start': 1}],
1832 \ prop_list(2, {'types': ['one', 'three']}))
1833 #" get text properties with a list of identifiers
1834 call assert_equal([
1835 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
1836 \ 'type': 'two', 'length': 2, 'start': 1},
1837 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1838 \ 'type': 'one', 'length': 2, 'start': 1}],
1839 \ prop_list(2, {'ids': [5, 10, 20]}))
1840 call prop_clear(1, line('$'))
1841 call assert_equal([], prop_list(2, {'types': ['one', 'two']}))
1842 call assert_equal([], prop_list(2, {'ids': [5, 10, 20]}))
1843
1844 #" get text properties from a hidden buffer
1845 edit! Xaaa
1846 call setline(1, repeat([repeat('b', 60)], 10))
1847 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1848 call prop_add(4, 8, {'type': 'two', 'id': 10, 'end_col': 10})
1849 VAR bnr = bufnr()
1850 hide edit Xbbb
1851 call assert_equal([
1852 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1853 \ 'type': 'one', 'length': 2, 'start': 1},
1854 \ {'lnum': 4, 'id': 10, 'col': 8, 'type_bufnr': 0, 'end': 1,
1855 \ 'type': 'two', 'length': 2, 'start': 1}],
1856 \ prop_list(1, {'bufnr': bnr,
1857 \ 'types': ['one', 'two'], 'ids': [5, 10], 'end_lnum': -1}))
1858 #" get text properties from an unloaded buffer
1859 bunload! Xaaa
1860 call assert_equal([], prop_list(1, {'bufnr': bnr, 'end_lnum': -1}))
1861
Bram Moolenaar62aec932022-01-29 21:45:34 +00001862 call g:DeletePropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001863 :%bw!
1864 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001865 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001866endfunc
Bram Moolenaar23999d72020-12-23 14:36:00 +01001867
LemonBoy9bd3ce22022-04-18 21:54:02 +01001868func Test_prop_find_prev_on_same_line()
1869 new
1870
1871 call setline(1, 'the quikc bronw fox jumsp over the layz dog')
1872 call prop_type_add('misspell', #{highlight: 'ErrorMsg'})
1873 for col in [8, 14, 24, 38]
1874 call prop_add(1, col, #{type: 'misspell', length: 2})
1875 endfor
1876
1877 call cursor(1,18)
1878 let expected = [
1879 \ #{lnum: 1, id: 0, col: 14, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1},
1880 \ #{lnum: 1, id: 0, col: 24, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1}
1881 \ ]
1882
1883 let result = prop_find(#{type: 'misspell'}, 'b')
1884 call assert_equal(expected[0], result)
1885 let result = prop_find(#{type: 'misspell'}, 'f')
1886 call assert_equal(expected[1], result)
1887
1888 call prop_type_delete('misspell')
1889 bwipe!
1890endfunc
1891
LemonBoyb7a70122022-05-13 12:41:50 +01001892func Test_prop_spell()
1893 new
1894 set spell
1895 call AddPropTypes()
1896
1897 call setline(1, ["helo world", "helo helo helo"])
1898 call prop_add(1, 1, #{type: 'one', length: 4})
1899 call prop_add(1, 6, #{type: 'two', length: 5})
1900 call prop_add(2, 1, #{type: 'three', length: 4})
1901 call prop_add(2, 6, #{type: 'three', length: 4})
1902 call prop_add(2, 11, #{type: 'three', length: 4})
1903
1904 " The first prop over 'helo' increases its length after the word is corrected
1905 " to 'Hello', the second one is shifted to the right.
1906 let expected = [
1907 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
1908 \ 'length': 5, 'start': 1},
1909 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'two',
1910 \ 'length': 5, 'start': 1}
1911 \ ]
1912 call feedkeys("z=1\<CR>", 'xt')
1913
1914 call assert_equal('Hello world', getline(1))
1915 call assert_equal(expected, prop_list(1))
1916
1917 " Repeat the replacement done by z=
1918 spellrepall
1919
1920 let expected = [
1921 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'three',
1922 \ 'length': 5, 'start': 1},
1923 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'three',
1924 \ 'length': 5, 'start': 1},
1925 \ {'id': 0, 'col': 13, 'type_bufnr': 0, 'end': 1, 'type': 'three',
1926 \ 'length': 5, 'start': 1}
1927 \ ]
1928 call assert_equal('Hello Hello Hello', getline(2))
1929 call assert_equal(expected, prop_list(2))
1930
1931 call DeletePropTypes()
1932 set spell&
1933 bwipe!
1934endfunc
1935
LemonBoy4b936742022-05-13 21:56:28 +01001936func Test_prop_shift_block()
1937 new
1938 call AddPropTypes()
1939
1940 call setline(1, ['some highlighted text']->repeat(2))
1941 call prop_add(1, 10, #{type: 'one', length: 11})
1942 call prop_add(2, 10, #{type: 'two', length: 11})
1943
1944 call cursor(1, 1)
1945 call feedkeys("5l\<c-v>>", 'nxt')
1946 call cursor(2, 1)
1947 call feedkeys("5l\<c-v><", 'nxt')
1948
1949 let expected = [
1950 \ {'lnum': 1, 'id': 0, 'col': 8, 'type_bufnr': 0, 'end': 1, 'type': 'one',
1951 \ 'length': 11, 'start' : 1},
1952 \ {'lnum': 2, 'id': 0, 'col': 6, 'type_bufnr': 0, 'end': 1, 'type': 'two',
1953 \ 'length': 11, 'start' : 1}
1954 \ ]
1955 call assert_equal(expected, prop_list(1, #{end_lnum: 2}))
1956
1957 call DeletePropTypes()
1958 bwipe!
1959endfunc
LemonBoyb7a70122022-05-13 12:41:50 +01001960
LemonBoy698cb4c2022-05-14 18:10:15 +01001961func Test_prop_insert_multiline()
1962 new
1963 call AddPropTypes()
1964
1965 call setline(1, ['foobar', 'barbaz'])
1966 call prop_add(1, 4, #{end_lnum: 2, end_col: 4, type: 'one'})
1967
1968 call feedkeys("1Goquxqux\<Esc>", 'nxt')
1969 call feedkeys("2GOquxqux\<Esc>", 'nxt')
1970
1971 let lines =<< trim END
1972 foobar
1973 quxqux
1974 quxqux
1975 barbaz
1976 END
1977 call assert_equal(lines, getline(1, '$'))
1978 let expected = [
1979 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 0, 'type': 'one',
1980 \ 'length': 4 ,'start': 1},
1981 \ {'lnum': 2, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
1982 \ 'length': 7, 'start': 0},
1983 \ {'lnum': 3, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
1984 \ 'length': 7, 'start': 0},
1985 \ {'lnum': 4, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
1986 \ 'length': 3, 'start': 0}
1987 \ ]
1988 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
1989
1990 call DeletePropTypes()
1991 bwipe!
1992endfunc
1993
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001994" vim: shiftwidth=2 sts=2 expandtab