Bram Moolenaar | ded2782 | 2017-01-02 14:27:34 +0100 | [diff] [blame] | 1 | " Test for folding |
| 2 | |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 3 | source check.vim |
Bram Moolenaar | 907dad7 | 2018-07-10 15:07:15 +0200 | [diff] [blame] | 4 | source view_util.vim |
Bram Moolenaar | 7701f30 | 2018-10-02 21:20:32 +0200 | [diff] [blame] | 5 | source screendump.vim |
Bram Moolenaar | 907dad7 | 2018-07-10 15:07:15 +0200 | [diff] [blame] | 6 | |
Bram Moolenaar | 94be619 | 2017-04-22 22:40:11 +0200 | [diff] [blame] | 7 | func PrepIndent(arg) |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 8 | return [a:arg] + repeat(["\t".a:arg], 5) |
| 9 | endfu |
| 10 | |
Bram Moolenaar | 94be619 | 2017-04-22 22:40:11 +0200 | [diff] [blame] | 11 | func Test_address_fold() |
Bram Moolenaar | ded2782 | 2017-01-02 14:27:34 +0100 | [diff] [blame] | 12 | new |
| 13 | call setline(1, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/', |
| 14 | \ 'after fold 1', 'after fold 2', 'after fold 3']) |
| 15 | setl fen fdm=marker |
Bram Moolenaar | 518c9b1 | 2017-03-21 11:48:39 +0100 | [diff] [blame] | 16 | " The next commands should all copy the same part of the buffer, |
| 17 | " regardless of the addressing type, since the part to be copied |
Bram Moolenaar | ded2782 | 2017-01-02 14:27:34 +0100 | [diff] [blame] | 18 | " is folded away |
| 19 | :1y |
| 20 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1)) |
| 21 | :.y |
| 22 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1)) |
| 23 | :.+y |
| 24 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1)) |
| 25 | :.,.y |
| 26 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1)) |
| 27 | :sil .1,.y |
| 28 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1)) |
| 29 | " use silent to make E493 go away |
| 30 | :sil .+,.y |
| 31 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1)) |
| 32 | :,y |
| 33 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1)) |
| 34 | :,+y |
| 35 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/','after fold 1'], getreg(0,1,1)) |
| 36 | " using .+3 as second address should copy the whole folded line + the next 3 |
| 37 | " lines |
| 38 | :.,+3y |
| 39 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/', |
| 40 | \ 'after fold 1', 'after fold 2', 'after fold 3'], getreg(0,1,1)) |
| 41 | :sil .,-2y |
| 42 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1)) |
| 43 | |
| 44 | " now test again with folding disabled |
| 45 | set nofoldenable |
| 46 | :1y |
| 47 | call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1)) |
| 48 | :.y |
| 49 | call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1)) |
| 50 | :.+y |
| 51 | call assert_equal(['1'], getreg(0,1,1)) |
| 52 | :.,.y |
| 53 | call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1)) |
| 54 | " use silent to make E493 go away |
| 55 | :sil .1,.y |
| 56 | call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1)) |
| 57 | " use silent to make E493 go away |
| 58 | :sil .+,.y |
| 59 | call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1)) |
| 60 | :,y |
| 61 | call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1)) |
| 62 | :,+y |
| 63 | call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1)) |
| 64 | " using .+3 as second address should copy the whole folded line + the next 3 |
| 65 | " lines |
| 66 | :.,+3y |
| 67 | call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3'], getreg(0,1,1)) |
| 68 | :7 |
| 69 | :sil .,-2y |
| 70 | call assert_equal(['4', '5', '}/*}}}*/'], getreg(0,1,1)) |
| 71 | |
| 72 | quit! |
Bram Moolenaar | 1159b16 | 2017-02-28 21:53:56 +0100 | [diff] [blame] | 73 | endfunc |
Bram Moolenaar | 54b2bfa | 2017-01-02 14:57:08 +0100 | [diff] [blame] | 74 | |
Bram Moolenaar | 94be619 | 2017-04-22 22:40:11 +0200 | [diff] [blame] | 75 | func Test_indent_fold() |
Bram Moolenaar | 54b2bfa | 2017-01-02 14:57:08 +0100 | [diff] [blame] | 76 | new |
| 77 | call setline(1, ['', 'a', ' b', ' c']) |
| 78 | setl fen fdm=indent |
| 79 | 2 |
| 80 | norm! >> |
| 81 | let a=map(range(1,4), 'foldclosed(v:val)') |
| 82 | call assert_equal([-1,-1,-1,-1], a) |
| 83 | bw! |
Bram Moolenaar | 1159b16 | 2017-02-28 21:53:56 +0100 | [diff] [blame] | 84 | endfunc |
Bram Moolenaar | 54b2bfa | 2017-01-02 14:57:08 +0100 | [diff] [blame] | 85 | |
Bram Moolenaar | 94be619 | 2017-04-22 22:40:11 +0200 | [diff] [blame] | 86 | func Test_indent_fold2() |
Bram Moolenaar | 54b2bfa | 2017-01-02 14:57:08 +0100 | [diff] [blame] | 87 | new |
| 88 | call setline(1, ['', '{{{', '}}}', '{{{', '}}}']) |
| 89 | setl fen fdm=marker |
| 90 | 2 |
| 91 | norm! >> |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 92 | let a=map(range(1,5), 'v:val->foldclosed()') |
Bram Moolenaar | 54b2bfa | 2017-01-02 14:57:08 +0100 | [diff] [blame] | 93 | call assert_equal([-1,-1,-1,4,4], a) |
| 94 | bw! |
Bram Moolenaar | 1159b16 | 2017-02-28 21:53:56 +0100 | [diff] [blame] | 95 | endfunc |
| 96 | |
| 97 | func Test_manual_fold_with_filter() |
| 98 | if !executable('cat') |
| 99 | return |
| 100 | endif |
Bram Moolenaar | 3f3897e | 2017-03-04 15:28:53 +0100 | [diff] [blame] | 101 | for type in ['manual', 'marker'] |
| 102 | exe 'set foldmethod=' . type |
| 103 | new |
| 104 | call setline(1, range(1, 20)) |
| 105 | 4,$fold |
| 106 | %foldopen |
| 107 | 10,$fold |
| 108 | %foldopen |
| 109 | " This filter command should not have an effect |
| 110 | 1,8! cat |
| 111 | call feedkeys('5ggzdzMGdd', 'xt') |
| 112 | call assert_equal(['1', '2', '3', '4', '5', '6', '7', '8', '9'], getline(1, '$')) |
| 113 | |
| 114 | bwipe! |
| 115 | set foldmethod& |
| 116 | endfor |
Bram Moolenaar | 1159b16 | 2017-02-28 21:53:56 +0100 | [diff] [blame] | 117 | endfunc |
Bram Moolenaar | eadbc2b | 2017-03-04 18:42:39 +0100 | [diff] [blame] | 118 | |
Bram Moolenaar | 94be619 | 2017-04-22 22:40:11 +0200 | [diff] [blame] | 119 | func Test_indent_fold_with_read() |
Bram Moolenaar | eadbc2b | 2017-03-04 18:42:39 +0100 | [diff] [blame] | 120 | new |
| 121 | set foldmethod=indent |
| 122 | call setline(1, repeat(["\<Tab>a"], 4)) |
| 123 | for n in range(1, 4) |
| 124 | call assert_equal(1, foldlevel(n)) |
| 125 | endfor |
| 126 | |
| 127 | call writefile(["a", "", "\<Tab>a"], 'Xfile') |
| 128 | foldopen |
| 129 | 2read Xfile |
| 130 | %foldclose |
| 131 | call assert_equal(1, foldlevel(1)) |
| 132 | call assert_equal(2, foldclosedend(1)) |
| 133 | call assert_equal(0, foldlevel(3)) |
| 134 | call assert_equal(0, foldlevel(4)) |
| 135 | call assert_equal(1, foldlevel(5)) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 136 | call assert_equal(7, 5->foldclosedend()) |
Bram Moolenaar | eadbc2b | 2017-03-04 18:42:39 +0100 | [diff] [blame] | 137 | |
| 138 | bwipe! |
| 139 | set foldmethod& |
| 140 | call delete('Xfile') |
| 141 | endfunc |
| 142 | |
| 143 | func Test_combining_folds_indent() |
| 144 | new |
| 145 | let one = "\<Tab>a" |
| 146 | let zero = 'a' |
| 147 | call setline(1, [one, one, zero, zero, zero, one, one, one]) |
| 148 | set foldmethod=indent |
| 149 | 3,5d |
| 150 | %foldclose |
| 151 | call assert_equal(5, foldclosedend(1)) |
| 152 | |
| 153 | set foldmethod& |
| 154 | bwipe! |
| 155 | endfunc |
| 156 | |
| 157 | func Test_combining_folds_marker() |
| 158 | new |
| 159 | call setline(1, ['{{{', '}}}', '', '', '', '{{{', '', '}}}']) |
| 160 | set foldmethod=marker |
| 161 | 3,5d |
| 162 | %foldclose |
| 163 | call assert_equal(2, foldclosedend(1)) |
| 164 | |
| 165 | set foldmethod& |
| 166 | bwipe! |
| 167 | endfunc |
| 168 | |
Bram Moolenaar | 025a6b7 | 2017-03-12 20:37:21 +0100 | [diff] [blame] | 169 | func Test_folds_marker_in_comment() |
| 170 | new |
| 171 | call setline(1, ['" foo', 'bar', 'baz']) |
| 172 | setl fen fdm=marker |
| 173 | setl com=sO:\"\ -,mO:\"\ \ ,eO:\"\",:\" cms=\"%s |
| 174 | norm! zf2j |
| 175 | setl nofen |
| 176 | :1y |
| 177 | call assert_equal(['" foo{{{'], getreg(0,1,1)) |
| 178 | :+2y |
| 179 | call assert_equal(['baz"}}}'], getreg(0,1,1)) |
| 180 | |
| 181 | set foldmethod& |
| 182 | bwipe! |
| 183 | endfunc |
| 184 | |
Bram Moolenaar | eadbc2b | 2017-03-04 18:42:39 +0100 | [diff] [blame] | 185 | func s:TestFoldExpr(lnum) |
| 186 | let thisline = getline(a:lnum) |
| 187 | if thisline == 'a' |
| 188 | return 1 |
| 189 | elseif thisline == 'b' |
| 190 | return 0 |
| 191 | elseif thisline == 'c' |
| 192 | return '<1' |
| 193 | elseif thisline == 'd' |
| 194 | return '>1' |
| 195 | endif |
| 196 | return 0 |
| 197 | endfunction |
| 198 | |
| 199 | func Test_update_folds_expr_read() |
| 200 | new |
| 201 | call setline(1, ['a', 'a', 'a', 'a', 'a', 'a']) |
| 202 | set foldmethod=expr |
| 203 | set foldexpr=s:TestFoldExpr(v:lnum) |
| 204 | 2 |
| 205 | foldopen |
| 206 | call writefile(['b', 'b', 'a', 'a', 'd', 'a', 'a', 'c'], 'Xfile') |
| 207 | read Xfile |
| 208 | %foldclose |
| 209 | call assert_equal(2, foldclosedend(1)) |
| 210 | call assert_equal(0, foldlevel(3)) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 211 | call assert_equal(0, 4->foldlevel()) |
Bram Moolenaar | eadbc2b | 2017-03-04 18:42:39 +0100 | [diff] [blame] | 212 | call assert_equal(6, foldclosedend(5)) |
| 213 | call assert_equal(10, foldclosedend(7)) |
| 214 | call assert_equal(14, foldclosedend(11)) |
| 215 | |
| 216 | call delete('Xfile') |
| 217 | bwipe! |
| 218 | set foldmethod& foldexpr& |
| 219 | endfunc |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 220 | |
Bram Moolenaar | 94be619 | 2017-04-22 22:40:11 +0200 | [diff] [blame] | 221 | func Check_foldlevels(expected) |
| 222 | call assert_equal(a:expected, map(range(1, line('$')), 'foldlevel(v:val)')) |
| 223 | endfunc |
| 224 | |
| 225 | func Test_move_folds_around_manual() |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 226 | new |
| 227 | let input = PrepIndent("a") + PrepIndent("b") + PrepIndent("c") |
| 228 | call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c")) |
| 229 | let folds=[-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14] |
| 230 | " all folds closed |
| 231 | set foldenable foldlevel=0 fdm=indent |
| 232 | " needs a forced redraw |
| 233 | redraw! |
| 234 | set fdm=manual |
| 235 | call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)')) |
| 236 | call assert_equal(input, getline(1, '$')) |
| 237 | 7,12m0 |
| 238 | call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$')) |
| 239 | call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)')) |
| 240 | 10,12m0 |
| 241 | call assert_equal(PrepIndent("a")[1:] + PrepIndent("b") + ["a"] + PrepIndent("c"), getline(1, '$')) |
| 242 | call assert_equal([1, 1, 1, 1, 1, -1, 7, 7, 7, 7, 7, -1, -1, 14, 14, 14, 14, 14], map(range(1, line('$')), 'foldclosed(v:val)')) |
| 243 | " moving should not close the folds |
| 244 | %d |
| 245 | call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c")) |
| 246 | set fdm=indent |
| 247 | redraw! |
| 248 | set fdm=manual |
| 249 | call cursor(2, 1) |
Bram Moolenaar | 40ebc0a | 2017-03-16 15:59:14 +0100 | [diff] [blame] | 250 | %foldopen |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 251 | 7,12m0 |
| 252 | let folds=repeat([-1], 18) |
| 253 | call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$')) |
| 254 | call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)')) |
| 255 | norm! zM |
| 256 | " folds are not corrupted and all have been closed |
| 257 | call assert_equal([-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14], map(range(1, line('$')), 'foldclosed(v:val)')) |
| 258 | %d |
| 259 | call setline(1, ["a", "\tb", "\tc", "\td", "\te"]) |
| 260 | set fdm=indent |
| 261 | redraw! |
| 262 | set fdm=manual |
| 263 | %foldopen |
| 264 | 3m4 |
| 265 | %foldclose |
| 266 | call assert_equal(["a", "\tb", "\td", "\tc", "\te"], getline(1, '$')) |
| 267 | call assert_equal([-1, 5, 5, 5, 5], map(range(1, line('$')), 'foldclosedend(v:val)')) |
| 268 | %d |
| 269 | call setline(1, ["a", "\tb", "\tc", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"]) |
| 270 | set fdm=indent foldlevel=0 |
| 271 | set fdm=manual |
| 272 | %foldopen |
| 273 | 3m1 |
| 274 | %foldclose |
| 275 | call assert_equal(["a", "\tc", "\tb", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"], getline(1, '$')) |
| 276 | call assert_equal(0, foldlevel(2)) |
| 277 | call assert_equal(5, foldclosedend(3)) |
| 278 | call assert_equal([-1, -1, 3, 3, 3, -1, 7, 7, 7, 7], map(range(1, line('$')), 'foldclosed(v:val)')) |
| 279 | 2,6m$ |
| 280 | %foldclose |
| 281 | call assert_equal(5, foldclosedend(2)) |
| 282 | call assert_equal(0, foldlevel(6)) |
| 283 | call assert_equal(9, foldclosedend(7)) |
| 284 | call assert_equal([-1, 2, 2, 2, 2, -1, 7, 7, 7, -1], map(range(1, line('$')), 'foldclosed(v:val)')) |
Bram Moolenaar | 495b7dd | 2017-09-16 17:19:22 +0200 | [diff] [blame] | 285 | |
Bram Moolenaar | 40ebc0a | 2017-03-16 15:59:14 +0100 | [diff] [blame] | 286 | %d |
| 287 | " Ensure moving around the edges still works. |
| 288 | call setline(1, PrepIndent("a") + repeat(["a"], 3) + ["\ta"]) |
| 289 | set fdm=indent foldlevel=0 |
| 290 | set fdm=manual |
| 291 | %foldopen |
| 292 | 6m$ |
| 293 | " The first fold has been truncated to the 5'th line. |
| 294 | " Second fold has been moved up because the moved line is now below it. |
Bram Moolenaar | 94be619 | 2017-04-22 22:40:11 +0200 | [diff] [blame] | 295 | call Check_foldlevels([0, 1, 1, 1, 1, 0, 0, 0, 1, 0]) |
| 296 | |
| 297 | %delete |
| 298 | set fdm=indent foldlevel=0 |
| 299 | call setline(1, [ |
| 300 | \ "a", |
| 301 | \ "\ta", |
| 302 | \ "\t\ta", |
| 303 | \ "\t\ta", |
| 304 | \ "\t\ta", |
| 305 | \ "a", |
| 306 | \ "a"]) |
| 307 | set fdm=manual |
| 308 | %foldopen! |
| 309 | 4,5m6 |
| 310 | call Check_foldlevels([0, 1, 2, 0, 0, 0, 0]) |
| 311 | |
| 312 | %delete |
| 313 | set fdm=indent |
| 314 | call setline(1, [ |
| 315 | \ "\ta", |
| 316 | \ "\t\ta", |
| 317 | \ "\t\ta", |
| 318 | \ "\t\ta", |
| 319 | \ "\ta", |
| 320 | \ "\t\ta", |
| 321 | \ "\t\ta", |
| 322 | \ "\t\ta", |
| 323 | \ "\ta", |
| 324 | \ "\t\ta", |
| 325 | \ "\t\ta", |
| 326 | \ "\t\ta", |
| 327 | \ "\t\ta", |
| 328 | \ "\ta", |
| 329 | \ "a"]) |
| 330 | set fdm=manual |
| 331 | %foldopen! |
| 332 | 13m7 |
| 333 | call Check_foldlevels([1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 0]) |
| 334 | |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 335 | bw! |
| 336 | endfunc |
| 337 | |
Bram Moolenaar | 94be619 | 2017-04-22 22:40:11 +0200 | [diff] [blame] | 338 | func Test_move_folds_around_indent() |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 339 | new |
| 340 | let input = PrepIndent("a") + PrepIndent("b") + PrepIndent("c") |
| 341 | call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c")) |
| 342 | let folds=[-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14] |
| 343 | " all folds closed |
| 344 | set fdm=indent |
| 345 | call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)')) |
| 346 | call assert_equal(input, getline(1, '$')) |
| 347 | 7,12m0 |
| 348 | call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$')) |
| 349 | call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)')) |
| 350 | 10,12m0 |
| 351 | call assert_equal(PrepIndent("a")[1:] + PrepIndent("b") + ["a"] + PrepIndent("c"), getline(1, '$')) |
| 352 | call assert_equal([1, 1, 1, 1, 1, -1, 7, 7, 7, 7, 7, -1, -1, 14, 14, 14, 14, 14], map(range(1, line('$')), 'foldclosed(v:val)')) |
| 353 | " moving should not close the folds |
| 354 | %d |
| 355 | call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c")) |
| 356 | set fdm=indent |
| 357 | call cursor(2, 1) |
Bram Moolenaar | 40ebc0a | 2017-03-16 15:59:14 +0100 | [diff] [blame] | 358 | %foldopen |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 359 | 7,12m0 |
| 360 | let folds=repeat([-1], 18) |
| 361 | call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$')) |
| 362 | call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)')) |
| 363 | norm! zM |
| 364 | " folds are not corrupted and all have been closed |
| 365 | call assert_equal([-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14], map(range(1, line('$')), 'foldclosed(v:val)')) |
| 366 | %d |
| 367 | call setline(1, ["a", "\tb", "\tc", "\td", "\te"]) |
| 368 | set fdm=indent |
| 369 | %foldopen |
| 370 | 3m4 |
| 371 | %foldclose |
| 372 | call assert_equal(["a", "\tb", "\td", "\tc", "\te"], getline(1, '$')) |
| 373 | call assert_equal([-1, 5, 5, 5, 5], map(range(1, line('$')), 'foldclosedend(v:val)')) |
| 374 | %d |
| 375 | call setline(1, ["a", "\tb", "\tc", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"]) |
| 376 | set fdm=indent foldlevel=0 |
| 377 | %foldopen |
| 378 | 3m1 |
| 379 | %foldclose |
| 380 | call assert_equal(["a", "\tc", "\tb", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"], getline(1, '$')) |
| 381 | call assert_equal(1, foldlevel(2)) |
| 382 | call assert_equal(5, foldclosedend(3)) |
| 383 | call assert_equal([-1, 2, 2, 2, 2, -1, 7, 7, 7, 7], map(range(1, line('$')), 'foldclosed(v:val)')) |
| 384 | 2,6m$ |
| 385 | %foldclose |
| 386 | call assert_equal(9, foldclosedend(2)) |
| 387 | call assert_equal(1, foldlevel(6)) |
| 388 | call assert_equal(9, foldclosedend(7)) |
| 389 | call assert_equal([-1, 2, 2, 2, 2, 2, 2, 2, 2, -1], map(range(1, line('$')), 'foldclosed(v:val)')) |
Bram Moolenaar | 40ebc0a | 2017-03-16 15:59:14 +0100 | [diff] [blame] | 390 | " Ensure moving around the edges still works. |
| 391 | %d |
| 392 | call setline(1, PrepIndent("a") + repeat(["a"], 3) + ["\ta"]) |
| 393 | set fdm=indent foldlevel=0 |
| 394 | %foldopen |
| 395 | 6m$ |
| 396 | " The first fold has been truncated to the 5'th line. |
| 397 | " Second fold has been moved up because the moved line is now below it. |
Bram Moolenaar | 94be619 | 2017-04-22 22:40:11 +0200 | [diff] [blame] | 398 | call Check_foldlevels([0, 1, 1, 1, 1, 0, 0, 0, 1, 1]) |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 399 | bw! |
| 400 | endfunc |
Bram Moolenaar | 518c9b1 | 2017-03-21 11:48:39 +0100 | [diff] [blame] | 401 | |
| 402 | func Test_folddoopen_folddoclosed() |
| 403 | new |
| 404 | call setline(1, range(1, 9)) |
| 405 | set foldmethod=manual |
| 406 | 1,3 fold |
| 407 | 6,8 fold |
| 408 | |
| 409 | " Test without range. |
| 410 | folddoopen s/$/o/ |
| 411 | folddoclosed s/$/c/ |
| 412 | call assert_equal(['1c', '2c', '3c', |
| 413 | \ '4o', '5o', |
| 414 | \ '6c', '7c', '8c', |
| 415 | \ '9o'], getline(1, '$')) |
| 416 | |
| 417 | " Test with range. |
| 418 | call setline(1, range(1, 9)) |
| 419 | 1,8 folddoopen s/$/o/ |
| 420 | 4,$ folddoclosed s/$/c/ |
| 421 | call assert_equal(['1', '2', '3', |
| 422 | \ '4o', '5o', |
| 423 | \ '6c', '7c', '8c', |
| 424 | \ '9'], getline(1, '$')) |
| 425 | |
| 426 | set foldmethod& |
| 427 | bw! |
| 428 | endfunc |
| 429 | |
| 430 | func Test_fold_error() |
| 431 | new |
| 432 | call setline(1, [1, 2]) |
| 433 | |
| 434 | for fm in ['indent', 'expr', 'syntax', 'diff'] |
| 435 | exe 'set foldmethod=' . fm |
| 436 | call assert_fails('norm zf', 'E350:') |
| 437 | call assert_fails('norm zd', 'E351:') |
| 438 | call assert_fails('norm zE', 'E352:') |
| 439 | endfor |
| 440 | |
| 441 | set foldmethod=manual |
| 442 | call assert_fails('norm zd', 'E490:') |
| 443 | call assert_fails('norm zo', 'E490:') |
| 444 | call assert_fails('3fold', 'E16:') |
| 445 | |
| 446 | set foldmethod=marker |
| 447 | set nomodifiable |
| 448 | call assert_fails('1,2fold', 'E21:') |
| 449 | |
| 450 | set modifiable& |
| 451 | set foldmethod& |
| 452 | bw! |
| 453 | endfunc |
Bram Moolenaar | 495b7dd | 2017-09-16 17:19:22 +0200 | [diff] [blame] | 454 | |
| 455 | func Test_foldtext_recursive() |
| 456 | new |
| 457 | call setline(1, ['{{{', 'some text', '}}}']) |
| 458 | setlocal foldenable foldmethod=marker foldtext=foldtextresult(v\:foldstart) |
| 459 | " This was crashing because of endless recursion. |
| 460 | 2foldclose |
| 461 | redraw |
| 462 | call assert_equal(1, foldlevel(2)) |
| 463 | call assert_equal(1, foldclosed(2)) |
| 464 | call assert_equal(3, foldclosedend(2)) |
| 465 | bwipe! |
| 466 | endfunc |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 467 | |
| 468 | " Various fold related tests |
| 469 | |
| 470 | " Basic test if a fold can be created, opened, moving to the end and closed |
| 471 | func Test_fold_manual() |
| 472 | enew! |
| 473 | set fdm=manual |
| 474 | |
| 475 | let content = ['1 aa', '2 bb', '3 cc'] |
| 476 | call append(0, content) |
| 477 | call cursor(1, 1) |
| 478 | normal zf2j |
| 479 | call assert_equal('1 aa', getline(foldclosed('.'))) |
| 480 | normal zo |
| 481 | call assert_equal(-1, foldclosed('.')) |
| 482 | normal ]z |
| 483 | call assert_equal('3 cc', getline('.')) |
| 484 | normal zc |
| 485 | call assert_equal('1 aa', getline(foldclosed('.'))) |
| 486 | |
| 487 | set fdm& |
| 488 | enew! |
| 489 | endfunc |
| 490 | |
| 491 | " test folding with markers. |
| 492 | func Test_fold_marker() |
| 493 | enew! |
| 494 | set fdm=marker fdl=1 fdc=3 |
| 495 | |
| 496 | let content = ['4 dd {{{', '5 ee {{{ }}}', '6 ff }}}'] |
| 497 | call append(0, content) |
| 498 | call cursor(2, 1) |
| 499 | call assert_equal(2, foldlevel('.')) |
| 500 | normal [z |
| 501 | call assert_equal(1, foldlevel('.')) |
| 502 | exe "normal jo{{ \<Esc>r{jj" |
| 503 | call assert_equal(1, foldlevel('.')) |
| 504 | normal kYpj |
| 505 | call assert_equal(0, foldlevel('.')) |
| 506 | |
| 507 | set fdm& fdl& fdc& |
| 508 | enew! |
| 509 | endfunc |
| 510 | |
Bram Moolenaar | 4af7259 | 2018-12-09 15:00:52 +0100 | [diff] [blame] | 511 | " test create fold markers with C filetype |
| 512 | func Test_fold_create_marker_in_C() |
| 513 | enew! |
| 514 | set fdm=marker fdl=9 |
| 515 | set filetype=c |
| 516 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 517 | let content =<< trim [CODE] |
| 518 | /* |
| 519 | * comment |
| 520 | * |
| 521 | * |
| 522 | */ |
| 523 | int f(int* p) { |
| 524 | *p = 3; |
| 525 | return 0; |
| 526 | } |
| 527 | [CODE] |
| 528 | |
Bram Moolenaar | 4af7259 | 2018-12-09 15:00:52 +0100 | [diff] [blame] | 529 | for c in range(len(content) - 1) |
| 530 | bw! |
| 531 | call append(0, content) |
| 532 | call cursor(c + 1, 1) |
| 533 | norm! zfG |
| 534 | call assert_equal(content[c] . (c < 4 ? '{{{' : '/*{{{*/'), getline(c + 1)) |
| 535 | endfor |
| 536 | |
| 537 | set fdm& fdl& |
| 538 | enew! |
| 539 | endfunc |
| 540 | |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 541 | " test folding with indent |
| 542 | func Test_fold_indent() |
| 543 | enew! |
| 544 | set fdm=indent sw=2 |
| 545 | |
| 546 | let content = ['1 aa', '2 bb', '3 cc'] |
| 547 | call append(0, content) |
| 548 | call cursor(2, 1) |
| 549 | exe "normal i \<Esc>jI " |
| 550 | call assert_equal(2, foldlevel('.')) |
| 551 | normal k |
| 552 | call assert_equal(1, foldlevel('.')) |
| 553 | |
| 554 | set fdm& sw& |
| 555 | enew! |
| 556 | endfunc |
| 557 | |
| 558 | " test syntax folding |
| 559 | func Test_fold_syntax() |
| 560 | if !has('syntax') |
| 561 | return |
| 562 | endif |
| 563 | |
| 564 | enew! |
| 565 | set fdm=syntax fdl=0 |
| 566 | |
| 567 | syn region Hup start="dd" end="ii" fold contains=Fd1,Fd2,Fd3 |
| 568 | syn region Fd1 start="ee" end="ff" fold contained |
| 569 | syn region Fd2 start="gg" end="hh" fold contained |
| 570 | syn region Fd3 start="commentstart" end="commentend" fold contained |
| 571 | let content = ['3 cc', '4 dd {{{', '5 ee {{{ }}}', '{{{{', '6 ff }}}', |
| 572 | \ '6 ff }}}', '7 gg', '8 hh', '9 ii'] |
| 573 | call append(0, content) |
| 574 | normal Gzk |
| 575 | call assert_equal('9 ii', getline('.')) |
| 576 | normal k |
| 577 | call assert_equal('3 cc', getline('.')) |
| 578 | exe "normal jAcommentstart \<Esc>Acommentend" |
| 579 | set fdl=1 |
| 580 | normal 3j |
| 581 | call assert_equal('7 gg', getline('.')) |
| 582 | set fdl=0 |
| 583 | exe "normal zO\<C-L>j" |
| 584 | call assert_equal('8 hh', getline('.')) |
| 585 | syn clear Fd1 Fd2 Fd3 Hup |
| 586 | |
| 587 | set fdm& fdl& |
| 588 | enew! |
| 589 | endfunc |
| 590 | |
| 591 | func Flvl() |
| 592 | let l = getline(v:lnum) |
| 593 | if l =~ "bb$" |
| 594 | return 2 |
| 595 | elseif l =~ "gg$" |
| 596 | return "s1" |
| 597 | elseif l =~ "ii$" |
| 598 | return ">2" |
| 599 | elseif l =~ "kk$" |
| 600 | return "0" |
| 601 | endif |
| 602 | return "=" |
| 603 | endfun |
| 604 | |
| 605 | " test expression folding |
| 606 | func Test_fold_expr() |
| 607 | enew! |
| 608 | set fdm=expr fde=Flvl() |
| 609 | |
| 610 | let content = ['1 aa', |
| 611 | \ '2 bb', |
| 612 | \ '3 cc', |
| 613 | \ '4 dd {{{commentstart commentend', |
| 614 | \ '5 ee {{{ }}}', |
| 615 | \ '{{{', |
| 616 | \ '6 ff }}}', |
| 617 | \ '6 ff }}}', |
| 618 | \ ' 7 gg', |
| 619 | \ ' 8 hh', |
| 620 | \ '9 ii', |
| 621 | \ 'a jj', |
| 622 | \ 'b kk'] |
| 623 | call append(0, content) |
| 624 | call cursor(1, 1) |
| 625 | exe "normal /bb$\<CR>" |
| 626 | call assert_equal(2, foldlevel('.')) |
| 627 | exe "normal /hh$\<CR>" |
| 628 | call assert_equal(1, foldlevel('.')) |
| 629 | exe "normal /ii$\<CR>" |
| 630 | call assert_equal(2, foldlevel('.')) |
| 631 | exe "normal /kk$\<CR>" |
| 632 | call assert_equal(0, foldlevel('.')) |
| 633 | |
| 634 | set fdm& fde& |
| 635 | enew! |
| 636 | endfunc |
| 637 | |
| 638 | " Bug with fdm=indent and moving folds |
| 639 | " Moving a fold a few times, messes up the folds below the moved fold. |
| 640 | " Fixed by 7.4.700 |
| 641 | func Test_fold_move() |
| 642 | enew! |
| 643 | set fdm=indent sw=2 fdl=0 |
| 644 | |
| 645 | let content = ['', '', 'Line1', ' Line2', ' Line3', |
| 646 | \ 'Line4', ' Line5', ' Line6', |
| 647 | \ 'Line7', ' Line8', ' Line9'] |
| 648 | call append(0, content) |
| 649 | normal zM |
| 650 | call cursor(4, 1) |
| 651 | move 2 |
| 652 | move 1 |
| 653 | call assert_equal(7, foldclosed(7)) |
| 654 | call assert_equal(8, foldclosedend(7)) |
| 655 | call assert_equal(0, foldlevel(9)) |
| 656 | call assert_equal(10, foldclosed(10)) |
| 657 | call assert_equal(11, foldclosedend(10)) |
| 658 | call assert_equal('+-- 2 lines: Line2', foldtextresult(2)) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 659 | call assert_equal('+-- 2 lines: Line8', 10->foldtextresult()) |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 660 | |
| 661 | set fdm& sw& fdl& |
| 662 | enew! |
| 663 | endfunc |
Bram Moolenaar | fb094e1 | 2017-11-05 20:59:28 +0100 | [diff] [blame] | 664 | |
| 665 | " test for patch 7.3.637 |
| 666 | " Cannot catch the error caused by a foldopen when there is no fold. |
| 667 | func Test_foldopen_exception() |
| 668 | enew! |
| 669 | let a = 'No error caught' |
| 670 | try |
| 671 | foldopen |
| 672 | catch |
| 673 | let a = matchstr(v:exception,'^[^ ]*') |
| 674 | endtry |
| 675 | call assert_equal('Vim(foldopen):E490:', a) |
| 676 | |
| 677 | let a = 'No error caught' |
| 678 | try |
| 679 | foobar |
| 680 | catch |
| 681 | let a = matchstr(v:exception,'^[^ ]*') |
| 682 | endtry |
| 683 | call assert_match('E492:', a) |
| 684 | endfunc |
Bram Moolenaar | 907dad7 | 2018-07-10 15:07:15 +0200 | [diff] [blame] | 685 | |
| 686 | func Test_fold_last_line_with_pagedown() |
| 687 | enew! |
| 688 | set fdm=manual |
| 689 | |
| 690 | let expect = '+-- 11 lines: 9---' |
| 691 | let content = range(1,19) |
| 692 | call append(0, content) |
| 693 | normal dd9G |
| 694 | normal zfG |
| 695 | normal zt |
| 696 | call assert_equal('9', getline(foldclosed('.'))) |
| 697 | call assert_equal('19', getline(foldclosedend('.'))) |
| 698 | call assert_equal(expect, ScreenLines(1, len(expect))[0]) |
| 699 | call feedkeys("\<C-F>", 'xt') |
| 700 | call assert_equal(expect, ScreenLines(1, len(expect))[0]) |
| 701 | call feedkeys("\<C-F>", 'xt') |
| 702 | call assert_equal(expect, ScreenLines(1, len(expect))[0]) |
| 703 | call feedkeys("\<C-B>\<C-F>\<C-F>", 'xt') |
| 704 | call assert_equal(expect, ScreenLines(1, len(expect))[0]) |
| 705 | |
| 706 | set fdm& |
| 707 | enew! |
| 708 | endfunc |
Bram Moolenaar | 7701f30 | 2018-10-02 21:20:32 +0200 | [diff] [blame] | 709 | |
| 710 | func Test_folds_with_rnu() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 711 | CheckScreendump |
Bram Moolenaar | 7701f30 | 2018-10-02 21:20:32 +0200 | [diff] [blame] | 712 | |
| 713 | call writefile([ |
| 714 | \ 'set fdm=marker rnu foldcolumn=2', |
| 715 | \ 'call setline(1, ["{{{1", "nline 1", "{{{1", "line 2"])', |
| 716 | \ ], 'Xtest_folds_with_rnu') |
| 717 | let buf = RunVimInTerminal('-S Xtest_folds_with_rnu', {}) |
| 718 | |
| 719 | call VerifyScreenDump(buf, 'Test_folds_with_rnu_01', {}) |
| 720 | call term_sendkeys(buf, "j") |
| 721 | call VerifyScreenDump(buf, 'Test_folds_with_rnu_02', {}) |
| 722 | |
| 723 | " clean up |
| 724 | call StopVimInTerminal(buf) |
| 725 | call delete('Xtest_folds_with_rnu') |
| 726 | endfunc |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 727 | |
| 728 | func Test_folds_marker_in_comment2() |
| 729 | new |
| 730 | call setline(1, ['Lorem ipsum dolor sit', 'Lorem ipsum dolor sit', 'Lorem ipsum dolor sit']) |
| 731 | setl fen fdm=marker |
| 732 | setl commentstring=<!--%s--> |
| 733 | setl comments=s:<!--,m:\ \ \ \ ,e:--> |
| 734 | norm! zf2j |
| 735 | setl nofen |
| 736 | :1y |
| 737 | call assert_equal(['Lorem ipsum dolor sit<!--{{{-->'], getreg(0,1,1)) |
| 738 | :+2y |
| 739 | call assert_equal(['Lorem ipsum dolor sit<!--}}}-->'], getreg(0,1,1)) |
| 740 | |
| 741 | set foldmethod& |
| 742 | bwipe! |
| 743 | endfunc |
Bram Moolenaar | 9a4a8c4 | 2019-08-19 22:48:30 +0200 | [diff] [blame] | 744 | |
| 745 | func Test_fold_delete_with_marker() |
| 746 | new |
| 747 | call setline(1, ['func Func() {{{1', 'endfunc']) |
| 748 | 1,2yank |
| 749 | new |
| 750 | set fdm=marker |
| 751 | call setline(1, 'x') |
| 752 | normal! Vp |
| 753 | normal! zd |
| 754 | call assert_equal(['func Func() ', 'endfunc'], getline(1, '$')) |
| 755 | |
| 756 | set fdm& |
| 757 | bwipe! |
| 758 | bwipe! |
| 759 | endfunc |
Bram Moolenaar | 7a9bd7c | 2019-09-17 22:42:55 +0200 | [diff] [blame] | 760 | |
| 761 | func Test_fold_delete_with_marker_and_whichwrap() |
| 762 | new |
| 763 | let content1 = [''] |
| 764 | let content2 = ['folded line 1 "{{{1', ' test', ' test2', ' test3', '', 'folded line 2 "{{{1', ' test', ' test2', ' test3'] |
| 765 | call setline(1, content1 + content2) |
| 766 | set fdm=marker ww+=l |
| 767 | normal! x |
| 768 | call assert_equal(content2, getline(1, '$')) |
| 769 | set fdm& ww& |
| 770 | bwipe! |
| 771 | endfunc |