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