Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1f700b1672c20d9eef0cbfd574b8cdf4e7a6c27c (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
/*******************************************************************************
 * 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 LINE_1 = "private static String a;\n";
	private static final String LINE_2 = "private static String b;\n";
	private static final String LINE_3 = "private 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, 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 + 8, 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 + 8, 6) },
				getSelection());

		executeCommand(MULTI_SELECTION_DOWN);

		assertArrayEquals(new IRegion[] { new Region(L1_LEN + 8, 6), new Region(L1_LEN + L2_LEN + 8, 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, 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, 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, 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 + 8, 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 + 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(L1_LEN + 8, 6), new Region(L1_LEN + L2_LEN + 8, 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 + 8, 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());
	}

	// 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