blob: 452d9d23e80997b1d5895d100f9e4c1d1fd2d6d5 [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.
Ben Jacksona7704222022-08-20 20:54:51 +0100164 call cursor(5, 1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100165 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).
Ben Jacksona7704222022-08-20 20:54:51 +0100185 call cursor(3, 1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100186 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.
Ben Jacksona7704222022-08-20 20:54:51 +0100233 call cursor(6, 1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100234 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.
Ben Jacksona7704222022-08-20 20:54:51 +0100238 call cursor(1, 1)
Bram Moolenaare05a89a2020-01-10 19:56:46 +0100239 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:')
Bram Moolenaard83392a2022-09-01 12:22:46 +0100384 call assert_fails('call prop_add_list(#{type: "one"}, test_null_list())', 'E1298:')
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200385 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!
Ben Jacksona7704222022-08-20 20:54:51 +0100429
430 new
431 call AddPropTypes()
432 call SetupPropsInFirstLine()
433 let props = Get_expected_props() " [whole, one, two, three]
434 call assert_equal(props, prop_list(1))
435
436 " remove one by types
437 call assert_equal(1, prop_remove({'types': ['one', 'two', 'three']}, 1))
438 unlet props[1] " [whole, two, three]
439 call assert_equal(props, prop_list(1))
440
441 " remove 'all' by types
442 call assert_equal(2, prop_remove({'types': ['three', 'whole'], 'all': 1}, 1))
443 unlet props[0] " [two, three]
444 unlet props[1] " [three]
445 call assert_equal(props, prop_list(1))
446
447 " remove none by types
448 call assert_equal(0, prop_remove({'types': ['three', 'whole'], 'all': 1}, 1))
449 call assert_equal(props, prop_list(1))
450
451 " no types
452 call assert_fails("call prop_remove({'types': []}, 1)", 'E968:')
453 call assert_fails("call prop_remove({'types': ['not_a_real_type']}, 1)", 'E971:')
454
455 " only one of types and type can be supplied
456 call assert_fails("call prop_remove({'type': 'one', 'types': ['three'], 'all': 1}, 1)", 'E1295:')
457
458 call DeletePropTypes()
459 bwipe!
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100460endfunc
461
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200462def Test_prop_add_vim9()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100463 prop_type_add('comment', {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200464 highlight: 'Directory',
465 priority: 123,
466 start_incl: true,
467 end_incl: true,
468 combine: false,
469 })
470 prop_type_delete('comment')
471enddef
472
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200473def Test_prop_remove_vim9()
474 new
Bram Moolenaar62aec932022-01-29 21:45:34 +0000475 g:AddPropTypes()
476 g:SetupPropsInFirstLine()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100477 assert_equal(1, prop_remove({type: 'three', id: 13, both: true, all: true}))
Bram Moolenaar62aec932022-01-29 21:45:34 +0000478 g:DeletePropTypes()
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200479 bwipe!
480enddef
481
Bram Moolenaar196d1572019-01-02 23:47:18 +0100482func SetupOneLine()
483 call setline(1, 'xonex xtwoxx')
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200484 normal gg0
Bram Moolenaar196d1572019-01-02 23:47:18 +0100485 call AddPropTypes()
486 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
487 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
488 let expected = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200489 \ #{type_bufnr: 0, col: 2, length: 3, id: 11, type: 'one', start: 1, end: 1},
490 \ #{type_bufnr: 0, col: 8, length: 3, id: 12, type: 'two', start: 1, end: 1},
Bram Moolenaar196d1572019-01-02 23:47:18 +0100491 \]
492 call assert_equal(expected, prop_list(1))
493 return expected
494endfunc
495
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100496func Test_prop_add_remove_buf()
497 new
498 let bufnr = bufnr('')
499 call AddPropTypes()
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100500 for lnum in range(1, 4)
501 call setline(lnum, 'one two three')
502 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100503 wincmd w
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100504 for lnum in range(1, 4)
505 call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
506 call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
507 call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
508 endfor
509
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100510 let props = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200511 \ #{type_bufnr: 0, col: 1, length: 3, id: 11, type: 'one', start: 1, end: 1},
512 \ #{type_bufnr: 0, col: 5, length: 3, id: 12, type: 'two', start: 1, end: 1},
513 \ #{type_bufnr: 0, col: 11, length: 3, id: 13, type: 'three', start: 1, end: 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100514 \]
515 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100516
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100517 " remove by id
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100518 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100519 unlet props[1]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100520
521 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100522 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100523 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
524 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
525 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
526
527 call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4)
528 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
529 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
530 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
531 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
532
533 call prop_remove({'id': 12, 'bufnr': bufnr})
534 for lnum in range(1, 4)
535 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
536 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100537
538 " remove by type
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100539 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100540 unlet props[0]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100541
542 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100543 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100544 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
545 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
546 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
547
548 call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4)
549 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
550 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
551 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
552 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
553
554 call prop_remove({'type': 'one', 'bufnr': bufnr})
555 for lnum in range(1, 4)
556 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
557 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100558
559 call DeletePropTypes()
560 wincmd w
561 bwipe!
562endfunc
563
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100564func Test_prop_backspace()
565 new
566 set bs=2
Bram Moolenaar196d1572019-01-02 23:47:18 +0100567 let expected = SetupOneLine() " 'xonex xtwoxx'
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100568
569 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
570 call assert_equal('one xtwoxx', getline(1))
571 let expected[0].col = 1
572 let expected[1].col = 6
573 call assert_equal(expected, prop_list(1))
574
575 call DeletePropTypes()
576 bwipe!
577 set bs&
578endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100579
LemonBoyd0b1a092022-05-12 18:45:18 +0100580func Test_prop_change()
581 new
582 let expected = SetupOneLine() " 'xonex xtwoxx'
583
584 " Characterwise.
585 exe "normal 7|c$\<Esc>"
586 call assert_equal('xonex ', getline(1))
587 call assert_equal(expected[:0], prop_list(1))
588 " Linewise.
589 exe "normal cc\<Esc>"
590 call assert_equal('', getline(1))
591 call assert_equal([], prop_list(1))
592
593 call DeletePropTypes()
594 bwipe!
595 set bs&
596endfunc
597
Bram Moolenaar196d1572019-01-02 23:47:18 +0100598func Test_prop_replace()
599 new
600 set bs=2
601 let expected = SetupOneLine() " 'xonex xtwoxx'
602
603 exe "normal 0Ryyy\<Esc>"
604 call assert_equal('yyyex xtwoxx', getline(1))
605 call assert_equal(expected, prop_list(1))
606
607 exe "normal ftRyy\<BS>"
608 call assert_equal('yyyex xywoxx', getline(1))
609 call assert_equal(expected, prop_list(1))
610
611 exe "normal 0fwRyy\<BS>"
612 call assert_equal('yyyex xyyoxx', getline(1))
613 call assert_equal(expected, prop_list(1))
614
615 exe "normal 0foRyy\<BS>\<BS>"
616 call assert_equal('yyyex xyyoxx', getline(1))
617 call assert_equal(expected, prop_list(1))
618
LemonBoy0d534d92022-05-21 11:20:42 +0100619 " Replace three 1-byte chars with three 2-byte ones.
620 exe "normal 0l3rรธ"
621 call assert_equal('yรธรธรธx xyyoxx', getline(1))
622 let expected[0].length += 3
623 let expected[1].col += 3
624 call assert_equal(expected, prop_list(1))
625
Bram Moolenaar196d1572019-01-02 23:47:18 +0100626 call DeletePropTypes()
627 bwipe!
628 set bs&
629endfunc
630
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200631func Test_prop_open_line()
632 new
633
634 " open new line, props stay in top line
635 let expected = SetupOneLine() " 'xonex xtwoxx'
636 exe "normal o\<Esc>"
637 call assert_equal('xonex xtwoxx', getline(1))
638 call assert_equal('', getline(2))
639 call assert_equal(expected, prop_list(1))
640 call DeletePropTypes()
641
642 " move all props to next line
643 let expected = SetupOneLine() " 'xonex xtwoxx'
644 exe "normal 0i\<CR>\<Esc>"
645 call assert_equal('', getline(1))
646 call assert_equal('xonex xtwoxx', getline(2))
647 call assert_equal(expected, prop_list(2))
648 call DeletePropTypes()
649
650 " split just before prop, move all props to next line
651 let expected = SetupOneLine() " 'xonex xtwoxx'
652 exe "normal 0li\<CR>\<Esc>"
653 call assert_equal('x', getline(1))
654 call assert_equal('onex xtwoxx', getline(2))
655 let expected[0].col -= 1
656 let expected[1].col -= 1
657 call assert_equal(expected, prop_list(2))
658 call DeletePropTypes()
659
660 " split inside prop, split first prop
661 let expected = SetupOneLine() " 'xonex xtwoxx'
662 exe "normal 0lli\<CR>\<Esc>"
663 call assert_equal('xo', getline(1))
664 call assert_equal('nex xtwoxx', getline(2))
665 let exp_first = [deepcopy(expected[0])]
666 let exp_first[0].length = 1
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200667 let exp_first[0].end = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200668 call assert_equal(exp_first, prop_list(1))
669 let expected[0].col = 1
670 let expected[0].length = 2
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200671 let expected[0].start = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200672 let expected[1].col -= 2
673 call assert_equal(expected, prop_list(2))
674 call DeletePropTypes()
675
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200676 " split just after first prop, second prop move to next line
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200677 let expected = SetupOneLine() " 'xonex xtwoxx'
678 exe "normal 0fea\<CR>\<Esc>"
679 call assert_equal('xone', getline(1))
680 call assert_equal('x xtwoxx', getline(2))
681 let exp_first = expected[0:0]
682 call assert_equal(exp_first, prop_list(1))
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200683 let expected = expected[1:1]
684 let expected[0].col -= 4
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200685 call assert_equal(expected, prop_list(2))
686 call DeletePropTypes()
687
LemonBoy788c06a2022-05-14 18:48:05 +0100688 " split at the space character with 'ai' active, the leading space is removed
689 " in the second line and the prop is shifted accordingly.
690 let expected = SetupOneLine() " 'xonex xtwoxx'
691 set ai
692 exe "normal 6|i\<CR>\<Esc>"
693 call assert_equal('xonex', getline(1))
694 call assert_equal('xtwoxx', getline(2))
695 let expected[1].col -= 6
696 call assert_equal(expected, prop_list(1) + prop_list(2))
697 set ai&
698 call DeletePropTypes()
699
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200700 bwipe!
701 set bs&
702endfunc
703
Bram Moolenaarecb00c72022-08-07 14:55:14 +0100704func Test_prop_put()
705 new
706 let expected = SetupOneLine() " 'xonex xtwoxx'
707
708 let @a = 'new'
709 " insert just after the prop
710 normal 03l"ap
711 " insert inside the prop
712 normal 02l"ap
713 " insert just before the prop
714 normal 0"ap
715
716 call assert_equal('xnewonnewenewx xtwoxx', getline(1))
717 let expected[0].col += 3
718 let expected[0].length += 3
719 let expected[1].col += 9
720 call assert_equal(expected, prop_list(1))
721
722 " Visually select 4 chars in the prop and put "AB" to replace them
723 let @a = 'AB'
724 normal 05lv3l"ap
725 call assert_equal('xnewoABenewx xtwoxx', getline(1))
726 let expected[0].length -= 2
727 let expected[1].col -= 2
728 call assert_equal(expected, prop_list(1))
729
730 call DeletePropTypes()
731 bwipe!
732endfunc
733
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100734func Test_prop_clear()
735 new
736 call AddPropTypes()
737 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100738 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100739
Bram Moolenaara5a78822019-09-04 21:57:18 +0200740 eval 1->prop_clear()
741 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100742
743 call DeletePropTypes()
744 bwipe!
745endfunc
746
747func Test_prop_clear_buf()
748 new
749 call AddPropTypes()
750 call SetupPropsInFirstLine()
751 let bufnr = bufnr('')
752 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100753 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100754
755 call prop_clear(1, 1, {'bufnr': bufnr})
756 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
757
758 wincmd w
759 call DeletePropTypes()
760 bwipe!
761endfunc
762
Bram Moolenaar21b50382019-01-04 18:07:24 +0100763func Test_prop_setline()
764 new
765 call AddPropTypes()
766 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100767 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100768
769 call setline(1, 'foobar')
770 call assert_equal([], prop_list(1))
771
772 call DeletePropTypes()
773 bwipe!
774endfunc
775
776func Test_prop_setbufline()
777 new
778 call AddPropTypes()
779 call SetupPropsInFirstLine()
780 let bufnr = bufnr('')
781 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100782 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100783
784 call setbufline(bufnr, 1, 'foobar')
785 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
786
787 wincmd w
788 call DeletePropTypes()
789 bwipe!
790endfunc
791
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100792func Test_prop_substitute()
793 new
794 " Set first line to 'one two three'
795 call AddPropTypes()
796 call SetupPropsInFirstLine()
797 let expected_props = Get_expected_props()
798 call assert_equal(expected_props, prop_list(1))
799
800 " Change "n" in "one" to XX: 'oXXe two three'
801 s/n/XX/
802 let expected_props[0].length += 1
803 let expected_props[1].length += 1
804 let expected_props[2].col += 1
805 let expected_props[3].col += 1
806 call assert_equal(expected_props, prop_list(1))
807
808 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
809 s/t//g
810 let expected_props[0].length -= 2
811 let expected_props[2].length -= 1
812 let expected_props[3].length -= 1
813 let expected_props[3].col -= 1
814 call assert_equal(expected_props, prop_list(1))
815
816 " Split the line by changing w to line break: 'oXXe ', 'o hree'
817 " The long prop is split and spans both lines.
818 " The props on "two" and "three" move to the next line.
819 s/w/\r/
820 let new_props = [
821 \ copy(expected_props[0]),
822 \ copy(expected_props[2]),
823 \ copy(expected_props[3]),
824 \ ]
825 let expected_props[0].length = 5
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200826 let expected_props[0].end = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100827 unlet expected_props[3]
828 unlet expected_props[2]
829 call assert_equal(expected_props, prop_list(1))
830
831 let new_props[0].length = 6
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200832 let new_props[0].start = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100833 let new_props[1].col = 1
834 let new_props[1].length = 1
835 let new_props[2].col = 3
836 call assert_equal(new_props, prop_list(2))
837
838 call DeletePropTypes()
839 bwipe!
840endfunc
841
Bram Moolenaar663bc892019-01-08 23:07:24 +0100842func Test_prop_change_indent()
843 call prop_type_add('comment', {'highlight': 'Directory'})
844 new
845 call setline(1, [' xxx', 'yyyyy'])
846 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200847 let expect = #{type_bufnr: 0, col: 2, length: 2, type: 'comment', start: 1, end: 1, id: 0}
Bram Moolenaar663bc892019-01-08 23:07:24 +0100848 call assert_equal([expect], prop_list(2))
849
850 set shiftwidth=3
851 normal 2G>>
852 call assert_equal(' yyyyy', getline(2))
853 let expect.col += 3
854 call assert_equal([expect], prop_list(2))
855
856 normal 2G==
857 call assert_equal(' yyyyy', getline(2))
858 let expect.col = 6
859 call assert_equal([expect], prop_list(2))
860
861 call prop_clear(2)
862 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
863 let expect.col = 2
864 let expect.length = 5
865 call assert_equal([expect], prop_list(2))
866
867 normal 2G<<
868 call assert_equal(' yyyyy', getline(2))
869 let expect.length = 2
870 call assert_equal([expect], prop_list(2))
871
872 set shiftwidth&
873 call prop_type_delete('comment')
874endfunc
875
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100876" Setup a three line prop in lines 2 - 4.
877" Add short props in line 1 and 5.
878func Setup_three_line_prop()
879 new
880 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
881 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
882 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
883 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
884endfunc
885
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100886func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200887 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100888 new
889 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
890
891 " start halfway line 1, end halfway line 3
892 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200893 let expect1 = #{type_bufnr: 0, col: 3, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100894 call assert_equal([expect1], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200895 let expect2 = #{type_bufnr: 0, col: 1, length: 10, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100896 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200897 let expect3 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100898 call assert_equal([expect3], prop_list(3))
899 call prop_clear(1, 3)
900
901 " include all three lines
902 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
903 let expect1.col = 1
904 let expect1.length = 8
905 call assert_equal([expect1], prop_list(1))
906 call assert_equal([expect2], prop_list(2))
907 let expect3.length = 9
908 call assert_equal([expect3], prop_list(3))
909 call prop_clear(1, 3)
910
911 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100912
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100913 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100914 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200915 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 +0100916 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200917 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100918 call assert_equal([expect2], prop_list(2))
919 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100920 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200921 let expect2 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100922 call assert_equal([expect2], prop_list(2))
923 bwipe!
924
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100925 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100926 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200927 let expect3 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100928 call assert_equal([expect3], prop_list(3))
Martin Tournoije2390c72021-07-28 13:30:16 +0200929 let expect4 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100930 call assert_equal([expect4], prop_list(4))
931 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100932 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100933 call assert_equal([expect3], prop_list(3))
934 call assert_equal([expect_short], prop_list(4))
935 bwipe!
936
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100937 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100938 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200939 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100940 call assert_equal([expect2], prop_list(2))
941 call append(2, "new line")
942 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200943 let expect3 = #{type_bufnr: 0, col: 1, length: 9, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100944 call assert_equal([expect3], prop_list(3))
945 bwipe!
946
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100947 call prop_type_delete('comment')
948endfunc
949
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100950func Run_test_with_line2byte(add_props)
951 new
952 setlocal ff=unix
953 if a:add_props
954 call prop_type_add('textprop', #{highlight: 'Search'})
955 endif
Bram Moolenaare5a0e8c2022-08-09 21:37:55 +0100956 " Add a text prop to every fourth line and then change every fifth line so
957 " that it causes a data block split a few times.
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100958 for nr in range(1, 1000)
959 call setline(nr, 'some longer text here')
Bram Moolenaare5a0e8c2022-08-09 21:37:55 +0100960 if a:add_props && nr % 4 == 0
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100961 call prop_add(nr, 13, #{type: 'textprop', length: 4})
962 endif
963 endfor
Bram Moolenaare5a0e8c2022-08-09 21:37:55 +0100964 let expected = 22 * 997 + 1
965 call assert_equal(expected, line2byte(998))
966
967 for nr in range(1, 1000, 5)
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100968 exe nr .. "s/longer/much more/"
Bram Moolenaare5a0e8c2022-08-09 21:37:55 +0100969 let expected += 3
970 call assert_equal(expected, line2byte(998), 'line ' .. nr)
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100971 endfor
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100972
973 if a:add_props
974 call prop_type_delete('textprop')
975 endif
976 bwipe!
977endfunc
978
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100979func Test_prop_line2byte()
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100980 call prop_type_add('comment', {'highlight': 'Directory'})
981 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100982 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100983 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100984 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100985 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100986 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100987 bwipe!
Bram Moolenaar14c75302021-08-15 14:28:40 +0200988
989 new
Bram Moolenaara401bba2021-08-15 15:04:41 +0200990 setlocal ff=unix
Bram Moolenaar14c75302021-08-15 14:28:40 +0200991 call setline(1, range(500))
992 call assert_equal(1491, line2byte(401))
993 call prop_add(2, 1, {'type': 'comment'})
994 call prop_add(222, 1, {'type': 'comment'})
995 call assert_equal(1491, line2byte(401))
996 call prop_remove({'type': 'comment'})
997 call assert_equal(1491, line2byte(401))
998 bwipe!
999
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +02001000 new
Bram Moolenaar49b93042021-08-25 17:02:00 +02001001 setlocal ff=unix
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +02001002 call setline(1, range(520))
1003 call assert_equal(1491, line2byte(401))
1004 call prop_add(2, 1, {'type': 'comment'})
1005 call assert_equal(1491, line2byte(401))
1006 2delete
1007 call assert_equal(1489, line2byte(400))
1008 bwipe!
1009
Bram Moolenaarcf85d972022-08-08 14:59:47 +01001010 " Add many lines so that the data block is split.
1011 " With and without props should give the same result.
1012 call Run_test_with_line2byte(0)
1013 call Run_test_with_line2byte(1)
1014
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01001015 call prop_type_delete('comment')
1016endfunc
1017
Bram Moolenaar9df53b62020-01-13 20:40:51 +01001018func Test_prop_byte2line()
1019 new
1020 set ff=unix
1021 call setline(1, ['one one', 'two two', 'three three', 'four four', 'five'])
1022 call assert_equal(4, byte2line(line2byte(4)))
1023 call assert_equal(5, byte2line(line2byte(5)))
1024
1025 call prop_type_add('prop', {'highlight': 'Directory'})
1026 call prop_add(3, 1, {'length': 5, 'type': 'prop'})
1027 call assert_equal(4, byte2line(line2byte(4)))
1028 call assert_equal(5, byte2line(line2byte(5)))
1029
1030 bwipe!
1031 call prop_type_delete('prop')
1032endfunc
1033
Bram Moolenaar59ff6402021-01-30 17:16:28 +01001034func Test_prop_goto_byte()
1035 new
1036 call setline(1, '')
1037 call setline(2, 'two three')
1038 call setline(3, '')
1039 call setline(4, 'four five')
1040
1041 call prop_type_add('testprop', {'highlight': 'Directory'})
1042 call search('^two')
1043 call prop_add(line('.'), col('.'), {
1044 \ 'length': len('two'),
1045 \ 'type': 'testprop'
1046 \ })
1047
1048 call search('two \zsthree')
1049 let expected_pos = line2byte(line('.')) + col('.') - 1
1050 exe expected_pos .. 'goto'
1051 let actual_pos = line2byte(line('.')) + col('.') - 1
1052 eval actual_pos->assert_equal(expected_pos)
1053
1054 call search('four \zsfive')
1055 let expected_pos = line2byte(line('.')) + col('.') - 1
1056 exe expected_pos .. 'goto'
1057 let actual_pos = line2byte(line('.')) + col('.') - 1
1058 eval actual_pos->assert_equal(expected_pos)
1059
1060 call prop_type_delete('testprop')
1061 bwipe!
1062endfunc
1063
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001064func Test_prop_undo()
1065 new
1066 call prop_type_add('comment', {'highlight': 'Directory'})
1067 call setline(1, ['oneone', 'twotwo', 'three'])
1068 " Set 'undolevels' to break changes into undo-able pieces.
1069 set ul&
1070
1071 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001072 let expected = [#{type_bufnr: 0, col: 3, length: 2, id: 0, type: 'comment', start: 1, end: 1}]
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001073 call assert_equal(expected, prop_list(1))
1074
1075 " Insert a character, then undo.
1076 exe "normal 0lllix\<Esc>"
1077 set ul&
1078 let expected[0].length = 3
1079 call assert_equal(expected, prop_list(1))
1080 undo
1081 let expected[0].length = 2
1082 call assert_equal(expected, prop_list(1))
1083
1084 " Delete a character, then undo
1085 exe "normal 0lllx"
1086 set ul&
1087 let expected[0].length = 1
1088 call assert_equal(expected, prop_list(1))
1089 undo
1090 let expected[0].length = 2
1091 call assert_equal(expected, prop_list(1))
1092
1093 " Delete the line, then undo
1094 1d
1095 set ul&
1096 call assert_equal([], prop_list(1))
1097 undo
1098 call assert_equal(expected, prop_list(1))
1099
1100 " Insert a character, delete two characters, then undo with "U"
1101 exe "normal 0lllix\<Esc>"
1102 set ul&
1103 let expected[0].length = 3
1104 call assert_equal(expected, prop_list(1))
1105 exe "normal 0lllxx"
1106 set ul&
1107 let expected[0].length = 1
1108 call assert_equal(expected, prop_list(1))
1109 normal U
1110 let expected[0].length = 2
1111 call assert_equal(expected, prop_list(1))
1112
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001113 " substitute a word, then undo
1114 call setline(1, 'the number 123 is highlighted.')
1115 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001116 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001117 call assert_equal(expected, prop_list(1))
1118 set ul&
1119 1s/number/foo
1120 let expected[0].col = 9
1121 call assert_equal(expected, prop_list(1))
1122 undo
1123 let expected[0].col = 12
1124 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001125 call prop_clear(1)
1126
1127 " substitute with backslash
1128 call setline(1, 'the number 123 is highlighted.')
1129 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001130 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001131 call assert_equal(expected, prop_list(1))
1132 1s/the/\The
1133 call assert_equal(expected, prop_list(1))
1134 1s/^/\\
1135 let expected[0].col += 1
1136 call assert_equal(expected, prop_list(1))
1137 1s/^/\~
1138 let expected[0].col += 1
1139 call assert_equal(expected, prop_list(1))
1140 1s/123/12\\3
1141 let expected[0].length += 1
1142 call assert_equal(expected, prop_list(1))
1143 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001144
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001145 bwipe!
1146 call prop_type_delete('comment')
1147endfunc
1148
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001149func Test_prop_delete_text()
1150 new
1151 call prop_type_add('comment', {'highlight': 'Directory'})
1152 call setline(1, ['oneone', 'twotwo', 'three'])
1153
1154 " zero length property
1155 call prop_add(1, 3, {'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001156 let expected = [#{type_bufnr: 0, col: 3, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001157 call assert_equal(expected, prop_list(1))
1158
1159 " delete one char moves the property
1160 normal! x
Martin Tournoije2390c72021-07-28 13:30:16 +02001161 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001162 call assert_equal(expected, prop_list(1))
1163
1164 " delete char of the property has no effect
1165 normal! lx
Martin Tournoije2390c72021-07-28 13:30:16 +02001166 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001167 call assert_equal(expected, prop_list(1))
1168
1169 " delete more chars moves property to first column, is not deleted
1170 normal! 0xxxx
Martin Tournoije2390c72021-07-28 13:30:16 +02001171 let expected = [#{type_bufnr: 0, col: 1, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001172 call assert_equal(expected, prop_list(1))
1173
1174 bwipe!
1175 call prop_type_delete('comment')
1176endfunc
1177
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001178" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +02001179func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +02001180 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +01001181 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +02001182 if g:orig_encoding != 'utf-8'
1183 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001184 endif
1185 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +02001186 \ "call setline(1, ["
1187 \ .. "'One two',"
1188 \ .. "'Numbรฉr 123 รคnd thล“n 4ยพ7.',"
1189 \ .. "'--aa--bb--cc--dd--',"
1190 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001191 \ .. "'first line',"
1192 \ .. "' second line ',"
1193 \ .. "'third line',"
1194 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +02001195 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001196 \ "hi NumberProp ctermfg=blue",
1197 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001198 \ "hi BackgroundProp ctermbg=lightgrey",
1199 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001200 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +02001201 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
1202 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001203 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
1204 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
1205 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001206 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
1207 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
1208 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +01001209 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001210 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001211 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
1212 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001213 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
1214 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
1215 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
1216 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001217 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
1218 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001219 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001220 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
1221 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
1222 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
1223 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +02001224 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001225 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001226 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001227 \ "syn match Comment '//.*'",
1228 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001229 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01001230 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001231 \ "normal 6G0i\<BS>\<Esc>",
1232 \ "normal 3J",
1233 \ "normal 3G",
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001234 \], 'XtestProp', 'D')
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001235 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001236 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +01001237
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001238 " clean up
1239 call StopVimInTerminal(buf)
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001240endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +02001241
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001242func Test_textprop_hl_override()
1243 CheckScreendump
1244
1245 let lines =<< trim END
1246 call setline(1, ['One one one one one', 'Two two two two two', 'Three three three three'])
1247 hi OverProp ctermfg=blue ctermbg=yellow
1248 hi CursorLine cterm=bold,underline ctermfg=red ctermbg=green
1249 hi Vsual ctermfg=cyan ctermbg=grey
1250 call prop_type_add('under', #{highlight: 'OverProp'})
1251 call prop_type_add('over', #{highlight: 'OverProp', override: 1})
1252 call prop_add(1, 5, #{type: 'under', length: 4})
1253 call prop_add(1, 13, #{type: 'over', length: 4})
1254 call prop_add(2, 5, #{type: 'under', length: 4})
1255 call prop_add(2, 13, #{type: 'over', length: 4})
1256 call prop_add(3, 5, #{type: 'under', length: 4})
1257 call prop_add(3, 13, #{type: 'over', length: 4})
1258 set cursorline
1259 2
1260 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001261 call writefile(lines, 'XtestOverProp', 'D')
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001262 let buf = RunVimInTerminal('-S XtestOverProp', {'rows': 8})
1263 call VerifyScreenDump(buf, 'Test_textprop_hl_override_1', {})
1264
1265 call term_sendkeys(buf, "3Gllv$hh")
1266 call VerifyScreenDump(buf, 'Test_textprop_hl_override_2', {})
1267 call term_sendkeys(buf, "\<Esc>")
1268
1269 " clean up
1270 call StopVimInTerminal(buf)
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001271endfunc
1272
Bram Moolenaar8055d172019-05-17 22:57:26 +02001273func RunTestVisualBlock(width, dump)
1274 call writefile([
1275 \ "call setline(1, ["
1276 \ .. "'xxxxxxxxx 123 x',"
1277 \ .. "'xxxxxxxx 123 x',"
1278 \ .. "'xxxxxxx 123 x',"
1279 \ .. "'xxxxxx 123 x',"
1280 \ .. "'xxxxx 123 x',"
1281 \ .. "'xxxx 123 xx',"
1282 \ .. "'xxx 123 xxx',"
1283 \ .. "'xx 123 xxxx',"
1284 \ .. "'x 123 xxxxx',"
1285 \ .. "' 123 xxxxxx',"
1286 \ .. "])",
1287 \ "hi SearchProp ctermbg=yellow",
1288 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
1289 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
1290 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
1291 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
1292 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
1293 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
1294 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
1295 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
1296 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
1297 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
1298 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
1299 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001300 \], 'XtestPropVis', 'D')
Bram Moolenaar8055d172019-05-17 22:57:26 +02001301 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
1302 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
1303
1304 " clean up
1305 call StopVimInTerminal(buf)
Bram Moolenaar8055d172019-05-17 22:57:26 +02001306endfunc
1307
1308" screenshot test with Visual block mode operations
1309func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +02001310 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +02001311
1312 " Delete two columns while text props are three chars wide.
1313 call RunTestVisualBlock(2, '01')
1314
1315 " Same, but delete four columns
1316 call RunTestVisualBlock(4, '02')
1317endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +02001318
Bram Moolenaara956bf62019-06-19 17:34:24 +02001319func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +02001320 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +02001321
Bram Moolenaara956bf62019-06-19 17:34:24 +02001322 let lines =<< trim END
1323 call setline(1, [
1324 \ "\txxx",
1325 \ "x\txxx",
1326 \ ])
1327 hi SearchProp ctermbg=yellow
1328 call prop_type_add('search', {'highlight': 'SearchProp'})
1329 call prop_add(1, 2, {'length': 3, 'type': 'search'})
1330 call prop_add(2, 3, {'length': 3, 'type': 'search'})
1331 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001332 call writefile(lines, 'XtestPropTab', 'D')
Bram Moolenaara956bf62019-06-19 17:34:24 +02001333 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
1334 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
1335
1336 " clean up
1337 call StopVimInTerminal(buf)
Bram Moolenaara956bf62019-06-19 17:34:24 +02001338endfunc
1339
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001340func Test_textprop_nowrap_scrolled()
1341 CheckScreendump
1342
1343 let lines =<< trim END
1344 vim9script
1345 set nowrap
1346 setline(1, 'The number 123 is smaller than 4567.' .. repeat('X', &columns))
1347 prop_type_add('number', {'highlight': 'ErrorMsg'})
1348 prop_add(1, 12, {'length': 3, 'type': 'number'})
1349 prop_add(1, 32, {'length': 4, 'type': 'number'})
1350 feedkeys('gg20zl', 'nxt')
1351 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001352 call writefile(lines, 'XtestNowrap', 'D')
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001353 let buf = RunVimInTerminal('-S XtestNowrap', {'rows': 6})
1354 call VerifyScreenDump(buf, 'Test_textprop_nowrap_01', {})
1355
1356 call term_sendkeys(buf, "$")
1357 call VerifyScreenDump(buf, 'Test_textprop_nowrap_02', {})
1358
1359 " clean up
1360 call StopVimInTerminal(buf)
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001361endfunc
1362
Bram Moolenaar952c9b02022-08-10 16:00:33 +01001363func Test_textprop_text_priority()
1364 CheckScreendump
1365
1366 let lines =<< trim END
1367 call setline(1, "function( call, argument, here )")
1368
1369 call prop_type_add('one', #{highlight: 'Error'})
1370 call prop_type_add('two', #{highlight: 'Function'})
1371 call prop_type_add('three', #{highlight: 'DiffChange'})
1372 call prop_type_add('arg', #{highlight: 'Search'})
1373
1374 call prop_add(1, 27, #{type: 'arg', length: len('here')})
1375 call prop_add(1, 27, #{type: 'three', text: 'three: '})
1376 call prop_add(1, 11, #{type: 'one', text: 'one: '})
1377 call prop_add(1, 11, #{type: 'arg', length: len('call')})
1378 call prop_add(1, 17, #{type: 'two', text: 'two: '})
1379 call prop_add(1, 17, #{type: 'arg', length: len('argument')})
1380 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001381 call writefile(lines, 'XtestPropPrio', 'D')
Bram Moolenaar952c9b02022-08-10 16:00:33 +01001382 let buf = RunVimInTerminal('-S XtestPropPrio', {'rows': 5})
1383 call VerifyScreenDump(buf, 'Test_prop_at_same_pos', {})
1384
1385 " clean up
1386 call StopVimInTerminal(buf)
Bram Moolenaar952c9b02022-08-10 16:00:33 +01001387endfunc
1388
Bram Moolenaar34390282019-10-16 14:38:26 +02001389func Test_textprop_with_syntax()
1390 CheckScreendump
1391
1392 let lines =<< trim END
1393 call setline(1, [
1394 \ "(abc)",
1395 \ ])
1396 syn match csParens "[()]" display
1397 hi! link csParens MatchParen
1398
1399 call prop_type_add('TPTitle', #{ highlight: 'Title' })
1400 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
1401 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001402 call writefile(lines, 'XtestPropSyn', 'D')
Bram Moolenaar34390282019-10-16 14:38:26 +02001403 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
1404 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
1405
1406 " clean up
1407 call StopVimInTerminal(buf)
Bram Moolenaar34390282019-10-16 14:38:26 +02001408endfunc
1409
Bram Moolenaard79eef22019-05-24 20:41:55 +02001410" Adding a text property to a new buffer should not fail
1411func Test_textprop_empty_buffer()
1412 call prop_type_add('comment', {'highlight': 'Search'})
1413 new
1414 call prop_add(1, 1, {'type': 'comment'})
1415 close
Bram Moolenaaradfde112019-05-25 22:11:45 +02001416 call prop_type_delete('comment')
1417endfunc
1418
Bram Moolenaard74af422019-06-28 21:38:00 +02001419" Adding a text property with invalid highlight should be ignored.
1420func Test_textprop_invalid_highlight()
1421 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
1422 new
Ben Jacksona7704222022-08-20 20:54:51 +01001423 call setline(1, ['asdf', 'asdf'])
Bram Moolenaard74af422019-06-28 21:38:00 +02001424 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
1425 redraw
1426 bwipe!
1427 call prop_type_delete('dni')
1428endfunc
1429
Bram Moolenaaradfde112019-05-25 22:11:45 +02001430" Adding a text property to an empty buffer and then editing another
1431func Test_textprop_empty_buffer_next()
1432 call prop_type_add("xxx", {})
1433 call prop_add(1, 1, {"type": "xxx"})
1434 next X
1435 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +02001436endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001437
1438func Test_textprop_remove_from_buf()
1439 new
1440 let buf = bufnr('')
1441 call prop_type_add('one', {'bufnr': buf})
1442 call prop_add(1, 1, {'type': 'one', 'id': 234})
1443 file x
1444 edit y
1445 call prop_remove({'id': 234, 'bufnr': buf}, 1)
1446 call prop_type_delete('one', {'bufnr': buf})
1447 bwipe! x
1448 close
1449endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +02001450
1451func Test_textprop_in_unloaded_buf()
1452 edit Xaaa
1453 call setline(1, 'aaa')
1454 write
1455 edit Xbbb
1456 call setline(1, 'bbb')
1457 write
1458 let bnr = bufnr('')
1459 edit Xaaa
1460
1461 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
1462 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
1463 exe 'buf ' .. bnr
1464 call assert_equal('bbb', getline(1))
1465 call assert_equal(0, prop_list(1)->len())
1466
1467 bwipe! Xaaa
1468 bwipe! Xbbb
1469 cal delete('Xaaa')
1470 cal delete('Xbbb')
1471endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001472
1473func Test_proptype_substitute2()
1474 new
1475 " text_prop.vim
1476 call setline(1, [
1477 \ 'The num 123 is smaller than 4567.',
1478 \ '123 The number 123 is smaller than 4567.',
1479 \ '123 The number 123 is smaller than 4567.'])
1480
1481 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1482
1483 call prop_add(1, 12, {'length': 3, 'type': 'number'})
1484 call prop_add(2, 1, {'length': 3, 'type': 'number'})
1485 call prop_add(3, 36, {'length': 4, 'type': 'number'})
1486 set ul&
Martin Tournoije2390c72021-07-28 13:30:16 +02001487 let expected = [
1488 \ #{type_bufnr: 0, id: 0, col: 13, end: 1, type: 'number', length: 3, start: 1},
1489 \ #{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'number', length: 3, start: 1},
1490 \ #{type_bufnr: 0, id: 0, col: 50, end: 1, type: 'number', length: 4, start: 1}]
1491
1492 " TODO
Bram Moolenaar213bbaf2022-08-05 19:46:48 +01001493 if 0
1494 " Add some text in between
1495 %s/\s\+/ /g
1496 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001497
Bram Moolenaar213bbaf2022-08-05 19:46:48 +01001498 " remove some text
1499 :1s/[a-z]\{3\}//g
1500 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
1501 call assert_equal(expected, prop_list(1))
1502 endif
1503
1504 call prop_type_delete('number')
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001505 bwipe!
1506endfunc
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001507
Bram Moolenaar8902b312020-09-20 21:04:35 +02001508" This was causing property corruption.
1509func Test_proptype_substitute3()
1510 new
1511 call setline(1, ['abcxxx', 'def'])
1512 call prop_type_add("test", {"highlight": "Search"})
1513 call prop_add(1, 2, {"end_lnum": 2, "end_col": 2, "type": "test"})
1514 %s/x\+$//
1515 redraw
1516
1517 call prop_type_delete('test')
1518 bwipe!
1519endfunc
1520
Bram Moolenaar213bbaf2022-08-05 19:46:48 +01001521func Test_proptype_substitute_join()
1522 new
1523 call setline(1, [
1524 \ 'This is some end',
1525 \ 'start is highlighted end',
1526 \ 'some is highlighted',
1527 \ 'start is also highlighted'])
1528
1529 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1530
1531 call prop_add(1, 6, {'length': 2, 'type': 'number'})
1532 call prop_add(2, 7, {'length': 2, 'type': 'number'})
1533 call prop_add(3, 6, {'length': 2, 'type': 'number'})
1534 call prop_add(4, 7, {'length': 2, 'type': 'number'})
1535 " The highlighted "is" in line 1, 2 and 4 is kept and ajudsted.
1536 " The highlighted "is" in line 3 is deleted.
1537 let expected = [
1538 \ #{type_bufnr: 0, id: 0, col: 6, end: 1, type: 'number', length: 2, start: 1},
1539 \ #{type_bufnr: 0, id: 0, col: 21, end: 1, type: 'number', length: 2, start: 1},
1540 \ #{type_bufnr: 0, id: 0, col: 43, end: 1, type: 'number', length: 2, start: 1}]
1541
1542 s/end\nstart/joined/
1543 s/end\n.*\nstart/joined/
1544 call assert_equal('This is some joined is highlighted joined is also highlighted', getline(1))
1545 call assert_equal(expected, prop_list(1))
1546
1547 call prop_type_delete('number')
1548 bwipe!
1549endfunc
1550
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001551func SaveOptions()
1552 let d = #{tabstop: &tabstop,
1553 \ softtabstop: &softtabstop,
1554 \ shiftwidth: &shiftwidth,
1555 \ expandtab: &expandtab,
1556 \ foldmethod: '"' .. &foldmethod .. '"',
1557 \ }
1558 return d
1559endfunc
1560
1561func RestoreOptions(dict)
1562 for name in keys(a:dict)
1563 exe 'let &' .. name .. ' = ' .. a:dict[name]
1564 endfor
1565endfunc
1566
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001567func Test_textprop_noexpandtab()
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001568 new
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001569 let save_dict = SaveOptions()
1570
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001571 set tabstop=8
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001572 set softtabstop=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001573 set shiftwidth=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001574 set noexpandtab
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001575 set foldmethod=marker
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001576
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001577 call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx")
1578 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1579 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1580 call feedkeys("0i\<tab>", "tx")
1581 call prop_remove({'type': 'test'})
1582 call prop_add(1, 2, {'end_col': 3, 'type': 'test'})
1583 call feedkeys("A\<left>\<tab>", "tx")
1584 call prop_remove({'type': 'test'})
1585 try
1586 " It is correct that this does not pass
1587 call prop_add(1, 6, {'end_col': 7, 'type': 'test'})
1588 " Has already collapsed here, start_col:6 does not result in an error
1589 call feedkeys("A\<left>\<tab>", "tx")
1590 catch /^Vim\%((\a\+)\)\=:E964/
1591 endtry
1592 call prop_remove({'type': 'test'})
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001593 call prop_type_delete('test')
1594
1595 call RestoreOptions(save_dict)
1596 bwipe!
1597endfunc
1598
1599func Test_textprop_noexpandtab_redraw()
1600 new
1601 let save_dict = SaveOptions()
1602
1603 set tabstop=8
1604 set softtabstop=4
1605 set shiftwidth=4
1606 set noexpandtab
1607 set foldmethod=marker
1608
1609 call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx")
1610 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1611 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1612 call feedkeys("0i\<tab>", "tx")
1613 " Internally broken at the next line
1614 call feedkeys("A\<left>\<tab>", "tx")
1615 redraw
1616 " Index calculation failed internally on next line
1617 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1618 call prop_remove({'type': 'test', 'all': v:true})
1619 call prop_type_delete('test')
1620 call prop_type_delete('test')
1621
1622 call RestoreOptions(save_dict)
1623 bwipe!
1624endfunc
1625
1626func Test_textprop_ins_str()
1627 new
1628 call setline(1, 'just some text')
1629 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1630 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001631 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 +01001632
1633 call feedkeys("foi\<F8>\<Esc>", "tx")
1634 call assert_equal('just s<F8>ome text', getline(1))
Martin Tournoije2390c72021-07-28 13:30:16 +02001635 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 +01001636
1637 bwipe!
1638 call prop_remove({'type': 'test'})
1639 call prop_type_delete('test')
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001640endfunc
Bram Moolenaar66b98852020-03-11 19:15:52 +01001641
1642func Test_find_prop_later_in_line()
1643 new
1644 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1645 call setline(1, 'just some text')
1646 call prop_add(1, 1, {'length': 4, 'type': 'test'})
1647 call prop_add(1, 10, {'length': 3, 'type': 'test'})
1648
Martin Tournoije2390c72021-07-28 13:30:16 +02001649 call assert_equal(
1650 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 10, end: 1, type: 'test', length: 3, start: 1},
1651 \ prop_find(#{type: 'test', lnum: 1, col: 6}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001652
1653 bwipe!
1654 call prop_type_delete('test')
1655endfunc
1656
1657func Test_find_zerowidth_prop_sol()
1658 new
1659 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1660 call setline(1, 'just some text')
1661 call prop_add(1, 1, {'length': 0, 'type': 'test'})
1662
Martin Tournoije2390c72021-07-28 13:30:16 +02001663 call assert_equal(
1664 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 1, end: 1, type: 'test', length: 0, start: 1},
1665 \ prop_find(#{type: 'test', lnum: 1}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001666
1667 bwipe!
1668 call prop_type_delete('test')
1669endfunc
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001670
1671" Test for passing invalid arguments to prop_xxx() functions
1672func Test_prop_func_invalid_args()
1673 call assert_fails('call prop_clear(1, 2, [])', 'E715:')
1674 call assert_fails('call prop_clear(-1, 2)', 'E16:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001675 call assert_fails('call prop_find(test_null_dict())', 'E1297:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001676 call assert_fails('call prop_find({"bufnr" : []})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001677 call assert_fails('call prop_find({})', 'E968:')
1678 call assert_fails('call prop_find({}, "x")', 'E474:')
1679 call assert_fails('call prop_find({"lnum" : -2})', 'E16:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001680 call assert_fails('call prop_list(1, [])', 'E1206:')
Bram Moolenaar9d489562020-07-30 20:08:50 +02001681 call assert_fails('call prop_list(-1, {})', 'E16:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001682 call assert_fails('call prop_remove([])', 'E1206:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001683 call assert_fails('call prop_remove({}, -2)', 'E16:')
1684 call assert_fails('call prop_remove({})', 'E968:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001685 call assert_fails('call prop_type_add([], {})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001686 call assert_fails("call prop_type_change('long', {'xyz' : 10})", 'E971:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001687 call assert_fails("call prop_type_delete([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001688 call assert_fails("call prop_type_delete('xyz', [])", 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001689 call assert_fails("call prop_type_get([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001690 call assert_fails("call prop_type_get('', [])", 'E474:')
1691 call assert_fails("call prop_type_list([])", 'E715:')
Bram Moolenaar3dc34742021-03-02 13:36:47 +01001692 call assert_fails("call prop_type_add('yyy', 'not_a_dict')", 'E715:')
1693 call assert_fails("call prop_add(1, 5, {'type':'missing_type', 'length':1})", 'E971:')
1694 call assert_fails("call prop_add(1, 5, {'type': ''})", 'E971:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001695 call assert_fails('call prop_add(1, 1, 0)', 'E1206:')
Bram Moolenaar3dc34742021-03-02 13:36:47 +01001696
1697 new
1698 call setline(1, ['first', 'second'])
1699 call prop_type_add('xxx', {})
1700
1701 call assert_fails("call prop_type_add('xxx', {})", 'E969:')
1702 call assert_fails("call prop_add(2, 0, {'type': 'xxx'})", 'E964:')
1703 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':1})", 'E475:')
1704 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':3})", 'E966:')
1705 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'length':-1})", 'E475:')
1706 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_col':0})", 'E475:')
1707 call assert_fails("call prop_add(2, 3, {'length':1})", 'E965:')
1708
1709 call prop_type_delete('xxx')
1710 bwipe!
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001711endfunc
1712
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001713func Test_prop_split_join()
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001714 new
1715 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1716 call setline(1, 'just some text')
1717 call prop_add(1, 6, {'length': 4, 'type': 'test'})
1718
1719 " Split in middle of "some"
1720 execute "normal! 8|i\<CR>"
Martin Tournoije2390c72021-07-28 13:30:16 +02001721 call assert_equal(
1722 \ [#{type_bufnr: 0, id: 0, col: 6, end: 0, type: 'test', length: 2, start: 1}],
1723 \ prop_list(1))
1724 call assert_equal(
1725 \ [#{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'test', length: 2, start: 0}],
1726 \ prop_list(2))
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001727
1728 " Join the two lines back together
1729 normal! 1GJ
Martin Tournoije2390c72021-07-28 13:30:16 +02001730 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 +02001731
1732 bwipe!
1733 call prop_type_delete('test')
1734endfunc
1735
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001736func Test_prop_increment_decrement()
1737 new
1738 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1739 call setline(1, 'its 998 times')
1740 call prop_add(1, 5, {'length': 3, 'type': 'test'})
1741
1742 exe "normal! 0f9\<C-A>"
1743 eval getline(1)->assert_equal('its 999 times')
1744 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001745 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 3, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001746
1747 exe "normal! 0f9\<C-A>"
1748 eval getline(1)->assert_equal('its 1000 times')
1749 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001750 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 4, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001751
1752 bwipe!
1753 call prop_type_delete('test')
1754endfunc
1755
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001756func Test_prop_block_insert()
1757 new
1758 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1759 call setline(1, ['one ', 'two '])
1760 call prop_add(1, 1, {'length': 3, 'type': 'test'})
1761 call prop_add(2, 1, {'length': 3, 'type': 'test'})
1762
1763 " insert "xx" in the first column of both lines
1764 exe "normal! gg0\<C-V>jIxx\<Esc>"
1765 eval getline(1, 2)->assert_equal(['xxone ', 'xxtwo '])
Martin Tournoije2390c72021-07-28 13:30:16 +02001766 let expected = [#{type_bufnr: 0, id: 0, col: 3, end: 1, type: 'test', length: 3, start: 1}]
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001767 eval prop_list(1)->assert_equal(expected)
1768 eval prop_list(2)->assert_equal(expected)
1769
1770 " insert "yy" inside the text props to make them longer
1771 exe "normal! gg03l\<C-V>jIyy\<Esc>"
1772 eval getline(1, 2)->assert_equal(['xxoyyne ', 'xxtyywo '])
1773 let expected[0].length = 5
1774 eval prop_list(1)->assert_equal(expected)
1775 eval prop_list(2)->assert_equal(expected)
1776
1777 " insert "zz" after the text props, text props don't change
1778 exe "normal! gg07l\<C-V>jIzz\<Esc>"
1779 eval getline(1, 2)->assert_equal(['xxoyynezz ', 'xxtyywozz '])
1780 eval prop_list(1)->assert_equal(expected)
1781 eval prop_list(2)->assert_equal(expected)
1782
1783 bwipe!
1784 call prop_type_delete('test')
1785endfunc
1786
Bram Moolenaar23999d72020-12-23 14:36:00 +01001787" this was causing an ml_get error because w_botline was wrong
1788func Test_prop_one_line_window()
1789 enew
1790 call range(2)->setline(1)
1791 call prop_type_add('testprop', {})
1792 call prop_add(1, 1, {'type': 'testprop'})
1793 call popup_create('popup', {'textprop': 'testprop'})
1794 $
1795 new
1796 wincmd _
1797 call feedkeys("\r", 'xt')
1798 redraw
1799
1800 call popup_clear()
1801 call prop_type_delete('testprop')
1802 close
1803 bwipe!
1804endfunc
1805
Bram Moolenaarf05a1e52022-08-02 11:48:53 +01001806def Test_prop_column_zero_error()
1807 prop_type_add('proptype', {highlight: 'Search'})
1808 var caught = false
1809 try
1810 popup_create([{
1811 text: 'a',
1812 props: [{col: 0, length: 1, type: 'type'}],
1813 }], {})
1814 catch /E964:/
1815 caught = true
1816 endtry
1817 assert_true(caught)
1818
1819 popup_clear()
1820 prop_type_delete('proptype')
1821enddef
1822
Bram Moolenaar840f91f2021-05-26 22:32:10 +02001823" This was calling ml_append_int() and copy a text property from a previous
1824" line at the wrong moment. Exact text length matters.
1825def Test_prop_splits_data_block()
1826 new
1827 var lines: list<string> = [repeat('x', 35)]->repeat(41)
1828 + [repeat('!', 35)]
1829 + [repeat('x', 35)]->repeat(56)
1830 lines->setline(1)
1831 prop_type_add('someprop', {highlight: 'ErrorMsg'})
1832 prop_add(1, 27, {end_lnum: 1, end_col: 70, type: 'someprop'})
1833 prop_remove({type: 'someprop'}, 1)
1834 prop_add(35, 22, {end_lnum: 43, end_col: 43, type: 'someprop'})
1835 prop_remove({type: 'someprop'}, 35, 43)
1836 assert_equal([], prop_list(42))
1837
1838 bwipe!
1839 prop_type_delete('someprop')
1840enddef
1841
Bram Moolenaar4cd5c522021-06-27 13:04:00 +02001842" This was calling ml_delete_int() and try to change text properties.
1843def Test_prop_add_delete_line()
1844 new
1845 var a = 10
1846 var b = 20
1847 repeat([''], a)->append('$')
1848 prop_type_add('Test', {highlight: 'ErrorMsg'})
1849 for lnum in range(1, a)
1850 for col in range(1, b)
1851 prop_add(1, 1, {end_lnum: lnum, end_col: col, type: 'Test'})
1852 endfor
1853 endfor
1854
1855 # check deleting lines is OK
1856 :5del
1857 :1del
1858 :$del
1859
1860 prop_type_delete('Test')
1861 bwipe!
1862enddef
1863
Paul Ollis1bdc60e2022-05-15 22:24:55 +01001864" This test is to detect a regression related to #10430. It is not an attempt
1865" fully cover deleting lines in the presence of multi-line properties.
1866def Test_delete_line_within_multiline_prop()
1867 new
1868 setline(1, '# Top.')
1869 append(1, ['some_text = """', 'A string.', '"""', '# Bottom.'])
1870 prop_type_add('Identifier', {'highlight': 'ModeMsg', 'priority': 0, 'combine': 0, 'start_incl': 0, 'end_incl': 0})
1871 prop_type_add('String', {'highlight': 'MoreMsg', 'priority': 0, 'combine': 0, 'start_incl': 0, 'end_incl': 0})
1872 prop_add(2, 1, {'type': 'Identifier', 'end_lnum': 2, 'end_col': 9})
1873 prop_add(2, 13, {'type': 'String', 'end_lnum': 4, 'end_col': 4})
1874
1875 # The property for line 3 should extend into the previous and next lines.
1876 var props = prop_list(3)
1877 var prop = props[0]
1878 assert_equal(1, len(props))
1879 assert_equal(0, prop['start'])
1880 assert_equal(0, prop['end'])
1881
1882 # This deletion should run without raising an exception.
1883 try
1884 :2 del
1885 catch
1886 assert_report('Line delete should have workd, but it raised an error.')
1887 endtry
1888
1889 # The property for line 2 (was 3) should no longer extend into the previous
1890 # line.
1891 props = prop_list(2)
1892 prop = props[0]
1893 assert_equal(1, len(props))
1894 assert_equal(1, prop['start'], 'Property was not changed to start within the line.')
1895
1896 # This deletion should run without raising an exception.
1897 try
1898 :3 del
1899 catch
1900 assert_report('Line delete should have workd, but it raised an error.')
1901 endtry
1902
1903 # The property for line 2 (originally 3) should no longer extend into the next
1904 # line.
1905 props = prop_list(2)
1906 prop = props[0]
1907 assert_equal(1, len(props))
1908 assert_equal(1, prop['end'], 'Property was not changed to end within the line.')
1909
1910 prop_type_delete('Identifier')
1911 prop_type_delete('String')
1912 bwip!
1913enddef
1914
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001915func Test_prop_in_linebreak()
1916 CheckRunVimInTerminal
1917
1918 let lines =<< trim END
1919 set breakindent linebreak breakat+=]
1920 call printf('%s]%s', repeat('x', 50), repeat('x', 70))->setline(1)
Bram Moolenaarcf2bb632022-09-02 13:26:29 +01001921 call prop_type_add('test', #{highlight: 'MatchParen'})
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001922 call prop_add(1, 51, #{length: 1, type: 'test'})
Bram Moolenaarcf2bb632022-09-02 13:26:29 +01001923 func AddMatch()
1924 syntax on
1925 syntax match xTest /.*/
1926 hi link xTest Comment
1927 set signcolumn=yes
1928 endfunc
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001929 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001930 call writefile(lines, 'XscriptPropLinebreak', 'D')
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001931 let buf = RunVimInTerminal('-S XscriptPropLinebreak', #{rows: 10})
Bram Moolenaarcf2bb632022-09-02 13:26:29 +01001932 call VerifyScreenDump(buf, 'Test_prop_linebreak_1', {})
1933
1934 call term_sendkeys(buf, ":call AddMatch()\<CR>")
1935 call VerifyScreenDump(buf, 'Test_prop_linebreak_2', {})
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001936
1937 call StopVimInTerminal(buf)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001938endfunc
1939
Bram Moolenaar52de3a82022-08-10 13:12:03 +01001940func Test_prop_with_linebreak()
1941 CheckRunVimInTerminal
1942
1943 let lines =<< trim END
1944 vim9script
1945 set linebreak
1946 setline(1, 'one twoword')
1947 prop_type_add('test', {highlight: 'Special'})
1948 prop_add(1, 4, {text: ': virtual text', type: 'test', text_wrap: 'wrap'})
1949 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001950 call writefile(lines, 'XscriptPropWithLinebreak', 'D')
Bram Moolenaar52de3a82022-08-10 13:12:03 +01001951 let buf = RunVimInTerminal('-S XscriptPropWithLinebreak', #{rows: 6, cols: 50})
1952 call VerifyScreenDump(buf, 'Test_prop_with_linebreak_1', {})
1953 call term_sendkeys(buf, "iasdf asdf asdf asdf asdf as\<Esc>")
1954 call VerifyScreenDump(buf, 'Test_prop_with_linebreak_2', {})
1955
1956 call StopVimInTerminal(buf)
Bram Moolenaar52de3a82022-08-10 13:12:03 +01001957endfunc
1958
Bram Moolenaar1d8844a2022-08-10 13:39:35 +01001959func Test_prop_with_wrap()
1960 CheckRunVimInTerminal
1961
1962 let lines =<< trim END
1963 vim9script
1964 set linebreak
1965 setline(1, 'asdf '->repeat(15))
1966 prop_type_add('test', {highlight: 'Special'})
1967 prop_add(1, 43, {text: 'some virtual text', type: 'test'})
1968 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001969 call writefile(lines, 'XscriptPropWithWrap', 'D')
Bram Moolenaar1d8844a2022-08-10 13:39:35 +01001970 let buf = RunVimInTerminal('-S XscriptPropWithWrap', #{rows: 6, cols: 50})
1971 call VerifyScreenDump(buf, 'Test_prop_with_wrap_1', {})
1972
1973 call StopVimInTerminal(buf)
Bram Moolenaar1d8844a2022-08-10 13:39:35 +01001974endfunc
1975
Bram Moolenaar42eba042021-11-30 20:22:49 +00001976func Test_prop_after_tab()
1977 CheckRunVimInTerminal
1978
1979 let lines =<< trim END
1980 set breakindent linebreak breakat+=]
1981 call setline(1, "\t[xxx]")
1982 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1983 call prop_add(1, 2, #{length: 1, type: 'test'})
1984 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001985 call writefile(lines, 'XscriptPropAfterTab', 'D')
Bram Moolenaar42eba042021-11-30 20:22:49 +00001986 let buf = RunVimInTerminal('-S XscriptPropAfterTab', #{rows: 10})
Bram Moolenaar42eba042021-11-30 20:22:49 +00001987 call VerifyScreenDump(buf, 'Test_prop_after_tab', {})
1988
1989 call StopVimInTerminal(buf)
Bram Moolenaar42eba042021-11-30 20:22:49 +00001990endfunc
1991
Bram Moolenaare428fa02022-08-09 16:55:41 +01001992func Test_prop_before_tab()
1993 CheckRunVimInTerminal
1994
1995 let lines =<< trim END
1996 call setline(1, ["\tx"]->repeat(6))
1997 call prop_type_add('test', #{highlight: 'Search'})
1998 call prop_add(1, 1, #{type: 'test', text: '123'})
1999 call prop_add(2, 1, #{type: 'test', text: '1234567'})
2000 call prop_add(3, 1, #{type: 'test', text: '12345678'})
2001 call prop_add(4, 1, #{type: 'test', text: '123456789'})
2002 call prop_add(5, 2, #{type: 'test', text: 'ABC'})
2003 call prop_add(6, 3, #{type: 'test', text: 'ABC'})
2004 normal gg0
2005 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002006 call writefile(lines, 'XscriptPropBeforeTab', 'D')
Bram Moolenaare428fa02022-08-09 16:55:41 +01002007 let buf = RunVimInTerminal('-S XscriptPropBeforeTab', #{rows: 8})
2008 call VerifyScreenDump(buf, 'Test_prop_before_tab_01', {})
2009 call term_sendkeys(buf, "$")
2010 call VerifyScreenDump(buf, 'Test_prop_before_tab_02', {})
2011 call term_sendkeys(buf, "j0")
2012 call VerifyScreenDump(buf, 'Test_prop_before_tab_03', {})
2013 call term_sendkeys(buf, "$")
2014 call VerifyScreenDump(buf, 'Test_prop_before_tab_04', {})
2015 call term_sendkeys(buf, "j0")
2016 call VerifyScreenDump(buf, 'Test_prop_before_tab_05', {})
2017 call term_sendkeys(buf, "$")
2018 call VerifyScreenDump(buf, 'Test_prop_before_tab_06', {})
2019 call term_sendkeys(buf, "j0")
2020 call VerifyScreenDump(buf, 'Test_prop_before_tab_07', {})
2021 call term_sendkeys(buf, "$")
2022 call VerifyScreenDump(buf, 'Test_prop_before_tab_08', {})
2023 call term_sendkeys(buf, "j")
2024 call VerifyScreenDump(buf, 'Test_prop_before_tab_09', {})
2025 call term_sendkeys(buf, "j")
2026 call VerifyScreenDump(buf, 'Test_prop_before_tab_10', {})
2027
2028 call StopVimInTerminal(buf)
Bram Moolenaare428fa02022-08-09 16:55:41 +01002029endfunc
2030
Bram Moolenaaracdc9112021-12-02 19:46:57 +00002031func Test_prop_after_linebreak()
2032 CheckRunVimInTerminal
2033
2034 let lines =<< trim END
2035 set linebreak wrap
2036 call printf('%s+(%s)', 'x'->repeat(&columns / 2), 'x'->repeat(&columns / 2))->setline(1)
2037 call prop_type_add('test', #{highlight: 'ErrorMsg'})
2038 call prop_add(1, (&columns / 2) + 2, #{length: 1, type: 'test'})
2039 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002040 call writefile(lines, 'XscriptPropAfterLinebreak', 'D')
Bram Moolenaaracdc9112021-12-02 19:46:57 +00002041 let buf = RunVimInTerminal('-S XscriptPropAfterLinebreak', #{rows: 10})
Bram Moolenaaracdc9112021-12-02 19:46:57 +00002042 call VerifyScreenDump(buf, 'Test_prop_after_linebreak', {})
2043
2044 call StopVimInTerminal(buf)
Bram Moolenaaracdc9112021-12-02 19:46:57 +00002045endfunc
2046
Martin Tournoije2390c72021-07-28 13:30:16 +02002047" Buffer number of 0 should be ignored, as if the parameter wasn't passed.
2048def Test_prop_bufnr_zero()
2049 new
2050 try
2051 var bufnr = bufnr('')
2052 setline(1, 'hello')
2053 prop_type_add('bufnr-global', {highlight: 'ErrorMsg'})
2054 prop_type_add('bufnr-buffer', {highlight: 'StatusLine', bufnr: bufnr})
2055
2056 prop_add(1, 1, {type: 'bufnr-global', length: 1})
2057 prop_add(1, 2, {type: 'bufnr-buffer', length: 1})
2058
2059 var list = prop_list(1)
2060 assert_equal([
2061 {id: 0, col: 1, type_bufnr: 0, end: 1, type: 'bufnr-global', length: 1, start: 1},
2062 {id: 0, col: 2, type_bufnr: bufnr, end: 1, type: 'bufnr-buffer', length: 1, start: 1},
2063 ], list)
2064
2065 assert_equal(
2066 {highlight: 'ErrorMsg', end_incl: 0, start_incl: 0, priority: 0, combine: 1},
2067 prop_type_get('bufnr-global', {bufnr: list[0].type_bufnr}))
2068
2069 assert_equal(
2070 {highlight: 'StatusLine', end_incl: 0, start_incl: 0, priority: 0, bufnr: bufnr, combine: 1},
2071 prop_type_get('bufnr-buffer', {bufnr: list[1].type_bufnr}))
2072 finally
2073 bwipe!
2074 prop_type_delete('bufnr-global')
2075 endtry
2076enddef
2077
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002078" Tests for the prop_list() function
2079func Test_prop_list()
2080 let lines =<< trim END
2081 new
Bram Moolenaar62aec932022-01-29 21:45:34 +00002082 call g:AddPropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002083 call setline(1, repeat([repeat('a', 60)], 10))
2084 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
2085 call prop_add(1, 5, {'type': 'two', 'id': 10, 'end_col': 7})
2086 call prop_add(3, 12, {'type': 'one', 'id': 20, 'end_col': 14})
2087 call prop_add(3, 13, {'type': 'two', 'id': 10, 'end_col': 15})
2088 call prop_add(5, 20, {'type': 'one', 'id': 10, 'end_col': 22})
2089 call prop_add(5, 21, {'type': 'two', 'id': 20, 'end_col': 23})
2090 call assert_equal([
2091 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2092 \ 'type': 'one', 'length': 2, 'start': 1},
2093 \ {'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
2094 \ 'type': 'two', 'length': 2, 'start': 1}], prop_list(1))
2095 #" text properties between a few lines
2096 call assert_equal([
2097 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2098 \ 'type': 'one', 'length': 2, 'start': 1},
2099 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
2100 \ 'type': 'two', 'length': 2, 'start': 1},
2101 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
2102 \ 'type': 'one', 'length': 2, 'start': 1},
2103 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
2104 \ 'type': 'two', 'length': 2, 'start': 1}],
2105 \ prop_list(2, {'end_lnum': 5}))
2106 #" text properties across all the lines
2107 call assert_equal([
2108 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2109 \ 'type': 'one', 'length': 2, 'start': 1},
2110 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2111 \ 'type': 'one', 'length': 2, 'start': 1},
2112 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
2113 \ 'type': 'one', 'length': 2, 'start': 1}],
2114 \ prop_list(1, {'types': ['one'], 'end_lnum': -1}))
2115 #" text properties with the specified identifier
2116 call assert_equal([
2117 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2118 \ 'type': 'one', 'length': 2, 'start': 1},
2119 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
2120 \ 'type': 'two', 'length': 2, 'start': 1}],
2121 \ prop_list(1, {'ids': [20], 'end_lnum': 10}))
2122 #" text properties of the specified type and id
2123 call assert_equal([
2124 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
2125 \ 'type': 'two', 'length': 2, 'start': 1},
2126 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
2127 \ 'type': 'two', 'length': 2, 'start': 1}],
2128 \ prop_list(1, {'types': ['two'], 'ids': [10], 'end_lnum': 20}))
2129 call assert_equal([], prop_list(1, {'ids': [40, 50], 'end_lnum': 10}))
2130 call assert_equal([], prop_list(6, {'end_lnum': 10}))
2131 call assert_equal([], prop_list(2, {'end_lnum': 2}))
2132 #" error cases
2133 call assert_fails("echo prop_list(1, {'end_lnum': -20})", 'E16:')
2134 call assert_fails("echo prop_list(4, {'end_lnum': 2})", 'E16:')
2135 call assert_fails("echo prop_list(1, {'end_lnum': '$'})", 'E889:')
2136 call assert_fails("echo prop_list(1, {'types': ['blue'], 'end_lnum': 10})",
2137 \ 'E971:')
2138 call assert_fails("echo prop_list(1, {'types': ['one', 'blue'],
2139 \ 'end_lnum': 10})", 'E971:')
2140 call assert_fails("echo prop_list(1, {'types': ['one', 10],
2141 \ 'end_lnum': 10})", 'E928:')
2142 call assert_fails("echo prop_list(1, {'types': ['']})", 'E971:')
2143 call assert_equal([], prop_list(2, {'types': []}))
2144 call assert_equal([], prop_list(2, {'types': test_null_list()}))
2145 call assert_fails("call prop_list(1, {'types': {}})", 'E714:')
2146 call assert_fails("call prop_list(1, {'types': 'one'})", 'E714:')
2147 call assert_equal([], prop_list(2, {'types': ['one'],
2148 \ 'ids': test_null_list()}))
2149 call assert_equal([], prop_list(2, {'types': ['one'], 'ids': []}))
2150 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': {}})",
2151 \ 'E714:')
2152 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': 10})",
2153 \ 'E714:')
2154 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [[]]})",
2155 \ 'E745:')
2156 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [10, []]})",
2157 \ 'E745:')
Martin Tournoije2390c72021-07-28 13:30:16 +02002158
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002159 #" get text properties from a non-current buffer
2160 wincmd w
2161 call assert_equal([
2162 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2163 \ 'type': 'one', 'length': 2, 'start': 1},
2164 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
2165 \ 'type': 'two', 'length': 2, 'start': 1},
2166 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2167 \ 'type': 'one', 'length': 2, 'start': 1},
2168 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
2169 \ 'type': 'two', 'length': 2, 'start': 1}],
2170 \ prop_list(1, {'bufnr': winbufnr(1), 'end_lnum': 4}))
2171 wincmd w
2172
2173 #" get text properties after clearing all the properties
2174 call prop_clear(1, line('$'))
2175 call assert_equal([], prop_list(1, {'end_lnum': 10}))
2176
2177 call prop_add(2, 4, {'type': 'one', 'id': 5, 'end_col': 6})
2178 call prop_add(2, 4, {'type': 'two', 'id': 10, 'end_col': 6})
2179 call prop_add(2, 4, {'type': 'three', 'id': 15, 'end_col': 6})
2180 #" get text properties with a list of types
2181 call assert_equal([
2182 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
2183 \ 'type': 'two', 'length': 2, 'start': 1},
2184 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2185 \ 'type': 'one', 'length': 2, 'start': 1}],
2186 \ prop_list(2, {'types': ['one', 'two']}))
2187 call assert_equal([
2188 \ {'id': 15, 'col': 4, 'type_bufnr': 0, 'end': 1,
2189 \ 'type': 'three', 'length': 2, 'start': 1},
2190 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2191 \ 'type': 'one', 'length': 2, 'start': 1}],
2192 \ prop_list(2, {'types': ['one', 'three']}))
2193 #" get text properties with a list of identifiers
2194 call assert_equal([
2195 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
2196 \ 'type': 'two', 'length': 2, 'start': 1},
2197 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2198 \ 'type': 'one', 'length': 2, 'start': 1}],
2199 \ prop_list(2, {'ids': [5, 10, 20]}))
2200 call prop_clear(1, line('$'))
2201 call assert_equal([], prop_list(2, {'types': ['one', 'two']}))
2202 call assert_equal([], prop_list(2, {'ids': [5, 10, 20]}))
2203
2204 #" get text properties from a hidden buffer
2205 edit! Xaaa
2206 call setline(1, repeat([repeat('b', 60)], 10))
2207 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
2208 call prop_add(4, 8, {'type': 'two', 'id': 10, 'end_col': 10})
2209 VAR bnr = bufnr()
2210 hide edit Xbbb
2211 call assert_equal([
2212 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2213 \ 'type': 'one', 'length': 2, 'start': 1},
2214 \ {'lnum': 4, 'id': 10, 'col': 8, 'type_bufnr': 0, 'end': 1,
2215 \ 'type': 'two', 'length': 2, 'start': 1}],
2216 \ prop_list(1, {'bufnr': bnr,
2217 \ 'types': ['one', 'two'], 'ids': [5, 10], 'end_lnum': -1}))
2218 #" get text properties from an unloaded buffer
2219 bunload! Xaaa
2220 call assert_equal([], prop_list(1, {'bufnr': bnr, 'end_lnum': -1}))
2221
Bram Moolenaar62aec932022-01-29 21:45:34 +00002222 call g:DeletePropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002223 :%bw!
2224 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002225 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002226endfunc
Bram Moolenaar23999d72020-12-23 14:36:00 +01002227
LemonBoy9bd3ce22022-04-18 21:54:02 +01002228func Test_prop_find_prev_on_same_line()
2229 new
2230
2231 call setline(1, 'the quikc bronw fox jumsp over the layz dog')
2232 call prop_type_add('misspell', #{highlight: 'ErrorMsg'})
2233 for col in [8, 14, 24, 38]
2234 call prop_add(1, col, #{type: 'misspell', length: 2})
2235 endfor
2236
Ben Jacksona7704222022-08-20 20:54:51 +01002237 call cursor(1, 18)
LemonBoy9bd3ce22022-04-18 21:54:02 +01002238 let expected = [
2239 \ #{lnum: 1, id: 0, col: 14, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1},
2240 \ #{lnum: 1, id: 0, col: 24, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1}
2241 \ ]
2242
2243 let result = prop_find(#{type: 'misspell'}, 'b')
2244 call assert_equal(expected[0], result)
2245 let result = prop_find(#{type: 'misspell'}, 'f')
2246 call assert_equal(expected[1], result)
2247
2248 call prop_type_delete('misspell')
2249 bwipe!
2250endfunc
2251
LemonBoyb7a70122022-05-13 12:41:50 +01002252func Test_prop_spell()
2253 new
2254 set spell
2255 call AddPropTypes()
2256
2257 call setline(1, ["helo world", "helo helo helo"])
2258 call prop_add(1, 1, #{type: 'one', length: 4})
2259 call prop_add(1, 6, #{type: 'two', length: 5})
2260 call prop_add(2, 1, #{type: 'three', length: 4})
2261 call prop_add(2, 6, #{type: 'three', length: 4})
2262 call prop_add(2, 11, #{type: 'three', length: 4})
2263
2264 " The first prop over 'helo' increases its length after the word is corrected
2265 " to 'Hello', the second one is shifted to the right.
2266 let expected = [
2267 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2268 \ 'length': 5, 'start': 1},
2269 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2270 \ 'length': 5, 'start': 1}
2271 \ ]
2272 call feedkeys("z=1\<CR>", 'xt')
2273
2274 call assert_equal('Hello world', getline(1))
2275 call assert_equal(expected, prop_list(1))
2276
2277 " Repeat the replacement done by z=
2278 spellrepall
2279
2280 let expected = [
2281 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2282 \ 'length': 5, 'start': 1},
2283 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2284 \ 'length': 5, 'start': 1},
2285 \ {'id': 0, 'col': 13, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2286 \ 'length': 5, 'start': 1}
2287 \ ]
2288 call assert_equal('Hello Hello Hello', getline(2))
2289 call assert_equal(expected, prop_list(2))
2290
2291 call DeletePropTypes()
2292 set spell&
2293 bwipe!
2294endfunc
2295
LemonBoy4b936742022-05-13 21:56:28 +01002296func Test_prop_shift_block()
2297 new
2298 call AddPropTypes()
2299
2300 call setline(1, ['some highlighted text']->repeat(2))
2301 call prop_add(1, 10, #{type: 'one', length: 11})
2302 call prop_add(2, 10, #{type: 'two', length: 11})
2303
2304 call cursor(1, 1)
2305 call feedkeys("5l\<c-v>>", 'nxt')
2306 call cursor(2, 1)
2307 call feedkeys("5l\<c-v><", 'nxt')
2308
2309 let expected = [
2310 \ {'lnum': 1, 'id': 0, 'col': 8, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2311 \ 'length': 11, 'start' : 1},
2312 \ {'lnum': 2, 'id': 0, 'col': 6, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2313 \ 'length': 11, 'start' : 1}
2314 \ ]
2315 call assert_equal(expected, prop_list(1, #{end_lnum: 2}))
2316
2317 call DeletePropTypes()
2318 bwipe!
2319endfunc
LemonBoyb7a70122022-05-13 12:41:50 +01002320
LemonBoy698cb4c2022-05-14 18:10:15 +01002321func Test_prop_insert_multiline()
2322 new
2323 call AddPropTypes()
2324
2325 call setline(1, ['foobar', 'barbaz'])
2326 call prop_add(1, 4, #{end_lnum: 2, end_col: 4, type: 'one'})
2327
2328 call feedkeys("1Goquxqux\<Esc>", 'nxt')
2329 call feedkeys("2GOquxqux\<Esc>", 'nxt')
2330
2331 let lines =<< trim END
2332 foobar
2333 quxqux
2334 quxqux
2335 barbaz
2336 END
2337 call assert_equal(lines, getline(1, '$'))
2338 let expected = [
2339 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 0, 'type': 'one',
Ben Jacksona7704222022-08-20 20:54:51 +01002340 \ 'length': 4 , 'start': 1},
LemonBoy698cb4c2022-05-14 18:10:15 +01002341 \ {'lnum': 2, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2342 \ 'length': 7, 'start': 0},
2343 \ {'lnum': 3, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2344 \ 'length': 7, 'start': 0},
2345 \ {'lnum': 4, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2346 \ 'length': 3, 'start': 0}
2347 \ ]
2348 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
2349
2350 call DeletePropTypes()
2351 bwipe!
2352endfunc
2353
LemonBoyb559b302022-05-15 13:08:02 +01002354func Test_prop_blockwise_change()
2355 new
2356 call AddPropTypes()
2357
2358 call setline(1, ['foooooo', 'bar', 'baaaaz'])
2359 call prop_add(1, 1, #{end_col: 3, type: 'one'})
2360 call prop_add(2, 1, #{end_col: 3, type: 'two'})
2361 call prop_add(3, 1, #{end_col: 3, type: 'three'})
2362
2363 " Replace the first two columns with '123', since 'start_incl' is false the
2364 " prop is not extended.
2365 call feedkeys("gg\<c-v>2jc123\<Esc>", 'nxt')
2366
2367 let lines =<< trim END
2368 123oooooo
2369 123ar
2370 123aaaaz
2371 END
2372 call assert_equal(lines, getline(1, '$'))
2373 let expected = [
2374 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2375 \ 'length': 1, 'start': 1},
2376 \ {'lnum': 2, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2377 \ 'length': 1, 'start': 1},
2378 \ {'lnum': 3, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1 ,
2379 \ 'type': 'three', 'length': 1, 'start': 1}
2380 \ ]
2381 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
2382
2383 call DeletePropTypes()
2384 bwipe!
2385endfunc
2386
Paul Ollis4c3d21a2022-05-24 21:26:37 +01002387func Do_test_props_do_not_affect_byte_offsets(ff, increment)
2388 new
2389 let lcount = 410
2390
2391 " File format affects byte-offset calculations, so make sure it is known.
2392 exec 'setlocal fileformat=' . a:ff
2393
2394 " Fill the buffer with varying length lines. We need a suitably large number
2395 " to force Vim code through paths wehere previous error have occurred. This
2396 " is more 'art' than 'science'.
2397 let text = 'a'
2398 call setline(1, text)
2399 let offsets = [1]
2400 for idx in range(lcount)
2401 call add(offsets, offsets[idx] + len(text) + a:increment)
2402 if (idx % 6) == 0
2403 let text = text . 'a'
2404 endif
2405 call append(line('$'), text)
2406 endfor
2407
2408 " Set a property that spans a few lines to cause Vim's internal buffer code
2409 " to perform a reasonable amount of rearrangement.
2410 call prop_type_add('one', {'highlight': 'ErrorMsg'})
2411 call prop_add(1, 1, {'type': 'one', 'end_lnum': 6, 'end_col': 2})
2412
2413 for idx in range(lcount)
2414 let boff = line2byte(idx + 1)
2415 call assert_equal(offsets[idx], boff, 'Bad byte offset at line ' . (idx + 1))
2416 endfor
2417
2418 call prop_type_delete('one')
2419 bwipe!
2420endfunc
2421
2422func Test_props_do_not_affect_byte_offsets()
2423 call Do_test_props_do_not_affect_byte_offsets('unix', 1)
2424endfunc
2425
2426func Test_props_do_not_affect_byte_offsets_dos()
2427 call Do_test_props_do_not_affect_byte_offsets('dos', 2)
2428endfunc
2429
2430func Test_props_do_not_affect_byte_offsets_editline()
2431 new
2432 let lcount = 410
2433
2434 " File format affects byte-offset calculations, so make sure it is known.
2435 setlocal fileformat=unix
2436
2437 " Fill the buffer with varying length lines. We need a suitably large number
2438 " to force Vim code through paths wehere previous error have occurred. This
2439 " is more 'art' than 'science'.
2440 let text = 'aa'
2441 call setline(1, text)
2442 let offsets = [1]
2443 for idx in range(lcount)
2444 call add(offsets, offsets[idx] + len(text) + 1)
2445 if (idx % 6) == 0
2446 let text = text . 'a'
2447 endif
2448 call append(line('$'), text)
2449 endfor
2450
2451 " Set a property that just covers the first line. When this test was
2452 " developed, this did not trigger a byte-offset error.
2453 call prop_type_add('one', {'highlight': 'ErrorMsg'})
2454 call prop_add(1, 1, {'type': 'one', 'end_lnum': 1, 'end_col': 3})
2455
2456 for idx in range(lcount)
2457 let boff = line2byte(idx + 1)
2458 call assert_equal(offsets[idx], boff,
2459 \ 'Confounding bad byte offset at line ' . (idx + 1))
2460 endfor
2461
2462 " Insert text in the middle of the first line, keeping the property
2463 " unchanged.
2464 :1
2465 normal aHello
2466 for idx in range(1, lcount)
2467 let offsets[idx] = offsets[idx] + 5
2468 endfor
2469
2470 for idx in range(lcount)
2471 let boff = line2byte(idx + 1)
2472 call assert_equal(offsets[idx], boff,
2473 \ 'Bad byte offset at line ' . (idx + 1))
2474 endfor
2475
2476 call prop_type_delete('one')
2477 bwipe!
2478endfunc
2479
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002480func Test_prop_inserts_text()
2481 CheckRunVimInTerminal
2482
2483 " Just a basic check for now
2484 let lines =<< trim END
2485 call setline(1, 'insert some text here and other text there and some more text after wrapping')
2486 call prop_type_add('someprop', #{highlight: 'ErrorMsg'})
2487 call prop_type_add('otherprop', #{highlight: 'Search'})
2488 call prop_type_add('moreprop', #{highlight: 'DiffAdd'})
2489 call prop_add(1, 18, #{type: 'someprop', text: 'SOME '})
Bram Moolenaar783ef722022-08-01 16:11:06 +01002490 call prop_add(1, 38, #{type: 'otherprop', text: "OTHER\t"})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002491 call prop_add(1, 69, #{type: 'moreprop', text: 'MORE '})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002492 normal $
Bram Moolenaar09ff4b52022-08-01 16:51:02 +01002493
2494 call setline(2, 'prepost')
2495 call prop_type_add('multibyte', #{highlight: 'Visual'})
2496 call prop_add(2, 4, #{type: 'multibyte', text: 'sรถmeๅ’Œๅนณtรฉxt'})
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002497
Bram Moolenaar25463612022-08-08 11:07:47 +01002498 call setline(3, 'Foo foo = { 1, 2 };')
Bram Moolenaar3331dd02022-08-10 16:49:02 +01002499 call prop_type_add('testprop', #{highlight: 'Comment'})
Bram Moolenaar25463612022-08-08 11:07:47 +01002500 call prop_add(3, 13, #{type: 'testprop', text: '.x='})
2501 call prop_add(3, 16, #{type: 'testprop', text: '.y='})
2502
2503 call setline(4, '')
2504 call prop_add(4, 1, #{type: 'someprop', text: 'empty line'})
Bram Moolenaar3331dd02022-08-10 16:49:02 +01002505
2506 call setline(5, 'look highlight')
2507 call prop_type_add('nohi', #{})
2508 call prop_add(5, 6, #{type: 'nohi', text: 'no '})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002509 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002510 call writefile(lines, 'XscriptPropsWithText', 'D')
Bram Moolenaar25463612022-08-08 11:07:47 +01002511 let buf = RunVimInTerminal('-S XscriptPropsWithText', #{rows: 8, cols: 60})
Bram Moolenaar711483c2022-07-30 21:33:46 +01002512 call VerifyScreenDump(buf, 'Test_prop_inserts_text_1', {})
2513
2514 call term_sendkeys(buf, ":set signcolumn=yes\<CR>")
2515 call VerifyScreenDump(buf, 'Test_prop_inserts_text_2', {})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002516
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002517 call term_sendkeys(buf, "2G$")
2518 call VerifyScreenDump(buf, 'Test_prop_inserts_text_3', {})
2519
Bram Moolenaar25463612022-08-08 11:07:47 +01002520 call term_sendkeys(buf, "3Gf1")
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002521 call VerifyScreenDump(buf, 'Test_prop_inserts_text_4', {})
Bram Moolenaar25463612022-08-08 11:07:47 +01002522 call term_sendkeys(buf, "f2")
2523 call VerifyScreenDump(buf, 'Test_prop_inserts_text_5', {})
2524
2525 call term_sendkeys(buf, "4G")
2526 call VerifyScreenDump(buf, 'Test_prop_inserts_text_6', {})
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002527
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002528 call StopVimInTerminal(buf)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002529endfunc
2530
Bram Moolenaare38fc862022-08-11 17:24:50 +01002531func Test_prop_inserts_text_highlight()
2532 CheckRunVimInTerminal
2533
2534 " Just a basic check for now
2535 let lines =<< trim END
2536 call setline(1, 'insert some text (here) and there')
2537 call prop_type_add('someprop', #{highlight: 'ErrorMsg'})
2538 let bef_prop = prop_add(1, 18, #{type: 'someprop', text: 'BEFORE'})
2539 set hlsearch
2540 let thematch = matchaddpos("DiffAdd", [[1, 18]])
2541 func DoAfter()
2542 call prop_remove(#{id: g:bef_prop})
2543 call prop_add(1, 19, #{type: 'someprop', text: 'AFTER'})
2544 let g:thematch = matchaddpos("DiffAdd", [[1, 18]])
2545 let @/ = ''
2546 endfunc
2547 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002548 call writefile(lines, 'XscriptPropsWithHighlight', 'D')
Bram Moolenaare38fc862022-08-11 17:24:50 +01002549 let buf = RunVimInTerminal('-S XscriptPropsWithHighlight', #{rows: 6, cols: 60})
2550 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_1', {})
2551 call term_sendkeys(buf, "/text (he\<CR>")
2552 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_2', {})
2553 call term_sendkeys(buf, ":call matchdelete(thematch)\<CR>")
2554 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_3', {})
2555
2556 call term_sendkeys(buf, ":call DoAfter()\<CR>")
2557 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_4', {})
2558 call term_sendkeys(buf, "/text (he\<CR>")
2559 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_5', {})
2560 call term_sendkeys(buf, ":call matchdelete(thematch)\<CR>")
2561 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_6', {})
2562
2563 call StopVimInTerminal(buf)
Bram Moolenaare38fc862022-08-11 17:24:50 +01002564endfunc
2565
Bram Moolenaarfb593c52022-09-17 18:57:36 +01002566func Test_prop_add_with_text_fails()
2567 call prop_type_add('failing', #{highlight: 'ErrorMsg'})
2568 call assert_fails("call prop_add(1, 0, #{type: 'failing', text: 'X', end_lnum: 1})", 'E1305:')
2569 call assert_fails("call prop_add(1, 0, #{type: 'failing', text: 'X', end_col: 1})", 'E1305:')
2570 call assert_fails("call prop_add(1, 0, #{type: 'failing', text: 'X', length: 1})", 'E1305:')
2571
2572 call prop_type_delete('failing')
2573endfunc
2574
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002575func Test_props_with_text_right_align_twice()
2576 CheckRunVimInTerminal
2577
2578 let lines =<< trim END
2579 call setline(1, ["some text some text some text some text", 'line two'])
Bram Moolenaarfb593c52022-09-17 18:57:36 +01002580 call prop_type_add('MyErrorText', #{highlight: 'ErrorMsg'})
2581 call prop_type_add('MyPadding', #{highlight: 'DiffChange'})
Bram Moolenaarc8bf59e2022-08-28 16:39:22 +01002582 call prop_add(1, 0, #{type: 'MyPadding', text: ' nothing here', text_wrap: 'wrap'})
2583 call prop_add(1, 0, #{type: 'MyErrorText', text: 'Some error', text_wrap: 'wrap', text_align: 'right'})
2584 call prop_add(1, 0, #{type: 'MyErrorText', text: 'Another error', text_wrap: 'wrap', text_align: 'right'})
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002585 normal G$
2586 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002587 call writefile(lines, 'XscriptPropsRightAlign', 'D')
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002588 let buf = RunVimInTerminal('-S XscriptPropsRightAlign', #{rows: 8})
2589 call VerifyScreenDump(buf, 'Test_prop_right_align_twice_1', {})
2590
2591 call term_sendkeys(buf, "ggisome more text\<Esc>G$")
2592 call VerifyScreenDump(buf, 'Test_prop_right_align_twice_2', {})
2593
Bram Moolenaarc8bf59e2022-08-28 16:39:22 +01002594 call term_sendkeys(buf, ":set signcolumn=yes\<CR>")
2595 call VerifyScreenDump(buf, 'Test_prop_right_align_twice_3', {})
2596
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002597 call StopVimInTerminal(buf)
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002598endfunc
2599
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002600func Test_props_with_text_after()
2601 CheckRunVimInTerminal
2602
2603 let lines =<< trim END
Bram Moolenaar3ec3b8e2022-08-05 21:39:30 +01002604 set showbreak=+++
Bram Moolenaar73c38422022-08-07 11:53:40 +01002605 set breakindent
2606 call setline(1, ' some text here and other text there')
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002607 call prop_type_add('rightprop', #{highlight: 'ErrorMsg'})
2608 call prop_type_add('afterprop', #{highlight: 'Search'})
2609 call prop_type_add('belowprop', #{highlight: 'DiffAdd'})
2610 call prop_add(1, 0, #{type: 'rightprop', text: ' RIGHT ', text_align: 'right'})
Bram Moolenaar783ef722022-08-01 16:11:06 +01002611 call prop_add(1, 0, #{type: 'afterprop', text: "\tAFTER\t", text_align: 'after'})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002612 call prop_add(1, 0, #{type: 'belowprop', text: ' BELOW ', text_align: 'below'})
Bram Moolenaar50e75fe2022-08-05 20:25:50 +01002613 call prop_add(1, 0, #{type: 'belowprop', text: ' ALSO BELOW ', text_align: 'below'})
Bram Moolenaar84b247f2022-08-01 11:17:40 +01002614
2615 call setline(2, 'Last line.')
2616 call prop_add(2, 0, #{type: 'afterprop', text: ' After Last ', text_align: 'after'})
2617 normal G$
Bram Moolenaar09ff4b52022-08-01 16:51:02 +01002618
2619 call setline(3, 'right here')
2620 call prop_add(3, 0, #{type: 'rightprop', text: 'sรถmeๅ’Œๅนณtรฉxt', text_align: 'right'})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002621 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002622 call writefile(lines, 'XscriptPropsWithTextAfter', 'D')
Bram Moolenaar50e75fe2022-08-05 20:25:50 +01002623 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfter', #{rows: 8, cols: 60})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002624 call VerifyScreenDump(buf, 'Test_prop_with_text_after_1', {})
2625
2626 call StopVimInTerminal(buf)
Bram Moolenaar82b14c12022-08-10 19:50:47 +01002627
2628 call assert_fails('call prop_add(1, 2, #{text: "yes", text_align: "right", type: "some"})', 'E1294:')
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002629endfunc
2630
Bram Moolenaarcba69522022-08-06 21:03:53 +01002631func Test_props_with_text_after_below_trunc()
2632 CheckRunVimInTerminal
2633
2634 let lines =<< trim END
2635 vim9script
2636 edit foobar
2637 set showbreak=+++
2638 setline(1, ['onasdf asdf asdf asdf asd fas df', 'two'])
2639 prop_type_add('test', {highlight: 'Special'})
2640 prop_add(1, 0, {
2641 type: 'test',
2642 text: 'the quick brown fox jumps over the lazy dog',
2643 text_align: 'after'
2644 })
2645 prop_add(1, 0, {
2646 type: 'test',
2647 text: 'the quick brown fox jumps over the lazy dog',
2648 text_align: 'below'
2649 })
2650 normal G$
2651 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002652 call writefile(lines, 'XscriptPropsAfterTrunc', 'D')
Bram Moolenaarcba69522022-08-06 21:03:53 +01002653 let buf = RunVimInTerminal('-S XscriptPropsAfterTrunc', #{rows: 8, cols: 60})
2654 call VerifyScreenDump(buf, 'Test_prop_with_text_after_below_trunc_1', {})
2655
2656 call StopVimInTerminal(buf)
Bram Moolenaarcba69522022-08-06 21:03:53 +01002657endfunc
2658
Bram Moolenaare175dc62022-08-01 22:18:50 +01002659func Test_props_with_text_after_joined()
2660 CheckRunVimInTerminal
2661
2662 let lines =<< trim END
2663 call setline(1, ['one', 'two', 'three', 'four'])
2664 call prop_type_add('afterprop', #{highlight: 'Search'})
2665 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE', text_align: 'after'})
2666 call prop_add(4, 0, #{type: 'afterprop', text: ' FOUR', text_align: 'after'})
2667 normal ggJ
2668 normal GkJ
2669
2670 call setline(3, ['a', 'b', 'c', 'd', 'e', 'f'])
2671 call prop_add(3, 0, #{type: 'afterprop', text: ' AAA', text_align: 'after'})
2672 call prop_add(5, 0, #{type: 'afterprop', text: ' CCC', text_align: 'after'})
2673 call prop_add(7, 0, #{type: 'afterprop', text: ' EEE', text_align: 'after'})
2674 call prop_add(8, 0, #{type: 'afterprop', text: ' FFF', text_align: 'after'})
2675 normal 3G6J
2676 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002677 call writefile(lines, 'XscriptPropsWithTextAfterJoined', 'D')
Bram Moolenaare175dc62022-08-01 22:18:50 +01002678 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterJoined', #{rows: 6, cols: 60})
2679 call VerifyScreenDump(buf, 'Test_prop_with_text_after_joined_1', {})
2680
2681 call StopVimInTerminal(buf)
Bram Moolenaare175dc62022-08-01 22:18:50 +01002682endfunc
2683
Bram Moolenaar398649e2022-08-04 15:03:48 +01002684func Test_props_with_text_after_truncated()
2685 CheckRunVimInTerminal
2686
2687 let lines =<< trim END
2688 call setline(1, ['one two three four five six seven'])
2689 call prop_type_add('afterprop', #{highlight: 'Search'})
2690 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE'})
2691
2692 call setline(2, ['one two three four five six seven'])
2693 call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right'})
2694
2695 call setline(3, ['one two three four five six seven'])
2696 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'})
2697
2698 call setline(4, ['cursor here'])
2699 normal 4Gfh
2700 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002701 call writefile(lines, 'XscriptPropsWithTextAfterTrunc', 'D')
Bram Moolenaar398649e2022-08-04 15:03:48 +01002702 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterTrunc', #{rows: 9, cols: 60})
2703 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_1', {})
2704
2705 call term_sendkeys(buf, ":37vsp\<CR>gg")
2706 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_2', {})
2707
2708 call term_sendkeys(buf, ":36wincmd |\<CR>")
2709 call term_sendkeys(buf, "2G$")
2710 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_3', {})
2711
2712 call term_sendkeys(buf, ":33wincmd |\<CR>")
2713 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_4', {})
2714
2715 call term_sendkeys(buf, ":18wincmd |\<CR>")
2716 call term_sendkeys(buf, "0fx")
2717 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_5', {})
2718
2719 call StopVimInTerminal(buf)
Bram Moolenaar398649e2022-08-04 15:03:48 +01002720endfunc
2721
Bram Moolenaar49a90792022-08-09 18:25:23 +01002722func Test_props_with_text_empty_line()
2723 CheckRunVimInTerminal
2724
2725 let lines =<< trim END
2726 call setline(1, ['', 'aaa', '', 'bbbbbb'])
2727 call prop_type_add('prop1', #{highlight: 'Search'})
2728 call prop_add(1, 1, #{type: 'prop1', text_wrap: 'wrap', text: repeat('X', &columns)})
2729 call prop_add(3, 1, #{type: 'prop1', text_wrap: 'wrap', text: repeat('X', &columns + 1)})
2730 normal gg0
2731 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002732 call writefile(lines, 'XscriptPropsWithTextEmptyLine', 'D')
Bram Moolenaar49a90792022-08-09 18:25:23 +01002733 let buf = RunVimInTerminal('-S XscriptPropsWithTextEmptyLine', #{rows: 8, cols: 60})
2734 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_1', {})
2735 call term_sendkeys(buf, "$")
2736 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_2', {})
2737 call term_sendkeys(buf, "j")
2738 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_3', {})
2739 call term_sendkeys(buf, "j")
2740 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_4', {})
2741 call term_sendkeys(buf, "j")
2742 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_5', {})
2743
2744 call StopVimInTerminal(buf)
Bram Moolenaar49a90792022-08-09 18:25:23 +01002745endfunc
2746
Bram Moolenaar398649e2022-08-04 15:03:48 +01002747func Test_props_with_text_after_wraps()
2748 CheckRunVimInTerminal
2749
2750 let lines =<< trim END
2751 call setline(1, ['one two three four five six seven'])
2752 call prop_type_add('afterprop', #{highlight: 'Search'})
2753 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE', text_wrap: 'wrap'})
2754
2755 call setline(2, ['one two three four five six seven'])
2756 call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right', text_wrap: 'wrap'})
2757
2758 call setline(3, ['one two three four five six seven'])
2759 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'})
2760
2761 call setline(4, ['cursor here'])
2762 normal 4Gfh
2763 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002764 call writefile(lines, 'XscriptPropsWithTextAfterWraps', 'D')
Bram Moolenaar398649e2022-08-04 15:03:48 +01002765 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterWraps', #{rows: 9, cols: 60})
2766 call VerifyScreenDump(buf, 'Test_prop_with_text_after_wraps_1', {})
2767
2768 call StopVimInTerminal(buf)
Bram Moolenaar398649e2022-08-04 15:03:48 +01002769endfunc
2770
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002771func Test_props_with_text_after_nowrap()
2772 CheckRunVimInTerminal
2773
2774 let lines =<< trim END
2775 set nowrap
Bram Moolenaar8f369fb2022-08-13 19:35:05 +01002776 call setline(1, ['one', 'two', 'three', 'four'])
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002777 call prop_type_add('belowprop', #{highlight: 'ErrorMsg'})
2778 call prop_type_add('anotherprop', #{highlight: 'Search'})
Bram Moolenaardb9b96d2022-08-06 17:38:53 +01002779 call prop_type_add('someprop', #{highlight: 'DiffChange'})
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002780 call prop_add(1, 0, #{type: 'belowprop', text: ' Below the line ', text_align: 'below'})
2781 call prop_add(2, 0, #{type: 'anotherprop', text: 'another', text_align: 'below'})
2782 call prop_add(2, 0, #{type: 'belowprop', text: 'One More Here', text_align: 'below'})
Bram Moolenaardb9b96d2022-08-06 17:38:53 +01002783 call prop_add(1, 0, #{type: 'someprop', text: 'right here', text_align: 'right'})
2784 call prop_add(1, 0, #{type: 'someprop', text: ' After the text', text_align: 'after'})
Bram Moolenaar8f369fb2022-08-13 19:35:05 +01002785 normal 3G$
2786
2787 call prop_add(3, 0, #{type: 'anotherprop', text: 'right aligned', text_align: 'right'})
2788 call prop_add(3, 0, #{type: 'anotherprop', text: 'also right aligned', text_align: 'right'})
Bram Moolenaar9113c2c2022-08-13 20:17:34 +01002789 hi CursorLine ctermbg=lightgrey
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002790 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002791 call writefile(lines, 'XscriptPropsAfterNowrap', 'D')
Bram Moolenaar8f369fb2022-08-13 19:35:05 +01002792 let buf = RunVimInTerminal('-S XscriptPropsAfterNowrap', #{rows: 12, cols: 60})
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002793 call VerifyScreenDump(buf, 'Test_prop_with_text_after_nowrap_1', {})
2794
Bram Moolenaar9113c2c2022-08-13 20:17:34 +01002795 call term_sendkeys(buf, ":set signcolumn=yes foldcolumn=3 cursorline\<CR>")
Bram Moolenaar1306b362022-08-06 15:59:06 +01002796 call VerifyScreenDump(buf, 'Test_prop_with_text_after_nowrap_2', {})
2797
Bram Moolenaar8f369fb2022-08-13 19:35:05 +01002798 call term_sendkeys(buf, "j")
2799 call VerifyScreenDump(buf, 'Test_prop_with_text_after_nowrap_3', {})
2800
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002801 call StopVimInTerminal(buf)
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002802endfunc
2803
Bram Moolenaar48ca24d2022-08-06 22:03:20 +01002804func Test_props_with_text_below_nowrap()
2805 CheckRunVimInTerminal
2806
2807 let lines =<< trim END
2808 vim9script
2809 edit foobar
2810 set nowrap
2811 set showbreak=+++\
2812 setline(1, ['onasdf asdf asdf sdf df asdf asdf e asdf asdf asdf asdf asd fas df', 'two'])
2813 prop_type_add('test', {highlight: 'Special'})
2814 prop_add(1, 0, {
2815 type: 'test',
2816 text: 'the quick brown fox jumps over the lazy dog',
2817 text_align: 'after'
2818 })
2819 prop_add(1, 0, {
2820 type: 'test',
2821 text: 'the quick brown fox jumps over the lazy dog',
2822 text_align: 'below'
2823 })
2824 normal G$
2825 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002826 call writefile(lines, 'XscriptPropsBelowNowrap', 'D')
Bram Moolenaar48ca24d2022-08-06 22:03:20 +01002827 let buf = RunVimInTerminal('-S XscriptPropsBelowNowrap', #{rows: 8, cols: 60})
2828 call VerifyScreenDump(buf, 'Test_prop_with_text_below_nowrap_1', {})
2829
2830 call term_sendkeys(buf, "gg$")
2831 call VerifyScreenDump(buf, 'Test_prop_with_text_below_nowrap_2', {})
2832
2833 call StopVimInTerminal(buf)
Bram Moolenaar48ca24d2022-08-06 22:03:20 +01002834endfunc
2835
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002836func Test_props_with_text_above()
2837 CheckRunVimInTerminal
2838
2839 let lines =<< trim END
2840 call setline(1, ['one two', 'three four', 'five six'])
2841 call prop_type_add('above1', #{highlight: 'Search'})
2842 call prop_type_add('above2', #{highlight: 'DiffChange'})
Bram Moolenaar6eda17d2022-09-12 19:25:11 +01002843 call prop_type_add('below', #{highlight: 'DiffAdd'})
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002844 call prop_add(1, 0, #{type: 'above1', text: 'first thing above', text_align: 'above'})
2845 call prop_add(1, 0, #{type: 'above2', text: 'second thing above', text_align: 'above'})
Bram Moolenaar79f8b842022-09-11 13:31:01 +01002846 call prop_add(3, 0, #{type: 'above1', text: 'another thing', text_align: 'above', text_padding_left: 3})
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002847
2848 normal gglllj
Bram Moolenaar6eda17d2022-09-12 19:25:11 +01002849 func AddPropBelow()
2850 call prop_add(1, 0, #{type: 'below', text: 'below', text_align: 'below'})
2851 endfunc
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002852 END
2853 call writefile(lines, 'XscriptPropsWithTextAbove', 'D')
2854 let buf = RunVimInTerminal('-S XscriptPropsWithTextAbove', #{rows: 9, cols: 60})
2855 call VerifyScreenDump(buf, 'Test_prop_with_text_above_1', {})
2856
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01002857 call term_sendkeys(buf, "ggg$")
2858 call VerifyScreenDump(buf, 'Test_prop_with_text_above_1a', {})
2859 call term_sendkeys(buf, "g0")
2860 call VerifyScreenDump(buf, 'Test_prop_with_text_above_1b', {})
2861
Bram Moolenaar4c7fd4d2022-09-17 17:15:33 +01002862 call term_sendkeys(buf, ":set showbreak=>>\<CR>")
2863 call term_sendkeys(buf, "ggll")
2864 call VerifyScreenDump(buf, 'Test_prop_with_text_above_1c', {})
2865 call term_sendkeys(buf, ":set showbreak=\<CR>")
2866
Bram Moolenaar88b79cb2022-09-10 22:32:14 +01002867 call term_sendkeys(buf, "ggI")
2868 call VerifyScreenDump(buf, 'Test_prop_with_text_above_2', {})
2869 call term_sendkeys(buf, "inserted \<Esc>")
2870 call VerifyScreenDump(buf, 'Test_prop_with_text_above_3', {})
2871
Bram Moolenaar79f8b842022-09-11 13:31:01 +01002872 call term_sendkeys(buf, ":set number signcolumn=yes\<CR>")
2873 call VerifyScreenDump(buf, 'Test_prop_with_text_above_4', {})
2874
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01002875 call term_sendkeys(buf, ":set nowrap\<CR>gg$j")
2876 call VerifyScreenDump(buf, 'Test_prop_with_text_above_5', {})
2877
Bram Moolenaar6eda17d2022-09-12 19:25:11 +01002878 call term_sendkeys(buf, ":call AddPropBelow()\<CR>")
2879 call term_sendkeys(buf, "ggve")
2880 call VerifyScreenDump(buf, 'Test_prop_with_text_above_6', {})
2881 call term_sendkeys(buf, "V")
2882 call VerifyScreenDump(buf, 'Test_prop_with_text_above_7', {})
2883
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002884 call term_sendkeys(buf, "\<Esc>ls\<CR>\<Esc>")
2885 call VerifyScreenDump(buf, 'Test_prop_with_text_above_8', {})
2886
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002887 call StopVimInTerminal(buf)
2888endfunc
2889
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01002890func Test_prop_above_with_indent()
2891 new
2892 call setline(1, ['first line', ' second line', ' line below'])
2893 setlocal cindent
2894 call prop_type_add('indented', #{highlight: 'Search'})
2895 call prop_add(3, 0, #{type: 'indented', text: 'here', text_align: 'above', text_padding_left: 4})
2896 call assert_equal(' line below', getline(3))
2897
2898 exe "normal 3G2|a\<CR>"
2899 call assert_equal(' ', getline(3))
2900 call assert_equal(' line below', getline(4))
2901
2902 bwipe!
2903 call prop_type_delete('indented')
2904endfunc
2905
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002906func Test_prop_below_split_line()
2907 CheckRunVimInTerminal
2908
2909 let lines =<< trim END
2910 vim9script
2911 setline(1, ['one one one', 'two two two', 'three three three'])
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002912 prop_type_add('test', {highlight: 'Search'})
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002913 prop_add(2, 0, {
2914 text: 'โ””โ”€ Virtual text below the 2nd line',
2915 type: 'test',
2916 text_align: 'below',
2917 text_padding_left: 3
2918 })
2919 END
2920 call writefile(lines, 'XscriptPropBelowSpitLine', 'D')
2921 let buf = RunVimInTerminal('-S XscriptPropBelowSpitLine', #{rows: 8})
2922 call term_sendkeys(buf, "2GA\<CR>xx")
2923 call VerifyScreenDump(buf, 'Test_prop_below_split_line_1', {})
2924
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002925 call term_sendkeys(buf, "\<Esc>:set number\<CR>")
2926 call VerifyScreenDump(buf, 'Test_prop_below_split_line_2', {})
2927
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002928 call StopVimInTerminal(buf)
2929endfunc
2930
Bram Moolenaar9e7e28f2022-08-14 16:36:38 +01002931func Test_props_with_text_override()
2932 CheckRunVimInTerminal
2933
2934 let lines =<< trim END
2935 vim9script
2936 setline(1, 'some text here')
2937 hi Likethis ctermfg=blue ctermbg=cyan
2938 prop_type_add('prop', {highlight: 'Likethis', override: true})
2939 prop_add(1, 6, {type: 'prop', text: ' inserted '})
2940 hi CursorLine cterm=underline ctermbg=lightgrey
2941 set cursorline
2942 END
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002943 call writefile(lines, 'XscriptPropsOverride', 'D')
Bram Moolenaar9e7e28f2022-08-14 16:36:38 +01002944 let buf = RunVimInTerminal('-S XscriptPropsOverride', #{rows: 6, cols: 60})
2945 call VerifyScreenDump(buf, 'Test_prop_with_text_override_1', {})
2946
2947 call term_sendkeys(buf, ":set nocursorline\<CR>")
2948 call term_sendkeys(buf, "0llvfr")
2949 call VerifyScreenDump(buf, 'Test_prop_with_text_override_2', {})
2950
2951 call StopVimInTerminal(buf)
Bram Moolenaar9e7e28f2022-08-14 16:36:38 +01002952endfunc
2953
Bram Moolenaar326c5d32022-08-12 13:05:49 +01002954func Test_props_with_text_CursorMoved()
2955 CheckRunVimInTerminal
2956
2957 let lines =<< trim END
2958 call setline(1, ['this is line one', 'this is line two', 'three', 'four', 'five'])
2959
2960 call prop_type_add('prop', #{highlight: 'Error'})
2961 let g:long_text = repeat('x', &columns * 2)
2962
2963 let g:prop_id = v:null
2964 func! Update()
2965 if line('.') == 1
2966 if g:prop_id == v:null
2967 let g:prop_id = prop_add(1, 0, #{type: 'prop', text_wrap: 'wrap', text: g:long_text})
2968 endif
2969 elseif g:prop_id != v:null
2970 call prop_remove(#{id: g:prop_id})
2971 let g:prop_id = v:null
2972 endif
2973 endfunc
2974
2975 autocmd CursorMoved * call Update()
2976 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002977 call writefile(lines, 'XscriptPropsCursorMovec', 'D')
Bram Moolenaar326c5d32022-08-12 13:05:49 +01002978 let buf = RunVimInTerminal('-S XscriptPropsCursorMovec', #{rows: 8, cols: 60})
2979 call term_sendkeys(buf, "gg0w")
2980 call VerifyScreenDump(buf, 'Test_prop_with_text_cursormoved_1', {})
2981
2982 call term_sendkeys(buf, "j")
2983 call VerifyScreenDump(buf, 'Test_prop_with_text_cursormoved_2', {})
2984
2985 " back to the first state
2986 call term_sendkeys(buf, "k")
2987 call VerifyScreenDump(buf, 'Test_prop_with_text_cursormoved_1', {})
2988
2989 call StopVimInTerminal(buf)
Bram Moolenaar326c5d32022-08-12 13:05:49 +01002990endfunc
2991
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002992func Test_props_with_text_after_split_join()
2993 CheckRunVimInTerminal
2994
2995 let lines =<< trim END
2996 call setline(1, ['1122'])
2997 call prop_type_add('belowprop', #{highlight: 'ErrorMsg'})
2998 call prop_add(1, 0, #{type: 'belowprop', text: ' Below the line ', text_align: 'below'})
2999 exe "normal f2i\<CR>\<Esc>"
3000
3001 func AddMore()
3002 call prop_type_add('another', #{highlight: 'Search'})
3003 call prop_add(1, 0, #{type: 'another', text: ' after the text ', text_align: 'after'})
3004 call prop_add(1, 0, #{type: 'another', text: ' right here', text_align: 'right'})
3005 endfunc
3006 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003007 call writefile(lines, 'XscriptPropsAfterSplitJoin', 'D')
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01003008 let buf = RunVimInTerminal('-S XscriptPropsAfterSplitJoin', #{rows: 8, cols: 60})
3009 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_1', {})
3010
3011 call term_sendkeys(buf, "ggJ")
3012 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_2', {})
3013
3014 call term_sendkeys(buf, ":call AddMore()\<CR>")
3015 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_3', {})
3016
3017 call term_sendkeys(buf, "ggf s\<CR>\<Esc>")
3018 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_4', {})
3019
3020 call term_sendkeys(buf, "ggJ")
3021 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_5', {})
3022
3023 call StopVimInTerminal(buf)
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01003024endfunc
3025
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01003026func Test_removed_prop_with_text_cleans_up_array()
3027 new
3028 call setline(1, 'some text here')
3029 call prop_type_add('some', #{highlight: 'ErrorMsg'})
3030 let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"})
3031 call assert_equal(-1, id1)
3032 let id2 = prop_add(1, 10, #{type: 'some', text: "HERE"})
3033 call assert_equal(-2, id2)
3034
3035 " removing the props resets the index
3036 call prop_remove(#{id: id1})
3037 call prop_remove(#{id: id2})
3038 let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"})
3039 call assert_equal(-1, id1)
3040
3041 call prop_type_delete('some')
3042 bwipe!
3043endfunc
3044
Bram Moolenaar1f4ee192022-08-01 15:52:55 +01003045def Test_insert_text_before_virtual_text()
3046 new foobar
3047 setline(1, '12345678')
3048 prop_type_add('test', {highlight: 'Search'})
3049 prop_add(1, 5, {
3050 type: 'test',
3051 text: ' virtual text '
3052 })
3053 normal! f4axyz
3054 normal! f5iXYZ
3055 assert_equal('1234xyzXYZ5678', getline(1))
3056
3057 prop_type_delete('test')
3058 bwipe!
3059enddef
3060
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003061func Test_insert_text_start_incl()
3062 CheckRunVimInTerminal
3063
3064 let lines =<< trim END
3065 vim9script
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01003066 setline(1, ['text one text two', '', 'function(arg)'])
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003067
3068 prop_type_add('propincl', {highlight: 'NonText', start_incl: true})
3069 prop_add(1, 6, {type: 'propincl', text: 'after '})
3070 cursor(1, 6)
3071 prop_type_add('propnotincl', {highlight: 'NonText', start_incl: false})
3072 prop_add(1, 15, {type: 'propnotincl', text: 'before '})
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01003073
3074 set cindent sw=4
3075 prop_type_add('argname', {highlight: 'DiffChange', start_incl: true})
3076 prop_add(3, 10, {type: 'argname', text: 'arg: '})
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003077 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003078 call writefile(lines, 'XscriptPropsStartIncl', 'D')
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003079 let buf = RunVimInTerminal('-S XscriptPropsStartIncl', #{rows: 8, cols: 60})
3080 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_1', {})
3081
3082 call term_sendkeys(buf, "i")
3083 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_2', {})
3084 call term_sendkeys(buf, "xx\<Esc>")
3085 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_3', {})
3086
3087 call term_sendkeys(buf, "2wi")
3088 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_4', {})
3089 call term_sendkeys(buf, "yy\<Esc>")
3090 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_5', {})
3091
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01003092 call term_sendkeys(buf, "3Gfai\<CR>\<Esc>")
3093 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_6', {})
3094 call term_sendkeys(buf, ">>")
3095 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_7', {})
3096 call term_sendkeys(buf, "<<<<")
3097 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_8', {})
3098
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003099 call StopVimInTerminal(buf)
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003100endfunc
3101
Bram Moolenaarc3a483f2022-08-14 19:37:36 +01003102func Test_insert_text_list_mode()
3103 CheckRunVimInTerminal
3104
3105 let lines =<< trim END
3106 vim9script
3107 setline(1, ['This is a line with quite a bit of text here.',
3108 'second line', 'third line'])
3109 set list listchars+=extends:ยป
3110 prop_type_add('Prop1', {highlight: 'Error'})
3111 prop_add(1, 0, {
3112 type: 'Prop1',
3113 text: 'The quick brown fox jumps over the lazy dog',
3114 text_align: 'right'
3115 })
3116 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003117 call writefile(lines, 'XscriptPropsListMode', 'D')
Bram Moolenaarc3a483f2022-08-14 19:37:36 +01003118 let buf = RunVimInTerminal('-S XscriptPropsListMode', #{rows: 8, cols: 60})
3119 call term_sendkeys(buf, "ggj")
3120 call VerifyScreenDump(buf, 'Test_prop_insert_list_mode_1', {})
3121
3122 call term_sendkeys(buf, ":set nowrap\<CR>")
3123 call VerifyScreenDump(buf, 'Test_prop_insert_list_mode_2', {})
3124
3125 call term_sendkeys(buf, "ggd32l")
3126 call VerifyScreenDump(buf, 'Test_prop_insert_list_mode_3', {})
3127
3128 call StopVimInTerminal(buf)
Bram Moolenaarc3a483f2022-08-14 19:37:36 +01003129endfunc
3130
Bram Moolenaarf396ce82022-08-23 18:39:37 +01003131func Test_insert_text_with_padding()
3132 CheckRunVimInTerminal
3133
3134 let lines =<< trim END
3135 vim9script
3136 setline(1, ['Some text to add virtual text to.',
3137 'second line',
3138 'Another line with some text to make the wrap.'])
3139 prop_type_add('theprop', {highlight: 'DiffChange'})
3140 prop_add(1, 0, {
3141 type: 'theprop',
3142 text: 'after',
3143 text_align: 'after',
3144 text_padding_left: 3,
3145 })
3146 prop_add(1, 0, {
3147 type: 'theprop',
3148 text: 'right aligned',
3149 text_align: 'right',
3150 text_padding_left: 5,
3151 })
3152 prop_add(1, 0, {
3153 type: 'theprop',
3154 text: 'below the line',
3155 text_align: 'below',
3156 text_padding_left: 4,
3157 })
3158 prop_add(3, 0, {
3159 type: 'theprop',
3160 text: 'rightmost',
3161 text_align: 'right',
3162 text_padding_left: 6,
3163 text_wrap: 'wrap',
3164 })
3165 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003166 call writefile(lines, 'XscriptPropsPadded', 'D')
Bram Moolenaarf396ce82022-08-23 18:39:37 +01003167 let buf = RunVimInTerminal('-S XscriptPropsPadded', #{rows: 8, cols: 60})
3168 call VerifyScreenDump(buf, 'Test_prop_text_with_padding_1', {})
3169
3170 call term_sendkeys(buf, "ggixxxxxxxxxx\<Esc>")
3171 call term_sendkeys(buf, "3Gix\<Esc>")
3172 call VerifyScreenDump(buf, 'Test_prop_text_with_padding_2', {})
3173
3174 call term_sendkeys(buf, "ggix\<Esc>")
3175 call VerifyScreenDump(buf, 'Test_prop_text_with_padding_3', {})
3176
Bram Moolenaara4abe512022-09-15 19:44:09 +01003177 call term_sendkeys(buf, ":set list\<CR>")
3178 call VerifyScreenDump(buf, 'Test_prop_text_with_padding_4', {})
3179
Bram Moolenaarf396ce82022-08-23 18:39:37 +01003180 call StopVimInTerminal(buf)
Bram Moolenaarf396ce82022-08-23 18:39:37 +01003181endfunc
3182
Bram Moolenaarf5240b92022-08-24 12:24:37 +01003183func Test_insert_text_change_arg()
3184 CheckRunVimInTerminal
3185
3186 let lines =<< trim END
3187 vim9script
3188 setline(1, ['SetErrorCode( 10, 20 )', 'second line'])
3189 prop_type_add('param', {highlight: 'DiffChange', start_incl: 1})
3190 prop_type_add('padd', {highlight: 'NonText', start_incl: 1})
3191 prop_add(1, 15, {
3192 type: 'param',
3193 text: 'id:',
3194 })
3195 prop_add(1, 15, {
3196 type: 'padd',
3197 text: '-',
3198 })
3199 prop_add(1, 19, {
3200 type: 'param',
3201 text: 'id:',
3202 })
3203 prop_add(1, 19, {
3204 type: 'padd',
3205 text: '-',
3206 })
3207 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003208 call writefile(lines, 'XscriptPropsChange', 'D')
Bram Moolenaarf5240b92022-08-24 12:24:37 +01003209 let buf = RunVimInTerminal('-S XscriptPropsChange', #{rows: 5, cols: 60})
3210 call VerifyScreenDump(buf, 'Test_prop_text_change_arg_1', {})
3211
3212 call term_sendkeys(buf, "ggf1cw1234\<Esc>")
3213 call VerifyScreenDump(buf, 'Test_prop_text_change_arg_2', {})
3214
3215 call StopVimInTerminal(buf)
Bram Moolenaarf5240b92022-08-24 12:24:37 +01003216endfunc
3217
Bram Moolenaar99fa7212020-04-26 15:59:55 +02003218" vim: shiftwidth=2 sts=2 expandtab