Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 1 | " Tests for defining text property types and adding text properties to the |
| 2 | " buffer. |
| 3 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 4 | source check.vim |
| 5 | CheckFeature textprop |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 6 | |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 7 | source screendump.vim |
| 8 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 9 | func Test_proptype_global() |
| 10 | call prop_type_add('comment', {'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1}) |
| 11 | let proptypes = prop_type_list() |
| 12 | call assert_equal(1, len(proptypes)) |
| 13 | call assert_equal('comment', proptypes[0]) |
| 14 | |
| 15 | let proptype = prop_type_get('comment') |
| 16 | call assert_equal('Directory', proptype['highlight']) |
| 17 | call assert_equal(123, proptype['priority']) |
| 18 | call assert_equal(1, proptype['start_incl']) |
| 19 | call assert_equal(1, proptype['end_incl']) |
| 20 | |
| 21 | call prop_type_delete('comment') |
| 22 | call assert_equal(0, len(prop_type_list())) |
| 23 | |
| 24 | call prop_type_add('one', {}) |
| 25 | call assert_equal(1, len(prop_type_list())) |
Bram Moolenaar | a5a7882 | 2019-09-04 21:57:18 +0200 | [diff] [blame] | 26 | let proptype = 'one'->prop_type_get() |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 27 | call assert_false(has_key(proptype, 'highlight')) |
| 28 | call assert_equal(0, proptype['priority']) |
| 29 | call assert_equal(0, proptype['start_incl']) |
| 30 | call assert_equal(0, proptype['end_incl']) |
| 31 | |
| 32 | call prop_type_add('two', {}) |
| 33 | call assert_equal(2, len(prop_type_list())) |
| 34 | call prop_type_delete('one') |
| 35 | call assert_equal(1, len(prop_type_list())) |
| 36 | call prop_type_delete('two') |
| 37 | call assert_equal(0, len(prop_type_list())) |
| 38 | endfunc |
| 39 | |
| 40 | func Test_proptype_buf() |
| 41 | let bufnr = bufnr('') |
| 42 | call prop_type_add('comment', {'bufnr': bufnr, 'highlight': 'Directory', 'priority': 123, 'start_incl': 1, 'end_incl': 1}) |
| 43 | let proptypes = prop_type_list({'bufnr': bufnr}) |
| 44 | call assert_equal(1, len(proptypes)) |
| 45 | call assert_equal('comment', proptypes[0]) |
| 46 | |
| 47 | let proptype = prop_type_get('comment', {'bufnr': bufnr}) |
| 48 | call assert_equal('Directory', proptype['highlight']) |
| 49 | call assert_equal(123, proptype['priority']) |
| 50 | call assert_equal(1, proptype['start_incl']) |
| 51 | call assert_equal(1, proptype['end_incl']) |
| 52 | |
| 53 | call prop_type_delete('comment', {'bufnr': bufnr}) |
Bram Moolenaar | a5a7882 | 2019-09-04 21:57:18 +0200 | [diff] [blame] | 54 | call assert_equal(0, len({'bufnr': bufnr}->prop_type_list())) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 55 | |
| 56 | call prop_type_add('one', {'bufnr': bufnr}) |
| 57 | let proptype = prop_type_get('one', {'bufnr': bufnr}) |
| 58 | call assert_false(has_key(proptype, 'highlight')) |
| 59 | call assert_equal(0, proptype['priority']) |
| 60 | call assert_equal(0, proptype['start_incl']) |
| 61 | call assert_equal(0, proptype['end_incl']) |
| 62 | |
| 63 | call prop_type_add('two', {'bufnr': bufnr}) |
| 64 | call assert_equal(2, len(prop_type_list({'bufnr': bufnr}))) |
| 65 | call prop_type_delete('one', {'bufnr': bufnr}) |
| 66 | call assert_equal(1, len(prop_type_list({'bufnr': bufnr}))) |
| 67 | call prop_type_delete('two', {'bufnr': bufnr}) |
| 68 | call assert_equal(0, len(prop_type_list({'bufnr': bufnr}))) |
Bram Moolenaar | f0884c5 | 2019-05-24 21:22:29 +0200 | [diff] [blame] | 69 | |
| 70 | call assert_fails("call prop_type_add('one', {'bufnr': 98764})", "E158:") |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 71 | endfunc |
| 72 | |
| 73 | func AddPropTypes() |
| 74 | call prop_type_add('one', {}) |
| 75 | call prop_type_add('two', {}) |
| 76 | call prop_type_add('three', {}) |
| 77 | call prop_type_add('whole', {}) |
| 78 | endfunc |
| 79 | |
| 80 | func DeletePropTypes() |
| 81 | call prop_type_delete('one') |
| 82 | call prop_type_delete('two') |
| 83 | call prop_type_delete('three') |
| 84 | call prop_type_delete('whole') |
| 85 | endfunc |
| 86 | |
| 87 | func SetupPropsInFirstLine() |
| 88 | call setline(1, 'one two three') |
| 89 | call prop_add(1, 1, {'length': 3, 'id': 11, 'type': 'one'}) |
Bram Moolenaar | a5a7882 | 2019-09-04 21:57:18 +0200 | [diff] [blame] | 90 | eval 1->prop_add(5, {'length': 3, 'id': 12, 'type': 'two'}) |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 91 | call prop_add(1, 9, {'length': 5, 'id': 13, 'type': 'three'}) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 92 | call prop_add(1, 1, {'length': 13, 'id': 14, 'type': 'whole'}) |
| 93 | endfunc |
| 94 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 95 | func Get_expected_props() |
| 96 | return [ |
| 97 | \ {'col': 1, 'length': 13, 'id': 14, 'type': 'whole', 'start': 1, 'end': 1}, |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 98 | \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1}, |
| 99 | \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1}, |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 100 | \ {'col': 9, 'length': 5, 'id': 13, 'type': 'three', 'start': 1, 'end': 1}, |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 101 | \ ] |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 102 | endfunc |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 103 | |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 104 | func Test_prop_find() |
| 105 | new |
| 106 | call setline(1, ['one one one', 'twotwo', 'three', 'fourfour', 'five', 'sixsix']) |
| 107 | |
| 108 | " Add two text props on lines 1 and 5, and one spanning lines 2 to 4. |
| 109 | call prop_type_add('prop_name', {'highlight': 'Directory'}) |
| 110 | call prop_add(1, 5, {'type': 'prop_name', 'id': 10, 'length': 3}) |
| 111 | call prop_add(2, 4, {'type': 'prop_name', 'id': 11, 'end_lnum': 4, 'end_col': 9}) |
| 112 | call prop_add(5, 4, {'type': 'prop_name', 'id': 12, 'length': 1}) |
| 113 | |
| 114 | let expected = [ |
| 115 | \ {'lnum': 1, 'col': 5, 'length': 3, 'id': 10, 'type': 'prop_name', 'start': 1, 'end': 1}, |
| 116 | \ {'lnum': 2, 'col': 4, 'id': 11, 'type': 'prop_name', 'start': 1, 'end': 0}, |
| 117 | \ {'lnum': 5, 'col': 4, 'length': 1, 'id': 12, 'type': 'prop_name', 'start': 1, 'end': 1} |
| 118 | \ ] |
| 119 | |
| 120 | " Starting at line 5 col 1 this should find the prop at line 5 col 4. |
| 121 | call cursor(5,1) |
| 122 | let result = prop_find({'type': 'prop_name'}, 'f') |
| 123 | call assert_equal(expected[2], result) |
| 124 | |
| 125 | " With skipstart left at false (default), this should find the prop at line |
| 126 | " 5 col 4. |
| 127 | let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4}, 'b') |
| 128 | call assert_equal(expected[2], result) |
| 129 | |
| 130 | " With skipstart set to true, this should skip the prop at line 5 col 4. |
| 131 | let result = prop_find({'type': 'prop_name', 'lnum': 5, 'col': 4, 'skipstart': 1}, 'b') |
| 132 | unlet result.length |
| 133 | call assert_equal(expected[1], result) |
| 134 | |
| 135 | " Search backwards from line 1 col 10 to find the prop on the same line. |
| 136 | let result = prop_find({'type': 'prop_name', 'lnum': 1, 'col': 10}, 'b') |
| 137 | call assert_equal(expected[0], result) |
| 138 | |
| 139 | " with skipstart set to false, if the start position is anywhere between the |
| 140 | " start and end lines of a text prop (searching forward or backward), the |
| 141 | " result should be the prop on the first line (the line with 'start' set to 1). |
| 142 | call cursor(3,1) |
| 143 | let result = prop_find({'type': 'prop_name'}, 'f') |
| 144 | unlet result.length |
| 145 | call assert_equal(expected[1], result) |
| 146 | let result = prop_find({'type': 'prop_name'}, 'b') |
| 147 | unlet result.length |
| 148 | call assert_equal(expected[1], result) |
| 149 | |
| 150 | " with skipstart set to true, if the start position is anywhere between the |
| 151 | " start and end lines of a text prop (searching forward or backward), all lines |
| 152 | " of the prop will be skipped. |
| 153 | let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'b') |
| 154 | call assert_equal(expected[0], result) |
| 155 | let result = prop_find({'type': 'prop_name', 'skipstart': 1}, 'f') |
| 156 | call assert_equal(expected[2], result) |
| 157 | |
| 158 | " Use skipstart to search through all props with type name 'prop_name'. |
| 159 | " First forward... |
| 160 | let lnum = 1 |
| 161 | let col = 1 |
| 162 | let i = 0 |
| 163 | for exp in expected |
| 164 | let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'f') |
| 165 | if !has_key(exp, "length") |
| 166 | unlet result.length |
| 167 | endif |
| 168 | call assert_equal(exp, result) |
| 169 | let lnum = result.lnum |
| 170 | let col = result.col |
| 171 | let i = i + 1 |
| 172 | endfor |
| 173 | |
| 174 | " ...then backwards. |
| 175 | let lnum = 6 |
| 176 | let col = 4 |
| 177 | let i = 2 |
| 178 | while i >= 0 |
| 179 | let result = prop_find({'type': 'prop_name', 'lnum': lnum, 'col': col, 'skipstart': 1}, 'b') |
| 180 | if !has_key(expected[i], "length") |
| 181 | unlet result.length |
| 182 | endif |
| 183 | call assert_equal(expected[i], result) |
| 184 | let lnum = result.lnum |
| 185 | let col = result.col |
| 186 | let i = i - 1 |
| 187 | endwhile |
| 188 | |
| 189 | " Starting from line 6 col 1 search backwards for prop with id 10. |
| 190 | call cursor(6,1) |
| 191 | let result = prop_find({'id': 10, 'skipstart': 1}, 'b') |
| 192 | call assert_equal(expected[0], result) |
| 193 | |
| 194 | " Starting from line 1 col 1 search forwards for prop with id 12. |
| 195 | call cursor(1,1) |
| 196 | let result = prop_find({'id': 12}, 'f') |
| 197 | call assert_equal(expected[2], result) |
| 198 | |
| 199 | " Search for a prop with an unknown id. |
| 200 | let result = prop_find({'id': 999}, 'f') |
| 201 | call assert_equal({}, result) |
| 202 | |
| 203 | " Search backwards from the proceeding position of the prop with id 11 |
| 204 | " (at line num 2 col 4). This should return an empty dict. |
| 205 | let result = prop_find({'id': 11, 'lnum': 2, 'col': 3}, 'b') |
| 206 | call assert_equal({}, result) |
| 207 | |
| 208 | " When lnum is given and col is omitted, use column 1. |
| 209 | let result = prop_find({'type': 'prop_name', 'lnum': 1}, 'f') |
| 210 | call assert_equal(expected[0], result) |
| 211 | |
| 212 | call prop_clear(1,6) |
| 213 | call prop_type_delete('prop_name') |
Bram Moolenaar | 4da7a25 | 2020-09-02 19:59:00 +0200 | [diff] [blame] | 214 | |
| 215 | " Multiple props per line, start on the first, should find the second. |
| 216 | let expected = {'lnum': 1, 'id': 0, 'col': 14, 'end': 1, 'type': 'misspell', 'length': 2, 'start': 1} |
| 217 | eval ['the quikc bronw fox jumsp over the layz dog']->repeat(2)->setline(1) |
| 218 | call prop_type_add('misspell', #{highlight: 'ErrorMsg'}) |
| 219 | for lnum in [1, 2] |
| 220 | for col in [8, 14, 24, 38] |
| 221 | call prop_add(lnum, col, #{type: 'misspell', length: 2}) |
| 222 | endfor |
| 223 | endfor |
| 224 | call cursor(1, 8) |
| 225 | let result = prop_find(#{type: 'misspell', skipstart: 1}, 'f') |
| 226 | call assert_equal(expected, result) |
| 227 | |
| 228 | call prop_type_delete('misspell') |
| 229 | bwipe! |
Bram Moolenaar | e05a89a | 2020-01-10 19:56:46 +0100 | [diff] [blame] | 230 | endfunc |
| 231 | |
Bram Moolenaar | 346f18e | 2020-03-13 21:36:40 +0100 | [diff] [blame] | 232 | func Test_prop_find_smaller_len_than_match_col() |
| 233 | new |
| 234 | call prop_type_add('test', {'highlight': 'ErrorMsg'}) |
| 235 | call setline(1, ['xxxx', 'x']) |
| 236 | call prop_add(1, 4, {'type': 'test'}) |
| 237 | call assert_equal({'id': 0, 'lnum': 1, 'col': 4, 'type': 'test', 'length': 0, 'start': 1, 'end': 1}, |
| 238 | \ prop_find({'type': 'test', 'lnum': 2, 'col': 1}, 'b')) |
| 239 | bwipe! |
| 240 | call prop_type_delete('test') |
| 241 | endfunc |
| 242 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 243 | func Test_prop_add() |
| 244 | new |
| 245 | call AddPropTypes() |
| 246 | call SetupPropsInFirstLine() |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 247 | let expected_props = Get_expected_props() |
| 248 | call assert_equal(expected_props, prop_list(1)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 249 | call assert_fails("call prop_add(10, 1, {'length': 1, 'id': 14, 'type': 'whole'})", 'E966:') |
| 250 | call assert_fails("call prop_add(1, 22, {'length': 1, 'id': 14, 'type': 'whole'})", 'E964:') |
| 251 | |
| 252 | " Insert a line above, text props must still be there. |
| 253 | call append(0, 'empty') |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 254 | call assert_equal(expected_props, prop_list(2)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 255 | " Delete a line above, text props must still be there. |
| 256 | 1del |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 257 | call assert_equal(expected_props, prop_list(1)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 258 | |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 259 | " Prop without length or end column is zero length |
| 260 | call prop_clear(1) |
Bram Moolenaar | 12f2003 | 2020-02-26 22:06:00 +0100 | [diff] [blame] | 261 | call prop_type_add('included', {'start_incl': 1, 'end_incl': 1}) |
| 262 | call prop_add(1, 5, #{type: 'included'}) |
| 263 | let expected = [#{col: 5, length: 0, type: 'included', id: 0, start: 1, end: 1}] |
| 264 | call assert_equal(expected, prop_list(1)) |
| 265 | |
| 266 | " Inserting text makes the prop bigger. |
| 267 | exe "normal 5|ixx\<Esc>" |
| 268 | let expected = [#{col: 5, length: 2, type: 'included', id: 0, start: 1, end: 1}] |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 269 | call assert_equal(expected, prop_list(1)) |
| 270 | |
Bram Moolenaar | f0884c5 | 2019-05-24 21:22:29 +0200 | [diff] [blame] | 271 | call assert_fails("call prop_add(1, 5, {'type': 'two', 'bufnr': 234343})", 'E158:') |
| 272 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 273 | call DeletePropTypes() |
Bram Moolenaar | 12f2003 | 2020-02-26 22:06:00 +0100 | [diff] [blame] | 274 | call prop_type_delete('included') |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 275 | bwipe! |
| 276 | endfunc |
| 277 | |
| 278 | func Test_prop_remove() |
| 279 | new |
| 280 | call AddPropTypes() |
| 281 | call SetupPropsInFirstLine() |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 282 | let props = Get_expected_props() |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 283 | call assert_equal(props, prop_list(1)) |
| 284 | |
| 285 | " remove by id |
Bram Moolenaar | a5a7882 | 2019-09-04 21:57:18 +0200 | [diff] [blame] | 286 | call assert_equal(1, {'id': 12}->prop_remove(1)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 287 | unlet props[2] |
| 288 | call assert_equal(props, prop_list(1)) |
| 289 | |
| 290 | " remove by type |
Bram Moolenaar | f0884c5 | 2019-05-24 21:22:29 +0200 | [diff] [blame] | 291 | call assert_equal(1, prop_remove({'type': 'one'}, 1)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 292 | unlet props[1] |
| 293 | call assert_equal(props, prop_list(1)) |
| 294 | |
Bram Moolenaar | f0884c5 | 2019-05-24 21:22:29 +0200 | [diff] [blame] | 295 | " remove from unknown buffer |
| 296 | call assert_fails("call prop_remove({'type': 'one', 'bufnr': 123456}, 1)", 'E158:') |
| 297 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 298 | call DeletePropTypes() |
| 299 | bwipe! |
Bram Moolenaar | 49b79bd | 2020-03-05 21:52:55 +0100 | [diff] [blame] | 300 | |
| 301 | new |
| 302 | call AddPropTypes() |
| 303 | call SetupPropsInFirstLine() |
| 304 | call prop_add(1, 6, {'length': 2, 'id': 11, 'type': 'three'}) |
| 305 | let props = Get_expected_props() |
| 306 | call insert(props, {'col': 6, 'length': 2, 'id': 11, 'type': 'three', 'start': 1, 'end': 1}, 3) |
| 307 | call assert_equal(props, prop_list(1)) |
| 308 | call assert_equal(1, prop_remove({'type': 'three', 'id': 11, 'both': 1, 'all': 1}, 1)) |
| 309 | unlet props[3] |
| 310 | call assert_equal(props, prop_list(1)) |
| 311 | |
| 312 | call assert_fails("call prop_remove({'id': 11, 'both': 1})", 'E860') |
| 313 | call assert_fails("call prop_remove({'type': 'three', 'both': 1})", 'E860') |
| 314 | |
| 315 | call DeletePropTypes() |
| 316 | bwipe! |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 317 | endfunc |
| 318 | |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 319 | func SetupOneLine() |
| 320 | call setline(1, 'xonex xtwoxx') |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 321 | normal gg0 |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 322 | call AddPropTypes() |
| 323 | call prop_add(1, 2, {'length': 3, 'id': 11, 'type': 'one'}) |
| 324 | call prop_add(1, 8, {'length': 3, 'id': 12, 'type': 'two'}) |
| 325 | let expected = [ |
| 326 | \ {'col': 2, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1}, |
| 327 | \ {'col': 8, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1}, |
| 328 | \] |
| 329 | call assert_equal(expected, prop_list(1)) |
| 330 | return expected |
| 331 | endfunc |
| 332 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 333 | func Test_prop_add_remove_buf() |
| 334 | new |
| 335 | let bufnr = bufnr('') |
| 336 | call AddPropTypes() |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 337 | for lnum in range(1, 4) |
| 338 | call setline(lnum, 'one two three') |
| 339 | endfor |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 340 | wincmd w |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 341 | for lnum in range(1, 4) |
| 342 | call prop_add(lnum, 1, {'length': 3, 'id': 11, 'type': 'one', 'bufnr': bufnr}) |
| 343 | call prop_add(lnum, 5, {'length': 3, 'id': 12, 'type': 'two', 'bufnr': bufnr}) |
| 344 | call prop_add(lnum, 11, {'length': 3, 'id': 13, 'type': 'three', 'bufnr': bufnr}) |
| 345 | endfor |
| 346 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 347 | let props = [ |
| 348 | \ {'col': 1, 'length': 3, 'id': 11, 'type': 'one', 'start': 1, 'end': 1}, |
| 349 | \ {'col': 5, 'length': 3, 'id': 12, 'type': 'two', 'start': 1, 'end': 1}, |
| 350 | \ {'col': 11, 'length': 3, 'id': 13, 'type': 'three', 'start': 1, 'end': 1}, |
| 351 | \] |
| 352 | call assert_equal(props, prop_list(1, {'bufnr': bufnr})) |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 353 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 354 | " remove by id |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 355 | let before_props = deepcopy(props) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 356 | unlet props[1] |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 357 | |
| 358 | call prop_remove({'id': 12, 'bufnr': bufnr}, 1) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 359 | call assert_equal(props, prop_list(1, {'bufnr': bufnr})) |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 360 | call assert_equal(before_props, prop_list(2, {'bufnr': bufnr})) |
| 361 | call assert_equal(before_props, prop_list(3, {'bufnr': bufnr})) |
| 362 | call assert_equal(before_props, prop_list(4, {'bufnr': bufnr})) |
| 363 | |
| 364 | call prop_remove({'id': 12, 'bufnr': bufnr}, 3, 4) |
| 365 | call assert_equal(props, prop_list(1, {'bufnr': bufnr})) |
| 366 | call assert_equal(before_props, prop_list(2, {'bufnr': bufnr})) |
| 367 | call assert_equal(props, prop_list(3, {'bufnr': bufnr})) |
| 368 | call assert_equal(props, prop_list(4, {'bufnr': bufnr})) |
| 369 | |
| 370 | call prop_remove({'id': 12, 'bufnr': bufnr}) |
| 371 | for lnum in range(1, 4) |
| 372 | call assert_equal(props, prop_list(lnum, {'bufnr': bufnr})) |
| 373 | endfor |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 374 | |
| 375 | " remove by type |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 376 | let before_props = deepcopy(props) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 377 | unlet props[0] |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 378 | |
| 379 | call prop_remove({'type': 'one', 'bufnr': bufnr}, 1) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 380 | call assert_equal(props, prop_list(1, {'bufnr': bufnr})) |
Bram Moolenaar | 0a2f578 | 2019-03-22 13:20:43 +0100 | [diff] [blame] | 381 | call assert_equal(before_props, prop_list(2, {'bufnr': bufnr})) |
| 382 | call assert_equal(before_props, prop_list(3, {'bufnr': bufnr})) |
| 383 | call assert_equal(before_props, prop_list(4, {'bufnr': bufnr})) |
| 384 | |
| 385 | call prop_remove({'type': 'one', 'bufnr': bufnr}, 3, 4) |
| 386 | call assert_equal(props, prop_list(1, {'bufnr': bufnr})) |
| 387 | call assert_equal(before_props, prop_list(2, {'bufnr': bufnr})) |
| 388 | call assert_equal(props, prop_list(3, {'bufnr': bufnr})) |
| 389 | call assert_equal(props, prop_list(4, {'bufnr': bufnr})) |
| 390 | |
| 391 | call prop_remove({'type': 'one', 'bufnr': bufnr}) |
| 392 | for lnum in range(1, 4) |
| 393 | call assert_equal(props, prop_list(lnum, {'bufnr': bufnr})) |
| 394 | endfor |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 395 | |
| 396 | call DeletePropTypes() |
| 397 | wincmd w |
| 398 | bwipe! |
| 399 | endfunc |
| 400 | |
Bram Moolenaar | 33c8ca9 | 2019-01-02 18:00:27 +0100 | [diff] [blame] | 401 | func Test_prop_backspace() |
| 402 | new |
| 403 | set bs=2 |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 404 | let expected = SetupOneLine() " 'xonex xtwoxx' |
Bram Moolenaar | 33c8ca9 | 2019-01-02 18:00:27 +0100 | [diff] [blame] | 405 | |
| 406 | exe "normal 0li\<BS>\<Esc>fxli\<BS>\<Esc>" |
| 407 | call assert_equal('one xtwoxx', getline(1)) |
| 408 | let expected[0].col = 1 |
| 409 | let expected[1].col = 6 |
| 410 | call assert_equal(expected, prop_list(1)) |
| 411 | |
| 412 | call DeletePropTypes() |
| 413 | bwipe! |
| 414 | set bs& |
| 415 | endfunc |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 416 | |
Bram Moolenaar | 196d157 | 2019-01-02 23:47:18 +0100 | [diff] [blame] | 417 | func Test_prop_replace() |
| 418 | new |
| 419 | set bs=2 |
| 420 | let expected = SetupOneLine() " 'xonex xtwoxx' |
| 421 | |
| 422 | exe "normal 0Ryyy\<Esc>" |
| 423 | call assert_equal('yyyex xtwoxx', getline(1)) |
| 424 | call assert_equal(expected, prop_list(1)) |
| 425 | |
| 426 | exe "normal ftRyy\<BS>" |
| 427 | call assert_equal('yyyex xywoxx', getline(1)) |
| 428 | call assert_equal(expected, prop_list(1)) |
| 429 | |
| 430 | exe "normal 0fwRyy\<BS>" |
| 431 | call assert_equal('yyyex xyyoxx', getline(1)) |
| 432 | call assert_equal(expected, prop_list(1)) |
| 433 | |
| 434 | exe "normal 0foRyy\<BS>\<BS>" |
| 435 | call assert_equal('yyyex xyyoxx', getline(1)) |
| 436 | call assert_equal(expected, prop_list(1)) |
| 437 | |
| 438 | call DeletePropTypes() |
| 439 | bwipe! |
| 440 | set bs& |
| 441 | endfunc |
| 442 | |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 443 | func Test_prop_open_line() |
| 444 | new |
| 445 | |
| 446 | " open new line, props stay in top line |
| 447 | let expected = SetupOneLine() " 'xonex xtwoxx' |
| 448 | exe "normal o\<Esc>" |
| 449 | call assert_equal('xonex xtwoxx', getline(1)) |
| 450 | call assert_equal('', getline(2)) |
| 451 | call assert_equal(expected, prop_list(1)) |
| 452 | call DeletePropTypes() |
| 453 | |
| 454 | " move all props to next line |
| 455 | let expected = SetupOneLine() " 'xonex xtwoxx' |
| 456 | exe "normal 0i\<CR>\<Esc>" |
| 457 | call assert_equal('', getline(1)) |
| 458 | call assert_equal('xonex xtwoxx', getline(2)) |
| 459 | call assert_equal(expected, prop_list(2)) |
| 460 | call DeletePropTypes() |
| 461 | |
| 462 | " split just before prop, move all props to next line |
| 463 | let expected = SetupOneLine() " 'xonex xtwoxx' |
| 464 | exe "normal 0li\<CR>\<Esc>" |
| 465 | call assert_equal('x', getline(1)) |
| 466 | call assert_equal('onex xtwoxx', getline(2)) |
| 467 | let expected[0].col -= 1 |
| 468 | let expected[1].col -= 1 |
| 469 | call assert_equal(expected, prop_list(2)) |
| 470 | call DeletePropTypes() |
| 471 | |
| 472 | " split inside prop, split first prop |
| 473 | let expected = SetupOneLine() " 'xonex xtwoxx' |
| 474 | exe "normal 0lli\<CR>\<Esc>" |
| 475 | call assert_equal('xo', getline(1)) |
| 476 | call assert_equal('nex xtwoxx', getline(2)) |
| 477 | let exp_first = [deepcopy(expected[0])] |
| 478 | let exp_first[0].length = 1 |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 479 | let exp_first[0].end = 0 |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 480 | call assert_equal(exp_first, prop_list(1)) |
| 481 | let expected[0].col = 1 |
| 482 | let expected[0].length = 2 |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 483 | let expected[0].start = 0 |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 484 | let expected[1].col -= 2 |
| 485 | call assert_equal(expected, prop_list(2)) |
| 486 | call DeletePropTypes() |
| 487 | |
Bram Moolenaar | 5c65e6a | 2019-05-17 11:08:56 +0200 | [diff] [blame] | 488 | " split just after first prop, second prop move to next line |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 489 | let expected = SetupOneLine() " 'xonex xtwoxx' |
| 490 | exe "normal 0fea\<CR>\<Esc>" |
| 491 | call assert_equal('xone', getline(1)) |
| 492 | call assert_equal('x xtwoxx', getline(2)) |
| 493 | let exp_first = expected[0:0] |
| 494 | call assert_equal(exp_first, prop_list(1)) |
Bram Moolenaar | 5c65e6a | 2019-05-17 11:08:56 +0200 | [diff] [blame] | 495 | let expected = expected[1:1] |
| 496 | let expected[0].col -= 4 |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 497 | call assert_equal(expected, prop_list(2)) |
| 498 | call DeletePropTypes() |
| 499 | |
| 500 | bwipe! |
| 501 | set bs& |
| 502 | endfunc |
| 503 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 504 | func Test_prop_clear() |
| 505 | new |
| 506 | call AddPropTypes() |
| 507 | call SetupPropsInFirstLine() |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 508 | call assert_equal(Get_expected_props(), prop_list(1)) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 509 | |
Bram Moolenaar | a5a7882 | 2019-09-04 21:57:18 +0200 | [diff] [blame] | 510 | eval 1->prop_clear() |
| 511 | call assert_equal([], 1->prop_list()) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 512 | |
| 513 | call DeletePropTypes() |
| 514 | bwipe! |
| 515 | endfunc |
| 516 | |
| 517 | func Test_prop_clear_buf() |
| 518 | new |
| 519 | call AddPropTypes() |
| 520 | call SetupPropsInFirstLine() |
| 521 | let bufnr = bufnr('') |
| 522 | wincmd w |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 523 | call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr})) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 524 | |
| 525 | call prop_clear(1, 1, {'bufnr': bufnr}) |
| 526 | call assert_equal([], prop_list(1, {'bufnr': bufnr})) |
| 527 | |
| 528 | wincmd w |
| 529 | call DeletePropTypes() |
| 530 | bwipe! |
| 531 | endfunc |
| 532 | |
Bram Moolenaar | 21b5038 | 2019-01-04 18:07:24 +0100 | [diff] [blame] | 533 | func Test_prop_setline() |
| 534 | new |
| 535 | call AddPropTypes() |
| 536 | call SetupPropsInFirstLine() |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 537 | call assert_equal(Get_expected_props(), prop_list(1)) |
Bram Moolenaar | 21b5038 | 2019-01-04 18:07:24 +0100 | [diff] [blame] | 538 | |
| 539 | call setline(1, 'foobar') |
| 540 | call assert_equal([], prop_list(1)) |
| 541 | |
| 542 | call DeletePropTypes() |
| 543 | bwipe! |
| 544 | endfunc |
| 545 | |
| 546 | func Test_prop_setbufline() |
| 547 | new |
| 548 | call AddPropTypes() |
| 549 | call SetupPropsInFirstLine() |
| 550 | let bufnr = bufnr('') |
| 551 | wincmd w |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 552 | call assert_equal(Get_expected_props(), prop_list(1, {'bufnr': bufnr})) |
Bram Moolenaar | 21b5038 | 2019-01-04 18:07:24 +0100 | [diff] [blame] | 553 | |
| 554 | call setbufline(bufnr, 1, 'foobar') |
| 555 | call assert_equal([], prop_list(1, {'bufnr': bufnr})) |
| 556 | |
| 557 | wincmd w |
| 558 | call DeletePropTypes() |
| 559 | bwipe! |
| 560 | endfunc |
| 561 | |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 562 | func Test_prop_substitute() |
| 563 | new |
| 564 | " Set first line to 'one two three' |
| 565 | call AddPropTypes() |
| 566 | call SetupPropsInFirstLine() |
| 567 | let expected_props = Get_expected_props() |
| 568 | call assert_equal(expected_props, prop_list(1)) |
| 569 | |
| 570 | " Change "n" in "one" to XX: 'oXXe two three' |
| 571 | s/n/XX/ |
| 572 | let expected_props[0].length += 1 |
| 573 | let expected_props[1].length += 1 |
| 574 | let expected_props[2].col += 1 |
| 575 | let expected_props[3].col += 1 |
| 576 | call assert_equal(expected_props, prop_list(1)) |
| 577 | |
| 578 | " Delete "t" in "two" and "three" to XX: 'oXXe wo hree' |
| 579 | s/t//g |
| 580 | let expected_props[0].length -= 2 |
| 581 | let expected_props[2].length -= 1 |
| 582 | let expected_props[3].length -= 1 |
| 583 | let expected_props[3].col -= 1 |
| 584 | call assert_equal(expected_props, prop_list(1)) |
| 585 | |
| 586 | " Split the line by changing w to line break: 'oXXe ', 'o hree' |
| 587 | " The long prop is split and spans both lines. |
| 588 | " The props on "two" and "three" move to the next line. |
| 589 | s/w/\r/ |
| 590 | let new_props = [ |
| 591 | \ copy(expected_props[0]), |
| 592 | \ copy(expected_props[2]), |
| 593 | \ copy(expected_props[3]), |
| 594 | \ ] |
| 595 | let expected_props[0].length = 5 |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 596 | let expected_props[0].end = 0 |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 597 | unlet expected_props[3] |
| 598 | unlet expected_props[2] |
| 599 | call assert_equal(expected_props, prop_list(1)) |
| 600 | |
| 601 | let new_props[0].length = 6 |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 602 | let new_props[0].start = 0 |
Bram Moolenaar | 4164bb2 | 2019-01-04 23:09:49 +0100 | [diff] [blame] | 603 | let new_props[1].col = 1 |
| 604 | let new_props[1].length = 1 |
| 605 | let new_props[2].col = 3 |
| 606 | call assert_equal(new_props, prop_list(2)) |
| 607 | |
| 608 | call DeletePropTypes() |
| 609 | bwipe! |
| 610 | endfunc |
| 611 | |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 612 | func Test_prop_change_indent() |
| 613 | call prop_type_add('comment', {'highlight': 'Directory'}) |
| 614 | new |
| 615 | call setline(1, [' xxx', 'yyyyy']) |
| 616 | call prop_add(2, 2, {'length': 2, 'type': 'comment'}) |
| 617 | let expect = {'col': 2, 'length': 2, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0} |
| 618 | call assert_equal([expect], prop_list(2)) |
| 619 | |
| 620 | set shiftwidth=3 |
| 621 | normal 2G>> |
| 622 | call assert_equal(' yyyyy', getline(2)) |
| 623 | let expect.col += 3 |
| 624 | call assert_equal([expect], prop_list(2)) |
| 625 | |
| 626 | normal 2G== |
| 627 | call assert_equal(' yyyyy', getline(2)) |
| 628 | let expect.col = 6 |
| 629 | call assert_equal([expect], prop_list(2)) |
| 630 | |
| 631 | call prop_clear(2) |
| 632 | call prop_add(2, 2, {'length': 5, 'type': 'comment'}) |
| 633 | let expect.col = 2 |
| 634 | let expect.length = 5 |
| 635 | call assert_equal([expect], prop_list(2)) |
| 636 | |
| 637 | normal 2G<< |
| 638 | call assert_equal(' yyyyy', getline(2)) |
| 639 | let expect.length = 2 |
| 640 | call assert_equal([expect], prop_list(2)) |
| 641 | |
| 642 | set shiftwidth& |
| 643 | call prop_type_delete('comment') |
| 644 | endfunc |
| 645 | |
Bram Moolenaar | c1a9bc1 | 2018-12-28 21:59:29 +0100 | [diff] [blame] | 646 | " Setup a three line prop in lines 2 - 4. |
| 647 | " Add short props in line 1 and 5. |
| 648 | func Setup_three_line_prop() |
| 649 | new |
| 650 | call setline(1, ['one', 'twotwo', 'three', 'fourfour', 'five']) |
| 651 | call prop_add(1, 2, {'length': 1, 'type': 'comment'}) |
| 652 | call prop_add(2, 4, {'end_lnum': 4, 'end_col': 5, 'type': 'comment'}) |
| 653 | call prop_add(5, 2, {'length': 1, 'type': 'comment'}) |
| 654 | endfunc |
| 655 | |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 656 | func Test_prop_multiline() |
Bram Moolenaar | a5a7882 | 2019-09-04 21:57:18 +0200 | [diff] [blame] | 657 | eval 'comment'->prop_type_add({'highlight': 'Directory'}) |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 658 | new |
| 659 | call setline(1, ['xxxxxxx', 'yyyyyyyyy', 'zzzzzzzz']) |
| 660 | |
| 661 | " start halfway line 1, end halfway line 3 |
| 662 | call prop_add(1, 3, {'end_lnum': 3, 'end_col': 5, 'type': 'comment'}) |
| 663 | let expect1 = {'col': 3, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0} |
| 664 | call assert_equal([expect1], prop_list(1)) |
| 665 | let expect2 = {'col': 1, 'length': 10, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0} |
| 666 | call assert_equal([expect2], prop_list(2)) |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 667 | let expect3 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0} |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 668 | call assert_equal([expect3], prop_list(3)) |
| 669 | call prop_clear(1, 3) |
| 670 | |
| 671 | " include all three lines |
| 672 | call prop_add(1, 1, {'end_lnum': 3, 'end_col': 999, 'type': 'comment'}) |
| 673 | let expect1.col = 1 |
| 674 | let expect1.length = 8 |
| 675 | call assert_equal([expect1], prop_list(1)) |
| 676 | call assert_equal([expect2], prop_list(2)) |
| 677 | let expect3.length = 9 |
| 678 | call assert_equal([expect3], prop_list(3)) |
| 679 | call prop_clear(1, 3) |
| 680 | |
| 681 | bwipe! |
Bram Moolenaar | c1a9bc1 | 2018-12-28 21:59:29 +0100 | [diff] [blame] | 682 | |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 683 | " Test deleting the first line of a multi-line prop. |
Bram Moolenaar | c1a9bc1 | 2018-12-28 21:59:29 +0100 | [diff] [blame] | 684 | call Setup_three_line_prop() |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 685 | let expect_short = {'col': 2, 'length': 1, 'type': 'comment', 'start': 1, 'end': 1, 'id': 0} |
| 686 | call assert_equal([expect_short], prop_list(1)) |
Bram Moolenaar | c1a9bc1 | 2018-12-28 21:59:29 +0100 | [diff] [blame] | 687 | let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0} |
| 688 | call assert_equal([expect2], prop_list(2)) |
| 689 | 2del |
Bram Moolenaar | c1a9bc1 | 2018-12-28 21:59:29 +0100 | [diff] [blame] | 690 | call assert_equal([expect_short], prop_list(1)) |
| 691 | let expect2 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0} |
| 692 | call assert_equal([expect2], prop_list(2)) |
| 693 | bwipe! |
| 694 | |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 695 | " Test deleting the last line of a multi-line prop. |
Bram Moolenaar | c1a9bc1 | 2018-12-28 21:59:29 +0100 | [diff] [blame] | 696 | call Setup_three_line_prop() |
| 697 | let expect3 = {'col': 1, 'length': 6, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0} |
| 698 | call assert_equal([expect3], prop_list(3)) |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 699 | let expect4 = {'col': 1, 'length': 4, 'type': 'comment', 'start': 0, 'end': 1, 'id': 0} |
Bram Moolenaar | c1a9bc1 | 2018-12-28 21:59:29 +0100 | [diff] [blame] | 700 | call assert_equal([expect4], prop_list(4)) |
| 701 | 4del |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 702 | let expect3.end = 1 |
Bram Moolenaar | c1a9bc1 | 2018-12-28 21:59:29 +0100 | [diff] [blame] | 703 | call assert_equal([expect3], prop_list(3)) |
| 704 | call assert_equal([expect_short], prop_list(4)) |
| 705 | bwipe! |
| 706 | |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 707 | " Test appending a line below the multi-line text prop start. |
Bram Moolenaar | b56ac04 | 2018-12-28 23:22:40 +0100 | [diff] [blame] | 708 | call Setup_three_line_prop() |
| 709 | let expect2 = {'col': 4, 'length': 4, 'type': 'comment', 'start': 1, 'end': 0, 'id': 0} |
| 710 | call assert_equal([expect2], prop_list(2)) |
| 711 | call append(2, "new line") |
| 712 | call assert_equal([expect2], prop_list(2)) |
| 713 | let expect3 = {'col': 1, 'length': 9, 'type': 'comment', 'start': 0, 'end': 0, 'id': 0} |
| 714 | call assert_equal([expect3], prop_list(3)) |
| 715 | bwipe! |
| 716 | |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 717 | call prop_type_delete('comment') |
| 718 | endfunc |
| 719 | |
Bram Moolenaar | 9df53b6 | 2020-01-13 20:40:51 +0100 | [diff] [blame] | 720 | func Test_prop_line2byte() |
Bram Moolenaar | b413d2e | 2018-12-25 23:15:46 +0100 | [diff] [blame] | 721 | call prop_type_add('comment', {'highlight': 'Directory'}) |
| 722 | new |
Bram Moolenaar | 00b1e04 | 2018-12-26 23:42:10 +0100 | [diff] [blame] | 723 | call setline(1, ['line1', 'second line', '']) |
Bram Moolenaar | 8cf734e | 2018-12-26 01:09:00 +0100 | [diff] [blame] | 724 | set ff=unix |
Bram Moolenaar | 00b1e04 | 2018-12-26 23:42:10 +0100 | [diff] [blame] | 725 | call assert_equal(19, line2byte(3)) |
Bram Moolenaar | b413d2e | 2018-12-25 23:15:46 +0100 | [diff] [blame] | 726 | call prop_add(1, 1, {'end_col': 3, 'type': 'comment'}) |
Bram Moolenaar | 00b1e04 | 2018-12-26 23:42:10 +0100 | [diff] [blame] | 727 | call assert_equal(19, line2byte(3)) |
Bram Moolenaar | b413d2e | 2018-12-25 23:15:46 +0100 | [diff] [blame] | 728 | |
| 729 | bwipe! |
| 730 | call prop_type_delete('comment') |
| 731 | endfunc |
| 732 | |
Bram Moolenaar | 9df53b6 | 2020-01-13 20:40:51 +0100 | [diff] [blame] | 733 | func Test_prop_byte2line() |
| 734 | new |
| 735 | set ff=unix |
| 736 | call setline(1, ['one one', 'two two', 'three three', 'four four', 'five']) |
| 737 | call assert_equal(4, byte2line(line2byte(4))) |
| 738 | call assert_equal(5, byte2line(line2byte(5))) |
| 739 | |
| 740 | call prop_type_add('prop', {'highlight': 'Directory'}) |
| 741 | call prop_add(3, 1, {'length': 5, 'type': 'prop'}) |
| 742 | call assert_equal(4, byte2line(line2byte(4))) |
| 743 | call assert_equal(5, byte2line(line2byte(5))) |
| 744 | |
| 745 | bwipe! |
| 746 | call prop_type_delete('prop') |
| 747 | endfunc |
| 748 | |
Bram Moolenaar | 7f1664e | 2019-01-04 17:21:24 +0100 | [diff] [blame] | 749 | func Test_prop_undo() |
| 750 | new |
| 751 | call prop_type_add('comment', {'highlight': 'Directory'}) |
| 752 | call setline(1, ['oneone', 'twotwo', 'three']) |
| 753 | " Set 'undolevels' to break changes into undo-able pieces. |
| 754 | set ul& |
| 755 | |
| 756 | call prop_add(1, 3, {'end_col': 5, 'type': 'comment'}) |
| 757 | let expected = [{'col': 3, 'length': 2, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ] |
| 758 | call assert_equal(expected, prop_list(1)) |
| 759 | |
| 760 | " Insert a character, then undo. |
| 761 | exe "normal 0lllix\<Esc>" |
| 762 | set ul& |
| 763 | let expected[0].length = 3 |
| 764 | call assert_equal(expected, prop_list(1)) |
| 765 | undo |
| 766 | let expected[0].length = 2 |
| 767 | call assert_equal(expected, prop_list(1)) |
| 768 | |
| 769 | " Delete a character, then undo |
| 770 | exe "normal 0lllx" |
| 771 | set ul& |
| 772 | let expected[0].length = 1 |
| 773 | call assert_equal(expected, prop_list(1)) |
| 774 | undo |
| 775 | let expected[0].length = 2 |
| 776 | call assert_equal(expected, prop_list(1)) |
| 777 | |
| 778 | " Delete the line, then undo |
| 779 | 1d |
| 780 | set ul& |
| 781 | call assert_equal([], prop_list(1)) |
| 782 | undo |
| 783 | call assert_equal(expected, prop_list(1)) |
| 784 | |
| 785 | " Insert a character, delete two characters, then undo with "U" |
| 786 | exe "normal 0lllix\<Esc>" |
| 787 | set ul& |
| 788 | let expected[0].length = 3 |
| 789 | call assert_equal(expected, prop_list(1)) |
| 790 | exe "normal 0lllxx" |
| 791 | set ul& |
| 792 | let expected[0].length = 1 |
| 793 | call assert_equal(expected, prop_list(1)) |
| 794 | normal U |
| 795 | let expected[0].length = 2 |
| 796 | call assert_equal(expected, prop_list(1)) |
| 797 | |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 798 | " substitute a word, then undo |
| 799 | call setline(1, 'the number 123 is highlighted.') |
| 800 | call prop_add(1, 12, {'length': 3, 'type': 'comment'}) |
| 801 | let expected = [{'col': 12, 'length': 3, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ] |
| 802 | call assert_equal(expected, prop_list(1)) |
| 803 | set ul& |
| 804 | 1s/number/foo |
| 805 | let expected[0].col = 9 |
| 806 | call assert_equal(expected, prop_list(1)) |
| 807 | undo |
| 808 | let expected[0].col = 12 |
| 809 | call assert_equal(expected, prop_list(1)) |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 810 | call prop_clear(1) |
| 811 | |
| 812 | " substitute with backslash |
| 813 | call setline(1, 'the number 123 is highlighted.') |
| 814 | call prop_add(1, 12, {'length': 3, 'type': 'comment'}) |
| 815 | let expected = [{'col': 12, 'length': 3, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ] |
| 816 | call assert_equal(expected, prop_list(1)) |
| 817 | 1s/the/\The |
| 818 | call assert_equal(expected, prop_list(1)) |
| 819 | 1s/^/\\ |
| 820 | let expected[0].col += 1 |
| 821 | call assert_equal(expected, prop_list(1)) |
| 822 | 1s/^/\~ |
| 823 | let expected[0].col += 1 |
| 824 | call assert_equal(expected, prop_list(1)) |
| 825 | 1s/123/12\\3 |
| 826 | let expected[0].length += 1 |
| 827 | call assert_equal(expected, prop_list(1)) |
| 828 | call prop_clear(1) |
Bram Moolenaar | 338dfda | 2019-05-19 15:19:57 +0200 | [diff] [blame] | 829 | |
Bram Moolenaar | 7f1664e | 2019-01-04 17:21:24 +0100 | [diff] [blame] | 830 | bwipe! |
| 831 | call prop_type_delete('comment') |
| 832 | endfunc |
| 833 | |
Bram Moolenaar | ecafcc1 | 2019-11-16 20:41:51 +0100 | [diff] [blame] | 834 | func Test_prop_delete_text() |
| 835 | new |
| 836 | call prop_type_add('comment', {'highlight': 'Directory'}) |
| 837 | call setline(1, ['oneone', 'twotwo', 'three']) |
| 838 | |
| 839 | " zero length property |
| 840 | call prop_add(1, 3, {'type': 'comment'}) |
| 841 | let expected = [{'col': 3, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ] |
| 842 | call assert_equal(expected, prop_list(1)) |
| 843 | |
| 844 | " delete one char moves the property |
| 845 | normal! x |
| 846 | let expected = [{'col': 2, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ] |
| 847 | call assert_equal(expected, prop_list(1)) |
| 848 | |
| 849 | " delete char of the property has no effect |
| 850 | normal! lx |
| 851 | let expected = [{'col': 2, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ] |
| 852 | call assert_equal(expected, prop_list(1)) |
| 853 | |
| 854 | " delete more chars moves property to first column, is not deleted |
| 855 | normal! 0xxxx |
| 856 | let expected = [{'col': 1, 'length': 0, 'id': 0, 'type': 'comment', 'start': 1, 'end': 1} ] |
| 857 | call assert_equal(expected, prop_list(1)) |
| 858 | |
| 859 | bwipe! |
| 860 | call prop_type_delete('comment') |
| 861 | endfunc |
| 862 | |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 863 | " screenshot test with textprop highlighting |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 864 | func Test_textprop_screenshot_various() |
Bram Moolenaar | 3439028 | 2019-10-16 14:38:26 +0200 | [diff] [blame] | 865 | CheckScreendump |
Bram Moolenaar | ed79d1e | 2019-02-22 14:38:58 +0100 | [diff] [blame] | 866 | " The Vim running in the terminal needs to use utf-8. |
Bram Moolenaar | 3439028 | 2019-10-16 14:38:26 +0200 | [diff] [blame] | 867 | if g:orig_encoding != 'utf-8' |
| 868 | throw 'Skipped: not using utf-8' |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 869 | endif |
| 870 | call writefile([ |
Bram Moolenaar | de24a87 | 2019-05-05 15:48:00 +0200 | [diff] [blame] | 871 | \ "call setline(1, [" |
| 872 | \ .. "'One two'," |
| 873 | \ .. "'Numbér 123 änd thœn 4¾7.'," |
| 874 | \ .. "'--aa--bb--cc--dd--'," |
| 875 | \ .. "'// comment with error in it'," |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 876 | \ .. "'first line'," |
| 877 | \ .. "' second line '," |
| 878 | \ .. "'third line'," |
| 879 | \ .. "' fourth line'," |
Bram Moolenaar | de24a87 | 2019-05-05 15:48:00 +0200 | [diff] [blame] | 880 | \ .. "])", |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 881 | \ "hi NumberProp ctermfg=blue", |
| 882 | \ "hi LongProp ctermbg=yellow", |
Bram Moolenaar | de24a87 | 2019-05-05 15:48:00 +0200 | [diff] [blame] | 883 | \ "hi BackgroundProp ctermbg=lightgrey", |
| 884 | \ "hi UnderlineProp cterm=underline", |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 885 | \ "call prop_type_add('number', {'highlight': 'NumberProp'})", |
Bram Moolenaar | a5a7882 | 2019-09-04 21:57:18 +0200 | [diff] [blame] | 886 | \ "call prop_type_add('long', {'highlight': 'NumberProp'})", |
| 887 | \ "call prop_type_change('long', {'highlight': 'LongProp'})", |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 888 | \ "call prop_type_add('start', {'highlight': 'NumberProp', 'start_incl': 1})", |
| 889 | \ "call prop_type_add('end', {'highlight': 'NumberProp', 'end_incl': 1})", |
| 890 | \ "call prop_type_add('both', {'highlight': 'NumberProp', 'start_incl': 1, 'end_incl': 1})", |
Bram Moolenaar | dbd4316 | 2019-11-09 21:28:14 +0100 | [diff] [blame] | 891 | \ "call prop_type_add('background', {'highlight': 'BackgroundProp', 'combine': 0})", |
| 892 | \ "call prop_type_add('backgroundcomb', {'highlight': 'NumberProp', 'combine': 1})", |
| 893 | \ "eval 'backgroundcomb'->prop_type_change({'highlight': 'BackgroundProp'})", |
Bram Moolenaar | 58e32ab | 2019-11-12 22:44:22 +0100 | [diff] [blame] | 894 | \ "call prop_type_add('error', {'highlight': 'UnderlineProp'})", |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 895 | \ "call prop_add(1, 4, {'end_lnum': 3, 'end_col': 3, 'type': 'long'})", |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 896 | \ "call prop_add(2, 9, {'length': 3, 'type': 'number'})", |
| 897 | \ "call prop_add(2, 24, {'length': 4, 'type': 'number'})", |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 898 | \ "call prop_add(3, 3, {'length': 2, 'type': 'number'})", |
| 899 | \ "call prop_add(3, 7, {'length': 2, 'type': 'start'})", |
| 900 | \ "call prop_add(3, 11, {'length': 2, 'type': 'end'})", |
| 901 | \ "call prop_add(3, 15, {'length': 2, 'type': 'both'})", |
Bram Moolenaar | dbd4316 | 2019-11-09 21:28:14 +0100 | [diff] [blame] | 902 | \ "call prop_add(4, 6, {'length': 3, 'type': 'background'})", |
| 903 | \ "call prop_add(4, 12, {'length': 10, 'type': 'backgroundcomb'})", |
Bram Moolenaar | de24a87 | 2019-05-05 15:48:00 +0200 | [diff] [blame] | 904 | \ "call prop_add(4, 17, {'length': 5, 'type': 'error'})", |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 905 | \ "call prop_add(5, 7, {'length': 4, 'type': 'long'})", |
| 906 | \ "call prop_add(6, 1, {'length': 8, 'type': 'long'})", |
| 907 | \ "call prop_add(8, 1, {'length': 1, 'type': 'long'})", |
| 908 | \ "call prop_add(8, 11, {'length': 4, 'type': 'long'})", |
Bram Moolenaar | bfd4512 | 2019-05-17 13:05:07 +0200 | [diff] [blame] | 909 | \ "set number cursorline", |
Bram Moolenaar | b9c67a5 | 2019-01-01 19:49:20 +0100 | [diff] [blame] | 910 | \ "hi clear SpellBad", |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 911 | \ "set spell", |
Bram Moolenaar | de24a87 | 2019-05-05 15:48:00 +0200 | [diff] [blame] | 912 | \ "syn match Comment '//.*'", |
| 913 | \ "hi Comment ctermfg=green", |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 914 | \ "normal 3G0llix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>lllix\<Esc>", |
Bram Moolenaar | 33c8ca9 | 2019-01-02 18:00:27 +0100 | [diff] [blame] | 915 | \ "normal 3G0lli\<BS>\<Esc>", |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 916 | \ "normal 6G0i\<BS>\<Esc>", |
| 917 | \ "normal 3J", |
| 918 | \ "normal 3G", |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 919 | \], 'XtestProp') |
Bram Moolenaar | 80e737c | 2019-05-17 19:56:34 +0200 | [diff] [blame] | 920 | let buf = RunVimInTerminal('-S XtestProp', {'rows': 8}) |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 921 | call VerifyScreenDump(buf, 'Test_textprop_01', {}) |
Bram Moolenaar | e3d31b0 | 2018-12-24 23:07:04 +0100 | [diff] [blame] | 922 | |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 923 | " clean up |
| 924 | call StopVimInTerminal(buf) |
Bram Moolenaar | 2f21fa8 | 2018-12-31 20:05:56 +0100 | [diff] [blame] | 925 | call delete('XtestProp') |
Bram Moolenaar | c6d86dc | 2018-12-31 13:57:36 +0100 | [diff] [blame] | 926 | endfunc |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 927 | |
| 928 | func RunTestVisualBlock(width, dump) |
| 929 | call writefile([ |
| 930 | \ "call setline(1, [" |
| 931 | \ .. "'xxxxxxxxx 123 x'," |
| 932 | \ .. "'xxxxxxxx 123 x'," |
| 933 | \ .. "'xxxxxxx 123 x'," |
| 934 | \ .. "'xxxxxx 123 x'," |
| 935 | \ .. "'xxxxx 123 x'," |
| 936 | \ .. "'xxxx 123 xx'," |
| 937 | \ .. "'xxx 123 xxx'," |
| 938 | \ .. "'xx 123 xxxx'," |
| 939 | \ .. "'x 123 xxxxx'," |
| 940 | \ .. "' 123 xxxxxx'," |
| 941 | \ .. "])", |
| 942 | \ "hi SearchProp ctermbg=yellow", |
| 943 | \ "call prop_type_add('search', {'highlight': 'SearchProp'})", |
| 944 | \ "call prop_add(1, 11, {'length': 3, 'type': 'search'})", |
| 945 | \ "call prop_add(2, 10, {'length': 3, 'type': 'search'})", |
| 946 | \ "call prop_add(3, 9, {'length': 3, 'type': 'search'})", |
| 947 | \ "call prop_add(4, 8, {'length': 3, 'type': 'search'})", |
| 948 | \ "call prop_add(5, 7, {'length': 3, 'type': 'search'})", |
| 949 | \ "call prop_add(6, 6, {'length': 3, 'type': 'search'})", |
| 950 | \ "call prop_add(7, 5, {'length': 3, 'type': 'search'})", |
| 951 | \ "call prop_add(8, 4, {'length': 3, 'type': 'search'})", |
| 952 | \ "call prop_add(9, 3, {'length': 3, 'type': 'search'})", |
| 953 | \ "call prop_add(10, 2, {'length': 3, 'type': 'search'})", |
| 954 | \ "normal 1G6|\<C-V>" .. repeat('l', a:width - 1) .. "10jx", |
| 955 | \], 'XtestPropVis') |
| 956 | let buf = RunVimInTerminal('-S XtestPropVis', {'rows': 12}) |
| 957 | call VerifyScreenDump(buf, 'Test_textprop_vis_' .. a:dump, {}) |
| 958 | |
| 959 | " clean up |
| 960 | call StopVimInTerminal(buf) |
| 961 | call delete('XtestPropVis') |
| 962 | endfunc |
| 963 | |
| 964 | " screenshot test with Visual block mode operations |
| 965 | func Test_textprop_screenshot_visual() |
Bram Moolenaar | 3439028 | 2019-10-16 14:38:26 +0200 | [diff] [blame] | 966 | CheckScreendump |
Bram Moolenaar | 8055d17 | 2019-05-17 22:57:26 +0200 | [diff] [blame] | 967 | |
| 968 | " Delete two columns while text props are three chars wide. |
| 969 | call RunTestVisualBlock(2, '01') |
| 970 | |
| 971 | " Same, but delete four columns |
| 972 | call RunTestVisualBlock(4, '02') |
| 973 | endfunc |
Bram Moolenaar | d79eef2 | 2019-05-24 20:41:55 +0200 | [diff] [blame] | 974 | |
Bram Moolenaar | a956bf6 | 2019-06-19 17:34:24 +0200 | [diff] [blame] | 975 | func Test_textprop_after_tab() |
Bram Moolenaar | 3439028 | 2019-10-16 14:38:26 +0200 | [diff] [blame] | 976 | CheckScreendump |
Bram Moolenaar | 37e66cf | 2019-06-19 18:16:10 +0200 | [diff] [blame] | 977 | |
Bram Moolenaar | a956bf6 | 2019-06-19 17:34:24 +0200 | [diff] [blame] | 978 | let lines =<< trim END |
| 979 | call setline(1, [ |
| 980 | \ "\txxx", |
| 981 | \ "x\txxx", |
| 982 | \ ]) |
| 983 | hi SearchProp ctermbg=yellow |
| 984 | call prop_type_add('search', {'highlight': 'SearchProp'}) |
| 985 | call prop_add(1, 2, {'length': 3, 'type': 'search'}) |
| 986 | call prop_add(2, 3, {'length': 3, 'type': 'search'}) |
| 987 | END |
| 988 | call writefile(lines, 'XtestPropTab') |
| 989 | let buf = RunVimInTerminal('-S XtestPropTab', {'rows': 6}) |
| 990 | call VerifyScreenDump(buf, 'Test_textprop_tab', {}) |
| 991 | |
| 992 | " clean up |
| 993 | call StopVimInTerminal(buf) |
| 994 | call delete('XtestPropTab') |
| 995 | endfunc |
| 996 | |
Bram Moolenaar | 3439028 | 2019-10-16 14:38:26 +0200 | [diff] [blame] | 997 | func Test_textprop_with_syntax() |
| 998 | CheckScreendump |
| 999 | |
| 1000 | let lines =<< trim END |
| 1001 | call setline(1, [ |
| 1002 | \ "(abc)", |
| 1003 | \ ]) |
| 1004 | syn match csParens "[()]" display |
| 1005 | hi! link csParens MatchParen |
| 1006 | |
| 1007 | call prop_type_add('TPTitle', #{ highlight: 'Title' }) |
| 1008 | call prop_add(1, 2, #{type: 'TPTitle', end_col: 5}) |
| 1009 | END |
| 1010 | call writefile(lines, 'XtestPropSyn') |
| 1011 | let buf = RunVimInTerminal('-S XtestPropSyn', {'rows': 6}) |
| 1012 | call VerifyScreenDump(buf, 'Test_textprop_syn_1', {}) |
| 1013 | |
| 1014 | " clean up |
| 1015 | call StopVimInTerminal(buf) |
| 1016 | call delete('XtestPropSyn') |
| 1017 | endfunc |
| 1018 | |
Bram Moolenaar | d79eef2 | 2019-05-24 20:41:55 +0200 | [diff] [blame] | 1019 | " Adding a text property to a new buffer should not fail |
| 1020 | func Test_textprop_empty_buffer() |
| 1021 | call prop_type_add('comment', {'highlight': 'Search'}) |
| 1022 | new |
| 1023 | call prop_add(1, 1, {'type': 'comment'}) |
| 1024 | close |
Bram Moolenaar | adfde11 | 2019-05-25 22:11:45 +0200 | [diff] [blame] | 1025 | call prop_type_delete('comment') |
| 1026 | endfunc |
| 1027 | |
Bram Moolenaar | d74af42 | 2019-06-28 21:38:00 +0200 | [diff] [blame] | 1028 | " Adding a text property with invalid highlight should be ignored. |
| 1029 | func Test_textprop_invalid_highlight() |
| 1030 | call assert_fails("call prop_type_add('dni', {'highlight': 'DoesNotExist'})", 'E970:') |
| 1031 | new |
| 1032 | call setline(1, ['asdf','asdf']) |
| 1033 | call prop_add(1, 1, {'length': 4, 'type': 'dni'}) |
| 1034 | redraw |
| 1035 | bwipe! |
| 1036 | call prop_type_delete('dni') |
| 1037 | endfunc |
| 1038 | |
Bram Moolenaar | adfde11 | 2019-05-25 22:11:45 +0200 | [diff] [blame] | 1039 | " Adding a text property to an empty buffer and then editing another |
| 1040 | func Test_textprop_empty_buffer_next() |
| 1041 | call prop_type_add("xxx", {}) |
| 1042 | call prop_add(1, 1, {"type": "xxx"}) |
| 1043 | next X |
| 1044 | call prop_type_delete('xxx') |
Bram Moolenaar | d79eef2 | 2019-05-24 20:41:55 +0200 | [diff] [blame] | 1045 | endfunc |
Bram Moolenaar | f0884c5 | 2019-05-24 21:22:29 +0200 | [diff] [blame] | 1046 | |
| 1047 | func Test_textprop_remove_from_buf() |
| 1048 | new |
| 1049 | let buf = bufnr('') |
| 1050 | call prop_type_add('one', {'bufnr': buf}) |
| 1051 | call prop_add(1, 1, {'type': 'one', 'id': 234}) |
| 1052 | file x |
| 1053 | edit y |
| 1054 | call prop_remove({'id': 234, 'bufnr': buf}, 1) |
| 1055 | call prop_type_delete('one', {'bufnr': buf}) |
| 1056 | bwipe! x |
| 1057 | close |
| 1058 | endfunc |
Bram Moolenaar | 45311b5 | 2019-08-13 22:27:32 +0200 | [diff] [blame] | 1059 | |
| 1060 | func Test_textprop_in_unloaded_buf() |
| 1061 | edit Xaaa |
| 1062 | call setline(1, 'aaa') |
| 1063 | write |
| 1064 | edit Xbbb |
| 1065 | call setline(1, 'bbb') |
| 1066 | write |
| 1067 | let bnr = bufnr('') |
| 1068 | edit Xaaa |
| 1069 | |
| 1070 | call prop_type_add('ErrorMsg', #{highlight:'ErrorMsg'}) |
| 1071 | call assert_fails("call prop_add(1, 1, #{end_lnum: 1, endcol: 2, type: 'ErrorMsg', bufnr: bnr})", 'E275:') |
| 1072 | exe 'buf ' .. bnr |
| 1073 | call assert_equal('bbb', getline(1)) |
| 1074 | call assert_equal(0, prop_list(1)->len()) |
| 1075 | |
| 1076 | bwipe! Xaaa |
| 1077 | bwipe! Xbbb |
| 1078 | cal delete('Xaaa') |
| 1079 | cal delete('Xbbb') |
| 1080 | endfunc |
Bram Moolenaar | 1fd30d7 | 2019-10-25 22:13:29 +0200 | [diff] [blame] | 1081 | |
| 1082 | func Test_proptype_substitute2() |
| 1083 | new |
| 1084 | " text_prop.vim |
| 1085 | call setline(1, [ |
| 1086 | \ 'The num 123 is smaller than 4567.', |
| 1087 | \ '123 The number 123 is smaller than 4567.', |
| 1088 | \ '123 The number 123 is smaller than 4567.']) |
| 1089 | |
| 1090 | call prop_type_add('number', {'highlight': 'ErrorMsg'}) |
| 1091 | |
| 1092 | call prop_add(1, 12, {'length': 3, 'type': 'number'}) |
| 1093 | call prop_add(2, 1, {'length': 3, 'type': 'number'}) |
| 1094 | call prop_add(3, 36, {'length': 4, 'type': 'number'}) |
| 1095 | set ul& |
| 1096 | let expected = [{'id': 0, 'col': 13, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}, |
| 1097 | \ {'id': 0, 'col': 1, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}, |
| 1098 | \ {'id': 0, 'col': 50, 'end': 1, 'type': 'number', 'length': 4, 'start': 1}] |
| 1099 | " Add some text in between |
| 1100 | %s/\s\+/ /g |
| 1101 | call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3)) |
| 1102 | |
| 1103 | " remove some text |
| 1104 | :1s/[a-z]\{3\}//g |
| 1105 | let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}] |
| 1106 | call assert_equal(expected, prop_list(1)) |
| 1107 | bwipe! |
| 1108 | endfunc |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1109 | |
Bram Moolenaar | ac15fd8 | 2020-01-09 21:35:48 +0100 | [diff] [blame] | 1110 | func SaveOptions() |
| 1111 | let d = #{tabstop: &tabstop, |
| 1112 | \ softtabstop: &softtabstop, |
| 1113 | \ shiftwidth: &shiftwidth, |
| 1114 | \ expandtab: &expandtab, |
| 1115 | \ foldmethod: '"' .. &foldmethod .. '"', |
| 1116 | \ } |
| 1117 | return d |
| 1118 | endfunc |
| 1119 | |
| 1120 | func RestoreOptions(dict) |
| 1121 | for name in keys(a:dict) |
| 1122 | exe 'let &' .. name .. ' = ' .. a:dict[name] |
| 1123 | endfor |
| 1124 | endfunc |
| 1125 | |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1126 | func Test_textprop_noexpandtab() |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1127 | new |
Bram Moolenaar | ac15fd8 | 2020-01-09 21:35:48 +0100 | [diff] [blame] | 1128 | let save_dict = SaveOptions() |
| 1129 | |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1130 | set tabstop=8 |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1131 | set softtabstop=4 |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1132 | set shiftwidth=4 |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1133 | set noexpandtab |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1134 | set foldmethod=marker |
Bram Moolenaar | ac15fd8 | 2020-01-09 21:35:48 +0100 | [diff] [blame] | 1135 | |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1136 | call feedkeys("\<esc>\<esc>0Ca\<cr>\<esc>\<up>", "tx") |
| 1137 | call prop_type_add('test', {'highlight': 'ErrorMsg'}) |
| 1138 | call prop_add(1, 1, {'end_col': 2, 'type': 'test'}) |
| 1139 | call feedkeys("0i\<tab>", "tx") |
| 1140 | call prop_remove({'type': 'test'}) |
| 1141 | call prop_add(1, 2, {'end_col': 3, 'type': 'test'}) |
| 1142 | call feedkeys("A\<left>\<tab>", "tx") |
| 1143 | call prop_remove({'type': 'test'}) |
| 1144 | try |
| 1145 | " It is correct that this does not pass |
| 1146 | call prop_add(1, 6, {'end_col': 7, 'type': 'test'}) |
| 1147 | " Has already collapsed here, start_col:6 does not result in an error |
| 1148 | call feedkeys("A\<left>\<tab>", "tx") |
| 1149 | catch /^Vim\%((\a\+)\)\=:E964/ |
| 1150 | endtry |
| 1151 | call prop_remove({'type': 'test'}) |
Bram Moolenaar | ac15fd8 | 2020-01-09 21:35:48 +0100 | [diff] [blame] | 1152 | call prop_type_delete('test') |
| 1153 | |
| 1154 | call RestoreOptions(save_dict) |
| 1155 | bwipe! |
| 1156 | endfunc |
| 1157 | |
| 1158 | func Test_textprop_noexpandtab_redraw() |
| 1159 | new |
| 1160 | let save_dict = SaveOptions() |
| 1161 | |
| 1162 | set tabstop=8 |
| 1163 | set softtabstop=4 |
| 1164 | set shiftwidth=4 |
| 1165 | set noexpandtab |
| 1166 | set foldmethod=marker |
| 1167 | |
| 1168 | call feedkeys("\<esc>\<esc>0Ca\<cr>\<space>\<esc>\<up>", "tx") |
| 1169 | call prop_type_add('test', {'highlight': 'ErrorMsg'}) |
| 1170 | call prop_add(1, 1, {'end_col': 2, 'type': 'test'}) |
| 1171 | call feedkeys("0i\<tab>", "tx") |
| 1172 | " Internally broken at the next line |
| 1173 | call feedkeys("A\<left>\<tab>", "tx") |
| 1174 | redraw |
| 1175 | " Index calculation failed internally on next line |
| 1176 | call prop_add(1, 1, {'end_col': 2, 'type': 'test'}) |
| 1177 | call prop_remove({'type': 'test', 'all': v:true}) |
| 1178 | call prop_type_delete('test') |
| 1179 | call prop_type_delete('test') |
| 1180 | |
| 1181 | call RestoreOptions(save_dict) |
| 1182 | bwipe! |
| 1183 | endfunc |
| 1184 | |
| 1185 | func Test_textprop_ins_str() |
| 1186 | new |
| 1187 | call setline(1, 'just some text') |
| 1188 | call prop_type_add('test', {'highlight': 'ErrorMsg'}) |
| 1189 | call prop_add(1, 1, {'end_col': 2, 'type': 'test'}) |
| 1190 | call assert_equal([{'id': 0, 'col': 1, 'end': 1, 'type': 'test', 'length': 1, 'start': 1}], prop_list(1)) |
| 1191 | |
| 1192 | call feedkeys("foi\<F8>\<Esc>", "tx") |
| 1193 | call assert_equal('just s<F8>ome text', getline(1)) |
| 1194 | call assert_equal([{'id': 0, 'col': 1, 'end': 1, 'type': 'test', 'length': 1, 'start': 1}], prop_list(1)) |
| 1195 | |
| 1196 | bwipe! |
| 1197 | call prop_remove({'type': 'test'}) |
| 1198 | call prop_type_delete('test') |
Bram Moolenaar | 5cb0b93 | 2020-01-03 21:25:59 +0100 | [diff] [blame] | 1199 | endfunc |
Bram Moolenaar | 66b9885 | 2020-03-11 19:15:52 +0100 | [diff] [blame] | 1200 | |
| 1201 | func Test_find_prop_later_in_line() |
| 1202 | new |
| 1203 | call prop_type_add('test', {'highlight': 'ErrorMsg'}) |
| 1204 | call setline(1, 'just some text') |
| 1205 | call prop_add(1, 1, {'length': 4, 'type': 'test'}) |
| 1206 | call prop_add(1, 10, {'length': 3, 'type': 'test'}) |
| 1207 | |
| 1208 | call assert_equal({'id': 0, 'lnum': 1, 'col': 10, 'end': 1, 'type': 'test', 'length': 3, 'start': 1}, |
| 1209 | \ prop_find(#{type: 'test', lnum: 1, col: 6})) |
| 1210 | |
| 1211 | bwipe! |
| 1212 | call prop_type_delete('test') |
| 1213 | endfunc |
| 1214 | |
| 1215 | func Test_find_zerowidth_prop_sol() |
| 1216 | new |
| 1217 | call prop_type_add('test', {'highlight': 'ErrorMsg'}) |
| 1218 | call setline(1, 'just some text') |
| 1219 | call prop_add(1, 1, {'length': 0, 'type': 'test'}) |
| 1220 | |
| 1221 | call assert_equal({'id': 0, 'lnum': 1, 'col': 1, 'end': 1, 'type': 'test', 'length': 0, 'start': 1}, |
| 1222 | \ prop_find(#{type: 'test', lnum: 1})) |
| 1223 | |
| 1224 | bwipe! |
| 1225 | call prop_type_delete('test') |
| 1226 | endfunc |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 1227 | |
| 1228 | " Test for passing invalid arguments to prop_xxx() functions |
| 1229 | func Test_prop_func_invalid_args() |
| 1230 | call assert_fails('call prop_clear(1, 2, [])', 'E715:') |
| 1231 | call assert_fails('call prop_clear(-1, 2)', 'E16:') |
| 1232 | call assert_fails('call prop_find(test_null_dict())', 'E474:') |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1233 | call assert_fails('call prop_find({"bufnr" : []})', 'E730:') |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 1234 | call assert_fails('call prop_find({})', 'E968:') |
| 1235 | call assert_fails('call prop_find({}, "x")', 'E474:') |
| 1236 | call assert_fails('call prop_find({"lnum" : -2})', 'E16:') |
| 1237 | call assert_fails('call prop_list(1, [])', 'E715:') |
Bram Moolenaar | 9d48956 | 2020-07-30 20:08:50 +0200 | [diff] [blame] | 1238 | call assert_fails('call prop_list(-1, {})', 'E16:') |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 1239 | call assert_fails('call prop_remove([])', 'E474:') |
| 1240 | call assert_fails('call prop_remove({}, -2)', 'E16:') |
| 1241 | call assert_fails('call prop_remove({})', 'E968:') |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1242 | call assert_fails('call prop_type_add([], {})', 'E730:') |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 1243 | call assert_fails("call prop_type_change('long', {'xyz' : 10})", 'E971:') |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1244 | call assert_fails("call prop_type_delete([])", 'E730:') |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 1245 | call assert_fails("call prop_type_delete('xyz', [])", 'E715:') |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1246 | call assert_fails("call prop_type_get([])", 'E730:') |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 1247 | call assert_fails("call prop_type_get('', [])", 'E474:') |
| 1248 | call assert_fails("call prop_type_list([])", 'E715:') |
| 1249 | endfunc |
| 1250 | |
Bram Moolenaar | 87be9be | 2020-05-30 15:32:02 +0200 | [diff] [blame] | 1251 | func Test_split_join() |
| 1252 | new |
| 1253 | call prop_type_add('test', {'highlight': 'ErrorMsg'}) |
| 1254 | call setline(1, 'just some text') |
| 1255 | call prop_add(1, 6, {'length': 4, 'type': 'test'}) |
| 1256 | |
| 1257 | " Split in middle of "some" |
| 1258 | execute "normal! 8|i\<CR>" |
| 1259 | call assert_equal([{'id': 0, 'col': 6, 'end': 0, 'type': 'test', 'length': 2, 'start': 1}], |
| 1260 | \ prop_list(1)) |
| 1261 | call assert_equal([{'id': 0, 'col': 1, 'end': 1, 'type': 'test', 'length': 2, 'start': 0}], |
| 1262 | \ prop_list(2)) |
| 1263 | |
| 1264 | " Join the two lines back together |
| 1265 | normal! 1GJ |
| 1266 | call assert_equal([{'id': 0, 'col': 6, 'end': 1, 'type': 'test', 'length': 5, 'start': 1}], prop_list(1)) |
| 1267 | |
| 1268 | bwipe! |
| 1269 | call prop_type_delete('test') |
| 1270 | endfunc |
| 1271 | |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 1272 | " vim: shiftwidth=2 sts=2 expandtab |