blob: 50012acaf8ef7b76de9c33c3490f55079f52766d [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
LemonBoy788c06a2022-05-14 18:48:05 +0100638 " split at the space character with 'ai' active, the leading space is removed
639 " in the second line and the prop is shifted accordingly.
640 let expected = SetupOneLine() " 'xonex xtwoxx'
641 set ai
642 exe "normal 6|i\<CR>\<Esc>"
643 call assert_equal('xonex', getline(1))
644 call assert_equal('xtwoxx', getline(2))
645 let expected[1].col -= 6
646 call assert_equal(expected, prop_list(1) + prop_list(2))
647 set ai&
648 call DeletePropTypes()
649
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200650 bwipe!
651 set bs&
652endfunc
653
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100654func Test_prop_clear()
655 new
656 call AddPropTypes()
657 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100658 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100659
Bram Moolenaara5a78822019-09-04 21:57:18 +0200660 eval 1->prop_clear()
661 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100662
663 call DeletePropTypes()
664 bwipe!
665endfunc
666
667func Test_prop_clear_buf()
668 new
669 call AddPropTypes()
670 call SetupPropsInFirstLine()
671 let bufnr = bufnr('')
672 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100673 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100674
675 call prop_clear(1, 1, {'bufnr': bufnr})
676 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
677
678 wincmd w
679 call DeletePropTypes()
680 bwipe!
681endfunc
682
Bram Moolenaar21b50382019-01-04 18:07:24 +0100683func Test_prop_setline()
684 new
685 call AddPropTypes()
686 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100687 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100688
689 call setline(1, 'foobar')
690 call assert_equal([], prop_list(1))
691
692 call DeletePropTypes()
693 bwipe!
694endfunc
695
696func Test_prop_setbufline()
697 new
698 call AddPropTypes()
699 call SetupPropsInFirstLine()
700 let bufnr = bufnr('')
701 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100702 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100703
704 call setbufline(bufnr, 1, 'foobar')
705 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
706
707 wincmd w
708 call DeletePropTypes()
709 bwipe!
710endfunc
711
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100712func Test_prop_substitute()
713 new
714 " Set first line to 'one two three'
715 call AddPropTypes()
716 call SetupPropsInFirstLine()
717 let expected_props = Get_expected_props()
718 call assert_equal(expected_props, prop_list(1))
719
720 " Change "n" in "one" to XX: 'oXXe two three'
721 s/n/XX/
722 let expected_props[0].length += 1
723 let expected_props[1].length += 1
724 let expected_props[2].col += 1
725 let expected_props[3].col += 1
726 call assert_equal(expected_props, prop_list(1))
727
728 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
729 s/t//g
730 let expected_props[0].length -= 2
731 let expected_props[2].length -= 1
732 let expected_props[3].length -= 1
733 let expected_props[3].col -= 1
734 call assert_equal(expected_props, prop_list(1))
735
736 " Split the line by changing w to line break: 'oXXe ', 'o hree'
737 " The long prop is split and spans both lines.
738 " The props on "two" and "three" move to the next line.
739 s/w/\r/
740 let new_props = [
741 \ copy(expected_props[0]),
742 \ copy(expected_props[2]),
743 \ copy(expected_props[3]),
744 \ ]
745 let expected_props[0].length = 5
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200746 let expected_props[0].end = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100747 unlet expected_props[3]
748 unlet expected_props[2]
749 call assert_equal(expected_props, prop_list(1))
750
751 let new_props[0].length = 6
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200752 let new_props[0].start = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100753 let new_props[1].col = 1
754 let new_props[1].length = 1
755 let new_props[2].col = 3
756 call assert_equal(new_props, prop_list(2))
757
758 call DeletePropTypes()
759 bwipe!
760endfunc
761
Bram Moolenaar663bc892019-01-08 23:07:24 +0100762func Test_prop_change_indent()
763 call prop_type_add('comment', {'highlight': 'Directory'})
764 new
765 call setline(1, [' xxx', 'yyyyy'])
766 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200767 let expect = #{type_bufnr: 0, col: 2, length: 2, type: 'comment', start: 1, end: 1, id: 0}
Bram Moolenaar663bc892019-01-08 23:07:24 +0100768 call assert_equal([expect], prop_list(2))
769
770 set shiftwidth=3
771 normal 2G>>
772 call assert_equal(' yyyyy', getline(2))
773 let expect.col += 3
774 call assert_equal([expect], prop_list(2))
775
776 normal 2G==
777 call assert_equal(' yyyyy', getline(2))
778 let expect.col = 6
779 call assert_equal([expect], prop_list(2))
780
781 call prop_clear(2)
782 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
783 let expect.col = 2
784 let expect.length = 5
785 call assert_equal([expect], prop_list(2))
786
787 normal 2G<<
788 call assert_equal(' yyyyy', getline(2))
789 let expect.length = 2
790 call assert_equal([expect], prop_list(2))
791
792 set shiftwidth&
793 call prop_type_delete('comment')
794endfunc
795
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100796" Setup a three line prop in lines 2 - 4.
797" Add short props in line 1 and 5.
798func Setup_three_line_prop()
799 new
800 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
801 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
802 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
803 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
804endfunc
805
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100806func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200807 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100808 new
809 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
810
811 " start halfway line 1, end halfway line 3
812 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200813 let expect1 = #{type_bufnr: 0, col: 3, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100814 call assert_equal([expect1], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200815 let expect2 = #{type_bufnr: 0, col: 1, length: 10, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100816 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200817 let expect3 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100818 call assert_equal([expect3], prop_list(3))
819 call prop_clear(1, 3)
820
821 " include all three lines
822 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
823 let expect1.col = 1
824 let expect1.length = 8
825 call assert_equal([expect1], prop_list(1))
826 call assert_equal([expect2], prop_list(2))
827 let expect3.length = 9
828 call assert_equal([expect3], prop_list(3))
829 call prop_clear(1, 3)
830
831 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100832
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100833 " Test deleting the first 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 expect_short = #{type_bufnr: 0, col: 2, length: 1, type: 'comment', start: 1, end: 1, id: 0}
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100836 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200837 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100838 call assert_equal([expect2], prop_list(2))
839 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100840 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200841 let expect2 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100842 call assert_equal([expect2], prop_list(2))
843 bwipe!
844
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100845 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100846 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200847 let expect3 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100848 call assert_equal([expect3], prop_list(3))
Martin Tournoije2390c72021-07-28 13:30:16 +0200849 let expect4 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100850 call assert_equal([expect4], prop_list(4))
851 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100852 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100853 call assert_equal([expect3], prop_list(3))
854 call assert_equal([expect_short], prop_list(4))
855 bwipe!
856
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100857 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100858 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200859 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100860 call assert_equal([expect2], prop_list(2))
861 call append(2, "new line")
862 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200863 let expect3 = #{type_bufnr: 0, col: 1, length: 9, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100864 call assert_equal([expect3], prop_list(3))
865 bwipe!
866
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100867 call prop_type_delete('comment')
868endfunc
869
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100870func Test_prop_line2byte()
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100871 call prop_type_add('comment', {'highlight': 'Directory'})
872 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100873 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100874 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100875 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100876 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100877 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100878 bwipe!
Bram Moolenaar14c75302021-08-15 14:28:40 +0200879
880 new
Bram Moolenaara401bba2021-08-15 15:04:41 +0200881 setlocal ff=unix
Bram Moolenaar14c75302021-08-15 14:28:40 +0200882 call setline(1, range(500))
883 call assert_equal(1491, line2byte(401))
884 call prop_add(2, 1, {'type': 'comment'})
885 call prop_add(222, 1, {'type': 'comment'})
886 call assert_equal(1491, line2byte(401))
887 call prop_remove({'type': 'comment'})
888 call assert_equal(1491, line2byte(401))
889 bwipe!
890
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +0200891 new
Bram Moolenaar49b93042021-08-25 17:02:00 +0200892 setlocal ff=unix
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +0200893 call setline(1, range(520))
894 call assert_equal(1491, line2byte(401))
895 call prop_add(2, 1, {'type': 'comment'})
896 call assert_equal(1491, line2byte(401))
897 2delete
898 call assert_equal(1489, line2byte(400))
899 bwipe!
900
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100901 call prop_type_delete('comment')
902endfunc
903
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100904func Test_prop_byte2line()
905 new
906 set ff=unix
907 call setline(1, ['one one', 'two two', 'three three', 'four four', 'five'])
908 call assert_equal(4, byte2line(line2byte(4)))
909 call assert_equal(5, byte2line(line2byte(5)))
910
911 call prop_type_add('prop', {'highlight': 'Directory'})
912 call prop_add(3, 1, {'length': 5, 'type': 'prop'})
913 call assert_equal(4, byte2line(line2byte(4)))
914 call assert_equal(5, byte2line(line2byte(5)))
915
916 bwipe!
917 call prop_type_delete('prop')
918endfunc
919
Bram Moolenaar59ff6402021-01-30 17:16:28 +0100920func Test_prop_goto_byte()
921 new
922 call setline(1, '')
923 call setline(2, 'two three')
924 call setline(3, '')
925 call setline(4, 'four five')
926
927 call prop_type_add('testprop', {'highlight': 'Directory'})
928 call search('^two')
929 call prop_add(line('.'), col('.'), {
930 \ 'length': len('two'),
931 \ 'type': 'testprop'
932 \ })
933
934 call search('two \zsthree')
935 let expected_pos = line2byte(line('.')) + col('.') - 1
936 exe expected_pos .. 'goto'
937 let actual_pos = line2byte(line('.')) + col('.') - 1
938 eval actual_pos->assert_equal(expected_pos)
939
940 call search('four \zsfive')
941 let expected_pos = line2byte(line('.')) + col('.') - 1
942 exe expected_pos .. 'goto'
943 let actual_pos = line2byte(line('.')) + col('.') - 1
944 eval actual_pos->assert_equal(expected_pos)
945
946 call prop_type_delete('testprop')
947 bwipe!
948endfunc
949
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100950func Test_prop_undo()
951 new
952 call prop_type_add('comment', {'highlight': 'Directory'})
953 call setline(1, ['oneone', 'twotwo', 'three'])
954 " Set 'undolevels' to break changes into undo-able pieces.
955 set ul&
956
957 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200958 let expected = [#{type_bufnr: 0, col: 3, length: 2, id: 0, type: 'comment', start: 1, end: 1}]
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100959 call assert_equal(expected, prop_list(1))
960
961 " Insert a character, then undo.
962 exe "normal 0lllix\<Esc>"
963 set ul&
964 let expected[0].length = 3
965 call assert_equal(expected, prop_list(1))
966 undo
967 let expected[0].length = 2
968 call assert_equal(expected, prop_list(1))
969
970 " Delete a character, then undo
971 exe "normal 0lllx"
972 set ul&
973 let expected[0].length = 1
974 call assert_equal(expected, prop_list(1))
975 undo
976 let expected[0].length = 2
977 call assert_equal(expected, prop_list(1))
978
979 " Delete the line, then undo
980 1d
981 set ul&
982 call assert_equal([], prop_list(1))
983 undo
984 call assert_equal(expected, prop_list(1))
985
986 " Insert a character, delete two characters, then undo with "U"
987 exe "normal 0lllix\<Esc>"
988 set ul&
989 let expected[0].length = 3
990 call assert_equal(expected, prop_list(1))
991 exe "normal 0lllxx"
992 set ul&
993 let expected[0].length = 1
994 call assert_equal(expected, prop_list(1))
995 normal U
996 let expected[0].length = 2
997 call assert_equal(expected, prop_list(1))
998
Bram Moolenaar338dfda2019-05-19 15:19:57 +0200999 " substitute a word, then undo
1000 call setline(1, 'the number 123 is highlighted.')
1001 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001002 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001003 call assert_equal(expected, prop_list(1))
1004 set ul&
1005 1s/number/foo
1006 let expected[0].col = 9
1007 call assert_equal(expected, prop_list(1))
1008 undo
1009 let expected[0].col = 12
1010 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001011 call prop_clear(1)
1012
1013 " substitute with backslash
1014 call setline(1, 'the number 123 is highlighted.')
1015 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001016 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001017 call assert_equal(expected, prop_list(1))
1018 1s/the/\The
1019 call assert_equal(expected, prop_list(1))
1020 1s/^/\\
1021 let expected[0].col += 1
1022 call assert_equal(expected, prop_list(1))
1023 1s/^/\~
1024 let expected[0].col += 1
1025 call assert_equal(expected, prop_list(1))
1026 1s/123/12\\3
1027 let expected[0].length += 1
1028 call assert_equal(expected, prop_list(1))
1029 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001030
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001031 bwipe!
1032 call prop_type_delete('comment')
1033endfunc
1034
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001035func Test_prop_delete_text()
1036 new
1037 call prop_type_add('comment', {'highlight': 'Directory'})
1038 call setline(1, ['oneone', 'twotwo', 'three'])
1039
1040 " zero length property
1041 call prop_add(1, 3, {'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001042 let expected = [#{type_bufnr: 0, col: 3, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001043 call assert_equal(expected, prop_list(1))
1044
1045 " delete one char moves the property
1046 normal! x
Martin Tournoije2390c72021-07-28 13:30:16 +02001047 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001048 call assert_equal(expected, prop_list(1))
1049
1050 " delete char of the property has no effect
1051 normal! lx
Martin Tournoije2390c72021-07-28 13:30:16 +02001052 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001053 call assert_equal(expected, prop_list(1))
1054
1055 " delete more chars moves property to first column, is not deleted
1056 normal! 0xxxx
Martin Tournoije2390c72021-07-28 13:30:16 +02001057 let expected = [#{type_bufnr: 0, col: 1, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001058 call assert_equal(expected, prop_list(1))
1059
1060 bwipe!
1061 call prop_type_delete('comment')
1062endfunc
1063
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001064" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +02001065func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +02001066 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +01001067 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +02001068 if g:orig_encoding != 'utf-8'
1069 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001070 endif
1071 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +02001072 \ "call setline(1, ["
1073 \ .. "'One two',"
1074 \ .. "'Numbér 123 änd thœn 4¾7.',"
1075 \ .. "'--aa--bb--cc--dd--',"
1076 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001077 \ .. "'first line',"
1078 \ .. "' second line ',"
1079 \ .. "'third line',"
1080 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +02001081 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001082 \ "hi NumberProp ctermfg=blue",
1083 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001084 \ "hi BackgroundProp ctermbg=lightgrey",
1085 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001086 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +02001087 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
1088 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001089 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
1090 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
1091 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001092 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
1093 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
1094 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +01001095 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001096 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001097 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
1098 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001099 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
1100 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
1101 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
1102 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001103 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
1104 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001105 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001106 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
1107 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
1108 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
1109 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +02001110 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001111 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001112 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001113 \ "syn match Comment '//.*'",
1114 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001115 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01001116 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001117 \ "normal 6G0i\<BS>\<Esc>",
1118 \ "normal 3J",
1119 \ "normal 3G",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001120 \], 'XtestProp')
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001121 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001122 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +01001123
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001124 " clean up
1125 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +01001126 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001127endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +02001128
1129func RunTestVisualBlock(width, dump)
1130 call writefile([
1131 \ "call setline(1, ["
1132 \ .. "'xxxxxxxxx 123 x',"
1133 \ .. "'xxxxxxxx 123 x',"
1134 \ .. "'xxxxxxx 123 x',"
1135 \ .. "'xxxxxx 123 x',"
1136 \ .. "'xxxxx 123 x',"
1137 \ .. "'xxxx 123 xx',"
1138 \ .. "'xxx 123 xxx',"
1139 \ .. "'xx 123 xxxx',"
1140 \ .. "'x 123 xxxxx',"
1141 \ .. "' 123 xxxxxx',"
1142 \ .. "])",
1143 \ "hi SearchProp ctermbg=yellow",
1144 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
1145 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
1146 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
1147 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
1148 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
1149 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
1150 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
1151 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
1152 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
1153 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
1154 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
1155 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
1156 \], 'XtestPropVis')
1157 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
1158 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
1159
1160 " clean up
1161 call StopVimInTerminal(buf)
1162 call delete('XtestPropVis')
1163endfunc
1164
1165" screenshot test with Visual block mode operations
1166func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +02001167 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +02001168
1169 " Delete two columns while text props are three chars wide.
1170 call RunTestVisualBlock(2, '01')
1171
1172 " Same, but delete four columns
1173 call RunTestVisualBlock(4, '02')
1174endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +02001175
Bram Moolenaara956bf62019-06-19 17:34:24 +02001176func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +02001177 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +02001178
Bram Moolenaara956bf62019-06-19 17:34:24 +02001179 let lines =<< trim END
1180 call setline(1, [
1181 \ "\txxx",
1182 \ "x\txxx",
1183 \ ])
1184 hi SearchProp ctermbg=yellow
1185 call prop_type_add('search', {'highlight': 'SearchProp'})
1186 call prop_add(1, 2, {'length': 3, 'type': 'search'})
1187 call prop_add(2, 3, {'length': 3, 'type': 'search'})
1188 END
1189 call writefile(lines, 'XtestPropTab')
1190 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
1191 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
1192
1193 " clean up
1194 call StopVimInTerminal(buf)
1195 call delete('XtestPropTab')
1196endfunc
1197
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001198func Test_textprop_nowrap_scrolled()
1199 CheckScreendump
1200
1201 let lines =<< trim END
1202 vim9script
1203 set nowrap
1204 setline(1, 'The number 123 is smaller than 4567.' .. repeat('X', &columns))
1205 prop_type_add('number', {'highlight': 'ErrorMsg'})
1206 prop_add(1, 12, {'length': 3, 'type': 'number'})
1207 prop_add(1, 32, {'length': 4, 'type': 'number'})
1208 feedkeys('gg20zl', 'nxt')
1209 END
1210 call writefile(lines, 'XtestNowrap')
1211 let buf = RunVimInTerminal('-S XtestNowrap', {'rows': 6})
1212 call VerifyScreenDump(buf, 'Test_textprop_nowrap_01', {})
1213
1214 call term_sendkeys(buf, "$")
1215 call VerifyScreenDump(buf, 'Test_textprop_nowrap_02', {})
1216
1217 " clean up
1218 call StopVimInTerminal(buf)
1219 call delete('XtestNowrap')
1220endfunc
1221
Bram Moolenaar34390282019-10-16 14:38:26 +02001222func Test_textprop_with_syntax()
1223 CheckScreendump
1224
1225 let lines =<< trim END
1226 call setline(1, [
1227 \ "(abc)",
1228 \ ])
1229 syn match csParens "[()]" display
1230 hi! link csParens MatchParen
1231
1232 call prop_type_add('TPTitle', #{ highlight: 'Title' })
1233 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
1234 END
1235 call writefile(lines, 'XtestPropSyn')
1236 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
1237 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
1238
1239 " clean up
1240 call StopVimInTerminal(buf)
1241 call delete('XtestPropSyn')
1242endfunc
1243
Bram Moolenaard79eef22019-05-24 20:41:55 +02001244" Adding a text property to a new buffer should not fail
1245func Test_textprop_empty_buffer()
1246 call prop_type_add('comment', {'highlight': 'Search'})
1247 new
1248 call prop_add(1, 1, {'type': 'comment'})
1249 close
Bram Moolenaaradfde112019-05-25 22:11:45 +02001250 call prop_type_delete('comment')
1251endfunc
1252
Bram Moolenaard74af422019-06-28 21:38:00 +02001253" Adding a text property with invalid highlight should be ignored.
1254func Test_textprop_invalid_highlight()
1255 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
1256 new
1257 call setline(1, ['asdf','asdf'])
1258 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
1259 redraw
1260 bwipe!
1261 call prop_type_delete('dni')
1262endfunc
1263
Bram Moolenaaradfde112019-05-25 22:11:45 +02001264" Adding a text property to an empty buffer and then editing another
1265func Test_textprop_empty_buffer_next()
1266 call prop_type_add("xxx", {})
1267 call prop_add(1, 1, {"type": "xxx"})
1268 next X
1269 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +02001270endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001271
1272func Test_textprop_remove_from_buf()
1273 new
1274 let buf = bufnr('')
1275 call prop_type_add('one', {'bufnr': buf})
1276 call prop_add(1, 1, {'type': 'one', 'id': 234})
1277 file x
1278 edit y
1279 call prop_remove({'id': 234, 'bufnr': buf}, 1)
1280 call prop_type_delete('one', {'bufnr': buf})
1281 bwipe! x
1282 close
1283endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +02001284
1285func Test_textprop_in_unloaded_buf()
1286 edit Xaaa
1287 call setline(1, 'aaa')
1288 write
1289 edit Xbbb
1290 call setline(1, 'bbb')
1291 write
1292 let bnr = bufnr('')
1293 edit Xaaa
1294
1295 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
1296 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
1297 exe 'buf ' .. bnr
1298 call assert_equal('bbb', getline(1))
1299 call assert_equal(0, prop_list(1)->len())
1300
1301 bwipe! Xaaa
1302 bwipe! Xbbb
1303 cal delete('Xaaa')
1304 cal delete('Xbbb')
1305endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001306
1307func Test_proptype_substitute2()
1308 new
1309 " text_prop.vim
1310 call setline(1, [
1311 \ 'The num 123 is smaller than 4567.',
1312 \ '123 The number 123 is smaller than 4567.',
1313 \ '123 The number 123 is smaller than 4567.'])
1314
1315 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1316
1317 call prop_add(1, 12, {'length': 3, 'type': 'number'})
1318 call prop_add(2, 1, {'length': 3, 'type': 'number'})
1319 call prop_add(3, 36, {'length': 4, 'type': 'number'})
1320 set ul&
Martin Tournoije2390c72021-07-28 13:30:16 +02001321 let expected = [
1322 \ #{type_bufnr: 0, id: 0, col: 13, end: 1, type: 'number', length: 3, start: 1},
1323 \ #{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'number', length: 3, start: 1},
1324 \ #{type_bufnr: 0, id: 0, col: 50, end: 1, type: 'number', length: 4, start: 1}]
1325
1326 " TODO
1327 return
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001328 " Add some text in between
1329 %s/\s\+/ /g
Martin Tournoije2390c72021-07-28 13:30:16 +02001330 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001331
1332 " remove some text
1333 :1s/[a-z]\{3\}//g
1334 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
1335 call assert_equal(expected, prop_list(1))
1336 bwipe!
1337endfunc
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001338
Bram Moolenaar8902b312020-09-20 21:04:35 +02001339" This was causing property corruption.
1340func Test_proptype_substitute3()
1341 new
1342 call setline(1, ['abcxxx', 'def'])
1343 call prop_type_add("test", {"highlight": "Search"})
1344 call prop_add(1, 2, {"end_lnum": 2, "end_col": 2, "type": "test"})
1345 %s/x\+$//
1346 redraw
1347
1348 call prop_type_delete('test')
1349 bwipe!
1350endfunc
1351
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001352func SaveOptions()
1353 let d = #{tabstop: &tabstop,
1354 \ softtabstop: &softtabstop,
1355 \ shiftwidth: &shiftwidth,
1356 \ expandtab: &expandtab,
1357 \ foldmethod: '"' .. &foldmethod .. '"',
1358 \ }
1359 return d
1360endfunc
1361
1362func RestoreOptions(dict)
1363 for name in keys(a:dict)
1364 exe 'let &' .. name .. ' = ' .. a:dict[name]
1365 endfor
1366endfunc
1367
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001368func Test_textprop_noexpandtab()
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001369 new
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001370 let save_dict = SaveOptions()
1371
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001372 set tabstop=8
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001373 set softtabstop=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001374 set shiftwidth=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001375 set noexpandtab
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001376 set foldmethod=marker
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001377
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001378 call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx")
1379 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1380 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1381 call feedkeys("0i\<tab>", "tx")
1382 call prop_remove({'type': 'test'})
1383 call prop_add(1, 2, {'end_col': 3, 'type': 'test'})
1384 call feedkeys("A\<left>\<tab>", "tx")
1385 call prop_remove({'type': 'test'})
1386 try
1387 " It is correct that this does not pass
1388 call prop_add(1, 6, {'end_col': 7, 'type': 'test'})
1389 " Has already collapsed here, start_col:6 does not result in an error
1390 call feedkeys("A\<left>\<tab>", "tx")
1391 catch /^Vim\%((\a\+)\)\=:E964/
1392 endtry
1393 call prop_remove({'type': 'test'})
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001394 call prop_type_delete('test')
1395
1396 call RestoreOptions(save_dict)
1397 bwipe!
1398endfunc
1399
1400func Test_textprop_noexpandtab_redraw()
1401 new
1402 let save_dict = SaveOptions()
1403
1404 set tabstop=8
1405 set softtabstop=4
1406 set shiftwidth=4
1407 set noexpandtab
1408 set foldmethod=marker
1409
1410 call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx")
1411 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1412 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1413 call feedkeys("0i\<tab>", "tx")
1414 " Internally broken at the next line
1415 call feedkeys("A\<left>\<tab>", "tx")
1416 redraw
1417 " Index calculation failed internally on next line
1418 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1419 call prop_remove({'type': 'test', 'all': v:true})
1420 call prop_type_delete('test')
1421 call prop_type_delete('test')
1422
1423 call RestoreOptions(save_dict)
1424 bwipe!
1425endfunc
1426
1427func Test_textprop_ins_str()
1428 new
1429 call setline(1, 'just some text')
1430 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1431 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001432 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 +01001433
1434 call feedkeys("foi\<F8>\<Esc>", "tx")
1435 call assert_equal('just s<F8>ome text', getline(1))
Martin Tournoije2390c72021-07-28 13:30:16 +02001436 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 +01001437
1438 bwipe!
1439 call prop_remove({'type': 'test'})
1440 call prop_type_delete('test')
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001441endfunc
Bram Moolenaar66b98852020-03-11 19:15:52 +01001442
1443func Test_find_prop_later_in_line()
1444 new
1445 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1446 call setline(1, 'just some text')
1447 call prop_add(1, 1, {'length': 4, 'type': 'test'})
1448 call prop_add(1, 10, {'length': 3, 'type': 'test'})
1449
Martin Tournoije2390c72021-07-28 13:30:16 +02001450 call assert_equal(
1451 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 10, end: 1, type: 'test', length: 3, start: 1},
1452 \ prop_find(#{type: 'test', lnum: 1, col: 6}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001453
1454 bwipe!
1455 call prop_type_delete('test')
1456endfunc
1457
1458func Test_find_zerowidth_prop_sol()
1459 new
1460 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1461 call setline(1, 'just some text')
1462 call prop_add(1, 1, {'length': 0, 'type': 'test'})
1463
Martin Tournoije2390c72021-07-28 13:30:16 +02001464 call assert_equal(
1465 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 1, end: 1, type: 'test', length: 0, start: 1},
1466 \ prop_find(#{type: 'test', lnum: 1}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001467
1468 bwipe!
1469 call prop_type_delete('test')
1470endfunc
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001471
1472" Test for passing invalid arguments to prop_xxx() functions
1473func Test_prop_func_invalid_args()
1474 call assert_fails('call prop_clear(1, 2, [])', 'E715:')
1475 call assert_fails('call prop_clear(-1, 2)', 'E16:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001476 call assert_fails('call prop_find(test_null_dict())', 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001477 call assert_fails('call prop_find({"bufnr" : []})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001478 call assert_fails('call prop_find({})', 'E968:')
1479 call assert_fails('call prop_find({}, "x")', 'E474:')
1480 call assert_fails('call prop_find({"lnum" : -2})', 'E16:')
1481 call assert_fails('call prop_list(1, [])', 'E715:')
Bram Moolenaar9d489562020-07-30 20:08:50 +02001482 call assert_fails('call prop_list(-1, {})', 'E16:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001483 call assert_fails('call prop_remove([])', 'E474:')
1484 call assert_fails('call prop_remove({}, -2)', 'E16:')
1485 call assert_fails('call prop_remove({})', 'E968:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001486 call assert_fails('call prop_type_add([], {})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001487 call assert_fails("call prop_type_change('long', {'xyz' : 10})", 'E971:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001488 call assert_fails("call prop_type_delete([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001489 call assert_fails("call prop_type_delete('xyz', [])", 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001490 call assert_fails("call prop_type_get([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001491 call assert_fails("call prop_type_get('', [])", 'E474:')
1492 call assert_fails("call prop_type_list([])", 'E715:')
Bram Moolenaar3dc34742021-03-02 13:36:47 +01001493 call assert_fails("call prop_type_add('yyy', 'not_a_dict')", 'E715:')
1494 call assert_fails("call prop_add(1, 5, {'type':'missing_type', 'length':1})", 'E971:')
1495 call assert_fails("call prop_add(1, 5, {'type': ''})", 'E971:')
1496 call assert_fails('call prop_add(1, 1, 0)', 'E715:')
1497
1498 new
1499 call setline(1, ['first', 'second'])
1500 call prop_type_add('xxx', {})
1501
1502 call assert_fails("call prop_type_add('xxx', {})", 'E969:')
1503 call assert_fails("call prop_add(2, 0, {'type': 'xxx'})", 'E964:')
1504 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':1})", 'E475:')
1505 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':3})", 'E966:')
1506 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'length':-1})", 'E475:')
1507 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_col':0})", 'E475:')
1508 call assert_fails("call prop_add(2, 3, {'length':1})", 'E965:')
1509
1510 call prop_type_delete('xxx')
1511 bwipe!
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001512endfunc
1513
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001514func Test_prop_split_join()
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001515 new
1516 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1517 call setline(1, 'just some text')
1518 call prop_add(1, 6, {'length': 4, 'type': 'test'})
1519
1520 " Split in middle of "some"
1521 execute "normal! 8|i\<CR>"
Martin Tournoije2390c72021-07-28 13:30:16 +02001522 call assert_equal(
1523 \ [#{type_bufnr: 0, id: 0, col: 6, end: 0, type: 'test', length: 2, start: 1}],
1524 \ prop_list(1))
1525 call assert_equal(
1526 \ [#{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'test', length: 2, start: 0}],
1527 \ prop_list(2))
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001528
1529 " Join the two lines back together
1530 normal! 1GJ
Martin Tournoije2390c72021-07-28 13:30:16 +02001531 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 +02001532
1533 bwipe!
1534 call prop_type_delete('test')
1535endfunc
1536
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001537func Test_prop_increment_decrement()
1538 new
1539 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1540 call setline(1, 'its 998 times')
1541 call prop_add(1, 5, {'length': 3, 'type': 'test'})
1542
1543 exe "normal! 0f9\<C-A>"
1544 eval getline(1)->assert_equal('its 999 times')
1545 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001546 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 3, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001547
1548 exe "normal! 0f9\<C-A>"
1549 eval getline(1)->assert_equal('its 1000 times')
1550 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001551 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 4, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001552
1553 bwipe!
1554 call prop_type_delete('test')
1555endfunc
1556
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001557func Test_prop_block_insert()
1558 new
1559 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1560 call setline(1, ['one ', 'two '])
1561 call prop_add(1, 1, {'length': 3, 'type': 'test'})
1562 call prop_add(2, 1, {'length': 3, 'type': 'test'})
1563
1564 " insert "xx" in the first column of both lines
1565 exe "normal! gg0\<C-V>jIxx\<Esc>"
1566 eval getline(1, 2)->assert_equal(['xxone ', 'xxtwo '])
Martin Tournoije2390c72021-07-28 13:30:16 +02001567 let expected = [#{type_bufnr: 0, id: 0, col: 3, end: 1, type: 'test', length: 3, start: 1}]
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001568 eval prop_list(1)->assert_equal(expected)
1569 eval prop_list(2)->assert_equal(expected)
1570
1571 " insert "yy" inside the text props to make them longer
1572 exe "normal! gg03l\<C-V>jIyy\<Esc>"
1573 eval getline(1, 2)->assert_equal(['xxoyyne ', 'xxtyywo '])
1574 let expected[0].length = 5
1575 eval prop_list(1)->assert_equal(expected)
1576 eval prop_list(2)->assert_equal(expected)
1577
1578 " insert "zz" after the text props, text props don't change
1579 exe "normal! gg07l\<C-V>jIzz\<Esc>"
1580 eval getline(1, 2)->assert_equal(['xxoyynezz ', 'xxtyywozz '])
1581 eval prop_list(1)->assert_equal(expected)
1582 eval prop_list(2)->assert_equal(expected)
1583
1584 bwipe!
1585 call prop_type_delete('test')
1586endfunc
1587
Bram Moolenaar23999d72020-12-23 14:36:00 +01001588" this was causing an ml_get error because w_botline was wrong
1589func Test_prop_one_line_window()
1590 enew
1591 call range(2)->setline(1)
1592 call prop_type_add('testprop', {})
1593 call prop_add(1, 1, {'type': 'testprop'})
1594 call popup_create('popup', {'textprop': 'testprop'})
1595 $
1596 new
1597 wincmd _
1598 call feedkeys("\r", 'xt')
1599 redraw
1600
1601 call popup_clear()
1602 call prop_type_delete('testprop')
1603 close
1604 bwipe!
1605endfunc
1606
Bram Moolenaar840f91f2021-05-26 22:32:10 +02001607" This was calling ml_append_int() and copy a text property from a previous
1608" line at the wrong moment. Exact text length matters.
1609def Test_prop_splits_data_block()
1610 new
1611 var lines: list<string> = [repeat('x', 35)]->repeat(41)
1612 + [repeat('!', 35)]
1613 + [repeat('x', 35)]->repeat(56)
1614 lines->setline(1)
1615 prop_type_add('someprop', {highlight: 'ErrorMsg'})
1616 prop_add(1, 27, {end_lnum: 1, end_col: 70, type: 'someprop'})
1617 prop_remove({type: 'someprop'}, 1)
1618 prop_add(35, 22, {end_lnum: 43, end_col: 43, type: 'someprop'})
1619 prop_remove({type: 'someprop'}, 35, 43)
1620 assert_equal([], prop_list(42))
1621
1622 bwipe!
1623 prop_type_delete('someprop')
1624enddef
1625
Bram Moolenaar4cd5c522021-06-27 13:04:00 +02001626" This was calling ml_delete_int() and try to change text properties.
1627def Test_prop_add_delete_line()
1628 new
1629 var a = 10
1630 var b = 20
1631 repeat([''], a)->append('$')
1632 prop_type_add('Test', {highlight: 'ErrorMsg'})
1633 for lnum in range(1, a)
1634 for col in range(1, b)
1635 prop_add(1, 1, {end_lnum: lnum, end_col: col, type: 'Test'})
1636 endfor
1637 endfor
1638
1639 # check deleting lines is OK
1640 :5del
1641 :1del
1642 :$del
1643
1644 prop_type_delete('Test')
1645 bwipe!
1646enddef
1647
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001648func Test_prop_in_linebreak()
1649 CheckRunVimInTerminal
1650
1651 let lines =<< trim END
1652 set breakindent linebreak breakat+=]
1653 call printf('%s]%s', repeat('x', 50), repeat('x', 70))->setline(1)
1654 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1655 call prop_add(1, 51, #{length: 1, type: 'test'})
1656 END
1657 call writefile(lines, 'XscriptPropLinebreak')
1658 let buf = RunVimInTerminal('-S XscriptPropLinebreak', #{rows: 10})
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001659 call VerifyScreenDump(buf, 'Test_prop_linebreak', {})
1660
1661 call StopVimInTerminal(buf)
1662 call delete('XscriptPropLinebreak')
1663endfunc
1664
Bram Moolenaar42eba042021-11-30 20:22:49 +00001665func Test_prop_after_tab()
1666 CheckRunVimInTerminal
1667
1668 let lines =<< trim END
1669 set breakindent linebreak breakat+=]
1670 call setline(1, "\t[xxx]")
1671 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1672 call prop_add(1, 2, #{length: 1, type: 'test'})
1673 END
1674 call writefile(lines, 'XscriptPropAfterTab')
1675 let buf = RunVimInTerminal('-S XscriptPropAfterTab', #{rows: 10})
Bram Moolenaar42eba042021-11-30 20:22:49 +00001676 call VerifyScreenDump(buf, 'Test_prop_after_tab', {})
1677
1678 call StopVimInTerminal(buf)
1679 call delete('XscriptPropAfterTab')
1680endfunc
1681
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001682func Test_prop_after_linebreak()
1683 CheckRunVimInTerminal
1684
1685 let lines =<< trim END
1686 set linebreak wrap
1687 call printf('%s+(%s)', 'x'->repeat(&columns / 2), 'x'->repeat(&columns / 2))->setline(1)
1688 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1689 call prop_add(1, (&columns / 2) + 2, #{length: 1, type: 'test'})
1690 END
1691 call writefile(lines, 'XscriptPropAfterLinebreak')
1692 let buf = RunVimInTerminal('-S XscriptPropAfterLinebreak', #{rows: 10})
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001693 call VerifyScreenDump(buf, 'Test_prop_after_linebreak', {})
1694
1695 call StopVimInTerminal(buf)
1696 call delete('XscriptPropAfterLinebreak')
1697endfunc
1698
Martin Tournoije2390c72021-07-28 13:30:16 +02001699" Buffer number of 0 should be ignored, as if the parameter wasn't passed.
1700def Test_prop_bufnr_zero()
1701 new
1702 try
1703 var bufnr = bufnr('')
1704 setline(1, 'hello')
1705 prop_type_add('bufnr-global', {highlight: 'ErrorMsg'})
1706 prop_type_add('bufnr-buffer', {highlight: 'StatusLine', bufnr: bufnr})
1707
1708 prop_add(1, 1, {type: 'bufnr-global', length: 1})
1709 prop_add(1, 2, {type: 'bufnr-buffer', length: 1})
1710
1711 var list = prop_list(1)
1712 assert_equal([
1713 {id: 0, col: 1, type_bufnr: 0, end: 1, type: 'bufnr-global', length: 1, start: 1},
1714 {id: 0, col: 2, type_bufnr: bufnr, end: 1, type: 'bufnr-buffer', length: 1, start: 1},
1715 ], list)
1716
1717 assert_equal(
1718 {highlight: 'ErrorMsg', end_incl: 0, start_incl: 0, priority: 0, combine: 1},
1719 prop_type_get('bufnr-global', {bufnr: list[0].type_bufnr}))
1720
1721 assert_equal(
1722 {highlight: 'StatusLine', end_incl: 0, start_incl: 0, priority: 0, bufnr: bufnr, combine: 1},
1723 prop_type_get('bufnr-buffer', {bufnr: list[1].type_bufnr}))
1724 finally
1725 bwipe!
1726 prop_type_delete('bufnr-global')
1727 endtry
1728enddef
1729
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001730" Tests for the prop_list() function
1731func Test_prop_list()
1732 let lines =<< trim END
1733 new
Bram Moolenaar62aec932022-01-29 21:45:34 +00001734 call g:AddPropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001735 call setline(1, repeat([repeat('a', 60)], 10))
1736 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1737 call prop_add(1, 5, {'type': 'two', 'id': 10, 'end_col': 7})
1738 call prop_add(3, 12, {'type': 'one', 'id': 20, 'end_col': 14})
1739 call prop_add(3, 13, {'type': 'two', 'id': 10, 'end_col': 15})
1740 call prop_add(5, 20, {'type': 'one', 'id': 10, 'end_col': 22})
1741 call prop_add(5, 21, {'type': 'two', 'id': 20, 'end_col': 23})
1742 call assert_equal([
1743 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1744 \ 'type': 'one', 'length': 2, 'start': 1},
1745 \ {'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
1746 \ 'type': 'two', 'length': 2, 'start': 1}], prop_list(1))
1747 #" text properties between a few lines
1748 call assert_equal([
1749 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1750 \ 'type': 'one', 'length': 2, 'start': 1},
1751 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
1752 \ 'type': 'two', 'length': 2, 'start': 1},
1753 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
1754 \ 'type': 'one', 'length': 2, 'start': 1},
1755 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
1756 \ 'type': 'two', 'length': 2, 'start': 1}],
1757 \ prop_list(2, {'end_lnum': 5}))
1758 #" text properties across all the lines
1759 call assert_equal([
1760 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1761 \ 'type': 'one', 'length': 2, 'start': 1},
1762 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1763 \ 'type': 'one', 'length': 2, 'start': 1},
1764 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
1765 \ 'type': 'one', 'length': 2, 'start': 1}],
1766 \ prop_list(1, {'types': ['one'], 'end_lnum': -1}))
1767 #" text properties with the specified identifier
1768 call assert_equal([
1769 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1770 \ 'type': 'one', 'length': 2, 'start': 1},
1771 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
1772 \ 'type': 'two', 'length': 2, 'start': 1}],
1773 \ prop_list(1, {'ids': [20], 'end_lnum': 10}))
1774 #" text properties of the specified type and id
1775 call assert_equal([
1776 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
1777 \ 'type': 'two', 'length': 2, 'start': 1},
1778 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
1779 \ 'type': 'two', 'length': 2, 'start': 1}],
1780 \ prop_list(1, {'types': ['two'], 'ids': [10], 'end_lnum': 20}))
1781 call assert_equal([], prop_list(1, {'ids': [40, 50], 'end_lnum': 10}))
1782 call assert_equal([], prop_list(6, {'end_lnum': 10}))
1783 call assert_equal([], prop_list(2, {'end_lnum': 2}))
1784 #" error cases
1785 call assert_fails("echo prop_list(1, {'end_lnum': -20})", 'E16:')
1786 call assert_fails("echo prop_list(4, {'end_lnum': 2})", 'E16:')
1787 call assert_fails("echo prop_list(1, {'end_lnum': '$'})", 'E889:')
1788 call assert_fails("echo prop_list(1, {'types': ['blue'], 'end_lnum': 10})",
1789 \ 'E971:')
1790 call assert_fails("echo prop_list(1, {'types': ['one', 'blue'],
1791 \ 'end_lnum': 10})", 'E971:')
1792 call assert_fails("echo prop_list(1, {'types': ['one', 10],
1793 \ 'end_lnum': 10})", 'E928:')
1794 call assert_fails("echo prop_list(1, {'types': ['']})", 'E971:')
1795 call assert_equal([], prop_list(2, {'types': []}))
1796 call assert_equal([], prop_list(2, {'types': test_null_list()}))
1797 call assert_fails("call prop_list(1, {'types': {}})", 'E714:')
1798 call assert_fails("call prop_list(1, {'types': 'one'})", 'E714:')
1799 call assert_equal([], prop_list(2, {'types': ['one'],
1800 \ 'ids': test_null_list()}))
1801 call assert_equal([], prop_list(2, {'types': ['one'], 'ids': []}))
1802 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': {}})",
1803 \ 'E714:')
1804 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': 10})",
1805 \ 'E714:')
1806 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [[]]})",
1807 \ 'E745:')
1808 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [10, []]})",
1809 \ 'E745:')
Martin Tournoije2390c72021-07-28 13:30:16 +02001810
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001811 #" get text properties from a non-current buffer
1812 wincmd w
1813 call assert_equal([
1814 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1815 \ 'type': 'one', 'length': 2, 'start': 1},
1816 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
1817 \ 'type': 'two', 'length': 2, 'start': 1},
1818 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1819 \ 'type': 'one', 'length': 2, 'start': 1},
1820 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
1821 \ 'type': 'two', 'length': 2, 'start': 1}],
1822 \ prop_list(1, {'bufnr': winbufnr(1), 'end_lnum': 4}))
1823 wincmd w
1824
1825 #" get text properties after clearing all the properties
1826 call prop_clear(1, line('$'))
1827 call assert_equal([], prop_list(1, {'end_lnum': 10}))
1828
1829 call prop_add(2, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1830 call prop_add(2, 4, {'type': 'two', 'id': 10, 'end_col': 6})
1831 call prop_add(2, 4, {'type': 'three', 'id': 15, 'end_col': 6})
1832 #" get text properties with a list of types
1833 call assert_equal([
1834 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
1835 \ 'type': 'two', 'length': 2, 'start': 1},
1836 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1837 \ 'type': 'one', 'length': 2, 'start': 1}],
1838 \ prop_list(2, {'types': ['one', 'two']}))
1839 call assert_equal([
1840 \ {'id': 15, 'col': 4, 'type_bufnr': 0, 'end': 1,
1841 \ 'type': 'three', 'length': 2, 'start': 1},
1842 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1843 \ 'type': 'one', 'length': 2, 'start': 1}],
1844 \ prop_list(2, {'types': ['one', 'three']}))
1845 #" get text properties with a list of identifiers
1846 call assert_equal([
1847 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
1848 \ 'type': 'two', 'length': 2, 'start': 1},
1849 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1850 \ 'type': 'one', 'length': 2, 'start': 1}],
1851 \ prop_list(2, {'ids': [5, 10, 20]}))
1852 call prop_clear(1, line('$'))
1853 call assert_equal([], prop_list(2, {'types': ['one', 'two']}))
1854 call assert_equal([], prop_list(2, {'ids': [5, 10, 20]}))
1855
1856 #" get text properties from a hidden buffer
1857 edit! Xaaa
1858 call setline(1, repeat([repeat('b', 60)], 10))
1859 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1860 call prop_add(4, 8, {'type': 'two', 'id': 10, 'end_col': 10})
1861 VAR bnr = bufnr()
1862 hide edit Xbbb
1863 call assert_equal([
1864 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1865 \ 'type': 'one', 'length': 2, 'start': 1},
1866 \ {'lnum': 4, 'id': 10, 'col': 8, 'type_bufnr': 0, 'end': 1,
1867 \ 'type': 'two', 'length': 2, 'start': 1}],
1868 \ prop_list(1, {'bufnr': bnr,
1869 \ 'types': ['one', 'two'], 'ids': [5, 10], 'end_lnum': -1}))
1870 #" get text properties from an unloaded buffer
1871 bunload! Xaaa
1872 call assert_equal([], prop_list(1, {'bufnr': bnr, 'end_lnum': -1}))
1873
Bram Moolenaar62aec932022-01-29 21:45:34 +00001874 call g:DeletePropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001875 :%bw!
1876 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001877 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001878endfunc
Bram Moolenaar23999d72020-12-23 14:36:00 +01001879
LemonBoy9bd3ce22022-04-18 21:54:02 +01001880func Test_prop_find_prev_on_same_line()
1881 new
1882
1883 call setline(1, 'the quikc bronw fox jumsp over the layz dog')
1884 call prop_type_add('misspell', #{highlight: 'ErrorMsg'})
1885 for col in [8, 14, 24, 38]
1886 call prop_add(1, col, #{type: 'misspell', length: 2})
1887 endfor
1888
1889 call cursor(1,18)
1890 let expected = [
1891 \ #{lnum: 1, id: 0, col: 14, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1},
1892 \ #{lnum: 1, id: 0, col: 24, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1}
1893 \ ]
1894
1895 let result = prop_find(#{type: 'misspell'}, 'b')
1896 call assert_equal(expected[0], result)
1897 let result = prop_find(#{type: 'misspell'}, 'f')
1898 call assert_equal(expected[1], result)
1899
1900 call prop_type_delete('misspell')
1901 bwipe!
1902endfunc
1903
LemonBoyb7a70122022-05-13 12:41:50 +01001904func Test_prop_spell()
1905 new
1906 set spell
1907 call AddPropTypes()
1908
1909 call setline(1, ["helo world", "helo helo helo"])
1910 call prop_add(1, 1, #{type: 'one', length: 4})
1911 call prop_add(1, 6, #{type: 'two', length: 5})
1912 call prop_add(2, 1, #{type: 'three', length: 4})
1913 call prop_add(2, 6, #{type: 'three', length: 4})
1914 call prop_add(2, 11, #{type: 'three', length: 4})
1915
1916 " The first prop over 'helo' increases its length after the word is corrected
1917 " to 'Hello', the second one is shifted to the right.
1918 let expected = [
1919 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
1920 \ 'length': 5, 'start': 1},
1921 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'two',
1922 \ 'length': 5, 'start': 1}
1923 \ ]
1924 call feedkeys("z=1\<CR>", 'xt')
1925
1926 call assert_equal('Hello world', getline(1))
1927 call assert_equal(expected, prop_list(1))
1928
1929 " Repeat the replacement done by z=
1930 spellrepall
1931
1932 let expected = [
1933 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'three',
1934 \ 'length': 5, 'start': 1},
1935 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'three',
1936 \ 'length': 5, 'start': 1},
1937 \ {'id': 0, 'col': 13, 'type_bufnr': 0, 'end': 1, 'type': 'three',
1938 \ 'length': 5, 'start': 1}
1939 \ ]
1940 call assert_equal('Hello Hello Hello', getline(2))
1941 call assert_equal(expected, prop_list(2))
1942
1943 call DeletePropTypes()
1944 set spell&
1945 bwipe!
1946endfunc
1947
LemonBoy4b936742022-05-13 21:56:28 +01001948func Test_prop_shift_block()
1949 new
1950 call AddPropTypes()
1951
1952 call setline(1, ['some highlighted text']->repeat(2))
1953 call prop_add(1, 10, #{type: 'one', length: 11})
1954 call prop_add(2, 10, #{type: 'two', length: 11})
1955
1956 call cursor(1, 1)
1957 call feedkeys("5l\<c-v>>", 'nxt')
1958 call cursor(2, 1)
1959 call feedkeys("5l\<c-v><", 'nxt')
1960
1961 let expected = [
1962 \ {'lnum': 1, 'id': 0, 'col': 8, 'type_bufnr': 0, 'end': 1, 'type': 'one',
1963 \ 'length': 11, 'start' : 1},
1964 \ {'lnum': 2, 'id': 0, 'col': 6, 'type_bufnr': 0, 'end': 1, 'type': 'two',
1965 \ 'length': 11, 'start' : 1}
1966 \ ]
1967 call assert_equal(expected, prop_list(1, #{end_lnum: 2}))
1968
1969 call DeletePropTypes()
1970 bwipe!
1971endfunc
LemonBoyb7a70122022-05-13 12:41:50 +01001972
LemonBoy698cb4c2022-05-14 18:10:15 +01001973func Test_prop_insert_multiline()
1974 new
1975 call AddPropTypes()
1976
1977 call setline(1, ['foobar', 'barbaz'])
1978 call prop_add(1, 4, #{end_lnum: 2, end_col: 4, type: 'one'})
1979
1980 call feedkeys("1Goquxqux\<Esc>", 'nxt')
1981 call feedkeys("2GOquxqux\<Esc>", 'nxt')
1982
1983 let lines =<< trim END
1984 foobar
1985 quxqux
1986 quxqux
1987 barbaz
1988 END
1989 call assert_equal(lines, getline(1, '$'))
1990 let expected = [
1991 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 0, 'type': 'one',
1992 \ 'length': 4 ,'start': 1},
1993 \ {'lnum': 2, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
1994 \ 'length': 7, 'start': 0},
1995 \ {'lnum': 3, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
1996 \ 'length': 7, 'start': 0},
1997 \ {'lnum': 4, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
1998 \ 'length': 3, 'start': 0}
1999 \ ]
2000 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
2001
2002 call DeletePropTypes()
2003 bwipe!
2004endfunc
2005
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002006" vim: shiftwidth=2 sts=2 expandtab