Bram Moolenaar | 41e0f2f | 2016-03-08 14:44:42 +0100 | [diff] [blame] | 1 | " Test for joining lines. |
| 2 | |
| 3 | func Test_join_with_count() |
| 4 | new |
| 5 | call setline(1, ['one', 'two', 'three', 'four']) |
| 6 | normal J |
| 7 | call assert_equal('one two', getline(1)) |
| 8 | %del |
| 9 | call setline(1, ['one', 'two', 'three', 'four']) |
| 10 | normal 10J |
| 11 | call assert_equal('one two three four', getline(1)) |
Bram Moolenaar | cc184cf | 2019-11-12 20:31:20 +0100 | [diff] [blame] | 12 | |
| 13 | call setline(1, ['one', '', 'two']) |
| 14 | normal J |
| 15 | call assert_equal('one', getline(1)) |
| 16 | |
| 17 | call setline(1, ['one', ' ', 'two']) |
| 18 | normal J |
| 19 | call assert_equal('one', getline(1)) |
| 20 | |
| 21 | call setline(1, ['one', '', '', 'two']) |
| 22 | normal JJ |
| 23 | call assert_equal('one', getline(1)) |
| 24 | |
| 25 | call setline(1, ['one', ' ', ' ', 'two']) |
| 26 | normal JJ |
| 27 | call assert_equal('one', getline(1)) |
| 28 | |
| 29 | call setline(1, ['one', '', '', 'two']) |
| 30 | normal 2J |
| 31 | call assert_equal('one', getline(1)) |
| 32 | |
Bram Moolenaar | 41e0f2f | 2016-03-08 14:44:42 +0100 | [diff] [blame] | 33 | quit! |
| 34 | endfunc |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 35 | |
| 36 | " Tests for setting the '[,'] marks when joining lines. |
| 37 | func Test_join_marks() |
| 38 | enew |
| 39 | call append(0, [ |
| 40 | \ "\t\tO sodales, ludite, vos qui", |
| 41 | \ "attamen consulite per voster honur. Tua pulchra " . |
| 42 | \ "facies me fay planszer milies", |
| 43 | \ "", |
| 44 | \ "This line.", |
| 45 | \ "Should be joined with the next line", |
| 46 | \ "and with this line"]) |
| 47 | |
| 48 | normal gg0gqj |
| 49 | call assert_equal([0, 1, 1, 0], getpos("'[")) |
| 50 | call assert_equal([0, 2, 1, 0], getpos("']")) |
| 51 | |
| 52 | /^This line/;'}-join |
| 53 | call assert_equal([0, 4, 11, 0], getpos("'[")) |
| 54 | call assert_equal([0, 4, 67, 0], getpos("']")) |
| 55 | enew! |
| 56 | endfunc |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 57 | |
| 58 | " Test for joining lines and marks in them |
| 59 | " in compatible and nocompatible modes |
| 60 | " and with 'joinspaces' set or not |
| 61 | " and with 'cpoptions' flag 'j' set or not |
| 62 | func Test_join_spaces_marks() |
| 63 | new |
| 64 | " Text used for the test |
| 65 | insert |
| 66 | asdfasdf. |
| 67 | asdf |
| 68 | asdfasdf. |
| 69 | asdf |
| 70 | asdfasdf. |
| 71 | asdf |
| 72 | asdfasdf. |
| 73 | asdf |
| 74 | asdfasdf. |
| 75 | asdf |
| 76 | asdfasdf. |
| 77 | asdf |
| 78 | asdfasdf. |
| 79 | asdf |
| 80 | asdfasdf |
| 81 | asdf |
| 82 | asdfasdf |
| 83 | asdf |
| 84 | asdfasdf |
| 85 | asdf |
| 86 | asdfasdf |
| 87 | asdf |
| 88 | asdfasdf |
| 89 | asdf |
| 90 | asdfasdf |
| 91 | asdf |
| 92 | asdfasdf |
| 93 | asdf |
| 94 | zx cvn. |
| 95 | as dfg? |
| 96 | hjkl iop! |
| 97 | ert |
| 98 | zx cvn. |
| 99 | as dfg? |
| 100 | hjkl iop! |
| 101 | ert |
| 102 | . |
| 103 | let text = getline(1, '$') |
| 104 | normal gg |
| 105 | |
| 106 | set nojoinspaces |
| 107 | set cpoptions-=j |
| 108 | normal JjJjJjJjJjJjJjJjJjJjJjJjJjJ |
| 109 | normal j05lmx |
| 110 | normal 2j06lmy |
| 111 | normal 2k4Jy3l$p |
| 112 | normal `xyl$p |
| 113 | normal `yy2l$p |
| 114 | |
| 115 | set cpoptions+=j |
| 116 | normal j05lmx |
| 117 | normal 2j06lmy |
| 118 | normal 2k4Jy3l$p |
| 119 | normal `xyl$p |
| 120 | normal `yy2l$p |
| 121 | |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 122 | " Expected output |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 123 | let expected =<< trim [DATA] |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 124 | asdfasdf. asdf |
| 125 | asdfasdf. asdf |
| 126 | asdfasdf. asdf |
| 127 | asdfasdf. asdf |
| 128 | asdfasdf. asdf |
| 129 | asdfasdf. asdf |
| 130 | asdfasdf. asdf |
| 131 | asdfasdf asdf |
| 132 | asdfasdf asdf |
| 133 | asdfasdf asdf |
| 134 | asdfasdf asdf |
| 135 | asdfasdf asdf |
| 136 | asdfasdf asdf |
| 137 | asdfasdf asdf |
| 138 | zx cvn. as dfg? hjkl iop! ert ernop |
| 139 | zx cvn. as dfg? hjkl iop! ert ernop |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 140 | [DATA] |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 142 | call assert_equal(expected, getline(1, '$')) |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 143 | |
| 144 | enew! |
| 145 | call append(0, text) |
| 146 | normal gg |
| 147 | |
| 148 | set cpoptions-=j |
| 149 | set joinspaces |
| 150 | normal JjJjJjJjJjJjJjJjJjJjJjJjJjJ |
| 151 | normal j05lmx |
| 152 | normal 2j06lmy |
| 153 | normal 2k4Jy3l$p |
| 154 | normal `xyl$p |
| 155 | normal `yy2l$p |
| 156 | |
| 157 | set cpoptions+=j |
| 158 | normal j05lmx |
| 159 | normal 2j06lmy |
| 160 | normal 2k4Jy3l$p |
| 161 | normal `xyl$p |
| 162 | normal `yy2l$p |
| 163 | |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 164 | " Expected output |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 165 | let expected =<< trim [DATA] |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 166 | asdfasdf. asdf |
| 167 | asdfasdf. asdf |
| 168 | asdfasdf. asdf |
| 169 | asdfasdf. asdf |
| 170 | asdfasdf. asdf |
| 171 | asdfasdf. asdf |
| 172 | asdfasdf. asdf |
| 173 | asdfasdf asdf |
| 174 | asdfasdf asdf |
| 175 | asdfasdf asdf |
| 176 | asdfasdf asdf |
| 177 | asdfasdf asdf |
| 178 | asdfasdf asdf |
| 179 | asdfasdf asdf |
| 180 | zx cvn. as dfg? hjkl iop! ert enop |
| 181 | zx cvn. as dfg? hjkl iop! ert ernop |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 182 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 183 | [DATA] |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 184 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 185 | call assert_equal(expected, getline(1, '$')) |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 186 | |
| 187 | enew! |
| 188 | call append(0, text) |
| 189 | normal gg |
| 190 | |
| 191 | set cpoptions-=j |
| 192 | set nojoinspaces |
| 193 | set compatible |
| 194 | |
| 195 | normal JjJjJjJjJjJjJjJjJjJjJjJjJjJ |
| 196 | normal j4Jy3l$pjdG |
| 197 | |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 198 | " Expected output |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 199 | let expected =<< trim [DATA] |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 200 | asdfasdf. asdf |
| 201 | asdfasdf. asdf |
| 202 | asdfasdf. asdf |
| 203 | asdfasdf. asdf |
| 204 | asdfasdf. asdf |
| 205 | asdfasdf. asdf |
| 206 | asdfasdf. asdf |
| 207 | asdfasdf asdf |
| 208 | asdfasdf asdf |
| 209 | asdfasdf asdf |
| 210 | asdfasdf asdf |
| 211 | asdfasdf asdf |
| 212 | asdfasdf asdf |
| 213 | asdfasdf asdf |
| 214 | zx cvn. as dfg? hjkl iop! ert a |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 215 | [DATA] |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 216 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 217 | call assert_equal(expected, getline(1, '$')) |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 218 | |
| 219 | set nocompatible |
| 220 | set cpoptions&vim |
| 221 | set joinspaces&vim |
| 222 | close! |
| 223 | endfunc |
| 224 | |
| 225 | " Test for joining lines with comments |
| 226 | func Test_join_lines_with_comments() |
| 227 | new |
| 228 | |
| 229 | " Text used by the test |
| 230 | insert |
| 231 | { |
| 232 | |
| 233 | /* |
| 234 | * Make sure the previous comment leader is not removed. |
| 235 | */ |
| 236 | |
| 237 | /* |
| 238 | * Make sure the previous comment leader is not removed. |
| 239 | */ |
| 240 | |
| 241 | // Should the next comment leader be left alone? |
| 242 | // Yes. |
| 243 | |
| 244 | // Should the next comment leader be left alone? |
| 245 | // Yes. |
| 246 | |
| 247 | /* Here the comment leader should be left intact. */ |
| 248 | // And so should this one. |
| 249 | |
| 250 | /* Here the comment leader should be left intact. */ |
| 251 | // And so should this one. |
| 252 | |
| 253 | if (condition) // Remove the next comment leader! |
| 254 | // OK, I will. |
| 255 | action(); |
| 256 | |
| 257 | if (condition) // Remove the next comment leader! |
| 258 | // OK, I will. |
| 259 | action(); |
| 260 | } |
| 261 | . |
| 262 | |
| 263 | call cursor(2, 1) |
| 264 | set comments=s1:/*,mb:*,ex:*/,:// |
| 265 | set nojoinspaces fo=j |
| 266 | set backspace=eol,start |
| 267 | |
| 268 | .,+3join |
| 269 | exe "normal j4J\<CR>" |
| 270 | .,+2join |
| 271 | exe "normal j3J\<CR>" |
| 272 | .,+2join |
| 273 | exe "normal j3J\<CR>" |
| 274 | .,+2join |
| 275 | exe "normal jj3J\<CR>" |
| 276 | |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 277 | " Expected output |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 278 | let expected =<< trim [CODE] |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 279 | { |
| 280 | /* Make sure the previous comment leader is not removed. */ |
| 281 | /* Make sure the previous comment leader is not removed. */ |
| 282 | // Should the next comment leader be left alone? Yes. |
| 283 | // Should the next comment leader be left alone? Yes. |
| 284 | /* Here the comment leader should be left intact. */ // And so should this one. |
| 285 | /* Here the comment leader should be left intact. */ // And so should this one. |
| 286 | if (condition) // Remove the next comment leader! OK, I will. |
| 287 | action(); |
| 288 | if (condition) // Remove the next comment leader! OK, I will. |
| 289 | action(); |
| 290 | } |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 291 | [CODE] |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 292 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 293 | call assert_equal(expected, getline(1, '$')) |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 294 | |
| 295 | set comments&vim |
| 296 | set joinspaces&vim |
| 297 | set fo&vim |
| 298 | set backspace&vim |
| 299 | close! |
| 300 | endfunc |
| 301 | |
| 302 | " Test for joining lines with different comment leaders |
| 303 | func Test_join_comments_2() |
| 304 | new |
| 305 | |
| 306 | insert |
| 307 | { |
| 308 | |
| 309 | /* |
| 310 | * Make sure the previous comment leader is not removed. |
| 311 | */ |
| 312 | |
| 313 | /* |
| 314 | * Make sure the previous comment leader is not removed. |
| 315 | */ |
| 316 | |
| 317 | /* List: |
| 318 | * - item1 |
| 319 | * foo bar baz |
| 320 | * foo bar baz |
| 321 | * - item2 |
| 322 | * foo bar baz |
| 323 | * foo bar baz |
| 324 | */ |
| 325 | |
| 326 | /* List: |
| 327 | * - item1 |
| 328 | * foo bar baz |
| 329 | * foo bar baz |
| 330 | * - item2 |
| 331 | * foo bar baz |
| 332 | * foo bar baz |
| 333 | */ |
| 334 | |
| 335 | // Should the next comment leader be left alone? |
| 336 | // Yes. |
| 337 | |
| 338 | // Should the next comment leader be left alone? |
| 339 | // Yes. |
| 340 | |
| 341 | /* Here the comment leader should be left intact. */ |
| 342 | // And so should this one. |
| 343 | |
| 344 | /* Here the comment leader should be left intact. */ |
| 345 | // And so should this one. |
| 346 | |
| 347 | if (condition) // Remove the next comment leader! |
| 348 | // OK, I will. |
| 349 | action(); |
| 350 | |
| 351 | if (condition) // Remove the next comment leader! |
| 352 | // OK, I will. |
| 353 | action(); |
| 354 | |
| 355 | int i = 7 /* foo *// 3 |
| 356 | // comment |
| 357 | ; |
| 358 | |
| 359 | int i = 7 /* foo *// 3 |
| 360 | // comment |
| 361 | ; |
| 362 | |
| 363 | ># Note that the last character of the ending comment leader (left angle |
| 364 | # bracket) is a comment leader itself. Make sure that this comment leader is |
| 365 | # not removed from the next line #< |
| 366 | < On this line a new comment is opened which spans 2 lines. This comment should |
| 367 | < retain its comment leader. |
| 368 | |
| 369 | ># Note that the last character of the ending comment leader (left angle |
| 370 | # bracket) is a comment leader itself. Make sure that this comment leader is |
| 371 | # not removed from the next line #< |
| 372 | < On this line a new comment is opened which spans 2 lines. This comment should |
| 373 | < retain its comment leader. |
| 374 | |
| 375 | } |
| 376 | . |
| 377 | |
| 378 | call cursor(2, 1) |
| 379 | set comments=sO:*\ -,mO:*\ \ ,exO:*/ |
| 380 | set comments+=s1:/*,mb:*,ex:*/,:// |
| 381 | set comments+=s1:>#,mb:#,ex:#<,:< |
| 382 | set cpoptions-=j joinspaces fo=j |
| 383 | set backspace=eol,start |
| 384 | |
| 385 | .,+3join |
| 386 | exe "normal j4J\<CR>" |
| 387 | .,+8join |
| 388 | exe "normal j9J\<CR>" |
| 389 | .,+2join |
| 390 | exe "normal j3J\<CR>" |
| 391 | .,+2join |
| 392 | exe "normal j3J\<CR>" |
| 393 | .,+2join |
| 394 | exe "normal jj3J\<CR>j" |
| 395 | .,+2join |
| 396 | exe "normal jj3J\<CR>j" |
| 397 | .,+5join |
| 398 | exe "normal j6J\<CR>" |
| 399 | exe "normal oSome code!\<CR>// Make sure backspacing does not remove this comment leader.\<Esc>0i\<C-H>\<Esc>" |
| 400 | |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 401 | " Expected output |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 402 | let expected =<< trim [CODE] |
| 403 | { |
| 404 | /* Make sure the previous comment leader is not removed. */ |
| 405 | /* Make sure the previous comment leader is not removed. */ |
| 406 | /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */ |
| 407 | /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */ |
| 408 | // Should the next comment leader be left alone? Yes. |
| 409 | // Should the next comment leader be left alone? Yes. |
| 410 | /* Here the comment leader should be left intact. */ // And so should this one. |
| 411 | /* Here the comment leader should be left intact. */ // And so should this one. |
| 412 | if (condition) // Remove the next comment leader! OK, I will. |
| 413 | action(); |
| 414 | if (condition) // Remove the next comment leader! OK, I will. |
| 415 | action(); |
| 416 | int i = 7 /* foo *// 3 // comment |
| 417 | ; |
| 418 | int i = 7 /* foo *// 3 // comment |
| 419 | ; |
| 420 | ># Note that the last character of the ending comment leader (left angle bracket) is a comment leader itself. Make sure that this comment leader is not removed from the next line #< < On this line a new comment is opened which spans 2 lines. This comment should retain its comment leader. |
| 421 | ># Note that the last character of the ending comment leader (left angle bracket) is a comment leader itself. Make sure that this comment leader is not removed from the next line #< < On this line a new comment is opened which spans 2 lines. This comment should retain its comment leader. |
| 422 | |
| 423 | Some code!// Make sure backspacing does not remove this comment leader. |
| 424 | } |
| 425 | [CODE] |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 426 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 427 | call assert_equal(expected, getline(1, '$')) |
Bram Moolenaar | fb222df | 2019-05-14 17:57:19 +0200 | [diff] [blame] | 428 | close! |
| 429 | endfunc |
Bram Moolenaar | 91b65e4 | 2019-12-17 22:10:58 +0100 | [diff] [blame] | 430 | |
| 431 | func Test_join_lines() |
| 432 | new |
| 433 | call setline(1, ['a', 'b', '', 'c', 'd']) |
| 434 | %join |
| 435 | call assert_equal('a b c d', getline(1)) |
| 436 | call setline(1, ['a', 'b', '', 'c', 'd']) |
| 437 | normal 5J |
| 438 | call assert_equal('a b c d', getline(1)) |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 439 | call setline(1, ['a', 'b', 'c']) |
| 440 | 2,2join |
| 441 | call assert_equal(['a', 'b', 'c'], getline(1, '$')) |
| 442 | call assert_equal(2, line('.')) |
| 443 | 2join |
| 444 | call assert_equal(['a', 'b c'], getline(1, '$')) |
Bram Moolenaar | 91b65e4 | 2019-12-17 22:10:58 +0100 | [diff] [blame] | 445 | bwipe! |
| 446 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 447 | |
| 448 | " vim: shiftwidth=2 sts=2 expandtab |