blob: 34c719d9642c626be33547367359181792746c08 [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:')
Bram Moolenaar4997f2a2022-10-13 14:00:45 +0100386
387 " only one error for multiple wrong values
388 call assert_fails('call prop_add_list(#{type: "one"}, [[{}, [], 0z00, 0.3]])', ['E728:', 'E728:'])
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200389 call DeletePropTypes()
390 bw!
391endfunc
392
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100393func Test_prop_remove()
394 new
395 call AddPropTypes()
396 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100397 let props = Get_expected_props()
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100398 call assert_equal(props, prop_list(1))
399
400 " remove by id
Bram Moolenaara5a78822019-09-04 21:57:18 +0200401 call assert_equal(1, {'id': 12}->prop_remove(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100402 unlet props[2]
403 call assert_equal(props, prop_list(1))
404
405 " remove by type
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200406 call assert_equal(1, prop_remove({'type': 'one'}, 1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100407 unlet props[1]
408 call assert_equal(props, prop_list(1))
409
Bram Moolenaarf0884c52019-05-24 21:22:29 +0200410 " remove from unknown buffer
411 call assert_fails("call prop_remove({'type': 'one', 'bufnr': 123456}, 1)", 'E158:')
412
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100413 call DeletePropTypes()
414 bwipe!
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100415
416 new
417 call AddPropTypes()
418 call SetupPropsInFirstLine()
419 call prop_add(1, 6, {'length': 2, 'id': 11, 'type': 'three'})
420 let props = Get_expected_props()
Martin Tournoije2390c72021-07-28 13:30:16 +0200421 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 +0100422 call assert_equal(props, prop_list(1))
423 call assert_equal(1, prop_remove({'type': 'three', 'id': 11, 'both': 1, 'all': 1}, 1))
424 unlet props[3]
425 call assert_equal(props, prop_list(1))
426
Bram Moolenaare2e40752020-09-04 21:18:46 +0200427 call assert_fails("call prop_remove({'id': 11, 'both': 1})", 'E860:')
428 call assert_fails("call prop_remove({'type': 'three', 'both': 1})", 'E860:')
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100429
430 call DeletePropTypes()
431 bwipe!
Ben Jacksona7704222022-08-20 20:54:51 +0100432
433 new
434 call AddPropTypes()
435 call SetupPropsInFirstLine()
436 let props = Get_expected_props() " [whole, one, two, three]
437 call assert_equal(props, prop_list(1))
438
439 " remove one by types
440 call assert_equal(1, prop_remove({'types': ['one', 'two', 'three']}, 1))
441 unlet props[1] " [whole, two, three]
442 call assert_equal(props, prop_list(1))
443
444 " remove 'all' by types
445 call assert_equal(2, prop_remove({'types': ['three', 'whole'], 'all': 1}, 1))
446 unlet props[0] " [two, three]
447 unlet props[1] " [three]
448 call assert_equal(props, prop_list(1))
449
450 " remove none by types
451 call assert_equal(0, prop_remove({'types': ['three', 'whole'], 'all': 1}, 1))
452 call assert_equal(props, prop_list(1))
453
454 " no types
455 call assert_fails("call prop_remove({'types': []}, 1)", 'E968:')
456 call assert_fails("call prop_remove({'types': ['not_a_real_type']}, 1)", 'E971:')
457
458 " only one of types and type can be supplied
459 call assert_fails("call prop_remove({'type': 'one', 'types': ['three'], 'all': 1}, 1)", 'E1295:')
460
461 call DeletePropTypes()
462 bwipe!
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100463endfunc
464
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200465def Test_prop_add_vim9()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100466 prop_type_add('comment', {
Bram Moolenaarfa2e38d2020-09-05 21:00:00 +0200467 highlight: 'Directory',
468 priority: 123,
469 start_incl: true,
470 end_incl: true,
471 combine: false,
472 })
473 prop_type_delete('comment')
474enddef
475
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200476def Test_prop_remove_vim9()
477 new
Bram Moolenaar62aec932022-01-29 21:45:34 +0000478 g:AddPropTypes()
479 g:SetupPropsInFirstLine()
Bram Moolenaare0de1712020-12-02 17:36:54 +0100480 assert_equal(1, prop_remove({type: 'three', id: 13, both: true, all: true}))
Bram Moolenaar62aec932022-01-29 21:45:34 +0000481 g:DeletePropTypes()
Bram Moolenaara5a40c52020-09-05 20:50:49 +0200482 bwipe!
483enddef
484
Bram Moolenaar196d1572019-01-02 23:47:18 +0100485func SetupOneLine()
486 call setline(1, 'xonex xtwoxx')
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200487 normal gg0
Bram Moolenaar196d1572019-01-02 23:47:18 +0100488 call AddPropTypes()
489 call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'})
490 call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'})
491 let expected = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200492 \ #{type_bufnr: 0, col: 2, length: 3, id: 11, type: 'one', start: 1, end: 1},
493 \ #{type_bufnr: 0, col: 8, length: 3, id: 12, type: 'two', start: 1, end: 1},
Bram Moolenaar196d1572019-01-02 23:47:18 +0100494 \]
495 call assert_equal(expected, prop_list(1))
496 return expected
497endfunc
498
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100499func Test_prop_add_remove_buf()
500 new
501 let bufnr = bufnr('')
502 call AddPropTypes()
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100503 for lnum in range(1, 4)
504 call setline(lnum, 'one two three')
505 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100506 wincmd w
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100507 for lnum in range(1, 4)
508 call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr})
509 call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr})
510 call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr})
511 endfor
512
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100513 let props = [
Martin Tournoije2390c72021-07-28 13:30:16 +0200514 \ #{type_bufnr: 0, col: 1, length: 3, id: 11, type: 'one', start: 1, end: 1},
515 \ #{type_bufnr: 0, col: 5, length: 3, id: 12, type: 'two', start: 1, end: 1},
516 \ #{type_bufnr: 0, col: 11, length: 3, id: 13, type: 'three', start: 1, end: 1},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100517 \]
518 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100519
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100520 " remove by id
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100521 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100522 unlet props[1]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100523
524 call prop_remove({'id': 12, 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100525 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100526 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
527 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
528 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
529
530 call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4)
531 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
532 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
533 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
534 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
535
536 call prop_remove({'id': 12, 'bufnr': bufnr})
537 for lnum in range(1, 4)
538 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
539 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100540
541 " remove by type
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100542 let before_props = deepcopy(props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100543 unlet props[0]
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100544
545 call prop_remove({'type': 'one', 'bufnr': bufnr}, 1)
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100546 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100547 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
548 call assert_equal(before_props, prop_list(3, {'bufnr': bufnr}))
549 call assert_equal(before_props, prop_list(4, {'bufnr': bufnr}))
550
551 call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4)
552 call assert_equal(props, prop_list(1, {'bufnr': bufnr}))
553 call assert_equal(before_props, prop_list(2, {'bufnr': bufnr}))
554 call assert_equal(props, prop_list(3, {'bufnr': bufnr}))
555 call assert_equal(props, prop_list(4, {'bufnr': bufnr}))
556
557 call prop_remove({'type': 'one', 'bufnr': bufnr})
558 for lnum in range(1, 4)
559 call assert_equal(props, prop_list(lnum, {'bufnr': bufnr}))
560 endfor
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100561
562 call DeletePropTypes()
563 wincmd w
564 bwipe!
565endfunc
566
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100567func Test_prop_backspace()
568 new
569 set bs=2
Bram Moolenaar196d1572019-01-02 23:47:18 +0100570 let expected = SetupOneLine() " 'xonex xtwoxx'
Bram Moolenaar33c8ca92019-01-02 18:00:27 +0100571
572 exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>"
573 call assert_equal('one xtwoxx', getline(1))
574 let expected[0].col = 1
575 let expected[1].col = 6
576 call assert_equal(expected, prop_list(1))
577
578 call DeletePropTypes()
579 bwipe!
580 set bs&
581endfunc
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100582
LemonBoyd0b1a092022-05-12 18:45:18 +0100583func Test_prop_change()
584 new
585 let expected = SetupOneLine() " 'xonex xtwoxx'
586
587 " Characterwise.
588 exe "normal 7|c$\<Esc>"
589 call assert_equal('xonex ', getline(1))
590 call assert_equal(expected[:0], prop_list(1))
591 " Linewise.
592 exe "normal cc\<Esc>"
593 call assert_equal('', getline(1))
594 call assert_equal([], prop_list(1))
595
596 call DeletePropTypes()
597 bwipe!
598 set bs&
599endfunc
600
Bram Moolenaar196d1572019-01-02 23:47:18 +0100601func Test_prop_replace()
602 new
603 set bs=2
604 let expected = SetupOneLine() " 'xonex xtwoxx'
605
606 exe "normal 0Ryyy\<Esc>"
607 call assert_equal('yyyex xtwoxx', getline(1))
608 call assert_equal(expected, prop_list(1))
609
610 exe "normal ftRyy\<BS>"
611 call assert_equal('yyyex xywoxx', getline(1))
612 call assert_equal(expected, prop_list(1))
613
614 exe "normal 0fwRyy\<BS>"
615 call assert_equal('yyyex xyyoxx', getline(1))
616 call assert_equal(expected, prop_list(1))
617
618 exe "normal 0foRyy\<BS>\<BS>"
619 call assert_equal('yyyex xyyoxx', getline(1))
620 call assert_equal(expected, prop_list(1))
621
LemonBoy0d534d92022-05-21 11:20:42 +0100622 " Replace three 1-byte chars with three 2-byte ones.
623 exe "normal 0l3rø"
624 call assert_equal('yøøøx xyyoxx', getline(1))
625 let expected[0].length += 3
626 let expected[1].col += 3
627 call assert_equal(expected, prop_list(1))
628
Bram Moolenaar196d1572019-01-02 23:47:18 +0100629 call DeletePropTypes()
630 bwipe!
631 set bs&
632endfunc
633
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200634func Test_prop_open_line()
635 new
636
637 " open new line, props stay in top line
638 let expected = SetupOneLine() " 'xonex xtwoxx'
639 exe "normal o\<Esc>"
640 call assert_equal('xonex xtwoxx', getline(1))
641 call assert_equal('', getline(2))
642 call assert_equal(expected, prop_list(1))
643 call DeletePropTypes()
644
645 " move all props to next line
646 let expected = SetupOneLine() " 'xonex xtwoxx'
647 exe "normal 0i\<CR>\<Esc>"
648 call assert_equal('', getline(1))
649 call assert_equal('xonex xtwoxx', getline(2))
650 call assert_equal(expected, prop_list(2))
651 call DeletePropTypes()
652
653 " split just before prop, move all props to next line
654 let expected = SetupOneLine() " 'xonex xtwoxx'
655 exe "normal 0li\<CR>\<Esc>"
656 call assert_equal('x', getline(1))
657 call assert_equal('onex xtwoxx', getline(2))
658 let expected[0].col -= 1
659 let expected[1].col -= 1
660 call assert_equal(expected, prop_list(2))
661 call DeletePropTypes()
662
663 " split inside prop, split first prop
664 let expected = SetupOneLine() " 'xonex xtwoxx'
665 exe "normal 0lli\<CR>\<Esc>"
666 call assert_equal('xo', getline(1))
667 call assert_equal('nex xtwoxx', getline(2))
668 let exp_first = [deepcopy(expected[0])]
669 let exp_first[0].length = 1
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200670 let exp_first[0].end = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200671 call assert_equal(exp_first, prop_list(1))
672 let expected[0].col = 1
673 let expected[0].length = 2
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200674 let expected[0].start = 0
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200675 let expected[1].col -= 2
676 call assert_equal(expected, prop_list(2))
677 call DeletePropTypes()
678
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200679 " split just after first prop, second prop move to next line
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200680 let expected = SetupOneLine() " 'xonex xtwoxx'
681 exe "normal 0fea\<CR>\<Esc>"
682 call assert_equal('xone', getline(1))
683 call assert_equal('x xtwoxx', getline(2))
684 let exp_first = expected[0:0]
685 call assert_equal(exp_first, prop_list(1))
Bram Moolenaar5c65e6a2019-05-17 11:08:56 +0200686 let expected = expected[1:1]
687 let expected[0].col -= 4
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200688 call assert_equal(expected, prop_list(2))
689 call DeletePropTypes()
690
LemonBoy788c06a2022-05-14 18:48:05 +0100691 " split at the space character with 'ai' active, the leading space is removed
692 " in the second line and the prop is shifted accordingly.
693 let expected = SetupOneLine() " 'xonex xtwoxx'
694 set ai
695 exe "normal 6|i\<CR>\<Esc>"
696 call assert_equal('xonex', getline(1))
697 call assert_equal('xtwoxx', getline(2))
698 let expected[1].col -= 6
699 call assert_equal(expected, prop_list(1) + prop_list(2))
700 set ai&
701 call DeletePropTypes()
702
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200703 bwipe!
704 set bs&
705endfunc
706
Bram Moolenaarecb00c72022-08-07 14:55:14 +0100707func Test_prop_put()
708 new
709 let expected = SetupOneLine() " 'xonex xtwoxx'
710
711 let @a = 'new'
712 " insert just after the prop
713 normal 03l"ap
714 " insert inside the prop
715 normal 02l"ap
716 " insert just before the prop
717 normal 0"ap
718
719 call assert_equal('xnewonnewenewx xtwoxx', getline(1))
720 let expected[0].col += 3
721 let expected[0].length += 3
722 let expected[1].col += 9
723 call assert_equal(expected, prop_list(1))
724
725 " Visually select 4 chars in the prop and put "AB" to replace them
726 let @a = 'AB'
727 normal 05lv3l"ap
728 call assert_equal('xnewoABenewx xtwoxx', getline(1))
729 let expected[0].length -= 2
730 let expected[1].col -= 2
731 call assert_equal(expected, prop_list(1))
732
733 call DeletePropTypes()
734 bwipe!
735endfunc
736
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100737func Test_prop_clear()
738 new
739 call AddPropTypes()
740 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100741 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100742
Bram Moolenaara5a78822019-09-04 21:57:18 +0200743 eval 1->prop_clear()
744 call assert_equal([], 1->prop_list())
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100745
746 call DeletePropTypes()
747 bwipe!
748endfunc
749
750func Test_prop_clear_buf()
751 new
752 call AddPropTypes()
753 call SetupPropsInFirstLine()
754 let bufnr = bufnr('')
755 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100756 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100757
758 call prop_clear(1, 1, {'bufnr': bufnr})
759 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
760
761 wincmd w
762 call DeletePropTypes()
763 bwipe!
764endfunc
765
Bram Moolenaar21b50382019-01-04 18:07:24 +0100766func Test_prop_setline()
767 new
768 call AddPropTypes()
769 call SetupPropsInFirstLine()
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100770 call assert_equal(Get_expected_props(), prop_list(1))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100771
772 call setline(1, 'foobar')
773 call assert_equal([], prop_list(1))
774
775 call DeletePropTypes()
776 bwipe!
777endfunc
778
779func Test_prop_setbufline()
780 new
781 call AddPropTypes()
782 call SetupPropsInFirstLine()
783 let bufnr = bufnr('')
784 wincmd w
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100785 call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr}))
Bram Moolenaar21b50382019-01-04 18:07:24 +0100786
787 call setbufline(bufnr, 1, 'foobar')
788 call assert_equal([], prop_list(1, {'bufnr': bufnr}))
789
790 wincmd w
791 call DeletePropTypes()
792 bwipe!
793endfunc
794
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100795func Test_prop_substitute()
796 new
797 " Set first line to 'one two three'
798 call AddPropTypes()
799 call SetupPropsInFirstLine()
800 let expected_props = Get_expected_props()
801 call assert_equal(expected_props, prop_list(1))
802
803 " Change "n" in "one" to XX: 'oXXe two three'
804 s/n/XX/
805 let expected_props[0].length += 1
806 let expected_props[1].length += 1
807 let expected_props[2].col += 1
808 let expected_props[3].col += 1
809 call assert_equal(expected_props, prop_list(1))
810
811 " Delete "t" in "two" and "three" to XX: 'oXXe wo hree'
812 s/t//g
813 let expected_props[0].length -= 2
814 let expected_props[2].length -= 1
815 let expected_props[3].length -= 1
816 let expected_props[3].col -= 1
817 call assert_equal(expected_props, prop_list(1))
818
819 " Split the line by changing w to line break: 'oXXe ', 'o hree'
820 " The long prop is split and spans both lines.
821 " The props on "two" and "three" move to the next line.
822 s/w/\r/
823 let new_props = [
824 \ copy(expected_props[0]),
825 \ copy(expected_props[2]),
826 \ copy(expected_props[3]),
827 \ ]
828 let expected_props[0].length = 5
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200829 let expected_props[0].end = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100830 unlet expected_props[3]
831 unlet expected_props[2]
832 call assert_equal(expected_props, prop_list(1))
833
834 let new_props[0].length = 6
Bram Moolenaar87be9be2020-05-30 15:32:02 +0200835 let new_props[0].start = 0
Bram Moolenaar4164bb22019-01-04 23:09:49 +0100836 let new_props[1].col = 1
837 let new_props[1].length = 1
838 let new_props[2].col = 3
839 call assert_equal(new_props, prop_list(2))
840
841 call DeletePropTypes()
842 bwipe!
843endfunc
844
Bram Moolenaar663bc892019-01-08 23:07:24 +0100845func Test_prop_change_indent()
846 call prop_type_add('comment', {'highlight': 'Directory'})
847 new
848 call setline(1, [' xxx', 'yyyyy'])
849 call prop_add(2, 2, {'length': 2, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200850 let expect = #{type_bufnr: 0, col: 2, length: 2, type: 'comment', start: 1, end: 1, id: 0}
Bram Moolenaar663bc892019-01-08 23:07:24 +0100851 call assert_equal([expect], prop_list(2))
852
853 set shiftwidth=3
854 normal 2G>>
855 call assert_equal(' yyyyy', getline(2))
856 let expect.col += 3
857 call assert_equal([expect], prop_list(2))
858
859 normal 2G==
860 call assert_equal(' yyyyy', getline(2))
861 let expect.col = 6
862 call assert_equal([expect], prop_list(2))
863
864 call prop_clear(2)
865 call prop_add(2, 2, {'length': 5, 'type': 'comment'})
866 let expect.col = 2
867 let expect.length = 5
868 call assert_equal([expect], prop_list(2))
869
870 normal 2G<<
871 call assert_equal(' yyyyy', getline(2))
872 let expect.length = 2
873 call assert_equal([expect], prop_list(2))
874
875 set shiftwidth&
876 call prop_type_delete('comment')
877endfunc
878
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100879" Setup a three line prop in lines 2 - 4.
880" Add short props in line 1 and 5.
881func Setup_three_line_prop()
882 new
883 call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five'])
884 call prop_add(1, 2, {'length': 1, 'type': 'comment'})
885 call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'})
886 call prop_add(5, 2, {'length': 1, 'type': 'comment'})
887endfunc
888
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100889func Test_prop_multiline()
Bram Moolenaara5a78822019-09-04 21:57:18 +0200890 eval 'comment'->prop_type_add({'highlight': 'Directory'})
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100891 new
892 call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz'])
893
894 " start halfway line 1, end halfway line 3
895 call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +0200896 let expect1 = #{type_bufnr: 0, col: 3, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100897 call assert_equal([expect1], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200898 let expect2 = #{type_bufnr: 0, col: 1, length: 10, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100899 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200900 let expect3 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100901 call assert_equal([expect3], prop_list(3))
902 call prop_clear(1, 3)
903
904 " include all three lines
905 call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'})
906 let expect1.col = 1
907 let expect1.length = 8
908 call assert_equal([expect1], prop_list(1))
909 call assert_equal([expect2], prop_list(2))
910 let expect3.length = 9
911 call assert_equal([expect3], prop_list(3))
912 call prop_clear(1, 3)
913
914 bwipe!
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100915
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100916 " Test deleting the first line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100917 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200918 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 +0100919 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200920 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100921 call assert_equal([expect2], prop_list(2))
922 2del
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100923 call assert_equal([expect_short], prop_list(1))
Martin Tournoije2390c72021-07-28 13:30:16 +0200924 let expect2 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100925 call assert_equal([expect2], prop_list(2))
926 bwipe!
927
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100928 " Test deleting the last line of a multi-line prop.
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100929 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200930 let expect3 = #{type_bufnr: 0, col: 1, length: 6, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100931 call assert_equal([expect3], prop_list(3))
Martin Tournoije2390c72021-07-28 13:30:16 +0200932 let expect4 = #{type_bufnr: 0, col: 1, length: 4, type: 'comment', start: 0, end: 1, id: 0}
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100933 call assert_equal([expect4], prop_list(4))
934 4del
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100935 let expect3.end = 1
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100936 call assert_equal([expect3], prop_list(3))
937 call assert_equal([expect_short], prop_list(4))
938 bwipe!
939
Bram Moolenaarb9c67a52019-01-01 19:49:20 +0100940 " Test appending a line below the multi-line text prop start.
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100941 call Setup_three_line_prop()
Martin Tournoije2390c72021-07-28 13:30:16 +0200942 let expect2 = #{type_bufnr: 0, col: 4, length: 4, type: 'comment', start: 1, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100943 call assert_equal([expect2], prop_list(2))
944 call append(2, "new line")
945 call assert_equal([expect2], prop_list(2))
Martin Tournoije2390c72021-07-28 13:30:16 +0200946 let expect3 = #{type_bufnr: 0, col: 1, length: 9, type: 'comment', start: 0, end: 0, id: 0}
Bram Moolenaarb56ac042018-12-28 23:22:40 +0100947 call assert_equal([expect3], prop_list(3))
948 bwipe!
949
Bram Moolenaare3d31b02018-12-24 23:07:04 +0100950 call prop_type_delete('comment')
951endfunc
952
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100953func Run_test_with_line2byte(add_props)
954 new
955 setlocal ff=unix
956 if a:add_props
957 call prop_type_add('textprop', #{highlight: 'Search'})
958 endif
Bram Moolenaare5a0e8c2022-08-09 21:37:55 +0100959 " Add a text prop to every fourth line and then change every fifth line so
960 " that it causes a data block split a few times.
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100961 for nr in range(1, 1000)
962 call setline(nr, 'some longer text here')
Bram Moolenaare5a0e8c2022-08-09 21:37:55 +0100963 if a:add_props && nr % 4 == 0
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100964 call prop_add(nr, 13, #{type: 'textprop', length: 4})
965 endif
966 endfor
Bram Moolenaare5a0e8c2022-08-09 21:37:55 +0100967 let expected = 22 * 997 + 1
968 call assert_equal(expected, line2byte(998))
969
970 for nr in range(1, 1000, 5)
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100971 exe nr .. "s/longer/much more/"
Bram Moolenaare5a0e8c2022-08-09 21:37:55 +0100972 let expected += 3
973 call assert_equal(expected, line2byte(998), 'line ' .. nr)
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100974 endfor
Bram Moolenaarcf85d972022-08-08 14:59:47 +0100975
976 if a:add_props
977 call prop_type_delete('textprop')
978 endif
979 bwipe!
980endfunc
981
Bram Moolenaar9df53b62020-01-13 20:40:51 +0100982func Test_prop_line2byte()
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100983 call prop_type_add('comment', {'highlight': 'Directory'})
984 new
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100985 call setline(1, ['line1', 'second line', ''])
Bram Moolenaar8cf734e2018-12-26 01:09:00 +0100986 set ff=unix
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100987 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100988 call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
Bram Moolenaar00b1e042018-12-26 23:42:10 +0100989 call assert_equal(19, line2byte(3))
Bram Moolenaarb413d2e2018-12-25 23:15:46 +0100990 bwipe!
Bram Moolenaar14c75302021-08-15 14:28:40 +0200991
992 new
Bram Moolenaara401bba2021-08-15 15:04:41 +0200993 setlocal ff=unix
Bram Moolenaar14c75302021-08-15 14:28:40 +0200994 call setline(1, range(500))
995 call assert_equal(1491, line2byte(401))
996 call prop_add(2, 1, {'type': 'comment'})
997 call prop_add(222, 1, {'type': 'comment'})
998 call assert_equal(1491, line2byte(401))
999 call prop_remove({'type': 'comment'})
1000 call assert_equal(1491, line2byte(401))
1001 bwipe!
1002
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +02001003 new
Bram Moolenaar49b93042021-08-25 17:02:00 +02001004 setlocal ff=unix
Bram Moolenaarcdd8a5e2021-08-25 16:40:03 +02001005 call setline(1, range(520))
1006 call assert_equal(1491, line2byte(401))
1007 call prop_add(2, 1, {'type': 'comment'})
1008 call assert_equal(1491, line2byte(401))
1009 2delete
1010 call assert_equal(1489, line2byte(400))
1011 bwipe!
1012
Bram Moolenaarcf85d972022-08-08 14:59:47 +01001013 " Add many lines so that the data block is split.
1014 " With and without props should give the same result.
1015 call Run_test_with_line2byte(0)
1016 call Run_test_with_line2byte(1)
1017
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01001018 call prop_type_delete('comment')
1019endfunc
1020
Bram Moolenaar9df53b62020-01-13 20:40:51 +01001021func Test_prop_byte2line()
1022 new
1023 set ff=unix
1024 call setline(1, ['one one', 'two two', 'three three', 'four four', 'five'])
1025 call assert_equal(4, byte2line(line2byte(4)))
1026 call assert_equal(5, byte2line(line2byte(5)))
1027
1028 call prop_type_add('prop', {'highlight': 'Directory'})
1029 call prop_add(3, 1, {'length': 5, 'type': 'prop'})
1030 call assert_equal(4, byte2line(line2byte(4)))
1031 call assert_equal(5, byte2line(line2byte(5)))
1032
1033 bwipe!
1034 call prop_type_delete('prop')
1035endfunc
1036
Bram Moolenaar59ff6402021-01-30 17:16:28 +01001037func Test_prop_goto_byte()
1038 new
1039 call setline(1, '')
1040 call setline(2, 'two three')
1041 call setline(3, '')
1042 call setline(4, 'four five')
1043
1044 call prop_type_add('testprop', {'highlight': 'Directory'})
1045 call search('^two')
1046 call prop_add(line('.'), col('.'), {
1047 \ 'length': len('two'),
1048 \ 'type': 'testprop'
1049 \ })
1050
1051 call search('two \zsthree')
1052 let expected_pos = line2byte(line('.')) + col('.') - 1
1053 exe expected_pos .. 'goto'
1054 let actual_pos = line2byte(line('.')) + col('.') - 1
1055 eval actual_pos->assert_equal(expected_pos)
1056
1057 call search('four \zsfive')
1058 let expected_pos = line2byte(line('.')) + col('.') - 1
1059 exe expected_pos .. 'goto'
1060 let actual_pos = line2byte(line('.')) + col('.') - 1
1061 eval actual_pos->assert_equal(expected_pos)
1062
1063 call prop_type_delete('testprop')
1064 bwipe!
1065endfunc
1066
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001067func Test_prop_undo()
1068 new
1069 call prop_type_add('comment', {'highlight': 'Directory'})
1070 call setline(1, ['oneone', 'twotwo', 'three'])
1071 " Set 'undolevels' to break changes into undo-able pieces.
1072 set ul&
1073
1074 call prop_add(1, 3, {'end_col': 5, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001075 let expected = [#{type_bufnr: 0, col: 3, length: 2, id: 0, type: 'comment', start: 1, end: 1}]
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001076 call assert_equal(expected, prop_list(1))
1077
1078 " Insert a character, then undo.
1079 exe "normal 0lllix\<Esc>"
1080 set ul&
1081 let expected[0].length = 3
1082 call assert_equal(expected, prop_list(1))
1083 undo
1084 let expected[0].length = 2
1085 call assert_equal(expected, prop_list(1))
1086
1087 " Delete a character, then undo
1088 exe "normal 0lllx"
1089 set ul&
1090 let expected[0].length = 1
1091 call assert_equal(expected, prop_list(1))
1092 undo
1093 let expected[0].length = 2
1094 call assert_equal(expected, prop_list(1))
1095
1096 " Delete the line, then undo
1097 1d
1098 set ul&
1099 call assert_equal([], prop_list(1))
1100 undo
1101 call assert_equal(expected, prop_list(1))
1102
1103 " Insert a character, delete two characters, then undo with "U"
1104 exe "normal 0lllix\<Esc>"
1105 set ul&
1106 let expected[0].length = 3
1107 call assert_equal(expected, prop_list(1))
1108 exe "normal 0lllxx"
1109 set ul&
1110 let expected[0].length = 1
1111 call assert_equal(expected, prop_list(1))
1112 normal U
1113 let expected[0].length = 2
1114 call assert_equal(expected, prop_list(1))
1115
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001116 " substitute a word, then undo
1117 call setline(1, 'the number 123 is highlighted.')
1118 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001119 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001120 call assert_equal(expected, prop_list(1))
1121 set ul&
1122 1s/number/foo
1123 let expected[0].col = 9
1124 call assert_equal(expected, prop_list(1))
1125 undo
1126 let expected[0].col = 12
1127 call assert_equal(expected, prop_list(1))
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001128 call prop_clear(1)
1129
1130 " substitute with backslash
1131 call setline(1, 'the number 123 is highlighted.')
1132 call prop_add(1, 12, {'length': 3, 'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001133 let expected = [#{type_bufnr: 0, col: 12, length: 3, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001134 call assert_equal(expected, prop_list(1))
1135 1s/the/\The
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/^/\~
1141 let expected[0].col += 1
1142 call assert_equal(expected, prop_list(1))
1143 1s/123/12\\3
1144 let expected[0].length += 1
1145 call assert_equal(expected, prop_list(1))
1146 call prop_clear(1)
Bram Moolenaar338dfda2019-05-19 15:19:57 +02001147
Bram Moolenaar7f1664e2019-01-04 17:21:24 +01001148 bwipe!
1149 call prop_type_delete('comment')
1150endfunc
1151
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001152func Test_prop_delete_text()
1153 new
1154 call prop_type_add('comment', {'highlight': 'Directory'})
1155 call setline(1, ['oneone', 'twotwo', 'three'])
1156
1157 " zero length property
1158 call prop_add(1, 3, {'type': 'comment'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001159 let expected = [#{type_bufnr: 0, col: 3, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001160 call assert_equal(expected, prop_list(1))
1161
1162 " delete one char moves the property
1163 normal! x
Martin Tournoije2390c72021-07-28 13:30:16 +02001164 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001165 call assert_equal(expected, prop_list(1))
1166
1167 " delete char of the property has no effect
1168 normal! lx
Martin Tournoije2390c72021-07-28 13:30:16 +02001169 let expected = [#{type_bufnr: 0, col: 2, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001170 call assert_equal(expected, prop_list(1))
1171
1172 " delete more chars moves property to first column, is not deleted
1173 normal! 0xxxx
Martin Tournoije2390c72021-07-28 13:30:16 +02001174 let expected = [#{type_bufnr: 0, col: 1, length: 0, id: 0, type: 'comment', start: 1, end: 1} ]
Bram Moolenaarecafcc12019-11-16 20:41:51 +01001175 call assert_equal(expected, prop_list(1))
1176
1177 bwipe!
1178 call prop_type_delete('comment')
1179endfunc
1180
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001181" screenshot test with textprop highlighting
Bram Moolenaar8055d172019-05-17 22:57:26 +02001182func Test_textprop_screenshot_various()
Bram Moolenaar34390282019-10-16 14:38:26 +02001183 CheckScreendump
Bram Moolenaared79d1e2019-02-22 14:38:58 +01001184 " The Vim running in the terminal needs to use utf-8.
Bram Moolenaar34390282019-10-16 14:38:26 +02001185 if g:orig_encoding != 'utf-8'
1186 throw 'Skipped: not using utf-8'
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001187 endif
1188 call writefile([
Bram Moolenaarde24a872019-05-05 15:48:00 +02001189 \ "call setline(1, ["
1190 \ .. "'One two',"
1191 \ .. "'Numbér 123 änd thœn 4¾7.',"
1192 \ .. "'--aa--bb--cc--dd--',"
1193 \ .. "'// comment with error in it',"
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001194 \ .. "'first line',"
1195 \ .. "' second line ',"
1196 \ .. "'third line',"
1197 \ .. "' fourth line',"
Bram Moolenaarde24a872019-05-05 15:48:00 +02001198 \ .. "])",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001199 \ "hi NumberProp ctermfg=blue",
1200 \ "hi LongProp ctermbg=yellow",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001201 \ "hi BackgroundProp ctermbg=lightgrey",
1202 \ "hi UnderlineProp cterm=underline",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001203 \ "call prop_type_add('number', {'highlight': 'NumberProp'})",
Bram Moolenaara5a78822019-09-04 21:57:18 +02001204 \ "call prop_type_add('long', {'highlight': 'NumberProp'})",
1205 \ "call prop_type_change('long', {'highlight': 'LongProp'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001206 \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})",
1207 \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})",
1208 \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001209 \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})",
1210 \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})",
1211 \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})",
Bram Moolenaar58e32ab2019-11-12 22:44:22 +01001212 \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001213 \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001214 \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})",
1215 \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001216 \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})",
1217 \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})",
1218 \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})",
1219 \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})",
Bram Moolenaardbd43162019-11-09 21:28:14 +01001220 \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})",
1221 \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001222 \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001223 \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})",
1224 \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})",
1225 \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})",
1226 \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})",
Bram Moolenaarbfd45122019-05-17 13:05:07 +02001227 \ "set number cursorline",
Bram Moolenaarb9c67a52019-01-01 19:49:20 +01001228 \ "hi clear SpellBad",
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001229 \ "set spell",
Bram Moolenaarde24a872019-05-05 15:48:00 +02001230 \ "syn match Comment '//.*'",
1231 \ "hi Comment ctermfg=green",
Bram Moolenaar44746aa2019-01-02 00:02:11 +01001232 \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>",
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01001233 \ "normal 3G0lli\<BS>\<Esc>",
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001234 \ "normal 6G0i\<BS>\<Esc>",
1235 \ "normal 3J",
1236 \ "normal 3G",
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001237 \], 'XtestProp', 'D')
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001238 let buf = RunVimInTerminal('-S XtestProp', {'rows': 8})
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001239 call VerifyScreenDump(buf, 'Test_textprop_01', {})
Bram Moolenaare3d31b02018-12-24 23:07:04 +01001240
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001241 " clean up
1242 call StopVimInTerminal(buf)
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01001243endfunc
Bram Moolenaar8055d172019-05-17 22:57:26 +02001244
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001245func Test_textprop_hl_override()
1246 CheckScreendump
1247
1248 let lines =<< trim END
1249 call setline(1, ['One one one one one', 'Two two two two two', 'Three three three three'])
1250 hi OverProp ctermfg=blue ctermbg=yellow
1251 hi CursorLine cterm=bold,underline ctermfg=red ctermbg=green
1252 hi Vsual ctermfg=cyan ctermbg=grey
1253 call prop_type_add('under', #{highlight: 'OverProp'})
1254 call prop_type_add('over', #{highlight: 'OverProp', override: 1})
1255 call prop_add(1, 5, #{type: 'under', length: 4})
1256 call prop_add(1, 13, #{type: 'over', length: 4})
1257 call prop_add(2, 5, #{type: 'under', length: 4})
1258 call prop_add(2, 13, #{type: 'over', length: 4})
1259 call prop_add(3, 5, #{type: 'under', length: 4})
1260 call prop_add(3, 13, #{type: 'over', length: 4})
1261 set cursorline
1262 2
1263 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001264 call writefile(lines, 'XtestOverProp', 'D')
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001265 let buf = RunVimInTerminal('-S XtestOverProp', {'rows': 8})
1266 call VerifyScreenDump(buf, 'Test_textprop_hl_override_1', {})
1267
1268 call term_sendkeys(buf, "3Gllv$hh")
1269 call VerifyScreenDump(buf, 'Test_textprop_hl_override_2', {})
1270 call term_sendkeys(buf, "\<Esc>")
1271
1272 " clean up
1273 call StopVimInTerminal(buf)
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +01001274endfunc
1275
Bram Moolenaar8055d172019-05-17 22:57:26 +02001276func RunTestVisualBlock(width, dump)
1277 call writefile([
1278 \ "call setline(1, ["
1279 \ .. "'xxxxxxxxx 123 x',"
1280 \ .. "'xxxxxxxx 123 x',"
1281 \ .. "'xxxxxxx 123 x',"
1282 \ .. "'xxxxxx 123 x',"
1283 \ .. "'xxxxx 123 x',"
1284 \ .. "'xxxx 123 xx',"
1285 \ .. "'xxx 123 xxx',"
1286 \ .. "'xx 123 xxxx',"
1287 \ .. "'x 123 xxxxx',"
1288 \ .. "' 123 xxxxxx',"
1289 \ .. "])",
1290 \ "hi SearchProp ctermbg=yellow",
1291 \ "call prop_type_add('search', {'highlight': 'SearchProp'})",
1292 \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})",
1293 \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})",
1294 \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})",
1295 \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})",
1296 \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})",
1297 \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})",
1298 \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})",
1299 \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})",
1300 \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})",
1301 \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})",
1302 \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx",
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001303 \], 'XtestPropVis', 'D')
Bram Moolenaar8055d172019-05-17 22:57:26 +02001304 let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12})
1305 call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {})
1306
1307 " clean up
1308 call StopVimInTerminal(buf)
Bram Moolenaar8055d172019-05-17 22:57:26 +02001309endfunc
1310
1311" screenshot test with Visual block mode operations
1312func Test_textprop_screenshot_visual()
Bram Moolenaar34390282019-10-16 14:38:26 +02001313 CheckScreendump
Bram Moolenaar8055d172019-05-17 22:57:26 +02001314
1315 " Delete two columns while text props are three chars wide.
1316 call RunTestVisualBlock(2, '01')
1317
1318 " Same, but delete four columns
1319 call RunTestVisualBlock(4, '02')
1320endfunc
Bram Moolenaard79eef22019-05-24 20:41:55 +02001321
Bram Moolenaara956bf62019-06-19 17:34:24 +02001322func Test_textprop_after_tab()
Bram Moolenaar34390282019-10-16 14:38:26 +02001323 CheckScreendump
Bram Moolenaar37e66cf2019-06-19 18:16:10 +02001324
Bram Moolenaara956bf62019-06-19 17:34:24 +02001325 let lines =<< trim END
1326 call setline(1, [
1327 \ "\txxx",
1328 \ "x\txxx",
1329 \ ])
1330 hi SearchProp ctermbg=yellow
1331 call prop_type_add('search', {'highlight': 'SearchProp'})
1332 call prop_add(1, 2, {'length': 3, 'type': 'search'})
1333 call prop_add(2, 3, {'length': 3, 'type': 'search'})
1334 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001335 call writefile(lines, 'XtestPropTab', 'D')
Bram Moolenaara956bf62019-06-19 17:34:24 +02001336 let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6})
1337 call VerifyScreenDump(buf, 'Test_textprop_tab', {})
1338
1339 " clean up
1340 call StopVimInTerminal(buf)
Bram Moolenaara956bf62019-06-19 17:34:24 +02001341endfunc
1342
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001343func Test_textprop_nowrap_scrolled()
1344 CheckScreendump
1345
1346 let lines =<< trim END
1347 vim9script
1348 set nowrap
1349 setline(1, 'The number 123 is smaller than 4567.' .. repeat('X', &columns))
1350 prop_type_add('number', {'highlight': 'ErrorMsg'})
1351 prop_add(1, 12, {'length': 3, 'type': 'number'})
1352 prop_add(1, 32, {'length': 4, 'type': 'number'})
1353 feedkeys('gg20zl', 'nxt')
1354 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001355 call writefile(lines, 'XtestNowrap', 'D')
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001356 let buf = RunVimInTerminal('-S XtestNowrap', {'rows': 6})
1357 call VerifyScreenDump(buf, 'Test_textprop_nowrap_01', {})
1358
1359 call term_sendkeys(buf, "$")
1360 call VerifyScreenDump(buf, 'Test_textprop_nowrap_02', {})
1361
1362 " clean up
1363 call StopVimInTerminal(buf)
Bram Moolenaarf3fa1842021-02-10 17:20:28 +01001364endfunc
1365
Bram Moolenaar952c9b02022-08-10 16:00:33 +01001366func Test_textprop_text_priority()
1367 CheckScreendump
1368
1369 let lines =<< trim END
1370 call setline(1, "function( call, argument, here )")
1371
1372 call prop_type_add('one', #{highlight: 'Error'})
1373 call prop_type_add('two', #{highlight: 'Function'})
1374 call prop_type_add('three', #{highlight: 'DiffChange'})
1375 call prop_type_add('arg', #{highlight: 'Search'})
1376
1377 call prop_add(1, 27, #{type: 'arg', length: len('here')})
1378 call prop_add(1, 27, #{type: 'three', text: 'three: '})
1379 call prop_add(1, 11, #{type: 'one', text: 'one: '})
1380 call prop_add(1, 11, #{type: 'arg', length: len('call')})
1381 call prop_add(1, 17, #{type: 'two', text: 'two: '})
1382 call prop_add(1, 17, #{type: 'arg', length: len('argument')})
1383 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001384 call writefile(lines, 'XtestPropPrio', 'D')
Bram Moolenaar952c9b02022-08-10 16:00:33 +01001385 let buf = RunVimInTerminal('-S XtestPropPrio', {'rows': 5})
1386 call VerifyScreenDump(buf, 'Test_prop_at_same_pos', {})
1387
1388 " clean up
1389 call StopVimInTerminal(buf)
Bram Moolenaar952c9b02022-08-10 16:00:33 +01001390endfunc
1391
Bram Moolenaar34390282019-10-16 14:38:26 +02001392func Test_textprop_with_syntax()
1393 CheckScreendump
1394
1395 let lines =<< trim END
1396 call setline(1, [
1397 \ "(abc)",
1398 \ ])
1399 syn match csParens "[()]" display
1400 hi! link csParens MatchParen
1401
1402 call prop_type_add('TPTitle', #{ highlight: 'Title' })
1403 call prop_add(1, 2, #{type: 'TPTitle', end_col: 5})
1404 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001405 call writefile(lines, 'XtestPropSyn', 'D')
Bram Moolenaar34390282019-10-16 14:38:26 +02001406 let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6})
1407 call VerifyScreenDump(buf, 'Test_textprop_syn_1', {})
1408
1409 " clean up
1410 call StopVimInTerminal(buf)
Bram Moolenaar34390282019-10-16 14:38:26 +02001411endfunc
1412
Bram Moolenaard79eef22019-05-24 20:41:55 +02001413" Adding a text property to a new buffer should not fail
1414func Test_textprop_empty_buffer()
1415 call prop_type_add('comment', {'highlight': 'Search'})
1416 new
1417 call prop_add(1, 1, {'type': 'comment'})
1418 close
Bram Moolenaaradfde112019-05-25 22:11:45 +02001419 call prop_type_delete('comment')
1420endfunc
1421
Bram Moolenaard74af422019-06-28 21:38:00 +02001422" Adding a text property with invalid highlight should be ignored.
1423func Test_textprop_invalid_highlight()
1424 call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:')
1425 new
Ben Jacksona7704222022-08-20 20:54:51 +01001426 call setline(1, ['asdf', 'asdf'])
Bram Moolenaard74af422019-06-28 21:38:00 +02001427 call prop_add(1, 1, {'length': 4, 'type': 'dni'})
1428 redraw
1429 bwipe!
1430 call prop_type_delete('dni')
1431endfunc
1432
Bram Moolenaaradfde112019-05-25 22:11:45 +02001433" Adding a text property to an empty buffer and then editing another
1434func Test_textprop_empty_buffer_next()
1435 call prop_type_add("xxx", {})
1436 call prop_add(1, 1, {"type": "xxx"})
1437 next X
1438 call prop_type_delete('xxx')
Bram Moolenaard79eef22019-05-24 20:41:55 +02001439endfunc
Bram Moolenaarf0884c52019-05-24 21:22:29 +02001440
1441func Test_textprop_remove_from_buf()
1442 new
1443 let buf = bufnr('')
1444 call prop_type_add('one', {'bufnr': buf})
1445 call prop_add(1, 1, {'type': 'one', 'id': 234})
1446 file x
1447 edit y
1448 call prop_remove({'id': 234, 'bufnr': buf}, 1)
1449 call prop_type_delete('one', {'bufnr': buf})
1450 bwipe! x
1451 close
1452endfunc
Bram Moolenaar45311b52019-08-13 22:27:32 +02001453
1454func Test_textprop_in_unloaded_buf()
1455 edit Xaaa
1456 call setline(1, 'aaa')
1457 write
1458 edit Xbbb
1459 call setline(1, 'bbb')
1460 write
1461 let bnr = bufnr('')
1462 edit Xaaa
1463
1464 call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'})
1465 call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:')
1466 exe 'buf ' .. bnr
1467 call assert_equal('bbb', getline(1))
1468 call assert_equal(0, prop_list(1)->len())
1469
1470 bwipe! Xaaa
1471 bwipe! Xbbb
1472 cal delete('Xaaa')
1473 cal delete('Xbbb')
1474endfunc
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001475
1476func Test_proptype_substitute2()
1477 new
1478 " text_prop.vim
1479 call setline(1, [
1480 \ 'The num 123 is smaller than 4567.',
1481 \ '123 The number 123 is smaller than 4567.',
1482 \ '123 The number 123 is smaller than 4567.'])
1483
1484 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1485
1486 call prop_add(1, 12, {'length': 3, 'type': 'number'})
1487 call prop_add(2, 1, {'length': 3, 'type': 'number'})
1488 call prop_add(3, 36, {'length': 4, 'type': 'number'})
1489 set ul&
Martin Tournoije2390c72021-07-28 13:30:16 +02001490 let expected = [
1491 \ #{type_bufnr: 0, id: 0, col: 13, end: 1, type: 'number', length: 3, start: 1},
1492 \ #{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'number', length: 3, start: 1},
1493 \ #{type_bufnr: 0, id: 0, col: 50, end: 1, type: 'number', length: 4, start: 1}]
1494
1495 " TODO
Bram Moolenaar213bbaf2022-08-05 19:46:48 +01001496 if 0
1497 " Add some text in between
1498 %s/\s\+/ /g
1499 call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3))
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001500
Bram Moolenaar213bbaf2022-08-05 19:46:48 +01001501 " remove some text
1502 :1s/[a-z]\{3\}//g
1503 let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
1504 call assert_equal(expected, prop_list(1))
1505 endif
1506
1507 call prop_type_delete('number')
Bram Moolenaar1fd30d72019-10-25 22:13:29 +02001508 bwipe!
1509endfunc
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001510
Bram Moolenaar8902b312020-09-20 21:04:35 +02001511" This was causing property corruption.
1512func Test_proptype_substitute3()
1513 new
1514 call setline(1, ['abcxxx', 'def'])
1515 call prop_type_add("test", {"highlight": "Search"})
1516 call prop_add(1, 2, {"end_lnum": 2, "end_col": 2, "type": "test"})
1517 %s/x\+$//
1518 redraw
1519
1520 call prop_type_delete('test')
1521 bwipe!
1522endfunc
1523
Bram Moolenaar213bbaf2022-08-05 19:46:48 +01001524func Test_proptype_substitute_join()
1525 new
1526 call setline(1, [
1527 \ 'This is some end',
1528 \ 'start is highlighted end',
1529 \ 'some is highlighted',
1530 \ 'start is also highlighted'])
1531
1532 call prop_type_add('number', {'highlight': 'ErrorMsg'})
1533
1534 call prop_add(1, 6, {'length': 2, 'type': 'number'})
1535 call prop_add(2, 7, {'length': 2, 'type': 'number'})
1536 call prop_add(3, 6, {'length': 2, 'type': 'number'})
1537 call prop_add(4, 7, {'length': 2, 'type': 'number'})
1538 " The highlighted "is" in line 1, 2 and 4 is kept and ajudsted.
1539 " The highlighted "is" in line 3 is deleted.
1540 let expected = [
1541 \ #{type_bufnr: 0, id: 0, col: 6, end: 1, type: 'number', length: 2, start: 1},
1542 \ #{type_bufnr: 0, id: 0, col: 21, end: 1, type: 'number', length: 2, start: 1},
1543 \ #{type_bufnr: 0, id: 0, col: 43, end: 1, type: 'number', length: 2, start: 1}]
1544
1545 s/end\nstart/joined/
1546 s/end\n.*\nstart/joined/
1547 call assert_equal('This is some joined is highlighted joined is also highlighted', getline(1))
1548 call assert_equal(expected, prop_list(1))
1549
1550 call prop_type_delete('number')
1551 bwipe!
1552endfunc
1553
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001554func SaveOptions()
1555 let d = #{tabstop: &tabstop,
1556 \ softtabstop: &softtabstop,
1557 \ shiftwidth: &shiftwidth,
1558 \ expandtab: &expandtab,
1559 \ foldmethod: '"' .. &foldmethod .. '"',
1560 \ }
1561 return d
1562endfunc
1563
1564func RestoreOptions(dict)
1565 for name in keys(a:dict)
1566 exe 'let &' .. name .. ' = ' .. a:dict[name]
1567 endfor
1568endfunc
1569
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001570func Test_textprop_noexpandtab()
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001571 new
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001572 let save_dict = SaveOptions()
1573
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001574 set tabstop=8
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001575 set softtabstop=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001576 set shiftwidth=4
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001577 set noexpandtab
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001578 set foldmethod=marker
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001579
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001580 call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx")
1581 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1582 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1583 call feedkeys("0i\<tab>", "tx")
1584 call prop_remove({'type': 'test'})
1585 call prop_add(1, 2, {'end_col': 3, 'type': 'test'})
1586 call feedkeys("A\<left>\<tab>", "tx")
1587 call prop_remove({'type': 'test'})
1588 try
1589 " It is correct that this does not pass
1590 call prop_add(1, 6, {'end_col': 7, 'type': 'test'})
1591 " Has already collapsed here, start_col:6 does not result in an error
1592 call feedkeys("A\<left>\<tab>", "tx")
1593 catch /^Vim\%((\a\+)\)\=:E964/
1594 endtry
1595 call prop_remove({'type': 'test'})
Bram Moolenaarac15fd82020-01-09 21:35:48 +01001596 call prop_type_delete('test')
1597
1598 call RestoreOptions(save_dict)
1599 bwipe!
1600endfunc
1601
1602func Test_textprop_noexpandtab_redraw()
1603 new
1604 let save_dict = SaveOptions()
1605
1606 set tabstop=8
1607 set softtabstop=4
1608 set shiftwidth=4
1609 set noexpandtab
1610 set foldmethod=marker
1611
1612 call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx")
1613 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1614 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1615 call feedkeys("0i\<tab>", "tx")
1616 " Internally broken at the next line
1617 call feedkeys("A\<left>\<tab>", "tx")
1618 redraw
1619 " Index calculation failed internally on next line
1620 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
1621 call prop_remove({'type': 'test', 'all': v:true})
1622 call prop_type_delete('test')
1623 call prop_type_delete('test')
1624
1625 call RestoreOptions(save_dict)
1626 bwipe!
1627endfunc
1628
1629func Test_textprop_ins_str()
1630 new
1631 call setline(1, 'just some text')
1632 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1633 call prop_add(1, 1, {'end_col': 2, 'type': 'test'})
Martin Tournoije2390c72021-07-28 13:30:16 +02001634 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 +01001635
1636 call feedkeys("foi\<F8>\<Esc>", "tx")
1637 call assert_equal('just s<F8>ome text', getline(1))
Martin Tournoije2390c72021-07-28 13:30:16 +02001638 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 +01001639
1640 bwipe!
1641 call prop_remove({'type': 'test'})
1642 call prop_type_delete('test')
Bram Moolenaar5cb0b932020-01-03 21:25:59 +01001643endfunc
Bram Moolenaar66b98852020-03-11 19:15:52 +01001644
1645func Test_find_prop_later_in_line()
1646 new
1647 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1648 call setline(1, 'just some text')
1649 call prop_add(1, 1, {'length': 4, 'type': 'test'})
1650 call prop_add(1, 10, {'length': 3, 'type': 'test'})
1651
Martin Tournoije2390c72021-07-28 13:30:16 +02001652 call assert_equal(
1653 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 10, end: 1, type: 'test', length: 3, start: 1},
1654 \ prop_find(#{type: 'test', lnum: 1, col: 6}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001655
1656 bwipe!
1657 call prop_type_delete('test')
1658endfunc
1659
1660func Test_find_zerowidth_prop_sol()
1661 new
1662 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1663 call setline(1, 'just some text')
1664 call prop_add(1, 1, {'length': 0, 'type': 'test'})
1665
Martin Tournoije2390c72021-07-28 13:30:16 +02001666 call assert_equal(
1667 \ #{type_bufnr: 0, id: 0, lnum: 1, col: 1, end: 1, type: 'test', length: 0, start: 1},
1668 \ prop_find(#{type: 'test', lnum: 1}))
Bram Moolenaar66b98852020-03-11 19:15:52 +01001669
1670 bwipe!
1671 call prop_type_delete('test')
1672endfunc
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001673
1674" Test for passing invalid arguments to prop_xxx() functions
1675func Test_prop_func_invalid_args()
1676 call assert_fails('call prop_clear(1, 2, [])', 'E715:')
1677 call assert_fails('call prop_clear(-1, 2)', 'E16:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001678 call assert_fails('call prop_find(test_null_dict())', 'E1297:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001679 call assert_fails('call prop_find({"bufnr" : []})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001680 call assert_fails('call prop_find({})', 'E968:')
1681 call assert_fails('call prop_find({}, "x")', 'E474:')
1682 call assert_fails('call prop_find({"lnum" : -2})', 'E16:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001683 call assert_fails('call prop_list(1, [])', 'E1206:')
Bram Moolenaar9d489562020-07-30 20:08:50 +02001684 call assert_fails('call prop_list(-1, {})', 'E16:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001685 call assert_fails('call prop_remove([])', 'E1206:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001686 call assert_fails('call prop_remove({}, -2)', 'E16:')
1687 call assert_fails('call prop_remove({})', 'E968:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001688 call assert_fails('call prop_type_add([], {})', 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001689 call assert_fails("call prop_type_change('long', {'xyz' : 10})", 'E971:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001690 call assert_fails("call prop_type_delete([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001691 call assert_fails("call prop_type_delete('xyz', [])", 'E715:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001692 call assert_fails("call prop_type_get([])", 'E730:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001693 call assert_fails("call prop_type_get('', [])", 'E474:')
1694 call assert_fails("call prop_type_list([])", 'E715:')
Bram Moolenaar3dc34742021-03-02 13:36:47 +01001695 call assert_fails("call prop_type_add('yyy', 'not_a_dict')", 'E715:')
1696 call assert_fails("call prop_add(1, 5, {'type':'missing_type', 'length':1})", 'E971:')
1697 call assert_fails("call prop_add(1, 5, {'type': ''})", 'E971:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001698 call assert_fails('call prop_add(1, 1, 0)', 'E1206:')
Bram Moolenaar3dc34742021-03-02 13:36:47 +01001699
1700 new
1701 call setline(1, ['first', 'second'])
1702 call prop_type_add('xxx', {})
1703
1704 call assert_fails("call prop_type_add('xxx', {})", 'E969:')
1705 call assert_fails("call prop_add(2, 0, {'type': 'xxx'})", 'E964:')
1706 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':1})", 'E475:')
1707 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_lnum':3})", 'E966:')
1708 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'length':-1})", 'E475:')
1709 call assert_fails("call prop_add(2, 3, {'type': 'xxx', 'end_col':0})", 'E475:')
1710 call assert_fails("call prop_add(2, 3, {'length':1})", 'E965:')
1711
1712 call prop_type_delete('xxx')
1713 bwipe!
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001714endfunc
1715
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001716func Test_prop_split_join()
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001717 new
1718 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1719 call setline(1, 'just some text')
1720 call prop_add(1, 6, {'length': 4, 'type': 'test'})
1721
1722 " Split in middle of "some"
1723 execute "normal! 8|i\<CR>"
Martin Tournoije2390c72021-07-28 13:30:16 +02001724 call assert_equal(
1725 \ [#{type_bufnr: 0, id: 0, col: 6, end: 0, type: 'test', length: 2, start: 1}],
1726 \ prop_list(1))
1727 call assert_equal(
1728 \ [#{type_bufnr: 0, id: 0, col: 1, end: 1, type: 'test', length: 2, start: 0}],
1729 \ prop_list(2))
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001730
1731 " Join the two lines back together
1732 normal! 1GJ
Martin Tournoije2390c72021-07-28 13:30:16 +02001733 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 +02001734
1735 bwipe!
1736 call prop_type_delete('test')
1737endfunc
1738
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001739func Test_prop_increment_decrement()
1740 new
1741 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1742 call setline(1, 'its 998 times')
1743 call prop_add(1, 5, {'length': 3, 'type': 'test'})
1744
1745 exe "normal! 0f9\<C-A>"
1746 eval getline(1)->assert_equal('its 999 times')
1747 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001748 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 3, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001749
1750 exe "normal! 0f9\<C-A>"
1751 eval getline(1)->assert_equal('its 1000 times')
1752 eval prop_list(1)->assert_equal([
Martin Tournoije2390c72021-07-28 13:30:16 +02001753 \ #{type_bufnr: 0, id: 0, col: 5, end: 1, type: 'test', length: 4, start: 1}])
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02001754
1755 bwipe!
1756 call prop_type_delete('test')
1757endfunc
1758
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001759func Test_prop_block_insert()
1760 new
1761 call prop_type_add('test', {'highlight': 'ErrorMsg'})
1762 call setline(1, ['one ', 'two '])
1763 call prop_add(1, 1, {'length': 3, 'type': 'test'})
1764 call prop_add(2, 1, {'length': 3, 'type': 'test'})
1765
1766 " insert "xx" in the first column of both lines
1767 exe "normal! gg0\<C-V>jIxx\<Esc>"
1768 eval getline(1, 2)->assert_equal(['xxone ', 'xxtwo '])
Martin Tournoije2390c72021-07-28 13:30:16 +02001769 let expected = [#{type_bufnr: 0, id: 0, col: 3, end: 1, type: 'test', length: 3, start: 1}]
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +02001770 eval prop_list(1)->assert_equal(expected)
1771 eval prop_list(2)->assert_equal(expected)
1772
1773 " insert "yy" inside the text props to make them longer
1774 exe "normal! gg03l\<C-V>jIyy\<Esc>"
1775 eval getline(1, 2)->assert_equal(['xxoyyne ', 'xxtyywo '])
1776 let expected[0].length = 5
1777 eval prop_list(1)->assert_equal(expected)
1778 eval prop_list(2)->assert_equal(expected)
1779
1780 " insert "zz" after the text props, text props don't change
1781 exe "normal! gg07l\<C-V>jIzz\<Esc>"
1782 eval getline(1, 2)->assert_equal(['xxoyynezz ', 'xxtyywozz '])
1783 eval prop_list(1)->assert_equal(expected)
1784 eval prop_list(2)->assert_equal(expected)
1785
1786 bwipe!
1787 call prop_type_delete('test')
1788endfunc
1789
Bram Moolenaar23999d72020-12-23 14:36:00 +01001790" this was causing an ml_get error because w_botline was wrong
1791func Test_prop_one_line_window()
1792 enew
1793 call range(2)->setline(1)
1794 call prop_type_add('testprop', {})
1795 call prop_add(1, 1, {'type': 'testprop'})
1796 call popup_create('popup', {'textprop': 'testprop'})
1797 $
1798 new
1799 wincmd _
1800 call feedkeys("\r", 'xt')
1801 redraw
1802
1803 call popup_clear()
1804 call prop_type_delete('testprop')
1805 close
1806 bwipe!
1807endfunc
1808
Bram Moolenaarf05a1e52022-08-02 11:48:53 +01001809def Test_prop_column_zero_error()
1810 prop_type_add('proptype', {highlight: 'Search'})
1811 var caught = false
1812 try
1813 popup_create([{
1814 text: 'a',
1815 props: [{col: 0, length: 1, type: 'type'}],
1816 }], {})
1817 catch /E964:/
1818 caught = true
1819 endtry
1820 assert_true(caught)
1821
1822 popup_clear()
1823 prop_type_delete('proptype')
1824enddef
1825
Bram Moolenaar840f91f2021-05-26 22:32:10 +02001826" This was calling ml_append_int() and copy a text property from a previous
1827" line at the wrong moment. Exact text length matters.
1828def Test_prop_splits_data_block()
1829 new
1830 var lines: list<string> = [repeat('x', 35)]->repeat(41)
1831 + [repeat('!', 35)]
1832 + [repeat('x', 35)]->repeat(56)
1833 lines->setline(1)
1834 prop_type_add('someprop', {highlight: 'ErrorMsg'})
1835 prop_add(1, 27, {end_lnum: 1, end_col: 70, type: 'someprop'})
1836 prop_remove({type: 'someprop'}, 1)
1837 prop_add(35, 22, {end_lnum: 43, end_col: 43, type: 'someprop'})
1838 prop_remove({type: 'someprop'}, 35, 43)
1839 assert_equal([], prop_list(42))
1840
1841 bwipe!
1842 prop_type_delete('someprop')
1843enddef
1844
Bram Moolenaar4cd5c522021-06-27 13:04:00 +02001845" This was calling ml_delete_int() and try to change text properties.
1846def Test_prop_add_delete_line()
1847 new
1848 var a = 10
1849 var b = 20
1850 repeat([''], a)->append('$')
1851 prop_type_add('Test', {highlight: 'ErrorMsg'})
1852 for lnum in range(1, a)
1853 for col in range(1, b)
1854 prop_add(1, 1, {end_lnum: lnum, end_col: col, type: 'Test'})
1855 endfor
1856 endfor
1857
1858 # check deleting lines is OK
1859 :5del
1860 :1del
1861 :$del
1862
1863 prop_type_delete('Test')
1864 bwipe!
1865enddef
1866
Paul Ollis1bdc60e2022-05-15 22:24:55 +01001867" This test is to detect a regression related to #10430. It is not an attempt
1868" fully cover deleting lines in the presence of multi-line properties.
1869def Test_delete_line_within_multiline_prop()
1870 new
1871 setline(1, '# Top.')
1872 append(1, ['some_text = """', 'A string.', '"""', '# Bottom.'])
1873 prop_type_add('Identifier', {'highlight': 'ModeMsg', 'priority': 0, 'combine': 0, 'start_incl': 0, 'end_incl': 0})
1874 prop_type_add('String', {'highlight': 'MoreMsg', 'priority': 0, 'combine': 0, 'start_incl': 0, 'end_incl': 0})
1875 prop_add(2, 1, {'type': 'Identifier', 'end_lnum': 2, 'end_col': 9})
1876 prop_add(2, 13, {'type': 'String', 'end_lnum': 4, 'end_col': 4})
1877
1878 # The property for line 3 should extend into the previous and next lines.
1879 var props = prop_list(3)
1880 var prop = props[0]
1881 assert_equal(1, len(props))
1882 assert_equal(0, prop['start'])
1883 assert_equal(0, prop['end'])
1884
1885 # This deletion should run without raising an exception.
1886 try
1887 :2 del
1888 catch
1889 assert_report('Line delete should have workd, but it raised an error.')
1890 endtry
1891
1892 # The property for line 2 (was 3) should no longer extend into the previous
1893 # line.
1894 props = prop_list(2)
1895 prop = props[0]
1896 assert_equal(1, len(props))
1897 assert_equal(1, prop['start'], 'Property was not changed to start within the line.')
1898
1899 # This deletion should run without raising an exception.
1900 try
1901 :3 del
1902 catch
1903 assert_report('Line delete should have workd, but it raised an error.')
1904 endtry
1905
1906 # The property for line 2 (originally 3) should no longer extend into the next
1907 # line.
1908 props = prop_list(2)
1909 prop = props[0]
1910 assert_equal(1, len(props))
1911 assert_equal(1, prop['end'], 'Property was not changed to end within the line.')
1912
1913 prop_type_delete('Identifier')
1914 prop_type_delete('String')
1915 bwip!
1916enddef
1917
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001918func Test_prop_in_linebreak()
1919 CheckRunVimInTerminal
1920
1921 let lines =<< trim END
1922 set breakindent linebreak breakat+=]
1923 call printf('%s]%s', repeat('x', 50), repeat('x', 70))->setline(1)
Bram Moolenaarcf2bb632022-09-02 13:26:29 +01001924 call prop_type_add('test', #{highlight: 'MatchParen'})
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001925 call prop_add(1, 51, #{length: 1, type: 'test'})
Bram Moolenaarcf2bb632022-09-02 13:26:29 +01001926 func AddMatch()
1927 syntax on
1928 syntax match xTest /.*/
1929 hi link xTest Comment
1930 set signcolumn=yes
1931 endfunc
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001932 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001933 call writefile(lines, 'XscriptPropLinebreak', 'D')
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001934 let buf = RunVimInTerminal('-S XscriptPropLinebreak', #{rows: 10})
Bram Moolenaarcf2bb632022-09-02 13:26:29 +01001935 call VerifyScreenDump(buf, 'Test_prop_linebreak_1', {})
1936
1937 call term_sendkeys(buf, ":call AddMatch()\<CR>")
1938 call VerifyScreenDump(buf, 'Test_prop_linebreak_2', {})
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001939
1940 call StopVimInTerminal(buf)
Bram Moolenaar6b839ac2021-11-29 21:12:35 +00001941endfunc
1942
Bram Moolenaar52de3a82022-08-10 13:12:03 +01001943func Test_prop_with_linebreak()
1944 CheckRunVimInTerminal
1945
1946 let lines =<< trim END
1947 vim9script
1948 set linebreak
1949 setline(1, 'one twoword')
1950 prop_type_add('test', {highlight: 'Special'})
1951 prop_add(1, 4, {text: ': virtual text', type: 'test', text_wrap: 'wrap'})
1952 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001953 call writefile(lines, 'XscriptPropWithLinebreak', 'D')
Bram Moolenaar52de3a82022-08-10 13:12:03 +01001954 let buf = RunVimInTerminal('-S XscriptPropWithLinebreak', #{rows: 6, cols: 50})
1955 call VerifyScreenDump(buf, 'Test_prop_with_linebreak_1', {})
1956 call term_sendkeys(buf, "iasdf asdf asdf asdf asdf as\<Esc>")
1957 call VerifyScreenDump(buf, 'Test_prop_with_linebreak_2', {})
1958
1959 call StopVimInTerminal(buf)
Bram Moolenaar52de3a82022-08-10 13:12:03 +01001960endfunc
1961
Bram Moolenaar1d8844a2022-08-10 13:39:35 +01001962func Test_prop_with_wrap()
1963 CheckRunVimInTerminal
1964
1965 let lines =<< trim END
1966 vim9script
1967 set linebreak
1968 setline(1, 'asdf '->repeat(15))
1969 prop_type_add('test', {highlight: 'Special'})
1970 prop_add(1, 43, {text: 'some virtual text', type: 'test'})
1971 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001972 call writefile(lines, 'XscriptPropWithWrap', 'D')
Bram Moolenaar1d8844a2022-08-10 13:39:35 +01001973 let buf = RunVimInTerminal('-S XscriptPropWithWrap', #{rows: 6, cols: 50})
1974 call VerifyScreenDump(buf, 'Test_prop_with_wrap_1', {})
1975
1976 call StopVimInTerminal(buf)
Bram Moolenaar1d8844a2022-08-10 13:39:35 +01001977endfunc
1978
Bram Moolenaar42eba042021-11-30 20:22:49 +00001979func Test_prop_after_tab()
1980 CheckRunVimInTerminal
1981
1982 let lines =<< trim END
1983 set breakindent linebreak breakat+=]
1984 call setline(1, "\t[xxx]")
1985 call prop_type_add('test', #{highlight: 'ErrorMsg'})
1986 call prop_add(1, 2, #{length: 1, type: 'test'})
1987 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01001988 call writefile(lines, 'XscriptPropAfterTab', 'D')
Bram Moolenaar42eba042021-11-30 20:22:49 +00001989 let buf = RunVimInTerminal('-S XscriptPropAfterTab', #{rows: 10})
Bram Moolenaar42eba042021-11-30 20:22:49 +00001990 call VerifyScreenDump(buf, 'Test_prop_after_tab', {})
1991
1992 call StopVimInTerminal(buf)
Bram Moolenaar42eba042021-11-30 20:22:49 +00001993endfunc
1994
Bram Moolenaare428fa02022-08-09 16:55:41 +01001995func Test_prop_before_tab()
1996 CheckRunVimInTerminal
1997
1998 let lines =<< trim END
1999 call setline(1, ["\tx"]->repeat(6))
2000 call prop_type_add('test', #{highlight: 'Search'})
2001 call prop_add(1, 1, #{type: 'test', text: '123'})
2002 call prop_add(2, 1, #{type: 'test', text: '1234567'})
2003 call prop_add(3, 1, #{type: 'test', text: '12345678'})
2004 call prop_add(4, 1, #{type: 'test', text: '123456789'})
2005 call prop_add(5, 2, #{type: 'test', text: 'ABC'})
2006 call prop_add(6, 3, #{type: 'test', text: 'ABC'})
2007 normal gg0
2008 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002009 call writefile(lines, 'XscriptPropBeforeTab', 'D')
Bram Moolenaare428fa02022-08-09 16:55:41 +01002010 let buf = RunVimInTerminal('-S XscriptPropBeforeTab', #{rows: 8})
2011 call VerifyScreenDump(buf, 'Test_prop_before_tab_01', {})
2012 call term_sendkeys(buf, "$")
2013 call VerifyScreenDump(buf, 'Test_prop_before_tab_02', {})
2014 call term_sendkeys(buf, "j0")
2015 call VerifyScreenDump(buf, 'Test_prop_before_tab_03', {})
2016 call term_sendkeys(buf, "$")
2017 call VerifyScreenDump(buf, 'Test_prop_before_tab_04', {})
2018 call term_sendkeys(buf, "j0")
2019 call VerifyScreenDump(buf, 'Test_prop_before_tab_05', {})
2020 call term_sendkeys(buf, "$")
2021 call VerifyScreenDump(buf, 'Test_prop_before_tab_06', {})
2022 call term_sendkeys(buf, "j0")
2023 call VerifyScreenDump(buf, 'Test_prop_before_tab_07', {})
2024 call term_sendkeys(buf, "$")
2025 call VerifyScreenDump(buf, 'Test_prop_before_tab_08', {})
2026 call term_sendkeys(buf, "j")
2027 call VerifyScreenDump(buf, 'Test_prop_before_tab_09', {})
2028 call term_sendkeys(buf, "j")
2029 call VerifyScreenDump(buf, 'Test_prop_before_tab_10', {})
2030
2031 call StopVimInTerminal(buf)
Bram Moolenaare428fa02022-08-09 16:55:41 +01002032endfunc
2033
Bram Moolenaaracdc9112021-12-02 19:46:57 +00002034func Test_prop_after_linebreak()
2035 CheckRunVimInTerminal
2036
2037 let lines =<< trim END
2038 set linebreak wrap
2039 call printf('%s+(%s)', 'x'->repeat(&columns / 2), 'x'->repeat(&columns / 2))->setline(1)
2040 call prop_type_add('test', #{highlight: 'ErrorMsg'})
2041 call prop_add(1, (&columns / 2) + 2, #{length: 1, type: 'test'})
2042 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002043 call writefile(lines, 'XscriptPropAfterLinebreak', 'D')
Bram Moolenaaracdc9112021-12-02 19:46:57 +00002044 let buf = RunVimInTerminal('-S XscriptPropAfterLinebreak', #{rows: 10})
Bram Moolenaaracdc9112021-12-02 19:46:57 +00002045 call VerifyScreenDump(buf, 'Test_prop_after_linebreak', {})
2046
2047 call StopVimInTerminal(buf)
Bram Moolenaaracdc9112021-12-02 19:46:57 +00002048endfunc
2049
Martin Tournoije2390c72021-07-28 13:30:16 +02002050" Buffer number of 0 should be ignored, as if the parameter wasn't passed.
2051def Test_prop_bufnr_zero()
2052 new
2053 try
2054 var bufnr = bufnr('')
2055 setline(1, 'hello')
2056 prop_type_add('bufnr-global', {highlight: 'ErrorMsg'})
2057 prop_type_add('bufnr-buffer', {highlight: 'StatusLine', bufnr: bufnr})
2058
2059 prop_add(1, 1, {type: 'bufnr-global', length: 1})
2060 prop_add(1, 2, {type: 'bufnr-buffer', length: 1})
2061
2062 var list = prop_list(1)
2063 assert_equal([
2064 {id: 0, col: 1, type_bufnr: 0, end: 1, type: 'bufnr-global', length: 1, start: 1},
2065 {id: 0, col: 2, type_bufnr: bufnr, end: 1, type: 'bufnr-buffer', length: 1, start: 1},
2066 ], list)
2067
2068 assert_equal(
2069 {highlight: 'ErrorMsg', end_incl: 0, start_incl: 0, priority: 0, combine: 1},
2070 prop_type_get('bufnr-global', {bufnr: list[0].type_bufnr}))
2071
2072 assert_equal(
2073 {highlight: 'StatusLine', end_incl: 0, start_incl: 0, priority: 0, bufnr: bufnr, combine: 1},
2074 prop_type_get('bufnr-buffer', {bufnr: list[1].type_bufnr}))
2075 finally
2076 bwipe!
2077 prop_type_delete('bufnr-global')
2078 endtry
2079enddef
2080
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002081" Tests for the prop_list() function
2082func Test_prop_list()
2083 let lines =<< trim END
2084 new
Bram Moolenaar62aec932022-01-29 21:45:34 +00002085 call g:AddPropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002086 call setline(1, repeat([repeat('a', 60)], 10))
2087 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
2088 call prop_add(1, 5, {'type': 'two', 'id': 10, 'end_col': 7})
2089 call prop_add(3, 12, {'type': 'one', 'id': 20, 'end_col': 14})
2090 call prop_add(3, 13, {'type': 'two', 'id': 10, 'end_col': 15})
2091 call prop_add(5, 20, {'type': 'one', 'id': 10, 'end_col': 22})
2092 call prop_add(5, 21, {'type': 'two', 'id': 20, 'end_col': 23})
2093 call assert_equal([
2094 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2095 \ 'type': 'one', 'length': 2, 'start': 1},
2096 \ {'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
2097 \ 'type': 'two', 'length': 2, 'start': 1}], prop_list(1))
2098 #" text properties between a few lines
2099 call assert_equal([
2100 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2101 \ 'type': 'one', 'length': 2, 'start': 1},
2102 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
2103 \ 'type': 'two', 'length': 2, 'start': 1},
2104 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
2105 \ 'type': 'one', 'length': 2, 'start': 1},
2106 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
2107 \ 'type': 'two', 'length': 2, 'start': 1}],
2108 \ prop_list(2, {'end_lnum': 5}))
2109 #" text properties across all the lines
2110 call assert_equal([
2111 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2112 \ 'type': 'one', 'length': 2, 'start': 1},
2113 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2114 \ 'type': 'one', 'length': 2, 'start': 1},
2115 \ {'lnum': 5, 'id': 10, 'col': 20, 'type_bufnr': 0, 'end': 1,
2116 \ 'type': 'one', 'length': 2, 'start': 1}],
2117 \ prop_list(1, {'types': ['one'], 'end_lnum': -1}))
2118 #" text properties with the specified identifier
2119 call assert_equal([
2120 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2121 \ 'type': 'one', 'length': 2, 'start': 1},
2122 \ {'lnum': 5, 'id': 20, 'col': 21, 'type_bufnr': 0, 'end': 1,
2123 \ 'type': 'two', 'length': 2, 'start': 1}],
2124 \ prop_list(1, {'ids': [20], 'end_lnum': 10}))
2125 #" text properties of the specified type and id
2126 call assert_equal([
2127 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
2128 \ 'type': 'two', 'length': 2, 'start': 1},
2129 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
2130 \ 'type': 'two', 'length': 2, 'start': 1}],
2131 \ prop_list(1, {'types': ['two'], 'ids': [10], 'end_lnum': 20}))
2132 call assert_equal([], prop_list(1, {'ids': [40, 50], 'end_lnum': 10}))
2133 call assert_equal([], prop_list(6, {'end_lnum': 10}))
2134 call assert_equal([], prop_list(2, {'end_lnum': 2}))
2135 #" error cases
2136 call assert_fails("echo prop_list(1, {'end_lnum': -20})", 'E16:')
2137 call assert_fails("echo prop_list(4, {'end_lnum': 2})", 'E16:')
2138 call assert_fails("echo prop_list(1, {'end_lnum': '$'})", 'E889:')
2139 call assert_fails("echo prop_list(1, {'types': ['blue'], 'end_lnum': 10})",
2140 \ 'E971:')
2141 call assert_fails("echo prop_list(1, {'types': ['one', 'blue'],
2142 \ 'end_lnum': 10})", 'E971:')
2143 call assert_fails("echo prop_list(1, {'types': ['one', 10],
2144 \ 'end_lnum': 10})", 'E928:')
2145 call assert_fails("echo prop_list(1, {'types': ['']})", 'E971:')
2146 call assert_equal([], prop_list(2, {'types': []}))
2147 call assert_equal([], prop_list(2, {'types': test_null_list()}))
2148 call assert_fails("call prop_list(1, {'types': {}})", 'E714:')
2149 call assert_fails("call prop_list(1, {'types': 'one'})", 'E714:')
2150 call assert_equal([], prop_list(2, {'types': ['one'],
2151 \ 'ids': test_null_list()}))
2152 call assert_equal([], prop_list(2, {'types': ['one'], 'ids': []}))
2153 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': {}})",
2154 \ 'E714:')
2155 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': 10})",
2156 \ 'E714:')
2157 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [[]]})",
2158 \ 'E745:')
2159 call assert_fails("call prop_list(1, {'types': ['one'], 'ids': [10, []]})",
2160 \ 'E745:')
Martin Tournoije2390c72021-07-28 13:30:16 +02002161
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002162 #" get text properties from a non-current buffer
2163 wincmd w
2164 call assert_equal([
2165 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2166 \ 'type': 'one', 'length': 2, 'start': 1},
2167 \ {'lnum': 1, 'id': 10, 'col': 5, 'type_bufnr': 0, 'end': 1,
2168 \ 'type': 'two', 'length': 2, 'start': 1},
2169 \ {'lnum': 3, 'id': 20, 'col': 12, 'type_bufnr': 0, 'end': 1,
2170 \ 'type': 'one', 'length': 2, 'start': 1},
2171 \ {'lnum': 3, 'id': 10, 'col': 13, 'type_bufnr': 0, 'end': 1,
2172 \ 'type': 'two', 'length': 2, 'start': 1}],
2173 \ prop_list(1, {'bufnr': winbufnr(1), 'end_lnum': 4}))
2174 wincmd w
2175
2176 #" get text properties after clearing all the properties
2177 call prop_clear(1, line('$'))
2178 call assert_equal([], prop_list(1, {'end_lnum': 10}))
2179
2180 call prop_add(2, 4, {'type': 'one', 'id': 5, 'end_col': 6})
2181 call prop_add(2, 4, {'type': 'two', 'id': 10, 'end_col': 6})
2182 call prop_add(2, 4, {'type': 'three', 'id': 15, 'end_col': 6})
2183 #" get text properties with a list of types
2184 call assert_equal([
2185 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
2186 \ 'type': 'two', 'length': 2, 'start': 1},
2187 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2188 \ 'type': 'one', 'length': 2, 'start': 1}],
2189 \ prop_list(2, {'types': ['one', 'two']}))
2190 call assert_equal([
2191 \ {'id': 15, 'col': 4, 'type_bufnr': 0, 'end': 1,
2192 \ 'type': 'three', 'length': 2, 'start': 1},
2193 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2194 \ 'type': 'one', 'length': 2, 'start': 1}],
2195 \ prop_list(2, {'types': ['one', 'three']}))
2196 #" get text properties with a list of identifiers
2197 call assert_equal([
2198 \ {'id': 10, 'col': 4, 'type_bufnr': 0, 'end': 1,
2199 \ 'type': 'two', 'length': 2, 'start': 1},
2200 \ {'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2201 \ 'type': 'one', 'length': 2, 'start': 1}],
2202 \ prop_list(2, {'ids': [5, 10, 20]}))
2203 call prop_clear(1, line('$'))
2204 call assert_equal([], prop_list(2, {'types': ['one', 'two']}))
2205 call assert_equal([], prop_list(2, {'ids': [5, 10, 20]}))
2206
2207 #" get text properties from a hidden buffer
2208 edit! Xaaa
2209 call setline(1, repeat([repeat('b', 60)], 10))
2210 call prop_add(1, 4, {'type': 'one', 'id': 5, 'end_col': 6})
2211 call prop_add(4, 8, {'type': 'two', 'id': 10, 'end_col': 10})
2212 VAR bnr = bufnr()
2213 hide edit Xbbb
2214 call assert_equal([
2215 \ {'lnum': 1, 'id': 5, 'col': 4, 'type_bufnr': 0, 'end': 1,
2216 \ 'type': 'one', 'length': 2, 'start': 1},
2217 \ {'lnum': 4, 'id': 10, 'col': 8, 'type_bufnr': 0, 'end': 1,
2218 \ 'type': 'two', 'length': 2, 'start': 1}],
2219 \ prop_list(1, {'bufnr': bnr,
2220 \ 'types': ['one', 'two'], 'ids': [5, 10], 'end_lnum': -1}))
2221 #" get text properties from an unloaded buffer
2222 bunload! Xaaa
2223 call assert_equal([], prop_list(1, {'bufnr': bnr, 'end_lnum': -1}))
2224
Bram Moolenaar62aec932022-01-29 21:45:34 +00002225 call g:DeletePropTypes()
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002226 :%bw!
2227 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002228 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanane0216622021-11-23 11:46:32 +00002229endfunc
Bram Moolenaar23999d72020-12-23 14:36:00 +01002230
LemonBoy9bd3ce22022-04-18 21:54:02 +01002231func Test_prop_find_prev_on_same_line()
2232 new
2233
2234 call setline(1, 'the quikc bronw fox jumsp over the layz dog')
2235 call prop_type_add('misspell', #{highlight: 'ErrorMsg'})
2236 for col in [8, 14, 24, 38]
2237 call prop_add(1, col, #{type: 'misspell', length: 2})
2238 endfor
2239
Ben Jacksona7704222022-08-20 20:54:51 +01002240 call cursor(1, 18)
LemonBoy9bd3ce22022-04-18 21:54:02 +01002241 let expected = [
2242 \ #{lnum: 1, id: 0, col: 14, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1},
2243 \ #{lnum: 1, id: 0, col: 24, end: 1, type: 'misspell', type_bufnr: 0, length: 2, start: 1}
2244 \ ]
2245
2246 let result = prop_find(#{type: 'misspell'}, 'b')
2247 call assert_equal(expected[0], result)
2248 let result = prop_find(#{type: 'misspell'}, 'f')
2249 call assert_equal(expected[1], result)
2250
2251 call prop_type_delete('misspell')
2252 bwipe!
2253endfunc
2254
LemonBoyb7a70122022-05-13 12:41:50 +01002255func Test_prop_spell()
2256 new
2257 set spell
2258 call AddPropTypes()
2259
2260 call setline(1, ["helo world", "helo helo helo"])
2261 call prop_add(1, 1, #{type: 'one', length: 4})
2262 call prop_add(1, 6, #{type: 'two', length: 5})
2263 call prop_add(2, 1, #{type: 'three', length: 4})
2264 call prop_add(2, 6, #{type: 'three', length: 4})
2265 call prop_add(2, 11, #{type: 'three', length: 4})
2266
2267 " The first prop over 'helo' increases its length after the word is corrected
2268 " to 'Hello', the second one is shifted to the right.
2269 let expected = [
2270 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2271 \ 'length': 5, 'start': 1},
2272 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2273 \ 'length': 5, 'start': 1}
2274 \ ]
2275 call feedkeys("z=1\<CR>", 'xt')
2276
2277 call assert_equal('Hello world', getline(1))
2278 call assert_equal(expected, prop_list(1))
2279
2280 " Repeat the replacement done by z=
2281 spellrepall
2282
2283 let expected = [
2284 \ {'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2285 \ 'length': 5, 'start': 1},
2286 \ {'id': 0, 'col': 7, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2287 \ 'length': 5, 'start': 1},
2288 \ {'id': 0, 'col': 13, 'type_bufnr': 0, 'end': 1, 'type': 'three',
2289 \ 'length': 5, 'start': 1}
2290 \ ]
2291 call assert_equal('Hello Hello Hello', getline(2))
2292 call assert_equal(expected, prop_list(2))
2293
2294 call DeletePropTypes()
2295 set spell&
2296 bwipe!
2297endfunc
2298
LemonBoy4b936742022-05-13 21:56:28 +01002299func Test_prop_shift_block()
2300 new
2301 call AddPropTypes()
2302
2303 call setline(1, ['some highlighted text']->repeat(2))
2304 call prop_add(1, 10, #{type: 'one', length: 11})
2305 call prop_add(2, 10, #{type: 'two', length: 11})
2306
2307 call cursor(1, 1)
2308 call feedkeys("5l\<c-v>>", 'nxt')
2309 call cursor(2, 1)
2310 call feedkeys("5l\<c-v><", 'nxt')
2311
2312 let expected = [
2313 \ {'lnum': 1, 'id': 0, 'col': 8, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2314 \ 'length': 11, 'start' : 1},
2315 \ {'lnum': 2, 'id': 0, 'col': 6, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2316 \ 'length': 11, 'start' : 1}
2317 \ ]
2318 call assert_equal(expected, prop_list(1, #{end_lnum: 2}))
2319
2320 call DeletePropTypes()
2321 bwipe!
2322endfunc
LemonBoyb7a70122022-05-13 12:41:50 +01002323
LemonBoy698cb4c2022-05-14 18:10:15 +01002324func Test_prop_insert_multiline()
2325 new
2326 call AddPropTypes()
2327
2328 call setline(1, ['foobar', 'barbaz'])
2329 call prop_add(1, 4, #{end_lnum: 2, end_col: 4, type: 'one'})
2330
2331 call feedkeys("1Goquxqux\<Esc>", 'nxt')
2332 call feedkeys("2GOquxqux\<Esc>", 'nxt')
2333
2334 let lines =<< trim END
2335 foobar
2336 quxqux
2337 quxqux
2338 barbaz
2339 END
2340 call assert_equal(lines, getline(1, '$'))
2341 let expected = [
2342 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 0, 'type': 'one',
Ben Jacksona7704222022-08-20 20:54:51 +01002343 \ 'length': 4 , 'start': 1},
LemonBoy698cb4c2022-05-14 18:10:15 +01002344 \ {'lnum': 2, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2345 \ 'length': 7, 'start': 0},
2346 \ {'lnum': 3, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 0, 'type': 'one',
2347 \ 'length': 7, 'start': 0},
2348 \ {'lnum': 4, 'id': 0, 'col': 1, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2349 \ 'length': 3, 'start': 0}
2350 \ ]
2351 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
2352
2353 call DeletePropTypes()
2354 bwipe!
2355endfunc
2356
LemonBoyb559b302022-05-15 13:08:02 +01002357func Test_prop_blockwise_change()
2358 new
2359 call AddPropTypes()
2360
2361 call setline(1, ['foooooo', 'bar', 'baaaaz'])
2362 call prop_add(1, 1, #{end_col: 3, type: 'one'})
2363 call prop_add(2, 1, #{end_col: 3, type: 'two'})
2364 call prop_add(3, 1, #{end_col: 3, type: 'three'})
2365
2366 " Replace the first two columns with '123', since 'start_incl' is false the
2367 " prop is not extended.
2368 call feedkeys("gg\<c-v>2jc123\<Esc>", 'nxt')
2369
2370 let lines =<< trim END
2371 123oooooo
2372 123ar
2373 123aaaaz
2374 END
2375 call assert_equal(lines, getline(1, '$'))
2376 let expected = [
2377 \ {'lnum': 1, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1, 'type': 'one',
2378 \ 'length': 1, 'start': 1},
2379 \ {'lnum': 2, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1, 'type': 'two',
2380 \ 'length': 1, 'start': 1},
2381 \ {'lnum': 3, 'id': 0, 'col': 4, 'type_bufnr': 0, 'end': 1 ,
2382 \ 'type': 'three', 'length': 1, 'start': 1}
2383 \ ]
2384 call assert_equal(expected, prop_list(1, #{end_lnum: 10}))
2385
2386 call DeletePropTypes()
2387 bwipe!
2388endfunc
2389
Paul Ollis4c3d21a2022-05-24 21:26:37 +01002390func Do_test_props_do_not_affect_byte_offsets(ff, increment)
2391 new
2392 let lcount = 410
2393
2394 " File format affects byte-offset calculations, so make sure it is known.
2395 exec 'setlocal fileformat=' . a:ff
2396
2397 " Fill the buffer with varying length lines. We need a suitably large number
2398 " to force Vim code through paths wehere previous error have occurred. This
2399 " is more 'art' than 'science'.
2400 let text = 'a'
2401 call setline(1, text)
2402 let offsets = [1]
2403 for idx in range(lcount)
2404 call add(offsets, offsets[idx] + len(text) + a:increment)
2405 if (idx % 6) == 0
2406 let text = text . 'a'
2407 endif
2408 call append(line('$'), text)
2409 endfor
2410
2411 " Set a property that spans a few lines to cause Vim's internal buffer code
2412 " to perform a reasonable amount of rearrangement.
2413 call prop_type_add('one', {'highlight': 'ErrorMsg'})
2414 call prop_add(1, 1, {'type': 'one', 'end_lnum': 6, 'end_col': 2})
2415
2416 for idx in range(lcount)
2417 let boff = line2byte(idx + 1)
2418 call assert_equal(offsets[idx], boff, 'Bad byte offset at line ' . (idx + 1))
2419 endfor
2420
2421 call prop_type_delete('one')
2422 bwipe!
2423endfunc
2424
2425func Test_props_do_not_affect_byte_offsets()
2426 call Do_test_props_do_not_affect_byte_offsets('unix', 1)
2427endfunc
2428
2429func Test_props_do_not_affect_byte_offsets_dos()
2430 call Do_test_props_do_not_affect_byte_offsets('dos', 2)
2431endfunc
2432
2433func Test_props_do_not_affect_byte_offsets_editline()
2434 new
2435 let lcount = 410
2436
2437 " File format affects byte-offset calculations, so make sure it is known.
2438 setlocal fileformat=unix
2439
2440 " Fill the buffer with varying length lines. We need a suitably large number
2441 " to force Vim code through paths wehere previous error have occurred. This
2442 " is more 'art' than 'science'.
2443 let text = 'aa'
2444 call setline(1, text)
2445 let offsets = [1]
2446 for idx in range(lcount)
2447 call add(offsets, offsets[idx] + len(text) + 1)
2448 if (idx % 6) == 0
2449 let text = text . 'a'
2450 endif
2451 call append(line('$'), text)
2452 endfor
2453
2454 " Set a property that just covers the first line. When this test was
2455 " developed, this did not trigger a byte-offset error.
2456 call prop_type_add('one', {'highlight': 'ErrorMsg'})
2457 call prop_add(1, 1, {'type': 'one', 'end_lnum': 1, 'end_col': 3})
2458
2459 for idx in range(lcount)
2460 let boff = line2byte(idx + 1)
2461 call assert_equal(offsets[idx], boff,
2462 \ 'Confounding bad byte offset at line ' . (idx + 1))
2463 endfor
2464
2465 " Insert text in the middle of the first line, keeping the property
2466 " unchanged.
2467 :1
2468 normal aHello
2469 for idx in range(1, lcount)
2470 let offsets[idx] = offsets[idx] + 5
2471 endfor
2472
2473 for idx in range(lcount)
2474 let boff = line2byte(idx + 1)
2475 call assert_equal(offsets[idx], boff,
2476 \ 'Bad byte offset at line ' . (idx + 1))
2477 endfor
2478
2479 call prop_type_delete('one')
2480 bwipe!
2481endfunc
2482
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002483func Test_prop_inserts_text()
2484 CheckRunVimInTerminal
2485
2486 " Just a basic check for now
2487 let lines =<< trim END
2488 call setline(1, 'insert some text here and other text there and some more text after wrapping')
2489 call prop_type_add('someprop', #{highlight: 'ErrorMsg'})
2490 call prop_type_add('otherprop', #{highlight: 'Search'})
2491 call prop_type_add('moreprop', #{highlight: 'DiffAdd'})
2492 call prop_add(1, 18, #{type: 'someprop', text: 'SOME '})
Bram Moolenaar783ef722022-08-01 16:11:06 +01002493 call prop_add(1, 38, #{type: 'otherprop', text: "OTHER\t"})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002494 call prop_add(1, 69, #{type: 'moreprop', text: 'MORE '})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002495 normal $
Bram Moolenaar09ff4b52022-08-01 16:51:02 +01002496
2497 call setline(2, 'prepost')
2498 call prop_type_add('multibyte', #{highlight: 'Visual'})
2499 call prop_add(2, 4, #{type: 'multibyte', text: 'söme和平téxt'})
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002500
Bram Moolenaar25463612022-08-08 11:07:47 +01002501 call setline(3, 'Foo foo = { 1, 2 };')
Bram Moolenaar3331dd02022-08-10 16:49:02 +01002502 call prop_type_add('testprop', #{highlight: 'Comment'})
Bram Moolenaar25463612022-08-08 11:07:47 +01002503 call prop_add(3, 13, #{type: 'testprop', text: '.x='})
2504 call prop_add(3, 16, #{type: 'testprop', text: '.y='})
2505
2506 call setline(4, '')
2507 call prop_add(4, 1, #{type: 'someprop', text: 'empty line'})
Bram Moolenaar3331dd02022-08-10 16:49:02 +01002508
2509 call setline(5, 'look highlight')
2510 call prop_type_add('nohi', #{})
2511 call prop_add(5, 6, #{type: 'nohi', text: 'no '})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002512 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002513 call writefile(lines, 'XscriptPropsWithText', 'D')
Bram Moolenaar25463612022-08-08 11:07:47 +01002514 let buf = RunVimInTerminal('-S XscriptPropsWithText', #{rows: 8, cols: 60})
Bram Moolenaar711483c2022-07-30 21:33:46 +01002515 call VerifyScreenDump(buf, 'Test_prop_inserts_text_1', {})
2516
2517 call term_sendkeys(buf, ":set signcolumn=yes\<CR>")
2518 call VerifyScreenDump(buf, 'Test_prop_inserts_text_2', {})
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002519
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002520 call term_sendkeys(buf, "2G$")
2521 call VerifyScreenDump(buf, 'Test_prop_inserts_text_3', {})
2522
Bram Moolenaar25463612022-08-08 11:07:47 +01002523 call term_sendkeys(buf, "3Gf1")
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002524 call VerifyScreenDump(buf, 'Test_prop_inserts_text_4', {})
Bram Moolenaar25463612022-08-08 11:07:47 +01002525 call term_sendkeys(buf, "f2")
2526 call VerifyScreenDump(buf, 'Test_prop_inserts_text_5', {})
2527
2528 call term_sendkeys(buf, "4G")
2529 call VerifyScreenDump(buf, 'Test_prop_inserts_text_6', {})
Bram Moolenaarafd2aa72022-08-05 13:07:23 +01002530
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002531 call StopVimInTerminal(buf)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002532endfunc
2533
Bram Moolenaare38fc862022-08-11 17:24:50 +01002534func Test_prop_inserts_text_highlight()
2535 CheckRunVimInTerminal
2536
2537 " Just a basic check for now
2538 let lines =<< trim END
2539 call setline(1, 'insert some text (here) and there')
2540 call prop_type_add('someprop', #{highlight: 'ErrorMsg'})
2541 let bef_prop = prop_add(1, 18, #{type: 'someprop', text: 'BEFORE'})
2542 set hlsearch
2543 let thematch = matchaddpos("DiffAdd", [[1, 18]])
2544 func DoAfter()
2545 call prop_remove(#{id: g:bef_prop})
2546 call prop_add(1, 19, #{type: 'someprop', text: 'AFTER'})
2547 let g:thematch = matchaddpos("DiffAdd", [[1, 18]])
2548 let @/ = ''
2549 endfunc
2550 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002551 call writefile(lines, 'XscriptPropsWithHighlight', 'D')
Bram Moolenaare38fc862022-08-11 17:24:50 +01002552 let buf = RunVimInTerminal('-S XscriptPropsWithHighlight', #{rows: 6, cols: 60})
2553 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_1', {})
2554 call term_sendkeys(buf, "/text (he\<CR>")
2555 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_2', {})
2556 call term_sendkeys(buf, ":call matchdelete(thematch)\<CR>")
2557 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_3', {})
2558
2559 call term_sendkeys(buf, ":call DoAfter()\<CR>")
2560 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_4', {})
2561 call term_sendkeys(buf, "/text (he\<CR>")
2562 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_5', {})
2563 call term_sendkeys(buf, ":call matchdelete(thematch)\<CR>")
2564 call VerifyScreenDump(buf, 'Test_prop_inserts_text_hi_6', {})
2565
2566 call StopVimInTerminal(buf)
Bram Moolenaare38fc862022-08-11 17:24:50 +01002567endfunc
2568
Bram Moolenaarfb593c52022-09-17 18:57:36 +01002569func Test_prop_add_with_text_fails()
2570 call prop_type_add('failing', #{highlight: 'ErrorMsg'})
2571 call assert_fails("call prop_add(1, 0, #{type: 'failing', text: 'X', end_lnum: 1})", 'E1305:')
2572 call assert_fails("call prop_add(1, 0, #{type: 'failing', text: 'X', end_col: 1})", 'E1305:')
2573 call assert_fails("call prop_add(1, 0, #{type: 'failing', text: 'X', length: 1})", 'E1305:')
2574
2575 call prop_type_delete('failing')
2576endfunc
2577
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002578func Test_props_with_text_right_align_twice()
2579 CheckRunVimInTerminal
2580
2581 let lines =<< trim END
2582 call setline(1, ["some text some text some text some text", 'line two'])
Bram Moolenaarfb593c52022-09-17 18:57:36 +01002583 call prop_type_add('MyErrorText', #{highlight: 'ErrorMsg'})
2584 call prop_type_add('MyPadding', #{highlight: 'DiffChange'})
Bram Moolenaarc8bf59e2022-08-28 16:39:22 +01002585 call prop_add(1, 0, #{type: 'MyPadding', text: ' nothing here', text_wrap: 'wrap'})
2586 call prop_add(1, 0, #{type: 'MyErrorText', text: 'Some error', text_wrap: 'wrap', text_align: 'right'})
2587 call prop_add(1, 0, #{type: 'MyErrorText', text: 'Another error', text_wrap: 'wrap', text_align: 'right'})
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002588 normal G$
2589 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002590 call writefile(lines, 'XscriptPropsRightAlign', 'D')
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002591 let buf = RunVimInTerminal('-S XscriptPropsRightAlign', #{rows: 8})
2592 call VerifyScreenDump(buf, 'Test_prop_right_align_twice_1', {})
2593
2594 call term_sendkeys(buf, "ggisome more text\<Esc>G$")
2595 call VerifyScreenDump(buf, 'Test_prop_right_align_twice_2', {})
2596
Bram Moolenaarc8bf59e2022-08-28 16:39:22 +01002597 call term_sendkeys(buf, ":set signcolumn=yes\<CR>")
2598 call VerifyScreenDump(buf, 'Test_prop_right_align_twice_3', {})
2599
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002600 call StopVimInTerminal(buf)
Bram Moolenaarf0ccfa42022-08-13 16:41:19 +01002601endfunc
2602
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002603func Test_props_with_text_after()
2604 CheckRunVimInTerminal
2605
2606 let lines =<< trim END
Bram Moolenaar3ec3b8e2022-08-05 21:39:30 +01002607 set showbreak=+++
Bram Moolenaar73c38422022-08-07 11:53:40 +01002608 set breakindent
2609 call setline(1, ' some text here and other text there')
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002610 call prop_type_add('rightprop', #{highlight: 'ErrorMsg'})
2611 call prop_type_add('afterprop', #{highlight: 'Search'})
2612 call prop_type_add('belowprop', #{highlight: 'DiffAdd'})
2613 call prop_add(1, 0, #{type: 'rightprop', text: ' RIGHT ', text_align: 'right'})
Bram Moolenaar783ef722022-08-01 16:11:06 +01002614 call prop_add(1, 0, #{type: 'afterprop', text: "\tAFTER\t", text_align: 'after'})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002615 call prop_add(1, 0, #{type: 'belowprop', text: ' BELOW ', text_align: 'below'})
Bram Moolenaar50e75fe2022-08-05 20:25:50 +01002616 call prop_add(1, 0, #{type: 'belowprop', text: ' ALSO BELOW ', text_align: 'below'})
Bram Moolenaar84b247f2022-08-01 11:17:40 +01002617
2618 call setline(2, 'Last line.')
2619 call prop_add(2, 0, #{type: 'afterprop', text: ' After Last ', text_align: 'after'})
2620 normal G$
Bram Moolenaar09ff4b52022-08-01 16:51:02 +01002621
2622 call setline(3, 'right here')
2623 call prop_add(3, 0, #{type: 'rightprop', text: 'söme和平téxt', text_align: 'right'})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002624 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002625 call writefile(lines, 'XscriptPropsWithTextAfter', 'D')
Bram Moolenaar50e75fe2022-08-05 20:25:50 +01002626 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfter', #{rows: 8, cols: 60})
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002627 call VerifyScreenDump(buf, 'Test_prop_with_text_after_1', {})
2628
2629 call StopVimInTerminal(buf)
Bram Moolenaar82b14c12022-08-10 19:50:47 +01002630
2631 call assert_fails('call prop_add(1, 2, #{text: "yes", text_align: "right", type: "some"})', 'E1294:')
Bram Moolenaarb7963df2022-07-31 17:12:43 +01002632endfunc
2633
Bram Moolenaar877151b2022-10-11 15:29:50 +01002634func Test_props_with_text_after_and_list()
2635 CheckRunVimInTerminal
2636
2637 let lines =<< trim END
2638 vim9script
2639 setline(1, ['one', 'two'])
2640 prop_type_add('test', {highlight: 'Special'})
2641 prop_add(1, 0, {
2642 type: 'test',
2643 text: range(50)->join(' '),
2644 text_align: 'after',
2645 text_padding_left: 3
2646 })
2647 prop_add(1, 0, {
2648 type: 'test',
2649 text: range(50)->join('-'),
2650 text_align: 'after',
2651 text_padding_left: 5
2652 })
2653 prop_add(1, 0, {
2654 type: 'test',
2655 text: range(50)->join('.'),
2656 text_align: 'after',
2657 text_padding_left: 1
2658 })
2659 normal G$
2660 END
2661 call writefile(lines, 'XscriptPropsAfter', 'D')
2662 let buf = RunVimInTerminal('-S XscriptPropsAfter', #{rows: 8, cols: 60})
2663 call VerifyScreenDump(buf, 'Test_props_after_1', {})
2664
2665 call term_sendkeys(buf, ":set list\<CR>")
2666 call VerifyScreenDump(buf, 'Test_props_after_2', {})
2667
2668 call StopVimInTerminal(buf)
2669endfunc
2670
Bram Moolenaarcba69522022-08-06 21:03:53 +01002671func Test_props_with_text_after_below_trunc()
2672 CheckRunVimInTerminal
2673
2674 let lines =<< trim END
2675 vim9script
2676 edit foobar
2677 set showbreak=+++
2678 setline(1, ['onasdf asdf asdf asdf asd fas df', 'two'])
2679 prop_type_add('test', {highlight: 'Special'})
2680 prop_add(1, 0, {
2681 type: 'test',
2682 text: 'the quick brown fox jumps over the lazy dog',
2683 text_align: 'after'
2684 })
2685 prop_add(1, 0, {
2686 type: 'test',
2687 text: 'the quick brown fox jumps over the lazy dog',
2688 text_align: 'below'
2689 })
2690 normal G$
2691 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002692 call writefile(lines, 'XscriptPropsAfterTrunc', 'D')
Bram Moolenaarcba69522022-08-06 21:03:53 +01002693 let buf = RunVimInTerminal('-S XscriptPropsAfterTrunc', #{rows: 8, cols: 60})
2694 call VerifyScreenDump(buf, 'Test_prop_with_text_after_below_trunc_1', {})
2695
Bram Moolenaarccf28372022-10-10 21:10:03 +01002696 call term_sendkeys(buf, ":set number\<CR>")
2697 call VerifyScreenDump(buf, 'Test_prop_with_text_after_below_trunc_2', {})
2698
Bram Moolenaarcba69522022-08-06 21:03:53 +01002699 call StopVimInTerminal(buf)
Bram Moolenaarcba69522022-08-06 21:03:53 +01002700endfunc
2701
Bram Moolenaare175dc62022-08-01 22:18:50 +01002702func Test_props_with_text_after_joined()
2703 CheckRunVimInTerminal
2704
2705 let lines =<< trim END
2706 call setline(1, ['one', 'two', 'three', 'four'])
2707 call prop_type_add('afterprop', #{highlight: 'Search'})
2708 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE', text_align: 'after'})
2709 call prop_add(4, 0, #{type: 'afterprop', text: ' FOUR', text_align: 'after'})
2710 normal ggJ
2711 normal GkJ
2712
2713 call setline(3, ['a', 'b', 'c', 'd', 'e', 'f'])
2714 call prop_add(3, 0, #{type: 'afterprop', text: ' AAA', text_align: 'after'})
2715 call prop_add(5, 0, #{type: 'afterprop', text: ' CCC', text_align: 'after'})
2716 call prop_add(7, 0, #{type: 'afterprop', text: ' EEE', text_align: 'after'})
2717 call prop_add(8, 0, #{type: 'afterprop', text: ' FFF', text_align: 'after'})
2718 normal 3G6J
2719 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002720 call writefile(lines, 'XscriptPropsWithTextAfterJoined', 'D')
Bram Moolenaare175dc62022-08-01 22:18:50 +01002721 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterJoined', #{rows: 6, cols: 60})
2722 call VerifyScreenDump(buf, 'Test_prop_with_text_after_joined_1', {})
2723
2724 call StopVimInTerminal(buf)
Bram Moolenaare175dc62022-08-01 22:18:50 +01002725endfunc
2726
Bram Moolenaar398649e2022-08-04 15:03:48 +01002727func Test_props_with_text_after_truncated()
2728 CheckRunVimInTerminal
2729
2730 let lines =<< trim END
2731 call setline(1, ['one two three four five six seven'])
2732 call prop_type_add('afterprop', #{highlight: 'Search'})
2733 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE'})
2734
2735 call setline(2, ['one two three four five six seven'])
2736 call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right'})
2737
2738 call setline(3, ['one two three four five six seven'])
2739 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'})
2740
2741 call setline(4, ['cursor here'])
2742 normal 4Gfh
2743 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002744 call writefile(lines, 'XscriptPropsWithTextAfterTrunc', 'D')
Bram Moolenaar398649e2022-08-04 15:03:48 +01002745 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterTrunc', #{rows: 9, cols: 60})
2746 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_1', {})
2747
2748 call term_sendkeys(buf, ":37vsp\<CR>gg")
2749 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_2', {})
2750
2751 call term_sendkeys(buf, ":36wincmd |\<CR>")
2752 call term_sendkeys(buf, "2G$")
2753 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_3', {})
2754
2755 call term_sendkeys(buf, ":33wincmd |\<CR>")
2756 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_4', {})
2757
2758 call term_sendkeys(buf, ":18wincmd |\<CR>")
2759 call term_sendkeys(buf, "0fx")
2760 call VerifyScreenDump(buf, 'Test_prop_with_text_after_trunc_5', {})
2761
2762 call StopVimInTerminal(buf)
Bram Moolenaar398649e2022-08-04 15:03:48 +01002763endfunc
2764
Bram Moolenaar49a90792022-08-09 18:25:23 +01002765func Test_props_with_text_empty_line()
2766 CheckRunVimInTerminal
2767
2768 let lines =<< trim END
2769 call setline(1, ['', 'aaa', '', 'bbbbbb'])
2770 call prop_type_add('prop1', #{highlight: 'Search'})
2771 call prop_add(1, 1, #{type: 'prop1', text_wrap: 'wrap', text: repeat('X', &columns)})
2772 call prop_add(3, 1, #{type: 'prop1', text_wrap: 'wrap', text: repeat('X', &columns + 1)})
2773 normal gg0
2774 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002775 call writefile(lines, 'XscriptPropsWithTextEmptyLine', 'D')
Bram Moolenaar49a90792022-08-09 18:25:23 +01002776 let buf = RunVimInTerminal('-S XscriptPropsWithTextEmptyLine', #{rows: 8, cols: 60})
2777 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_1', {})
2778 call term_sendkeys(buf, "$")
2779 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_2', {})
2780 call term_sendkeys(buf, "j")
2781 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_3', {})
2782 call term_sendkeys(buf, "j")
2783 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_4', {})
2784 call term_sendkeys(buf, "j")
2785 call VerifyScreenDump(buf, 'Test_prop_with_text_empty_line_5', {})
2786
2787 call StopVimInTerminal(buf)
Bram Moolenaar49a90792022-08-09 18:25:23 +01002788endfunc
2789
Bram Moolenaar398649e2022-08-04 15:03:48 +01002790func Test_props_with_text_after_wraps()
2791 CheckRunVimInTerminal
2792
2793 let lines =<< trim END
2794 call setline(1, ['one two three four five six seven'])
2795 call prop_type_add('afterprop', #{highlight: 'Search'})
2796 call prop_add(1, 0, #{type: 'afterprop', text: ' ONE and TWO and THREE and FOUR and FIVE', text_wrap: 'wrap'})
2797
2798 call setline(2, ['one two three four five six seven'])
2799 call prop_add(2, 0, #{type: 'afterprop', text: ' one AND two AND three AND four AND five', text_align: 'right', text_wrap: 'wrap'})
2800
2801 call setline(3, ['one two three four five six seven'])
2802 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'})
2803
2804 call setline(4, ['cursor here'])
2805 normal 4Gfh
2806 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002807 call writefile(lines, 'XscriptPropsWithTextAfterWraps', 'D')
Bram Moolenaar398649e2022-08-04 15:03:48 +01002808 let buf = RunVimInTerminal('-S XscriptPropsWithTextAfterWraps', #{rows: 9, cols: 60})
2809 call VerifyScreenDump(buf, 'Test_prop_with_text_after_wraps_1', {})
2810
2811 call StopVimInTerminal(buf)
Bram Moolenaar398649e2022-08-04 15:03:48 +01002812endfunc
2813
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002814func Test_props_with_text_after_nowrap()
2815 CheckRunVimInTerminal
2816
2817 let lines =<< trim END
2818 set nowrap
Bram Moolenaar8f369fb2022-08-13 19:35:05 +01002819 call setline(1, ['one', 'two', 'three', 'four'])
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002820 call prop_type_add('belowprop', #{highlight: 'ErrorMsg'})
2821 call prop_type_add('anotherprop', #{highlight: 'Search'})
Bram Moolenaardb9b96d2022-08-06 17:38:53 +01002822 call prop_type_add('someprop', #{highlight: 'DiffChange'})
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002823 call prop_add(1, 0, #{type: 'belowprop', text: ' Below the line ', text_align: 'below'})
2824 call prop_add(2, 0, #{type: 'anotherprop', text: 'another', text_align: 'below'})
2825 call prop_add(2, 0, #{type: 'belowprop', text: 'One More Here', text_align: 'below'})
Bram Moolenaardb9b96d2022-08-06 17:38:53 +01002826 call prop_add(1, 0, #{type: 'someprop', text: 'right here', text_align: 'right'})
2827 call prop_add(1, 0, #{type: 'someprop', text: ' After the text', text_align: 'after'})
Bram Moolenaar8f369fb2022-08-13 19:35:05 +01002828 normal 3G$
2829
2830 call prop_add(3, 0, #{type: 'anotherprop', text: 'right aligned', text_align: 'right'})
2831 call prop_add(3, 0, #{type: 'anotherprop', text: 'also right aligned', text_align: 'right'})
Bram Moolenaar9113c2c2022-08-13 20:17:34 +01002832 hi CursorLine ctermbg=lightgrey
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002833 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002834 call writefile(lines, 'XscriptPropsAfterNowrap', 'D')
Bram Moolenaar8f369fb2022-08-13 19:35:05 +01002835 let buf = RunVimInTerminal('-S XscriptPropsAfterNowrap', #{rows: 12, cols: 60})
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002836 call VerifyScreenDump(buf, 'Test_prop_with_text_after_nowrap_1', {})
2837
Bram Moolenaar9113c2c2022-08-13 20:17:34 +01002838 call term_sendkeys(buf, ":set signcolumn=yes foldcolumn=3 cursorline\<CR>")
Bram Moolenaar1306b362022-08-06 15:59:06 +01002839 call VerifyScreenDump(buf, 'Test_prop_with_text_after_nowrap_2', {})
2840
Bram Moolenaar8f369fb2022-08-13 19:35:05 +01002841 call term_sendkeys(buf, "j")
2842 call VerifyScreenDump(buf, 'Test_prop_with_text_after_nowrap_3', {})
2843
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002844 call StopVimInTerminal(buf)
Bram Moolenaar4d91d342022-08-06 13:48:20 +01002845endfunc
2846
Bram Moolenaar48ca24d2022-08-06 22:03:20 +01002847func Test_props_with_text_below_nowrap()
2848 CheckRunVimInTerminal
2849
2850 let lines =<< trim END
2851 vim9script
2852 edit foobar
2853 set nowrap
2854 set showbreak=+++\
2855 setline(1, ['onasdf asdf asdf sdf df asdf asdf e asdf asdf asdf asdf asd fas df', 'two'])
2856 prop_type_add('test', {highlight: 'Special'})
2857 prop_add(1, 0, {
2858 type: 'test',
2859 text: 'the quick brown fox jumps over the lazy dog',
2860 text_align: 'after'
2861 })
2862 prop_add(1, 0, {
2863 type: 'test',
2864 text: 'the quick brown fox jumps over the lazy dog',
2865 text_align: 'below'
2866 })
2867 normal G$
2868 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002869 call writefile(lines, 'XscriptPropsBelowNowrap', 'D')
Bram Moolenaar48ca24d2022-08-06 22:03:20 +01002870 let buf = RunVimInTerminal('-S XscriptPropsBelowNowrap', #{rows: 8, cols: 60})
2871 call VerifyScreenDump(buf, 'Test_prop_with_text_below_nowrap_1', {})
2872
2873 call term_sendkeys(buf, "gg$")
2874 call VerifyScreenDump(buf, 'Test_prop_with_text_below_nowrap_2', {})
2875
2876 call StopVimInTerminal(buf)
Bram Moolenaar48ca24d2022-08-06 22:03:20 +01002877endfunc
2878
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002879func Test_props_with_text_above()
2880 CheckRunVimInTerminal
2881
2882 let lines =<< trim END
2883 call setline(1, ['one two', 'three four', 'five six'])
2884 call prop_type_add('above1', #{highlight: 'Search'})
2885 call prop_type_add('above2', #{highlight: 'DiffChange'})
Bram Moolenaar6eda17d2022-09-12 19:25:11 +01002886 call prop_type_add('below', #{highlight: 'DiffAdd'})
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002887 call prop_add(1, 0, #{type: 'above1', text: 'first thing above', text_align: 'above'})
2888 call prop_add(1, 0, #{type: 'above2', text: 'second thing above', text_align: 'above'})
Bram Moolenaar79f8b842022-09-11 13:31:01 +01002889 call prop_add(3, 0, #{type: 'above1', text: 'another thing', text_align: 'above', text_padding_left: 3})
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002890
2891 normal gglllj
Bram Moolenaar6eda17d2022-09-12 19:25:11 +01002892 func AddPropBelow()
2893 call prop_add(1, 0, #{type: 'below', text: 'below', text_align: 'below'})
2894 endfunc
Bram Moolenaar9466fb82022-10-11 14:54:42 +01002895 func AddLongPropAbove()
2896 3,4delete
2897 set wrap
2898 call prop_add(1, 0, #{type: 'above1', text: range(50)->join(' '), text_align: 'above', text_padding_left: 2})
2899 endfunc
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002900 END
2901 call writefile(lines, 'XscriptPropsWithTextAbove', 'D')
2902 let buf = RunVimInTerminal('-S XscriptPropsWithTextAbove', #{rows: 9, cols: 60})
2903 call VerifyScreenDump(buf, 'Test_prop_with_text_above_1', {})
2904
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01002905 call term_sendkeys(buf, "ggg$")
2906 call VerifyScreenDump(buf, 'Test_prop_with_text_above_1a', {})
2907 call term_sendkeys(buf, "g0")
2908 call VerifyScreenDump(buf, 'Test_prop_with_text_above_1b', {})
2909
Bram Moolenaar4c7fd4d2022-09-17 17:15:33 +01002910 call term_sendkeys(buf, ":set showbreak=>>\<CR>")
2911 call term_sendkeys(buf, "ggll")
2912 call VerifyScreenDump(buf, 'Test_prop_with_text_above_1c', {})
2913 call term_sendkeys(buf, ":set showbreak=\<CR>")
2914
Bram Moolenaar88b79cb2022-09-10 22:32:14 +01002915 call term_sendkeys(buf, "ggI")
2916 call VerifyScreenDump(buf, 'Test_prop_with_text_above_2', {})
2917 call term_sendkeys(buf, "inserted \<Esc>")
2918 call VerifyScreenDump(buf, 'Test_prop_with_text_above_3', {})
2919
Bram Moolenaar79f8b842022-09-11 13:31:01 +01002920 call term_sendkeys(buf, ":set number signcolumn=yes\<CR>")
2921 call VerifyScreenDump(buf, 'Test_prop_with_text_above_4', {})
2922
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01002923 call term_sendkeys(buf, ":set nowrap\<CR>gg$j")
2924 call VerifyScreenDump(buf, 'Test_prop_with_text_above_5', {})
2925
Bram Moolenaar6eda17d2022-09-12 19:25:11 +01002926 call term_sendkeys(buf, ":call AddPropBelow()\<CR>")
2927 call term_sendkeys(buf, "ggve")
2928 call VerifyScreenDump(buf, 'Test_prop_with_text_above_6', {})
2929 call term_sendkeys(buf, "V")
2930 call VerifyScreenDump(buf, 'Test_prop_with_text_above_7', {})
2931
Bram Moolenaar3b93cf22022-09-13 18:34:18 +01002932 call term_sendkeys(buf, "\<Esc>ls\<CR>\<Esc>")
2933 call VerifyScreenDump(buf, 'Test_prop_with_text_above_8', {})
2934
Bram Moolenaar9466fb82022-10-11 14:54:42 +01002935 call term_sendkeys(buf, ":call AddLongPropAbove()\<CR>")
2936 call VerifyScreenDump(buf, 'Test_prop_with_text_above_9', {})
2937
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002938 call StopVimInTerminal(buf)
2939endfunc
2940
Bram Moolenaar702bd6c2022-09-14 16:09:57 +01002941func Test_prop_above_with_indent()
2942 new
2943 call setline(1, ['first line', ' second line', ' line below'])
2944 setlocal cindent
2945 call prop_type_add('indented', #{highlight: 'Search'})
2946 call prop_add(3, 0, #{type: 'indented', text: 'here', text_align: 'above', text_padding_left: 4})
2947 call assert_equal(' line below', getline(3))
2948
2949 exe "normal 3G2|a\<CR>"
2950 call assert_equal(' ', getline(3))
2951 call assert_equal(' line below', getline(4))
2952
2953 bwipe!
2954 call prop_type_delete('indented')
2955endfunc
2956
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002957func Test_prop_below_split_line()
2958 CheckRunVimInTerminal
2959
2960 let lines =<< trim END
2961 vim9script
2962 setline(1, ['one one one', 'two two two', 'three three three'])
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002963 prop_type_add('test', {highlight: 'Search'})
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002964 prop_add(2, 0, {
2965 text: '└─ Virtual text below the 2nd line',
2966 type: 'test',
2967 text_align: 'below',
2968 text_padding_left: 3
2969 })
2970 END
2971 call writefile(lines, 'XscriptPropBelowSpitLine', 'D')
2972 let buf = RunVimInTerminal('-S XscriptPropBelowSpitLine', #{rows: 8})
2973 call term_sendkeys(buf, "2GA\<CR>xx")
2974 call VerifyScreenDump(buf, 'Test_prop_below_split_line_1', {})
2975
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01002976 call term_sendkeys(buf, "\<Esc>:set number\<CR>")
2977 call VerifyScreenDump(buf, 'Test_prop_below_split_line_2', {})
2978
Bram Moolenaarb84d5652022-09-20 17:57:53 +01002979 call term_sendkeys(buf, ":set nowrap\<CR>")
2980 call VerifyScreenDump(buf, 'Test_prop_below_split_line_3', {})
2981
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002982 call StopVimInTerminal(buf)
2983endfunc
2984
Bram Moolenaar9e7e28f2022-08-14 16:36:38 +01002985func Test_props_with_text_override()
2986 CheckRunVimInTerminal
2987
2988 let lines =<< trim END
2989 vim9script
2990 setline(1, 'some text here')
2991 hi Likethis ctermfg=blue ctermbg=cyan
2992 prop_type_add('prop', {highlight: 'Likethis', override: true})
2993 prop_add(1, 6, {type: 'prop', text: ' inserted '})
2994 hi CursorLine cterm=underline ctermbg=lightgrey
2995 set cursorline
2996 END
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002997 call writefile(lines, 'XscriptPropsOverride', 'D')
Bram Moolenaar9e7e28f2022-08-14 16:36:38 +01002998 let buf = RunVimInTerminal('-S XscriptPropsOverride', #{rows: 6, cols: 60})
2999 call VerifyScreenDump(buf, 'Test_prop_with_text_override_1', {})
3000
3001 call term_sendkeys(buf, ":set nocursorline\<CR>")
3002 call term_sendkeys(buf, "0llvfr")
3003 call VerifyScreenDump(buf, 'Test_prop_with_text_override_2', {})
3004
3005 call StopVimInTerminal(buf)
Bram Moolenaar9e7e28f2022-08-14 16:36:38 +01003006endfunc
3007
Bram Moolenaar326c5d32022-08-12 13:05:49 +01003008func Test_props_with_text_CursorMoved()
3009 CheckRunVimInTerminal
3010
3011 let lines =<< trim END
3012 call setline(1, ['this is line one', 'this is line two', 'three', 'four', 'five'])
3013
3014 call prop_type_add('prop', #{highlight: 'Error'})
3015 let g:long_text = repeat('x', &columns * 2)
3016
3017 let g:prop_id = v:null
3018 func! Update()
3019 if line('.') == 1
3020 if g:prop_id == v:null
3021 let g:prop_id = prop_add(1, 0, #{type: 'prop', text_wrap: 'wrap', text: g:long_text})
3022 endif
3023 elseif g:prop_id != v:null
3024 call prop_remove(#{id: g:prop_id})
3025 let g:prop_id = v:null
3026 endif
3027 endfunc
3028
3029 autocmd CursorMoved * call Update()
3030 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003031 call writefile(lines, 'XscriptPropsCursorMovec', 'D')
Bram Moolenaar326c5d32022-08-12 13:05:49 +01003032 let buf = RunVimInTerminal('-S XscriptPropsCursorMovec', #{rows: 8, cols: 60})
3033 call term_sendkeys(buf, "gg0w")
3034 call VerifyScreenDump(buf, 'Test_prop_with_text_cursormoved_1', {})
3035
3036 call term_sendkeys(buf, "j")
3037 call VerifyScreenDump(buf, 'Test_prop_with_text_cursormoved_2', {})
3038
3039 " back to the first state
3040 call term_sendkeys(buf, "k")
3041 call VerifyScreenDump(buf, 'Test_prop_with_text_cursormoved_1', {})
3042
3043 call StopVimInTerminal(buf)
Bram Moolenaar326c5d32022-08-12 13:05:49 +01003044endfunc
3045
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01003046func Test_props_with_text_after_split_join()
3047 CheckRunVimInTerminal
3048
3049 let lines =<< trim END
3050 call setline(1, ['1122'])
3051 call prop_type_add('belowprop', #{highlight: 'ErrorMsg'})
3052 call prop_add(1, 0, #{type: 'belowprop', text: ' Below the line ', text_align: 'below'})
3053 exe "normal f2i\<CR>\<Esc>"
3054
3055 func AddMore()
3056 call prop_type_add('another', #{highlight: 'Search'})
3057 call prop_add(1, 0, #{type: 'another', text: ' after the text ', text_align: 'after'})
3058 call prop_add(1, 0, #{type: 'another', text: ' right here', text_align: 'right'})
3059 endfunc
3060 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003061 call writefile(lines, 'XscriptPropsAfterSplitJoin', 'D')
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01003062 let buf = RunVimInTerminal('-S XscriptPropsAfterSplitJoin', #{rows: 8, cols: 60})
3063 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_1', {})
3064
3065 call term_sendkeys(buf, "ggJ")
3066 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_2', {})
3067
3068 call term_sendkeys(buf, ":call AddMore()\<CR>")
3069 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_3', {})
3070
3071 call term_sendkeys(buf, "ggf s\<CR>\<Esc>")
3072 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_4', {})
3073
3074 call term_sendkeys(buf, "ggJ")
3075 call VerifyScreenDump(buf, 'Test_prop_with_text_after_join_split_5', {})
3076
3077 call StopVimInTerminal(buf)
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01003078endfunc
3079
Bram Moolenaar3a4cd392022-07-30 22:17:18 +01003080func Test_removed_prop_with_text_cleans_up_array()
3081 new
3082 call setline(1, 'some text here')
3083 call prop_type_add('some', #{highlight: 'ErrorMsg'})
3084 let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"})
3085 call assert_equal(-1, id1)
3086 let id2 = prop_add(1, 10, #{type: 'some', text: "HERE"})
3087 call assert_equal(-2, id2)
3088
3089 " removing the props resets the index
3090 call prop_remove(#{id: id1})
3091 call prop_remove(#{id: id2})
3092 let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"})
3093 call assert_equal(-1, id1)
3094
3095 call prop_type_delete('some')
3096 bwipe!
3097endfunc
3098
Bram Moolenaar1f4ee192022-08-01 15:52:55 +01003099def Test_insert_text_before_virtual_text()
3100 new foobar
3101 setline(1, '12345678')
3102 prop_type_add('test', {highlight: 'Search'})
3103 prop_add(1, 5, {
3104 type: 'test',
3105 text: ' virtual text '
3106 })
3107 normal! f4axyz
3108 normal! f5iXYZ
3109 assert_equal('1234xyzXYZ5678', getline(1))
3110
3111 prop_type_delete('test')
3112 bwipe!
3113enddef
3114
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003115func Test_insert_text_start_incl()
3116 CheckRunVimInTerminal
3117
3118 let lines =<< trim END
3119 vim9script
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01003120 setline(1, ['text one text two', '', 'function(arg)'])
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003121
3122 prop_type_add('propincl', {highlight: 'NonText', start_incl: true})
3123 prop_add(1, 6, {type: 'propincl', text: 'after '})
3124 cursor(1, 6)
3125 prop_type_add('propnotincl', {highlight: 'NonText', start_incl: false})
3126 prop_add(1, 15, {type: 'propnotincl', text: 'before '})
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01003127
3128 set cindent sw=4
3129 prop_type_add('argname', {highlight: 'DiffChange', start_incl: true})
3130 prop_add(3, 10, {type: 'argname', text: 'arg: '})
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003131 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003132 call writefile(lines, 'XscriptPropsStartIncl', 'D')
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003133 let buf = RunVimInTerminal('-S XscriptPropsStartIncl', #{rows: 8, cols: 60})
3134 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_1', {})
3135
3136 call term_sendkeys(buf, "i")
3137 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_2', {})
3138 call term_sendkeys(buf, "xx\<Esc>")
3139 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_3', {})
3140
3141 call term_sendkeys(buf, "2wi")
3142 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_4', {})
3143 call term_sendkeys(buf, "yy\<Esc>")
3144 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_5', {})
3145
Bram Moolenaard8d4cfc2022-08-15 15:55:10 +01003146 call term_sendkeys(buf, "3Gfai\<CR>\<Esc>")
3147 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_6', {})
3148 call term_sendkeys(buf, ">>")
3149 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_7', {})
3150 call term_sendkeys(buf, "<<<<")
3151 call VerifyScreenDump(buf, 'Test_prop_insert_start_incl_8', {})
3152
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003153 call StopVimInTerminal(buf)
Bram Moolenaar28c9f892022-08-14 13:28:55 +01003154endfunc
3155
Bram Moolenaarc3a483f2022-08-14 19:37:36 +01003156func Test_insert_text_list_mode()
3157 CheckRunVimInTerminal
3158
3159 let lines =<< trim END
3160 vim9script
3161 setline(1, ['This is a line with quite a bit of text here.',
3162 'second line', 'third line'])
3163 set list listchars+=extends:»
3164 prop_type_add('Prop1', {highlight: 'Error'})
3165 prop_add(1, 0, {
3166 type: 'Prop1',
3167 text: 'The quick brown fox jumps over the lazy dog',
3168 text_align: 'right'
3169 })
3170 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003171 call writefile(lines, 'XscriptPropsListMode', 'D')
Bram Moolenaarc3a483f2022-08-14 19:37:36 +01003172 let buf = RunVimInTerminal('-S XscriptPropsListMode', #{rows: 8, cols: 60})
3173 call term_sendkeys(buf, "ggj")
3174 call VerifyScreenDump(buf, 'Test_prop_insert_list_mode_1', {})
3175
3176 call term_sendkeys(buf, ":set nowrap\<CR>")
3177 call VerifyScreenDump(buf, 'Test_prop_insert_list_mode_2', {})
3178
3179 call term_sendkeys(buf, "ggd32l")
3180 call VerifyScreenDump(buf, 'Test_prop_insert_list_mode_3', {})
3181
3182 call StopVimInTerminal(buf)
Bram Moolenaarc3a483f2022-08-14 19:37:36 +01003183endfunc
3184
Bram Moolenaarf396ce82022-08-23 18:39:37 +01003185func Test_insert_text_with_padding()
3186 CheckRunVimInTerminal
3187
3188 let lines =<< trim END
3189 vim9script
3190 setline(1, ['Some text to add virtual text to.',
3191 'second line',
3192 'Another line with some text to make the wrap.'])
3193 prop_type_add('theprop', {highlight: 'DiffChange'})
3194 prop_add(1, 0, {
3195 type: 'theprop',
3196 text: 'after',
3197 text_align: 'after',
3198 text_padding_left: 3,
3199 })
3200 prop_add(1, 0, {
3201 type: 'theprop',
3202 text: 'right aligned',
3203 text_align: 'right',
3204 text_padding_left: 5,
3205 })
3206 prop_add(1, 0, {
3207 type: 'theprop',
3208 text: 'below the line',
3209 text_align: 'below',
3210 text_padding_left: 4,
3211 })
3212 prop_add(3, 0, {
3213 type: 'theprop',
3214 text: 'rightmost',
3215 text_align: 'right',
3216 text_padding_left: 6,
3217 text_wrap: 'wrap',
3218 })
3219 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003220 call writefile(lines, 'XscriptPropsPadded', 'D')
Bram Moolenaarf396ce82022-08-23 18:39:37 +01003221 let buf = RunVimInTerminal('-S XscriptPropsPadded', #{rows: 8, cols: 60})
3222 call VerifyScreenDump(buf, 'Test_prop_text_with_padding_1', {})
3223
3224 call term_sendkeys(buf, "ggixxxxxxxxxx\<Esc>")
3225 call term_sendkeys(buf, "3Gix\<Esc>")
3226 call VerifyScreenDump(buf, 'Test_prop_text_with_padding_2', {})
3227
3228 call term_sendkeys(buf, "ggix\<Esc>")
3229 call VerifyScreenDump(buf, 'Test_prop_text_with_padding_3', {})
3230
Bram Moolenaara4abe512022-09-15 19:44:09 +01003231 call term_sendkeys(buf, ":set list\<CR>")
3232 call VerifyScreenDump(buf, 'Test_prop_text_with_padding_4', {})
3233
Bram Moolenaarf396ce82022-08-23 18:39:37 +01003234 call StopVimInTerminal(buf)
Bram Moolenaarf396ce82022-08-23 18:39:37 +01003235endfunc
3236
Bram Moolenaar13845c42022-10-09 15:26:03 +01003237func Test_long_text_below_with_padding()
3238 CheckRunVimInTerminal
3239
3240 let lines =<< trim END
3241 vim9script
3242 setline(1, ['first line', 'second line'])
3243 prop_type_add('theprop', {highlight: 'DiffChange'})
3244 prop_add(1, 0, {
3245 type: 'theprop',
3246 text: 'after '->repeat(20),
3247 text_align: 'below',
3248 text_padding_left: 3,
3249 })
3250 prop_add(1, 0, {
3251 type: 'theprop',
3252 text: 'more '->repeat(20),
3253 text_align: 'below',
3254 text_padding_left: 30,
3255 })
3256 normal 2Gw
3257 END
3258 call writefile(lines, 'XlongTextBelowWithPadding', 'D')
3259 let buf = RunVimInTerminal('-S XlongTextBelowWithPadding', #{rows: 8, cols: 60})
3260 call VerifyScreenDump(buf, 'Test_long_text_with_padding_1', {})
3261
Bram Moolenaara9a36482022-10-11 16:47:22 +01003262 call term_sendkeys(buf, ":set list\<CR>")
3263 call VerifyScreenDump(buf, 'Test_long_text_with_padding_2', {})
3264
Bram Moolenaar13845c42022-10-09 15:26:03 +01003265 call StopVimInTerminal(buf)
3266endfunc
3267
Bram Moolenaarf167c7b2022-10-09 21:53:58 +01003268func Test_text_after_nowrap()
3269 CheckRunVimInTerminal
3270
Bram Moolenaarf167c7b2022-10-09 21:53:58 +01003271 let lines =<< trim END
3272 vim9script
Bram Moolenaarcd105412022-10-10 19:50:42 +01003273 setline(1, ['first line', range(80)->join(' '), 'third', 'fourth'])
Bram Moolenaarf167c7b2022-10-09 21:53:58 +01003274 set nowrap
3275 prop_type_add('theprop', {highlight: 'DiffChange'})
3276 prop_add(1, 0, {
3277 type: 'theprop',
Bram Moolenaarcd105412022-10-10 19:50:42 +01003278 text: 'right after the text '->repeat(3),
Bram Moolenaarf167c7b2022-10-09 21:53:58 +01003279 text_align: 'after',
3280 text_padding_left: 2,
3281 })
Bram Moolenaarcd105412022-10-10 19:50:42 +01003282 prop_add(1, 0, {
3283 type: 'theprop',
3284 text: 'in the middle '->repeat(4),
3285 text_align: 'after',
3286 text_padding_left: 3,
3287 })
3288 prop_add(1, 0, {
3289 type: 'theprop',
3290 text: 'the last one '->repeat(3),
3291 text_align: 'after',
3292 text_padding_left: 1,
3293 })
Bram Moolenaarf167c7b2022-10-09 21:53:58 +01003294 normal 2Gw
Bram Moolenaar7e017462022-10-11 21:02:09 +01003295 def g:ChangeText()
3296 prop_clear(1)
3297 set list
3298 prop_add(1, 0, {
3299 type: 'theprop',
3300 text: 'just after txt '->repeat(3),
3301 text_align: 'after',
3302 text_padding_left: 2,
3303 })
3304 prop_add(1, 0, {
3305 type: 'theprop',
3306 text: 'in the middle '->repeat(4),
3307 text_align: 'after',
3308 text_padding_left: 1,
3309 })
3310 enddef
Bram Moolenaarf167c7b2022-10-09 21:53:58 +01003311 END
3312 call writefile(lines, 'XTextAfterNowrap', 'D')
3313 let buf = RunVimInTerminal('-S XTextAfterNowrap', #{rows: 8, cols: 60})
3314 call VerifyScreenDump(buf, 'Test_text_after_nowrap_1', {})
3315
Bram Moolenaarcd105412022-10-10 19:50:42 +01003316 call term_sendkeys(buf, "30w")
3317 call VerifyScreenDump(buf, 'Test_text_after_nowrap_2', {})
3318
3319 call term_sendkeys(buf, "22w")
3320 call VerifyScreenDump(buf, 'Test_text_after_nowrap_3', {})
3321
3322 call term_sendkeys(buf, "$")
3323 call VerifyScreenDump(buf, 'Test_text_after_nowrap_4', {})
3324
Bram Moolenaar7e017462022-10-11 21:02:09 +01003325 call term_sendkeys(buf, "0")
3326 call term_sendkeys(buf, ":call ChangeText()\<CR>")
3327 call VerifyScreenDump(buf, 'Test_text_after_nowrap_5', {})
3328
Bram Moolenaarf167c7b2022-10-09 21:53:58 +01003329 call StopVimInTerminal(buf)
3330endfunc
3331
Bram Moolenaar1206c162022-10-10 15:40:04 +01003332func Test_text_below_nowrap()
3333 CheckRunVimInTerminal
3334
3335 let lines =<< trim END
3336 vim9script
3337 setline(1, ['first line', 'second line '->repeat(50), 'third', 'fourth'])
3338 set nowrap number
3339 prop_type_add('theprop', {highlight: 'DiffChange'})
3340 prop_add(1, 0, {
3341 type: 'theprop',
3342 text: 'one below the text '->repeat(5),
3343 text_align: 'below',
3344 text_padding_left: 2,
3345 })
3346 prop_add(1, 0, {
3347 type: 'theprop',
3348 text: 'two below the text '->repeat(5),
3349 text_align: 'below',
3350 text_padding_left: 2,
3351 })
3352 normal 2Gw
3353 END
3354 call writefile(lines, 'XTextBelowNowrap', 'D')
3355 let buf = RunVimInTerminal('-S XTextBelowNowrap', #{rows: 8, cols: 60})
3356 call VerifyScreenDump(buf, 'Test_text_below_nowrap_1', {})
3357
3358 call StopVimInTerminal(buf)
3359endfunc
3360
Bram Moolenaarf5240b92022-08-24 12:24:37 +01003361func Test_insert_text_change_arg()
3362 CheckRunVimInTerminal
3363
3364 let lines =<< trim END
3365 vim9script
3366 setline(1, ['SetErrorCode( 10, 20 )', 'second line'])
3367 prop_type_add('param', {highlight: 'DiffChange', start_incl: 1})
3368 prop_type_add('padd', {highlight: 'NonText', start_incl: 1})
3369 prop_add(1, 15, {
3370 type: 'param',
3371 text: 'id:',
3372 })
3373 prop_add(1, 15, {
3374 type: 'padd',
3375 text: '-',
3376 })
3377 prop_add(1, 19, {
3378 type: 'param',
3379 text: 'id:',
3380 })
3381 prop_add(1, 19, {
3382 type: 'padd',
3383 text: '-',
3384 })
3385 END
Bram Moolenaar2fdc9b52022-09-20 15:59:22 +01003386 call writefile(lines, 'XscriptPropsChange', 'D')
Bram Moolenaarf5240b92022-08-24 12:24:37 +01003387 let buf = RunVimInTerminal('-S XscriptPropsChange', #{rows: 5, cols: 60})
3388 call VerifyScreenDump(buf, 'Test_prop_text_change_arg_1', {})
3389
3390 call term_sendkeys(buf, "ggf1cw1234\<Esc>")
3391 call VerifyScreenDump(buf, 'Test_prop_text_change_arg_2', {})
3392
3393 call StopVimInTerminal(buf)
Bram Moolenaarf5240b92022-08-24 12:24:37 +01003394endfunc
3395
Bram Moolenaar99fa7212020-04-26 15:59:55 +02003396" vim: shiftwidth=2 sts=2 expandtab