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