blob: c6f6739d9d75509dc66a719c67d2bc62e4581bd9 [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
LemonBoy0d534d92022-05-21 11:20:42 +0100576 " Replace three 1-byte chars with three 2-byte ones.
577 exe "normal 0l3rø"
578 call assert_equal('yøøøx xyyoxx', getline(1))
579 let expected[0].length += 3
580 let expected[1].col += 3
581 call assert_equal(expected, prop_list(1))
582
Bram Moolenaar196d1572019-01-02 23:47:18 +0100583 call DeletePropTypes()
584 bwipe!
585 set bs&
586endfunc
587
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200588func Test_prop_open_line()
589 new
590
591 " open new line, props stay in top line
592 let expected = SetupOneLine() " 'xonex xtwoxx'
593 exe "normal o\<Esc>"
594 call assert_equal('xonex xtwoxx', getline(1))
595 call assert_equal('', getline(2))
596 call assert_equal(expected, prop_list(1))
597 call DeletePropTypes()
598
599 " move all props to next line
600 let expected = SetupOneLine() " 'xonex xtwoxx'
601 exe "normal 0i\<CR>\<Esc>"
602 call assert_equal('', getline(1))
603 call assert_equal('xonex xtwoxx', getline(2))
604 call assert_equal(expected, prop_list(2))
605 call DeletePropTypes()
606
607 " split just before prop, move all props to next line
608 let expected = SetupOneLine() " 'xonex xtwoxx'
609 exe "normal 0li\<CR>\<Esc>"
610 call assert_equal('x', getline(1))
611 call assert_equal('onex xtwoxx', getline(2))
612 let expected[0].col -= 1
613 let expected[1].col -= 1
614 call assert_equal(expected, prop_list(2))
615 call DeletePropTypes()
616
617 " split inside prop, split first prop
618 let expected = SetupOneLine() " 'xonex xtwoxx'
619 exe "normal 0lli\<CR>\<Esc>"
620 call assert_equal('xo', getline(1))
621 call assert_equal('nex xtwoxx', getline(2))
622 let exp_first = [deepcopy(expected[0])]
623 let exp_first[0].length = 1
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200624 let exp_first[0].end = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200625 call assert_equal(exp_first, prop_list(1))
626 let expected[0].col = 1
627 let expected[0].length = 2
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200628 let expected[0].start = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200629 let expected[1].col -= 2
630 call assert_equal(expected, prop_list(2))
631 call DeletePropTypes()
632
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200633 " split just after first prop, second prop move to next line
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200634 let expected = SetupOneLine() " 'xonex xtwoxx'
635 exe "normal 0fea\<CR>\<Esc>"
636 call assert_equal('xone', getline(1))
637 call assert_equal('x xtwoxx', getline(2))
638 let exp_first = expected[0:0]
639 call assert_equal(exp_first, prop_list(1))
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200640 let expected = expected[1:1]
641 let expected[0].col -= 4
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200642 call assert_equal(expected, prop_list(2))
643 call DeletePropTypes()
644
LemonBoy788c06a2022-05-14 18:48:05 +0100645 " split at the space character with 'ai' active, the leading space is removed
646 " in the second line and the prop is shifted accordingly.
647 let expected = SetupOneLine() " 'xonex xtwoxx'
648 set ai
649 exe "normal 6|i\<CR>\<Esc>"
650 call assert_equal('xonex', getline(1))
651 call assert_equal('xtwoxx', getline(2))
652 let expected[1].col -= 6
653 call assert_equal(expected, prop_list(1) + prop_list(2))
654 set ai&
655 call DeletePropTypes()
656
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200657 bwipe!
658 set bs&
659endfunc
660
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100661func Test_prop_clear()
662 new
663 call AddPropTypes()
664 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100665 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100666
Bram Moolenaara5a78822019-09-04 21:57:18 +0200667 eval 1->prop_clear()
668 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100669
670 call DeletePropTypes()
671 bwipe!
672endfunc
673
674func Test_prop_clear_buf()
675 new
676 call AddPropTypes()
677 call SetupPropsInFirstLine()
678 let bufnr = bufnr('')
679 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100680 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100681
682 call prop_clear(1, 1, {'bufnr': bufnr})
683 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
684
685 wincmd w
686 call DeletePropTypes()
687 bwipe!
688endfunc
689
Bram Moolenaar21b50382019-01-04 18:07:24 +0100690func Test_prop_setline()
691 new
692 call AddPropTypes()
693 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100694 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100695
696 call setline(1, 'foobar')
697 call assert_equal([], prop_list(1))
698
699 call DeletePropTypes()
700 bwipe!
701endfunc
702
703func Test_prop_setbufline()
704 new
705 call AddPropTypes()
706 call SetupPropsInFirstLine()
707 let bufnr = bufnr('')
708 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100709 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100710
711 call setbufline(bufnr, 1, 'foobar')
712 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
713
714 wincmd w
715 call DeletePropTypes()
716 bwipe!
717endfunc
718
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100719func Test_prop_substitute()
720 new
721 " Set first line to 'one two three'
722 call AddPropTypes()
723 call SetupPropsInFirstLine()
724 let expected_props = Get_expected_props()
725 call assert_equal(expected_props, prop_list(1))
726
727 " Change "n" in "one" to XX: 'oXXe two three'
728 s/n/XX/
729 let expected_props[0].length += 1
730 let expected_props[1].length += 1
731 let expected_props[2].col += 1
732 let expected_props[3].col += 1
733 call assert_equal(expected_props, prop_list(1))
734
735 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
736 s/t//g
737 let expected_props[0].length -= 2
738 let expected_props[2].length -= 1
739 let expected_props[3].length -= 1
740 let expected_props[3].col -= 1
741 call assert_equal(expected_props, prop_list(1))
742
743 " Split the line by changing w to line break: 'oXXe ', 'o hree'
744 " The long prop is split and spans both lines.
745 " The props on "two" and "three" move to the next line.
746 s/w/\r/
747 let new_props = [
748 \ copy(expected_props[0]),
749 \ copy(expected_props[2]),
750 \ copy(expected_props[3]),
751 \ ]
752 let expected_props[0].length = 5
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200753 let expected_props[0].end = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100754 unlet expected_props[3]
755 unlet expected_props[2]
756 call assert_equal(expected_props, prop_list(1))
757
758 let new_props[0].length = 6
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200759 let new_props[0].start = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100760 let new_props[1].col = 1
761 let new_props[1].length = 1
762 let new_props[2].col = 3
763 call assert_equal(new_props, prop_list(2))
764
765 call DeletePropTypes()
766 bwipe!
767endfunc
768
Bram Moolenaar663bc892019-01-08 23:07:24 +0100769func Test_prop_change_indent()
770 call prop_type_add('comment', {'highlight': 'Directory'})
771 new
772 call setline(1, [' xxx', 'yyyyy'])
773 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200774 let expect = #{type_bufnr: 0, col: 2, length: 2, type: 'comment', start: 1, end: 1, id: 0}
Bram Moolenaar663bc892019-01-08 23:07:24 +0100775 call assert_equal([expect], prop_list(2))
776
777 set shiftwidth=3
778 normal 2G>>
779 call assert_equal(' yyyyy', getline(2))
780 let expect.col += 3
781 call assert_equal([expect], prop_list(2))
782
783 normal 2G==
784 call assert_equal(' yyyyy', getline(2))
785 let expect.col = 6
786 call assert_equal([expect], prop_list(2))
787
788 call prop_clear(2)
789 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
790 let expect.col = 2
791 let expect.length = 5
792 call assert_equal([expect], prop_list(2))
793
794 normal 2G<<
795 call assert_equal(' yyyyy', getline(2))
796 let expect.length = 2
797 call assert_equal([expect], prop_list(2))
798
799 set shiftwidth&
800 call prop_type_delete('comment')
801endfunc
802
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100803" Setup a three line prop in lines 2 - 4.
804" Add short props in line 1 and 5.
805func Setup_three_line_prop()
806 new
807 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
808 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
809 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
810 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
811endfunc
812
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100813func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200814 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100815 new
816 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
817
818 " start halfway line 1, end halfway line 3
819 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200820 let expect1 = #{type_bufnr: 0, col: 3, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100821 call assert_equal([expect1], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200822 let expect2 = #{type_bufnr: 0, col: 1, length: 10, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100823 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200824 let expect3 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100825 call assert_equal([expect3], prop_list(3))
826 call prop_clear(1, 3)
827
828 " include all three lines
829 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
830 let expect1.col = 1
831 let expect1.length = 8
832 call assert_equal([expect1], prop_list(1))
833 call assert_equal([expect2], prop_list(2))
834 let expect3.length = 9
835 call assert_equal([expect3], prop_list(3))
836 call prop_clear(1, 3)
837
838 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100839
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100840 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100841 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200842 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 +0100843 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200844 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100845 call assert_equal([expect2], prop_list(2))
846 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100847 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200848 let expect2 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100849 call assert_equal([expect2], prop_list(2))
850 bwipe!
851
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100852 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100853 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200854 let expect3 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100855 call assert_equal([expect3], prop_list(3))
Martin Tournoije2390c72021-07-28 13:30:16 +0200856 let expect4 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100857 call assert_equal([expect4], prop_list(4))
858 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100859 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100860 call assert_equal([expect3], prop_list(3))
861 call assert_equal([expect_short], prop_list(4))
862 bwipe!
863
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100864 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100865 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200866 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100867 call assert_equal([expect2], prop_list(2))
868 call append(2, "new line")
869 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200870 let expect3 = #{type_bufnr: 0, col: 1, length: 9, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100871 call assert_equal([expect3], prop_list(3))
872 bwipe!
873
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100874 call prop_type_delete('comment')
875endfunc
876
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100877func Test_prop_line2byte()
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100878 call prop_type_add('comment', {'highlight': 'Directory'})
879 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100880 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100881 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100882 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100883 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100884 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100885 bwipe!
Bram Moolenaar14c75302021-08-15 14:28:40 +0200886
887 new
Bram Moolenaara401bba2021-08-15 15:04:41 +0200888 setlocal ff=unix
Bram Moolenaar14c75302021-08-15 14:28:40 +0200889 call setline(1, range(500))
890 call assert_equal(1491, line2byte(401))
891 call prop_add(2, 1, {'type': 'comment'})
892 call prop_add(222, 1, {'type': 'comment'})
893 call assert_equal(1491, line2byte(401))
894 call prop_remove({'type': 'comment'})
895 call assert_equal(1491, line2byte(401))
896 bwipe!
897
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +0200898 new
Bram Moolenaar49b93042021-08-25 17:02:00 +0200899 setlocal ff=unix
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +0200900 call setline(1, range(520))
901 call assert_equal(1491, line2byte(401))
902 call prop_add(2, 1, {'type': 'comment'})
903 call assert_equal(1491, line2byte(401))
904 2delete
905 call assert_equal(1489, line2byte(400))
906 bwipe!
907
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100908 call prop_type_delete('comment')
909endfunc
910
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100911func Test_prop_byte2line()
912 new
913 set ff=unix
914 call setline(1, ['one one', 'two two', 'three three', 'four four', 'five'])
915 call assert_equal(4, byte2line(line2byte(4)))
916 call assert_equal(5, byte2line(line2byte(5)))
917
918 call prop_type_add('prop', {'highlight': 'Directory'})
919 call prop_add(3, 1, {'length': 5, 'type': 'prop'})
920 call assert_equal(4, byte2line(line2byte(4)))
921 call assert_equal(5, byte2line(line2byte(5)))
922
923 bwipe!
924 call prop_type_delete('prop')
925endfunc
926
Bram Moolenaar59ff6402021-01-30 17:16:28 +0100927func Test_prop_goto_byte()
928 new
929 call setline(1, '')
930 call setline(2, 'two three')
931 call setline(3, '')
932 call setline(4, 'four five')
933
934 call prop_type_add('testprop', {'highlight': 'Directory'})
935 call search('^two')
936 call prop_add(line('.'), col('.'), {
937 \ 'length': len('two'),
938 \ 'type': 'testprop'
939 \ })
940
941 call search('two \zsthree')
942 let expected_pos = line2byte(line('.')) + col('.') - 1
943 exe expected_pos .. 'goto'
944 let actual_pos = line2byte(line('.')) + col('.') - 1
945 eval actual_pos->assert_equal(expected_pos)
946
947 call search('four \zsfive')
948 let expected_pos = line2byte(line('.')) + col('.') - 1
949 exe expected_pos .. 'goto'
950 let actual_pos = line2byte(line('.')) + col('.') - 1
951 eval actual_pos->assert_equal(expected_pos)
952
953 call prop_type_delete('testprop')
954 bwipe!
955endfunc
956
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100957func Test_prop_undo()
958 new
959 call prop_type_add('comment', {'highlight': 'Directory'})
960 call setline(1, ['oneone', 'twotwo', 'three'])
961 " Set 'undolevels' to break changes into undo-able pieces.
962 set ul&
963
964 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200965 let expected = [#{type_bufnr: 0, col: 3, length: 2, id: 0, type: 'comment', start: 1, end: 1}]
Bram Moolenaar7f1664e2019-01-04 17:21:24 +0100966 call assert_equal(expected, prop_list(1))
967
968 " Insert a character, then undo.
969 exe "normal 0lllix\<Esc>"
970 set ul&
971 let expected[0].length = 3
972 call assert_equal(expected, prop_list(1))
973 undo
974 let expected[0].length = 2
975 call assert_equal(expected, prop_list(1))
976
977 " Delete a character, then undo
978 exe "normal 0lllx"
979 set ul&
980 let expected[0].length = 1
981 call assert_equal(expected, prop_list(1))
982 undo
983 let expected[0].length = 2
984 call assert_equal(expected, prop_list(1))
985
986 " Delete the line, then undo
987 1d
988 set ul&
989 call assert_equal([], prop_list(1))
990 undo
991 call assert_equal(expected, prop_list(1))
992
993 " Insert a character, delete two characters, then undo with "U"
994 exe "normal 0lllix\<Esc>"
995 set ul&
996 let expected[0].length = 3
997 call assert_equal(expected, prop_list(1))
998 exe "normal 0lllxx"
999 set ul&
1000 let expected[0].length = 1
1001 call assert_equal(expected, prop_list(1))
1002 normal U
1003 let expected[0].length = 2
1004 call assert_equal(expected, prop_list(1))
1005
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001006 " substitute a word, then undo
1007 call setline(1, 'the number 123 is highlighted.')
1008 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001009 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001010 call assert_equal(expected, prop_list(1))
1011 set ul&
1012 1s/number/foo
1013 let expected[0].col = 9
1014 call assert_equal(expected, prop_list(1))
1015 undo
1016 let expected[0].col = 12
1017 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001018 call prop_clear(1)
1019
1020 " substitute with backslash
1021 call setline(1, 'the number 123 is highlighted.')
1022 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001023 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001024 call assert_equal(expected, prop_list(1))
1025 1s/the/\The
1026 call assert_equal(expected, prop_list(1))
1027 1s/^/\\
1028 let expected[0].col += 1
1029 call assert_equal(expected, prop_list(1))
1030 1s/^/\~
1031 let expected[0].col += 1
1032 call assert_equal(expected, prop_list(1))
1033 1s/123/12\\3
1034 let expected[0].length += 1
1035 call assert_equal(expected, prop_list(1))
1036 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001037
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001038 bwipe!
1039 call prop_type_delete('comment')
1040endfunc
1041
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001042func Test_prop_delete_text()
1043 new
1044 call prop_type_add('comment', {'highlight': 'Directory'})
1045 call setline(1, ['oneone', 'twotwo', 'three'])
1046
1047 " zero length property
1048 call prop_add(1, 3, {'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001049 let expected = [#{type_bufnr: 0, col: 3, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001050 call assert_equal(expected, prop_list(1))
1051
1052 " delete one char moves the property
1053 normal! x
Martin Tournoije2390c72021-07-28 13:30:16 +02001054 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001055 call assert_equal(expected, prop_list(1))
1056
1057 " delete char of the property has no effect
1058 normal! lx
Martin Tournoije2390c72021-07-28 13:30:16 +02001059 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001060 call assert_equal(expected, prop_list(1))
1061
1062 " delete more chars moves property to first column, is not deleted
1063 normal! 0xxxx
Martin Tournoije2390c72021-07-28 13:30:16 +02001064 let expected = [#{type_bufnr: 0, col: 1, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001065 call assert_equal(expected, prop_list(1))
1066
1067 bwipe!
1068 call prop_type_delete('comment')
1069endfunc
1070
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001071" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +02001072func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +02001073 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +01001074 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +02001075 if g:orig_encoding != 'utf-8'
1076 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001077 endif
1078 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +02001079 \ "call setline(1, ["
1080 \ .. "'One two',"
1081 \ .. "'Numbér 123 änd thœn 4¾7.',"
1082 \ .. "'--aa--bb--cc--dd--',"
1083 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001084 \ .. "'first line',"
1085 \ .. "' second line ',"
1086 \ .. "'third line',"
1087 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +02001088 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001089 \ "hi NumberProp ctermfg=blue",
1090 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001091 \ "hi BackgroundProp ctermbg=lightgrey",
1092 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001093 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +02001094 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
1095 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001096 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
1097 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
1098 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001099 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
1100 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
1101 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +01001102 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001103 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001104 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
1105 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001106 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
1107 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
1108 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
1109 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001110 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
1111 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001112 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001113 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
1114 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
1115 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
1116 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +02001117 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001118 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001119 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001120 \ "syn match Comment '//.*'",
1121 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001122 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01001123 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001124 \ "normal 6G0i\<BS>\<Esc>",
1125 \ "normal 3J",
1126 \ "normal 3G",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001127 \], 'XtestProp')
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001128 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001129 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +01001130
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001131 " clean up
1132 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +01001133 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001134endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +02001135
1136func RunTestVisualBlock(width, dump)
1137 call writefile([
1138 \ "call setline(1, ["
1139 \ .. "'xxxxxxxxx 123 x',"
1140 \ .. "'xxxxxxxx 123 x',"
1141 \ .. "'xxxxxxx 123 x',"
1142 \ .. "'xxxxxx 123 x',"
1143 \ .. "'xxxxx 123 x',"
1144 \ .. "'xxxx 123 xx',"
1145 \ .. "'xxx 123 xxx',"
1146 \ .. "'xx 123 xxxx',"
1147 \ .. "'x 123 xxxxx',"
1148 \ .. "' 123 xxxxxx',"
1149 \ .. "])",
1150 \ "hi SearchProp ctermbg=yellow",
1151 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
1152 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
1153 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
1154 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
1155 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
1156 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
1157 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
1158 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
1159 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
1160 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
1161 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
1162 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
1163 \], 'XtestPropVis')
1164 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
1165 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
1166
1167 " clean up
1168 call StopVimInTerminal(buf)
1169 call delete('XtestPropVis')
1170endfunc
1171
1172" screenshot test with Visual block mode operations
1173func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +02001174 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +02001175
1176 " Delete two columns while text props are three chars wide.
1177 call RunTestVisualBlock(2, '01')
1178
1179 " Same, but delete four columns
1180 call RunTestVisualBlock(4, '02')
1181endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +02001182
Bram Moolenaara956bf62019-06-19 17:34:24 +02001183func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +02001184 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +02001185
Bram Moolenaara956bf62019-06-19 17:34:24 +02001186 let lines =<< trim END
1187 call setline(1, [
1188 \ "\txxx",
1189 \ "x\txxx",
1190 \ ])
1191 hi SearchProp ctermbg=yellow
1192 call prop_type_add('search', {'highlight': 'SearchProp'})
1193 call prop_add(1, 2, {'length': 3, 'type': 'search'})
1194 call prop_add(2, 3, {'length': 3, 'type': 'search'})
1195 END
1196 call writefile(lines, 'XtestPropTab')
1197 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
1198 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
1199
1200 " clean up
1201 call StopVimInTerminal(buf)
1202 call delete('XtestPropTab')
1203endfunc
1204
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001205func Test_textprop_nowrap_scrolled()
1206 CheckScreendump
1207
1208 let lines =<< trim END
1209 vim9script
1210 set nowrap
1211 setline(1, 'The number 123 is smaller than 4567.' .. repeat('X', &columns))
1212 prop_type_add('number', {'highlight': 'ErrorMsg'})
1213 prop_add(1, 12, {'length': 3, 'type': 'number'})
1214 prop_add(1, 32, {'length': 4, 'type': 'number'})
1215 feedkeys('gg20zl', 'nxt')
1216 END
1217 call writefile(lines, 'XtestNowrap')
1218 let buf = RunVimInTerminal('-S XtestNowrap', {'rows': 6})
1219 call VerifyScreenDump(buf, 'Test_textprop_nowrap_01', {})
1220
1221 call term_sendkeys(buf, "$")
1222 call VerifyScreenDump(buf, 'Test_textprop_nowrap_02', {})
1223
1224 " clean up
1225 call StopVimInTerminal(buf)
1226 call delete('XtestNowrap')
1227endfunc
1228
Bram Moolenaar34390282019-10-16 14:38:26 +02001229func Test_textprop_with_syntax()
1230 CheckScreendump
1231
1232 let lines =<< trim END
1233 call setline(1, [
1234 \ "(abc)",
1235 \ ])
1236 syn match csParens "[()]" display
1237 hi! link csParens MatchParen
1238
1239 call prop_type_add('TPTitle', #{ highlight: 'Title' })
1240 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
1241 END
1242 call writefile(lines, 'XtestPropSyn')
1243 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
1244 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
1245
1246 " clean up
1247 call StopVimInTerminal(buf)
1248 call delete('XtestPropSyn')
1249endfunc
1250
Bram Moolenaard79eef22019-05-24 20:41:55 +02001251" Adding a text property to a new buffer should not fail
1252func Test_textprop_empty_buffer()
1253 call prop_type_add('comment', {'highlight': 'Search'})
1254 new
1255 call prop_add(1, 1, {'type': 'comment'})
1256 close
Bram Moolenaaradfde112019-05-25 22:11:45 +02001257 call prop_type_delete('comment')
1258endfunc
1259
Bram Moolenaard74af422019-06-28 21:38:00 +02001260" Adding a text property with invalid highlight should be ignored.
1261func Test_textprop_invalid_highlight()
1262 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
1263 new
1264 call setline(1, ['asdf','asdf'])
1265 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
1266 redraw
1267 bwipe!
1268 call prop_type_delete('dni')
1269endfunc
1270
Bram Moolenaaradfde112019-05-25 22:11:45 +02001271" Adding a text property to an empty buffer and then editing another
1272func Test_textprop_empty_buffer_next()
1273 call prop_type_add("xxx", {})
1274 call prop_add(1, 1, {"type": "xxx"})
1275 next X
1276 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +02001277endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001278
1279func Test_textprop_remove_from_buf()
1280 new
1281 let buf = bufnr('')
1282 call prop_type_add('one', {'bufnr': buf})
1283 call prop_add(1, 1, {'type': 'one', 'id': 234})
1284 file x
1285 edit y
1286 call prop_remove({'id': 234, 'bufnr': buf}, 1)
1287 call prop_type_delete('one', {'bufnr': buf})
1288 bwipe! x
1289 close
1290endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +02001291
1292func Test_textprop_in_unloaded_buf()
1293 edit Xaaa
1294 call setline(1, 'aaa')
1295 write
1296 edit Xbbb
1297 call setline(1, 'bbb')
1298 write
1299 let bnr = bufnr('')
1300 edit Xaaa
1301
1302 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
1303 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
1304 exe 'buf ' .. bnr
1305 call assert_equal('bbb', getline(1))
1306 call assert_equal(0, prop_list(1)->len())
1307
1308 bwipe! Xaaa
1309 bwipe! Xbbb
1310 cal delete('Xaaa')
1311 cal delete('Xbbb')
1312endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001313
1314func Test_proptype_substitute2()
1315 new
1316 " text_prop.vim
1317 call setline(1, [
1318 \ 'The num 123 is smaller than 4567.',
1319 \ '123 The number 123 is smaller than 4567.',
1320 \ '123 The number 123 is smaller than 4567.'])
1321
1322 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1323
1324 call prop_add(1, 12, {'length': 3, 'type': 'number'})
1325 call prop_add(2, 1, {'length': 3, 'type': 'number'})
1326 call prop_add(3, 36, {'length': 4, 'type': 'number'})
1327 set ul&
Martin Tournoije2390c72021-07-28 13:30:16 +02001328 let expected = [
1329 \ #{type_bufnr: 0, id: 0, col: 13, end: 1, type: 'number', length: 3, start: 1},
1330 \ #{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'number', length: 3, start: 1},
1331 \ #{type_bufnr: 0, id: 0, col: 50, end: 1, type: 'number', length: 4, start: 1}]
1332
1333 " TODO
1334 return
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001335 " Add some text in between
1336 %s/\s\+/ /g
Martin Tournoije2390c72021-07-28 13:30:16 +02001337 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001338
1339 " remove some text
1340 :1s/[a-z]\{3\}//g
1341 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
1342 call assert_equal(expected, prop_list(1))
1343 bwipe!
1344endfunc
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001345
Bram Moolenaar8902b312020-09-20 21:04:35 +02001346" This was causing property corruption.
1347func Test_proptype_substitute3()
1348 new
1349 call setline(1, ['abcxxx', 'def'])
1350 call prop_type_add("test", {"highlight": "Search"})
1351 call prop_add(1, 2, {"end_lnum": 2, "end_col": 2, "type": "test"})
1352 %s/x\+$//
1353 redraw
1354
1355 call prop_type_delete('test')
1356 bwipe!
1357endfunc
1358
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001359func SaveOptions()
1360 let d = #{tabstop: &tabstop,
1361 \ softtabstop: &softtabstop,
1362 \ shiftwidth: &shiftwidth,
1363 \ expandtab: &expandtab,
1364 \ foldmethod: '"' .. &foldmethod .. '"',
1365 \ }
1366 return d
1367endfunc
1368
1369func RestoreOptions(dict)
1370 for name in keys(a:dict)
1371 exe 'let &' .. name .. ' = ' .. a:dict[name]
1372 endfor
1373endfunc
1374
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001375func Test_textprop_noexpandtab()
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001376 new
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001377 let save_dict = SaveOptions()
1378
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001379 set tabstop=8
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001380 set softtabstop=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001381 set shiftwidth=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001382 set noexpandtab
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001383 set foldmethod=marker
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001384
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001385 call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx")
1386 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1387 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1388 call feedkeys("0i\<tab>", "tx")
1389 call prop_remove({'type': 'test'})
1390 call prop_add(1, 2, {'end_col': 3, 'type': 'test'})
1391 call feedkeys("A\<left>\<tab>", "tx")
1392 call prop_remove({'type': 'test'})
1393 try
1394 " It is correct that this does not pass
1395 call prop_add(1, 6, {'end_col': 7, 'type': 'test'})
1396 " Has already collapsed here, start_col:6 does not result in an error
1397 call feedkeys("A\<left>\<tab>", "tx")
1398 catch /^Vim\%((\a\+)\)\=:E964/
1399 endtry
1400 call prop_remove({'type': 'test'})
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001401 call prop_type_delete('test')
1402
1403 call RestoreOptions(save_dict)
1404 bwipe!
1405endfunc
1406
1407func Test_textprop_noexpandtab_redraw()
1408 new
1409 let save_dict = SaveOptions()
1410
1411 set tabstop=8
1412 set softtabstop=4
1413 set shiftwidth=4
1414 set noexpandtab
1415 set foldmethod=marker
1416
1417 call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx")
1418 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1419 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1420 call feedkeys("0i\<tab>", "tx")
1421 " Internally broken at the next line
1422 call feedkeys("A\<left>\<tab>", "tx")
1423 redraw
1424 " Index calculation failed internally on next line
1425 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1426 call prop_remove({'type': 'test', 'all': v:true})
1427 call prop_type_delete('test')
1428 call prop_type_delete('test')
1429
1430 call RestoreOptions(save_dict)
1431 bwipe!
1432endfunc
1433
1434func Test_textprop_ins_str()
1435 new
1436 call setline(1, 'just some text')
1437 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1438 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001439 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 +01001440
1441 call feedkeys("foi\<F8>\<Esc>", "tx")
1442 call assert_equal('just s<F8>ome text', getline(1))
Martin Tournoije2390c72021-07-28 13:30:16 +02001443 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 +01001444
1445 bwipe!
1446 call prop_remove({'type': 'test'})
1447 call prop_type_delete('test')
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001448endfunc
Bram Moolenaar66b98852020-03-11 19:15:52 +01001449
1450func Test_find_prop_later_in_line()
1451 new
1452 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1453 call setline(1, 'just some text')
1454 call prop_add(1, 1, {'length': 4, 'type': 'test'})
1455 call prop_add(1, 10, {'length': 3, 'type': 'test'})
1456
Martin Tournoije2390c72021-07-28 13:30:16 +02001457 call assert_equal(
1458 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 10, end: 1, type: 'test', length: 3, start: 1},
1459 \ prop_find(#{type: 'test', lnum: 1, col: 6}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001460
1461 bwipe!
1462 call prop_type_delete('test')
1463endfunc
1464
1465func Test_find_zerowidth_prop_sol()
1466 new
1467 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1468 call setline(1, 'just some text')
1469 call prop_add(1, 1, {'length': 0, 'type': 'test'})
1470
Martin Tournoije2390c72021-07-28 13:30:16 +02001471 call assert_equal(
1472 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 1, end: 1, type: 'test', length: 0, start: 1},
1473 \ prop_find(#{type: 'test', lnum: 1}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001474
1475 bwipe!
1476 call prop_type_delete('test')
1477endfunc
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001478
1479" Test for passing invalid arguments to prop_xxx() functions
1480func Test_prop_func_invalid_args()
1481 call assert_fails('call prop_clear(1, 2, [])', 'E715:')
1482 call assert_fails('call prop_clear(-1, 2)', 'E16:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001483 call assert_fails('call prop_find(test_null_dict())', 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001484 call assert_fails('call prop_find({"bufnr" : []})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001485 call assert_fails('call prop_find({})', 'E968:')
1486 call assert_fails('call prop_find({}, "x")', 'E474:')
1487 call assert_fails('call prop_find({"lnum" : -2})', 'E16:')
1488 call assert_fails('call prop_list(1, [])', 'E715:')
Bram Moolenaar9d489562020-07-30 20:08:50 +02001489 call assert_fails('call prop_list(-1, {})', 'E16:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001490 call assert_fails('call prop_remove([])', 'E474:')
1491 call assert_fails('call prop_remove({}, -2)', 'E16:')
1492 call assert_fails('call prop_remove({})', 'E968:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001493 call assert_fails('call prop_type_add([], {})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001494 call assert_fails("call prop_type_change('long', {'xyz' : 10})", 'E971:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001495 call assert_fails("call prop_type_delete([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001496 call assert_fails("call prop_type_delete('xyz', [])", 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001497 call assert_fails("call prop_type_get([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001498 call assert_fails("call prop_type_get('', [])", 'E474:')
1499 call assert_fails("call prop_type_list([])", 'E715:')
Bram Moolenaar3dc34742021-03-02 13:36:47 +01001500 call assert_fails("call prop_type_add('yyy', 'not_a_dict')", 'E715:')
1501 call assert_fails("call prop_add(1, 5, {'type':'missing_type', 'length':1})", 'E971:')
1502 call assert_fails("call prop_add(1, 5, {'type': ''})", 'E971:')
1503 call assert_fails('call prop_add(1, 1, 0)', 'E715:')
1504
1505 new
1506 call setline(1, ['first', 'second'])
1507 call prop_type_add('xxx', {})
1508
1509 call assert_fails("call prop_type_add('xxx', {})", 'E969:')
1510 call assert_fails("call prop_add(2, 0, {'type': 'xxx'})", 'E964:')
1511 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':1})", 'E475:')
1512 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':3})", 'E966:')
1513 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'length':-1})", 'E475:')
1514 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_col':0})", 'E475:')
1515 call assert_fails("call prop_add(2, 3, {'length':1})", 'E965:')
1516
1517 call prop_type_delete('xxx')
1518 bwipe!
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001519endfunc
1520
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001521func Test_prop_split_join()
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001522 new
1523 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1524 call setline(1, 'just some text')
1525 call prop_add(1, 6, {'length': 4, 'type': 'test'})
1526
1527 " Split in middle of "some"
1528 execute "normal! 8|i\<CR>"
Martin Tournoije2390c72021-07-28 13:30:16 +02001529 call assert_equal(
1530 \ [#{type_bufnr: 0, id: 0, col: 6, end: 0, type: 'test', length: 2, start: 1}],
1531 \ prop_list(1))
1532 call assert_equal(
1533 \ [#{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'test', length: 2, start: 0}],
1534 \ prop_list(2))
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001535
1536 " Join the two lines back together
1537 normal! 1GJ
Martin Tournoije2390c72021-07-28 13:30:16 +02001538 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 +02001539
1540 bwipe!
1541 call prop_type_delete('test')
1542endfunc
1543
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001544func Test_prop_increment_decrement()
1545 new
1546 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1547 call setline(1, 'its 998 times')
1548 call prop_add(1, 5, {'length': 3, 'type': 'test'})
1549
1550 exe "normal! 0f9\<C-A>"
1551 eval getline(1)->assert_equal('its 999 times')
1552 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001553 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 3, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001554
1555 exe "normal! 0f9\<C-A>"
1556 eval getline(1)->assert_equal('its 1000 times')
1557 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001558 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 4, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001559
1560 bwipe!
1561 call prop_type_delete('test')
1562endfunc
1563
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001564func Test_prop_block_insert()
1565 new
1566 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1567 call setline(1, ['one ', 'two '])
1568 call prop_add(1, 1, {'length': 3, 'type': 'test'})
1569 call prop_add(2, 1, {'length': 3, 'type': 'test'})
1570
1571 " insert "xx" in the first column of both lines
1572 exe "normal! gg0\<C-V>jIxx\<Esc>"
1573 eval getline(1, 2)->assert_equal(['xxone ', 'xxtwo '])
Martin Tournoije2390c72021-07-28 13:30:16 +02001574 let expected = [#{type_bufnr: 0, id: 0, col: 3, end: 1, type: 'test', length: 3, start: 1}]
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001575 eval prop_list(1)->assert_equal(expected)
1576 eval prop_list(2)->assert_equal(expected)
1577
1578 " insert "yy" inside the text props to make them longer
1579 exe "normal! gg03l\<C-V>jIyy\<Esc>"
1580 eval getline(1, 2)->assert_equal(['xxoyyne ', 'xxtyywo '])
1581 let expected[0].length = 5
1582 eval prop_list(1)->assert_equal(expected)
1583 eval prop_list(2)->assert_equal(expected)
1584
1585 " insert "zz" after the text props, text props don't change
1586 exe "normal! gg07l\<C-V>jIzz\<Esc>"
1587 eval getline(1, 2)->assert_equal(['xxoyynezz ', 'xxtyywozz '])
1588 eval prop_list(1)->assert_equal(expected)
1589 eval prop_list(2)->assert_equal(expected)
1590
1591 bwipe!
1592 call prop_type_delete('test')
1593endfunc
1594
Bram Moolenaar23999d72020-12-23 14:36:00 +01001595" this was causing an ml_get error because w_botline was wrong
1596func Test_prop_one_line_window()
1597 enew
1598 call range(2)->setline(1)
1599 call prop_type_add('testprop', {})
1600 call prop_add(1, 1, {'type': 'testprop'})
1601 call popup_create('popup', {'textprop': 'testprop'})
1602 $
1603 new
1604 wincmd _
1605 call feedkeys("\r", 'xt')
1606 redraw
1607
1608 call popup_clear()
1609 call prop_type_delete('testprop')
1610 close
1611 bwipe!
1612endfunc
1613
Bram Moolenaarf05a1e52022-08-02 11:48:53 +01001614def Test_prop_column_zero_error()
1615 prop_type_add('proptype', {highlight: 'Search'})
1616 var caught = false
1617 try
1618 popup_create([{
1619 text: 'a',
1620 props: [{col: 0, length: 1, type: 'type'}],
1621 }], {})
1622 catch /E964:/
1623 caught = true
1624 endtry
1625 assert_true(caught)
1626
1627 popup_clear()
1628 prop_type_delete('proptype')
1629enddef
1630
Bram Moolenaar840f91f2021-05-26 22:32:10 +02001631" This was calling ml_append_int() and copy a text property from a previous
1632" line at the wrong moment. Exact text length matters.
1633def Test_prop_splits_data_block()
1634 new
1635 var lines: list<string> = [repeat('x', 35)]->repeat(41)
1636 + [repeat('!', 35)]
1637 + [repeat('x', 35)]->repeat(56)
1638 lines->setline(1)
1639 prop_type_add('someprop', {highlight: 'ErrorMsg'})
1640 prop_add(1, 27, {end_lnum: 1, end_col: 70, type: 'someprop'})
1641 prop_remove({type: 'someprop'}, 1)
1642 prop_add(35, 22, {end_lnum: 43, end_col: 43, type: 'someprop'})
1643 prop_remove({type: 'someprop'}, 35, 43)
1644 assert_equal([], prop_list(42))
1645
1646 bwipe!
1647 prop_type_delete('someprop')
1648enddef
1649
Bram Moolenaar4cd5c522021-06-27 13:04:00 +02001650" This was calling ml_delete_int() and try to change text properties.
1651def Test_prop_add_delete_line()
1652 new
1653 var a = 10
1654 var b = 20
1655 repeat([''], a)->append('$')
1656 prop_type_add('Test', {highlight: 'ErrorMsg'})
1657 for lnum in range(1, a)
1658 for col in range(1, b)
1659 prop_add(1, 1, {end_lnum: lnum, end_col: col, type: 'Test'})
1660 endfor
1661 endfor
1662
1663 # check deleting lines is OK
1664 :5del
1665 :1del
1666 :$del
1667
1668 prop_type_delete('Test')
1669 bwipe!
1670enddef
1671
Paul Ollis1bdc60e2022-05-15 22:24:55 +01001672" This test is to detect a regression related to #10430. It is not an attempt
1673" fully cover deleting lines in the presence of multi-line properties.
1674def Test_delete_line_within_multiline_prop()
1675 new
1676 setline(1, '# Top.')
1677 append(1, ['some_text = """', 'A string.', '"""', '# Bottom.'])
1678 prop_type_add('Identifier', {'highlight': 'ModeMsg', 'priority': 0, 'combine': 0, 'start_incl': 0, 'end_incl': 0})
1679 prop_type_add('String', {'highlight': 'MoreMsg', 'priority': 0, 'combine': 0, 'start_incl': 0, 'end_incl': 0})
1680 prop_add(2, 1, {'type': 'Identifier', 'end_lnum': 2, 'end_col': 9})
1681 prop_add(2, 13, {'type': 'String', 'end_lnum': 4, 'end_col': 4})
1682
1683 # The property for line 3 should extend into the previous and next lines.
1684 var props = prop_list(3)
1685 var prop = props[0]
1686 assert_equal(1, len(props))
1687 assert_equal(0, prop['start'])
1688 assert_equal(0, prop['end'])
1689
1690 # This deletion should run without raising an exception.
1691 try
1692 :2 del
1693 catch
1694 assert_report('Line delete should have workd, but it raised an error.')
1695 endtry
1696
1697 # The property for line 2 (was 3) should no longer extend into the previous
1698 # line.
1699 props = prop_list(2)
1700 prop = props[0]
1701 assert_equal(1, len(props))
1702 assert_equal(1, prop['start'], 'Property was not changed to start within the line.')
1703
1704 # This deletion should run without raising an exception.
1705 try
1706 :3 del
1707 catch
1708 assert_report('Line delete should have workd, but it raised an error.')
1709 endtry
1710
1711 # The property for line 2 (originally 3) should no longer extend into the next
1712 # line.
1713 props = prop_list(2)
1714 prop = props[0]
1715 assert_equal(1, len(props))
1716 assert_equal(1, prop['end'], 'Property was not changed to end within the line.')
1717
1718 prop_type_delete('Identifier')
1719 prop_type_delete('String')
1720 bwip!
1721enddef
1722
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001723func Test_prop_in_linebreak()
1724 CheckRunVimInTerminal
1725
1726 let lines =<< trim END
1727 set breakindent linebreak breakat+=]
1728 call printf('%s]%s', repeat('x', 50), repeat('x', 70))->setline(1)
1729 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1730 call prop_add(1, 51, #{length: 1, type: 'test'})
1731 END
1732 call writefile(lines, 'XscriptPropLinebreak')
1733 let buf = RunVimInTerminal('-S XscriptPropLinebreak', #{rows: 10})
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001734 call VerifyScreenDump(buf, 'Test_prop_linebreak', {})
1735
1736 call StopVimInTerminal(buf)
1737 call delete('XscriptPropLinebreak')
1738endfunc
1739
Bram Moolenaar42eba042021-11-30 20:22:49 +00001740func Test_prop_after_tab()
1741 CheckRunVimInTerminal
1742
1743 let lines =<< trim END
1744 set breakindent linebreak breakat+=]
1745 call setline(1, "\t[xxx]")
1746 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1747 call prop_add(1, 2, #{length: 1, type: 'test'})
1748 END
1749 call writefile(lines, 'XscriptPropAfterTab')
1750 let buf = RunVimInTerminal('-S XscriptPropAfterTab', #{rows: 10})
Bram Moolenaar42eba042021-11-30 20:22:49 +00001751 call VerifyScreenDump(buf, 'Test_prop_after_tab', {})
1752
1753 call StopVimInTerminal(buf)
1754 call delete('XscriptPropAfterTab')
1755endfunc
1756
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001757func Test_prop_after_linebreak()
1758 CheckRunVimInTerminal
1759
1760 let lines =<< trim END
1761 set linebreak wrap
1762 call printf('%s+(%s)', 'x'->repeat(&columns / 2), 'x'->repeat(&columns / 2))->setline(1)
1763 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1764 call prop_add(1, (&columns / 2) + 2, #{length: 1, type: 'test'})
1765 END
1766 call writefile(lines, 'XscriptPropAfterLinebreak')
1767 let buf = RunVimInTerminal('-S XscriptPropAfterLinebreak', #{rows: 10})
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001768 call VerifyScreenDump(buf, 'Test_prop_after_linebreak', {})
1769
1770 call StopVimInTerminal(buf)
1771 call delete('XscriptPropAfterLinebreak')
1772endfunc
1773
Martin Tournoije2390c72021-07-28 13:30:16 +02001774" Buffer number of 0 should be ignored, as if the parameter wasn't passed.
1775def Test_prop_bufnr_zero()
1776 new
1777 try
1778 var bufnr = bufnr('')
1779 setline(1, 'hello')
1780 prop_type_add('bufnr-global', {highlight: 'ErrorMsg'})
1781 prop_type_add('bufnr-buffer', {highlight: 'StatusLine', bufnr: bufnr})
1782
1783 prop_add(1, 1, {type: 'bufnr-global', length: 1})
1784 prop_add(1, 2, {type: 'bufnr-buffer', length: 1})
1785
1786 var list = prop_list(1)
1787 assert_equal([
1788 {id: 0, col: 1, type_bufnr: 0, end: 1, type: 'bufnr-global', length: 1, start: 1},
1789 {id: 0, col: 2, type_bufnr: bufnr, end: 1, type: 'bufnr-buffer', length: 1, start: 1},
1790 ], list)
1791
1792 assert_equal(
1793 {highlight: 'ErrorMsg', end_incl: 0, start_incl: 0, priority: 0, combine: 1},
1794 prop_type_get('bufnr-global', {bufnr: list[0].type_bufnr}))
1795
1796 assert_equal(
1797 {highlight: 'StatusLine', end_incl: 0, start_incl: 0, priority: 0, bufnr: bufnr, combine: 1},
1798 prop_type_get('bufnr-buffer', {bufnr: list[1].type_bufnr}))
1799 finally
1800 bwipe!
1801 prop_type_delete('bufnr-global')
1802 endtry
1803enddef
1804
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001805" Tests for the prop_list() function
1806func Test_prop_list()
1807 let lines =<< trim END
1808 new
Bram Moolenaar62aec932022-01-29 21:45:34 +00001809 call g:AddPropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001810 call setline(1, repeat([repeat('a', 60)], 10))
1811 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1812 call prop_add(1, 5, {'type': 'two', 'id': 10, 'end_col': 7})
1813 call prop_add(3, 12, {'type': 'one', 'id': 20, 'end_col': 14})
1814 call prop_add(3, 13, {'type': 'two', 'id': 10, 'end_col': 15})
1815 call prop_add(5, 20, {'type': 'one', 'id': 10, 'end_col': 22})
1816 call prop_add(5, 21, {'type': 'two', 'id': 20, 'end_col': 23})
1817 call assert_equal([
1818 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1819 \ 'type': 'one', 'length': 2, 'start': 1},
1820 \ {'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
1821 \ 'type': 'two', 'length': 2, 'start': 1}], prop_list(1))
1822 #" text properties between a few lines
1823 call assert_equal([
1824 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1825 \ 'type': 'one', 'length': 2, 'start': 1},
1826 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
1827 \ 'type': 'two', 'length': 2, 'start': 1},
1828 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
1829 \ 'type': 'one', 'length': 2, 'start': 1},
1830 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
1831 \ 'type': 'two', 'length': 2, 'start': 1}],
1832 \ prop_list(2, {'end_lnum': 5}))
1833 #" text properties across all the lines
1834 call assert_equal([
1835 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1836 \ 'type': 'one', 'length': 2, 'start': 1},
1837 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1838 \ 'type': 'one', 'length': 2, 'start': 1},
1839 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
1840 \ 'type': 'one', 'length': 2, 'start': 1}],
1841 \ prop_list(1, {'types': ['one'], 'end_lnum': -1}))
1842 #" text properties with the specified identifier
1843 call assert_equal([
1844 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1845 \ 'type': 'one', 'length': 2, 'start': 1},
1846 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
1847 \ 'type': 'two', 'length': 2, 'start': 1}],
1848 \ prop_list(1, {'ids': [20], 'end_lnum': 10}))
1849 #" text properties of the specified type and id
1850 call assert_equal([
1851 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
1852 \ 'type': 'two', 'length': 2, 'start': 1},
1853 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
1854 \ 'type': 'two', 'length': 2, 'start': 1}],
1855 \ prop_list(1, {'types': ['two'], 'ids': [10], 'end_lnum': 20}))
1856 call assert_equal([], prop_list(1, {'ids': [40, 50], 'end_lnum': 10}))
1857 call assert_equal([], prop_list(6, {'end_lnum': 10}))
1858 call assert_equal([], prop_list(2, {'end_lnum': 2}))
1859 #" error cases
1860 call assert_fails("echo prop_list(1, {'end_lnum': -20})", 'E16:')
1861 call assert_fails("echo prop_list(4, {'end_lnum': 2})", 'E16:')
1862 call assert_fails("echo prop_list(1, {'end_lnum': '$'})", 'E889:')
1863 call assert_fails("echo prop_list(1, {'types': ['blue'], 'end_lnum': 10})",
1864 \ 'E971:')
1865 call assert_fails("echo prop_list(1, {'types': ['one', 'blue'],
1866 \ 'end_lnum': 10})", 'E971:')
1867 call assert_fails("echo prop_list(1, {'types': ['one', 10],
1868 \ 'end_lnum': 10})", 'E928:')
1869 call assert_fails("echo prop_list(1, {'types': ['']})", 'E971:')
1870 call assert_equal([], prop_list(2, {'types': []}))
1871 call assert_equal([], prop_list(2, {'types': test_null_list()}))
1872 call assert_fails("call prop_list(1, {'types': {}})", 'E714:')
1873 call assert_fails("call prop_list(1, {'types': 'one'})", 'E714:')
1874 call assert_equal([], prop_list(2, {'types': ['one'],
1875 \ 'ids': test_null_list()}))
1876 call assert_equal([], prop_list(2, {'types': ['one'], 'ids': []}))
1877 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': {}})",
1878 \ 'E714:')
1879 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': 10})",
1880 \ 'E714:')
1881 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [[]]})",
1882 \ 'E745:')
1883 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [10, []]})",
1884 \ 'E745:')
Martin Tournoije2390c72021-07-28 13:30:16 +02001885
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001886 #" get text properties from a non-current buffer
1887 wincmd w
1888 call assert_equal([
1889 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1890 \ 'type': 'one', 'length': 2, 'start': 1},
1891 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
1892 \ 'type': 'two', 'length': 2, 'start': 1},
1893 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
1894 \ 'type': 'one', 'length': 2, 'start': 1},
1895 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
1896 \ 'type': 'two', 'length': 2, 'start': 1}],
1897 \ prop_list(1, {'bufnr': winbufnr(1), 'end_lnum': 4}))
1898 wincmd w
1899
1900 #" get text properties after clearing all the properties
1901 call prop_clear(1, line('$'))
1902 call assert_equal([], prop_list(1, {'end_lnum': 10}))
1903
1904 call prop_add(2, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1905 call prop_add(2, 4, {'type': 'two', 'id': 10, 'end_col': 6})
1906 call prop_add(2, 4, {'type': 'three', 'id': 15, 'end_col': 6})
1907 #" get text properties with a list of types
1908 call assert_equal([
1909 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
1910 \ 'type': 'two', 'length': 2, 'start': 1},
1911 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1912 \ 'type': 'one', 'length': 2, 'start': 1}],
1913 \ prop_list(2, {'types': ['one', 'two']}))
1914 call assert_equal([
1915 \ {'id': 15, 'col': 4, 'type_bufnr': 0, 'end': 1,
1916 \ 'type': 'three', 'length': 2, 'start': 1},
1917 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1918 \ 'type': 'one', 'length': 2, 'start': 1}],
1919 \ prop_list(2, {'types': ['one', 'three']}))
1920 #" get text properties with a list of identifiers
1921 call assert_equal([
1922 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
1923 \ 'type': 'two', 'length': 2, 'start': 1},
1924 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1925 \ 'type': 'one', 'length': 2, 'start': 1}],
1926 \ prop_list(2, {'ids': [5, 10, 20]}))
1927 call prop_clear(1, line('$'))
1928 call assert_equal([], prop_list(2, {'types': ['one', 'two']}))
1929 call assert_equal([], prop_list(2, {'ids': [5, 10, 20]}))
1930
1931 #" get text properties from a hidden buffer
1932 edit! Xaaa
1933 call setline(1, repeat([repeat('b', 60)], 10))
1934 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1935 call prop_add(4, 8, {'type': 'two', 'id': 10, 'end_col': 10})
1936 VAR bnr = bufnr()
1937 hide edit Xbbb
1938 call assert_equal([
1939 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1940 \ 'type': 'one', 'length': 2, 'start': 1},
1941 \ {'lnum': 4, 'id': 10, 'col': 8, 'type_bufnr': 0, 'end': 1,
1942 \ 'type': 'two', 'length': 2, 'start': 1}],
1943 \ prop_list(1, {'bufnr': bnr,
1944 \ 'types': ['one', 'two'], 'ids': [5, 10], 'end_lnum': -1}))
1945 #" get text properties from an unloaded buffer
1946 bunload! Xaaa
1947 call assert_equal([], prop_list(1, {'bufnr': bnr, 'end_lnum': -1}))
1948
Bram Moolenaar62aec932022-01-29 21:45:34 +00001949 call g:DeletePropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001950 :%bw!
1951 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001952 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001953endfunc
Bram Moolenaar23999d72020-12-23 14:36:00 +01001954
LemonBoy9bd3ce22022-04-18 21:54:02 +01001955func Test_prop_find_prev_on_same_line()
1956 new
1957
1958 call setline(1, 'the quikc bronw fox jumsp over the layz dog')
1959 call prop_type_add('misspell', #{highlight: 'ErrorMsg'})
1960 for col in [8, 14, 24, 38]
1961 call prop_add(1, col, #{type: 'misspell', length: 2})
1962 endfor
1963
1964 call cursor(1,18)
1965 let expected = [
1966 \ #{lnum: 1, id: 0, col: 14, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1},
1967 \ #{lnum: 1, id: 0, col: 24, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1}
1968 \ ]
1969
1970 let result = prop_find(#{type: 'misspell'}, 'b')
1971 call assert_equal(expected[0], result)
1972 let result = prop_find(#{type: 'misspell'}, 'f')
1973 call assert_equal(expected[1], result)
1974
1975 call prop_type_delete('misspell')
1976 bwipe!
1977endfunc
1978
LemonBoyb7a70122022-05-13 12:41:50 +01001979func Test_prop_spell()
1980 new
1981 set spell
1982 call AddPropTypes()
1983
1984 call setline(1, ["helo world", "helo helo helo"])
1985 call prop_add(1, 1, #{type: 'one', length: 4})
1986 call prop_add(1, 6, #{type: 'two', length: 5})
1987 call prop_add(2, 1, #{type: 'three', length: 4})
1988 call prop_add(2, 6, #{type: 'three', length: 4})
1989 call prop_add(2, 11, #{type: 'three', length: 4})
1990
1991 " The first prop over 'helo' increases its length after the word is corrected
1992 " to 'Hello', the second one is shifted to the right.
1993 let expected = [
1994 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
1995 \ 'length': 5, 'start': 1},
1996 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'two',
1997 \ 'length': 5, 'start': 1}
1998 \ ]
1999 call feedkeys("z=1\<CR>", 'xt')
2000
2001 call assert_equal('Hello world', getline(1))
2002 call assert_equal(expected, prop_list(1))
2003
2004 " Repeat the replacement done by z=
2005 spellrepall
2006
2007 let expected = [
2008 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2009 \ 'length': 5, 'start': 1},
2010 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2011 \ 'length': 5, 'start': 1},
2012 \ {'id': 0, 'col': 13, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2013 \ 'length': 5, 'start': 1}
2014 \ ]
2015 call assert_equal('Hello Hello Hello', getline(2))
2016 call assert_equal(expected, prop_list(2))
2017
2018 call DeletePropTypes()
2019 set spell&
2020 bwipe!
2021endfunc
2022
LemonBoy4b936742022-05-13 21:56:28 +01002023func Test_prop_shift_block()
2024 new
2025 call AddPropTypes()
2026
2027 call setline(1, ['some highlighted text']->repeat(2))
2028 call prop_add(1, 10, #{type: 'one', length: 11})
2029 call prop_add(2, 10, #{type: 'two', length: 11})
2030
2031 call cursor(1, 1)
2032 call feedkeys("5l\<c-v>>", 'nxt')
2033 call cursor(2, 1)
2034 call feedkeys("5l\<c-v><", 'nxt')
2035
2036 let expected = [
2037 \ {'lnum': 1, 'id': 0, 'col': 8, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2038 \ 'length': 11, 'start' : 1},
2039 \ {'lnum': 2, 'id': 0, 'col': 6, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2040 \ 'length': 11, 'start' : 1}
2041 \ ]
2042 call assert_equal(expected, prop_list(1, #{end_lnum: 2}))
2043
2044 call DeletePropTypes()
2045 bwipe!
2046endfunc
LemonBoyb7a70122022-05-13 12:41:50 +01002047
LemonBoy698cb4c2022-05-14 18:10:15 +01002048func Test_prop_insert_multiline()
2049 new
2050 call AddPropTypes()
2051
2052 call setline(1, ['foobar', 'barbaz'])
2053 call prop_add(1, 4, #{end_lnum: 2, end_col: 4, type: 'one'})
2054
2055 call feedkeys("1Goquxqux\<Esc>", 'nxt')
2056 call feedkeys("2GOquxqux\<Esc>", 'nxt')
2057
2058 let lines =<< trim END
2059 foobar
2060 quxqux
2061 quxqux
2062 barbaz
2063 END
2064 call assert_equal(lines, getline(1, '$'))
2065 let expected = [
2066 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2067 \ 'length': 4 ,'start': 1},
2068 \ {'lnum': 2, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2069 \ 'length': 7, 'start': 0},
2070 \ {'lnum': 3, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2071 \ 'length': 7, 'start': 0},
2072 \ {'lnum': 4, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2073 \ 'length': 3, 'start': 0}
2074 \ ]
2075 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
2076
2077 call DeletePropTypes()
2078 bwipe!
2079endfunc
2080
LemonBoyb559b302022-05-15 13:08:02 +01002081func Test_prop_blockwise_change()
2082 new
2083 call AddPropTypes()
2084
2085 call setline(1, ['foooooo', 'bar', 'baaaaz'])
2086 call prop_add(1, 1, #{end_col: 3, type: 'one'})
2087 call prop_add(2, 1, #{end_col: 3, type: 'two'})
2088 call prop_add(3, 1, #{end_col: 3, type: 'three'})
2089
2090 " Replace the first two columns with '123', since 'start_incl' is false the
2091 " prop is not extended.
2092 call feedkeys("gg\<c-v>2jc123\<Esc>", 'nxt')
2093
2094 let lines =<< trim END
2095 123oooooo
2096 123ar
2097 123aaaaz
2098 END
2099 call assert_equal(lines, getline(1, '$'))
2100 let expected = [
2101 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2102 \ 'length': 1, 'start': 1},
2103 \ {'lnum': 2, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2104 \ 'length': 1, 'start': 1},
2105 \ {'lnum': 3, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1 ,
2106 \ 'type': 'three', 'length': 1, 'start': 1}
2107 \ ]
2108 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
2109
2110 call DeletePropTypes()
2111 bwipe!
2112endfunc
2113
Paul Ollis4c3d21a2022-05-24 21:26:37 +01002114func Do_test_props_do_not_affect_byte_offsets(ff, increment)
2115 new
2116 let lcount = 410
2117
2118 " File format affects byte-offset calculations, so make sure it is known.
2119 exec 'setlocal fileformat=' . a:ff
2120
2121 " Fill the buffer with varying length lines. We need a suitably large number
2122 " to force Vim code through paths wehere previous error have occurred. This
2123 " is more 'art' than 'science'.
2124 let text = 'a'
2125 call setline(1, text)
2126 let offsets = [1]
2127 for idx in range(lcount)
2128 call add(offsets, offsets[idx] + len(text) + a:increment)
2129 if (idx % 6) == 0
2130 let text = text . 'a'
2131 endif
2132 call append(line('$'), text)
2133 endfor
2134
2135 " Set a property that spans a few lines to cause Vim's internal buffer code
2136 " to perform a reasonable amount of rearrangement.
2137 call prop_type_add('one', {'highlight': 'ErrorMsg'})
2138 call prop_add(1, 1, {'type': 'one', 'end_lnum': 6, 'end_col': 2})
2139
2140 for idx in range(lcount)
2141 let boff = line2byte(idx + 1)
2142 call assert_equal(offsets[idx], boff, 'Bad byte offset at line ' . (idx + 1))
2143 endfor
2144
2145 call prop_type_delete('one')
2146 bwipe!
2147endfunc
2148
2149func Test_props_do_not_affect_byte_offsets()
2150 call Do_test_props_do_not_affect_byte_offsets('unix', 1)
2151endfunc
2152
2153func Test_props_do_not_affect_byte_offsets_dos()
2154 call Do_test_props_do_not_affect_byte_offsets('dos', 2)
2155endfunc
2156
2157func Test_props_do_not_affect_byte_offsets_editline()
2158 new
2159 let lcount = 410
2160
2161 " File format affects byte-offset calculations, so make sure it is known.
2162 setlocal fileformat=unix
2163
2164 " Fill the buffer with varying length lines. We need a suitably large number
2165 " to force Vim code through paths wehere previous error have occurred. This
2166 " is more 'art' than 'science'.
2167 let text = 'aa'
2168 call setline(1, text)
2169 let offsets = [1]
2170 for idx in range(lcount)
2171 call add(offsets, offsets[idx] + len(text) + 1)
2172 if (idx % 6) == 0
2173 let text = text . 'a'
2174 endif
2175 call append(line('$'), text)
2176 endfor
2177
2178 " Set a property that just covers the first line. When this test was
2179 " developed, this did not trigger a byte-offset error.
2180 call prop_type_add('one', {'highlight': 'ErrorMsg'})
2181 call prop_add(1, 1, {'type': 'one', 'end_lnum': 1, 'end_col': 3})
2182
2183 for idx in range(lcount)
2184 let boff = line2byte(idx + 1)
2185 call assert_equal(offsets[idx], boff,
2186 \ 'Confounding bad byte offset at line ' . (idx + 1))
2187 endfor
2188
2189 " Insert text in the middle of the first line, keeping the property
2190 " unchanged.
2191 :1
2192 normal aHello
2193 for idx in range(1, lcount)
2194 let offsets[idx] = offsets[idx] + 5
2195 endfor
2196
2197 for idx in range(lcount)
2198 let boff = line2byte(idx + 1)
2199 call assert_equal(offsets[idx], boff,
2200 \ 'Bad byte offset at line ' . (idx + 1))
2201 endfor
2202
2203 call prop_type_delete('one')
2204 bwipe!
2205endfunc
2206
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002207func Test_prop_inserts_text()
2208 CheckRunVimInTerminal
2209
2210 " Just a basic check for now
2211 let lines =<< trim END
2212 call setline(1, 'insert some text here and other text there and some more text after wrapping')
2213 call prop_type_add('someprop', #{highlight: 'ErrorMsg'})
2214 call prop_type_add('otherprop', #{highlight: 'Search'})
2215 call prop_type_add('moreprop', #{highlight: 'DiffAdd'})
2216 call prop_add(1, 18, #{type: 'someprop', text: 'SOME '})
Bram Moolenaar783ef722022-08-01 16:11:06 +01002217 call prop_add(1, 38, #{type: 'otherprop', text: "OTHER\t"})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002218 call prop_add(1, 69, #{type: 'moreprop', text: 'MORE '})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002219 normal $
Bram Moolenaar09ff4b52022-08-01 16:51:02 +01002220
2221 call setline(2, 'prepost')
2222 call prop_type_add('multibyte', #{highlight: 'Visual'})
2223 call prop_add(2, 4, #{type: 'multibyte', text: 'söme和平téxt'})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002224 END
2225 call writefile(lines, 'XscriptPropsWithText')
2226 let buf = RunVimInTerminal('-S XscriptPropsWithText', #{rows: 6, cols: 60})
Bram Moolenaar711483c2022-07-30 21:33:46 +01002227 call VerifyScreenDump(buf, 'Test_prop_inserts_text_1', {})
2228
2229 call term_sendkeys(buf, ":set signcolumn=yes\<CR>")
2230 call VerifyScreenDump(buf, 'Test_prop_inserts_text_2', {})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002231
2232 call StopVimInTerminal(buf)
2233 call delete('XscriptPropsWithText')
2234endfunc
2235
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002236func Test_props_with_text_after()
2237 CheckRunVimInTerminal
2238
2239 let lines =<< trim END
2240 call setline(1, 'some text here and other text there')
2241 call prop_type_add('rightprop', #{highlight: 'ErrorMsg'})
2242 call prop_type_add('afterprop', #{highlight: 'Search'})
2243 call prop_type_add('belowprop', #{highlight: 'DiffAdd'})
2244 call prop_add(1, 0, #{type: 'rightprop', text: ' RIGHT ', text_align: 'right'})
Bram Moolenaar783ef722022-08-01 16:11:06 +01002245 call prop_add(1, 0, #{type: 'afterprop', text: "\tAFTER\t", text_align: 'after'})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002246 call prop_add(1, 0, #{type: 'belowprop', text: ' BELOW ', text_align: 'below'})
Bram Moolenaar84b247f2022-08-01 11:17:40 +01002247
2248 call setline(2, 'Last line.')
2249 call prop_add(2, 0, #{type: 'afterprop', text: ' After Last ', text_align: 'after'})
2250 normal G$
Bram Moolenaar09ff4b52022-08-01 16:51:02 +01002251
2252 call setline(3, 'right here')
2253 call prop_add(3, 0, #{type: 'rightprop', text: 'söme和平téxt', text_align: 'right'})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002254 END
2255 call writefile(lines, 'XscriptPropsWithTextAfter')
2256 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfter', #{rows: 6, cols: 60})
2257 call VerifyScreenDump(buf, 'Test_prop_with_text_after_1', {})
2258
2259 call StopVimInTerminal(buf)
2260 call delete('XscriptPropsWithTextAfter')
2261endfunc
2262
Bram Moolenaare175dc62022-08-01 22:18:50 +01002263func Test_props_with_text_after_joined()
2264 CheckRunVimInTerminal
2265
2266 let lines =<< trim END
2267 call setline(1, ['one', 'two', 'three', 'four'])
2268 call prop_type_add('afterprop', #{highlight: 'Search'})
2269 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE', text_align: 'after'})
2270 call prop_add(4, 0, #{type: 'afterprop', text: ' FOUR', text_align: 'after'})
2271 normal ggJ
2272 normal GkJ
2273
2274 call setline(3, ['a', 'b', 'c', 'd', 'e', 'f'])
2275 call prop_add(3, 0, #{type: 'afterprop', text: ' AAA', text_align: 'after'})
2276 call prop_add(5, 0, #{type: 'afterprop', text: ' CCC', text_align: 'after'})
2277 call prop_add(7, 0, #{type: 'afterprop', text: ' EEE', text_align: 'after'})
2278 call prop_add(8, 0, #{type: 'afterprop', text: ' FFF', text_align: 'after'})
2279 normal 3G6J
2280 END
2281 call writefile(lines, 'XscriptPropsWithTextAfterJoined')
2282 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterJoined', #{rows: 6, cols: 60})
2283 call VerifyScreenDump(buf, 'Test_prop_with_text_after_joined_1', {})
2284
2285 call StopVimInTerminal(buf)
2286 call delete('XscriptPropsWithTextAfterJoined')
2287endfunc
2288
Bram Moolenaar398649e2022-08-04 15:03:48 +01002289func Test_props_with_text_after_truncated()
2290 CheckRunVimInTerminal
2291
2292 let lines =<< trim END
2293 call setline(1, ['one two three four five six seven'])
2294 call prop_type_add('afterprop', #{highlight: 'Search'})
2295 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE'})
2296
2297 call setline(2, ['one two three four five six seven'])
2298 call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right'})
2299
2300 call setline(3, ['one two three four five six seven'])
2301 call prop_add(3, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five lets wrap after some more text', text_align: 'below'})
2302
2303 call setline(4, ['cursor here'])
2304 normal 4Gfh
2305 END
2306 call writefile(lines, 'XscriptPropsWithTextAfterTrunc')
2307 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterTrunc', #{rows: 9, cols: 60})
2308 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_1', {})
2309
2310 call term_sendkeys(buf, ":37vsp\<CR>gg")
2311 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_2', {})
2312
2313 call term_sendkeys(buf, ":36wincmd |\<CR>")
2314 call term_sendkeys(buf, "2G$")
2315 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_3', {})
2316
2317 call term_sendkeys(buf, ":33wincmd |\<CR>")
2318 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_4', {})
2319
2320 call term_sendkeys(buf, ":18wincmd |\<CR>")
2321 call term_sendkeys(buf, "0fx")
2322 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_5', {})
2323
2324 call StopVimInTerminal(buf)
2325 call delete('XscriptPropsWithTextAfterTrunc')
2326endfunc
2327
2328func Test_props_with_text_after_wraps()
2329 CheckRunVimInTerminal
2330
2331 let lines =<< trim END
2332 call setline(1, ['one two three four five six seven'])
2333 call prop_type_add('afterprop', #{highlight: 'Search'})
2334 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE', text_wrap: 'wrap'})
2335
2336 call setline(2, ['one two three four five six seven'])
2337 call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right', text_wrap: 'wrap'})
2338
2339 call setline(3, ['one two three four five six seven'])
2340 call prop_add(3, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five lets wrap after some more text', text_align: 'below', text_wrap: 'wrap'})
2341
2342 call setline(4, ['cursor here'])
2343 normal 4Gfh
2344 END
2345 call writefile(lines, 'XscriptPropsWithTextAfterWraps')
2346 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterWraps', #{rows: 9, cols: 60})
2347 call VerifyScreenDump(buf, 'Test_prop_with_text_after_wraps_1', {})
2348
2349 call StopVimInTerminal(buf)
2350 call delete('XscriptPropsWithTextAfterWraps')
2351endfunc
2352
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01002353func Test_removed_prop_with_text_cleans_up_array()
2354 new
2355 call setline(1, 'some text here')
2356 call prop_type_add('some', #{highlight: 'ErrorMsg'})
2357 let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"})
2358 call assert_equal(-1, id1)
2359 let id2 = prop_add(1, 10, #{type: 'some', text: "HERE"})
2360 call assert_equal(-2, id2)
2361
2362 " removing the props resets the index
2363 call prop_remove(#{id: id1})
2364 call prop_remove(#{id: id2})
2365 let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"})
2366 call assert_equal(-1, id1)
2367
2368 call prop_type_delete('some')
2369 bwipe!
2370endfunc
2371
Bram Moolenaar1f4ee192022-08-01 15:52:55 +01002372def Test_insert_text_before_virtual_text()
2373 new foobar
2374 setline(1, '12345678')
2375 prop_type_add('test', {highlight: 'Search'})
2376 prop_add(1, 5, {
2377 type: 'test',
2378 text: ' virtual text '
2379 })
2380 normal! f4axyz
2381 normal! f5iXYZ
2382 assert_equal('1234xyzXYZ5678', getline(1))
2383
2384 prop_type_delete('test')
2385 bwipe!
2386enddef
2387
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002388" vim: shiftwidth=2 sts=2 expandtab