findController.ts 23.7 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 {OccurrencesRegistry} 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 37 38 39

export enum FindStartFocusAction {
	NoFocusChange,
	FocusFindInput,
	FocusReplaceInput
}

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

const CONTEXT_FIND_WIDGET_VISIBLE = 'findWidgetVisible';

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

	static ID = 'editor.contrib.findController';

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

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

A
Alex Dima 已提交
53
	constructor(editor:editorCommon.ICommonCodeEditor, @IKeybindingService keybindingService: IKeybindingService) {
54 55 56 57 58 59 60 61 62
		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 已提交
63
		this._register(this._editor.addListener2(editorCommon.EventType.ModelChanged, () => {
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
			let shouldRestartFind = (this._editor.getModel() && this._state.isRevealed);

			this.disposeModel();

			if (shouldRestartFind) {
				this._start({
					forceRevealReplace: false,
					seedSearchStringFromSelection: false,
					seedSearchScopeFromSelection: false,
					shouldFocus: FindStartFocusAction.NoFocusChange,
					shouldAnimate: false
				});
			}
		}));
	}

	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);
	}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	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;
	}

149 150 151 152 153 154 155 156 157 158 159 160 161 162
	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) {
163 164
			let selectionSearchString = this.getSelectionSearchString();
			if (selectionSearchString) {
165 166 167 168 169
				if (this._state.isRegex) {
					stateChanges.searchString = strings.escapeRegExpCharacters(selectionSearchString);
				} else {
					stateChanges.searchString = selectionSearchString;
				}
170 171 172
			}
		}

173 174
		let selection = this._editor.getSelection();

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		stateChanges.searchScope = null;
		if (opts.seedSearchScopeFromSelection && selection.startLineNumber < selection.endLineNumber) {
			// Take search scope into account only if it is more than one line.
			stateChanges.searchScope = selection;
		}

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

		this._state.change(stateChanges, false);

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

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

	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 已提交
228 229 230

	public selectAllMatches(): boolean {
		if (this._model) {
I
Inori 已提交
231
			this._model.selectAllMatches();
232
			this._editor.focus();
I
Inori 已提交
233 234 235 236
			return true;
		}
		return false;
	}
237 238 239 240
}

export class StartFindAction extends EditorAction {

241
	constructor(descriptor: editorCommon.IEditorActionDescriptorData, editor: editorCommon.ICommonCodeEditor) {
242 243 244 245 246
		super(descriptor, editor, Behaviour.WidgetFocus);
	}

	public run(): TPromise<boolean> {
		let controller = CommonFindController.getFindController(this.editor);
247 248 249 250 251 252 253
		controller.start({
			forceRevealReplace: false,
			seedSearchStringFromSelection: true,
			seedSearchScopeFromSelection: true,
			shouldFocus: FindStartFocusAction.FocusFindInput,
			shouldAnimate: true
		});
254 255 256 257
		return TPromise.as(true);
	}
}

B
Benjamin Pasero 已提交
258
export abstract class MatchFindAction extends EditorAction {
259
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
260 261 262 263 264
		super(descriptor, editor, Behaviour.WidgetFocus);
	}

	public run(): TPromise<boolean> {
		let controller = CommonFindController.getFindController(this.editor);
265
		if (!this._run(controller)) {
266 267 268 269 270 271 272
			controller.start({
				forceRevealReplace: false,
				seedSearchStringFromSelection: (controller.getState().searchString.length === 0),
				seedSearchScopeFromSelection: false,
				shouldFocus: FindStartFocusAction.NoFocusChange,
				shouldAnimate: true
			});
273
			this._run(controller);
274 275 276
		}
		return TPromise.as(true);
	}
277 278

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

281 282 283 284 285
export class NextMatchFindAction extends MatchFindAction {
	protected _run(controller:CommonFindController): boolean {
		return controller.moveToNextMatch();
	}
}
286

287 288 289 290 291 292
export class PreviousMatchFindAction extends MatchFindAction {
	protected _run(controller:CommonFindController): boolean {
		return controller.moveToPrevMatch();
	}
}

B
Benjamin Pasero 已提交
293
export abstract class SelectionMatchFindAction extends EditorAction {
294
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
295 296 297 298 299
		super(descriptor, editor, Behaviour.WidgetFocus);
	}

	public run(): TPromise<boolean> {
		let controller = CommonFindController.getFindController(this.editor);
300 301 302 303 304
		let selectionSearchString = controller.getSelectionSearchString();
		if (selectionSearchString) {
			controller.setSearchString(selectionSearchString);
		}
		if (!this._run(controller)) {
305 306
			controller.start({
				forceRevealReplace: false,
307
				seedSearchStringFromSelection: false,
308 309 310 311
				seedSearchScopeFromSelection: false,
				shouldFocus: FindStartFocusAction.NoFocusChange,
				shouldAnimate: true
			});
312
			this._run(controller);
313 314 315
		}
		return TPromise.as(true);
	}
316 317 318 319 320 321 322 323 324 325 326 327 328 329

	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();
	}
330 331 332 333
}

export class StartFindReplaceAction extends EditorAction {

334
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
335 336 337 338 339
		super(descriptor, editor, Behaviour.WidgetFocus | Behaviour.Writeable);
	}

	public run(): TPromise<boolean> {
		let controller = CommonFindController.getFindController(this.editor);
340 341
		controller.start({
			forceRevealReplace: true,
342
			seedSearchStringFromSelection: true,
343 344 345 346
			seedSearchScopeFromSelection: true,
			shouldFocus: FindStartFocusAction.FocusReplaceInput,
			shouldAnimate: true
		});
347 348 349 350 351 352 353 354 355
		return TPromise.as(true);
	}
}

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

A
Alex Dima 已提交
356
	nextMatch: editorCommon.IEditorSelection;
357 358
}

A
Alex Dima 已提交
359
function multiCursorFind(editor:editorCommon.ICommonCodeEditor, changeFindSearchString:boolean): IMultiCursorFindResult {
360 361 362
	let controller = CommonFindController.getFindController(editor);
	let state = controller.getState();
	let searchText: string,
A
Alex Dima 已提交
363
		nextMatch: editorCommon.IEditorSelection;
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

	// 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;
			nextMatch = Selection.createSelection(s.startLineNumber, word.startColumn, s.startLineNumber, word.endColumn);
		} else {
			searchText = editor.getModel().getValueInRange(s);
		}
		if (changeFindSearchString) {
			controller.setSearchString(searchText);
		}
	}

	return {
		searchText: searchText,
		matchCase: matchCase,
		wholeWord: wholeWord,
		nextMatch: nextMatch
	};
}

409
export class SelectNextFindMatchAction extends EditorAction {
410
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
411 412 413
		super(descriptor, editor, Behaviour.WidgetFocus);
	}

A
Alex Dima 已提交
414
	protected _getNextMatch(): editorCommon.IEditorSelection {
415 416 417 418 419 420 421 422 423 424 425
		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];

426
		let nextMatch = this.editor.getModel().findNextMatch(r.searchText, lastAddedSelection.getEndPosition(), false, r.matchCase, r.wholeWord);
427 428 429 430 431 432 433 434 435

		if (!nextMatch) {
			return null;
		}

		return Selection.createSelection(nextMatch.startLineNumber, nextMatch.startColumn, nextMatch.endLineNumber, nextMatch.endColumn);
	}
}

436
export class AddSelectionToNextFindMatchAction extends SelectNextFindMatchAction {
437
	static ID = FIND_IDS.AddSelectionToNextFindMatchAction;
438

439 440
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
		super(descriptor, editor);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	}

	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);
	}
}

458
export class MoveSelectionToNextFindMatchAction extends SelectNextFindMatchAction {
459
	static ID = FIND_IDS.MoveSelectionToNextFindMatchAction;
460

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

	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);
	}
}

480
export class SelectHighlightsAction extends EditorAction {
481 482 483
	static ID = 'editor.action.selectHighlights';
	static COMPAT_ID = 'editor.action.changeAll';

484
	constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
485
		let behaviour = Behaviour.WidgetFocus | Behaviour.Writeable;
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
		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);
		}

502
		let matches = this.editor.getModel().findMatches(r.searchText, true, false, r.matchCase, r.wholeWord);
503 504 505 506 507 508 509 510

		if (matches.length > 0) {
			this.editor.setSelections(matches.map(m => Selection.createSelection(m.startLineNumber, m.startColumn, m.endLineNumber, m.endColumn)));
		}
		return TPromise.as(true);
	}
}

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

A
Alex Dima 已提交
514
	private editor: editorCommon.ICommonCodeEditor;
515
	private decorations: string[];
516 517
	private updateSoon: RunOnceScheduler;
	private lastWordUnderCursor: editorCommon.IEditorRange;
518

519
	constructor(editor:editorCommon.ICommonCodeEditor) {
520 521 522
		super();
		this.editor = editor;
		this.decorations = [];
523 524
		this.updateSoon = this._register(new RunOnceScheduler(() => this._update(), 300));
		this.lastWordUnderCursor = null;
525

526 527
		this._register(editor.addListener2(editorCommon.EventType.CursorSelectionChanged, (e: editorCommon.ICursorSelectionChangedEvent) => {
			if (e.selection.isEmpty()) {
A
Alex Dima 已提交
528
				if (e.reason === editorCommon.CursorChangeReason.Explicit) {
529 530 531 532 533
					if (!this.lastWordUnderCursor || !this.lastWordUnderCursor.containsPosition(e.selection.getStartPosition())) {
						// no longer valid
						this.removeDecorations();
					}
					this.updateSoon.schedule();
534 535 536 537 538 539 540 541
				} else {
					this.removeDecorations();

				}
			} else {
				this._update();
			}
		}));
A
Alex Dima 已提交
542
		this._register(editor.addListener2(editorCommon.EventType.ModelChanged, (e) => {
543 544
			this.removeDecorations();
		}));
545 546 547
		this._register(CommonFindController.getFindController(editor).getState().addChangeListener((e) => {
			this._update();
		}));
548 549 550 551 552 553 554
	}

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

	private removeDecorations(): void {
555
		this.lastWordUnderCursor = null;
556 557 558 559 560 561
		if (this.decorations.length > 0) {
			this.decorations = this.editor.deltaDecorations(this.decorations, []);
		}
	}

	private _update(): void {
562 563 564 565 566
		let model = this.editor.getModel();
		if (!model) {
			return;
		}

567
		this.lastWordUnderCursor = null;
A
Alex Dima 已提交
568
		if (!this.editor.getConfiguration().contribInfo.selectionHighlight) {
569 570 571 572 573 574 575 576 577
			return;
		}

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

578
		let hasFindOccurences = OccurrencesRegistry.has(model);
579 580
		if (r.nextMatch) {
			// This is an empty selection
581
			if (hasFindOccurences) {
582 583 584 585
				// Do not interfere with semantic word highlighting in the no selection case
				this.removeDecorations();
				return;
			}
586 587

			this.lastWordUnderCursor = r.nextMatch;
588 589 590 591 592 593 594 595 596 597 598
		}
		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 已提交
599 600 601 602 603 604 605 606 607 608 609
		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;
			}
		}

610

611
		let allMatches = model.findMatches(r.searchText, true, false, r.matchCase, r.wholeWord);
612 613 614 615 616
		allMatches.sort(Range.compareRangesUsingStarts);

		selections.sort(Range.compareRangesUsingStarts);

		// do not overlap with selection (issue #64 and #512)
A
Alex Dima 已提交
617
		let matches: editorCommon.IEditorRange[] = [];
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
		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 已提交
646
					stickiness: editorCommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,
647 648 649 650 651 652 653
					className: 'selectionHighlight',
					// Show in overviewRuler only if model has no semantic highlighting
					overviewRuler: (hasFindOccurences ? undefined : {
						color: '#A0A0A0',
						darkColor: '#A0A0A0',
						position: editorCommon.OverviewRulerLane.Center
					})
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
				}
			};
		});

		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
671
}, 'Select All Occurences of Find Match'));
672 673 674 675
// 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
676
}, 'Change All Occurrences'));
677 678 679 680

// register actions
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(StartFindAction, FIND_IDS.StartFindAction, nls.localize('startFindAction',"Find"), {
	context: ContextKey.None,
681
	primary: KeyMod.CtrlCmd | KeyCode.KEY_F
682
}, 'Find'));
683 684 685 686
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] }
687
}, 'Find Next'));
688 689 690 691
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] }
692
}, 'Find Previous'));
693 694 695
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(NextSelectionMatchFindAction, FIND_IDS.NextSelectionMatchFindAction, nls.localize('nextSelectionMatchFindAction', "Find Next Selection"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.CtrlCmd | KeyCode.F3
696
}, 'Find Next Selection'));
697 698 699
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(PreviousSelectionMatchFindAction, FIND_IDS.PreviousSelectionMatchFindAction, nls.localize('previousSelectionMatchFindAction', "Find Previous Selection"), {
	context: ContextKey.EditorFocus,
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.F3
700
}, 'Find Previous Selection'));
701 702 703 704
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 }
705
}, 'Replace'));
706 707 708
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)
709
}, 'Move Last Selection To Next Find Match'));
710 711 712
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
713
}, 'Add Selection To Next Find Match'));
714 715 716 717 718 719 720 721

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(), {
722 723
	primary: KeyCode.Escape,
	secondary: [KeyMod.Shift | KeyCode.Escape]
724 725 726 727 728 729 730 731 732 733 734 735 736
}, 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 }
});
737
registerFindCommand(FIND_IDS.ReplaceOneAction, x => x.replace(), {
I
isidor 已提交
738
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_1
739 740 741 742
});
registerFindCommand(FIND_IDS.ReplaceAllAction, x => x.replaceAll(), {
	primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.Enter
});
I
Inori 已提交
743 744 745
registerFindCommand(FIND_IDS.SelectAllMatchesAction, x => x.selectAllMatches(), {
	primary: KeyMod.Alt | KeyCode.Enter
});