-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-editor-spec.js
8879 lines (7689 loc) · 333 KB
/
text-editor-spec.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
const fs = require('fs');
const path = require('path');
const temp = require('temp').track();
const dedent = require('dedent');
const { clipboard } = require('electron');
const TextEditor = require('../src/text-editor');
const TextBuffer = require('text-buffer');
const TextMateLanguageMode = require('../src/text-mate-language-mode');
const TreeSitterLanguageMode = require('../src/tree-sitter-language-mode');
describe('TextEditor', () => {
let buffer, editor, lineLengths;
beforeEach(async () => {
editor = await atom.workspace.open('sample.js');
buffer = editor.buffer;
editor.update({ autoIndent: false });
lineLengths = buffer.getLines().map(line => line.length);
await atom.packages.activatePackage('language-javascript');
});
it('generates unique ids for each editor', async () => {
// Deserialized editors are initialized with the serialized id. We can
// initialize an editor with what we expect to be the next id:
const deserialized = new TextEditor({ id: editor.id + 1 });
expect(deserialized.id).toEqual(editor.id + 1);
// The id generator should skip the id used up by the deserialized one:
const fresh = new TextEditor();
expect(fresh.id).toNotEqual(deserialized.id);
});
describe('when the editor is deserialized', () => {
it('restores selections and folds based on markers in the buffer', async () => {
editor.setSelectedBufferRange([[1, 2], [3, 4]]);
editor.addSelectionForBufferRange([[5, 6], [7, 5]], { reversed: true });
editor.foldBufferRow(4);
expect(editor.isFoldedAtBufferRow(4)).toBeTruthy();
const buffer2 = await TextBuffer.deserialize(editor.buffer.serialize());
const editor2 = TextEditor.deserialize(editor.serialize(), {
assert: atom.assert,
textEditors: atom.textEditors,
project: {
bufferForIdSync() {
return buffer2;
}
}
});
expect(editor2.id).toBe(editor.id);
expect(editor2.getBuffer().getPath()).toBe(editor.getBuffer().getPath());
expect(editor2.getSelectedBufferRanges()).toEqual([
[[1, 2], [3, 4]],
[[5, 6], [7, 5]]
]);
expect(editor2.getSelections()[1].isReversed()).toBeTruthy();
expect(editor2.isFoldedAtBufferRow(4)).toBeTruthy();
editor2.destroy();
});
it("restores the editor's layout configuration", async () => {
editor.update({
softTabs: true,
atomicSoftTabs: false,
tabLength: 12,
softWrapped: true,
softWrapAtPreferredLineLength: true,
softWrapHangingIndentLength: 8,
invisibles: { space: 'S' },
showInvisibles: true,
editorWidthInChars: 120
});
// Force buffer and display layer to be deserialized as well, rather than
// reusing the same buffer instance
const buffer2 = await TextBuffer.deserialize(editor.buffer.serialize());
const editor2 = TextEditor.deserialize(editor.serialize(), {
assert: atom.assert,
textEditors: atom.textEditors,
project: {
bufferForIdSync() {
return buffer2;
}
}
});
expect(editor2.getSoftTabs()).toBe(editor.getSoftTabs());
expect(editor2.hasAtomicSoftTabs()).toBe(editor.hasAtomicSoftTabs());
expect(editor2.getTabLength()).toBe(editor.getTabLength());
expect(editor2.getSoftWrapColumn()).toBe(editor.getSoftWrapColumn());
expect(editor2.getSoftWrapHangingIndentLength()).toBe(
editor.getSoftWrapHangingIndentLength()
);
expect(editor2.getInvisibles()).toEqual(editor.getInvisibles());
expect(editor2.getEditorWidthInChars()).toBe(
editor.getEditorWidthInChars()
);
expect(editor2.displayLayer.tabLength).toBe(editor2.getTabLength());
expect(editor2.displayLayer.softWrapColumn).toBe(
editor2.getSoftWrapColumn()
);
});
it('ignores buffers with retired IDs', () => {
const editor2 = TextEditor.deserialize(editor.serialize(), {
assert: atom.assert,
textEditors: atom.textEditors,
project: {
bufferForIdSync() {
return null;
}
}
});
expect(editor2).toBeNull();
});
});
describe('.copy()', () => {
it('returns a different editor with the same initial state', () => {
expect(editor.getAutoHeight()).toBeFalsy();
expect(editor.getAutoWidth()).toBeFalsy();
expect(editor.getShowCursorOnSelection()).toBeTruthy();
const element = editor.getElement();
element.setHeight(100);
element.setWidth(100);
jasmine.attachToDOM(element);
editor.update({ showCursorOnSelection: false });
editor.setSelectedBufferRange([[1, 2], [3, 4]]);
editor.addSelectionForBufferRange([[5, 6], [7, 8]], { reversed: true });
editor.setScrollTopRow(3);
expect(editor.getScrollTopRow()).toBe(3);
editor.setScrollLeftColumn(4);
expect(editor.getScrollLeftColumn()).toBe(4);
editor.foldBufferRow(4);
expect(editor.isFoldedAtBufferRow(4)).toBeTruthy();
const editor2 = editor.copy();
const element2 = editor2.getElement();
element2.setHeight(100);
element2.setWidth(100);
jasmine.attachToDOM(element2);
expect(editor2.id).not.toBe(editor.id);
expect(editor2.getSelectedBufferRanges()).toEqual(
editor.getSelectedBufferRanges()
);
expect(editor2.getSelections()[1].isReversed()).toBeTruthy();
expect(editor2.getScrollTopRow()).toBe(3);
expect(editor2.getScrollLeftColumn()).toBe(4);
expect(editor2.isFoldedAtBufferRow(4)).toBeTruthy();
expect(editor2.getAutoWidth()).toBe(false);
expect(editor2.getAutoHeight()).toBe(false);
expect(editor2.getShowCursorOnSelection()).toBeFalsy();
// editor2 can now diverge from its origin edit session
editor2.getLastSelection().setBufferRange([[2, 1], [4, 3]]);
expect(editor2.getSelectedBufferRanges()).not.toEqual(
editor.getSelectedBufferRanges()
);
editor2.unfoldBufferRow(4);
expect(editor2.isFoldedAtBufferRow(4)).not.toBe(
editor.isFoldedAtBufferRow(4)
);
});
});
describe('.update()', () => {
it('updates the editor with the supplied config parameters', () => {
let changeSpy;
const { element } = editor; // force element initialization
element.setUpdatedSynchronously(false);
editor.update({ showInvisibles: true });
editor.onDidChange((changeSpy = jasmine.createSpy('onDidChange')));
const returnedPromise = editor.update({
tabLength: 6,
softTabs: false,
softWrapped: true,
editorWidthInChars: 40,
showInvisibles: false,
mini: false,
lineNumberGutterVisible: false,
scrollPastEnd: true,
autoHeight: false,
maxScreenLineLength: 1000
});
expect(returnedPromise).toBe(element.component.getNextUpdatePromise());
expect(changeSpy.callCount).toBe(1);
expect(editor.getTabLength()).toBe(6);
expect(editor.getSoftTabs()).toBe(false);
expect(editor.isSoftWrapped()).toBe(true);
expect(editor.getEditorWidthInChars()).toBe(40);
expect(editor.getInvisibles()).toEqual({});
expect(editor.isMini()).toBe(false);
expect(editor.isLineNumberGutterVisible()).toBe(false);
expect(editor.getScrollPastEnd()).toBe(true);
expect(editor.getAutoHeight()).toBe(false);
});
});
describe('title', () => {
describe('.getTitle()', () => {
it("uses the basename of the buffer's path as its title, or 'untitled' if the path is undefined", () => {
expect(editor.getTitle()).toBe('sample.js');
buffer.setPath(undefined);
expect(editor.getTitle()).toBe('untitled');
});
});
describe('.getLongTitle()', () => {
it('returns file name when there is no opened file with identical name', () => {
expect(editor.getLongTitle()).toBe('sample.js');
buffer.setPath(undefined);
expect(editor.getLongTitle()).toBe('untitled');
});
it("returns '<filename> — <parent-directory>' when opened files have identical file names", async () => {
const editor1 = await atom.workspace.open(
path.join('sample-theme-1', 'readme')
);
const editor2 = await atom.workspace.open(
path.join('sample-theme-2', 'readme')
);
expect(editor1.getLongTitle()).toBe('readme \u2014 sample-theme-1');
expect(editor2.getLongTitle()).toBe('readme \u2014 sample-theme-2');
});
it("returns '<filename> — <parent-directories>' when opened files have identical file names in subdirectories", async () => {
const path1 = path.join('sample-theme-1', 'src', 'js');
const path2 = path.join('sample-theme-2', 'src', 'js');
const editor1 = await atom.workspace.open(path.join(path1, 'main.js'));
const editor2 = await atom.workspace.open(path.join(path2, 'main.js'));
expect(editor1.getLongTitle()).toBe(`main.js \u2014 ${path1}`);
expect(editor2.getLongTitle()).toBe(`main.js \u2014 ${path2}`);
});
it("returns '<filename> — <parent-directories>' when opened files have identical file and same parent dir name", async () => {
const editor1 = await atom.workspace.open(
path.join('sample-theme-2', 'src', 'js', 'main.js')
);
const editor2 = await atom.workspace.open(
path.join('sample-theme-2', 'src', 'js', 'plugin', 'main.js')
);
expect(editor1.getLongTitle()).toBe('main.js \u2014 js');
expect(editor2.getLongTitle()).toBe(
`main.js \u2014 ${path.join('js', 'plugin')}`
);
});
it('returns the filename when the editor is not in the workspace', async () => {
editor.onDidDestroy(() => {
expect(editor.getLongTitle()).toBe('sample.js');
});
await atom.workspace.getActivePane().close();
expect(editor.isDestroyed()).toBe(true);
});
});
it('notifies ::onDidChangeTitle observers when the underlying buffer path changes', () => {
const observed = [];
editor.onDidChangeTitle(title => observed.push(title));
buffer.setPath('/foo/bar/baz.txt');
buffer.setPath(undefined);
expect(observed).toEqual(['baz.txt', 'untitled']);
});
});
describe('path', () => {
it('notifies ::onDidChangePath observers when the underlying buffer path changes', () => {
const observed = [];
editor.onDidChangePath(filePath => observed.push(filePath));
buffer.setPath(__filename);
buffer.setPath(undefined);
expect(observed).toEqual([__filename, undefined]);
});
});
describe('encoding', () => {
it('notifies ::onDidChangeEncoding observers when the editor encoding changes', () => {
const observed = [];
editor.onDidChangeEncoding(encoding => observed.push(encoding));
editor.setEncoding('utf16le');
editor.setEncoding('utf16le');
editor.setEncoding('utf16be');
editor.setEncoding();
editor.setEncoding();
expect(observed).toEqual(['utf16le', 'utf16be', 'utf8']);
});
});
describe('cursor', () => {
describe('.getLastCursor()', () => {
it('returns the most recently created cursor', () => {
editor.addCursorAtScreenPosition([1, 0]);
const lastCursor = editor.addCursorAtScreenPosition([2, 0]);
expect(editor.getLastCursor()).toBe(lastCursor);
});
it('creates a new cursor at (0, 0) if the last cursor has been destroyed', () => {
editor.getLastCursor().destroy();
expect(editor.getLastCursor().getBufferPosition()).toEqual([0, 0]);
});
});
describe('.getCursors()', () => {
it('creates a new cursor at (0, 0) if the last cursor has been destroyed', () => {
editor.getLastCursor().destroy();
expect(editor.getCursors()[0].getBufferPosition()).toEqual([0, 0]);
});
});
describe('when the cursor moves', () => {
it('clears a goal column established by vertical movement', () => {
editor.setText('b');
editor.setCursorBufferPosition([0, 0]);
editor.insertNewline();
editor.moveUp();
editor.insertText('a');
editor.moveDown();
expect(editor.getCursorBufferPosition()).toEqual([1, 1]);
});
it('emits an event with the old position, new position, and the cursor that moved', () => {
const cursorCallback = jasmine.createSpy('cursor-changed-position');
const editorCallback = jasmine.createSpy(
'editor-changed-cursor-position'
);
editor.getLastCursor().onDidChangePosition(cursorCallback);
editor.onDidChangeCursorPosition(editorCallback);
editor.setCursorBufferPosition([2, 4]);
expect(editorCallback).toHaveBeenCalled();
expect(cursorCallback).toHaveBeenCalled();
const eventObject = editorCallback.mostRecentCall.args[0];
expect(cursorCallback.mostRecentCall.args[0]).toEqual(eventObject);
expect(eventObject.oldBufferPosition).toEqual([0, 0]);
expect(eventObject.oldScreenPosition).toEqual([0, 0]);
expect(eventObject.newBufferPosition).toEqual([2, 4]);
expect(eventObject.newScreenPosition).toEqual([2, 4]);
expect(eventObject.cursor).toBe(editor.getLastCursor());
});
});
describe('.setCursorScreenPosition(screenPosition)', () => {
it('clears a goal column established by vertical movement', () => {
// set a goal column by moving down
editor.setCursorScreenPosition({ row: 3, column: lineLengths[3] });
editor.moveDown();
expect(editor.getCursorScreenPosition().column).not.toBe(6);
// clear the goal column by explicitly setting the cursor position
editor.setCursorScreenPosition([4, 6]);
expect(editor.getCursorScreenPosition().column).toBe(6);
editor.moveDown();
expect(editor.getCursorScreenPosition().column).toBe(6);
});
it('merges multiple cursors', () => {
editor.setCursorScreenPosition([0, 0]);
editor.addCursorAtScreenPosition([0, 1]);
const [cursor1] = editor.getCursors();
editor.setCursorScreenPosition([4, 7]);
expect(editor.getCursors().length).toBe(1);
expect(editor.getCursors()).toEqual([cursor1]);
expect(editor.getCursorScreenPosition()).toEqual([4, 7]);
});
describe('when soft-wrap is enabled and code is folded', () => {
beforeEach(() => {
editor.setSoftWrapped(true);
editor.setDefaultCharWidth(1);
editor.setEditorWidthInChars(50);
editor.foldBufferRowRange(2, 3);
});
it('positions the cursor at the buffer position that corresponds to the given screen position', () => {
editor.setCursorScreenPosition([9, 0]);
expect(editor.getCursorBufferPosition()).toEqual([8, 11]);
});
});
});
describe('.moveUp()', () => {
it('moves the cursor up', () => {
editor.setCursorScreenPosition([2, 2]);
editor.moveUp();
expect(editor.getCursorScreenPosition()).toEqual([1, 2]);
});
it('retains the goal column across lines of differing length', () => {
expect(lineLengths[6]).toBeGreaterThan(32);
editor.setCursorScreenPosition({ row: 6, column: 32 });
editor.moveUp();
expect(editor.getCursorScreenPosition().column).toBe(lineLengths[5]);
editor.moveUp();
expect(editor.getCursorScreenPosition().column).toBe(lineLengths[4]);
editor.moveUp();
expect(editor.getCursorScreenPosition().column).toBe(32);
});
describe('when the cursor is on the first line', () => {
it('moves the cursor to the beginning of the line, but retains the goal column', () => {
editor.setCursorScreenPosition([0, 4]);
editor.moveUp();
expect(editor.getCursorScreenPosition()).toEqual([0, 0]);
editor.moveDown();
expect(editor.getCursorScreenPosition()).toEqual([1, 4]);
});
});
describe('when there is a selection', () => {
beforeEach(() => editor.setSelectedBufferRange([[4, 9], [5, 10]]));
it('moves above the selection', () => {
const cursor = editor.getLastCursor();
editor.moveUp();
expect(cursor.getBufferPosition()).toEqual([3, 9]);
});
});
it('merges cursors when they overlap', () => {
editor.addCursorAtScreenPosition([1, 0]);
const [cursor1] = editor.getCursors();
editor.moveUp();
expect(editor.getCursors()).toEqual([cursor1]);
expect(cursor1.getBufferPosition()).toEqual([0, 0]);
});
describe('when the cursor was moved down from the beginning of an indented soft-wrapped line', () => {
it('moves to the beginning of the previous line', () => {
editor.setSoftWrapped(true);
editor.setDefaultCharWidth(1);
editor.setEditorWidthInChars(50);
editor.setCursorScreenPosition([3, 0]);
editor.moveDown();
editor.moveDown();
editor.moveUp();
expect(editor.getCursorScreenPosition()).toEqual([4, 4]);
});
});
});
describe('.moveDown()', () => {
it('moves the cursor down', () => {
editor.setCursorScreenPosition([2, 2]);
editor.moveDown();
expect(editor.getCursorScreenPosition()).toEqual([3, 2]);
});
it('retains the goal column across lines of differing length', () => {
editor.setCursorScreenPosition({ row: 3, column: lineLengths[3] });
editor.moveDown();
expect(editor.getCursorScreenPosition().column).toBe(lineLengths[4]);
editor.moveDown();
expect(editor.getCursorScreenPosition().column).toBe(lineLengths[5]);
editor.moveDown();
expect(editor.getCursorScreenPosition().column).toBe(lineLengths[3]);
});
describe('when the cursor is on the last line', () => {
it('moves the cursor to the end of line, but retains the goal column when moving back up', () => {
const lastLineIndex = buffer.getLines().length - 1;
const lastLine = buffer.lineForRow(lastLineIndex);
expect(lastLine.length).toBeGreaterThan(0);
editor.setCursorScreenPosition({
row: lastLineIndex,
column: editor.getTabLength()
});
editor.moveDown();
expect(editor.getCursorScreenPosition()).toEqual({
row: lastLineIndex,
column: lastLine.length
});
editor.moveUp();
expect(editor.getCursorScreenPosition().column).toBe(
editor.getTabLength()
);
});
it('retains a goal column of 0 when moving back up', () => {
const lastLineIndex = buffer.getLines().length - 1;
const lastLine = buffer.lineForRow(lastLineIndex);
expect(lastLine.length).toBeGreaterThan(0);
editor.setCursorScreenPosition({ row: lastLineIndex, column: 0 });
editor.moveDown();
editor.moveUp();
expect(editor.getCursorScreenPosition().column).toBe(0);
});
});
describe('when the cursor is at the beginning of an indented soft-wrapped line', () => {
it("moves to the beginning of the line's continuation on the next screen row", () => {
editor.setSoftWrapped(true);
editor.setDefaultCharWidth(1);
editor.setEditorWidthInChars(50);
editor.setCursorScreenPosition([3, 0]);
editor.moveDown();
expect(editor.getCursorScreenPosition()).toEqual([4, 4]);
});
});
describe('when there is a selection', () => {
beforeEach(() => editor.setSelectedBufferRange([[4, 9], [5, 10]]));
it('moves below the selection', () => {
const cursor = editor.getLastCursor();
editor.moveDown();
expect(cursor.getBufferPosition()).toEqual([6, 10]);
});
});
it('merges cursors when they overlap', () => {
editor.setCursorScreenPosition([12, 2]);
editor.addCursorAtScreenPosition([11, 2]);
const [cursor1] = editor.getCursors();
editor.moveDown();
expect(editor.getCursors()).toEqual([cursor1]);
expect(cursor1.getBufferPosition()).toEqual([12, 2]);
});
});
describe('.moveLeft()', () => {
it('moves the cursor by one column to the left', () => {
editor.setCursorScreenPosition([1, 8]);
editor.moveLeft();
expect(editor.getCursorScreenPosition()).toEqual([1, 7]);
});
it('moves the cursor by n columns to the left', () => {
editor.setCursorScreenPosition([1, 8]);
editor.moveLeft(4);
expect(editor.getCursorScreenPosition()).toEqual([1, 4]);
});
it('moves the cursor by two rows up when the columnCount is longer than an entire line', () => {
editor.setCursorScreenPosition([2, 2]);
editor.moveLeft(34);
expect(editor.getCursorScreenPosition()).toEqual([0, 29]);
});
it('moves the cursor to the beginning columnCount is longer than the position in the buffer', () => {
editor.setCursorScreenPosition([1, 0]);
editor.moveLeft(100);
expect(editor.getCursorScreenPosition()).toEqual([0, 0]);
});
describe('when the cursor is in the first column', () => {
describe('when there is a previous line', () => {
it('wraps to the end of the previous line', () => {
editor.setCursorScreenPosition({ row: 1, column: 0 });
editor.moveLeft();
expect(editor.getCursorScreenPosition()).toEqual({
row: 0,
column: buffer.lineForRow(0).length
});
});
it('moves the cursor by one row up and n columns to the left', () => {
editor.setCursorScreenPosition([1, 0]);
editor.moveLeft(4);
expect(editor.getCursorScreenPosition()).toEqual([0, 26]);
});
});
describe('when the next line is empty', () => {
it('wraps to the beginning of the previous line', () => {
editor.setCursorScreenPosition([11, 0]);
editor.moveLeft();
expect(editor.getCursorScreenPosition()).toEqual([10, 0]);
});
});
describe('when line is wrapped and follow previous line indentation', () => {
beforeEach(() => {
editor.setSoftWrapped(true);
editor.setDefaultCharWidth(1);
editor.setEditorWidthInChars(50);
});
it('wraps to the end of the previous line', () => {
editor.setCursorScreenPosition([4, 4]);
editor.moveLeft();
expect(editor.getCursorScreenPosition()).toEqual([3, 46]);
});
});
describe('when the cursor is on the first line', () => {
it('remains in the same position (0,0)', () => {
editor.setCursorScreenPosition({ row: 0, column: 0 });
editor.moveLeft();
expect(editor.getCursorScreenPosition()).toEqual({
row: 0,
column: 0
});
});
it('remains in the same position (0,0) when columnCount is specified', () => {
editor.setCursorScreenPosition([0, 0]);
editor.moveLeft(4);
expect(editor.getCursorScreenPosition()).toEqual([0, 0]);
});
});
});
describe('when softTabs is enabled and the cursor is preceded by leading whitespace', () => {
it('skips tabLength worth of whitespace at a time', () => {
editor.setCursorBufferPosition([5, 6]);
editor.moveLeft();
expect(editor.getCursorBufferPosition()).toEqual([5, 4]);
});
});
describe('when there is a selection', () => {
beforeEach(() => editor.setSelectedBufferRange([[5, 22], [5, 27]]));
it('moves to the left of the selection', () => {
const cursor = editor.getLastCursor();
editor.moveLeft();
expect(cursor.getBufferPosition()).toEqual([5, 22]);
editor.moveLeft();
expect(cursor.getBufferPosition()).toEqual([5, 21]);
});
});
it('merges cursors when they overlap', () => {
editor.setCursorScreenPosition([0, 0]);
editor.addCursorAtScreenPosition([0, 1]);
const [cursor1] = editor.getCursors();
editor.moveLeft();
expect(editor.getCursors()).toEqual([cursor1]);
expect(cursor1.getBufferPosition()).toEqual([0, 0]);
});
});
describe('.moveRight()', () => {
it('moves the cursor by one column to the right', () => {
editor.setCursorScreenPosition([3, 3]);
editor.moveRight();
expect(editor.getCursorScreenPosition()).toEqual([3, 4]);
});
it('moves the cursor by n columns to the right', () => {
editor.setCursorScreenPosition([3, 7]);
editor.moveRight(4);
expect(editor.getCursorScreenPosition()).toEqual([3, 11]);
});
it('moves the cursor by two rows down when the columnCount is longer than an entire line', () => {
editor.setCursorScreenPosition([0, 29]);
editor.moveRight(34);
expect(editor.getCursorScreenPosition()).toEqual([2, 2]);
});
it('moves the cursor to the end of the buffer when columnCount is longer than the number of characters following the cursor position', () => {
editor.setCursorScreenPosition([11, 5]);
editor.moveRight(100);
expect(editor.getCursorScreenPosition()).toEqual([12, 2]);
});
describe('when the cursor is on the last column of a line', () => {
describe('when there is a subsequent line', () => {
it('wraps to the beginning of the next line', () => {
editor.setCursorScreenPosition([0, buffer.lineForRow(0).length]);
editor.moveRight();
expect(editor.getCursorScreenPosition()).toEqual([1, 0]);
});
it('moves the cursor by one row down and n columns to the right', () => {
editor.setCursorScreenPosition([0, buffer.lineForRow(0).length]);
editor.moveRight(4);
expect(editor.getCursorScreenPosition()).toEqual([1, 3]);
});
});
describe('when the next line is empty', () => {
it('wraps to the beginning of the next line', () => {
editor.setCursorScreenPosition([9, 4]);
editor.moveRight();
expect(editor.getCursorScreenPosition()).toEqual([10, 0]);
});
});
describe('when the cursor is on the last line', () => {
it('remains in the same position', () => {
const lastLineIndex = buffer.getLines().length - 1;
const lastLine = buffer.lineForRow(lastLineIndex);
expect(lastLine.length).toBeGreaterThan(0);
const lastPosition = {
row: lastLineIndex,
column: lastLine.length
};
editor.setCursorScreenPosition(lastPosition);
editor.moveRight();
expect(editor.getCursorScreenPosition()).toEqual(lastPosition);
});
});
});
describe('when there is a selection', () => {
beforeEach(() => editor.setSelectedBufferRange([[5, 22], [5, 27]]));
it('moves to the left of the selection', () => {
const cursor = editor.getLastCursor();
editor.moveRight();
expect(cursor.getBufferPosition()).toEqual([5, 27]);
editor.moveRight();
expect(cursor.getBufferPosition()).toEqual([5, 28]);
});
});
it('merges cursors when they overlap', () => {
editor.setCursorScreenPosition([12, 2]);
editor.addCursorAtScreenPosition([12, 1]);
const [cursor1] = editor.getCursors();
editor.moveRight();
expect(editor.getCursors()).toEqual([cursor1]);
expect(cursor1.getBufferPosition()).toEqual([12, 2]);
});
});
describe('.moveToTop()', () => {
it('moves the cursor to the top of the buffer', () => {
editor.setCursorScreenPosition([11, 1]);
editor.addCursorAtScreenPosition([12, 0]);
editor.moveToTop();
expect(editor.getCursors().length).toBe(1);
expect(editor.getCursorBufferPosition()).toEqual([0, 0]);
});
});
describe('.moveToBottom()', () => {
it('moves the cursor to the bottom of the buffer', () => {
editor.setCursorScreenPosition([0, 0]);
editor.addCursorAtScreenPosition([1, 0]);
editor.moveToBottom();
expect(editor.getCursors().length).toBe(1);
expect(editor.getCursorBufferPosition()).toEqual([12, 2]);
});
});
describe('.moveToBeginningOfScreenLine()', () => {
describe('when soft wrap is on', () => {
it('moves cursor to the beginning of the screen line', () => {
editor.setSoftWrapped(true);
editor.setEditorWidthInChars(10);
editor.setCursorScreenPosition([1, 2]);
editor.moveToBeginningOfScreenLine();
const cursor = editor.getLastCursor();
expect(cursor.getScreenPosition()).toEqual([1, 0]);
});
});
describe('when soft wrap is off', () => {
it('moves cursor to the beginning of the line', () => {
editor.setCursorScreenPosition([0, 5]);
editor.addCursorAtScreenPosition([1, 7]);
editor.moveToBeginningOfScreenLine();
expect(editor.getCursors().length).toBe(2);
const [cursor1, cursor2] = editor.getCursors();
expect(cursor1.getBufferPosition()).toEqual([0, 0]);
expect(cursor2.getBufferPosition()).toEqual([1, 0]);
});
});
});
describe('.moveToEndOfScreenLine()', () => {
describe('when soft wrap is on', () => {
it('moves cursor to the beginning of the screen line', () => {
editor.setSoftWrapped(true);
editor.setDefaultCharWidth(1);
editor.setEditorWidthInChars(10);
editor.setCursorScreenPosition([1, 2]);
editor.moveToEndOfScreenLine();
const cursor = editor.getLastCursor();
expect(cursor.getScreenPosition()).toEqual([1, 9]);
});
});
describe('when soft wrap is off', () => {
it('moves cursor to the end of line', () => {
editor.setCursorScreenPosition([0, 0]);
editor.addCursorAtScreenPosition([1, 0]);
editor.moveToEndOfScreenLine();
expect(editor.getCursors().length).toBe(2);
const [cursor1, cursor2] = editor.getCursors();
expect(cursor1.getBufferPosition()).toEqual([0, 29]);
expect(cursor2.getBufferPosition()).toEqual([1, 30]);
});
});
});
describe('.moveToBeginningOfLine()', () => {
it('moves cursor to the beginning of the buffer line', () => {
editor.setSoftWrapped(true);
editor.setDefaultCharWidth(1);
editor.setEditorWidthInChars(10);
editor.setCursorScreenPosition([1, 2]);
editor.moveToBeginningOfLine();
const cursor = editor.getLastCursor();
expect(cursor.getScreenPosition()).toEqual([0, 0]);
});
});
describe('.moveToEndOfLine()', () => {
it('moves cursor to the end of the buffer line', () => {
editor.setSoftWrapped(true);
editor.setDefaultCharWidth(1);
editor.setEditorWidthInChars(10);
editor.setCursorScreenPosition([0, 2]);
editor.moveToEndOfLine();
const cursor = editor.getLastCursor();
expect(cursor.getScreenPosition()).toEqual([4, 4]);
});
});
describe('.moveToFirstCharacterOfLine()', () => {
describe('when soft wrap is on', () => {
it("moves to the first character of the current screen line or the beginning of the screen line if it's already on the first character", () => {
editor.setSoftWrapped(true);
editor.setDefaultCharWidth(1);
editor.setEditorWidthInChars(10);
editor.setCursorScreenPosition([2, 5]);
editor.addCursorAtScreenPosition([8, 7]);
editor.moveToFirstCharacterOfLine();
const [cursor1, cursor2] = editor.getCursors();
expect(cursor1.getScreenPosition()).toEqual([2, 0]);
expect(cursor2.getScreenPosition()).toEqual([8, 2]);
editor.moveToFirstCharacterOfLine();
expect(cursor1.getScreenPosition()).toEqual([2, 0]);
expect(cursor2.getScreenPosition()).toEqual([8, 2]);
});
});
describe('when soft wrap is off', () => {
it("moves to the first character of the current line or the beginning of the line if it's already on the first character", () => {
editor.setCursorScreenPosition([0, 5]);
editor.addCursorAtScreenPosition([1, 7]);
editor.moveToFirstCharacterOfLine();
const [cursor1, cursor2] = editor.getCursors();
expect(cursor1.getBufferPosition()).toEqual([0, 0]);
expect(cursor2.getBufferPosition()).toEqual([1, 2]);
editor.moveToFirstCharacterOfLine();
expect(cursor1.getBufferPosition()).toEqual([0, 0]);
expect(cursor2.getBufferPosition()).toEqual([1, 0]);
});
it('moves to the beginning of the line if it only contains whitespace ', () => {
editor.setText('first\n \nthird');
editor.setCursorScreenPosition([1, 2]);
editor.moveToFirstCharacterOfLine();
const cursor = editor.getLastCursor();
expect(cursor.getBufferPosition()).toEqual([1, 0]);
});
describe('when invisible characters are enabled with soft tabs', () => {
it('moves to the first character of the current line without being confused by the invisible characters', () => {
editor.update({ showInvisibles: true });
editor.setCursorScreenPosition([1, 7]);
editor.moveToFirstCharacterOfLine();
expect(editor.getCursorBufferPosition()).toEqual([1, 2]);
editor.moveToFirstCharacterOfLine();
expect(editor.getCursorBufferPosition()).toEqual([1, 0]);
});
});
describe('when invisible characters are enabled with hard tabs', () => {
it('moves to the first character of the current line without being confused by the invisible characters', () => {
editor.update({ showInvisibles: true });
buffer.setTextInRange([[1, 0], [1, Infinity]], '\t\t\ta', {
normalizeLineEndings: false
});
editor.setCursorScreenPosition([1, 7]);
editor.moveToFirstCharacterOfLine();
expect(editor.getCursorBufferPosition()).toEqual([1, 3]);
editor.moveToFirstCharacterOfLine();
expect(editor.getCursorBufferPosition()).toEqual([1, 0]);
});
});
});
it('clears the goal column', () => {
editor.setText('first\n\nthird');
editor.setCursorScreenPosition([0, 3]);
editor.moveDown();
editor.moveToFirstCharacterOfLine();
editor.moveDown();
expect(editor.getCursorBufferPosition()).toEqual([2, 0]);
});
});
describe('.moveToBeginningOfWord()', () => {
it('moves the cursor to the beginning of the word', () => {
editor.setCursorBufferPosition([0, 8]);
editor.addCursorAtBufferPosition([1, 12]);
editor.addCursorAtBufferPosition([3, 0]);
const [cursor1, cursor2, cursor3] = editor.getCursors();
editor.moveToBeginningOfWord();
expect(cursor1.getBufferPosition()).toEqual([0, 4]);
expect(cursor2.getBufferPosition()).toEqual([1, 11]);
expect(cursor3.getBufferPosition()).toEqual([2, 39]);
});
it('does not fail at position [0, 0]', () => {
editor.setCursorBufferPosition([0, 0]);
editor.moveToBeginningOfWord();
});
it('treats lines with only whitespace as a word', () => {
editor.setCursorBufferPosition([11, 0]);
editor.moveToBeginningOfWord();
expect(editor.getCursorBufferPosition()).toEqual([10, 0]);
});
it('treats lines with only whitespace as a word (CRLF line ending)', () => {
editor.buffer.setText(buffer.getText().replace(/\n/g, '\r\n'));
editor.setCursorBufferPosition([11, 0]);
editor.moveToBeginningOfWord();
expect(editor.getCursorBufferPosition()).toEqual([10, 0]);
});
it('works when the current line is blank', () => {
editor.setCursorBufferPosition([10, 0]);
editor.moveToBeginningOfWord();
expect(editor.getCursorBufferPosition()).toEqual([9, 2]);
});
it('works when the current line is blank (CRLF line ending)', () => {
editor.buffer.setText(buffer.getText().replace(/\n/g, '\r\n'));
editor.setCursorBufferPosition([10, 0]);
editor.moveToBeginningOfWord();
expect(editor.getCursorBufferPosition()).toEqual([9, 2]);
editor.buffer.setText(buffer.getText().replace(/\r\n/g, '\n'));
});
});
describe('.moveToPreviousWordBoundary()', () => {
it('moves the cursor to the previous word boundary', () => {
editor.setCursorBufferPosition([0, 8]);
editor.addCursorAtBufferPosition([2, 0]);
editor.addCursorAtBufferPosition([2, 4]);
editor.addCursorAtBufferPosition([3, 14]);
const [cursor1, cursor2, cursor3, cursor4] = editor.getCursors();
editor.moveToPreviousWordBoundary();
expect(cursor1.getBufferPosition()).toEqual([0, 4]);
expect(cursor2.getBufferPosition()).toEqual([1, 30]);
expect(cursor3.getBufferPosition()).toEqual([2, 0]);
expect(cursor4.getBufferPosition()).toEqual([3, 13]);
});
});
describe('.moveToNextWordBoundary()', () => {
it('moves the cursor to the previous word boundary', () => {
editor.setCursorBufferPosition([0, 8]);
editor.addCursorAtBufferPosition([2, 40]);
editor.addCursorAtBufferPosition([3, 0]);