forked from codemirror/codemirror5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvim_test.js
3792 lines (3727 loc) · 131 KB
/
vim_test.js
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
CodeMirror.Vim.suppressErrorLogging = true;
var code = '' +
' wOrd1 (#%\n' +
' word3] \n' +
'aopop pop 0 1 2 3 4\n' +
' (a) [b] {c} \n' +
'int getchar(void) {\n' +
' static char buf[BUFSIZ];\n' +
' static char *bufp = buf;\n' +
' if (n == 0) { /* buffer is empty */\n' +
' n = read(0, buf, sizeof buf);\n' +
' bufp = buf;\n' +
' }\n' +
'\n' +
' return (--n >= 0) ? (unsigned char) *bufp++ : EOF;\n' +
' \n' +
'}\n';
var lines = (function() {
lineText = code.split('\n');
var ret = [];
for (var i = 0; i < lineText.length; i++) {
ret[i] = {
line: i,
length: lineText[i].length,
lineText: lineText[i],
textStart: /^\s*/.exec(lineText[i])[0].length
};
}
return ret;
})();
var endOfDocument = makeCursor(lines.length - 1,
lines[lines.length - 1].length);
var wordLine = lines[0];
var bigWordLine = lines[1];
var charLine = lines[2];
var bracesLine = lines[3];
var seekBraceLine = lines[4];
var word1 = {
start: { line: wordLine.line, ch: 1 },
end: { line: wordLine.line, ch: 5 }
};
var word2 = {
start: { line: wordLine.line, ch: word1.end.ch + 2 },
end: { line: wordLine.line, ch: word1.end.ch + 4 }
};
var word3 = {
start: { line: bigWordLine.line, ch: 1 },
end: { line: bigWordLine.line, ch: 5 }
};
var bigWord1 = word1;
var bigWord2 = word2;
var bigWord3 = {
start: { line: bigWordLine.line, ch: 1 },
end: { line: bigWordLine.line, ch: 7 }
};
var bigWord4 = {
start: { line: bigWordLine.line, ch: bigWord1.end.ch + 3 },
end: { line: bigWordLine.line, ch: bigWord1.end.ch + 7 }
};
var oChars = [ { line: charLine.line, ch: 1 },
{ line: charLine.line, ch: 3 },
{ line: charLine.line, ch: 7 } ];
var pChars = [ { line: charLine.line, ch: 2 },
{ line: charLine.line, ch: 4 },
{ line: charLine.line, ch: 6 },
{ line: charLine.line, ch: 8 } ];
var numChars = [ { line: charLine.line, ch: 10 },
{ line: charLine.line, ch: 12 },
{ line: charLine.line, ch: 14 },
{ line: charLine.line, ch: 16 },
{ line: charLine.line, ch: 18 }];
var parens1 = {
start: { line: bracesLine.line, ch: 1 },
end: { line: bracesLine.line, ch: 3 }
};
var squares1 = {
start: { line: bracesLine.line, ch: 5 },
end: { line: bracesLine.line, ch: 7 }
};
var curlys1 = {
start: { line: bracesLine.line, ch: 9 },
end: { line: bracesLine.line, ch: 11 }
};
var seekOutside = {
start: { line: seekBraceLine.line, ch: 1 },
end: { line: seekBraceLine.line, ch: 16 }
};
var seekInside = {
start: { line: seekBraceLine.line, ch: 14 },
end: { line: seekBraceLine.line, ch: 11 }
};
function copyCursor(cur) {
return { ch: cur.ch, line: cur.line };
}
function forEach(arr, func) {
for (var i = 0; i < arr.length; i++) {
func(arr[i], i, arr);
}
}
function testVim(name, run, opts, expectedFail) {
var vimOpts = {
lineNumbers: true,
vimMode: true,
showCursorWhenSelecting: true,
value: code
};
for (var prop in opts) {
if (opts.hasOwnProperty(prop)) {
vimOpts[prop] = opts[prop];
}
}
return test('vim_' + name, function() {
var place = document.getElementById("testground");
var cm = CodeMirror(place, vimOpts);
var vim = CodeMirror.Vim.maybeInitVimState_(cm);
function doKeysFn(cm) {
return function(args) {
if (args instanceof Array) {
arguments = args;
}
for (var i = 0; i < arguments.length; i++) {
CodeMirror.Vim.handleKey(cm, arguments[i]);
}
}
}
function doInsertModeKeysFn(cm) {
return function(args) {
if (args instanceof Array) { arguments = args; }
function executeHandler(handler) {
if (typeof handler == 'string') {
CodeMirror.commands[handler](cm);
} else {
handler(cm);
}
return true;
}
for (var i = 0; i < arguments.length; i++) {
var key = arguments[i];
// Find key in keymap and handle.
var handled = CodeMirror.lookupKey(key, 'vim-insert', executeHandler);
// Record for insert mode.
if (handled == "handled" && cm.state.vim.insertMode && arguments[i] != 'Esc') {
var lastChange = CodeMirror.Vim.getVimGlobalState_().macroModeState.lastInsertModeChanges;
if (lastChange) {
lastChange.changes.push(new CodeMirror.Vim.InsertModeKey(key));
}
}
}
}
}
function doExFn(cm) {
return function(command) {
cm.openDialog = helpers.fakeOpenDialog(command);
helpers.doKeys(':');
}
}
function assertCursorAtFn(cm) {
return function(line, ch) {
var pos;
if (ch == null && typeof line.line == 'number') {
pos = line;
} else {
pos = makeCursor(line, ch);
}
eqPos(pos, cm.getCursor());
}
}
function fakeOpenDialog(result) {
return function(text, callback) {
return callback(result);
}
}
function fakeOpenNotification(matcher) {
return function(text) {
matcher(text);
}
}
var helpers = {
doKeys: doKeysFn(cm),
// Warning: Only emulates keymap events, not character insertions. Use
// replaceRange to simulate character insertions.
// Keys are in CodeMirror format, NOT vim format.
doInsertModeKeys: doInsertModeKeysFn(cm),
doEx: doExFn(cm),
assertCursorAt: assertCursorAtFn(cm),
fakeOpenDialog: fakeOpenDialog,
fakeOpenNotification: fakeOpenNotification,
getRegisterController: function() {
return CodeMirror.Vim.getRegisterController();
}
}
CodeMirror.Vim.resetVimGlobalState_();
var successful = false;
var savedOpenNotification = cm.openNotification;
try {
run(cm, vim, helpers);
successful = true;
} finally {
cm.openNotification = savedOpenNotification;
if (!successful || verbose) {
place.style.visibility = "visible";
} else {
place.removeChild(cm.getWrapperElement());
}
}
}, expectedFail);
};
testVim('qq@q', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('q', 'q', 'l', 'l', 'q');
helpers.assertCursorAt(0,2);
helpers.doKeys('@', 'q');
helpers.assertCursorAt(0,4);
}, { value: ' '});
testVim('@@', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('q', 'q', 'l', 'l', 'q');
helpers.assertCursorAt(0,2);
helpers.doKeys('@', 'q');
helpers.assertCursorAt(0,4);
helpers.doKeys('@', '@');
helpers.assertCursorAt(0,6);
}, { value: ' '});
var jumplistScene = ''+
'word\n'+
'(word)\n'+
'{word\n'+
'word.\n'+
'\n'+
'word search\n'+
'}word\n'+
'word\n'+
'word\n';
function testJumplist(name, keys, endPos, startPos, dialog) {
endPos = makeCursor(endPos[0], endPos[1]);
startPos = makeCursor(startPos[0], startPos[1]);
testVim(name, function(cm, vim, helpers) {
CodeMirror.Vim.resetVimGlobalState_();
if(dialog)cm.openDialog = helpers.fakeOpenDialog('word');
cm.setCursor(startPos);
helpers.doKeys.apply(null, keys);
helpers.assertCursorAt(endPos);
}, {value: jumplistScene});
};
testJumplist('jumplist_H', ['H', '<C-o>'], [5,2], [5,2]);
testJumplist('jumplist_M', ['M', '<C-o>'], [2,2], [2,2]);
testJumplist('jumplist_L', ['L', '<C-o>'], [2,2], [2,2]);
testJumplist('jumplist_[[', ['[', '[', '<C-o>'], [5,2], [5,2]);
testJumplist('jumplist_]]', [']', ']', '<C-o>'], [2,2], [2,2]);
testJumplist('jumplist_G', ['G', '<C-o>'], [5,2], [5,2]);
testJumplist('jumplist_gg', ['g', 'g', '<C-o>'], [5,2], [5,2]);
testJumplist('jumplist_%', ['%', '<C-o>'], [1,5], [1,5]);
testJumplist('jumplist_{', ['{', '<C-o>'], [1,5], [1,5]);
testJumplist('jumplist_}', ['}', '<C-o>'], [1,5], [1,5]);
testJumplist('jumplist_\'', ['m', 'a', 'h', '\'', 'a', 'h', '<C-i>'], [1,0], [1,5]);
testJumplist('jumplist_`', ['m', 'a', 'h', '`', 'a', 'h', '<C-i>'], [1,5], [1,5]);
testJumplist('jumplist_*_cachedCursor', ['*', '<C-o>'], [1,3], [1,3]);
testJumplist('jumplist_#_cachedCursor', ['#', '<C-o>'], [1,3], [1,3]);
testJumplist('jumplist_n', ['#', 'n', '<C-o>'], [1,1], [2,3]);
testJumplist('jumplist_N', ['#', 'N', '<C-o>'], [1,1], [2,3]);
testJumplist('jumplist_repeat_<c-o>', ['*', '*', '*', '3', '<C-o>'], [2,3], [2,3]);
testJumplist('jumplist_repeat_<c-i>', ['*', '*', '*', '3', '<C-o>', '2', '<C-i>'], [5,0], [2,3]);
testJumplist('jumplist_repeated_motion', ['3', '*', '<C-o>'], [2,3], [2,3]);
testJumplist('jumplist_/', ['/', '<C-o>'], [2,3], [2,3], 'dialog');
testJumplist('jumplist_?', ['?', '<C-o>'], [2,3], [2,3], 'dialog');
testJumplist('jumplist_skip_delted_mark<c-o>',
['*', 'n', 'n', 'k', 'd', 'k', '<C-o>', '<C-o>', '<C-o>'],
[0,2], [0,2]);
testJumplist('jumplist_skip_delted_mark<c-i>',
['*', 'n', 'n', 'k', 'd', 'k', '<C-o>', '<C-i>', '<C-i>'],
[1,0], [0,2]);
/**
* @param name Name of the test
* @param keys An array of keys or a string with a single key to simulate.
* @param endPos The expected end position of the cursor.
* @param startPos The position the cursor should start at, defaults to 0, 0.
*/
function testMotion(name, keys, endPos, startPos) {
testVim(name, function(cm, vim, helpers) {
if (!startPos) {
startPos = { line: 0, ch: 0 };
}
cm.setCursor(startPos);
helpers.doKeys(keys);
helpers.assertCursorAt(endPos);
});
};
function makeCursor(line, ch) {
return { line: line, ch: ch };
};
function offsetCursor(cur, offsetLine, offsetCh) {
return { line: cur.line + offsetLine, ch: cur.ch + offsetCh };
};
// Motion tests
testMotion('|', '|', makeCursor(0, 0), makeCursor(0,4));
testMotion('|_repeat', ['3', '|'], makeCursor(0, 2), makeCursor(0,4));
testMotion('h', 'h', makeCursor(0, 0), word1.start);
testMotion('h_repeat', ['3', 'h'], offsetCursor(word1.end, 0, -3), word1.end);
testMotion('l', 'l', makeCursor(0, 1));
testMotion('l_repeat', ['2', 'l'], makeCursor(0, 2));
testMotion('j', 'j', offsetCursor(word1.end, 1, 0), word1.end);
testMotion('j_repeat', ['2', 'j'], offsetCursor(word1.end, 2, 0), word1.end);
testMotion('j_repeat_clip', ['1000', 'j'], endOfDocument);
testMotion('k', 'k', offsetCursor(word3.end, -1, 0), word3.end);
testMotion('k_repeat', ['2', 'k'], makeCursor(0, 4), makeCursor(2, 4));
testMotion('k_repeat_clip', ['1000', 'k'], makeCursor(0, 4), makeCursor(2, 4));
testMotion('w', 'w', word1.start);
testMotion('w_multiple_newlines_no_space', 'w', makeCursor(12, 2), makeCursor(11, 2));
testMotion('w_multiple_newlines_with_space', 'w', makeCursor(14, 0), makeCursor(12, 51));
testMotion('w_repeat', ['2', 'w'], word2.start);
testMotion('w_wrap', ['w'], word3.start, word2.start);
testMotion('w_endOfDocument', 'w', endOfDocument, endOfDocument);
testMotion('w_start_to_end', ['1000', 'w'], endOfDocument, makeCursor(0, 0));
testMotion('W', 'W', bigWord1.start);
testMotion('W_repeat', ['2', 'W'], bigWord3.start, bigWord1.start);
testMotion('e', 'e', word1.end);
testMotion('e_repeat', ['2', 'e'], word2.end);
testMotion('e_wrap', 'e', word3.end, word2.end);
testMotion('e_endOfDocument', 'e', endOfDocument, endOfDocument);
testMotion('e_start_to_end', ['1000', 'e'], endOfDocument, makeCursor(0, 0));
testMotion('b', 'b', word3.start, word3.end);
testMotion('b_repeat', ['2', 'b'], word2.start, word3.end);
testMotion('b_wrap', 'b', word2.start, word3.start);
testMotion('b_startOfDocument', 'b', makeCursor(0, 0), makeCursor(0, 0));
testMotion('b_end_to_start', ['1000', 'b'], makeCursor(0, 0), endOfDocument);
testMotion('ge', ['g', 'e'], word2.end, word3.end);
testMotion('ge_repeat', ['2', 'g', 'e'], word1.end, word3.start);
testMotion('ge_wrap', ['g', 'e'], word2.end, word3.start);
testMotion('ge_startOfDocument', ['g', 'e'], makeCursor(0, 0),
makeCursor(0, 0));
testMotion('ge_end_to_start', ['1000', 'g', 'e'], makeCursor(0, 0), endOfDocument);
testMotion('gg', ['g', 'g'], makeCursor(lines[0].line, lines[0].textStart),
makeCursor(3, 1));
testMotion('gg_repeat', ['3', 'g', 'g'],
makeCursor(lines[2].line, lines[2].textStart));
testMotion('G', 'G',
makeCursor(lines[lines.length - 1].line, lines[lines.length - 1].textStart),
makeCursor(3, 1));
testMotion('G_repeat', ['3', 'G'], makeCursor(lines[2].line,
lines[2].textStart));
// TODO: Make the test code long enough to test Ctrl-F and Ctrl-B.
testMotion('0', '0', makeCursor(0, 0), makeCursor(0, 8));
testMotion('^', '^', makeCursor(0, lines[0].textStart), makeCursor(0, 8));
testMotion('+', '+', makeCursor(1, lines[1].textStart), makeCursor(0, 8));
testMotion('-', '-', makeCursor(0, lines[0].textStart), makeCursor(1, 4));
testMotion('_', ['6','_'], makeCursor(5, lines[5].textStart), makeCursor(0, 8));
testMotion('$', '$', makeCursor(0, lines[0].length - 1), makeCursor(0, 1));
testMotion('$_repeat', ['2', '$'], makeCursor(1, lines[1].length - 1),
makeCursor(0, 3));
testMotion('f', ['f', 'p'], pChars[0], makeCursor(charLine.line, 0));
testMotion('f_repeat', ['2', 'f', 'p'], pChars[2], pChars[0]);
testMotion('f_num', ['f', '2'], numChars[2], makeCursor(charLine.line, 0));
testMotion('t', ['t','p'], offsetCursor(pChars[0], 0, -1),
makeCursor(charLine.line, 0));
testMotion('t_repeat', ['2', 't', 'p'], offsetCursor(pChars[2], 0, -1),
pChars[0]);
testMotion('F', ['F', 'p'], pChars[0], pChars[1]);
testMotion('F_repeat', ['2', 'F', 'p'], pChars[0], pChars[2]);
testMotion('T', ['T', 'p'], offsetCursor(pChars[0], 0, 1), pChars[1]);
testMotion('T_repeat', ['2', 'T', 'p'], offsetCursor(pChars[0], 0, 1), pChars[2]);
testMotion('%_parens', ['%'], parens1.end, parens1.start);
testMotion('%_squares', ['%'], squares1.end, squares1.start);
testMotion('%_braces', ['%'], curlys1.end, curlys1.start);
testMotion('%_seek_outside', ['%'], seekOutside.end, seekOutside.start);
testMotion('%_seek_inside', ['%'], seekInside.end, seekInside.start);
testVim('%_seek_skip', function(cm, vim, helpers) {
cm.setCursor(0,0);
helpers.doKeys(['%']);
helpers.assertCursorAt(0,9);
}, {value:'01234"("()'});
testVim('%_skip_string', function(cm, vim, helpers) {
cm.setCursor(0,0);
helpers.doKeys(['%']);
helpers.assertCursorAt(0,4);
cm.setCursor(0,2);
helpers.doKeys(['%']);
helpers.assertCursorAt(0,0);
}, {value:'(")")'});
(')')
testVim('%_skip_comment', function(cm, vim, helpers) {
cm.setCursor(0,0);
helpers.doKeys(['%']);
helpers.assertCursorAt(0,6);
cm.setCursor(0,3);
helpers.doKeys(['%']);
helpers.assertCursorAt(0,0);
}, {value:'(/*)*/)'});
// Make sure that moving down after going to the end of a line always leaves you
// at the end of a line, but preserves the offset in other cases
testVim('Changing lines after Eol operation', function(cm, vim, helpers) {
cm.setCursor(0,0);
helpers.doKeys(['$']);
helpers.doKeys(['j']);
// After moving to Eol and then down, we should be at Eol of line 2
helpers.assertCursorAt({ line: 1, ch: lines[1].length - 1 });
helpers.doKeys(['j']);
// After moving down, we should be at Eol of line 3
helpers.assertCursorAt({ line: 2, ch: lines[2].length - 1 });
helpers.doKeys(['h']);
helpers.doKeys(['j']);
// After moving back one space and then down, since line 4 is shorter than line 2, we should
// be at Eol of line 2 - 1
helpers.assertCursorAt({ line: 3, ch: lines[3].length - 1 });
helpers.doKeys(['j']);
helpers.doKeys(['j']);
// After moving down again, since line 3 has enough characters, we should be back to the
// same place we were at on line 1
helpers.assertCursorAt({ line: 5, ch: lines[2].length - 2 });
});
//making sure gj and gk recover from clipping
testVim('gj_gk_clipping', function(cm,vim,helpers){
cm.setCursor(0, 1);
helpers.doKeys('g','j','g','j');
helpers.assertCursorAt(2, 1);
helpers.doKeys('g','k','g','k');
helpers.assertCursorAt(0, 1);
},{value: 'line 1\n\nline 2'});
//testing a mix of j/k and gj/gk
testVim('j_k_and_gj_gk', function(cm,vim,helpers){
cm.setSize(120);
cm.setCursor(0, 0);
//go to the last character on the first line
helpers.doKeys('$');
//move up/down on the column within the wrapped line
//side-effect: cursor is not locked to eol anymore
helpers.doKeys('g','k');
var cur=cm.getCursor();
eq(cur.line,0);
is((cur.ch<176),'gk didn\'t move cursor back (1)');
helpers.doKeys('g','j');
helpers.assertCursorAt(0, 176);
//should move to character 177 on line 2 (j/k preserve character index within line)
helpers.doKeys('j');
//due to different line wrapping, the cursor can be on a different screen-x now
//gj and gk preserve screen-x on movement, much like moveV
helpers.doKeys('3','g','k');
cur=cm.getCursor();
eq(cur.line,1);
is((cur.ch<176),'gk didn\'t move cursor back (2)');
helpers.doKeys('g','j','2','g','j');
//should return to the same character-index
helpers.doKeys('k');
helpers.assertCursorAt(0, 176);
},{ lineWrapping:true, value: 'This line is intentially long to test movement of gj and gk over wrapped lines. I will start on the end of this line, then make a step up and back to set the origin for j and k.\nThis line is supposed to be even longer than the previous. I will jump here and make another wiggle with gj and gk, before I jump back to the line above. Both wiggles should not change my cursor\'s target character but both j/k and gj/gk change each other\'s reference position.'});
testVim('gj_gk', function(cm, vim, helpers) {
if (phantom) return;
cm.setSize(120);
// Test top of document edge case.
cm.setCursor(0, 4);
helpers.doKeys('g', 'j');
helpers.doKeys('10', 'g', 'k');
helpers.assertCursorAt(0, 4);
// Test moving down preserves column position.
helpers.doKeys('g', 'j');
var pos1 = cm.getCursor();
var expectedPos2 = { line: 0, ch: (pos1.ch - 4) * 2 + 4};
helpers.doKeys('g', 'j');
helpers.assertCursorAt(expectedPos2);
// Move to the last character
cm.setCursor(0, 0);
// Move left to reset HSPos
helpers.doKeys('h');
// Test bottom of document edge case.
helpers.doKeys('100', 'g', 'j');
var endingPos = cm.getCursor();
is(endingPos != 0, 'gj should not be on wrapped line 0');
var topLeftCharCoords = cm.charCoords(makeCursor(0, 0));
var endingCharCoords = cm.charCoords(endingPos);
is(topLeftCharCoords.left == endingCharCoords.left, 'gj should end up on column 0');
},{ lineNumbers: false, lineWrapping:true, value: 'Thislineisintentiallylongtotestmovementofgjandgkoverwrappedlines.' });
testVim('}', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('}');
helpers.assertCursorAt(1, 0);
cm.setCursor(0, 0);
helpers.doKeys('2', '}');
helpers.assertCursorAt(4, 0);
cm.setCursor(0, 0);
helpers.doKeys('6', '}');
helpers.assertCursorAt(5, 0);
}, { value: 'a\n\nb\nc\n\nd' });
testVim('{', function(cm, vim, helpers) {
cm.setCursor(5, 0);
helpers.doKeys('{');
helpers.assertCursorAt(4, 0);
cm.setCursor(5, 0);
helpers.doKeys('2', '{');
helpers.assertCursorAt(1, 0);
cm.setCursor(5, 0);
helpers.doKeys('6', '{');
helpers.assertCursorAt(0, 0);
}, { value: 'a\n\nb\nc\n\nd' });
testVim('paragraph motions', function(cm, vim, helpers) {
cm.setCursor(10, 0);
helpers.doKeys('{');
helpers.assertCursorAt(4, 0);
helpers.doKeys('{');
helpers.assertCursorAt(0, 0);
helpers.doKeys('2', '}');
helpers.assertCursorAt(7, 0);
helpers.doKeys('2', '}');
helpers.assertCursorAt(16, 0);
cm.setCursor(9, 0);
helpers.doKeys('}');
helpers.assertCursorAt(14, 0);
cm.setCursor(6, 0);
helpers.doKeys('}');
helpers.assertCursorAt(7, 0);
// ip inside empty space
cm.setCursor(10, 0);
helpers.doKeys('v', 'i', 'p');
eqPos(Pos(7, 0), cm.getCursor('anchor'));
eqPos(Pos(12, 0), cm.getCursor('head'));
helpers.doKeys('i', 'p');
eqPos(Pos(7, 0), cm.getCursor('anchor'));
eqPos(Pos(13, 1), cm.getCursor('head'));
helpers.doKeys('2', 'i', 'p');
eqPos(Pos(7, 0), cm.getCursor('anchor'));
eqPos(Pos(16, 1), cm.getCursor('head'));
// should switch to visualLine mode
cm.setCursor(14, 0);
helpers.doKeys('<Esc>', 'v', 'i', 'p');
helpers.assertCursorAt(14, 0);
cm.setCursor(14, 0);
helpers.doKeys('<Esc>', 'V', 'i', 'p');
eqPos(Pos(16, 1), cm.getCursor('head'));
// ap inside empty space
cm.setCursor(10, 0);
helpers.doKeys('<Esc>', 'v', 'a', 'p');
eqPos(Pos(7, 0), cm.getCursor('anchor'));
eqPos(Pos(13, 1), cm.getCursor('head'));
helpers.doKeys('a', 'p');
eqPos(Pos(7, 0), cm.getCursor('anchor'));
eqPos(Pos(16, 1), cm.getCursor('head'));
cm.setCursor(13, 0);
helpers.doKeys('v', 'a', 'p');
eqPos(Pos(13, 0), cm.getCursor('anchor'));
eqPos(Pos(14, 0), cm.getCursor('head'));
cm.setCursor(16, 0);
helpers.doKeys('v', 'a', 'p');
eqPos(Pos(14, 0), cm.getCursor('anchor'));
eqPos(Pos(16, 1), cm.getCursor('head'));
cm.setCursor(0, 0);
helpers.doKeys('v', 'a', 'p');
eqPos(Pos(0, 0), cm.getCursor('anchor'));
eqPos(Pos(4, 0), cm.getCursor('head'));
cm.setCursor(0, 0);
helpers.doKeys('d', 'i', 'p');
var register = helpers.getRegisterController().getRegister();
eq('a\na\n', register.toString());
is(register.linewise);
helpers.doKeys('3', 'j', 'p');
helpers.doKeys('y', 'i', 'p');
is(register.linewise);
eq('b\na\na\nc\n', register.toString());
}, { value: 'a\na\n\n\n\nb\nc\n\n\n\n\n\n\nd\n\ne\nf' });
// Operator tests
testVim('dl', function(cm, vim, helpers) {
var curStart = makeCursor(0, 0);
cm.setCursor(curStart);
helpers.doKeys('d', 'l');
eq('word1 ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq(' ', register.toString());
is(!register.linewise);
eqPos(curStart, cm.getCursor());
}, { value: ' word1 ' });
testVim('dl_eol', function(cm, vim, helpers) {
cm.setCursor(0, 6);
helpers.doKeys('d', 'l');
eq(' word1', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq(' ', register.toString());
is(!register.linewise);
helpers.assertCursorAt(0, 5);
}, { value: ' word1 ' });
testVim('dl_repeat', function(cm, vim, helpers) {
var curStart = makeCursor(0, 0);
cm.setCursor(curStart);
helpers.doKeys('2', 'd', 'l');
eq('ord1 ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq(' w', register.toString());
is(!register.linewise);
eqPos(curStart, cm.getCursor());
}, { value: ' word1 ' });
testVim('dh', function(cm, vim, helpers) {
var curStart = makeCursor(0, 3);
cm.setCursor(curStart);
helpers.doKeys('d', 'h');
eq(' wrd1 ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('o', register.toString());
is(!register.linewise);
eqPos(offsetCursor(curStart, 0 , -1), cm.getCursor());
}, { value: ' word1 ' });
testVim('dj', function(cm, vim, helpers) {
var curStart = makeCursor(0, 3);
cm.setCursor(curStart);
helpers.doKeys('d', 'j');
eq(' word3', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq(' word1\nword2\n', register.toString());
is(register.linewise);
helpers.assertCursorAt(0, 1);
}, { value: ' word1\nword2\n word3' });
testVim('dj_end_of_document', function(cm, vim, helpers) {
var curStart = makeCursor(0, 3);
cm.setCursor(curStart);
helpers.doKeys('d', 'j');
eq(' word1 ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('', register.toString());
is(!register.linewise);
helpers.assertCursorAt(0, 3);
}, { value: ' word1 ' });
testVim('dk', function(cm, vim, helpers) {
var curStart = makeCursor(1, 3);
cm.setCursor(curStart);
helpers.doKeys('d', 'k');
eq(' word3', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq(' word1\nword2\n', register.toString());
is(register.linewise);
helpers.assertCursorAt(0, 1);
}, { value: ' word1\nword2\n word3' });
testVim('dk_start_of_document', function(cm, vim, helpers) {
var curStart = makeCursor(0, 3);
cm.setCursor(curStart);
helpers.doKeys('d', 'k');
eq(' word1 ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('', register.toString());
is(!register.linewise);
helpers.assertCursorAt(0, 3);
}, { value: ' word1 ' });
testVim('dw_space', function(cm, vim, helpers) {
var curStart = makeCursor(0, 0);
cm.setCursor(curStart);
helpers.doKeys('d', 'w');
eq('word1 ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq(' ', register.toString());
is(!register.linewise);
eqPos(curStart, cm.getCursor());
}, { value: ' word1 ' });
testVim('dw_word', function(cm, vim, helpers) {
var curStart = makeCursor(0, 1);
cm.setCursor(curStart);
helpers.doKeys('d', 'w');
eq(' word2', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('word1 ', register.toString());
is(!register.linewise);
eqPos(curStart, cm.getCursor());
}, { value: ' word1 word2' });
testVim('dw_unicode_word', function(cm, vim, helpers) {
helpers.doKeys('d', 'w');
eq(cm.getValue().length, 10);
helpers.doKeys('d', 'w');
eq(cm.getValue().length, 6);
helpers.doKeys('d', 'w');
eq(cm.getValue().length, 5);
helpers.doKeys('d', 'e');
eq(cm.getValue().length, 2);
}, { value: ' \u0562\u0561\u0580\u0587\xbbe\xb5g ' });
testVim('dw_only_word', function(cm, vim, helpers) {
// Test that if there is only 1 word left, dw deletes till the end of the
// line.
cm.setCursor(0, 1);
helpers.doKeys('d', 'w');
eq(' ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('word1 ', register.toString());
is(!register.linewise);
helpers.assertCursorAt(0, 0);
}, { value: ' word1 ' });
testVim('dw_eol', function(cm, vim, helpers) {
// Assert that dw does not delete the newline if last word to delete is at end
// of line.
cm.setCursor(0, 1);
helpers.doKeys('d', 'w');
eq(' \nword2', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('word1', register.toString());
is(!register.linewise);
helpers.assertCursorAt(0, 0);
}, { value: ' word1\nword2' });
testVim('dw_eol_with_multiple_newlines', function(cm, vim, helpers) {
// Assert that dw does not delete the newline if last word to delete is at end
// of line and it is followed by multiple newlines.
cm.setCursor(0, 1);
helpers.doKeys('d', 'w');
eq(' \n\nword2', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('word1', register.toString());
is(!register.linewise);
helpers.assertCursorAt(0, 0);
}, { value: ' word1\n\nword2' });
testVim('dw_empty_line_followed_by_whitespace', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'w');
eq(' \nword', cm.getValue());
}, { value: '\n \nword' });
testVim('dw_empty_line_followed_by_word', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'w');
eq('word', cm.getValue());
}, { value: '\nword' });
testVim('dw_empty_line_followed_by_empty_line', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'w');
eq('\n', cm.getValue());
}, { value: '\n\n' });
testVim('dw_whitespace_followed_by_whitespace', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'w');
eq('\n \n', cm.getValue());
}, { value: ' \n \n' });
testVim('dw_whitespace_followed_by_empty_line', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'w');
eq('\n\n', cm.getValue());
}, { value: ' \n\n' });
testVim('dw_word_whitespace_word', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'w');
eq('\n \nword2', cm.getValue());
}, { value: 'word1\n \nword2'})
testVim('dw_end_of_document', function(cm, vim, helpers) {
cm.setCursor(1, 2);
helpers.doKeys('d', 'w');
eq('\nab', cm.getValue());
}, { value: '\nabc' });
testVim('dw_repeat', function(cm, vim, helpers) {
// Assert that dw does delete newline if it should go to the next line, and
// that repeat works properly.
cm.setCursor(0, 1);
helpers.doKeys('d', '2', 'w');
eq(' ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('word1\nword2', register.toString());
is(!register.linewise);
helpers.assertCursorAt(0, 0);
}, { value: ' word1\nword2' });
testVim('de_word_start_and_empty_lines', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'e');
eq('\n\n', cm.getValue());
}, { value: 'word\n\n' });
testVim('de_word_end_and_empty_lines', function(cm, vim, helpers) {
cm.setCursor(0, 3);
helpers.doKeys('d', 'e');
eq('wor', cm.getValue());
}, { value: 'word\n\n\n' });
testVim('de_whitespace_and_empty_lines', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'e');
eq('', cm.getValue());
}, { value: ' \n\n\n' });
testVim('de_end_of_document', function(cm, vim, helpers) {
cm.setCursor(1, 2);
helpers.doKeys('d', 'e');
eq('\nab', cm.getValue());
}, { value: '\nabc' });
testVim('db_empty_lines', function(cm, vim, helpers) {
cm.setCursor(2, 0);
helpers.doKeys('d', 'b');
eq('\n\n', cm.getValue());
}, { value: '\n\n\n' });
testVim('db_word_start_and_empty_lines', function(cm, vim, helpers) {
cm.setCursor(2, 0);
helpers.doKeys('d', 'b');
eq('\nword', cm.getValue());
}, { value: '\n\nword' });
testVim('db_word_end_and_empty_lines', function(cm, vim, helpers) {
cm.setCursor(2, 3);
helpers.doKeys('d', 'b');
eq('\n\nd', cm.getValue());
}, { value: '\n\nword' });
testVim('db_whitespace_and_empty_lines', function(cm, vim, helpers) {
cm.setCursor(2, 0);
helpers.doKeys('d', 'b');
eq('', cm.getValue());
}, { value: '\n \n' });
testVim('db_start_of_document', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'b');
eq('abc\n', cm.getValue());
}, { value: 'abc\n' });
testVim('dge_empty_lines', function(cm, vim, helpers) {
cm.setCursor(1, 0);
helpers.doKeys('d', 'g', 'e');
// Note: In real VIM the result should be '', but it's not quite consistent,
// since 2 newlines are deleted. But in the similar case of word\n\n, only
// 1 newline is deleted. We'll diverge from VIM's behavior since it's much
// easier this way.
eq('\n', cm.getValue());
}, { value: '\n\n' });
testVim('dge_word_and_empty_lines', function(cm, vim, helpers) {
cm.setCursor(1, 0);
helpers.doKeys('d', 'g', 'e');
eq('wor\n', cm.getValue());
}, { value: 'word\n\n'});
testVim('dge_whitespace_and_empty_lines', function(cm, vim, helpers) {
cm.setCursor(2, 0);
helpers.doKeys('d', 'g', 'e');
eq('', cm.getValue());
}, { value: '\n \n' });
testVim('dge_start_of_document', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('d', 'g', 'e');
eq('bc\n', cm.getValue());
}, { value: 'abc\n' });
testVim('d_inclusive', function(cm, vim, helpers) {
// Assert that when inclusive is set, the character the cursor is on gets
// deleted too.
var curStart = makeCursor(0, 1);
cm.setCursor(curStart);
helpers.doKeys('d', 'e');
eq(' ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('word1', register.toString());
is(!register.linewise);
eqPos(curStart, cm.getCursor());
}, { value: ' word1 ' });
testVim('d_reverse', function(cm, vim, helpers) {
// Test that deleting in reverse works.
cm.setCursor(1, 0);
helpers.doKeys('d', 'b');
eq(' word2 ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('word1\n', register.toString());
is(!register.linewise);
helpers.assertCursorAt(0, 1);
}, { value: ' word1\nword2 ' });
testVim('dd', function(cm, vim, helpers) {
cm.setCursor(0, 3);
var expectedBuffer = cm.getRange({ line: 0, ch: 0 },
{ line: 1, ch: 0 });
var expectedLineCount = cm.lineCount() - 1;
helpers.doKeys('d', 'd');
eq(expectedLineCount, cm.lineCount());
var register = helpers.getRegisterController().getRegister();
eq(expectedBuffer, register.toString());
is(register.linewise);
helpers.assertCursorAt(0, lines[1].textStart);
});
testVim('dd_prefix_repeat', function(cm, vim, helpers) {
cm.setCursor(0, 3);
var expectedBuffer = cm.getRange({ line: 0, ch: 0 },
{ line: 2, ch: 0 });
var expectedLineCount = cm.lineCount() - 2;
helpers.doKeys('2', 'd', 'd');
eq(expectedLineCount, cm.lineCount());
var register = helpers.getRegisterController().getRegister();
eq(expectedBuffer, register.toString());
is(register.linewise);
helpers.assertCursorAt(0, lines[2].textStart);
});
testVim('dd_motion_repeat', function(cm, vim, helpers) {
cm.setCursor(0, 3);
var expectedBuffer = cm.getRange({ line: 0, ch: 0 },
{ line: 2, ch: 0 });
var expectedLineCount = cm.lineCount() - 2;
helpers.doKeys('d', '2', 'd');
eq(expectedLineCount, cm.lineCount());
var register = helpers.getRegisterController().getRegister();
eq(expectedBuffer, register.toString());
is(register.linewise);
helpers.assertCursorAt(0, lines[2].textStart);
});
testVim('dd_multiply_repeat', function(cm, vim, helpers) {
cm.setCursor(0, 3);
var expectedBuffer = cm.getRange({ line: 0, ch: 0 },
{ line: 6, ch: 0 });
var expectedLineCount = cm.lineCount() - 6;
helpers.doKeys('2', 'd', '3', 'd');
eq(expectedLineCount, cm.lineCount());
var register = helpers.getRegisterController().getRegister();
eq(expectedBuffer, register.toString());
is(register.linewise);
helpers.assertCursorAt(0, lines[6].textStart);
});
testVim('dd_lastline', function(cm, vim, helpers) {
cm.setCursor(cm.lineCount(), 0);
var expectedLineCount = cm.lineCount() - 1;
helpers.doKeys('d', 'd');
eq(expectedLineCount, cm.lineCount());
helpers.assertCursorAt(cm.lineCount() - 1, 0);
});
testVim('dd_only_line', function(cm, vim, helpers) {
cm.setCursor(0, 0);
var expectedRegister = cm.getValue() + "\n";
helpers.doKeys('d','d');
eq(1, cm.lineCount());
eq('', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq(expectedRegister, register.toString());
}, { value: "thisistheonlyline" });
// Yank commands should behave the exact same as d commands, expect that nothing
// gets deleted.
testVim('yw_repeat', function(cm, vim, helpers) {
// Assert that yw does yank newline if it should go to the next line, and
// that repeat works properly.
var curStart = makeCursor(0, 1);
cm.setCursor(curStart);
helpers.doKeys('y', '2', 'w');
eq(' word1\nword2', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('word1\nword2', register.toString());
is(!register.linewise);
eqPos(curStart, cm.getCursor());
}, { value: ' word1\nword2' });
testVim('yy_multiply_repeat', function(cm, vim, helpers) {
var curStart = makeCursor(0, 3);
cm.setCursor(curStart);
var expectedBuffer = cm.getRange({ line: 0, ch: 0 },
{ line: 6, ch: 0 });
var expectedLineCount = cm.lineCount();
helpers.doKeys('2', 'y', '3', 'y');
eq(expectedLineCount, cm.lineCount());
var register = helpers.getRegisterController().getRegister();
eq(expectedBuffer, register.toString());
is(register.linewise);
eqPos(curStart, cm.getCursor());
});
// Change commands behave like d commands except that it also enters insert
// mode. In addition, when the change is linewise, an additional newline is
// inserted so that insert mode starts on that line.
testVim('cw', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('c', '2', 'w');
eq(' word3', cm.getValue());
helpers.assertCursorAt(0, 0);
}, { value: 'word1 word2 word3'});
testVim('cw_repeat', function(cm, vim, helpers) {
// Assert that cw does delete newline if it should go to the next line, and
// that repeat works properly.
var curStart = makeCursor(0, 1);
cm.setCursor(curStart);
helpers.doKeys('c', '2', 'w');
eq(' ', cm.getValue());
var register = helpers.getRegisterController().getRegister();
eq('word1\nword2', register.toString());
is(!register.linewise);
eqPos(curStart, cm.getCursor());
eq('vim-insert', cm.getOption('keyMap'));
}, { value: ' word1\nword2' });
testVim('cc_multiply_repeat', function(cm, vim, helpers) {
cm.setCursor(0, 3);
var expectedBuffer = cm.getRange({ line: 0, ch: 0 },
{ line: 6, ch: 0 });
var expectedLineCount = cm.lineCount() - 5;
helpers.doKeys('2', 'c', '3', 'c');
eq(expectedLineCount, cm.lineCount());
var register = helpers.getRegisterController().getRegister();
eq(expectedBuffer, register.toString());
is(register.linewise);
eq('vim-insert', cm.getOption('keyMap'));
});
testVim('cc_should_not_append_to_document', function(cm, vim, helpers) {
var expectedLineCount = cm.lineCount();
cm.setCursor(cm.lastLine(), 0);
helpers.doKeys('c', 'c');
eq(expectedLineCount, cm.lineCount());
});
function fillArray(val, times) {
var arr = [];
for (var i = 0; i < times; i++) {
arr.push(val);
}
return arr;
}
testVim('c_visual_block', function(cm, vim, helpers) {