Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 85b43634ae8d6decdad48cd8e18ab1dc50908278 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2022 Dirk Steinkamp
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Dirk Steinkamp <dirk.steinkamp@gmx.de> - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.editors.tests;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Control;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.filesystem.EFS;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IMultiTextSelection;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.MultiTextSelection;
import org.eclipse.jface.text.Region;

import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.ide.IDE;

import org.eclipse.ui.texteditor.AbstractTextEditor;

/*
 * Note: this test would better fit in the org.eclipse.ui.workbench.texteditor bundle, but initializing
 * an editor from this bundle is quite tricky without the IDE and EFS utils.
 */
public class TextMultiCaretSelectionCommandsTest {
	private static final String MULTI_SELECTION_DOWN = "org.eclipse.ui.edit.text.select.selectMultiSelectionDown";
	private static final String ADD_ALL_MATCHES_TO_MULTI_SELECTION = "org.eclipse.ui.edit.text.select.addAllMatchesToMultiSelection";
	private static final String MULTI_SELECTION_UP = "org.eclipse.ui.edit.text.select.selectMultiSelectionUp";
	private static final String STOP_MULTI_SELECTION = "org.eclipse.ui.edit.text.select.stopMultiSelection";
	private static final String MULTI_CARET_DOWN = "org.eclipse.ui.edit.text.select.multiCaretDown";
	private static final String MULTI_CARET_UP = "org.eclipse.ui.edit.text.select.multiCaretUp";

	private static final String LINE_1 = "private static String a;\n";
	private static final String LINE_2 = "private static String b; // this is a little longer\n";
	private static final String LINE_3 = "\t\tprivate static String c;\n";
	private static final String LINE_4 = "private static String d";

	private static final int L1_LEN = LINE_1.length();
	private static final int L2_LEN = LINE_2.length();
	private static final int L3_LEN = LINE_3.length();
	private static final int L4_LEN = LINE_4.length();

	private static File file;
	private static AbstractTextEditor editor;
	private static StyledText widget;

	@Before
	public void setUpBeforeClass() throws IOException, PartInitException, CoreException {
		file = File.createTempFile(TextMultiCaretSelectionCommandsTest.class.getName(), ".txt");
		Files.write(file.toPath(), (LINE_1 + LINE_2 + LINE_3 + LINE_4) //
				.getBytes());
		editor = (AbstractTextEditor) IDE.openEditorOnFileStore(
				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), EFS.getStore(file.toURI()));
		widget = (StyledText) editor.getAdapter(Control.class);
	}

	@After
	public void tearDown() {
		editor.dispose();
		file.delete();
	}

	@Test
	public void testMultiSelectionDown_withFirstIdentifierSelected_addsIdenticalIdentifiersToSelection()
			throws Exception {
		setSelection(new IRegion[] { new Region(0, 7) });
		assertEquals(7, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 7), new Region(L1_LEN, 7) }, getSelection());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 7), new Region(L1_LEN, 7), new Region(L1_LEN + L2_LEN + 2, 7) },
				getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(7, 0) }, getSelection());
	}

	@Test
	public void testMultiSelectionDown_withSecondIdentifierSelectedIdentifier_addsNextOccurenceToSelection()
			throws Exception {
		setSelection(new IRegion[] { new Region(8, 6) });
		assertEquals(14, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(14, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(8, 6), new Region(L1_LEN + 8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionDown_withSelectionInSecondRow_addsIdenticalIdentifierInThirdRowToSelection()
			throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + 8, 6) });
		assertEquals(L1_LEN + 14, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(L1_LEN + 14, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 6), new Region(L1_LEN + L2_LEN + 10, 6) },
				getSelection());
	}

	@Test
	public void testMultiSelectionDown_withTwoSelectionsAndAnchorBelow_reducesSelection() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + 8, 6) });
		// It is important here to build up the selection in steps, so the
		// handler can determine an anchor region
		executeCommand(MULTI_SELECTION_UP);
		assertArrayEquals(new IRegion[] { new Region(8, 6), new Region(L1_LEN + 8, 6) }, getSelection());

		executeCommand(MULTI_SELECTION_DOWN);

		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionDown_withTwoSelectionsAndAnchorAbove_extendsSelection() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + 8, 6) });
		// It is important here to build up the selection in steps, so the
		// handler can determine an anchor region
		executeCommand(MULTI_SELECTION_DOWN);
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 6), new Region(L1_LEN + L2_LEN + 10, 6) },
				getSelection());

		executeCommand(MULTI_SELECTION_DOWN);

		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 6), new Region(L1_LEN + L2_LEN + 10, 6),
				new Region(L1_LEN + L2_LEN + L3_LEN + 8, 6) }, getSelection());
	}

	// Caret-related tests for ADD_NEXT_MATCH_TO_MULTI_SELECTION
	// that check how the selection is expanded

	@Test
	public void testMultiSelectionDown_withCaretInFirstIdentifier_selectsFullIdentifier() throws Exception {
		setSelection(new IRegion[] { new Region(1, 0) });
		assertEquals(1, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 7) }, getSelection());
	}

	@Test
	public void testMultiSelectionDown_withCaretInSecondIdentifier_selectsFullIdentifier() throws Exception {
		setSelection(new IRegion[] { new Region(11, 0) });
		assertEquals(11, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(14, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionDown_withCaretBetweenIdentifierCharAndNonIdentifierChar_selectsFullIdentifier()
			throws Exception {
		setSelection(new IRegion[] { new Region(23, 0) });
		assertEquals(23, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(23, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(22, 1) }, getSelection());
	}

	@Test
	public void testMultiSelectionDown_withCaretInSecondRow_selectsFullIdentifier() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + 11, 0) });
		assertEquals(L1_LEN + 11, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(L1_LEN + 14, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionDown_withCaretInIdentifierWithNoFollowingMatch_selectsFullIdentifier()
			throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN + L3_LEN + 11, 0) });
		assertEquals(L1_LEN + L2_LEN + L3_LEN + 11, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(L1_LEN + L2_LEN + L3_LEN + 14, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + L2_LEN + L3_LEN + 8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionDown_withCaretAtEndOfDocument_selectsFullIdentifier() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN + L3_LEN + L4_LEN, 0) });
		assertEquals(L1_LEN + L2_LEN + L3_LEN + L4_LEN, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);

		assertEquals(L1_LEN + L2_LEN + L3_LEN + L4_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + L2_LEN + L3_LEN + L4_LEN - 1, 1) }, getSelection());
	}

	@Test
	public void testAddAllMatches_withSingleSelection_selectsAllOccurences() throws Exception {
		setSelection(new IRegion[] { new Region(0, 7) });
		assertEquals(7, widget.getCaretOffset());

		executeCommand(ADD_ALL_MATCHES_TO_MULTI_SELECTION);

		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 7), new Region(L1_LEN, 7), new Region(L1_LEN + L2_LEN + 2, 7),
				new Region(L1_LEN + L2_LEN + L3_LEN, 7) }, getSelection());
	}

	@Test
	public void testAddAllMatches_withDoubleSelectionOfSameText_selectsAllOccurences() throws Exception {
		setSelection(new IRegion[] { new Region(0, 7), new Region(L1_LEN, 7) });
		assertEquals(7, widget.getCaretOffset());

		executeCommand(ADD_ALL_MATCHES_TO_MULTI_SELECTION);

		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 7), new Region(L1_LEN, 7), new Region(L1_LEN + L2_LEN + 2, 7),
				new Region(L1_LEN + L2_LEN + L3_LEN, 7) }, getSelection());
	}

	@Test
	public void testAddAllMatches_withDoubleSelectionOfDifferentTexts_doesNotChangeSelection() throws Exception {
		setSelection(new IRegion[] { new Region(0, 7), new Region(8, 7) });
		assertEquals(7, widget.getCaretOffset());

		executeCommand(ADD_ALL_MATCHES_TO_MULTI_SELECTION);

		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 7), new Region(8, 7) }, getSelection());
	}

	@Test
	public void testAddAllMatches_withCaretInIdentifier_selectsAllOccurencesOfIdentifier() throws Exception {
		setSelection(new IRegion[] { new Region(2, 0) });
		assertEquals(2, widget.getCaretOffset());

		executeCommand(ADD_ALL_MATCHES_TO_MULTI_SELECTION);

		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 7), new Region(L1_LEN, 7), new Region(L1_LEN + L2_LEN + 2, 7),
				new Region(L1_LEN + L2_LEN + L3_LEN, 7) }, getSelection());
	}

	@Test
	public void testMultiSelectionUp_withCaretInIdentifier_selectsFullIdentifier() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + 11, 0) });
		assertEquals(L1_LEN + 11, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_UP);

		assertEquals(L1_LEN + 8 + 6, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionUp_withSingleSelectionAndNoPreviousMatch_doesNothing() throws Exception {
		setSelection(new IRegion[] { new Region(8, 6) });
		assertEquals(14, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_UP);

		assertEquals(14, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionUp_withTwoSelections_removesSecondSelection() throws Exception {
		setSelection(new IRegion[] { new Region(8, 6), new Region(L1_LEN + 8, 6) });
		assertEquals(14, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_UP);

		assertEquals(14, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionUp_withThreeSelections_removesThirdSelection() throws Exception {
		setSelection(
				new IRegion[] { new Region(8, 6), new Region(L1_LEN + 8, 6), new Region(L1_LEN + L2_LEN + 10, 6) });
		assertEquals(14, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_UP);

		assertEquals(14, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(8, 6), new Region(L1_LEN + 8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionUp_withTwoSelectionsAndAnchorAbove_reducesSelection() throws Exception {
		setSelection(new IRegion[] { new Region(8, 6) });
		// It is important here to build up the selection in steps, so the
		// handler can determine an anchor region
		executeCommand(MULTI_SELECTION_DOWN);
		assertArrayEquals(new IRegion[] { new Region(8, 6), new Region(L1_LEN + 8, 6) }, getSelection());

		executeCommand(MULTI_SELECTION_UP);

		assertArrayEquals(new IRegion[] { new Region(8, 6) }, getSelection());
	}

	@Test
	public void testMultiSelectionUp_withTwoSelectionsAndAnchorBelow_extendsSelection() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN + 10, 6) });
		// It is important here to build up the selection in steps, so the
		// handler can determine an anchor region
		executeCommand(MULTI_SELECTION_UP);
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 6), new Region(L1_LEN + L2_LEN + 10, 6) },
				getSelection());

		executeCommand(MULTI_SELECTION_UP);

		assertArrayEquals(
				new IRegion[] { new Region(8, 6), new Region(L1_LEN + 8, 6), new Region(L1_LEN + L2_LEN + 10, 6) },
				getSelection());
	}

	@Test
	public void testStopMultiSelection_withSingleSelection_doesNotChangeSelectionOrCaretOffset() throws Exception {
		setSelection(new IRegion[] { new Region(0, 7) });

		assertEquals(7, widget.getCaretOffset());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 7) }, getSelection());
	}

	@Test
	public void testStopMultiSelection_withMultiSelection_revokesSelectionAndKeepsFirstCaretOffset() throws Exception {
		setSelection(new IRegion[] { new Region(0, 7), new Region(L1_LEN, 7) });

		assertEquals(7, widget.getCaretOffset());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(7, 0) }, getSelection());
	}

	@Test
	public void testStopMultiSelection_withMultiSelectionAndCaretAtBeginning_revokesSelectionAndKeepsFirstCaretOffset()
			throws Exception {
		setSelection(new IRegion[] { new Region(0, 7), new Region(L1_LEN, 7) });
		assertEquals(7, widget.getCaretOffset());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(7, 0) }, getSelection());
	}

	@Test
	public void testStopMultiSelection_withMultiSelectionAndCaretAfterLastSelection_revokesSelectionAndKeepsCaretOffset()
			throws Exception {
		setSelection(new IRegion[] { new Region(0, 7), new Region(L1_LEN, 7), new Region(L1_LEN + L2_LEN, 7) });
		assertEquals(7, widget.getCaretOffset());

		executeCommand(MULTI_SELECTION_DOWN);
		executeCommand(MULTI_SELECTION_DOWN);

		// TODO How to place the caret at the end without dismissing the
		// selection? Should rather be 57
		assertEquals(7, widget.getCaretOffset());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(7, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(7, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretDown_withCaret_addsCaretsInNextLines() throws Exception {
		setSelection(new IRegion[] { new Region(0, 0) });
		assertEquals(0, widget.getCaretOffset());

		executeCommand(MULTI_CARET_DOWN);

		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0), new Region(L1_LEN, 0) }, getSelection());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_DOWN);

		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0), new Region(L1_LEN, 0), new Region(L1_LEN + L2_LEN, 0) },
				getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretDown_withTwoCaretsAndAnchorRegionBelow_removesFirstCaret() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN, 0) });
		assertEquals(L1_LEN, widget.getCaretOffset());

		executeCommand(MULTI_CARET_UP);

		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0), new Region(L1_LEN, 0) }, getSelection());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_DOWN);

		assertEquals(L1_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN, 0) }, getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(L1_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretDown_withSingleSelection_addsSelectionInNextLine() throws Exception {
		setSelection(new IRegion[] { new Region(0, 3) });
		assertEquals(3, widget.getCaretOffset());

		executeCommand(MULTI_CARET_DOWN);

		assertEquals(3, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 3), new Region(L1_LEN, 3) }, getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(3, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(3, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretDown_withSingleCaretAtEndOfLongerLine_addsCaretAtEndOfNextLine() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN, 0) });
		assertEquals(L1_LEN + L2_LEN, widget.getCaretOffset());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_DOWN);

		assertEquals(L1_LEN + L2_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + L2_LEN, 0), new Region(L1_LEN + L2_LEN + L3_LEN, 0) },
				getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(L1_LEN + L2_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + L2_LEN, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretDown_withSingleCaretInLineAboveLineWithTabs_addsCaretInNextLineRespectingTabs()
			throws Exception {
		widget.setTabs(4); // Make default explicit
		setSelection(new IRegion[] { new Region(L1_LEN + 8, 0) });
		assertEquals(L1_LEN + 8, widget.getCaretOffset());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_DOWN);

		assertEquals(L1_LEN + 8, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 0), new Region(L1_LEN + L2_LEN + 2, 0) },
				getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(L1_LEN + 8, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretDown_withSingleCaretInLineWithTabs_addsCaretInNextLineRespectingTabs() throws Exception {
		widget.setTabs(4); // Make default explicit
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN + 2, 0) });
		assertEquals(L1_LEN + L2_LEN + 2, widget.getCaretOffset());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_DOWN);

		assertEquals(L1_LEN + L2_LEN + 2, widget.getCaretOffset());
		assertArrayEquals(
				new IRegion[] { new Region(L1_LEN + L2_LEN + 2, 0), new Region(L1_LEN + L2_LEN + L3_LEN + 8, 0) },
				getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(L1_LEN + L2_LEN + 2, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + L2_LEN + 2, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretDown_withSingleCaretAtEndOfText_doesNotChangeCaret() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN + L3_LEN + L4_LEN, 0) });
		assertEquals(L1_LEN + L2_LEN + L3_LEN + L4_LEN, widget.getCaretOffset());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_DOWN);

		assertEquals(L1_LEN + L2_LEN + L3_LEN + L4_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + L2_LEN + L3_LEN + L4_LEN, 0) }, getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(L1_LEN + L2_LEN + L3_LEN + L4_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + L2_LEN + L3_LEN + L4_LEN, 0) }, getSelection());
	}

	/////////////////////////////////////////////////////
	@Test
	public void testMultiCaretUp_withCaret_addsCaretsInPreviousLines() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN, 0) });
		assertEquals(L1_LEN + L2_LEN, widget.getCaretOffset());

		executeCommand(MULTI_CARET_UP);

		assertEquals(L1_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN, 0), new Region(L1_LEN + L2_LEN, 0) }, getSelection());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_UP);

		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0), new Region(L1_LEN, 0), new Region(L1_LEN + L2_LEN, 0) },
				getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretUp_withTwoCaretsAndAnchorRegionAbove_removesLastCaret() throws Exception {
		setSelection(new IRegion[] { new Region(0, 0) });
		assertEquals(0, widget.getCaretOffset());

		executeCommand(MULTI_CARET_DOWN);

		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0), new Region(L1_LEN, 0) }, getSelection());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_UP);

		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0) }, getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretUp_withSingleSelection_addsSelectionInPreviousLine() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN, 3) });
		assertEquals(L1_LEN + 3, widget.getCaretOffset());

		executeCommand(MULTI_CARET_UP);

		assertEquals(3, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 3), new Region(L1_LEN, 3) }, getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(3, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(3, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretUp_withSingleCaretAtEndOfLongerLine_addsCaretAtEndOfPreviousLine() throws Exception {
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN, 0) });
		assertEquals(L1_LEN + L2_LEN, widget.getCaretOffset());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_UP);

		assertEquals(L1_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN, 0), new Region(L1_LEN + L2_LEN, 0) }, getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(L1_LEN, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretUp_withSingleCaretInLineBelowLineWithTabs_addsCaretInPreviousLineRespectingTabs()
			throws Exception {
		widget.setTabs(4); // Make default explicit
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN + L3_LEN + 8, 0) });
		assertEquals(L1_LEN + L2_LEN + L3_LEN + 8, widget.getCaretOffset());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_UP);

		assertEquals(L1_LEN + L2_LEN + 2, widget.getCaretOffset());
		assertArrayEquals(
				new IRegion[] { new Region(L1_LEN + L2_LEN + 2, 0), new Region(L1_LEN + L2_LEN + L3_LEN + 8, 0) },
				getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(L1_LEN + L2_LEN + 2, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + L2_LEN + 2, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretUp_withSingleCaretInLineWithTabs_addsCaretInPreviousLineRespectingTabs()
			throws Exception {
		widget.setTabs(4); // Make default explicit
		setSelection(new IRegion[] { new Region(L1_LEN + L2_LEN + 2, 0) });
		assertEquals(L1_LEN + L2_LEN + 2, widget.getCaretOffset());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_UP);

		assertEquals(L1_LEN + 8, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 0), new Region(L1_LEN + L2_LEN + 2, 0) },
				getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(L1_LEN + 8, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 0) }, getSelection());
	}

	@Test
	public void testMultiCaretUp_withSingleCaretAtBeginningOfText_doesNotChangeCaret() throws Exception {
		setSelection(new IRegion[] { new Region(0, 0) });
		assertEquals(0, widget.getCaretOffset());

		widget.setSize(800, 500); // make sure the widget is not size (0,0)
		executeCommand(MULTI_CARET_UP);

		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0) }, getSelection());

		executeCommand(STOP_MULTI_SELECTION);
		assertEquals(0, widget.getCaretOffset());
		assertArrayEquals(new IRegion[] { new Region(0, 0) }, getSelection());
	}

	// Helper methods

	private void executeCommand(String commandId) throws Exception {
		Command command = PlatformUI.getWorkbench().getService(ICommandService.class).getCommand(commandId);
		command.executeWithChecks(new ExecutionEvent(command, Collections.EMPTY_MAP, null, null));
	}

	private void setSelection(IRegion[] regions) {
		IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
		editor.getSelectionProvider().setSelection(new MultiTextSelection(document, regions));
	}

	private IRegion[] getSelection() {
		return ((IMultiTextSelection) editor.getSelectionProvider().getSelection()).getRegions();
	}
}

Back to the top