blob: 4f03ef52d16968167289951ca949c83241e8faf4 [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
Bram Moolenaar10246902022-08-08 17:08:05 +010074def Test_proptype_add_remove()
75 # add and remove a prop type so that the array is empty
76 prop_type_add('local', {bufnr: bufnr('%')})
77 prop_type_delete('local', {bufnr: bufnr('%')})
78 prop_type_add('global', {highlight: 'ErrorMsg'})
79 prop_add(1, 1, {length: 1, type: 'global'})
80 redraw
81
82 prop_clear(1)
83 prop_type_delete('global')
84enddef
85
Martin Tournoije2390c72021-07-28 13:30:16 +020086def Test_proptype_buf_list()
87 new
88 var bufnr = bufnr('')
89 try
90 prop_type_add('global', {})
91 prop_type_add('local', {bufnr: bufnr})
92
93 prop_add(1, 1, {type: 'global'})
94 prop_add(1, 1, {type: 'local'})
95
96 assert_equal([
97 {type: 'local', type_bufnr: bufnr, id: 0, col: 1, end: 1, length: 0, start: 1},
98 {type: 'global', type_bufnr: 0, id: 0, col: 1, end: 1, length: 0, start: 1},
99 ], prop_list(1))
100 assert_equal(
101 {lnum: 1, id: 0, col: 1, type_bufnr: bufnr, end: 1, type: 'local', length: 0, start: 1},
102 prop_find({lnum: 1, type: 'local'}))
103 assert_equal(
104 {lnum: 1, id: 0, col: 1, type_bufnr: 0, end: 1, type: 'global', length: 0, start: 1},
105 prop_find({lnum: 1, type: 'global'}))
106
107 prop_remove({type: 'global'}, 1)
108 prop_remove({type: 'local'}, 1)
109 finally
110 prop_type_delete('global')
111 prop_type_delete('local', {bufnr: bufnr})
112 bwipe!
113 endtry
114enddef
115
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100116func AddPropTypes()
117 call prop_type_add('one', {})
118 call prop_type_add('two', {})
119 call prop_type_add('three', {})
120 call prop_type_add('whole', {})
121endfunc
122
123func DeletePropTypes()
124 call prop_type_delete('one')
125 call prop_type_delete('two')
126 call prop_type_delete('three')
127 call prop_type_delete('whole')
128endfunc
129
130func SetupPropsInFirstLine()
131 call setline(1, 'one two three')
132 call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'})
Bram Moolenaara5a78822019-09-04 21:57:18 +0200133 eval 1->prop_add(5, {'length': 3, 'id': 12, 'type': 'two'})
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100134 call prop_add(1, 9, {'length': 5, 'id': 13, 'type': 'three'})
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100135 call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'})
136endfunc
137
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100138func Get_expected_props()
139 return [
Martin Tournoije2390c72021-07-28 13:30:16 +0200140 \ #{type_bufnr: 0, col: 1, length: 13, id: 14, type: 'whole', start: 1, end: 1},
141 \ #{type_bufnr: 0, col: 1, length: 3, id: 11, type: 'one', start: 1, end: 1},
142 \ #{type_bufnr: 0, col: 5, length: 3, id: 12, type: 'two', start: 1, end: 1},
143 \ #{type_bufnr: 0, col: 9, length: 5, id: 13, type: 'three', start: 1, end: 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100144 \ ]
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100145endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100146
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100147func Test_prop_find()
148 new
149 call setline(1, ['one one one', 'twotwo', 'three', 'fourfour', 'five', 'sixsix'])
Martin Tournoije2390c72021-07-28 13:30:16 +0200150
151 " Add two text props on lines 1 and 5, and one spanning lines 2 to 4.
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100152 call prop_type_add('prop_name', {'highlight': 'Directory'})
153 call prop_add(1, 5, {'type': 'prop_name', 'id': 10, 'length': 3})
154 call prop_add(2, 4, {'type': 'prop_name', 'id': 11, 'end_lnum': 4, 'end_col': 9})
155 call prop_add(5, 4, {'type': 'prop_name', 'id': 12, 'length': 1})
156
157 let expected = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200158 \ #{type_bufnr: 0, lnum: 1, col: 5, length: 3, id: 10, type: 'prop_name', start: 1, end: 1},
159 \ #{type_bufnr: 0, lnum: 2, col: 4, id: 11, type: 'prop_name', start: 1, end: 0},
160 \ #{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 +0100161 \ ]
162
163 " Starting at line 5 col 1 this should find the prop at line 5 col 4.
164 call cursor(5,1)
165 let result = prop_find({'type': 'prop_name'}, 'f')
166 call assert_equal(expected[2], result)
167
168 " With skipstart left at false (default), this should find the prop at line
169 " 5 col 4.
170 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4}, 'b')
171 call assert_equal(expected[2], result)
172
173 " With skipstart set to true, this should skip the prop at line 5 col 4.
174 let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4, 'skipstart': 1}, 'b')
175 unlet result.length
176 call assert_equal(expected[1], result)
177
178 " Search backwards from line 1 col 10 to find the prop on the same line.
179 let result = prop_find({'type': 'prop_name', 'lnum': 1, 'col': 10}, 'b')
180 call assert_equal(expected[0], result)
181
182 " with skipstart set to false, if the start position is anywhere between the
183 " start and end lines of a text prop (searching forward or backward), the
184 " result should be the prop on the first line (the line with 'start' set to 1).
185 call cursor(3,1)
186 let result = prop_find({'type': 'prop_name'}, 'f')
187 unlet result.length
188 call assert_equal(expected[1], result)
189 let result = prop_find({'type': 'prop_name'}, 'b')
190 unlet result.length
191 call assert_equal(expected[1], result)
192
193 " with skipstart set to true, if the start position is anywhere between the
194 " start and end lines of a text prop (searching forward or backward), all lines
195 " of the prop will be skipped.
196 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'b')
197 call assert_equal(expected[0], result)
198 let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'f')
199 call assert_equal(expected[2], result)
200
201 " Use skipstart to search through all props with type name 'prop_name'.
202 " First forward...
203 let lnum = 1
204 let col = 1
205 let i = 0
206 for exp in expected
207 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'f')
208 if !has_key(exp, "length")
209 unlet result.length
210 endif
211 call assert_equal(exp, result)
212 let lnum = result.lnum
213 let col = result.col
214 let i = i + 1
215 endfor
216
217 " ...then backwards.
218 let lnum = 6
219 let col = 4
220 let i = 2
221 while i >= 0
222 let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'b')
223 if !has_key(expected[i], "length")
224 unlet result.length
225 endif
226 call assert_equal(expected[i], result)
227 let lnum = result.lnum
228 let col = result.col
229 let i = i - 1
Martin Tournoije2390c72021-07-28 13:30:16 +0200230 endwhile
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100231
232 " Starting from line 6 col 1 search backwards for prop with id 10.
233 call cursor(6,1)
234 let result = prop_find({'id': 10, 'skipstart': 1}, 'b')
235 call assert_equal(expected[0], result)
236
237 " Starting from line 1 col 1 search forwards for prop with id 12.
238 call cursor(1,1)
239 let result = prop_find({'id': 12}, 'f')
240 call assert_equal(expected[2], result)
241
242 " Search for a prop with an unknown id.
243 let result = prop_find({'id': 999}, 'f')
244 call assert_equal({}, result)
245
246 " Search backwards from the proceeding position of the prop with id 11
247 " (at line num 2 col 4). This should return an empty dict.
248 let result = prop_find({'id': 11, 'lnum': 2, 'col': 3}, 'b')
249 call assert_equal({}, result)
250
251 " When lnum is given and col is omitted, use column 1.
252 let result = prop_find({'type': 'prop_name', 'lnum': 1}, 'f')
253 call assert_equal(expected[0], result)
254
Bram Moolenaare041dde2021-08-01 21:30:12 +0200255 " Negative ID is possible, just like prop is not found.
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200256 call assert_equal({}, prop_find({'id': -1}))
Bram Moolenaare041dde2021-08-01 21:30:12 +0200257 call assert_equal({}, prop_find({'id': -2}))
Bram Moolenaar8e3fc132021-07-31 18:33:57 +0200258
Bram Moolenaare041dde2021-08-01 21:30:12 +0200259 call prop_clear(1, 6)
260
261 " Default ID is zero
262 call prop_add(5, 4, {'type': 'prop_name', 'length': 1})
263 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}))
264
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100265 call prop_type_delete('prop_name')
Bram Moolenaare041dde2021-08-01 21:30:12 +0200266 call prop_clear(1, 6)
Bram Moolenaar4da7a252020-09-02 19:59:00 +0200267 bwipe!
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100268endfunc
269
Bram Moolenaareb245562020-09-03 22:33:44 +0200270def Test_prop_find2()
271 # Multiple props per line, start on the first, should find the second.
272 new
273 ['the quikc bronw fox jumsp over the layz dog']->repeat(2)->setline(1)
Bram Moolenaare0de1712020-12-02 17:36:54 +0100274 prop_type_add('misspell', {highlight: 'ErrorMsg'})
Bram Moolenaareb245562020-09-03 22:33:44 +0200275 for lnum in [1, 2]
276 for col in [8, 14, 24, 38]
Bram Moolenaare0de1712020-12-02 17:36:54 +0100277 prop_add(lnum, col, {type: 'misspell', length: 2})
Bram Moolenaareb245562020-09-03 22:33:44 +0200278 endfor
279 endfor
280 cursor(1, 8)
Martin Tournoije2390c72021-07-28 13:30:16 +0200281 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 +0100282 var result = prop_find({type: 'misspell', skipstart: true}, 'f')
Bram Moolenaareb245562020-09-03 22:33:44 +0200283 assert_equal(expected, result)
284
285 prop_type_delete('misspell')
286 bwipe!
287enddef
288
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100289func Test_prop_find_smaller_len_than_match_col()
290 new
291 call prop_type_add('test', {'highlight': 'ErrorMsg'})
292 call setline(1, ['xxxx', 'x'])
293 call prop_add(1, 4, {'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200294 call assert_equal(
295 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 4, type: 'test', length: 0, start: 1, end: 1},
Bram Moolenaar346f18e2020-03-13 21:36:40 +0100296 \ prop_find({'type': 'test', 'lnum': 2, 'col': 1}, 'b'))
297 bwipe!
298 call prop_type_delete('test')
299endfunc
300
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100301func Test_prop_find_with_both_option_enabled()
302 " Initialize
303 new
304 call AddPropTypes()
305 call SetupPropsInFirstLine()
306 let props = Get_expected_props()->map({_, v -> extend(v, {'lnum': 1})})
307 " Test
308 call assert_fails("call prop_find({'both': 1})", 'E968:')
309 call assert_fails("call prop_find({'id': 11, 'both': 1})", 'E860:')
310 call assert_fails("call prop_find({'type': 'three', 'both': 1})", 'E860:')
311 call assert_equal({}, prop_find({'id': 11, 'type': 'three', 'both': 1}))
312 call assert_equal({}, prop_find({'id': 130000, 'type': 'one', 'both': 1}))
313 call assert_equal(props[2], prop_find({'id': 12, 'type': 'two', 'both': 1}))
314 call assert_equal(props[0], prop_find({'id': 14, 'type': 'whole', 'both': 1}))
315 " Clean up
316 call DeletePropTypes()
317 bwipe!
318endfunc
319
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100320func Test_prop_add()
321 new
322 call AddPropTypes()
323 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100324 let expected_props = Get_expected_props()
325 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100326 call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:')
327 call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:')
Martin Tournoije2390c72021-07-28 13:30:16 +0200328
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100329 " Insert a line above, text props must still be there.
330 call append(0, 'empty')
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100331 call assert_equal(expected_props, prop_list(2))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100332 " Delete a line above, text props must still be there.
333 1del
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100334 call assert_equal(expected_props, prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100335
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100336 " Prop without length or end column is zero length
337 call prop_clear(1)
Bram Moolenaar12f20032020-02-26 22:06:00 +0100338 call prop_type_add('included', {'start_incl': 1, 'end_incl': 1})
339 call prop_add(1, 5, #{type: 'included'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200340 let expected = [#{type_bufnr: 0, col: 5, length: 0, type: 'included', id: 0, start: 1, end: 1}]
Bram Moolenaar12f20032020-02-26 22:06:00 +0100341 call assert_equal(expected, prop_list(1))
342
343 " Inserting text makes the prop bigger.
344 exe "normal 5|ixx\<Esc>"
Martin Tournoije2390c72021-07-28 13:30:16 +0200345 let expected = [#{type_bufnr: 0, col: 5, length: 2, type: 'included', id: 0, start: 1, end: 1}]
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100346 call assert_equal(expected, prop_list(1))
347
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200348 call assert_fails("call prop_add(1, 5, {'type': 'two', 'bufnr': 234343})", 'E158:')
349
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100350 call DeletePropTypes()
Bram Moolenaar12f20032020-02-26 22:06:00 +0100351 call prop_type_delete('included')
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100352 bwipe!
353endfunc
354
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200355" Test for the prop_add_list() function
356func Test_prop_add_list()
357 new
358 call AddPropTypes()
359 call setline(1, ['one one one', 'two two two', 'six six six', 'ten ten ten'])
360 call prop_add_list(#{type: 'one', id: 2},
361 \ [[1, 1, 1, 3], [2, 5, 2, 7], [3, 6, 4, 6]])
362 call assert_equal([#{id: 2, col: 1, type_bufnr: 0, end: 1, type: 'one',
363 \ length: 2, start: 1}], prop_list(1))
364 call assert_equal([#{id: 2, col: 5, type_bufnr: 0, end: 1, type: 'one',
365 \ length: 2, start: 1}], prop_list(2))
366 call assert_equal([#{id: 2, col: 6, type_bufnr: 0, end: 0, type: 'one',
367 \ length: 7, start: 1}], prop_list(3))
368 call assert_equal([#{id: 2, col: 1, type_bufnr: 0, end: 1, type: 'one',
369 \ length: 5, start: 0}], prop_list(4))
370 call assert_fails('call prop_add_list([1, 2], [[1, 1, 3]])', 'E1206:')
371 call assert_fails('call prop_add_list({}, {})', 'E1211:')
372 call assert_fails('call prop_add_list({}, [[1, 1, 3]])', 'E965:')
373 call assert_fails('call prop_add_list(#{type: "abc"}, [[1, 1, 1, 3]])', 'E971:')
374 call assert_fails('call prop_add_list(#{type: "one"}, [[]])', 'E474:')
375 call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, 1, 1], {}])', 'E714:')
376 call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, "a"]])', 'E474:')
377 call assert_fails('call prop_add_list(#{type: "one"}, [[2, 2]])', 'E474:')
378 call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, 2], [2, 2]])', 'E474:')
379 call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, 1, 2], [4, 1, 5, 2]])', 'E966:')
380 call assert_fails('call prop_add_list(#{type: "one"}, [[3, 1, 1, 2]])', 'E966:')
381 call assert_fails('call prop_add_list(#{type: "one"}, [[2, 2, 2, 2], [3, 20, 3, 22]])', 'E964:')
382 call assert_fails('eval #{type: "one"}->prop_add_list([[2, 2, 2, 2], [3, 20, 3, 22]])', 'E964:')
383 call assert_fails('call prop_add_list(test_null_dict(), [[2, 2, 2]])', 'E965:')
384 call assert_fails('call prop_add_list(#{type: "one"}, test_null_list())', 'E714:')
385 call assert_fails('call prop_add_list(#{type: "one"}, [test_null_list()])', 'E714:')
386 call DeletePropTypes()
387 bw!
388endfunc
389
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100390func Test_prop_remove()
391 new
392 call AddPropTypes()
393 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100394 let props = Get_expected_props()
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100395 call assert_equal(props, prop_list(1))
396
397 " remove by id
Bram Moolenaara5a78822019-09-04 21:57:18 +0200398 call assert_equal(1, {'id': 12}->prop_remove(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100399 unlet props[2]
400 call assert_equal(props, prop_list(1))
401
402 " remove by type
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200403 call assert_equal(1, prop_remove({'type': 'one'}, 1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100404 unlet props[1]
405 call assert_equal(props, prop_list(1))
406
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200407 " remove from unknown buffer
408 call assert_fails("call prop_remove({'type': 'one', 'bufnr': 123456}, 1)", 'E158:')
409
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100410 call DeletePropTypes()
411 bwipe!
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100412
413 new
414 call AddPropTypes()
415 call SetupPropsInFirstLine()
416 call prop_add(1, 6, {'length': 2, 'id': 11, 'type': 'three'})
417 let props = Get_expected_props()
Martin Tournoije2390c72021-07-28 13:30:16 +0200418 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 +0100419 call assert_equal(props, prop_list(1))
420 call assert_equal(1, prop_remove({'type': 'three', 'id': 11, 'both': 1, 'all': 1}, 1))
421 unlet props[3]
422 call assert_equal(props, prop_list(1))
423
Bram Moolenaare2e40752020-09-04 21:18:46 +0200424 call assert_fails("call prop_remove({'id': 11, 'both': 1})", 'E860:')
425 call assert_fails("call prop_remove({'type': 'three', 'both': 1})", 'E860:')
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100426
427 call DeletePropTypes()
428 bwipe!
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100429endfunc
430
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200431def Test_prop_add_vim9()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100432 prop_type_add('comment', {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200433 highlight: 'Directory',
434 priority: 123,
435 start_incl: true,
436 end_incl: true,
437 combine: false,
438 })
439 prop_type_delete('comment')
440enddef
441
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200442def Test_prop_remove_vim9()
443 new
Bram Moolenaar62aec932022-01-29 21:45:34 +0000444 g:AddPropTypes()
445 g:SetupPropsInFirstLine()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100446 assert_equal(1, prop_remove({type: 'three', id: 13, both: true, all: true}))
Bram Moolenaar62aec932022-01-29 21:45:34 +0000447 g:DeletePropTypes()
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200448 bwipe!
449enddef
450
Bram Moolenaar196d1572019-01-02 23:47:18 +0100451func SetupOneLine()
452 call setline(1, 'xonex xtwoxx')
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200453 normal gg0
Bram Moolenaar196d1572019-01-02 23:47:18 +0100454 call AddPropTypes()
455 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
456 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
457 let expected = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200458 \ #{type_bufnr: 0, col: 2, length: 3, id: 11, type: 'one', start: 1, end: 1},
459 \ #{type_bufnr: 0, col: 8, length: 3, id: 12, type: 'two', start: 1, end: 1},
Bram Moolenaar196d1572019-01-02 23:47:18 +0100460 \]
461 call assert_equal(expected, prop_list(1))
462 return expected
463endfunc
464
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100465func Test_prop_add_remove_buf()
466 new
467 let bufnr = bufnr('')
468 call AddPropTypes()
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100469 for lnum in range(1, 4)
470 call setline(lnum, 'one two three')
471 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100472 wincmd w
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100473 for lnum in range(1, 4)
474 call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
475 call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
476 call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
477 endfor
478
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100479 let props = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200480 \ #{type_bufnr: 0, col: 1, length: 3, id: 11, type: 'one', start: 1, end: 1},
481 \ #{type_bufnr: 0, col: 5, length: 3, id: 12, type: 'two', start: 1, end: 1},
482 \ #{type_bufnr: 0, col: 11, length: 3, id: 13, type: 'three', start: 1, end: 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100483 \]
484 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100485
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100486 " remove by id
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100487 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100488 unlet props[1]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100489
490 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100491 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100492 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
493 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
494 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
495
496 call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4)
497 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
498 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
499 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
500 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
501
502 call prop_remove({'id': 12, 'bufnr': bufnr})
503 for lnum in range(1, 4)
504 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
505 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100506
507 " remove by type
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100508 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100509 unlet props[0]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100510
511 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100512 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100513 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
514 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
515 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
516
517 call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4)
518 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
519 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
520 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
521 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
522
523 call prop_remove({'type': 'one', 'bufnr': bufnr})
524 for lnum in range(1, 4)
525 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
526 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100527
528 call DeletePropTypes()
529 wincmd w
530 bwipe!
531endfunc
532
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100533func Test_prop_backspace()
534 new
535 set bs=2
Bram Moolenaar196d1572019-01-02 23:47:18 +0100536 let expected = SetupOneLine() " 'xonex xtwoxx'
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100537
538 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
539 call assert_equal('one xtwoxx', getline(1))
540 let expected[0].col = 1
541 let expected[1].col = 6
542 call assert_equal(expected, prop_list(1))
543
544 call DeletePropTypes()
545 bwipe!
546 set bs&
547endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100548
LemonBoyd0b1a092022-05-12 18:45:18 +0100549func Test_prop_change()
550 new
551 let expected = SetupOneLine() " 'xonex xtwoxx'
552
553 " Characterwise.
554 exe "normal 7|c$\<Esc>"
555 call assert_equal('xonex ', getline(1))
556 call assert_equal(expected[:0], prop_list(1))
557 " Linewise.
558 exe "normal cc\<Esc>"
559 call assert_equal('', getline(1))
560 call assert_equal([], prop_list(1))
561
562 call DeletePropTypes()
563 bwipe!
564 set bs&
565endfunc
566
Bram Moolenaar196d1572019-01-02 23:47:18 +0100567func Test_prop_replace()
568 new
569 set bs=2
570 let expected = SetupOneLine() " 'xonex xtwoxx'
571
572 exe "normal 0Ryyy\<Esc>"
573 call assert_equal('yyyex xtwoxx', getline(1))
574 call assert_equal(expected, prop_list(1))
575
576 exe "normal ftRyy\<BS>"
577 call assert_equal('yyyex xywoxx', getline(1))
578 call assert_equal(expected, prop_list(1))
579
580 exe "normal 0fwRyy\<BS>"
581 call assert_equal('yyyex xyyoxx', getline(1))
582 call assert_equal(expected, prop_list(1))
583
584 exe "normal 0foRyy\<BS>\<BS>"
585 call assert_equal('yyyex xyyoxx', getline(1))
586 call assert_equal(expected, prop_list(1))
587
LemonBoy0d534d92022-05-21 11:20:42 +0100588 " Replace three 1-byte chars with three 2-byte ones.
589 exe "normal 0l3rø"
590 call assert_equal('yøøøx xyyoxx', getline(1))
591 let expected[0].length += 3
592 let expected[1].col += 3
593 call assert_equal(expected, prop_list(1))
594
Bram Moolenaar196d1572019-01-02 23:47:18 +0100595 call DeletePropTypes()
596 bwipe!
597 set bs&
598endfunc
599
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200600func Test_prop_open_line()
601 new
602
603 " open new line, props stay in top line
604 let expected = SetupOneLine() " 'xonex xtwoxx'
605 exe "normal o\<Esc>"
606 call assert_equal('xonex xtwoxx', getline(1))
607 call assert_equal('', getline(2))
608 call assert_equal(expected, prop_list(1))
609 call DeletePropTypes()
610
611 " move all props to next line
612 let expected = SetupOneLine() " 'xonex xtwoxx'
613 exe "normal 0i\<CR>\<Esc>"
614 call assert_equal('', getline(1))
615 call assert_equal('xonex xtwoxx', getline(2))
616 call assert_equal(expected, prop_list(2))
617 call DeletePropTypes()
618
619 " split just before prop, move all props to next line
620 let expected = SetupOneLine() " 'xonex xtwoxx'
621 exe "normal 0li\<CR>\<Esc>"
622 call assert_equal('x', getline(1))
623 call assert_equal('onex xtwoxx', getline(2))
624 let expected[0].col -= 1
625 let expected[1].col -= 1
626 call assert_equal(expected, prop_list(2))
627 call DeletePropTypes()
628
629 " split inside prop, split first prop
630 let expected = SetupOneLine() " 'xonex xtwoxx'
631 exe "normal 0lli\<CR>\<Esc>"
632 call assert_equal('xo', getline(1))
633 call assert_equal('nex xtwoxx', getline(2))
634 let exp_first = [deepcopy(expected[0])]
635 let exp_first[0].length = 1
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200636 let exp_first[0].end = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200637 call assert_equal(exp_first, prop_list(1))
638 let expected[0].col = 1
639 let expected[0].length = 2
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200640 let expected[0].start = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200641 let expected[1].col -= 2
642 call assert_equal(expected, prop_list(2))
643 call DeletePropTypes()
644
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200645 " split just after first prop, second prop move to next line
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200646 let expected = SetupOneLine() " 'xonex xtwoxx'
647 exe "normal 0fea\<CR>\<Esc>"
648 call assert_equal('xone', getline(1))
649 call assert_equal('x xtwoxx', getline(2))
650 let exp_first = expected[0:0]
651 call assert_equal(exp_first, prop_list(1))
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200652 let expected = expected[1:1]
653 let expected[0].col -= 4
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200654 call assert_equal(expected, prop_list(2))
655 call DeletePropTypes()
656
LemonBoy788c06a2022-05-14 18:48:05 +0100657 " split at the space character with 'ai' active, the leading space is removed
658 " in the second line and the prop is shifted accordingly.
659 let expected = SetupOneLine() " 'xonex xtwoxx'
660 set ai
661 exe "normal 6|i\<CR>\<Esc>"
662 call assert_equal('xonex', getline(1))
663 call assert_equal('xtwoxx', getline(2))
664 let expected[1].col -= 6
665 call assert_equal(expected, prop_list(1) + prop_list(2))
666 set ai&
667 call DeletePropTypes()
668
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200669 bwipe!
670 set bs&
671endfunc
672
Bram Moolenaarecb00c72022-08-07 14:55:14 +0100673func Test_prop_put()
674 new
675 let expected = SetupOneLine() " 'xonex xtwoxx'
676
677 let @a = 'new'
678 " insert just after the prop
679 normal 03l"ap
680 " insert inside the prop
681 normal 02l"ap
682 " insert just before the prop
683 normal 0"ap
684
685 call assert_equal('xnewonnewenewx xtwoxx', getline(1))
686 let expected[0].col += 3
687 let expected[0].length += 3
688 let expected[1].col += 9
689 call assert_equal(expected, prop_list(1))
690
691 " Visually select 4 chars in the prop and put "AB" to replace them
692 let @a = 'AB'
693 normal 05lv3l"ap
694 call assert_equal('xnewoABenewx xtwoxx', getline(1))
695 let expected[0].length -= 2
696 let expected[1].col -= 2
697 call assert_equal(expected, prop_list(1))
698
699 call DeletePropTypes()
700 bwipe!
701endfunc
702
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100703func Test_prop_clear()
704 new
705 call AddPropTypes()
706 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100707 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100708
Bram Moolenaara5a78822019-09-04 21:57:18 +0200709 eval 1->prop_clear()
710 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100711
712 call DeletePropTypes()
713 bwipe!
714endfunc
715
716func Test_prop_clear_buf()
717 new
718 call AddPropTypes()
719 call SetupPropsInFirstLine()
720 let bufnr = bufnr('')
721 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100722 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100723
724 call prop_clear(1, 1, {'bufnr': bufnr})
725 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
726
727 wincmd w
728 call DeletePropTypes()
729 bwipe!
730endfunc
731
Bram Moolenaar21b50382019-01-04 18:07:24 +0100732func Test_prop_setline()
733 new
734 call AddPropTypes()
735 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100736 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100737
738 call setline(1, 'foobar')
739 call assert_equal([], prop_list(1))
740
741 call DeletePropTypes()
742 bwipe!
743endfunc
744
745func Test_prop_setbufline()
746 new
747 call AddPropTypes()
748 call SetupPropsInFirstLine()
749 let bufnr = bufnr('')
750 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100751 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100752
753 call setbufline(bufnr, 1, 'foobar')
754 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
755
756 wincmd w
757 call DeletePropTypes()
758 bwipe!
759endfunc
760
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100761func Test_prop_substitute()
762 new
763 " Set first line to 'one two three'
764 call AddPropTypes()
765 call SetupPropsInFirstLine()
766 let expected_props = Get_expected_props()
767 call assert_equal(expected_props, prop_list(1))
768
769 " Change "n" in "one" to XX: 'oXXe two three'
770 s/n/XX/
771 let expected_props[0].length += 1
772 let expected_props[1].length += 1
773 let expected_props[2].col += 1
774 let expected_props[3].col += 1
775 call assert_equal(expected_props, prop_list(1))
776
777 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
778 s/t//g
779 let expected_props[0].length -= 2
780 let expected_props[2].length -= 1
781 let expected_props[3].length -= 1
782 let expected_props[3].col -= 1
783 call assert_equal(expected_props, prop_list(1))
784
785 " Split the line by changing w to line break: 'oXXe ', 'o hree'
786 " The long prop is split and spans both lines.
787 " The props on "two" and "three" move to the next line.
788 s/w/\r/
789 let new_props = [
790 \ copy(expected_props[0]),
791 \ copy(expected_props[2]),
792 \ copy(expected_props[3]),
793 \ ]
794 let expected_props[0].length = 5
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200795 let expected_props[0].end = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100796 unlet expected_props[3]
797 unlet expected_props[2]
798 call assert_equal(expected_props, prop_list(1))
799
800 let new_props[0].length = 6
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200801 let new_props[0].start = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100802 let new_props[1].col = 1
803 let new_props[1].length = 1
804 let new_props[2].col = 3
805 call assert_equal(new_props, prop_list(2))
806
807 call DeletePropTypes()
808 bwipe!
809endfunc
810
Bram Moolenaar663bc892019-01-08 23:07:24 +0100811func Test_prop_change_indent()
812 call prop_type_add('comment', {'highlight': 'Directory'})
813 new
814 call setline(1, [' xxx', 'yyyyy'])
815 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200816 let expect = #{type_bufnr: 0, col: 2, length: 2, type: 'comment', start: 1, end: 1, id: 0}
Bram Moolenaar663bc892019-01-08 23:07:24 +0100817 call assert_equal([expect], prop_list(2))
818
819 set shiftwidth=3
820 normal 2G>>
821 call assert_equal(' yyyyy', getline(2))
822 let expect.col += 3
823 call assert_equal([expect], prop_list(2))
824
825 normal 2G==
826 call assert_equal(' yyyyy', getline(2))
827 let expect.col = 6
828 call assert_equal([expect], prop_list(2))
829
830 call prop_clear(2)
831 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
832 let expect.col = 2
833 let expect.length = 5
834 call assert_equal([expect], prop_list(2))
835
836 normal 2G<<
837 call assert_equal(' yyyyy', getline(2))
838 let expect.length = 2
839 call assert_equal([expect], prop_list(2))
840
841 set shiftwidth&
842 call prop_type_delete('comment')
843endfunc
844
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100845" Setup a three line prop in lines 2 - 4.
846" Add short props in line 1 and 5.
847func Setup_three_line_prop()
848 new
849 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
850 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
851 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
852 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
853endfunc
854
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100855func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200856 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100857 new
858 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
859
860 " start halfway line 1, end halfway line 3
861 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200862 let expect1 = #{type_bufnr: 0, col: 3, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100863 call assert_equal([expect1], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200864 let expect2 = #{type_bufnr: 0, col: 1, length: 10, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100865 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200866 let expect3 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100867 call assert_equal([expect3], prop_list(3))
868 call prop_clear(1, 3)
869
870 " include all three lines
871 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
872 let expect1.col = 1
873 let expect1.length = 8
874 call assert_equal([expect1], prop_list(1))
875 call assert_equal([expect2], prop_list(2))
876 let expect3.length = 9
877 call assert_equal([expect3], prop_list(3))
878 call prop_clear(1, 3)
879
880 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100881
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100882 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100883 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200884 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 +0100885 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200886 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100887 call assert_equal([expect2], prop_list(2))
888 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100889 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200890 let expect2 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100891 call assert_equal([expect2], prop_list(2))
892 bwipe!
893
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100894 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100895 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200896 let expect3 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100897 call assert_equal([expect3], prop_list(3))
Martin Tournoije2390c72021-07-28 13:30:16 +0200898 let expect4 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100899 call assert_equal([expect4], prop_list(4))
900 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100901 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100902 call assert_equal([expect3], prop_list(3))
903 call assert_equal([expect_short], prop_list(4))
904 bwipe!
905
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100906 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100907 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200908 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100909 call assert_equal([expect2], prop_list(2))
910 call append(2, "new line")
911 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200912 let expect3 = #{type_bufnr: 0, col: 1, length: 9, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100913 call assert_equal([expect3], prop_list(3))
914 bwipe!
915
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100916 call prop_type_delete('comment')
917endfunc
918
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100919func Run_test_with_line2byte(add_props)
920 new
921 setlocal ff=unix
922 if a:add_props
923 call prop_type_add('textprop', #{highlight: 'Search'})
924 endif
925 for nr in range(1, 1000)
926 call setline(nr, 'some longer text here')
927 if a:add_props && nr % 17 == 0
928 call prop_add(nr, 13, #{type: 'textprop', length: 4})
929 endif
930 endfor
931 call assert_equal(21935, line2byte(998))
932 for nr in range(1, 1000, 7)
933 exe nr .. "s/longer/much more/"
934 endfor
Bram Moolenaare6a3d812022-08-08 18:12:30 +0100935 " FIXME: somehow this fails on MS-Windows
936 if !(a:add_props && has('win32'))
937 call assert_equal(22364, line2byte(998))
938 endif
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100939
940 if a:add_props
941 call prop_type_delete('textprop')
942 endif
943 bwipe!
944endfunc
945
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100946func Test_prop_line2byte()
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100947 call prop_type_add('comment', {'highlight': 'Directory'})
948 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100949 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100950 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100951 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100952 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100953 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100954 bwipe!
Bram Moolenaar14c75302021-08-15 14:28:40 +0200955
956 new
Bram Moolenaara401bba2021-08-15 15:04:41 +0200957 setlocal ff=unix
Bram Moolenaar14c75302021-08-15 14:28:40 +0200958 call setline(1, range(500))
959 call assert_equal(1491, line2byte(401))
960 call prop_add(2, 1, {'type': 'comment'})
961 call prop_add(222, 1, {'type': 'comment'})
962 call assert_equal(1491, line2byte(401))
963 call prop_remove({'type': 'comment'})
964 call assert_equal(1491, line2byte(401))
965 bwipe!
966
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +0200967 new
Bram Moolenaar49b93042021-08-25 17:02:00 +0200968 setlocal ff=unix
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +0200969 call setline(1, range(520))
970 call assert_equal(1491, line2byte(401))
971 call prop_add(2, 1, {'type': 'comment'})
972 call assert_equal(1491, line2byte(401))
973 2delete
974 call assert_equal(1489, line2byte(400))
975 bwipe!
976
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100977 " Add many lines so that the data block is split.
978 " With and without props should give the same result.
979 call Run_test_with_line2byte(0)
980 call Run_test_with_line2byte(1)
981
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100982 call prop_type_delete('comment')
983endfunc
984
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100985func Test_prop_byte2line()
986 new
987 set ff=unix
988 call setline(1, ['one one', 'two two', 'three three', 'four four', 'five'])
989 call assert_equal(4, byte2line(line2byte(4)))
990 call assert_equal(5, byte2line(line2byte(5)))
991
992 call prop_type_add('prop', {'highlight': 'Directory'})
993 call prop_add(3, 1, {'length': 5, 'type': 'prop'})
994 call assert_equal(4, byte2line(line2byte(4)))
995 call assert_equal(5, byte2line(line2byte(5)))
996
997 bwipe!
998 call prop_type_delete('prop')
999endfunc
1000
Bram Moolenaar59ff6402021-01-30 17:16:28 +01001001func Test_prop_goto_byte()
1002 new
1003 call setline(1, '')
1004 call setline(2, 'two three')
1005 call setline(3, '')
1006 call setline(4, 'four five')
1007
1008 call prop_type_add('testprop', {'highlight': 'Directory'})
1009 call search('^two')
1010 call prop_add(line('.'), col('.'), {
1011 \ 'length': len('two'),
1012 \ 'type': 'testprop'
1013 \ })
1014
1015 call search('two \zsthree')
1016 let expected_pos = line2byte(line('.')) + col('.') - 1
1017 exe expected_pos .. 'goto'
1018 let actual_pos = line2byte(line('.')) + col('.') - 1
1019 eval actual_pos->assert_equal(expected_pos)
1020
1021 call search('four \zsfive')
1022 let expected_pos = line2byte(line('.')) + col('.') - 1
1023 exe expected_pos .. 'goto'
1024 let actual_pos = line2byte(line('.')) + col('.') - 1
1025 eval actual_pos->assert_equal(expected_pos)
1026
1027 call prop_type_delete('testprop')
1028 bwipe!
1029endfunc
1030
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001031func Test_prop_undo()
1032 new
1033 call prop_type_add('comment', {'highlight': 'Directory'})
1034 call setline(1, ['oneone', 'twotwo', 'three'])
1035 " Set 'undolevels' to break changes into undo-able pieces.
1036 set ul&
1037
1038 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001039 let expected = [#{type_bufnr: 0, col: 3, length: 2, id: 0, type: 'comment', start: 1, end: 1}]
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001040 call assert_equal(expected, prop_list(1))
1041
1042 " Insert a character, then undo.
1043 exe "normal 0lllix\<Esc>"
1044 set ul&
1045 let expected[0].length = 3
1046 call assert_equal(expected, prop_list(1))
1047 undo
1048 let expected[0].length = 2
1049 call assert_equal(expected, prop_list(1))
1050
1051 " Delete a character, then undo
1052 exe "normal 0lllx"
1053 set ul&
1054 let expected[0].length = 1
1055 call assert_equal(expected, prop_list(1))
1056 undo
1057 let expected[0].length = 2
1058 call assert_equal(expected, prop_list(1))
1059
1060 " Delete the line, then undo
1061 1d
1062 set ul&
1063 call assert_equal([], prop_list(1))
1064 undo
1065 call assert_equal(expected, prop_list(1))
1066
1067 " Insert a character, delete two characters, then undo with "U"
1068 exe "normal 0lllix\<Esc>"
1069 set ul&
1070 let expected[0].length = 3
1071 call assert_equal(expected, prop_list(1))
1072 exe "normal 0lllxx"
1073 set ul&
1074 let expected[0].length = 1
1075 call assert_equal(expected, prop_list(1))
1076 normal U
1077 let expected[0].length = 2
1078 call assert_equal(expected, prop_list(1))
1079
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001080 " substitute a word, then undo
1081 call setline(1, 'the number 123 is highlighted.')
1082 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001083 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001084 call assert_equal(expected, prop_list(1))
1085 set ul&
1086 1s/number/foo
1087 let expected[0].col = 9
1088 call assert_equal(expected, prop_list(1))
1089 undo
1090 let expected[0].col = 12
1091 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001092 call prop_clear(1)
1093
1094 " substitute with backslash
1095 call setline(1, 'the number 123 is highlighted.')
1096 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001097 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001098 call assert_equal(expected, prop_list(1))
1099 1s/the/\The
1100 call assert_equal(expected, prop_list(1))
1101 1s/^/\\
1102 let expected[0].col += 1
1103 call assert_equal(expected, prop_list(1))
1104 1s/^/\~
1105 let expected[0].col += 1
1106 call assert_equal(expected, prop_list(1))
1107 1s/123/12\\3
1108 let expected[0].length += 1
1109 call assert_equal(expected, prop_list(1))
1110 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001111
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001112 bwipe!
1113 call prop_type_delete('comment')
1114endfunc
1115
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001116func Test_prop_delete_text()
1117 new
1118 call prop_type_add('comment', {'highlight': 'Directory'})
1119 call setline(1, ['oneone', 'twotwo', 'three'])
1120
1121 " zero length property
1122 call prop_add(1, 3, {'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001123 let expected = [#{type_bufnr: 0, col: 3, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001124 call assert_equal(expected, prop_list(1))
1125
1126 " delete one char moves the property
1127 normal! x
Martin Tournoije2390c72021-07-28 13:30:16 +02001128 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001129 call assert_equal(expected, prop_list(1))
1130
1131 " delete char of the property has no effect
1132 normal! lx
Martin Tournoije2390c72021-07-28 13:30:16 +02001133 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001134 call assert_equal(expected, prop_list(1))
1135
1136 " delete more chars moves property to first column, is not deleted
1137 normal! 0xxxx
Martin Tournoije2390c72021-07-28 13:30:16 +02001138 let expected = [#{type_bufnr: 0, col: 1, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001139 call assert_equal(expected, prop_list(1))
1140
1141 bwipe!
1142 call prop_type_delete('comment')
1143endfunc
1144
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001145" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +02001146func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +02001147 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +01001148 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +02001149 if g:orig_encoding != 'utf-8'
1150 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001151 endif
1152 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +02001153 \ "call setline(1, ["
1154 \ .. "'One two',"
1155 \ .. "'Numbér 123 änd thœn 4¾7.',"
1156 \ .. "'--aa--bb--cc--dd--',"
1157 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001158 \ .. "'first line',"
1159 \ .. "' second line ',"
1160 \ .. "'third line',"
1161 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +02001162 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001163 \ "hi NumberProp ctermfg=blue",
1164 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001165 \ "hi BackgroundProp ctermbg=lightgrey",
1166 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001167 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +02001168 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
1169 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001170 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
1171 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
1172 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001173 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
1174 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
1175 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +01001176 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001177 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001178 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
1179 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001180 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
1181 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
1182 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
1183 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001184 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
1185 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001186 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001187 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
1188 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
1189 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
1190 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +02001191 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001192 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001193 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001194 \ "syn match Comment '//.*'",
1195 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001196 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01001197 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001198 \ "normal 6G0i\<BS>\<Esc>",
1199 \ "normal 3J",
1200 \ "normal 3G",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001201 \], 'XtestProp')
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001202 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001203 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +01001204
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001205 " clean up
1206 call StopVimInTerminal(buf)
Bram Moolenaar2f21fa82018-12-31 20:05:56 +01001207 call delete('XtestProp')
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001208endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +02001209
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001210func Test_textprop_hl_override()
1211 CheckScreendump
1212
1213 let lines =<< trim END
1214 call setline(1, ['One one one one one', 'Two two two two two', 'Three three three three'])
1215 hi OverProp ctermfg=blue ctermbg=yellow
1216 hi CursorLine cterm=bold,underline ctermfg=red ctermbg=green
1217 hi Vsual ctermfg=cyan ctermbg=grey
1218 call prop_type_add('under', #{highlight: 'OverProp'})
1219 call prop_type_add('over', #{highlight: 'OverProp', override: 1})
1220 call prop_add(1, 5, #{type: 'under', length: 4})
1221 call prop_add(1, 13, #{type: 'over', length: 4})
1222 call prop_add(2, 5, #{type: 'under', length: 4})
1223 call prop_add(2, 13, #{type: 'over', length: 4})
1224 call prop_add(3, 5, #{type: 'under', length: 4})
1225 call prop_add(3, 13, #{type: 'over', length: 4})
1226 set cursorline
1227 2
1228 END
1229 call writefile(lines, 'XtestOverProp')
1230 let buf = RunVimInTerminal('-S XtestOverProp', {'rows': 8})
1231 call VerifyScreenDump(buf, 'Test_textprop_hl_override_1', {})
1232
1233 call term_sendkeys(buf, "3Gllv$hh")
1234 call VerifyScreenDump(buf, 'Test_textprop_hl_override_2', {})
1235 call term_sendkeys(buf, "\<Esc>")
1236
1237 " clean up
1238 call StopVimInTerminal(buf)
1239 call delete('XtestOverProp')
1240endfunc
1241
Bram Moolenaar8055d172019-05-17 22:57:26 +02001242func RunTestVisualBlock(width, dump)
1243 call writefile([
1244 \ "call setline(1, ["
1245 \ .. "'xxxxxxxxx 123 x',"
1246 \ .. "'xxxxxxxx 123 x',"
1247 \ .. "'xxxxxxx 123 x',"
1248 \ .. "'xxxxxx 123 x',"
1249 \ .. "'xxxxx 123 x',"
1250 \ .. "'xxxx 123 xx',"
1251 \ .. "'xxx 123 xxx',"
1252 \ .. "'xx 123 xxxx',"
1253 \ .. "'x 123 xxxxx',"
1254 \ .. "' 123 xxxxxx',"
1255 \ .. "])",
1256 \ "hi SearchProp ctermbg=yellow",
1257 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
1258 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
1259 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
1260 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
1261 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
1262 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
1263 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
1264 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
1265 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
1266 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
1267 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
1268 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
1269 \], 'XtestPropVis')
1270 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
1271 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
1272
1273 " clean up
1274 call StopVimInTerminal(buf)
1275 call delete('XtestPropVis')
1276endfunc
1277
1278" screenshot test with Visual block mode operations
1279func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +02001280 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +02001281
1282 " Delete two columns while text props are three chars wide.
1283 call RunTestVisualBlock(2, '01')
1284
1285 " Same, but delete four columns
1286 call RunTestVisualBlock(4, '02')
1287endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +02001288
Bram Moolenaara956bf62019-06-19 17:34:24 +02001289func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +02001290 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +02001291
Bram Moolenaara956bf62019-06-19 17:34:24 +02001292 let lines =<< trim END
1293 call setline(1, [
1294 \ "\txxx",
1295 \ "x\txxx",
1296 \ ])
1297 hi SearchProp ctermbg=yellow
1298 call prop_type_add('search', {'highlight': 'SearchProp'})
1299 call prop_add(1, 2, {'length': 3, 'type': 'search'})
1300 call prop_add(2, 3, {'length': 3, 'type': 'search'})
1301 END
1302 call writefile(lines, 'XtestPropTab')
1303 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
1304 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
1305
1306 " clean up
1307 call StopVimInTerminal(buf)
1308 call delete('XtestPropTab')
1309endfunc
1310
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001311func Test_textprop_nowrap_scrolled()
1312 CheckScreendump
1313
1314 let lines =<< trim END
1315 vim9script
1316 set nowrap
1317 setline(1, 'The number 123 is smaller than 4567.' .. repeat('X', &columns))
1318 prop_type_add('number', {'highlight': 'ErrorMsg'})
1319 prop_add(1, 12, {'length': 3, 'type': 'number'})
1320 prop_add(1, 32, {'length': 4, 'type': 'number'})
1321 feedkeys('gg20zl', 'nxt')
1322 END
1323 call writefile(lines, 'XtestNowrap')
1324 let buf = RunVimInTerminal('-S XtestNowrap', {'rows': 6})
1325 call VerifyScreenDump(buf, 'Test_textprop_nowrap_01', {})
1326
1327 call term_sendkeys(buf, "$")
1328 call VerifyScreenDump(buf, 'Test_textprop_nowrap_02', {})
1329
1330 " clean up
1331 call StopVimInTerminal(buf)
1332 call delete('XtestNowrap')
1333endfunc
1334
Bram Moolenaar34390282019-10-16 14:38:26 +02001335func Test_textprop_with_syntax()
1336 CheckScreendump
1337
1338 let lines =<< trim END
1339 call setline(1, [
1340 \ "(abc)",
1341 \ ])
1342 syn match csParens "[()]" display
1343 hi! link csParens MatchParen
1344
1345 call prop_type_add('TPTitle', #{ highlight: 'Title' })
1346 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
1347 END
1348 call writefile(lines, 'XtestPropSyn')
1349 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
1350 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
1351
1352 " clean up
1353 call StopVimInTerminal(buf)
1354 call delete('XtestPropSyn')
1355endfunc
1356
Bram Moolenaard79eef22019-05-24 20:41:55 +02001357" Adding a text property to a new buffer should not fail
1358func Test_textprop_empty_buffer()
1359 call prop_type_add('comment', {'highlight': 'Search'})
1360 new
1361 call prop_add(1, 1, {'type': 'comment'})
1362 close
Bram Moolenaaradfde112019-05-25 22:11:45 +02001363 call prop_type_delete('comment')
1364endfunc
1365
Bram Moolenaard74af422019-06-28 21:38:00 +02001366" Adding a text property with invalid highlight should be ignored.
1367func Test_textprop_invalid_highlight()
1368 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
1369 new
1370 call setline(1, ['asdf','asdf'])
1371 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
1372 redraw
1373 bwipe!
1374 call prop_type_delete('dni')
1375endfunc
1376
Bram Moolenaaradfde112019-05-25 22:11:45 +02001377" Adding a text property to an empty buffer and then editing another
1378func Test_textprop_empty_buffer_next()
1379 call prop_type_add("xxx", {})
1380 call prop_add(1, 1, {"type": "xxx"})
1381 next X
1382 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +02001383endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001384
1385func Test_textprop_remove_from_buf()
1386 new
1387 let buf = bufnr('')
1388 call prop_type_add('one', {'bufnr': buf})
1389 call prop_add(1, 1, {'type': 'one', 'id': 234})
1390 file x
1391 edit y
1392 call prop_remove({'id': 234, 'bufnr': buf}, 1)
1393 call prop_type_delete('one', {'bufnr': buf})
1394 bwipe! x
1395 close
1396endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +02001397
1398func Test_textprop_in_unloaded_buf()
1399 edit Xaaa
1400 call setline(1, 'aaa')
1401 write
1402 edit Xbbb
1403 call setline(1, 'bbb')
1404 write
1405 let bnr = bufnr('')
1406 edit Xaaa
1407
1408 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
1409 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
1410 exe 'buf ' .. bnr
1411 call assert_equal('bbb', getline(1))
1412 call assert_equal(0, prop_list(1)->len())
1413
1414 bwipe! Xaaa
1415 bwipe! Xbbb
1416 cal delete('Xaaa')
1417 cal delete('Xbbb')
1418endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001419
1420func Test_proptype_substitute2()
1421 new
1422 " text_prop.vim
1423 call setline(1, [
1424 \ 'The num 123 is smaller than 4567.',
1425 \ '123 The number 123 is smaller than 4567.',
1426 \ '123 The number 123 is smaller than 4567.'])
1427
1428 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1429
1430 call prop_add(1, 12, {'length': 3, 'type': 'number'})
1431 call prop_add(2, 1, {'length': 3, 'type': 'number'})
1432 call prop_add(3, 36, {'length': 4, 'type': 'number'})
1433 set ul&
Martin Tournoije2390c72021-07-28 13:30:16 +02001434 let expected = [
1435 \ #{type_bufnr: 0, id: 0, col: 13, end: 1, type: 'number', length: 3, start: 1},
1436 \ #{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'number', length: 3, start: 1},
1437 \ #{type_bufnr: 0, id: 0, col: 50, end: 1, type: 'number', length: 4, start: 1}]
1438
1439 " TODO
Bram Moolenaar213bbaf2022-08-05 19:46:48 +01001440 if 0
1441 " Add some text in between
1442 %s/\s\+/ /g
1443 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001444
Bram Moolenaar213bbaf2022-08-05 19:46:48 +01001445 " remove some text
1446 :1s/[a-z]\{3\}//g
1447 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
1448 call assert_equal(expected, prop_list(1))
1449 endif
1450
1451 call prop_type_delete('number')
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001452 bwipe!
1453endfunc
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001454
Bram Moolenaar8902b312020-09-20 21:04:35 +02001455" This was causing property corruption.
1456func Test_proptype_substitute3()
1457 new
1458 call setline(1, ['abcxxx', 'def'])
1459 call prop_type_add("test", {"highlight": "Search"})
1460 call prop_add(1, 2, {"end_lnum": 2, "end_col": 2, "type": "test"})
1461 %s/x\+$//
1462 redraw
1463
1464 call prop_type_delete('test')
1465 bwipe!
1466endfunc
1467
Bram Moolenaar213bbaf2022-08-05 19:46:48 +01001468func Test_proptype_substitute_join()
1469 new
1470 call setline(1, [
1471 \ 'This is some end',
1472 \ 'start is highlighted end',
1473 \ 'some is highlighted',
1474 \ 'start is also highlighted'])
1475
1476 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1477
1478 call prop_add(1, 6, {'length': 2, 'type': 'number'})
1479 call prop_add(2, 7, {'length': 2, 'type': 'number'})
1480 call prop_add(3, 6, {'length': 2, 'type': 'number'})
1481 call prop_add(4, 7, {'length': 2, 'type': 'number'})
1482 " The highlighted "is" in line 1, 2 and 4 is kept and ajudsted.
1483 " The highlighted "is" in line 3 is deleted.
1484 let expected = [
1485 \ #{type_bufnr: 0, id: 0, col: 6, end: 1, type: 'number', length: 2, start: 1},
1486 \ #{type_bufnr: 0, id: 0, col: 21, end: 1, type: 'number', length: 2, start: 1},
1487 \ #{type_bufnr: 0, id: 0, col: 43, end: 1, type: 'number', length: 2, start: 1}]
1488
1489 s/end\nstart/joined/
1490 s/end\n.*\nstart/joined/
1491 call assert_equal('This is some joined is highlighted joined is also highlighted', getline(1))
1492 call assert_equal(expected, prop_list(1))
1493
1494 call prop_type_delete('number')
1495 bwipe!
1496endfunc
1497
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001498func SaveOptions()
1499 let d = #{tabstop: &tabstop,
1500 \ softtabstop: &softtabstop,
1501 \ shiftwidth: &shiftwidth,
1502 \ expandtab: &expandtab,
1503 \ foldmethod: '"' .. &foldmethod .. '"',
1504 \ }
1505 return d
1506endfunc
1507
1508func RestoreOptions(dict)
1509 for name in keys(a:dict)
1510 exe 'let &' .. name .. ' = ' .. a:dict[name]
1511 endfor
1512endfunc
1513
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001514func Test_textprop_noexpandtab()
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001515 new
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001516 let save_dict = SaveOptions()
1517
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001518 set tabstop=8
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001519 set softtabstop=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001520 set shiftwidth=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001521 set noexpandtab
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001522 set foldmethod=marker
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001523
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001524 call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx")
1525 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1526 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1527 call feedkeys("0i\<tab>", "tx")
1528 call prop_remove({'type': 'test'})
1529 call prop_add(1, 2, {'end_col': 3, 'type': 'test'})
1530 call feedkeys("A\<left>\<tab>", "tx")
1531 call prop_remove({'type': 'test'})
1532 try
1533 " It is correct that this does not pass
1534 call prop_add(1, 6, {'end_col': 7, 'type': 'test'})
1535 " Has already collapsed here, start_col:6 does not result in an error
1536 call feedkeys("A\<left>\<tab>", "tx")
1537 catch /^Vim\%((\a\+)\)\=:E964/
1538 endtry
1539 call prop_remove({'type': 'test'})
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001540 call prop_type_delete('test')
1541
1542 call RestoreOptions(save_dict)
1543 bwipe!
1544endfunc
1545
1546func Test_textprop_noexpandtab_redraw()
1547 new
1548 let save_dict = SaveOptions()
1549
1550 set tabstop=8
1551 set softtabstop=4
1552 set shiftwidth=4
1553 set noexpandtab
1554 set foldmethod=marker
1555
1556 call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx")
1557 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1558 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1559 call feedkeys("0i\<tab>", "tx")
1560 " Internally broken at the next line
1561 call feedkeys("A\<left>\<tab>", "tx")
1562 redraw
1563 " Index calculation failed internally on next line
1564 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1565 call prop_remove({'type': 'test', 'all': v:true})
1566 call prop_type_delete('test')
1567 call prop_type_delete('test')
1568
1569 call RestoreOptions(save_dict)
1570 bwipe!
1571endfunc
1572
1573func Test_textprop_ins_str()
1574 new
1575 call setline(1, 'just some text')
1576 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1577 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001578 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 +01001579
1580 call feedkeys("foi\<F8>\<Esc>", "tx")
1581 call assert_equal('just s<F8>ome text', getline(1))
Martin Tournoije2390c72021-07-28 13:30:16 +02001582 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 +01001583
1584 bwipe!
1585 call prop_remove({'type': 'test'})
1586 call prop_type_delete('test')
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001587endfunc
Bram Moolenaar66b98852020-03-11 19:15:52 +01001588
1589func Test_find_prop_later_in_line()
1590 new
1591 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1592 call setline(1, 'just some text')
1593 call prop_add(1, 1, {'length': 4, 'type': 'test'})
1594 call prop_add(1, 10, {'length': 3, 'type': 'test'})
1595
Martin Tournoije2390c72021-07-28 13:30:16 +02001596 call assert_equal(
1597 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 10, end: 1, type: 'test', length: 3, start: 1},
1598 \ prop_find(#{type: 'test', lnum: 1, col: 6}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001599
1600 bwipe!
1601 call prop_type_delete('test')
1602endfunc
1603
1604func Test_find_zerowidth_prop_sol()
1605 new
1606 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1607 call setline(1, 'just some text')
1608 call prop_add(1, 1, {'length': 0, 'type': 'test'})
1609
Martin Tournoije2390c72021-07-28 13:30:16 +02001610 call assert_equal(
1611 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 1, end: 1, type: 'test', length: 0, start: 1},
1612 \ prop_find(#{type: 'test', lnum: 1}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001613
1614 bwipe!
1615 call prop_type_delete('test')
1616endfunc
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001617
1618" Test for passing invalid arguments to prop_xxx() functions
1619func Test_prop_func_invalid_args()
1620 call assert_fails('call prop_clear(1, 2, [])', 'E715:')
1621 call assert_fails('call prop_clear(-1, 2)', 'E16:')
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02001622 call assert_fails('call prop_find(test_null_dict())', 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001623 call assert_fails('call prop_find({"bufnr" : []})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001624 call assert_fails('call prop_find({})', 'E968:')
1625 call assert_fails('call prop_find({}, "x")', 'E474:')
1626 call assert_fails('call prop_find({"lnum" : -2})', 'E16:')
1627 call assert_fails('call prop_list(1, [])', 'E715:')
Bram Moolenaar9d489562020-07-30 20:08:50 +02001628 call assert_fails('call prop_list(-1, {})', 'E16:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001629 call assert_fails('call prop_remove([])', 'E474:')
1630 call assert_fails('call prop_remove({}, -2)', 'E16:')
1631 call assert_fails('call prop_remove({})', 'E968:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001632 call assert_fails('call prop_type_add([], {})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001633 call assert_fails("call prop_type_change('long', {'xyz' : 10})", 'E971:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001634 call assert_fails("call prop_type_delete([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001635 call assert_fails("call prop_type_delete('xyz', [])", 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001636 call assert_fails("call prop_type_get([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001637 call assert_fails("call prop_type_get('', [])", 'E474:')
1638 call assert_fails("call prop_type_list([])", 'E715:')
Bram Moolenaar3dc34742021-03-02 13:36:47 +01001639 call assert_fails("call prop_type_add('yyy', 'not_a_dict')", 'E715:')
1640 call assert_fails("call prop_add(1, 5, {'type':'missing_type', 'length':1})", 'E971:')
1641 call assert_fails("call prop_add(1, 5, {'type': ''})", 'E971:')
1642 call assert_fails('call prop_add(1, 1, 0)', 'E715:')
1643
1644 new
1645 call setline(1, ['first', 'second'])
1646 call prop_type_add('xxx', {})
1647
1648 call assert_fails("call prop_type_add('xxx', {})", 'E969:')
1649 call assert_fails("call prop_add(2, 0, {'type': 'xxx'})", 'E964:')
1650 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':1})", 'E475:')
1651 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':3})", 'E966:')
1652 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'length':-1})", 'E475:')
1653 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_col':0})", 'E475:')
1654 call assert_fails("call prop_add(2, 3, {'length':1})", 'E965:')
1655
1656 call prop_type_delete('xxx')
1657 bwipe!
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001658endfunc
1659
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001660func Test_prop_split_join()
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001661 new
1662 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1663 call setline(1, 'just some text')
1664 call prop_add(1, 6, {'length': 4, 'type': 'test'})
1665
1666 " Split in middle of "some"
1667 execute "normal! 8|i\<CR>"
Martin Tournoije2390c72021-07-28 13:30:16 +02001668 call assert_equal(
1669 \ [#{type_bufnr: 0, id: 0, col: 6, end: 0, type: 'test', length: 2, start: 1}],
1670 \ prop_list(1))
1671 call assert_equal(
1672 \ [#{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'test', length: 2, start: 0}],
1673 \ prop_list(2))
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001674
1675 " Join the two lines back together
1676 normal! 1GJ
Martin Tournoije2390c72021-07-28 13:30:16 +02001677 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 +02001678
1679 bwipe!
1680 call prop_type_delete('test')
1681endfunc
1682
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001683func Test_prop_increment_decrement()
1684 new
1685 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1686 call setline(1, 'its 998 times')
1687 call prop_add(1, 5, {'length': 3, 'type': 'test'})
1688
1689 exe "normal! 0f9\<C-A>"
1690 eval getline(1)->assert_equal('its 999 times')
1691 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001692 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 3, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001693
1694 exe "normal! 0f9\<C-A>"
1695 eval getline(1)->assert_equal('its 1000 times')
1696 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001697 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 4, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001698
1699 bwipe!
1700 call prop_type_delete('test')
1701endfunc
1702
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001703func Test_prop_block_insert()
1704 new
1705 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1706 call setline(1, ['one ', 'two '])
1707 call prop_add(1, 1, {'length': 3, 'type': 'test'})
1708 call prop_add(2, 1, {'length': 3, 'type': 'test'})
1709
1710 " insert "xx" in the first column of both lines
1711 exe "normal! gg0\<C-V>jIxx\<Esc>"
1712 eval getline(1, 2)->assert_equal(['xxone ', 'xxtwo '])
Martin Tournoije2390c72021-07-28 13:30:16 +02001713 let expected = [#{type_bufnr: 0, id: 0, col: 3, end: 1, type: 'test', length: 3, start: 1}]
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001714 eval prop_list(1)->assert_equal(expected)
1715 eval prop_list(2)->assert_equal(expected)
1716
1717 " insert "yy" inside the text props to make them longer
1718 exe "normal! gg03l\<C-V>jIyy\<Esc>"
1719 eval getline(1, 2)->assert_equal(['xxoyyne ', 'xxtyywo '])
1720 let expected[0].length = 5
1721 eval prop_list(1)->assert_equal(expected)
1722 eval prop_list(2)->assert_equal(expected)
1723
1724 " insert "zz" after the text props, text props don't change
1725 exe "normal! gg07l\<C-V>jIzz\<Esc>"
1726 eval getline(1, 2)->assert_equal(['xxoyynezz ', 'xxtyywozz '])
1727 eval prop_list(1)->assert_equal(expected)
1728 eval prop_list(2)->assert_equal(expected)
1729
1730 bwipe!
1731 call prop_type_delete('test')
1732endfunc
1733
Bram Moolenaar23999d72020-12-23 14:36:00 +01001734" this was causing an ml_get error because w_botline was wrong
1735func Test_prop_one_line_window()
1736 enew
1737 call range(2)->setline(1)
1738 call prop_type_add('testprop', {})
1739 call prop_add(1, 1, {'type': 'testprop'})
1740 call popup_create('popup', {'textprop': 'testprop'})
1741 $
1742 new
1743 wincmd _
1744 call feedkeys("\r", 'xt')
1745 redraw
1746
1747 call popup_clear()
1748 call prop_type_delete('testprop')
1749 close
1750 bwipe!
1751endfunc
1752
Bram Moolenaarf05a1e52022-08-02 11:48:53 +01001753def Test_prop_column_zero_error()
1754 prop_type_add('proptype', {highlight: 'Search'})
1755 var caught = false
1756 try
1757 popup_create([{
1758 text: 'a',
1759 props: [{col: 0, length: 1, type: 'type'}],
1760 }], {})
1761 catch /E964:/
1762 caught = true
1763 endtry
1764 assert_true(caught)
1765
1766 popup_clear()
1767 prop_type_delete('proptype')
1768enddef
1769
Bram Moolenaar840f91f2021-05-26 22:32:10 +02001770" This was calling ml_append_int() and copy a text property from a previous
1771" line at the wrong moment. Exact text length matters.
1772def Test_prop_splits_data_block()
1773 new
1774 var lines: list<string> = [repeat('x', 35)]->repeat(41)
1775 + [repeat('!', 35)]
1776 + [repeat('x', 35)]->repeat(56)
1777 lines->setline(1)
1778 prop_type_add('someprop', {highlight: 'ErrorMsg'})
1779 prop_add(1, 27, {end_lnum: 1, end_col: 70, type: 'someprop'})
1780 prop_remove({type: 'someprop'}, 1)
1781 prop_add(35, 22, {end_lnum: 43, end_col: 43, type: 'someprop'})
1782 prop_remove({type: 'someprop'}, 35, 43)
1783 assert_equal([], prop_list(42))
1784
1785 bwipe!
1786 prop_type_delete('someprop')
1787enddef
1788
Bram Moolenaar4cd5c522021-06-27 13:04:00 +02001789" This was calling ml_delete_int() and try to change text properties.
1790def Test_prop_add_delete_line()
1791 new
1792 var a = 10
1793 var b = 20
1794 repeat([''], a)->append('$')
1795 prop_type_add('Test', {highlight: 'ErrorMsg'})
1796 for lnum in range(1, a)
1797 for col in range(1, b)
1798 prop_add(1, 1, {end_lnum: lnum, end_col: col, type: 'Test'})
1799 endfor
1800 endfor
1801
1802 # check deleting lines is OK
1803 :5del
1804 :1del
1805 :$del
1806
1807 prop_type_delete('Test')
1808 bwipe!
1809enddef
1810
Paul Ollis1bdc60e2022-05-15 22:24:55 +01001811" This test is to detect a regression related to #10430. It is not an attempt
1812" fully cover deleting lines in the presence of multi-line properties.
1813def Test_delete_line_within_multiline_prop()
1814 new
1815 setline(1, '# Top.')
1816 append(1, ['some_text = """', 'A string.', '"""', '# Bottom.'])
1817 prop_type_add('Identifier', {'highlight': 'ModeMsg', 'priority': 0, 'combine': 0, 'start_incl': 0, 'end_incl': 0})
1818 prop_type_add('String', {'highlight': 'MoreMsg', 'priority': 0, 'combine': 0, 'start_incl': 0, 'end_incl': 0})
1819 prop_add(2, 1, {'type': 'Identifier', 'end_lnum': 2, 'end_col': 9})
1820 prop_add(2, 13, {'type': 'String', 'end_lnum': 4, 'end_col': 4})
1821
1822 # The property for line 3 should extend into the previous and next lines.
1823 var props = prop_list(3)
1824 var prop = props[0]
1825 assert_equal(1, len(props))
1826 assert_equal(0, prop['start'])
1827 assert_equal(0, prop['end'])
1828
1829 # This deletion should run without raising an exception.
1830 try
1831 :2 del
1832 catch
1833 assert_report('Line delete should have workd, but it raised an error.')
1834 endtry
1835
1836 # The property for line 2 (was 3) should no longer extend into the previous
1837 # line.
1838 props = prop_list(2)
1839 prop = props[0]
1840 assert_equal(1, len(props))
1841 assert_equal(1, prop['start'], 'Property was not changed to start within the line.')
1842
1843 # This deletion should run without raising an exception.
1844 try
1845 :3 del
1846 catch
1847 assert_report('Line delete should have workd, but it raised an error.')
1848 endtry
1849
1850 # The property for line 2 (originally 3) should no longer extend into the next
1851 # line.
1852 props = prop_list(2)
1853 prop = props[0]
1854 assert_equal(1, len(props))
1855 assert_equal(1, prop['end'], 'Property was not changed to end within the line.')
1856
1857 prop_type_delete('Identifier')
1858 prop_type_delete('String')
1859 bwip!
1860enddef
1861
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001862func Test_prop_in_linebreak()
1863 CheckRunVimInTerminal
1864
1865 let lines =<< trim END
1866 set breakindent linebreak breakat+=]
1867 call printf('%s]%s', repeat('x', 50), repeat('x', 70))->setline(1)
1868 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1869 call prop_add(1, 51, #{length: 1, type: 'test'})
1870 END
1871 call writefile(lines, 'XscriptPropLinebreak')
1872 let buf = RunVimInTerminal('-S XscriptPropLinebreak', #{rows: 10})
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001873 call VerifyScreenDump(buf, 'Test_prop_linebreak', {})
1874
1875 call StopVimInTerminal(buf)
1876 call delete('XscriptPropLinebreak')
1877endfunc
1878
Bram Moolenaar42eba042021-11-30 20:22:49 +00001879func Test_prop_after_tab()
1880 CheckRunVimInTerminal
1881
1882 let lines =<< trim END
1883 set breakindent linebreak breakat+=]
1884 call setline(1, "\t[xxx]")
1885 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1886 call prop_add(1, 2, #{length: 1, type: 'test'})
1887 END
1888 call writefile(lines, 'XscriptPropAfterTab')
1889 let buf = RunVimInTerminal('-S XscriptPropAfterTab', #{rows: 10})
Bram Moolenaar42eba042021-11-30 20:22:49 +00001890 call VerifyScreenDump(buf, 'Test_prop_after_tab', {})
1891
1892 call StopVimInTerminal(buf)
1893 call delete('XscriptPropAfterTab')
1894endfunc
1895
Bram Moolenaare428fa02022-08-09 16:55:41 +01001896func Test_prop_before_tab()
1897 CheckRunVimInTerminal
1898
1899 let lines =<< trim END
1900 call setline(1, ["\tx"]->repeat(6))
1901 call prop_type_add('test', #{highlight: 'Search'})
1902 call prop_add(1, 1, #{type: 'test', text: '123'})
1903 call prop_add(2, 1, #{type: 'test', text: '1234567'})
1904 call prop_add(3, 1, #{type: 'test', text: '12345678'})
1905 call prop_add(4, 1, #{type: 'test', text: '123456789'})
1906 call prop_add(5, 2, #{type: 'test', text: 'ABC'})
1907 call prop_add(6, 3, #{type: 'test', text: 'ABC'})
1908 normal gg0
1909 END
1910 call writefile(lines, 'XscriptPropBeforeTab')
1911 let buf = RunVimInTerminal('-S XscriptPropBeforeTab', #{rows: 8})
1912 call VerifyScreenDump(buf, 'Test_prop_before_tab_01', {})
1913 call term_sendkeys(buf, "$")
1914 call VerifyScreenDump(buf, 'Test_prop_before_tab_02', {})
1915 call term_sendkeys(buf, "j0")
1916 call VerifyScreenDump(buf, 'Test_prop_before_tab_03', {})
1917 call term_sendkeys(buf, "$")
1918 call VerifyScreenDump(buf, 'Test_prop_before_tab_04', {})
1919 call term_sendkeys(buf, "j0")
1920 call VerifyScreenDump(buf, 'Test_prop_before_tab_05', {})
1921 call term_sendkeys(buf, "$")
1922 call VerifyScreenDump(buf, 'Test_prop_before_tab_06', {})
1923 call term_sendkeys(buf, "j0")
1924 call VerifyScreenDump(buf, 'Test_prop_before_tab_07', {})
1925 call term_sendkeys(buf, "$")
1926 call VerifyScreenDump(buf, 'Test_prop_before_tab_08', {})
1927 call term_sendkeys(buf, "j")
1928 call VerifyScreenDump(buf, 'Test_prop_before_tab_09', {})
1929 call term_sendkeys(buf, "j")
1930 call VerifyScreenDump(buf, 'Test_prop_before_tab_10', {})
1931
1932 call StopVimInTerminal(buf)
1933 call delete('XscriptPropBeforeTab')
1934endfunc
1935
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001936func Test_prop_after_linebreak()
1937 CheckRunVimInTerminal
1938
1939 let lines =<< trim END
1940 set linebreak wrap
1941 call printf('%s+(%s)', 'x'->repeat(&columns / 2), 'x'->repeat(&columns / 2))->setline(1)
1942 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1943 call prop_add(1, (&columns / 2) + 2, #{length: 1, type: 'test'})
1944 END
1945 call writefile(lines, 'XscriptPropAfterLinebreak')
1946 let buf = RunVimInTerminal('-S XscriptPropAfterLinebreak', #{rows: 10})
Bram Moolenaaracdc9112021-12-02 19:46:57 +00001947 call VerifyScreenDump(buf, 'Test_prop_after_linebreak', {})
1948
1949 call StopVimInTerminal(buf)
1950 call delete('XscriptPropAfterLinebreak')
1951endfunc
1952
Martin Tournoije2390c72021-07-28 13:30:16 +02001953" Buffer number of 0 should be ignored, as if the parameter wasn't passed.
1954def Test_prop_bufnr_zero()
1955 new
1956 try
1957 var bufnr = bufnr('')
1958 setline(1, 'hello')
1959 prop_type_add('bufnr-global', {highlight: 'ErrorMsg'})
1960 prop_type_add('bufnr-buffer', {highlight: 'StatusLine', bufnr: bufnr})
1961
1962 prop_add(1, 1, {type: 'bufnr-global', length: 1})
1963 prop_add(1, 2, {type: 'bufnr-buffer', length: 1})
1964
1965 var list = prop_list(1)
1966 assert_equal([
1967 {id: 0, col: 1, type_bufnr: 0, end: 1, type: 'bufnr-global', length: 1, start: 1},
1968 {id: 0, col: 2, type_bufnr: bufnr, end: 1, type: 'bufnr-buffer', length: 1, start: 1},
1969 ], list)
1970
1971 assert_equal(
1972 {highlight: 'ErrorMsg', end_incl: 0, start_incl: 0, priority: 0, combine: 1},
1973 prop_type_get('bufnr-global', {bufnr: list[0].type_bufnr}))
1974
1975 assert_equal(
1976 {highlight: 'StatusLine', end_incl: 0, start_incl: 0, priority: 0, bufnr: bufnr, combine: 1},
1977 prop_type_get('bufnr-buffer', {bufnr: list[1].type_bufnr}))
1978 finally
1979 bwipe!
1980 prop_type_delete('bufnr-global')
1981 endtry
1982enddef
1983
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001984" Tests for the prop_list() function
1985func Test_prop_list()
1986 let lines =<< trim END
1987 new
Bram Moolenaar62aec932022-01-29 21:45:34 +00001988 call g:AddPropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00001989 call setline(1, repeat([repeat('a', 60)], 10))
1990 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
1991 call prop_add(1, 5, {'type': 'two', 'id': 10, 'end_col': 7})
1992 call prop_add(3, 12, {'type': 'one', 'id': 20, 'end_col': 14})
1993 call prop_add(3, 13, {'type': 'two', 'id': 10, 'end_col': 15})
1994 call prop_add(5, 20, {'type': 'one', 'id': 10, 'end_col': 22})
1995 call prop_add(5, 21, {'type': 'two', 'id': 20, 'end_col': 23})
1996 call assert_equal([
1997 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
1998 \ 'type': 'one', 'length': 2, 'start': 1},
1999 \ {'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
2000 \ 'type': 'two', 'length': 2, 'start': 1}], prop_list(1))
2001 #" text properties between a few lines
2002 call assert_equal([
2003 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2004 \ 'type': 'one', 'length': 2, 'start': 1},
2005 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
2006 \ 'type': 'two', 'length': 2, 'start': 1},
2007 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
2008 \ 'type': 'one', 'length': 2, 'start': 1},
2009 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
2010 \ 'type': 'two', 'length': 2, 'start': 1}],
2011 \ prop_list(2, {'end_lnum': 5}))
2012 #" text properties across all the lines
2013 call assert_equal([
2014 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2015 \ 'type': 'one', 'length': 2, 'start': 1},
2016 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2017 \ 'type': 'one', 'length': 2, 'start': 1},
2018 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
2019 \ 'type': 'one', 'length': 2, 'start': 1}],
2020 \ prop_list(1, {'types': ['one'], 'end_lnum': -1}))
2021 #" text properties with the specified identifier
2022 call assert_equal([
2023 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2024 \ 'type': 'one', 'length': 2, 'start': 1},
2025 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
2026 \ 'type': 'two', 'length': 2, 'start': 1}],
2027 \ prop_list(1, {'ids': [20], 'end_lnum': 10}))
2028 #" text properties of the specified type and id
2029 call assert_equal([
2030 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
2031 \ 'type': 'two', 'length': 2, 'start': 1},
2032 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
2033 \ 'type': 'two', 'length': 2, 'start': 1}],
2034 \ prop_list(1, {'types': ['two'], 'ids': [10], 'end_lnum': 20}))
2035 call assert_equal([], prop_list(1, {'ids': [40, 50], 'end_lnum': 10}))
2036 call assert_equal([], prop_list(6, {'end_lnum': 10}))
2037 call assert_equal([], prop_list(2, {'end_lnum': 2}))
2038 #" error cases
2039 call assert_fails("echo prop_list(1, {'end_lnum': -20})", 'E16:')
2040 call assert_fails("echo prop_list(4, {'end_lnum': 2})", 'E16:')
2041 call assert_fails("echo prop_list(1, {'end_lnum': '$'})", 'E889:')
2042 call assert_fails("echo prop_list(1, {'types': ['blue'], 'end_lnum': 10})",
2043 \ 'E971:')
2044 call assert_fails("echo prop_list(1, {'types': ['one', 'blue'],
2045 \ 'end_lnum': 10})", 'E971:')
2046 call assert_fails("echo prop_list(1, {'types': ['one', 10],
2047 \ 'end_lnum': 10})", 'E928:')
2048 call assert_fails("echo prop_list(1, {'types': ['']})", 'E971:')
2049 call assert_equal([], prop_list(2, {'types': []}))
2050 call assert_equal([], prop_list(2, {'types': test_null_list()}))
2051 call assert_fails("call prop_list(1, {'types': {}})", 'E714:')
2052 call assert_fails("call prop_list(1, {'types': 'one'})", 'E714:')
2053 call assert_equal([], prop_list(2, {'types': ['one'],
2054 \ 'ids': test_null_list()}))
2055 call assert_equal([], prop_list(2, {'types': ['one'], 'ids': []}))
2056 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': {}})",
2057 \ 'E714:')
2058 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': 10})",
2059 \ 'E714:')
2060 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [[]]})",
2061 \ 'E745:')
2062 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [10, []]})",
2063 \ 'E745:')
Martin Tournoije2390c72021-07-28 13:30:16 +02002064
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002065 #" get text properties from a non-current buffer
2066 wincmd w
2067 call assert_equal([
2068 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2069 \ 'type': 'one', 'length': 2, 'start': 1},
2070 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
2071 \ 'type': 'two', 'length': 2, 'start': 1},
2072 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2073 \ 'type': 'one', 'length': 2, 'start': 1},
2074 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
2075 \ 'type': 'two', 'length': 2, 'start': 1}],
2076 \ prop_list(1, {'bufnr': winbufnr(1), 'end_lnum': 4}))
2077 wincmd w
2078
2079 #" get text properties after clearing all the properties
2080 call prop_clear(1, line('$'))
2081 call assert_equal([], prop_list(1, {'end_lnum': 10}))
2082
2083 call prop_add(2, 4, {'type': 'one', 'id': 5, 'end_col': 6})
2084 call prop_add(2, 4, {'type': 'two', 'id': 10, 'end_col': 6})
2085 call prop_add(2, 4, {'type': 'three', 'id': 15, 'end_col': 6})
2086 #" get text properties with a list of types
2087 call assert_equal([
2088 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
2089 \ 'type': 'two', 'length': 2, 'start': 1},
2090 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2091 \ 'type': 'one', 'length': 2, 'start': 1}],
2092 \ prop_list(2, {'types': ['one', 'two']}))
2093 call assert_equal([
2094 \ {'id': 15, 'col': 4, 'type_bufnr': 0, 'end': 1,
2095 \ 'type': 'three', 'length': 2, 'start': 1},
2096 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2097 \ 'type': 'one', 'length': 2, 'start': 1}],
2098 \ prop_list(2, {'types': ['one', 'three']}))
2099 #" get text properties with a list of identifiers
2100 call assert_equal([
2101 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
2102 \ 'type': 'two', 'length': 2, 'start': 1},
2103 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2104 \ 'type': 'one', 'length': 2, 'start': 1}],
2105 \ prop_list(2, {'ids': [5, 10, 20]}))
2106 call prop_clear(1, line('$'))
2107 call assert_equal([], prop_list(2, {'types': ['one', 'two']}))
2108 call assert_equal([], prop_list(2, {'ids': [5, 10, 20]}))
2109
2110 #" get text properties from a hidden buffer
2111 edit! Xaaa
2112 call setline(1, repeat([repeat('b', 60)], 10))
2113 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
2114 call prop_add(4, 8, {'type': 'two', 'id': 10, 'end_col': 10})
2115 VAR bnr = bufnr()
2116 hide edit Xbbb
2117 call assert_equal([
2118 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2119 \ 'type': 'one', 'length': 2, 'start': 1},
2120 \ {'lnum': 4, 'id': 10, 'col': 8, 'type_bufnr': 0, 'end': 1,
2121 \ 'type': 'two', 'length': 2, 'start': 1}],
2122 \ prop_list(1, {'bufnr': bnr,
2123 \ 'types': ['one', 'two'], 'ids': [5, 10], 'end_lnum': -1}))
2124 #" get text properties from an unloaded buffer
2125 bunload! Xaaa
2126 call assert_equal([], prop_list(1, {'bufnr': bnr, 'end_lnum': -1}))
2127
Bram Moolenaar62aec932022-01-29 21:45:34 +00002128 call g:DeletePropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002129 :%bw!
2130 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002131 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002132endfunc
Bram Moolenaar23999d72020-12-23 14:36:00 +01002133
LemonBoy9bd3ce22022-04-18 21:54:02 +01002134func Test_prop_find_prev_on_same_line()
2135 new
2136
2137 call setline(1, 'the quikc bronw fox jumsp over the layz dog')
2138 call prop_type_add('misspell', #{highlight: 'ErrorMsg'})
2139 for col in [8, 14, 24, 38]
2140 call prop_add(1, col, #{type: 'misspell', length: 2})
2141 endfor
2142
2143 call cursor(1,18)
2144 let expected = [
2145 \ #{lnum: 1, id: 0, col: 14, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1},
2146 \ #{lnum: 1, id: 0, col: 24, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1}
2147 \ ]
2148
2149 let result = prop_find(#{type: 'misspell'}, 'b')
2150 call assert_equal(expected[0], result)
2151 let result = prop_find(#{type: 'misspell'}, 'f')
2152 call assert_equal(expected[1], result)
2153
2154 call prop_type_delete('misspell')
2155 bwipe!
2156endfunc
2157
LemonBoyb7a70122022-05-13 12:41:50 +01002158func Test_prop_spell()
2159 new
2160 set spell
2161 call AddPropTypes()
2162
2163 call setline(1, ["helo world", "helo helo helo"])
2164 call prop_add(1, 1, #{type: 'one', length: 4})
2165 call prop_add(1, 6, #{type: 'two', length: 5})
2166 call prop_add(2, 1, #{type: 'three', length: 4})
2167 call prop_add(2, 6, #{type: 'three', length: 4})
2168 call prop_add(2, 11, #{type: 'three', length: 4})
2169
2170 " The first prop over 'helo' increases its length after the word is corrected
2171 " to 'Hello', the second one is shifted to the right.
2172 let expected = [
2173 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2174 \ 'length': 5, 'start': 1},
2175 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2176 \ 'length': 5, 'start': 1}
2177 \ ]
2178 call feedkeys("z=1\<CR>", 'xt')
2179
2180 call assert_equal('Hello world', getline(1))
2181 call assert_equal(expected, prop_list(1))
2182
2183 " Repeat the replacement done by z=
2184 spellrepall
2185
2186 let expected = [
2187 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2188 \ 'length': 5, 'start': 1},
2189 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2190 \ 'length': 5, 'start': 1},
2191 \ {'id': 0, 'col': 13, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2192 \ 'length': 5, 'start': 1}
2193 \ ]
2194 call assert_equal('Hello Hello Hello', getline(2))
2195 call assert_equal(expected, prop_list(2))
2196
2197 call DeletePropTypes()
2198 set spell&
2199 bwipe!
2200endfunc
2201
LemonBoy4b936742022-05-13 21:56:28 +01002202func Test_prop_shift_block()
2203 new
2204 call AddPropTypes()
2205
2206 call setline(1, ['some highlighted text']->repeat(2))
2207 call prop_add(1, 10, #{type: 'one', length: 11})
2208 call prop_add(2, 10, #{type: 'two', length: 11})
2209
2210 call cursor(1, 1)
2211 call feedkeys("5l\<c-v>>", 'nxt')
2212 call cursor(2, 1)
2213 call feedkeys("5l\<c-v><", 'nxt')
2214
2215 let expected = [
2216 \ {'lnum': 1, 'id': 0, 'col': 8, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2217 \ 'length': 11, 'start' : 1},
2218 \ {'lnum': 2, 'id': 0, 'col': 6, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2219 \ 'length': 11, 'start' : 1}
2220 \ ]
2221 call assert_equal(expected, prop_list(1, #{end_lnum: 2}))
2222
2223 call DeletePropTypes()
2224 bwipe!
2225endfunc
LemonBoyb7a70122022-05-13 12:41:50 +01002226
LemonBoy698cb4c2022-05-14 18:10:15 +01002227func Test_prop_insert_multiline()
2228 new
2229 call AddPropTypes()
2230
2231 call setline(1, ['foobar', 'barbaz'])
2232 call prop_add(1, 4, #{end_lnum: 2, end_col: 4, type: 'one'})
2233
2234 call feedkeys("1Goquxqux\<Esc>", 'nxt')
2235 call feedkeys("2GOquxqux\<Esc>", 'nxt')
2236
2237 let lines =<< trim END
2238 foobar
2239 quxqux
2240 quxqux
2241 barbaz
2242 END
2243 call assert_equal(lines, getline(1, '$'))
2244 let expected = [
2245 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2246 \ 'length': 4 ,'start': 1},
2247 \ {'lnum': 2, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2248 \ 'length': 7, 'start': 0},
2249 \ {'lnum': 3, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2250 \ 'length': 7, 'start': 0},
2251 \ {'lnum': 4, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2252 \ 'length': 3, 'start': 0}
2253 \ ]
2254 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
2255
2256 call DeletePropTypes()
2257 bwipe!
2258endfunc
2259
LemonBoyb559b302022-05-15 13:08:02 +01002260func Test_prop_blockwise_change()
2261 new
2262 call AddPropTypes()
2263
2264 call setline(1, ['foooooo', 'bar', 'baaaaz'])
2265 call prop_add(1, 1, #{end_col: 3, type: 'one'})
2266 call prop_add(2, 1, #{end_col: 3, type: 'two'})
2267 call prop_add(3, 1, #{end_col: 3, type: 'three'})
2268
2269 " Replace the first two columns with '123', since 'start_incl' is false the
2270 " prop is not extended.
2271 call feedkeys("gg\<c-v>2jc123\<Esc>", 'nxt')
2272
2273 let lines =<< trim END
2274 123oooooo
2275 123ar
2276 123aaaaz
2277 END
2278 call assert_equal(lines, getline(1, '$'))
2279 let expected = [
2280 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2281 \ 'length': 1, 'start': 1},
2282 \ {'lnum': 2, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2283 \ 'length': 1, 'start': 1},
2284 \ {'lnum': 3, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1 ,
2285 \ 'type': 'three', 'length': 1, 'start': 1}
2286 \ ]
2287 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
2288
2289 call DeletePropTypes()
2290 bwipe!
2291endfunc
2292
Paul Ollis4c3d21a2022-05-24 21:26:37 +01002293func Do_test_props_do_not_affect_byte_offsets(ff, increment)
2294 new
2295 let lcount = 410
2296
2297 " File format affects byte-offset calculations, so make sure it is known.
2298 exec 'setlocal fileformat=' . a:ff
2299
2300 " Fill the buffer with varying length lines. We need a suitably large number
2301 " to force Vim code through paths wehere previous error have occurred. This
2302 " is more 'art' than 'science'.
2303 let text = 'a'
2304 call setline(1, text)
2305 let offsets = [1]
2306 for idx in range(lcount)
2307 call add(offsets, offsets[idx] + len(text) + a:increment)
2308 if (idx % 6) == 0
2309 let text = text . 'a'
2310 endif
2311 call append(line('$'), text)
2312 endfor
2313
2314 " Set a property that spans a few lines to cause Vim's internal buffer code
2315 " to perform a reasonable amount of rearrangement.
2316 call prop_type_add('one', {'highlight': 'ErrorMsg'})
2317 call prop_add(1, 1, {'type': 'one', 'end_lnum': 6, 'end_col': 2})
2318
2319 for idx in range(lcount)
2320 let boff = line2byte(idx + 1)
2321 call assert_equal(offsets[idx], boff, 'Bad byte offset at line ' . (idx + 1))
2322 endfor
2323
2324 call prop_type_delete('one')
2325 bwipe!
2326endfunc
2327
2328func Test_props_do_not_affect_byte_offsets()
2329 call Do_test_props_do_not_affect_byte_offsets('unix', 1)
2330endfunc
2331
2332func Test_props_do_not_affect_byte_offsets_dos()
2333 call Do_test_props_do_not_affect_byte_offsets('dos', 2)
2334endfunc
2335
2336func Test_props_do_not_affect_byte_offsets_editline()
2337 new
2338 let lcount = 410
2339
2340 " File format affects byte-offset calculations, so make sure it is known.
2341 setlocal fileformat=unix
2342
2343 " Fill the buffer with varying length lines. We need a suitably large number
2344 " to force Vim code through paths wehere previous error have occurred. This
2345 " is more 'art' than 'science'.
2346 let text = 'aa'
2347 call setline(1, text)
2348 let offsets = [1]
2349 for idx in range(lcount)
2350 call add(offsets, offsets[idx] + len(text) + 1)
2351 if (idx % 6) == 0
2352 let text = text . 'a'
2353 endif
2354 call append(line('$'), text)
2355 endfor
2356
2357 " Set a property that just covers the first line. When this test was
2358 " developed, this did not trigger a byte-offset error.
2359 call prop_type_add('one', {'highlight': 'ErrorMsg'})
2360 call prop_add(1, 1, {'type': 'one', 'end_lnum': 1, 'end_col': 3})
2361
2362 for idx in range(lcount)
2363 let boff = line2byte(idx + 1)
2364 call assert_equal(offsets[idx], boff,
2365 \ 'Confounding bad byte offset at line ' . (idx + 1))
2366 endfor
2367
2368 " Insert text in the middle of the first line, keeping the property
2369 " unchanged.
2370 :1
2371 normal aHello
2372 for idx in range(1, lcount)
2373 let offsets[idx] = offsets[idx] + 5
2374 endfor
2375
2376 for idx in range(lcount)
2377 let boff = line2byte(idx + 1)
2378 call assert_equal(offsets[idx], boff,
2379 \ 'Bad byte offset at line ' . (idx + 1))
2380 endfor
2381
2382 call prop_type_delete('one')
2383 bwipe!
2384endfunc
2385
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002386func Test_prop_inserts_text()
2387 CheckRunVimInTerminal
2388
2389 " Just a basic check for now
2390 let lines =<< trim END
2391 call setline(1, 'insert some text here and other text there and some more text after wrapping')
2392 call prop_type_add('someprop', #{highlight: 'ErrorMsg'})
2393 call prop_type_add('otherprop', #{highlight: 'Search'})
2394 call prop_type_add('moreprop', #{highlight: 'DiffAdd'})
2395 call prop_add(1, 18, #{type: 'someprop', text: 'SOME '})
Bram Moolenaar783ef722022-08-01 16:11:06 +01002396 call prop_add(1, 38, #{type: 'otherprop', text: "OTHER\t"})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002397 call prop_add(1, 69, #{type: 'moreprop', text: 'MORE '})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002398 normal $
Bram Moolenaar09ff4b52022-08-01 16:51:02 +01002399
2400 call setline(2, 'prepost')
2401 call prop_type_add('multibyte', #{highlight: 'Visual'})
2402 call prop_add(2, 4, #{type: 'multibyte', text: 'söme和平téxt'})
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002403
Bram Moolenaar25463612022-08-08 11:07:47 +01002404 call setline(3, 'Foo foo = { 1, 2 };')
2405 call prop_type_add( 'testprop', #{highlight: 'Comment'})
2406 call prop_add(3, 13, #{type: 'testprop', text: '.x='})
2407 call prop_add(3, 16, #{type: 'testprop', text: '.y='})
2408
2409 call setline(4, '')
2410 call prop_add(4, 1, #{type: 'someprop', text: 'empty line'})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002411 END
2412 call writefile(lines, 'XscriptPropsWithText')
Bram Moolenaar25463612022-08-08 11:07:47 +01002413 let buf = RunVimInTerminal('-S XscriptPropsWithText', #{rows: 8, cols: 60})
Bram Moolenaar711483c2022-07-30 21:33:46 +01002414 call VerifyScreenDump(buf, 'Test_prop_inserts_text_1', {})
2415
2416 call term_sendkeys(buf, ":set signcolumn=yes\<CR>")
2417 call VerifyScreenDump(buf, 'Test_prop_inserts_text_2', {})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002418
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002419 call term_sendkeys(buf, "2G$")
2420 call VerifyScreenDump(buf, 'Test_prop_inserts_text_3', {})
2421
Bram Moolenaar25463612022-08-08 11:07:47 +01002422 call term_sendkeys(buf, "3Gf1")
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002423 call VerifyScreenDump(buf, 'Test_prop_inserts_text_4', {})
Bram Moolenaar25463612022-08-08 11:07:47 +01002424 call term_sendkeys(buf, "f2")
2425 call VerifyScreenDump(buf, 'Test_prop_inserts_text_5', {})
2426
2427 call term_sendkeys(buf, "4G")
2428 call VerifyScreenDump(buf, 'Test_prop_inserts_text_6', {})
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002429
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002430 call StopVimInTerminal(buf)
2431 call delete('XscriptPropsWithText')
2432endfunc
2433
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002434func Test_props_with_text_after()
2435 CheckRunVimInTerminal
2436
2437 let lines =<< trim END
Bram Moolenaar3ec3b8e2022-08-05 21:39:30 +01002438 set showbreak=+++
Bram Moolenaar73c38422022-08-07 11:53:40 +01002439 set breakindent
2440 call setline(1, ' some text here and other text there')
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002441 call prop_type_add('rightprop', #{highlight: 'ErrorMsg'})
2442 call prop_type_add('afterprop', #{highlight: 'Search'})
2443 call prop_type_add('belowprop', #{highlight: 'DiffAdd'})
2444 call prop_add(1, 0, #{type: 'rightprop', text: ' RIGHT ', text_align: 'right'})
Bram Moolenaar783ef722022-08-01 16:11:06 +01002445 call prop_add(1, 0, #{type: 'afterprop', text: "\tAFTER\t", text_align: 'after'})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002446 call prop_add(1, 0, #{type: 'belowprop', text: ' BELOW ', text_align: 'below'})
Bram Moolenaar50e75fe2022-08-05 20:25:50 +01002447 call prop_add(1, 0, #{type: 'belowprop', text: ' ALSO BELOW ', text_align: 'below'})
Bram Moolenaar84b247f2022-08-01 11:17:40 +01002448
2449 call setline(2, 'Last line.')
2450 call prop_add(2, 0, #{type: 'afterprop', text: ' After Last ', text_align: 'after'})
2451 normal G$
Bram Moolenaar09ff4b52022-08-01 16:51:02 +01002452
2453 call setline(3, 'right here')
2454 call prop_add(3, 0, #{type: 'rightprop', text: 'söme和平téxt', text_align: 'right'})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002455 END
2456 call writefile(lines, 'XscriptPropsWithTextAfter')
Bram Moolenaar50e75fe2022-08-05 20:25:50 +01002457 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfter', #{rows: 8, cols: 60})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002458 call VerifyScreenDump(buf, 'Test_prop_with_text_after_1', {})
2459
2460 call StopVimInTerminal(buf)
2461 call delete('XscriptPropsWithTextAfter')
2462endfunc
2463
Bram Moolenaarcba69522022-08-06 21:03:53 +01002464func Test_props_with_text_after_below_trunc()
2465 CheckRunVimInTerminal
2466
2467 let lines =<< trim END
2468 vim9script
2469 edit foobar
2470 set showbreak=+++
2471 setline(1, ['onasdf asdf asdf asdf asd fas df', 'two'])
2472 prop_type_add('test', {highlight: 'Special'})
2473 prop_add(1, 0, {
2474 type: 'test',
2475 text: 'the quick brown fox jumps over the lazy dog',
2476 text_align: 'after'
2477 })
2478 prop_add(1, 0, {
2479 type: 'test',
2480 text: 'the quick brown fox jumps over the lazy dog',
2481 text_align: 'below'
2482 })
2483 normal G$
2484 END
2485 call writefile(lines, 'XscriptPropsAfterTrunc')
2486 let buf = RunVimInTerminal('-S XscriptPropsAfterTrunc', #{rows: 8, cols: 60})
2487 call VerifyScreenDump(buf, 'Test_prop_with_text_after_below_trunc_1', {})
2488
2489 call StopVimInTerminal(buf)
2490 call delete('XscriptPropsAfterTrunc')
2491endfunc
2492
Bram Moolenaare175dc62022-08-01 22:18:50 +01002493func Test_props_with_text_after_joined()
2494 CheckRunVimInTerminal
2495
2496 let lines =<< trim END
2497 call setline(1, ['one', 'two', 'three', 'four'])
2498 call prop_type_add('afterprop', #{highlight: 'Search'})
2499 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE', text_align: 'after'})
2500 call prop_add(4, 0, #{type: 'afterprop', text: ' FOUR', text_align: 'after'})
2501 normal ggJ
2502 normal GkJ
2503
2504 call setline(3, ['a', 'b', 'c', 'd', 'e', 'f'])
2505 call prop_add(3, 0, #{type: 'afterprop', text: ' AAA', text_align: 'after'})
2506 call prop_add(5, 0, #{type: 'afterprop', text: ' CCC', text_align: 'after'})
2507 call prop_add(7, 0, #{type: 'afterprop', text: ' EEE', text_align: 'after'})
2508 call prop_add(8, 0, #{type: 'afterprop', text: ' FFF', text_align: 'after'})
2509 normal 3G6J
2510 END
2511 call writefile(lines, 'XscriptPropsWithTextAfterJoined')
2512 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterJoined', #{rows: 6, cols: 60})
2513 call VerifyScreenDump(buf, 'Test_prop_with_text_after_joined_1', {})
2514
2515 call StopVimInTerminal(buf)
2516 call delete('XscriptPropsWithTextAfterJoined')
2517endfunc
2518
Bram Moolenaar398649e2022-08-04 15:03:48 +01002519func Test_props_with_text_after_truncated()
2520 CheckRunVimInTerminal
2521
2522 let lines =<< trim END
2523 call setline(1, ['one two three four five six seven'])
2524 call prop_type_add('afterprop', #{highlight: 'Search'})
2525 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE'})
2526
2527 call setline(2, ['one two three four five six seven'])
2528 call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right'})
2529
2530 call setline(3, ['one two three four five six seven'])
2531 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'})
2532
2533 call setline(4, ['cursor here'])
2534 normal 4Gfh
2535 END
2536 call writefile(lines, 'XscriptPropsWithTextAfterTrunc')
2537 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterTrunc', #{rows: 9, cols: 60})
2538 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_1', {})
2539
2540 call term_sendkeys(buf, ":37vsp\<CR>gg")
2541 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_2', {})
2542
2543 call term_sendkeys(buf, ":36wincmd |\<CR>")
2544 call term_sendkeys(buf, "2G$")
2545 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_3', {})
2546
2547 call term_sendkeys(buf, ":33wincmd |\<CR>")
2548 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_4', {})
2549
2550 call term_sendkeys(buf, ":18wincmd |\<CR>")
2551 call term_sendkeys(buf, "0fx")
2552 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_5', {})
2553
2554 call StopVimInTerminal(buf)
2555 call delete('XscriptPropsWithTextAfterTrunc')
2556endfunc
2557
Bram Moolenaar49a90792022-08-09 18:25:23 +01002558func Test_props_with_text_empty_line()
2559 CheckRunVimInTerminal
2560
2561 let lines =<< trim END
2562 call setline(1, ['', 'aaa', '', 'bbbbbb'])
2563 call prop_type_add('prop1', #{highlight: 'Search'})
2564 call prop_add(1, 1, #{type: 'prop1', text_wrap: 'wrap', text: repeat('X', &columns)})
2565 call prop_add(3, 1, #{type: 'prop1', text_wrap: 'wrap', text: repeat('X', &columns + 1)})
2566 normal gg0
2567 END
2568 call writefile(lines, 'XscriptPropsWithTextEmptyLine')
2569 let buf = RunVimInTerminal('-S XscriptPropsWithTextEmptyLine', #{rows: 8, cols: 60})
2570 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_1', {})
2571 call term_sendkeys(buf, "$")
2572 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_2', {})
2573 call term_sendkeys(buf, "j")
2574 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_3', {})
2575 call term_sendkeys(buf, "j")
2576 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_4', {})
2577 call term_sendkeys(buf, "j")
2578 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_5', {})
2579
2580 call StopVimInTerminal(buf)
2581 call delete('XscriptPropsWithTextEmptyLine')
2582endfunc
2583
Bram Moolenaar398649e2022-08-04 15:03:48 +01002584func Test_props_with_text_after_wraps()
2585 CheckRunVimInTerminal
2586
2587 let lines =<< trim END
2588 call setline(1, ['one two three four five six seven'])
2589 call prop_type_add('afterprop', #{highlight: 'Search'})
2590 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE', text_wrap: 'wrap'})
2591
2592 call setline(2, ['one two three four five six seven'])
2593 call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right', text_wrap: 'wrap'})
2594
2595 call setline(3, ['one two three four five six seven'])
2596 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'})
2597
2598 call setline(4, ['cursor here'])
2599 normal 4Gfh
2600 END
2601 call writefile(lines, 'XscriptPropsWithTextAfterWraps')
2602 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterWraps', #{rows: 9, cols: 60})
2603 call VerifyScreenDump(buf, 'Test_prop_with_text_after_wraps_1', {})
2604
2605 call StopVimInTerminal(buf)
2606 call delete('XscriptPropsWithTextAfterWraps')
2607endfunc
2608
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002609func Test_props_with_text_after_nowrap()
2610 CheckRunVimInTerminal
2611
2612 let lines =<< trim END
2613 set nowrap
2614 call setline(1, ['one', 'two', 'three'])
2615 call prop_type_add('belowprop', #{highlight: 'ErrorMsg'})
2616 call prop_type_add('anotherprop', #{highlight: 'Search'})
Bram Moolenaardb9b96d2022-08-06 17:38:53 +01002617 call prop_type_add('someprop', #{highlight: 'DiffChange'})
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002618 call prop_add(1, 0, #{type: 'belowprop', text: ' Below the line ', text_align: 'below'})
2619 call prop_add(2, 0, #{type: 'anotherprop', text: 'another', text_align: 'below'})
2620 call prop_add(2, 0, #{type: 'belowprop', text: 'One More Here', text_align: 'below'})
Bram Moolenaardb9b96d2022-08-06 17:38:53 +01002621 call prop_add(1, 0, #{type: 'someprop', text: 'right here', text_align: 'right'})
2622 call prop_add(1, 0, #{type: 'someprop', text: ' After the text', text_align: 'after'})
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002623 normal G$
2624 END
2625 call writefile(lines, 'XscriptPropsAfterNowrap')
Bram Moolenaardb9b96d2022-08-06 17:38:53 +01002626 let buf = RunVimInTerminal('-S XscriptPropsAfterNowrap', #{rows: 10, cols: 60})
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002627 call VerifyScreenDump(buf, 'Test_prop_with_text_after_nowrap_1', {})
2628
Bram Moolenaar1306b362022-08-06 15:59:06 +01002629 call term_sendkeys(buf, ":set signcolumn=yes foldcolumn=3\<CR>")
2630 call VerifyScreenDump(buf, 'Test_prop_with_text_after_nowrap_2', {})
2631
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002632 call StopVimInTerminal(buf)
2633 call delete('XscriptPropsAfterNowrap')
2634endfunc
2635
Bram Moolenaar48ca24d2022-08-06 22:03:20 +01002636func Test_props_with_text_below_nowrap()
2637 CheckRunVimInTerminal
2638
2639 let lines =<< trim END
2640 vim9script
2641 edit foobar
2642 set nowrap
2643 set showbreak=+++\
2644 setline(1, ['onasdf asdf asdf sdf df asdf asdf e asdf asdf asdf asdf asd fas df', 'two'])
2645 prop_type_add('test', {highlight: 'Special'})
2646 prop_add(1, 0, {
2647 type: 'test',
2648 text: 'the quick brown fox jumps over the lazy dog',
2649 text_align: 'after'
2650 })
2651 prop_add(1, 0, {
2652 type: 'test',
2653 text: 'the quick brown fox jumps over the lazy dog',
2654 text_align: 'below'
2655 })
2656 normal G$
2657 END
2658 call writefile(lines, 'XscriptPropsBelowNowrap')
2659 let buf = RunVimInTerminal('-S XscriptPropsBelowNowrap', #{rows: 8, cols: 60})
2660 call VerifyScreenDump(buf, 'Test_prop_with_text_below_nowrap_1', {})
2661
2662 call term_sendkeys(buf, "gg$")
2663 call VerifyScreenDump(buf, 'Test_prop_with_text_below_nowrap_2', {})
2664
2665 call StopVimInTerminal(buf)
2666 call delete('XscriptPropsBelowNowrap')
2667endfunc
2668
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002669func Test_props_with_text_after_split_join()
2670 CheckRunVimInTerminal
2671
2672 let lines =<< trim END
2673 call setline(1, ['1122'])
2674 call prop_type_add('belowprop', #{highlight: 'ErrorMsg'})
2675 call prop_add(1, 0, #{type: 'belowprop', text: ' Below the line ', text_align: 'below'})
2676 exe "normal f2i\<CR>\<Esc>"
2677
2678 func AddMore()
2679 call prop_type_add('another', #{highlight: 'Search'})
2680 call prop_add(1, 0, #{type: 'another', text: ' after the text ', text_align: 'after'})
2681 call prop_add(1, 0, #{type: 'another', text: ' right here', text_align: 'right'})
2682 endfunc
2683 END
2684 call writefile(lines, 'XscriptPropsAfterSplitJoin')
2685 let buf = RunVimInTerminal('-S XscriptPropsAfterSplitJoin', #{rows: 8, cols: 60})
2686 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_1', {})
2687
2688 call term_sendkeys(buf, "ggJ")
2689 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_2', {})
2690
2691 call term_sendkeys(buf, ":call AddMore()\<CR>")
2692 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_3', {})
2693
2694 call term_sendkeys(buf, "ggf s\<CR>\<Esc>")
2695 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_4', {})
2696
2697 call term_sendkeys(buf, "ggJ")
2698 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_5', {})
2699
2700 call StopVimInTerminal(buf)
2701 call delete('XscriptPropsAfterSplitJoin')
2702endfunc
2703
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01002704func Test_removed_prop_with_text_cleans_up_array()
2705 new
2706 call setline(1, 'some text here')
2707 call prop_type_add('some', #{highlight: 'ErrorMsg'})
2708 let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"})
2709 call assert_equal(-1, id1)
2710 let id2 = prop_add(1, 10, #{type: 'some', text: "HERE"})
2711 call assert_equal(-2, id2)
2712
2713 " removing the props resets the index
2714 call prop_remove(#{id: id1})
2715 call prop_remove(#{id: id2})
2716 let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"})
2717 call assert_equal(-1, id1)
2718
2719 call prop_type_delete('some')
2720 bwipe!
2721endfunc
2722
Bram Moolenaar1f4ee192022-08-01 15:52:55 +01002723def Test_insert_text_before_virtual_text()
2724 new foobar
2725 setline(1, '12345678')
2726 prop_type_add('test', {highlight: 'Search'})
2727 prop_add(1, 5, {
2728 type: 'test',
2729 text: ' virtual text '
2730 })
2731 normal! f4axyz
2732 normal! f5iXYZ
2733 assert_equal('1234xyzXYZ5678', getline(1))
2734
2735 prop_type_delete('test')
2736 bwipe!
2737enddef
2738
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002739" vim: shiftwidth=2 sts=2 expandtab