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

import * as nls from 'vs/nls';
A
Alex Dima 已提交
8
import {KeyCode, KeyMod} from 'vs/base/common/keyCodes';
9
import {Disposable} from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
10 11
import {TPromise} from 'vs/base/common/winjs.base';
import {IKeybindingContextKey, IKeybindingService, IKeybindings} from 'vs/platform/keybinding/common/keybindingService';
12
import {Range} from 'vs/editor/common/core/range';
A
Alex Dima 已提交
13 14 15
import {Selection} from 'vs/editor/common/core/selection';
import {EditorAction} from 'vs/editor/common/editorAction';
import {Behaviour} from 'vs/editor/common/editorActionEnablement';
16
import * as strings from 'vs/base/common/strings';
A
Alex Dima 已提交
17 18 19 20
import * as editorCommon from 'vs/editor/common/editorCommon';
import {CommonEditorRegistry, ContextKey, EditorActionDescriptor} from 'vs/editor/common/editorCommonExtensions';
import {FIND_IDS, FindModelBoundToEditorModel} from 'vs/editor/contrib/find/common/findModel';
import {FindReplaceState, FindReplaceStateChangedEvent, INewFindReplaceState} from 'vs/editor/contrib/find/common/findState';
21
import {DocumentHighlightProviderRegistry} from 'vs/editor/common/modes';
22
import {RunOnceScheduler} from 'vs/base/common/async';
23 24 25 26 27 28 29 30 31 32 33 34 35 36

export enum FindStartFocusAction {
	NoFocusChange,
	FocusFindInput,
	FocusReplaceInput
}

export interface IFindStartOptions {
	forceRevealReplace:boolean;
	seedSearchStringFromSelection:boolean;
	shouldFocus:FindStartFocusAction;
	shouldAnimate:boolean;
}

37
export const CONTEXT_FIND_WIDGET_VISIBLE = 'findWidgetVisible';
38

A
Alex Dima 已提交
39
export class CommonFindController extends Disposable implements editorCommon.IEditorContribution {
40 41 42

	static ID = 'editor.contrib.findController';

A
Alex Dima 已提交
43
	private _editor: editorCommon.ICommonCodeEditor;
44 45 46 47
	private _findWidgetVisible: IKeybindingContextKey<boolean>;
	protected _state: FindReplaceState;
	private _model: FindModelBoundToEditorModel;

A
Alex Dima 已提交
48
	static getFindController(editor:editorCommon.ICommonCodeEditor): CommonFindController {
49 50 51
		return <CommonFindController>editor.getContribution(CommonFindController.ID);
	}

A
Alex Dima 已提交
52
	constructor(editor:editorCommon.ICommonCodeEditor, @IKeybindingService keybindingService: IKeybindingService) {
53 54 55 56 57 58 59 60 61
		super();
		this._editor = editor;
		this._findWidgetVisible = keybindingService.createKey(CONTEXT_FIND_WIDGET_VISIBLE, false);

		this._state = this._register(new FindReplaceState());
		this._register(this._state.addChangeListener((e) => this._onStateChanged(e)));

		this._model = null;

A
Alex Dima 已提交
62
		this._register(this._editor.onDidChangeModel(() => {
63 64 65 66
			let shouldRestartFind = (this._editor.getModel() && this._state.isRevealed);

			this.disposeModel();

67 68 69 70
			this._state.change({
				searchScope: null
			}, false);

71 72 73 74 75
			if (shouldRestartFind) {
				this._start({
					forceRevealReplace: false,
					seedSearchStringFromSelection: false,
					shouldFocus: FindStartFocusAction.NoFocusChange,
76
					shouldAnimate: false,
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
				});
			}
		}));
	}

	public dispose(): void {
		this.disposeModel();
		super.dispose();
	}

	private disposeModel(): void {
		if (this._model) {
			this._model.dispose();
			this._model = null;
		}
	}

	public getId(): string {
		return CommonFindController.ID;
	}

	private _onStateChanged(e:FindReplaceStateChangedEvent): void {
		if (e.isRevealed) {
			if (this._state.isRevealed) {
				this._findWidgetVisible.set(true);
			} else {
				this._findWidgetVisible.reset();
				this.disposeModel();
			}
		}
	}

	public getState(): FindReplaceState {
		return this._state;
	}

	public closeFindWidget(): void {
		this._state.change({ isRevealed: false }, false);
		this._editor.focus();
	}

	public toggleCaseSensitive(): void {
		this._state.change({ matchCase: !this._state.matchCase }, false);
	}

	public toggleWholeWords(): void {
		this._state.change({ wholeWord: !this._state.wholeWord }, false);
	}

	public toggleRegex(): void {
		this._state.change({ isRegex: !this._state.isRegex }, false);
	}

	public setSearchString(searchString:string): void {
		this._state.change({ searchString: searchString }, false);
	}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	public getSelectionSearchString(): string {
		let selection = this._editor.getSelection();

		if (selection.startLineNumber === selection.endLineNumber) {
			if (selection.isEmpty()) {
				let wordAtPosition = this._editor.getModel().getWordAtPosition(selection.getStartPosition());
				if (wordAtPosition) {
					return wordAtPosition.word;
				}
			} else {
				return this._editor.getModel().getValueInRange(selection);
			}
		}

		return null;
	}

151 152 153 154 155 156 157 158 159 160 161 162 163 164
	protected _start(opts:IFindStartOptions): void {
		this.disposeModel();

		if (!this._editor.getModel()) {
			// cannot do anything with an editor that doesn't have a model...
			return;
		}

		let stateChanges: INewFindReplaceState = {
			isRevealed: true
		};

		// Consider editor selection and overwrite the state with it
		if (opts.seedSearchStringFromSelection) {
165 166
			let selectionSearchString = this.getSelectionSearchString();
			if (selectionSearchString) {
167 168 169 170 171
				if (this._state.isRegex) {
					stateChanges.searchString = strings.escapeRegExpCharacters(selectionSearchString);
				} else {
					stateChanges.searchString = selectionSearchString;
				}
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
			}
		}

		// Overwrite isReplaceRevealed
		if (opts.forceRevealReplace) {
			stateChanges.isReplaceRevealed = true;
		}

		this._state.change(stateChanges, false);

		if (!this._model) {
			this._model = new FindModelBoundToEditorModel(this._editor, this._state);
		}
	}

187 188
	public start(opts:IFindStartOptions): void {
		this._start(opts);
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
	}

	public moveToNextMatch(): boolean {
		if (this._model) {
			this._model.moveToNextMatch();
			return true;
		}
		return false;
	}

	public moveToPrevMatch(): boolean {
		if (this._model) {
			this._model.moveToPrevMatch();
			return true;
		}
		return false;
	}

	public replace(): boolean {
		if (this._model) {
			this._model.replace();
			return true;
		}
		return false;
	}

	public replaceAll(): boolean {
		if (this._model) {
			this._model.replaceAll();
			return true;
		}
		return false;
	}
I
Inori 已提交
222 223 224

	public selectAllMatches(): boolean {
		if (this._model) {
I
Inori 已提交
225
			this._model.selectAllMatches();
226
			this._editor.focus();
I
Inori 已提交
227 228 229 230
			return true;
		}
		return false;
	}
231 232 233 234
}

export class StartFindAction extends EditorAction {

235
	constructor(descriptor: editorCommon.IEditorActionDescriptorData, editor: editorCommon.ICommonCodeEditor) {
236 237 238 239 240
		super(descriptor, editor, Behaviour.WidgetFocus);
	}

	public run(): TPromise<boolean> {
		let controller = CommonFindController.getFindController(this.editor);
241 242 243 244 245 246
		controller.start({
			forceRevealReplace: false,
			seedSearchStringFromSelection: true,
			shouldFocus: FindStartFocusAction.FocusFindInput,
			shouldAnimate: true
		});
247 248 249 250
		return TPromise.as(true);
	}
}

B
Benjamin Pasero 已提交
251
export abstract class MatchFindAction extends EditorAction {
252
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
253 254 255 256 257
		super(descriptor, editor, Behaviour.WidgetFocus);
	}

	public run(): TPromise<boolean> {
		let controller = CommonFindController.getFindController(this.editor);
258
		if (!this._run(controller)) {
259 260 261 262 263 264
			controller.start({
				forceRevealReplace: false,
				seedSearchStringFromSelection: (controller.getState().searchString.length === 0),
				shouldFocus: FindStartFocusAction.NoFocusChange,
				shouldAnimate: true
			});
265
			this._run(controller);
266 267 268
		}
		return TPromise.as(true);
	}
269 270

	protected abstract _run(controller:CommonFindController): boolean;
271 272
}

273 274 275 276 277
export class NextMatchFindAction extends MatchFindAction {
	protected _run(controller:CommonFindController): boolean {
		return controller.moveToNextMatch();
	}
}
278

279 280 281 282 283 284
export class PreviousMatchFindAction extends MatchFindAction {
	protected _run(controller:CommonFindController): boolean {
		return controller.moveToPrevMatch();
	}
}

B
Benjamin Pasero 已提交
285
export abstract class SelectionMatchFindAction extends EditorAction {
286
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
287 288 289 290 291
		super(descriptor, editor, Behaviour.WidgetFocus);
	}

	public run(): TPromise<boolean> {
		let controller = CommonFindController.getFindController(this.editor);
292 293 294 295 296
		let selectionSearchString = controller.getSelectionSearchString();
		if (selectionSearchString) {
			controller.setSearchString(selectionSearchString);
		}
		if (!this._run(controller)) {
297 298
			controller.start({
				forceRevealReplace: false,
299
				seedSearchStringFromSelection: false,
300 301 302
				shouldFocus: FindStartFocusAction.NoFocusChange,
				shouldAnimate: true
			});
303
			this._run(controller);
304 305 306
		}
		return TPromise.as(true);
	}
307 308 309 310 311 312 313 314 315 316 317 318 319 320

	protected abstract _run(controller:CommonFindController): boolean;
}

export class NextSelectionMatchFindAction extends SelectionMatchFindAction {
	protected _run(controller:CommonFindController): boolean {
		return controller.moveToNextMatch();
	}
}

export class PreviousSelectionMatchFindAction extends SelectionMatchFindAction {
	protected _run(controller:CommonFindController): boolean {
		return controller.moveToPrevMatch();
	}
321 322 323 324
}

export class StartFindReplaceAction extends EditorAction {

325
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
326 327 328 329 330
		super(descriptor, editor, Behaviour.WidgetFocus | Behaviour.Writeable);
	}

	public run(): TPromise<boolean> {
		let controller = CommonFindController.getFindController(this.editor);
331 332
		controller.start({
			forceRevealReplace: true,
333
			seedSearchStringFromSelection: true,
334 335 336
			shouldFocus: FindStartFocusAction.FocusReplaceInput,
			shouldAnimate: true
		});
337 338 339 340 341 342 343 344 345
		return TPromise.as(true);
	}
}

export interface IMultiCursorFindResult {
	searchText:string;
	matchCase:boolean;
	wholeWord:boolean;

346
	nextMatch: Selection;
一丝 已提交
347
	previousMatch: Selection;
348 349
}

A
Alex Dima 已提交
350
function multiCursorFind(editor:editorCommon.ICommonCodeEditor, changeFindSearchString:boolean): IMultiCursorFindResult {
351 352 353
	let controller = CommonFindController.getFindController(editor);
	let state = controller.getState();
	let searchText: string,
一丝 已提交
354 355
		nextMatch: Selection,
		previousMatch: Selection;
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

	// In any case, if the find widget was ever opened, the options are taken from it
	let wholeWord = state.wholeWord;
	let matchCase = state.matchCase;

	// Find widget owns what we search for if:
	//  - focus is not in the editor (i.e. it is in the find widget)
	//  - and the search widget is visible
	//  - and the search string is non-empty
	if (!editor.isFocused() && state.isRevealed && state.searchString.length > 0) {
		// Find widget owns what is searched for
		searchText = state.searchString;
	} else {
		// Selection owns what is searched for
		let s = editor.getSelection();

		if (s.startLineNumber !== s.endLineNumber) {
			// Cannot search for multiline string... yet...
			return null;
		}

		if (s.isEmpty()) {
			// selection is empty => expand to current word
			let word = editor.getModel().getWordAtPosition(s.getStartPosition());
			if (!word) {
				return null;
			}
			searchText = word.word;
A
Alex Dima 已提交
384
			nextMatch = new Selection(s.startLineNumber, word.startColumn, s.startLineNumber, word.endColumn);
一丝 已提交
385
			previousMatch = new Selection(s.startLineNumber, word.startColumn, s.startLineNumber, word.endColumn);
386 387 388 389 390 391 392 393 394 395 396 397
		} else {
			searchText = editor.getModel().getValueInRange(s);
		}
		if (changeFindSearchString) {
			controller.setSearchString(searchText);
		}
	}

	return {
		searchText: searchText,
		matchCase: matchCase,
		wholeWord: wholeWord,
一丝 已提交
398 399
		nextMatch: nextMatch,
		previousMatch: previousMatch
400 401 402
	};
}

403
export class SelectNextFindMatchAction extends EditorAction {
404
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
405 406 407
		super(descriptor, editor, Behaviour.WidgetFocus);
	}

408
	protected _getNextMatch(): Selection {
409 410 411 412 413 414 415 416 417 418 419
		let r = multiCursorFind(this.editor, true);
		if (!r) {
			return null;
		}
		if (r.nextMatch) {
			return r.nextMatch;
		}

		let allSelections = this.editor.getSelections();
		let lastAddedSelection = allSelections[allSelections.length - 1];

420
		let nextMatch = this.editor.getModel().findNextMatch(r.searchText, lastAddedSelection.getEndPosition(), false, r.matchCase, r.wholeWord);
421 422 423 424 425

		if (!nextMatch) {
			return null;
		}

A
Alex Dima 已提交
426
		return new Selection(nextMatch.startLineNumber, nextMatch.startColumn, nextMatch.endLineNumber, nextMatch.endColumn);
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
export class SelectPreviousFindMatchAction extends EditorAction {
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
		super(descriptor, editor, Behaviour.WidgetFocus);
	}

	protected _getPreviousMatch(): Selection {
		let r = multiCursorFind(this.editor, true);
		if (!r) {
			return null;
		}
		if (r.previousMatch) {
			return r.previousMatch;
		}

		let allSelections = this.editor.getSelections();
		let lastAddedSelection = allSelections[allSelections.length - 1];

		let previousMatch = this.editor.getModel().findPreviousMatch(r.searchText, lastAddedSelection.getStartPosition(), false, r.matchCase, r.wholeWord);

		if (!previousMatch) {
			return null;
		}

		return new Selection(previousMatch.startLineNumber, previousMatch.startColumn, previousMatch.endLineNumber, previousMatch.endColumn);
	}
}

457
export class AddSelectionToNextFindMatchAction extends SelectNextFindMatchAction {
458
	static ID = FIND_IDS.AddSelectionToNextFindMatchAction;
459

460 461
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
		super(descriptor, editor);
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	}

	public run(): TPromise<boolean> {
		let nextMatch = this._getNextMatch();

		if (!nextMatch) {
			return TPromise.as(false);
		}

		let allSelections = this.editor.getSelections();
		this.editor.setSelections(allSelections.concat(nextMatch));
		this.editor.revealRangeInCenterIfOutsideViewport(nextMatch);

		return TPromise.as(true);
	}
}

一丝 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
export class AddSelectionToPreviousFindMatchAction extends SelectPreviousFindMatchAction {
	static ID = FIND_IDS.AddSelectionToPreviousFindMatchAction;

	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
		super(descriptor, editor);
	}

	public run(): TPromise<boolean> {
		let previousMatch = this._getPreviousMatch();

		if (!previousMatch) {
			return TPromise.as(false);
		}

		let allSelections = this.editor.getSelections();
		this.editor.setSelections(allSelections.concat(previousMatch));
		this.editor.revealRangeInCenterIfOutsideViewport(previousMatch);

		return TPromise.as(true);
	}
}

501
export class MoveSelectionToNextFindMatchAction extends SelectNextFindMatchAction {
502
	static ID = FIND_IDS.MoveSelectionToNextFindMatchAction;
503

504 505
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
		super(descriptor, editor);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	}

	public run(): TPromise<boolean> {
		let nextMatch = this._getNextMatch();

		if (!nextMatch) {
			return TPromise.as(false);
		}

		let allSelections = this.editor.getSelections();
		this.editor.setSelections(allSelections.slice(0, allSelections.length - 1).concat(nextMatch));
		this.editor.revealRangeInCenterIfOutsideViewport(nextMatch);

		return TPromise.as(true);
	}
}

一丝 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
export class MoveSelectionToPreviousFindMatchAction extends SelectPreviousFindMatchAction {
	static ID = FIND_IDS.MoveSelectionToPreviousFindMatchAction;

	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
		super(descriptor, editor);
	}

	public run(): TPromise<boolean> {
		let previousMatch = this._getPreviousMatch();

		if (!previousMatch) {
			return TPromise.as(false);
		}

		let allSelections = this.editor.getSelections();
		this.editor.setSelections(allSelections.slice(0, allSelections.length - 1).concat(previousMatch));
		this.editor.revealRangeInCenterIfOutsideViewport(previousMatch);

		return TPromise.as(true);
	}
}

545
export class SelectHighlightsAction extends EditorAction {
546 547 548
	static ID = 'editor.action.selectHighlights';
	static COMPAT_ID = 'editor.action.changeAll';

549
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
550
		let behaviour = Behaviour.WidgetFocus | Behaviour.Writeable;
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
		if (descriptor.id === SelectHighlightsAction.COMPAT_ID) {
			behaviour |= Behaviour.ShowInContextMenu;
		}
		super(descriptor, editor, behaviour);
	}

	public getGroupId(): string {
		return '2_change/1_changeAll';
	}

	public run(): TPromise<boolean> {
		let r = multiCursorFind(this.editor, true);
		if (!r) {
			return TPromise.as(false);
		}

567
		let matches = this.editor.getModel().findMatches(r.searchText, true, false, r.matchCase, r.wholeWord);
568 569

		if (matches.length > 0) {
A
Alex Dima 已提交
570
			this.editor.setSelections(matches.map(m => new Selection(m.startLineNumber, m.startColumn, m.endLineNumber, m.endColumn)));
571 572 573 574 575
		}
		return TPromise.as(true);
	}
}

A
Alex Dima 已提交
576
export class SelectionHighlighter extends Disposable implements editorCommon.IEditorContribution {
577 578
	static ID = 'editor.contrib.selectionHighlighter';

A
Alex Dima 已提交
579
	private editor: editorCommon.ICommonCodeEditor;
580
	private decorations: string[];
581
	private updateSoon: RunOnceScheduler;
582
	private lastWordUnderCursor: Range;
583

584
	constructor(editor:editorCommon.ICommonCodeEditor) {
585 586 587
		super();
		this.editor = editor;
		this.decorations = [];
588 589
		this.updateSoon = this._register(new RunOnceScheduler(() => this._update(), 300));
		this.lastWordUnderCursor = null;
590

A
Alex Dima 已提交
591
		this._register(editor.onDidChangeCursorSelection((e: editorCommon.ICursorSelectionChangedEvent) => {
592
			if (e.selection.isEmpty()) {
A
Alex Dima 已提交
593
				if (e.reason === editorCommon.CursorChangeReason.Explicit) {
594 595 596 597 598
					if (!this.lastWordUnderCursor || !this.lastWordUnderCursor.containsPosition(e.selection.getStartPosition())) {
						// no longer valid
						this.removeDecorations();
					}
					this.updateSoon.schedule();
599 600 601 602 603 604 605 606
				} else {
					this.removeDecorations();

				}
			} else {
				this._update();
			}
		}));
A
Alex Dima 已提交
607
		this._register(editor.onDidChangeModel((e) => {
608 609
			this.removeDecorations();
		}));
610 611 612
		this._register(CommonFindController.getFindController(editor).getState().addChangeListener((e) => {
			this._update();
		}));
613 614 615 616 617 618 619
	}

	public getId(): string {
		return SelectionHighlighter.ID;
	}

	private removeDecorations(): void {
620
		this.lastWordUnderCursor = null;
621 622 623 624 625 626
		if (this.decorations.length > 0) {
			this.decorations = this.editor.deltaDecorations(this.decorations, []);
		}
	}

	private _update(): void {
627 628 629 630 631
		let model = this.editor.getModel();
		if (!model) {
			return;
		}

632
		this.lastWordUnderCursor = null;
A
Alex Dima 已提交
633
		if (!this.editor.getConfiguration().contribInfo.selectionHighlight) {
634 635 636 637 638 639 640 641 642
			return;
		}

		let r = multiCursorFind(this.editor, false);
		if (!r) {
			this.removeDecorations();
			return;
		}

643
		let hasFindOccurences = DocumentHighlightProviderRegistry.has(model);
644 645
		if (r.nextMatch) {
			// This is an empty selection
646
			if (hasFindOccurences) {
647 648 649 650
				// Do not interfere with semantic word highlighting in the no selection case
				this.removeDecorations();
				return;
			}
651 652

			this.lastWordUnderCursor = r.nextMatch;
653 654 655 656 657 658 659 660 661 662 663
		}
		if (/^[ \t]+$/.test(r.searchText)) {
			// whitespace only selection
			this.removeDecorations();
			return;
		}
		if (r.searchText.length > 200) {
			// very long selection
			this.removeDecorations();
			return;
		}
A
Alex Dima 已提交
664 665 666 667 668 669 670 671 672 673 674
		let selections = this.editor.getSelections();
		let firstSelectedText = model.getValueInRange(selections[0]);
		for (let i = 1; i < selections.length; i++) {
			let selectedText = model.getValueInRange(selections[i]);
			if (firstSelectedText !== selectedText) {
				// not all selections have the same text
				this.removeDecorations();
				return;
			}
		}

675

676
		let allMatches = model.findMatches(r.searchText, true, false, r.matchCase, r.wholeWord);
677 678 679 680 681
		allMatches.sort(Range.compareRangesUsingStarts);

		selections.sort(Range.compareRangesUsingStarts);

		// do not overlap with selection (issue #64 and #512)
682
		let matches: Range[] = [];
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
		for (let i = 0, j = 0, len = allMatches.length, lenJ = selections.length; i < len; ) {
			let match = allMatches[i];

			if (j >= lenJ) {
				// finished all editor selections
				matches.push(match);
				i++;
			} else {
				let cmp = Range.compareRangesUsingStarts(match, selections[j]);
				if (cmp < 0) {
					// match is before sel
					matches.push(match);
					i++;
				} else if (cmp > 0) {
					// sel is before match
					j++;
				} else {
					// sel is equal to match
					i++;
					j++;
				}
			}
		}

		let decorations = matches.map(r => {
			return {
				range: r,
				options: {
A
Alex Dima 已提交
711
					stickiness: editorCommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,
712 713 714 715 716 717 718
					className: 'selectionHighlight',
					// Show in overviewRuler only if model has no semantic highlighting
					overviewRuler: (hasFindOccurences ? undefined : {
						color: '#A0A0A0',
						darkColor: '#A0A0A0',
						position: editorCommon.OverviewRulerLane.Center
					})
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
				}
			};
		});

		this.decorations = this.editor.deltaDecorations(this.decorations, decorations);
	}

	public dispose(): void {
		this.removeDecorations();
		super.dispose();
	}
}


CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(SelectHighlightsAction, SelectHighlightsAction.ID, nls.localize('selectAllOccurencesOfFindMatch', "Select All Occurences of Find Match"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_L
736
}, 'Select All Occurences of Find Match'));
737 738 739 740
// register SelectHighlightsAction again to replace the now removed Change All action
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(SelectHighlightsAction, SelectHighlightsAction.COMPAT_ID, nls.localize('changeAll.label', "Change All Occurrences"), {
	context: ContextKey.EditorTextFocus,
	primary: KeyMod.CtrlCmd | KeyCode.F2
741
}, 'Change All Occurrences'));
742 743 744 745

// register actions
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(StartFindAction, FIND_IDS.StartFindAction, nls.localize('startFindAction',"Find"), {
	context: ContextKey.None,
746
	primary: KeyMod.CtrlCmd | KeyCode.KEY_F
747
}, 'Find'));
748 749 750 751
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(NextMatchFindAction, FIND_IDS.NextMatchFindAction, nls.localize('findNextMatchAction', "Find Next"), {
	context: ContextKey.EditorFocus,
	primary: KeyCode.F3,
	mac: { primary: KeyMod.CtrlCmd | KeyCode.KEY_G, secondary: [KeyCode.F3] }
752
}, 'Find Next'));
753 754 755 756
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(PreviousMatchFindAction, FIND_IDS.PreviousMatchFindAction, nls.localize('findPreviousMatchAction', "Find Previous"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.Shift | KeyCode.F3,
	mac: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_G, secondary: [KeyMod.Shift | KeyCode.F3] }
757
}, 'Find Previous'));
758 759 760
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(NextSelectionMatchFindAction, FIND_IDS.NextSelectionMatchFindAction, nls.localize('nextSelectionMatchFindAction', "Find Next Selection"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.CtrlCmd | KeyCode.F3
761
}, 'Find Next Selection'));
762 763 764
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(PreviousSelectionMatchFindAction, FIND_IDS.PreviousSelectionMatchFindAction, nls.localize('previousSelectionMatchFindAction', "Find Previous Selection"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.F3
765
}, 'Find Previous Selection'));
766 767 768 769
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(StartFindReplaceAction, FIND_IDS.StartFindReplaceAction, nls.localize('startReplace', "Replace"), {
	context: ContextKey.None,
	primary: KeyMod.CtrlCmd | KeyCode.KEY_H,
	mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_F }
770
}, 'Replace'));
771 772 773
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(MoveSelectionToNextFindMatchAction, MoveSelectionToNextFindMatchAction.ID, nls.localize('moveSelectionToNextFindMatch', "Move Last Selection To Next Find Match"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_D)
774
}, 'Move Last Selection To Next Find Match'));
一丝 已提交
775 776 777 778 779
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(MoveSelectionToPreviousFindMatchAction, MoveSelectionToPreviousFindMatchAction.ID, nls.localize('moveSelectionToPreviousFindMatch', "Move Last Selection To Previous Find Match"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.chord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_Y)
}, 'Move Last Selection To Previous Find Match'));

780 781 782
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(AddSelectionToNextFindMatchAction, AddSelectionToNextFindMatchAction.ID, nls.localize('addSelectionToNextFindMatch', "Add Selection To Next Find Match"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.CtrlCmd | KeyCode.KEY_D
783
}, 'Add Selection To Next Find Match'));
一丝 已提交
784 785 786 787
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(AddSelectionToPreviousFindMatchAction, AddSelectionToPreviousFindMatchAction.ID, nls.localize('addSelectionToPreviousFindMatch', "Add Selection To Previous Find Match"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.CtrlCmd | KeyCode.KEY_Y
}, 'Add Selection To Previous Find Match'));
788 789 790 791 792 793 794 795

function registerFindCommand(id:string, callback:(controller:CommonFindController)=>void, keybindings:IKeybindings, needsKey:string = null): void {
	CommonEditorRegistry.registerEditorCommand(id, CommonEditorRegistry.commandWeight(5), keybindings, false, needsKey, (ctx, editor, args) => {
		callback(CommonFindController.getFindController(editor));
	});
}

registerFindCommand(FIND_IDS.CloseFindWidgetCommand, x => x.closeFindWidget(), {
796 797
	primary: KeyCode.Escape,
	secondary: [KeyMod.Shift | KeyCode.Escape]
798 799 800 801 802 803 804 805 806 807 808 809 810
}, CONTEXT_FIND_WIDGET_VISIBLE);
registerFindCommand(FIND_IDS.ToggleCaseSensitiveCommand, x => x.toggleCaseSensitive(), {
	primary: KeyMod.Alt | KeyCode.KEY_C,
	mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_C }
});
registerFindCommand(FIND_IDS.ToggleWholeWordCommand, x => x.toggleWholeWords(), {
	primary: KeyMod.Alt | KeyCode.KEY_W,
	mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_W }
});
registerFindCommand(FIND_IDS.ToggleRegexCommand, x => x.toggleRegex(), {
	primary: KeyMod.Alt | KeyCode.KEY_R,
	mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_R }
});
811
registerFindCommand(FIND_IDS.ReplaceOneAction, x => x.replace(), {
I
isidor 已提交
812
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_1
813
}, CONTEXT_FIND_WIDGET_VISIBLE);
814 815
registerFindCommand(FIND_IDS.ReplaceAllAction, x => x.replaceAll(), {
	primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.Enter
816
}, CONTEXT_FIND_WIDGET_VISIBLE);
I
Inori 已提交
817 818
registerFindCommand(FIND_IDS.SelectAllMatchesAction, x => x.selectAllMatches(), {
	primary: KeyMod.Alt | KeyCode.Enter
819
}, CONTEXT_FIND_WIDGET_VISIBLE);