findController.test.ts 23.6 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

A
Alex Dima 已提交
7
import * as assert from 'assert';
S
Sandeep Somavarapu 已提交
8 9
import { TPromise } from 'vs/base/common/winjs.base';
import { Emitter } from 'vs/base/common/event';
J
Johannes Rieken 已提交
10 11 12
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { Position } from 'vs/editor/common/core/position';
import { Selection } from 'vs/editor/common/core/selection';
A
Alex Dima 已提交
13
import { Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
14
import { EndOfLineSequence, ICommonCodeEditor } from 'vs/editor/common/editorCommon';
15 16
import {
	CommonFindController, FindStartFocusAction, IFindStartOptions,
17 18
	NextMatchFindAction, StartFindAction, SelectHighlightsAction,
	AddSelectionToNextFindMatchAction
19
} from 'vs/editor/contrib/find/common/findController';
20
import { MockCodeEditor, withMockCodeEditor } from 'vs/editor/test/common/mocks/mockCodeEditor';
J
Johannes Rieken 已提交
21
import { HistoryNavigator } from 'vs/base/common/history';
S
Sandeep Somavarapu 已提交
22
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
23
import { IStorageService } from 'vs/platform/storage/common/storage';
24
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
S
Sandeep Somavarapu 已提交
25
import { Delayer } from 'vs/base/common/async';
26 27 28 29

class TestFindController extends CommonFindController {

	public hasFocus: boolean;
30
	public delayUpdateHistory: boolean = false;
S
Sandeep Somavarapu 已提交
31 32 33 34
	public delayedUpdateHistoryPromise: TPromise<void>;

	private _delayedUpdateHistoryEvent: Emitter<void> = new Emitter<void>();

35 36
	constructor(editor: ICommonCodeEditor, @IContextKeyService contextKeyService: IContextKeyService, @IStorageService storageService: IStorageService) {
		super(editor, contextKeyService, storageService);
S
Sandeep Somavarapu 已提交
37 38
		this._updateHistoryDelayer = new Delayer<void>(50);
	}
39

J
Johannes Rieken 已提交
40
	protected _start(opts: IFindStartOptions): void {
41 42 43 44 45 46
		super._start(opts);

		if (opts.shouldFocus !== FindStartFocusAction.NoFocusChange) {
			this.hasFocus = true;
		}
	}
47 48

	protected _delayedUpdateHistory() {
S
Sandeep Somavarapu 已提交
49 50 51 52 53 54 55 56 57
		if (!this.delayedUpdateHistoryPromise) {
			this.delayedUpdateHistoryPromise = new TPromise<void>((c, e) => {
				const disposable = this._delayedUpdateHistoryEvent.event(() => {
					disposable.dispose();
					this.delayedUpdateHistoryPromise = null;
					c(null);
				});
			});
		}
58 59 60 61 62 63
		if (this.delayUpdateHistory) {
			super._delayedUpdateHistory();
		} else {
			this._updateHistory();
		}
	}
S
Sandeep Somavarapu 已提交
64 65 66 67 68

	protected _updateHistory() {
		super._updateHistory();
		this._delayedUpdateHistoryEvent.fire();
	}
69 70
}

71 72 73 74
function fromRange(rng: Range): number[] {
	return [rng.startLineNumber, rng.startColumn, rng.endLineNumber, rng.endColumn];
}

75
suite('FindController', () => {
76
	let queryState = {};
77 78 79 80 81 82
	let serviceCollection = new ServiceCollection();
	serviceCollection.set(IStorageService, <any>{
		get: (key) => queryState[key],
		getBoolean: (key) => !!queryState[key],
		store: (key: string, value: any) => { queryState[key] = value; }
	});
83 84 85 86 87 88 89

	test('issue #1857: F3, Find Next, acts like "Find Under Cursor"', () => {
		withMockCodeEditor([
			'ABC',
			'ABC',
			'XYZ',
			'ABC'
90
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
91 92 93 94

			// The cursor is at the very top, of the file, at the first ABC
			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			let findState = findController.getState();
A
Alex Dima 已提交
95 96
			let startFindAction = new StartFindAction();
			let nextMatchFindAction = new NextMatchFindAction();
97 98

			// I hit Ctrl+F to show the Find dialog
A
Alex Dima 已提交
99
			startFindAction.run(null, editor);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

			// I type ABC.
			findState.change({ searchString: 'A' }, true);
			findState.change({ searchString: 'AB' }, true);
			findState.change({ searchString: 'ABC' }, true);

			// The first ABC is highlighted.
			assert.deepEqual(fromRange(editor.getSelection()), [1, 1, 1, 4]);

			// I hit Esc to exit the Find dialog.
			findController.closeFindWidget();
			findController.hasFocus = false;

			// The cursor is now at end of the first line, with ABC on that line highlighted.
			assert.deepEqual(fromRange(editor.getSelection()), [1, 1, 1, 4]);

			// I hit delete to remove it and change the text to XYZ.
117
			editor.pushUndoStop();
118 119
			editor.executeEdits('test', [EditOperation.delete(new Range(1, 1, 1, 4))]);
			editor.executeEdits('test', [EditOperation.insert(new Position(1, 1), 'XYZ')]);
120
			editor.pushUndoStop();
121 122 123 124 125 126 127 128 129 130 131 132

			// At this point the text editor looks like this:
			//   XYZ
			//   ABC
			//   XYZ
			//   ABC
			assert.equal(editor.getModel().getLineContent(1), 'XYZ');

			// The cursor is at end of the first line.
			assert.deepEqual(fromRange(editor.getSelection()), [1, 4, 1, 4]);

			// I hit F3 to "Find Next" to find the next occurrence of ABC, but instead it searches for XYZ.
A
Alex Dima 已提交
133
			nextMatchFindAction.run(null, editor);
134 135 136 137 138 139 140

			assert.equal(findState.searchString, 'ABC');
			assert.equal(findController.hasFocus, false);

			findController.dispose();
		});
	});
141 142 143 144

	test('issue #3090: F3 does not loop with two matches on a single line', () => {
		withMockCodeEditor([
			'import nls = require(\'vs/nls\');'
145
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
146 147

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
A
Alex Dima 已提交
148
			let nextMatchFindAction = new NextMatchFindAction();
149 150 151 152 153 154

			editor.setPosition({
				lineNumber: 1,
				column: 9
			});

A
Alex Dima 已提交
155
			nextMatchFindAction.run(null, editor);
156 157
			assert.deepEqual(fromRange(editor.getSelection()), [1, 26, 1, 29]);

A
Alex Dima 已提交
158
			nextMatchFindAction.run(null, editor);
159 160 161 162 163
			assert.deepEqual(fromRange(editor.getSelection()), [1, 8, 1, 11]);

			findController.dispose();
		});
	});
164 165 166 167 168 169

	test('issue #6149: Auto-escape highlighted text for search and replace regex mode', () => {
		withMockCodeEditor([
			'var x = (3 * 5)',
			'var y = (3 * 5)',
			'var z = (3  * 5)',
170
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
171 172

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
A
Alex Dima 已提交
173 174
			let startFindAction = new StartFindAction();
			let nextMatchFindAction = new NextMatchFindAction();
175 176 177 178

			editor.setSelection(new Selection(1, 9, 1, 13));

			findController.toggleRegex();
A
Alex Dima 已提交
179
			startFindAction.run(null, editor);
180

A
Alex Dima 已提交
181
			nextMatchFindAction.run(null, editor);
182 183
			assert.deepEqual(fromRange(editor.getSelection()), [2, 9, 2, 13]);

A
Alex Dima 已提交
184
			nextMatchFindAction.run(null, editor);
185 186 187 188 189
			assert.deepEqual(fromRange(editor.getSelection()), [1, 9, 1, 13]);

			findController.dispose();
		});
	});
190 191 192 193 194 195

	test('issue #8817: Cursor position changes when you cancel multicursor', () => {
		withMockCodeEditor([
			'var x = (3 * 5)',
			'var y = (3 * 5)',
			'var z = (3 * 5)',
196
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
197 198

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
A
Alex Dima 已提交
199
			let selectHighlightsAction = new SelectHighlightsAction();
200 201 202

			editor.setSelection(new Selection(2, 9, 2, 16));

A
Alex Dima 已提交
203
			selectHighlightsAction.run(null, editor);
204 205 206 207 208 209 210 211 212 213 214 215 216
			assert.deepEqual(editor.getSelections().map(fromRange), [
				[2, 9, 2, 16],
				[1, 9, 1, 16],
				[3, 9, 3, 16],
			]);

			editor.trigger('test', 'removeSecondaryCursors', null);

			assert.deepEqual(fromRange(editor.getSelection()), [2, 9, 2, 16]);

			findController.dispose();
		});
	});
217 218 219 220 221 222

	test('issue #9043: Clear search scope when find widget is hidden', () => {
		withMockCodeEditor([
			'var x = (3 * 5)',
			'var y = (3 * 5)',
			'var z = (3 * 5)',
223
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			findController.start({
				forceRevealReplace: false,
				seedSearchStringFromSelection: false,
				shouldFocus: FindStartFocusAction.NoFocusChange,
				shouldAnimate: false
			});

			assert.equal(findController.getState().searchScope, null);

			findController.getState().change({
				searchScope: new Range(1, 1, 1, 5)
			}, false);

			assert.deepEqual(findController.getState().searchScope, new Range(1, 1, 1, 5));

			findController.closeFindWidget();
			assert.equal(findController.getState().searchScope, null);
		});
	});
245 246 247 248 249 250

	test('find term is added to history on state change', () => {
		withMockCodeEditor([
			'var x = (3 * 5)',
			'var y = (3 * 5)',
			'var z = (3 * 5)',
251
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			findController.getState().change({ searchString: '1' }, false);
			findController.getState().change({ searchString: '2' }, false);
			findController.getState().change({ searchString: '3' }, false);

			assert.deepEqual(['1', '2', '3'], toArray(findController.getHistory()));
		});
	});

	test('find term is added with delay', (done) => {
		withMockCodeEditor([
			'var x = (3 * 5)',
			'var y = (3 * 5)',
			'var z = (3 * 5)',
267
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
268 269 270 271 272 273 274

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			findController.delayUpdateHistory = true;
			findController.getState().change({ searchString: '1' }, false);
			findController.getState().change({ searchString: '2' }, false);
			findController.getState().change({ searchString: '3' }, false);

S
Sandeep Somavarapu 已提交
275
			findController.delayedUpdateHistoryPromise.then(() => {
276 277
				assert.deepEqual(['3'], toArray(findController.getHistory()));
				done();
S
Sandeep Somavarapu 已提交
278
			});
279 280 281 282 283 284 285 286
		});
	});

	test('show previous find term', () => {
		withMockCodeEditor([
			'var x = (3 * 5)',
			'var y = (3 * 5)',
			'var z = (3 * 5)',
287
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			findController.getState().change({ searchString: '1' }, false);
			findController.getState().change({ searchString: '2' }, false);
			findController.getState().change({ searchString: '3' }, false);

			findController.showPreviousFindTerm();
			assert.deepEqual('2', findController.getState().searchString);
		});
	});

	test('show previous find term do not update history', () => {
		withMockCodeEditor([
			'var x = (3 * 5)',
			'var y = (3 * 5)',
			'var z = (3 * 5)',
304
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			findController.getState().change({ searchString: '1' }, false);
			findController.getState().change({ searchString: '2' }, false);
			findController.getState().change({ searchString: '3' }, false);

			findController.showPreviousFindTerm();
			assert.deepEqual(['1', '2', '3'], toArray(findController.getHistory()));
		});
	});

	test('show next find term', () => {
		withMockCodeEditor([
			'var x = (3 * 5)',
			'var y = (3 * 5)',
			'var z = (3 * 5)',
321
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			findController.getState().change({ searchString: '1' }, false);
			findController.getState().change({ searchString: '2' }, false);
			findController.getState().change({ searchString: '3' }, false);
			findController.getState().change({ searchString: '4' }, false);

			findController.showPreviousFindTerm();
			findController.showPreviousFindTerm();
			findController.showNextFindTerm();
			assert.deepEqual('3', findController.getState().searchString);
		});
	});

	test('show next find term do not update history', () => {
		withMockCodeEditor([
			'var x = (3 * 5)',
			'var y = (3 * 5)',
			'var z = (3 * 5)',
341
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
342 343 344 345 346 347 348 349 350 351 352 353 354 355

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			findController.getState().change({ searchString: '1' }, false);
			findController.getState().change({ searchString: '2' }, false);
			findController.getState().change({ searchString: '3' }, false);
			findController.getState().change({ searchString: '4' }, false);

			findController.showPreviousFindTerm();
			findController.showPreviousFindTerm();
			findController.showNextFindTerm();
			assert.deepEqual(['1', '2', '3', '4'], toArray(findController.getHistory()));
		});
	});

356 357 358 359 360 361 362 363 364 365 366
	test('AddSelectionToNextFindMatchAction can work with multiline', () => {
		withMockCodeEditor([
			'',
			'qwe',
			'rty',
			'',
			'qwe',
			'',
			'rty',
			'qwe',
			'rty'
367
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
368 369 370 371 372 373 374 375 376 377 378 379 380

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			let addSelectionToNextFindMatch = new AddSelectionToNextFindMatchAction();

			editor.setSelection(new Selection(2, 1, 3, 4));

			addSelectionToNextFindMatch.run(null, editor);
			assert.deepEqual(editor.getSelections().map(fromRange), [
				[2, 1, 3, 4],
				[8, 1, 9, 4]
			]);

			editor.trigger('test', 'removeSecondaryCursors', null);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

			assert.deepEqual(fromRange(editor.getSelection()), [2, 1, 3, 4]);

			findController.dispose();
		});
	});

	test('issue #23541: Multiline Ctrl+D does not work in CRLF files', () => {
		withMockCodeEditor([
			'',
			'qwe',
			'rty',
			'',
			'qwe',
			'',
			'rty',
			'qwe',
			'rty'
399
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414

			editor.getModel().setEOL(EndOfLineSequence.CRLF);

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			let addSelectionToNextFindMatch = new AddSelectionToNextFindMatchAction();

			editor.setSelection(new Selection(2, 1, 3, 4));

			addSelectionToNextFindMatch.run(null, editor);
			assert.deepEqual(editor.getSelections().map(fromRange), [
				[2, 1, 3, 4],
				[8, 1, 9, 4]
			]);

			editor.trigger('test', 'removeSecondaryCursors', null);
415 416 417 418 419 420 421

			assert.deepEqual(fromRange(editor.getSelection()), [2, 1, 3, 4]);

			findController.dispose();
		});
	});

422 423 424
	test('issue #18111: Regex replace with single space replaces with no space', () => {
		withMockCodeEditor([
			'HRESULT OnAmbientPropertyChange(DISPID   dispid);'
425
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);

			let startFindAction = new StartFindAction();
			startFindAction.run(null, editor);

			findController.getState().change({ searchString: '\\b\\s{3}\\b', replaceString: ' ', isRegex: true }, false);
			findController.moveToNextMatch();

			assert.deepEqual(editor.getSelections().map(fromRange), [
				[1, 39, 1, 42]
			]);

			findController.replace();

			assert.deepEqual(editor.getValue(), 'HRESULT OnAmbientPropertyChange(DISPID dispid);');

			findController.dispose();
		});
	});

447 448 449 450 451 452 453 454 455 456
	function toArray(historyNavigator: HistoryNavigator<string>): string[] {
		let result = [];
		historyNavigator.first();
		if (historyNavigator.current()) {
			do {
				result.push(historyNavigator.current());
			} while (historyNavigator.next());
		}
		return result;
	}
457

458
	function testAddSelectionToNextFindMatchAction(text: string[], callback: (editor: MockCodeEditor, action: AddSelectionToNextFindMatchAction, findController: TestFindController) => void): void {
459
		withMockCodeEditor(text, { serviceCollection: serviceCollection }, (editor, cursor) => {
460 461 462 463 464

			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);

			let action = new AddSelectionToNextFindMatchAction();

465
			callback(editor, action, findController);
466 467 468 469 470 471

			findController.dispose();
		});
	}

	test('AddSelectionToNextFindMatchAction starting with single collapsed selection', () => {
472 473 474 475 476 477
		const text = [
			'abc pizza',
			'abc house',
			'abc bar'
		];
		testAddSelectionToNextFindMatchAction(text, (editor, action, findController) => {
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
			editor.setSelections([
				new Selection(1, 2, 1, 2),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
				new Selection(3, 1, 3, 4),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
				new Selection(3, 1, 3, 4),
			]);
		});
	});

	test('AddSelectionToNextFindMatchAction starting with two selections, one being collapsed 1)', () => {
510 511 512 513 514 515
		const text = [
			'abc pizza',
			'abc house',
			'abc bar'
		];
		testAddSelectionToNextFindMatchAction(text, (editor, action, findController) => {
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
			editor.setSelections([
				new Selection(1, 1, 1, 4),
				new Selection(2, 2, 2, 2),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
				new Selection(3, 1, 3, 4),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
				new Selection(3, 1, 3, 4),
			]);
		});
	});

	test('AddSelectionToNextFindMatchAction starting with two selections, one being collapsed 2)', () => {
544 545 546 547 548 549
		const text = [
			'abc pizza',
			'abc house',
			'abc bar'
		];
		testAddSelectionToNextFindMatchAction(text, (editor, action, findController) => {
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
			editor.setSelections([
				new Selection(1, 2, 1, 2),
				new Selection(2, 1, 2, 4),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
				new Selection(3, 1, 3, 4),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
				new Selection(3, 1, 3, 4),
			]);
		});
	});

	test('AddSelectionToNextFindMatchAction starting with all collapsed selections', () => {
578 579 580 581 582 583
		const text = [
			'abc pizza',
			'abc house',
			'abc bar'
		];
		testAddSelectionToNextFindMatchAction(text, (editor, action, findController) => {
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
			editor.setSelections([
				new Selection(1, 2, 1, 2),
				new Selection(2, 2, 2, 2),
				new Selection(3, 1, 3, 1),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
				new Selection(3, 1, 3, 4),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 4),
				new Selection(2, 1, 2, 4),
				new Selection(3, 1, 3, 4),
			]);
		});
	});

	test('AddSelectionToNextFindMatchAction starting with all collapsed selections on different words', () => {
607 608 609 610 611 612
		const text = [
			'abc pizza',
			'abc house',
			'abc bar'
		];
		testAddSelectionToNextFindMatchAction(text, (editor, action, findController) => {
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
			editor.setSelections([
				new Selection(1, 6, 1, 6),
				new Selection(2, 6, 2, 6),
				new Selection(3, 6, 3, 6),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 5, 1, 10),
				new Selection(2, 5, 2, 10),
				new Selection(3, 5, 3, 8),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 5, 1, 10),
				new Selection(2, 5, 2, 10),
				new Selection(3, 5, 3, 8),
			]);
		});
	});
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

	test('issue #20651: AddSelectionToNextFindMatchAction case insensitive', () => {
		const text = [
			'test',
			'testte',
			'Test',
			'testte',
			'test'
		];
		testAddSelectionToNextFindMatchAction(text, (editor, action, findController) => {
			editor.setSelections([
				new Selection(1, 1, 1, 5),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 5),
				new Selection(2, 1, 2, 5),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 5),
				new Selection(2, 1, 2, 5),
				new Selection(3, 1, 3, 5),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 5),
				new Selection(2, 1, 2, 5),
				new Selection(3, 1, 3, 5),
				new Selection(4, 1, 4, 5),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 5),
				new Selection(2, 1, 2, 5),
				new Selection(3, 1, 3, 5),
				new Selection(4, 1, 4, 5),
				new Selection(5, 1, 5, 5),
			]);

			action.run(null, editor);
			assert.deepEqual(editor.getSelections(), [
				new Selection(1, 1, 1, 5),
				new Selection(2, 1, 2, 5),
				new Selection(3, 1, 3, 5),
				new Selection(4, 1, 4, 5),
				new Selection(5, 1, 5, 5),
			]);
		});
	});
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

suite('FindController query options persistence', () => {
	let queryState = { 'editor.isRegex': false, 'editor.matchCase': false, 'editor.wholeWord': false };
	let serviceCollection = new ServiceCollection();
	serviceCollection.set(IStorageService, <any>{
		get: (key) => queryState[key],
		getBoolean: (key) => !!queryState[key],
		store: (key: string, value: any) => { queryState[key] = value; }
	});

	test('matchCase', () => {
		withMockCodeEditor([
			'abc',
			'ABC',
			'XYZ',
			'ABC'
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
			queryState = { 'editor.isRegex': false, 'editor.matchCase': true, 'editor.wholeWord': false };
			// The cursor is at the very top, of the file, at the first ABC
			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			let findState = findController.getState();
			let startFindAction = new StartFindAction();

			// I hit Ctrl+F to show the Find dialog
			startFindAction.run(null, editor);

			// I type ABC.
			findState.change({ searchString: 'ABC' }, true);
			// The second ABC is highlighted as matchCase is true.
			assert.deepEqual(fromRange(editor.getSelection()), [2, 1, 2, 4]);

			findController.dispose();
		});
	});

	queryState = { 'editor.isRegex': false, 'editor.matchCase': false, 'editor.wholeWord': true };

	test('wholeWord', () => {
		withMockCodeEditor([
			'ABC',
			'AB',
			'XYZ',
			'ABC'
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
			queryState = { 'editor.isRegex': false, 'editor.matchCase': false, 'editor.wholeWord': true };
			// The cursor is at the very top, of the file, at the first ABC
			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			let findState = findController.getState();
			let startFindAction = new StartFindAction();

			// I hit Ctrl+F to show the Find dialog
			startFindAction.run(null, editor);

			// I type AB.
			findState.change({ searchString: 'AB' }, true);
			// The second AB is highlighted as wholeWord is true.
			assert.deepEqual(fromRange(editor.getSelection()), [2, 1, 2, 3]);

			findController.dispose();
		});
	});

	test('toggling options is saved', () => {
		withMockCodeEditor([
			'ABC',
			'AB',
			'XYZ',
			'ABC'
		], { serviceCollection: serviceCollection }, (editor, cursor) => {
			queryState = { 'editor.isRegex': false, 'editor.matchCase': false, 'editor.wholeWord': true };
			// The cursor is at the very top, of the file, at the first ABC
			let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController);
			findController.toggleRegex();
			assert.equal(queryState['editor.isRegex'], true);

			findController.dispose();
		});
	});
});