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