This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathEditor-test.js
More file actions
1940 lines (1728 loc) · 123 KB
/
Editor-test.js
File metadata and controls
1940 lines (1728 loc) · 123 KB
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
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define: false, describe: false, it: false, expect: false, beforeEach: false, afterEach: false, waitsFor: false, runs: false, $: false, jasmine: false */
define(function (require, exports, module) {
'use strict';
var Editor = require("editor/Editor").Editor,
EditorManager = require("editor/EditorManager"),
KeyEvent = require("utils/KeyEvent"),
LanguageManager = require("language/LanguageManager"),
PreferencesManager = require("preferences/PreferencesManager"),
SpecRunnerUtils = require("spec/SpecRunnerUtils");
var langNames = {
css: {mode: "css", langName: "CSS"},
javascript: {mode: "javascript", langName: "JavaScript"},
html: {mode: "html", langName: "HTML"},
unknown: {mode: null, langName: "Text"}
};
function compareMode(expected, actual) {
if (typeof actual === "string") {
return actual === expected;
} else if (actual === null) {
return expected === null;
}
return actual === expected;
}
function expectModeAndLang(editor, lang) {
expect(editor.getModeForSelection()).toSpecifyModeNamed(lang.mode);
expect(editor.getLanguageForSelection().getName()).toBe(lang.langName);
}
describe("Editor", function () {
var defaultContent = "Brackets is going to be awesome!\n";
var myDocument, myEditor;
function createTestEditor(content, languageId) {
// create dummy Document and Editor
var mocks = SpecRunnerUtils.createMockEditor(content, languageId);
myDocument = mocks.doc;
myEditor = mocks.editor;
}
beforeEach(function () {
this.addMatchers({
toSpecifyModeNamed: function (expected) {
return compareMode(expected, this.actual);
}
});
});
afterEach(function () {
if (myEditor) {
SpecRunnerUtils.destroyMockEditor(myDocument);
myEditor = null;
myDocument = null;
}
});
describe("Editor wrapper", function () {
beforeEach(function () {
createTestEditor(defaultContent, "");
});
it("should initialize with content", function () {
// verify editor content
expect(myEditor._codeMirror.getValue()).toEqual(defaultContent);
});
// FUTURE: this should really be in a Document unit test, but there's no "official"
// way to create the model for a Document without manually creating an Editor, so we're
// testing this here for now until we get a real central model.
it("should trigger a synchronous Document change event when an edit is performed", function () {
var changeFired = false;
function changeHandler(event, doc, changeList) {
$(myDocument).off("change", changeHandler);
changeFired = true;
expect(doc).toBe(myDocument);
expect(changeList.length).toBe(1);
expect(changeList[0].from).toEqual({line: 0, ch: 0});
expect(changeList[0].to).toEqual({line: 1, ch: 0});
expect(changeList[0].text).toEqual(["new content"]);
}
$(myDocument).on("change", changeHandler);
myEditor._codeMirror.setValue("new content");
expect(changeFired).toBe(true);
});
it("should send an array of multiple change records for an operation", function () {
var cm = myEditor._codeMirror,
changeHandler = jasmine.createSpy();
$(myDocument).on("change", changeHandler);
cm.operation(function () {
cm.replaceRange("inserted", {line: 1, ch: 0});
cm.replaceRange("", {line: 0, ch: 0}, {line: 0, ch: 4});
});
expect(changeHandler.callCount).toBe(1);
var args = changeHandler.mostRecentCall.args;
expect(args[1]).toBe(myDocument);
expect(args[2][0].text).toEqual(["inserted"]);
expect(args[2][0].from).toEqual({line: 1, ch: 0});
expect(args[2][0].to).toEqual({line: 1, ch: 0});
expect(args[2][1].text).toEqual([""]);
expect(args[2][1].from).toEqual({line: 0, ch: 0});
expect(args[2][1].to).toEqual({line: 0, ch: 4});
});
it("should set mode based on Document language", function () {
createTestEditor(defaultContent, "html");
var htmlLanguage = LanguageManager.getLanguage("html");
expect(myEditor.getModeForDocument()).toBe(htmlLanguage.getMode());
});
});
describe("Focus", function () {
beforeEach(function () {
createTestEditor(defaultContent, "");
});
it("should not have focus until explicitly set", function () {
expect(myEditor.hasFocus()).toBe(false);
});
it("should be able to detect when it has focus", function () {
myEditor.focus();
expect(myEditor.hasFocus()).toBe(true);
});
});
describe("getModeForSelection()", function () {
var jsContent = "var foo;";
var htmlContent = "<html><head>\n" +
" <script>\n" +
" var bar;\n" +
" </script>\n" +
"</head><body>\n" +
" <p>Hello</p>\n" +
"</body></html>";
it("should get mode in homogeneous file", function () {
createTestEditor(jsContent, langNames.javascript.mode);
// Mode at point
myEditor.setCursorPos(0, 0); // first char in text
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setCursorPos(0, 8); // last char in text
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setCursorPos(0, 3); // middle of text
expectModeAndLang(myEditor, langNames.javascript);
// Mode for range
myEditor.setSelection({line: 0, ch: 4}, {line: 0, ch: 7});
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setSelection({line: 0, ch: 0}, {line: 0, ch: 8}); // select all
expectModeAndLang(myEditor, langNames.javascript);
// Mode for multiple cursors/selections
myEditor.setSelections([{start: {line: 0, ch: 0}, end: {line: 0, ch: 0}},
{start: {line: 0, ch: 5}, end: {line: 0, ch: 5}}]);
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setSelections([{start: {line: 0, ch: 0}, end: {line: 0, ch: 3}},
{start: {line: 0, ch: 5}, end: {line: 0, ch: 7}}]);
expectModeAndLang(myEditor, langNames.javascript);
});
it("should get mode in HTML file", function () {
createTestEditor(htmlContent, "html");
// Mode at point
myEditor.setCursorPos(0, 0); // first char in text
expectModeAndLang(myEditor, langNames.html);
myEditor.setCursorPos(6, 14); // last char in text
expectModeAndLang(myEditor, langNames.html);
myEditor.setCursorPos(5, 7); // middle of text - html
expectModeAndLang(myEditor, langNames.html);
myEditor.setCursorPos(2, 7); // middle of text - js
expectModeAndLang(myEditor, langNames.javascript);
// Mode for range - homogeneous mode
myEditor.setSelection({line: 5, ch: 2}, {line: 5, ch: 14});
expectModeAndLang(myEditor, langNames.html);
myEditor.setSelection({line: 5, ch: 0}, {line: 6, ch: 0}); // whole line
expectModeAndLang(myEditor, langNames.html);
myEditor.setSelection({line: 2, ch: 4}, {line: 2, ch: 12});
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setSelection({line: 2, ch: 0}, {line: 3, ch: 0}); // whole line
expectModeAndLang(myEditor, langNames.javascript);
// Mode for multiple cursors/selections - homogeneous mode
myEditor.setSelections([{start: {line: 2, ch: 0}, end: {line: 2, ch: 0}},
{start: {line: 2, ch: 4}, end: {line: 2, ch: 4}}]);
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setSelections([{start: {line: 2, ch: 0}, end: {line: 2, ch: 2}},
{start: {line: 2, ch: 4}, end: {line: 2, ch: 6}}]);
expectModeAndLang(myEditor, langNames.javascript);
myEditor.setSelections([{start: {line: 0, ch: 0}, end: {line: 0, ch: 0}},
{start: {line: 5, ch: 7}, end: {line: 5, ch: 7}},
{start: {line: 6, ch: 14}, end: {line: 6, ch: 14}}]);
expectModeAndLang(myEditor, langNames.html);
myEditor.setSelections([{start: {line: 0, ch: 0}, end: {line: 0, ch: 2}},
{start: {line: 5, ch: 7}, end: {line: 5, ch: 9}},
{start: {line: 6, ch: 12}, end: {line: 6, ch: 14}}]);
expectModeAndLang(myEditor, langNames.html);
// Mode for range - mix of modes
myEditor.setSelection({line: 2, ch: 4}, {line: 3, ch: 7});
expectModeAndLang(myEditor, langNames.unknown);
// Mode for multiple cursors/selections - mix of modes
myEditor.setSelections([{start: {line: 0, ch: 0}, end: {line: 0, ch: 0}},
{start: {line: 2, ch: 4}, end: {line: 2, ch: 4}},
{start: {line: 6, ch: 14}, end: {line: 6, ch: 14}}]);
expectModeAndLang(myEditor, langNames.unknown);
myEditor.setSelections([{start: {line: 0, ch: 0}, end: {line: 0, ch: 2}},
{start: {line: 2, ch: 4}, end: {line: 2, ch: 7}},
{start: {line: 6, ch: 12}, end: {line: 6, ch: 14}}]);
expectModeAndLang(myEditor, langNames.unknown);
myEditor.setSelections([{start: {line: 0, ch: 0}, end: {line: 2, ch: 0}},
{start: {line: 2, ch: 4}, end: {line: 2, ch: 7}},
{start: {line: 6, ch: 12}, end: {line: 6, ch: 14}}]);
expectModeAndLang(myEditor, langNames.unknown);
myEditor.setSelections([{start: {line: 0, ch: 0}, end: {line: 0, ch: 2}},
{start: {line: 2, ch: 4}, end: {line: 5, ch: 3}},
{start: {line: 6, ch: 12}, end: {line: 6, ch: 14}}]);
expectModeAndLang(myEditor, langNames.unknown);
// Mode for range - mix of modes where start & endpoints are same mode
// Known limitation of getModeForSelection() that it does not spot where the mode
// differs in mid-selection
myEditor.setSelection({line: 0, ch: 0}, {line: 6, ch: 14}); // select all
expectModeAndLang(myEditor, langNames.html);
});
});
describe("Column/ch conversion", function () {
it("should get mode in HTML file", function () {
var content =
"foo () {\n" +
" one;\n" +
"\ttwo;\n" +
"}\n" +
"\n" +
"\tA\tB";
createTestEditor(content, "javascript");
// Tab size 4
expect(myEditor.getColOffset({line: 1, ch: 0})).toBe(0);
expect(myEditor.getColOffset({line: 1, ch: 1})).toBe(1);
expect(myEditor.getColOffset({line: 1, ch: 2})).toBe(2);
expect(myEditor.getColOffset({line: 1, ch: 3})).toBe(3);
expect(myEditor.getColOffset({line: 1, ch: 4})).toBe(4);
expect(myEditor.getColOffset({line: 1, ch: 5})).toBe(5);
expect(myEditor.getColOffset({line: 2, ch: 0})).toBe(0);
expect(myEditor.getColOffset({line: 2, ch: 1})).toBe(4);
expect(myEditor.getColOffset({line: 2, ch: 2})).toBe(5);
expect(myEditor.getColOffset({line: 4, ch: 0})).toBe(0);
expect(myEditor.getColOffset({line: 5, ch: 1})).toBe(4);
expect(myEditor.getColOffset({line: 5, ch: 2})).toBe(5);
expect(myEditor.getColOffset({line: 5, ch: 3})).toBe(8);
expect(myEditor.getColOffset({line: 5, ch: 4})).toBe(9);
// Tab size 2
Editor.setTabSize(2);
expect(myEditor.getColOffset({line: 1, ch: 0})).toBe(0); // first line is all spaces: should be unchanged
expect(myEditor.getColOffset({line: 1, ch: 1})).toBe(1);
expect(myEditor.getColOffset({line: 1, ch: 2})).toBe(2);
expect(myEditor.getColOffset({line: 1, ch: 3})).toBe(3);
expect(myEditor.getColOffset({line: 1, ch: 4})).toBe(4);
expect(myEditor.getColOffset({line: 1, ch: 5})).toBe(5);
expect(myEditor.getColOffset({line: 2, ch: 0})).toBe(0); // but line with a tab shows different behavior
expect(myEditor.getColOffset({line: 2, ch: 1})).toBe(2);
expect(myEditor.getColOffset({line: 2, ch: 2})).toBe(3);
expect(myEditor.getColOffset({line: 4, ch: 0})).toBe(0);
expect(myEditor.getColOffset({line: 5, ch: 1})).toBe(2); // same here
expect(myEditor.getColOffset({line: 5, ch: 2})).toBe(3);
expect(myEditor.getColOffset({line: 5, ch: 3})).toBe(4);
expect(myEditor.getColOffset({line: 5, ch: 4})).toBe(5);
// Restore default
Editor.setTabSize(4);
});
});
function makeDummyLines(num) {
var content = [], i;
for (i = 0; i < num; i++) {
content.push("this is line " + i);
}
return content;
}
describe("Selections", function () {
beforeEach(function () {
createTestEditor(makeDummyLines(10).join("\n"), "unknown");
});
describe("hasSelection", function () {
it("should return false for a single cursor", function () {
myEditor._codeMirror.setCursor(0, 2);
expect(myEditor.hasSelection()).toBe(false);
});
it("should return true for a single selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 1}, {line: 0, ch: 5});
expect(myEditor.hasSelection()).toBe(true);
});
it("should return false for multiple cursors", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 2);
expect(myEditor.hasSelection()).toBe(false);
});
it("should return true for multiple selections", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 2);
expect(myEditor.hasSelection()).toBe(true);
});
it("should return true for mixed cursors and selections", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 2);
expect(myEditor.hasSelection()).toBe(true);
});
});
describe("getCursorPos", function () {
it("should return a single cursor", function () {
myEditor._codeMirror.setCursor(0, 2);
expect(myEditor.getCursorPos()).toEqual({line: 0, ch: 2});
expect(myEditor.getCursorPos(false, "start")).toEqual({line: 0, ch: 2});
expect(myEditor.getCursorPos(false, "anchor")).toEqual({line: 0, ch: 2});
expect(myEditor.getCursorPos(false, "end")).toEqual({line: 0, ch: 2});
expect(myEditor.getCursorPos(false, "head")).toEqual({line: 0, ch: 2});
});
it("should return the correct ends of a single selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 1}, {line: 0, ch: 5});
expect(myEditor.getCursorPos()).toEqual({line: 0, ch: 5});
expect(myEditor.getCursorPos(false, "start")).toEqual({line: 0, ch: 1});
expect(myEditor.getCursorPos(false, "anchor")).toEqual({line: 0, ch: 1});
expect(myEditor.getCursorPos(false, "end")).toEqual({line: 0, ch: 5});
expect(myEditor.getCursorPos(false, "head")).toEqual({line: 0, ch: 5});
});
it("should return the default primary cursor in a multiple cursor selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 2);
expect(myEditor.getCursorPos()).toEqual({line: 2, ch: 1});
expect(myEditor.getCursorPos(false, "start")).toEqual({line: 2, ch: 1});
expect(myEditor.getCursorPos(false, "anchor")).toEqual({line: 2, ch: 1});
expect(myEditor.getCursorPos(false, "end")).toEqual({line: 2, ch: 1});
expect(myEditor.getCursorPos(false, "head")).toEqual({line: 2, ch: 1});
});
it("should return the specific primary cursor in a multiple cursor selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 1);
expect(myEditor.getCursorPos()).toEqual({line: 1, ch: 1});
expect(myEditor.getCursorPos(false, "start")).toEqual({line: 1, ch: 1});
expect(myEditor.getCursorPos(false, "anchor")).toEqual({line: 1, ch: 1});
expect(myEditor.getCursorPos(false, "end")).toEqual({line: 1, ch: 1});
expect(myEditor.getCursorPos(false, "head")).toEqual({line: 1, ch: 1});
});
it("should return the correct ends of the default primary selection in a multiple selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 2);
expect(myEditor.getCursorPos()).toEqual({line: 2, ch: 4});
expect(myEditor.getCursorPos(false, "start")).toEqual({line: 2, ch: 1});
expect(myEditor.getCursorPos(false, "anchor")).toEqual({line: 2, ch: 1});
expect(myEditor.getCursorPos(false, "end")).toEqual({line: 2, ch: 4});
expect(myEditor.getCursorPos(false, "head")).toEqual({line: 2, ch: 4});
});
it("should return the correct ends of a specific primary selection in a multiple selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 1);
expect(myEditor.getCursorPos()).toEqual({line: 1, ch: 4});
expect(myEditor.getCursorPos(false, "start")).toEqual({line: 1, ch: 1});
expect(myEditor.getCursorPos(false, "anchor")).toEqual({line: 1, ch: 1});
expect(myEditor.getCursorPos(false, "end")).toEqual({line: 1, ch: 4});
expect(myEditor.getCursorPos(false, "head")).toEqual({line: 1, ch: 4});
});
});
describe("setCursorPos", function () {
it("should replace an existing single cursor", function () {
myEditor._codeMirror.setCursor(0, 2);
myEditor.setCursorPos(1, 3);
expect(myEditor.getCursorPos()).toEqual({line: 1, ch: 3});
});
it("should replace an existing single selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 1}, {line: 0, ch: 5});
myEditor.setCursorPos(1, 3);
expect(myEditor.getCursorPos()).toEqual({line: 1, ch: 3});
});
it("should replace existing multiple cursors", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 2);
myEditor.setCursorPos(1, 3);
expect(myEditor.getCursorPos()).toEqual({line: 1, ch: 3});
});
it("should replace existing multiple selections", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 2);
myEditor.setCursorPos(1, 3);
expect(myEditor.getCursorPos()).toEqual({line: 1, ch: 3});
});
});
describe("getSelection", function () {
it("should return a single cursor", function () {
myEditor._codeMirror.setCursor(0, 2);
expect(myEditor.getSelection()).toEqual({start: {line: 0, ch: 2}, end: {line: 0, ch: 2}, reversed: false});
});
it("should return a single selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 1}, {line: 0, ch: 5});
expect(myEditor.getSelection()).toEqual({start: {line: 0, ch: 1}, end: {line: 0, ch: 5}, reversed: false});
});
it("should return a multiline selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 5}, {line: 1, ch: 3});
expect(myEditor.getSelection()).toEqual({start: {line: 0, ch: 5}, end: {line: 1, ch: 3}, reversed: false});
});
it("should return a single selection in the proper order when reversed", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 5}, {line: 0, ch: 1});
expect(myEditor.getSelection()).toEqual({start: {line: 0, ch: 1}, end: {line: 0, ch: 5}, reversed: true});
});
it("should return a multiline selection in the proper order when reversed", function () {
myEditor._codeMirror.setSelection({line: 1, ch: 3}, {line: 0, ch: 5});
expect(myEditor.getSelection()).toEqual({start: {line: 0, ch: 5}, end: {line: 1, ch: 3}, reversed: true});
});
it("should return the default primary cursor in a multiple cursor selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 2);
expect(myEditor.getSelection()).toEqual({start: {line: 2, ch: 1}, end: {line: 2, ch: 1}, reversed: false});
});
it("should return the specific primary cursor in a multiple cursor selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 1);
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 1}, end: {line: 1, ch: 1}, reversed: false});
});
it("should return the default primary selection in a multiple selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 2);
expect(myEditor.getSelection()).toEqual({start: {line: 2, ch: 1}, end: {line: 2, ch: 4}, reversed: false});
});
it("should return the default primary selection in the proper order when reversed", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 4}, head: {line: 2, ch: 1}}
], 2);
expect(myEditor.getSelection()).toEqual({start: {line: 2, ch: 1}, end: {line: 2, ch: 4}, reversed: true});
});
it("should return the specific primary selection in a multiple selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 1);
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 1}, end: {line: 1, ch: 4}, reversed: false});
});
it("should return the specific primary selection in the proper order when reversed", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 4}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 1);
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 1}, end: {line: 1, ch: 4}, reversed: true});
});
});
describe("getSelections", function () {
it("should return a single cursor", function () {
myEditor._codeMirror.setCursor(0, 2);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 2}, end: {line: 0, ch: 2}, reversed: false, primary: true}]);
});
it("should return a single selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 1}, {line: 0, ch: 5});
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 0, ch: 5}, reversed: false, primary: true}]);
});
it("should properly reverse a single selection whose head is before its anchor", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 5}, {line: 0, ch: 1});
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 0, ch: 5}, reversed: true, primary: true}]);
});
it("should return multiple cursors", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 2);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 0, ch: 1}, reversed: false, primary: false},
{start: {line: 1, ch: 1}, end: {line: 1, ch: 1}, reversed: false, primary: false},
{start: {line: 2, ch: 1}, end: {line: 2, ch: 1}, reversed: false, primary: true}
]);
});
it("should return a multiple selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 2);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 0, ch: 4}, reversed: false, primary: false},
{start: {line: 1, ch: 1}, end: {line: 1, ch: 4}, reversed: false, primary: false},
{start: {line: 2, ch: 1}, end: {line: 2, ch: 4}, reversed: false, primary: true}
]);
});
it("should properly reverse selections whose heads are before their anchors in a multiple selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 4}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 4}, head: {line: 2, ch: 1}}
], 2);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 0, ch: 4}, reversed: true, primary: false},
{start: {line: 1, ch: 1}, end: {line: 1, ch: 4}, reversed: false, primary: false},
{start: {line: 2, ch: 1}, end: {line: 2, ch: 4}, reversed: true, primary: true}
]);
});
it("should properly reverse multiline selections whose heads are before their anchors in a multiple selection", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 1, ch: 3}, head: {line: 0, ch: 5}},
{anchor: {line: 4, ch: 4}, head: {line: 3, ch: 1}}
], 1);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 5}, end: {line: 1, ch: 3}, reversed: true, primary: false},
{start: {line: 3, ch: 1}, end: {line: 4, ch: 4}, reversed: true, primary: true}
]);
});
});
describe("getSelectedText", function () {
it("should return empty string for a cursor", function () {
myEditor._codeMirror.setCursor(0, 2);
expect(myEditor.getSelectedText()).toEqual("");
expect(myEditor.getSelectedText(true)).toEqual("");
});
it("should return the contents of a single selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 8}, {line: 0, ch: 14});
expect(myEditor.getSelectedText()).toEqual("line 0");
expect(myEditor.getSelectedText(true)).toEqual("line 0");
});
it("should return the primary selection by default, but concatenate contents if allSelections is true", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}},
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
{anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}}
], 2);
expect(myEditor.getSelectedText()).toEqual("line 2");
expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2");
});
it("should return a primary selection other than the last", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}},
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
{anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}}
], 1);
expect(myEditor.getSelectedText()).toEqual("line 1");
expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2");
});
it("should return the contents of a multiple selection when some selections are reversed", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 14}, head: {line: 0, ch: 8}},
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
{anchor: {line: 2, ch: 14}, head: {line: 2, ch: 8}}
], 2);
expect(myEditor.getSelectedText()).toEqual("line 2");
expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2");
});
});
describe("setSelection", function () {
it("should replace an existing single cursor", function () {
myEditor._codeMirror.setCursor(0, 2);
myEditor.setSelection({line: 1, ch: 3}, {line: 2, ch: 5});
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 3}, end: {line: 2, ch: 5}, reversed: false});
});
it("should replace an existing single selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 1}, {line: 0, ch: 5});
myEditor.setSelection({line: 1, ch: 3}, {line: 2, ch: 5});
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 3}, end: {line: 2, ch: 5}, reversed: false});
});
it("should allow implicit end", function () {
myEditor.setSelection({line: 1, ch: 3});
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 3}, end: {line: 1, ch: 3}, reversed: false});
});
it("should replace existing multiple cursors", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 2);
myEditor.setSelection({line: 1, ch: 3}, {line: 2, ch: 5});
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 3}, end: {line: 2, ch: 5}, reversed: false});
});
it("should replace existing multiple selections", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 2);
myEditor.setSelection({line: 1, ch: 3}, {line: 2, ch: 5});
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 3}, end: {line: 2, ch: 5}, reversed: false});
});
});
describe("setSelections", function () {
it("should replace an existing single cursor", function () {
myEditor._codeMirror.setCursor(0, 2);
myEditor.setSelections([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}}]);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}, reversed: false, primary: false},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false, primary: true}]);
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false});
});
it("should replace an existing single selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 1}, {line: 0, ch: 5});
myEditor.setSelections([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}}]);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}, reversed: false, primary: false},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false, primary: true}]);
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false});
});
it("should replace existing multiple cursors", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 1}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 1}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 1}}
], 2);
myEditor.setSelections([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}}]);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}, reversed: false, primary: false},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false, primary: true}]);
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false});
});
it("should replace existing multiple selections", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 1}, head: {line: 0, ch: 4}},
{anchor: {line: 1, ch: 1}, head: {line: 1, ch: 4}},
{anchor: {line: 2, ch: 1}, head: {line: 2, ch: 4}}
], 2);
myEditor.setSelections([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}}]);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}, reversed: false, primary: false},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false, primary: true}]);
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false});
});
it("should specify non-default primary selection", function () {
myEditor.setSelections([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}, primary: true},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}}]);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}, reversed: false, primary: true},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false, primary: false}]);
expect(myEditor.getSelection()).toEqual({start: {line: 0, ch: 1}, end: {line: 1, ch: 3}, reversed: false});
});
it("should sort and merge overlapping selections", function () {
myEditor.setSelections([{start: {line: 2, ch: 4}, end: {line: 3, ch: 0}},
{start: {line: 2, ch: 3}, end: {line: 2, ch: 6}},
{start: {line: 1, ch: 1}, end: {line: 1, ch: 4}}]);
expect(myEditor.getSelections()).toEqual([{start: {line: 1, ch: 1}, end: {line: 1, ch: 4}, reversed: false, primary: true},
{start: {line: 2, ch: 3}, end: {line: 3, ch: 0}, reversed: false, primary: false}]);
expect(myEditor.getSelection()).toEqual({start: {line: 1, ch: 1}, end: {line: 1, ch: 4}, reversed: false});
});
it("should properly set reversed selections", function () {
myEditor.setSelections([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}, reversed: true},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}}]);
expect(myEditor.getSelections()).toEqual([{start: {line: 0, ch: 1}, end: {line: 1, ch: 3}, reversed: true, primary: false},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 5}, reversed: false, primary: true}]);
});
});
describe("convertToLineSelections", function () {
it("should expand a cursor to a line selection, keeping original selection for tracking", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 0, ch: 4}}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(1);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 1, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(1);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
});
it("should expand a range within a line to a line selection, keeping original selection for tracking", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 0, ch: 8}}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(1);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 1, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(1);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
});
it("should expand a range that spans multiple lines to a line selection", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 1, ch: 8}}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(1);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 2, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(1);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
});
it("should preserve the reversed attribute on a tracked range", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 0, ch: 8}, reversed: true}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(1);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 1, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(1);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
});
it("should keep a line selection the same if expandEndAtStartOfLine is not set", function () {
var origSelections = [{start: {line: 0, ch: 0}, end: {line: 1, ch: 0}}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(1);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 1, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(1);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
});
it("should expand a line selection if expandEndAtStartOfLine is set", function () {
var origSelections = [{start: {line: 0, ch: 0}, end: {line: 1, ch: 0}}],
result = myEditor.convertToLineSelections(origSelections, {expandEndAtStartOfLine: true});
expect(result.length).toBe(1);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 2, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(1);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
});
it("should process a discontiguous mix of cursor, range, and line selections separately, preserving the primary tracked selection", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 0, ch: 4}},
{start: {line: 2, ch: 4}, end: {line: 2, ch: 8}, primary: true},
{start: {line: 4, ch: 4}, end: {line: 5, ch: 8}},
{start: {line: 7, ch: 0}, end: {line: 8, ch: 0}}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(4);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 1, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(1);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
expect(result[1].selectionForEdit.start).toEqual({line: 2, ch: 0});
expect(result[1].selectionForEdit.end).toEqual({line: 3, ch: 0});
expect(result[1].selectionsToTrack.length).toBe(1);
expect(result[1].selectionsToTrack[0]).toEqual(origSelections[1]);
expect(result[2].selectionForEdit.start).toEqual({line: 4, ch: 0});
expect(result[2].selectionForEdit.end).toEqual({line: 6, ch: 0});
expect(result[2].selectionsToTrack.length).toBe(1);
expect(result[2].selectionsToTrack[0]).toEqual(origSelections[2]);
expect(result[3].selectionForEdit.start).toEqual({line: 7, ch: 0});
expect(result[3].selectionForEdit.end).toEqual({line: 8, ch: 0}); // not expanded since expandEndAtStartOfLine is false
expect(result[3].selectionsToTrack.length).toBe(1);
expect(result[3].selectionsToTrack[0]).toEqual(origSelections[3]);
});
it("should merge selections on same line, preserving primary/reversed info on subsumed selections", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 0, ch: 4}},
{start: {line: 0, ch: 8}, end: {line: 1, ch: 8}, primary: true, reversed: true},
{start: {line: 4, ch: 0}, end: {line: 5, ch: 0}}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(2);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 2, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(2);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
expect(result[0].selectionsToTrack[1]).toEqual(origSelections[1]);
expect(result[1].selectionForEdit.start).toEqual({line: 4, ch: 0});
expect(result[1].selectionForEdit.end).toEqual({line: 5, ch: 0});
expect(result[1].selectionsToTrack.length).toBe(1);
expect(result[1].selectionsToTrack[0]).toEqual(origSelections[2]);
});
it("should merge selections on adjacent lines by default", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 0, ch: 4}},
{start: {line: 1, ch: 4}, end: {line: 1, ch: 4}, primary: true},
{start: {line: 4, ch: 0}, end: {line: 5, ch: 0}}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(2);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 2, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(2);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
expect(result[0].selectionsToTrack[1]).toEqual(origSelections[1]);
expect(result[1].selectionForEdit.start).toEqual({line: 4, ch: 0});
expect(result[1].selectionForEdit.end).toEqual({line: 5, ch: 0});
expect(result[1].selectionsToTrack.length).toBe(1);
expect(result[1].selectionsToTrack[0]).toEqual(origSelections[2]);
});
it("should merge adjacent multiline selections where the first selection ends on the same line where the second selection starts", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 1, ch: 4}, primary: true},
{start: {line: 1, ch: 8}, end: {line: 2, ch: 8}}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(1);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 3, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(2);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
expect(result[0].selectionsToTrack[1]).toEqual(origSelections[1]);
});
it("should not merge selections on adjacent lines if mergeAdjacent is false", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 0, ch: 4}},
{start: {line: 1, ch: 4}, end: {line: 1, ch: 4}, primary: true},
{start: {line: 4, ch: 0}, end: {line: 5, ch: 0}}],
result = myEditor.convertToLineSelections(origSelections, {mergeAdjacent: false});
expect(result.length).toBe(3);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 1, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(1);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
expect(result[1].selectionForEdit.start).toEqual({line: 1, ch: 0});
expect(result[1].selectionForEdit.end).toEqual({line: 2, ch: 0});
expect(result[1].selectionsToTrack.length).toBe(1);
expect(result[1].selectionsToTrack[0]).toEqual(origSelections[1]);
expect(result[2].selectionForEdit.start).toEqual({line: 4, ch: 0});
expect(result[2].selectionForEdit.end).toEqual({line: 5, ch: 0}); // not expanded since expandEndAtStartOfLine not set
expect(result[2].selectionsToTrack.length).toBe(1);
expect(result[2].selectionsToTrack[0]).toEqual(origSelections[2]);
});
it("should merge line selections separated by a one-line gap by default if expandEndAtStartOfLine is true", function () {
var origSelections = [{start: {line: 0, ch: 0}, end: {line: 1, ch: 0}, primary: true},
{start: {line: 2, ch: 0}, end: {line: 3, ch: 0}}],
result = myEditor.convertToLineSelections(origSelections, {expandEndAtStartOfLine: true});
expect(result.length).toBe(1);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 4, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(2);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
expect(result[0].selectionsToTrack[1]).toEqual(origSelections[1]);
});
it("should not merge line selections separated by a one-line gap if expandEndAtStartOfLine is true but mergeAdjacent is false", function () {
// Note that in this case, if you were to actually set this as a multiple selection, CodeMirror would
// merge the adjacent selections at that point. But while processing an edit you might not want that.
var origSelections = [{start: {line: 0, ch: 0}, end: {line: 1, ch: 0}, primary: true},
{start: {line: 2, ch: 0}, end: {line: 3, ch: 0}}],
result = myEditor.convertToLineSelections(origSelections, {expandEndAtStartOfLine: true, mergeAdjacent: false});
expect(result.length).toBe(2);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 2, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(1);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
expect(result[1].selectionForEdit.start).toEqual({line: 2, ch: 0});
expect(result[1].selectionForEdit.end).toEqual({line: 4, ch: 0});
expect(result[1].selectionsToTrack.length).toBe(1);
expect(result[1].selectionsToTrack[0]).toEqual(origSelections[1]);
});
it("should merge multiple adjacent/overlapping selections together", function () {
var origSelections = [{start: {line: 0, ch: 4}, end: {line: 0, ch: 4}},
{start: {line: 1, ch: 4}, end: {line: 2, ch: 4}, primary: true, reversed: true},
{start: {line: 2, ch: 8}, end: {line: 5, ch: 0}}],
result = myEditor.convertToLineSelections(origSelections);
expect(result.length).toBe(1);
expect(result[0].selectionForEdit.start).toEqual({line: 0, ch: 0});
expect(result[0].selectionForEdit.end).toEqual({line: 5, ch: 0});
expect(result[0].selectionsToTrack.length).toBe(3);
expect(result[0].selectionsToTrack[0]).toEqual(origSelections[0]);
expect(result[0].selectionsToTrack[1]).toEqual(origSelections[1]);
expect(result[0].selectionsToTrack[2]).toEqual(origSelections[2]);
});
});
});
describe("Soft tabs", function () {
beforeEach(function () {
// Configure the editor's CM instance for 4-space soft tabs, regardless of prefs.
createTestEditor("", "unknown");
var instance = myEditor._codeMirror;
instance.setOption("indentWithTabs", false);
instance.setOption("indentUnit", 4);
});
function checkSoftTab(sel, dir, command, expectedSel, expectedText) {
var input, endPos;
expectedText = expectedText || myEditor.document.getText();
if (Array.isArray(sel)) {
myEditor.setSelections(sel);
} else {
myEditor.setCursorPos(sel);
}
myEditor._handleSoftTabNavigation(dir, command);
if (Array.isArray(expectedSel)) {
expect(myEditor.getSelections()).toEqual(expectedSel);
} else {
expect(myEditor.getCursorPos()).toEqual(expectedSel);
}
expect(myEditor.document.getText()).toEqual(expectedText);
}
it("should move left by a soft tab if cursor is immediately after 1 indent level worth of spaces at beginning of line", function () {
myEditor.document.setText(" content");
checkSoftTab({line: 0, ch: 4}, -1, "moveH", {line: 0, ch: 0});
});
it("should backspace by a soft tab if cursor is immediately after 1 indent level worth of spaces at beginning of line", function () {
myEditor.document.setText(" content");
checkSoftTab({line: 0, ch: 4}, -1, "deleteH", {line: 0, ch: 0}, "content");
});
it("should move right by a soft tab if cursor is immediately before 1 indent level worth of spaces at beginning of line", function () {
myEditor.document.setText(" content");
checkSoftTab({line: 0, ch: 0}, 1, "moveH", {line: 0, ch: 4});
});
it("should delete right by a soft tab if cursor is immediately before 1 indent level worth of spaces at beginning of line", function () {
myEditor.document.setText(" content");
checkSoftTab({line: 0, ch: 0}, 1, "deleteH", {line: 0, ch: 0}, "content");
});
it("should move left by a soft tab if cursor is immediately after 2 indent levels worth of spaces at beginning of line", function () {
myEditor.document.setText(" content");
checkSoftTab({line: 0, ch: 8}, -1, "moveH", {line: 0, ch: 4});
});
it("should backspace by a soft tab if cursor is immediately after 2 indent levels worth of spaces at beginning of line", function () {
myEditor.document.setText(" content");