join_spec.lua (10898B)
1 -- Test for joining lines 2 3 local t = require('test.testutil') 4 local n = require('test.functional.testnvim')() 5 6 local clear, eq = n.clear, t.eq 7 local eval, command = n.eval, n.command 8 local feed = n.feed 9 local insert = n.insert 10 local expect = n.expect 11 local feed_command = n.feed_command 12 13 describe('joining lines', function() 14 before_each(clear) 15 16 it('is working', function() 17 command('new') 18 command([[call setline(1, ['one', 'two', 'three', 'four'])]]) 19 command('normal J') 20 eq('one two', eval('getline(1)')) 21 command('%del') 22 command([[call setline(1, ['one', 'two', 'three', 'four'])]]) 23 command('normal 10J') 24 eq('one two three four', eval('getline(1)')) 25 end) 26 27 -- luacheck: ignore 613 (Trailing whitespaces in a string) 28 -- luacheck: ignore 611 (Line contains only whitespaces) 29 it("keeps marks with different 'joinspaces' settings", function() 30 insert([[ 31 firstline 32 asdfasdf. 33 asdf 34 asdfasdf. 35 asdf 36 asdfasdf. 37 asdf 38 asdfasdf. 39 asdf 40 asdfasdf. 41 asdf 42 asdfasdf. 43 asdf 44 asdfasdf. 45 asdf 46 asdfasdf 47 asdf 48 asdfasdf 49 asdf 50 asdfasdf 51 asdf 52 asdfasdf 53 asdf 54 asdfasdf 55 asdf 56 asdfasdf 57 asdf 58 asdfasdf 59 asdf 60 zx cvn. 61 as dfg? 62 hjkl iop! 63 ert 64 ]]) 65 66 -- Switch off 'joinspaces', then join some lines in the buffer using "J". 67 -- Also set a few marks and record their movement when joining lines. 68 feed_command('set nojoinspaces') 69 feed_command('/firstline/') 70 feed('j"td/^$/<cr>') 71 feed('PJjJjJjJjJjJjJjJjJjJjJjJjJjJ') 72 feed('j05lmx2j06lmy2k4Jy3l$p`xyl$p`yy2l$p') 73 74 -- Do the same with 'joinspaces' on. 75 feed_command('set joinspaces') 76 feed('j"tp') 77 feed('JjJjJjJjJjJjJjJjJjJjJjJjJjJ') 78 feed('j05lmx2j06lmy2k4Jy3l$p`xyl$p`yy2l$po<esc>') 79 80 feed_command('1d') 81 82 expect([[ 83 asdfasdf. asdf 84 asdfasdf. asdf 85 asdfasdf. asdf 86 asdfasdf. asdf 87 asdfasdf. asdf 88 asdfasdf. asdf 89 asdfasdf. asdf 90 asdfasdf asdf 91 asdfasdf asdf 92 asdfasdf asdf 93 asdfasdf asdf 94 asdfasdf asdf 95 asdfasdf asdf 96 asdfasdf asdf 97 zx cvn. as dfg? hjkl iop! ert ernop 98 99 asdfasdf. asdf 100 asdfasdf. asdf 101 asdfasdf. asdf 102 asdfasdf. asdf 103 asdfasdf. asdf 104 asdfasdf. asdf 105 asdfasdf. asdf 106 asdfasdf asdf 107 asdfasdf asdf 108 asdfasdf asdf 109 asdfasdf asdf 110 asdfasdf asdf 111 asdfasdf asdf 112 asdfasdf asdf 113 zx cvn. as dfg? hjkl iop! ert enop 114 ]]) 115 end) 116 117 it("removes comment leaders with 'joinspaces' off", function() 118 insert([[ 119 { 120 121 /* 122 * Make sure the previous comment leader is not removed. 123 */ 124 125 /* 126 * Make sure the previous comment leader is not removed. 127 */ 128 129 // Should the next comment leader be left alone? 130 // Yes. 131 132 // Should the next comment leader be left alone? 133 // Yes. 134 135 /* Here the comment leader should be left intact. */ 136 // And so should this one. 137 138 /* Here the comment leader should be left intact. */ 139 // And so should this one. 140 141 if (condition) // Remove the next comment leader! 142 // OK, I will. 143 action(); 144 145 if (condition) // Remove the next comment leader! 146 // OK, I will. 147 action(); 148 } 149 ]]) 150 151 feed_command('/^{/+1') 152 feed_command('set comments=s1:/*,mb:*,ex:*/,://') 153 feed_command('set nojoinspaces') 154 feed_command('set backspace=eol,start') 155 156 -- With 'joinspaces' switched off, join lines using both "J" and :join and 157 -- verify that comment leaders are stripped or kept as appropriate. 158 feed_command('.,+3join') 159 feed('j4J<cr>') 160 feed_command('.,+2join') 161 feed('j3J<cr>') 162 feed_command('.,+2join') 163 feed('j3J<cr>') 164 feed_command('.,+2join') 165 feed('jj3J<cr>') 166 167 expect([[ 168 { 169 /* Make sure the previous comment leader is not removed. */ 170 /* Make sure the previous comment leader is not removed. */ 171 // Should the next comment leader be left alone? Yes. 172 // Should the next comment leader be left alone? Yes. 173 /* Here the comment leader should be left intact. */ // And so should this one. 174 /* Here the comment leader should be left intact. */ // And so should this one. 175 if (condition) // Remove the next comment leader! OK, I will. 176 action(); 177 if (condition) // Remove the next comment leader! OK, I will. 178 action(); 179 } 180 ]]) 181 end) 182 183 -- This test case has nothing to do with joining lines. 184 it("Ctrl-u and 'backspace' compatibility", function() 185 -- Notice that the buffer text, which is intended to helpfully hint at 186 -- what's being done in the test, is off by one line. (For example, "this 187 -- should be deleted" should not be deleted, but the line below it should, 188 -- and is.) This is likely a mistake, but was kept here for consistency. 189 insert([[ 190 1 this shouldn't be deleted 191 2 this shouldn't be deleted 192 3 this shouldn't be deleted 193 4 this should be deleted 194 5 this shouldn't be deleted 195 6 this shouldn't be deleted 196 7 this shouldn't be deleted 197 8 this shouldn't be deleted (not touched yet) 198 ]]) 199 200 -- As mentioned above, we mimic the wrong initial cursor position in the old 201 -- test by advancing one line further. 202 feed_command([[/^\d\+ this]], '+1') 203 204 -- Test with the default 'backspace' setting. 205 feed('Avim1<c-u><esc><cr>') 206 feed('Avim2<c-g>u<c-u><esc><cr>') 207 feed_command('set cpo-=<') 208 feed_command('inoremap <c-u> <left><c-u>') 209 feed('Avim3<c-u><esc><cr>') 210 feed_command('iunmap <c-u>') 211 feed('Avim4<c-u><c-u><esc><cr>') 212 213 -- Test with 'backspace' set to the compatible setting. 214 feed_command('set backspace=') 215 feed('A vim5<esc>A<c-u><c-u><esc><cr>') 216 feed('A vim6<esc>Azwei<c-g>u<c-u><esc><cr>') 217 feed_command('inoremap <c-u> <left><c-u>') 218 feed('A vim7<c-u><c-u><esc><cr>') 219 220 expect([[ 221 1 this shouldn't be deleted 222 2 this shouldn't be deleted 223 3 this shouldn't be deleted 224 4 this should be deleted3 225 226 6 this shouldn't be deleted vim5 227 7 this shouldn't be deleted vim6 228 8 this shouldn't be deleted (not touched yet) vim7 229 ]]) 230 end) 231 232 it("removes comment leaders with 'joinspaces' on", function() 233 insert([[ 234 { 235 236 /* 237 * Make sure the previous comment leader is not removed. 238 */ 239 240 /* 241 * Make sure the previous comment leader is not removed. 242 */ 243 244 /* List: 245 * - item1 246 * foo bar baz 247 * foo bar baz 248 * - item2 249 * foo bar baz 250 * foo bar baz 251 */ 252 253 /* List: 254 * - item1 255 * foo bar baz 256 * foo bar baz 257 * - item2 258 * foo bar baz 259 * foo bar baz 260 */ 261 262 // Should the next comment leader be left alone? 263 // Yes. 264 265 // Should the next comment leader be left alone? 266 // Yes. 267 268 /* Here the comment leader should be left intact. */ 269 // And so should this one. 270 271 /* Here the comment leader should be left intact. */ 272 // And so should this one. 273 274 if (condition) // Remove the next comment leader! 275 // OK, I will. 276 action(); 277 278 if (condition) // Remove the next comment leader! 279 // OK, I will. 280 action(); 281 282 int i = 7 /* foo *// 3 283 // comment 284 ; 285 286 int i = 7 /* foo *// 3 287 // comment 288 ; 289 290 ># Note that the last character of the ending comment leader (left angle 291 # bracket) is a comment leader itself. Make sure that this comment leader is 292 # not removed from the next line #< 293 < On this line a new comment is opened which spans 2 lines. This comment should 294 < retain its comment leader. 295 296 ># Note that the last character of the ending comment leader (left angle 297 # bracket) is a comment leader itself. Make sure that this comment leader is 298 # not removed from the next line #< 299 < On this line a new comment is opened which spans 2 lines. This comment should 300 < retain its comment leader. 301 302 } 303 ]]) 304 305 feed_command('/^{/+1') 306 feed_command([[set comments=sO:*\ -,mO:*\ \ ,exO:*/]]) 307 feed_command('set comments+=s1:/*,mb:*,ex:*/,://') 308 feed_command('set comments+=s1:>#,mb:#,ex:#<,:<') 309 feed_command('set backspace=eol,start') 310 311 -- With 'joinspaces' on (the default setting), again join lines and verify 312 -- that comment leaders are stripped or kept as appropriate. 313 feed_command('.,+3join') 314 feed('j4J<cr>') 315 feed_command('.,+8join') 316 feed('j9J<cr>') 317 feed_command('.,+2join') 318 feed('j3J<cr>') 319 feed_command('.,+2join') 320 feed('j3J<cr>') 321 feed_command('.,+2join') 322 feed('jj3J<cr>') 323 feed('j') 324 feed_command('.,+2join') 325 feed('jj3J<cr>') 326 feed('j') 327 feed_command('.,+5join') 328 feed('j6J<cr>') 329 feed( 330 'oSome code!<cr>// Make sure backspacing does not remove this comment leader.<esc>0i<bs><esc>' 331 ) 332 333 expect([[ 334 { 335 /* Make sure the previous comment leader is not removed. */ 336 /* Make sure the previous comment leader is not removed. */ 337 /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */ 338 /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */ 339 // Should the next comment leader be left alone? Yes. 340 // Should the next comment leader be left alone? Yes. 341 /* Here the comment leader should be left intact. */ // And so should this one. 342 /* Here the comment leader should be left intact. */ // And so should this one. 343 if (condition) // Remove the next comment leader! OK, I will. 344 action(); 345 if (condition) // Remove the next comment leader! OK, I will. 346 action(); 347 int i = 7 /* foo *// 3 // comment 348 ; 349 int i = 7 /* foo *// 3 // comment 350 ; 351 ># 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. 352 ># 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. 353 354 Some code!// Make sure backspacing does not remove this comment leader. 355 } 356 ]]) 357 end) 358 end)