searchActions.ts 11.4 KB
Newer Older
S
Sandeep Somavarapu 已提交
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.
 *--------------------------------------------------------------------------------------------*/

import nls = require('vs/nls');
7
import DOM = require('vs/base/browser/dom');
S
Sandeep Somavarapu 已提交
8 9 10 11 12 13 14
import errors = require('vs/base/common/errors');
import { TPromise } from 'vs/base/common/winjs.base';
import URI from 'vs/base/common/uri';
import { Action } from 'vs/base/common/actions';
import { ToggleViewletAction } from 'vs/workbench/browser/viewlet';
import { IViewletService } from 'vs/workbench/services/viewlet/common/viewletService';
import { ITree } from 'vs/base/parts/tree/browser/tree';
S
Sandeep Somavarapu 已提交
15
import { INavigator } from 'vs/base/common/iterator';
S
Sandeep Somavarapu 已提交
16
import { SearchViewlet } from 'vs/workbench/parts/search/browser/searchViewlet';
17 18
import { SearchResult, Match, FileMatch, FileMatchOrMatch } from 'vs/workbench/parts/search/common/searchModel';
import { IReplaceService } from 'vs/workbench/parts/search/common/replace';
S
Sandeep Somavarapu 已提交
19 20 21 22
import * as Constants from 'vs/workbench/parts/search/common/constants';
import { CollapseAllAction as TreeCollapseAction } from 'vs/base/parts/tree/browser/treeDefaults';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { OpenGlobalSettingsAction } from 'vs/workbench/browser/actions/openSettings';
23
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
24
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
25
import { Keybinding, KeyCode, KeyMod, CommonKeybindings } from 'vs/base/common/keyCodes';
26
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
S
Sandeep Somavarapu 已提交
27
import { FileEditorInput } from 'vs/workbench/parts/files/common/editors/fileEditorInput';
S
Sandeep Somavarapu 已提交
28

S
Sandeep Somavarapu 已提交
29 30 31
export function isSearchViewletFocussed(viewletService: IViewletService): boolean {
	let activeViewlet = viewletService.getActiveViewlet();
	let activeElement = document.activeElement;
32 33 34
	return activeViewlet && activeViewlet.getId() === Constants.VIEWLET_ID && activeElement && DOM.isAncestor(activeElement, (<SearchViewlet>activeViewlet).getContainer().getHTMLElement());
}

S
Sandeep Somavarapu 已提交
35 36 37 38
export function appendKeyBindingLabel(label: string, keyBinding: Keybinding, keyBindingService2: IKeybindingService): string
export function appendKeyBindingLabel(label: string, keyBinding: number, keyBindingService2: IKeybindingService): string
export function appendKeyBindingLabel(label: string, keyBinding: any, keyBindingService2: IKeybindingService): string {
	keyBinding = typeof keyBinding === 'number' ? new Keybinding(keyBinding) : keyBinding;
A
Alex Dima 已提交
39
	return label + ' (' + keyBindingService2.getLabelFor(keyBinding) + ')';
40 41
}

S
Sandeep Somavarapu 已提交
42 43 44 45 46 47 48 49
export class OpenSearchViewletAction extends ToggleViewletAction {

	public static ID = Constants.VIEWLET_ID;
	public static LABEL = nls.localize('showSearchViewlet', "Show Search");

	constructor(id: string, label: string, @IViewletService viewletService: IViewletService, @IWorkbenchEditorService editorService: IWorkbenchEditorService) {
		super(id, label, Constants.VIEWLET_ID, viewletService, editorService);
	}
50

S
Sandeep Somavarapu 已提交
51 52
}

S
Sandeep Somavarapu 已提交
53 54 55 56 57 58 59 60 61 62 63
export class ReplaceInFilesAction extends Action {

	public static ID = 'workbench.action.replaceInFiles';
	public static LABEL = nls.localize('replaceInFiles', "Replace in Files");

	constructor(id: string, label: string, @IViewletService private viewletService: IViewletService) {
		super(id, label);
	}

	public run(): TPromise<any> {
		return this.viewletService.openViewlet(Constants.VIEWLET_ID, true).then((viewlet) => {
S
Sandeep Somavarapu 已提交
64
			let searchAndReplaceWidget = (<SearchViewlet>viewlet).searchAndReplaceWidget;
65 66
			searchAndReplaceWidget.toggleReplace(true);
			searchAndReplaceWidget.focus(false, true);
S
Sandeep Somavarapu 已提交
67 68 69 70
		});
	}
}

S
Sandeep Somavarapu 已提交
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
export class FindInFolderAction extends Action {

	private resource: URI;

	constructor(resource: URI, @IViewletService private viewletService: IViewletService) {
		super('workbench.search.action.findInFolder', nls.localize('findInFolder', "Find in Folder"));

		this.resource = resource;
	}

	public run(event?: any): TPromise<any> {
		return this.viewletService.openViewlet(Constants.VIEWLET_ID, true).then((viewlet: SearchViewlet) => {
			viewlet.searchInFolder(this.resource);
		});
	}
}

export class RefreshAction extends Action {

	constructor(private viewlet: SearchViewlet) {
		super('refresh');

		this.label = nls.localize('RefreshAction.label', "Refresh");
		this.enabled = false;
		this.class = 'search-action refresh';
	}

	public run(): TPromise<void> {
		this.viewlet.onQueryChanged(true);

		return TPromise.as(null);
	}
}

export class CollapseAllAction extends TreeCollapseAction {

	constructor(viewlet: SearchViewlet) {
		super(viewlet.getControl(), false);
		this.class = 'search-action collapse';
	}
}

export class ClearSearchResultsAction extends Action {

	constructor(private viewlet: SearchViewlet) {
		super('clearSearchResults');

		this.label = nls.localize('ClearSearchResultsAction.label', "Clear Search Results");
		this.enabled = false;
		this.class = 'search-action clear-search-results';
	}

	public run(): TPromise<void> {
		this.viewlet.clearSearchResults();

		return TPromise.as(null);
	}
}

S
Sandeep Somavarapu 已提交
130 131
export abstract class AbstractSearchAndReplaceAction extends Action {

S
Sandeep Somavarapu 已提交
132 133 134 135 136 137 138 139 140 141
	/**
	 * Returns element to focus after removing the given element
	 */
	protected getElementToFocusAfterRemoved(viewer: ITree, elementToBeRemoved: FileMatchOrMatch): FileMatchOrMatch {
		let elementToFocus = this.getNextElementAfterRemoved(viewer, elementToBeRemoved);
		if (!elementToFocus) {
			elementToFocus = this.getPreviousElementAfterRemoved(viewer, elementToBeRemoved);
		}
		return elementToFocus;
	}
S
Sandeep Somavarapu 已提交
142

S
Sandeep Somavarapu 已提交
143 144 145 146 147 148 149 150 151 152
	protected getNextElementAfterRemoved(viewer: ITree, element: FileMatchOrMatch): FileMatchOrMatch {
		let navigator: INavigator<any> = this.getNavigatorAt(element, viewer);
		if (element instanceof FileMatch) {
			// If file match is removed then next element is the next file match
			while (!!navigator.next() && !(navigator.current() instanceof FileMatch)) { };
		} else {
			navigator.next();
		}
		return navigator.current();
	}
S
Sandeep Somavarapu 已提交
153

S
Sandeep Somavarapu 已提交
154 155 156 157 158 159 160
	protected getPreviousElementAfterRemoved(viewer: ITree, element: FileMatchOrMatch): FileMatchOrMatch {
		let navigator: INavigator<any> = this.getNavigatorAt(element, viewer);
		let previousElement = navigator.previous();
		if (element instanceof Match && element.parent().matches().length === 1) {
			// If this is the only match, then the file match is also removed
			// Hence take the previous element to file match
			previousElement = navigator.previous();
S
Sandeep Somavarapu 已提交
161
		}
S
Sandeep Somavarapu 已提交
162 163 164 165 166 167 168
		return previousElement;
	}

	private getNavigatorAt(element: FileMatchOrMatch, viewer: ITree): INavigator<any> {
		let navigator:INavigator<any> = viewer.getNavigator();
		while (navigator.current() !== element && !!navigator.next()) { };
		return navigator;
S
Sandeep Somavarapu 已提交
169 170 171 172
	}
}

export class RemoveAction extends AbstractSearchAndReplaceAction {
S
Sandeep Somavarapu 已提交
173

174 175
	constructor(private viewer: ITree, private element: FileMatchOrMatch) {
		super('remove', nls.localize('RemoveAction.label', "Remove"), 'action-remove');
S
Sandeep Somavarapu 已提交
176 177
	}

178
	public run(): TPromise<any> {
S
Sandeep Somavarapu 已提交
179
		let nextFocusElement = this.getElementToFocusAfterRemoved(this.viewer, this.element);
S
Sandeep Somavarapu 已提交
180 181
		if (nextFocusElement) {
			this.viewer.setFocus(nextFocusElement);
182 183
		}

184
		let elementToRefresh: any;
185
		if (this.element instanceof FileMatch) {
S
Sandeep Somavarapu 已提交
186
			let parent: SearchResult = <SearchResult>this.element.parent();
187
			parent.remove(<FileMatch>this.element);
S
Sandeep Somavarapu 已提交
188
			elementToRefresh = parent;
S
Sandeep Somavarapu 已提交
189
		} else {
S
Sandeep Somavarapu 已提交
190
			let parent: FileMatch = <FileMatch>this.element.parent();
191
			parent.remove(<Match>this.element);
S
Sandeep Somavarapu 已提交
192
			elementToRefresh = parent.count() === 0 ? parent.parent() : parent;
S
Sandeep Somavarapu 已提交
193
		}
194

S
Sandeep Somavarapu 已提交
195
		this.viewer.DOMFocus();
196 197 198
		return this.viewer.refresh(elementToRefresh);
	}

199
}
S
Sandeep Somavarapu 已提交
200

S
Sandeep Somavarapu 已提交
201
export class ReplaceAllAction extends AbstractSearchAndReplaceAction {
S
Sandeep Somavarapu 已提交
202

203 204 205 206
	public static get KEY_BINDING(): number {
		return KeyMod.Shift | CommonKeybindings.CTRLCMD_ENTER;
	}

207
	constructor(private viewer: ITree, private fileMatch: FileMatch, private viewlet: SearchViewlet,
S
Sandeep Somavarapu 已提交
208 209 210
		@IReplaceService private replaceService: IReplaceService,
		@IKeybindingService keyBindingService2: IKeybindingService,
		@ITelemetryService private telemetryService: ITelemetryService) {
A
Alex Dima 已提交
211
		super('file-action-replace-all', appendKeyBindingLabel(nls.localize('file.replaceAll.label', "Replace All"), ReplaceAllAction.KEY_BINDING, keyBindingService2), 'action-replace-all');
S
Sandeep Somavarapu 已提交
212 213
	}

214
	public run(): TPromise<any> {
215
		this.telemetryService.publicLog('replaceAll.action.selected');
S
Sandeep Somavarapu 已提交
216
		let nextFocusElement = this.getElementToFocusAfterRemoved(this.viewer, this.fileMatch);
S
Sandeep Somavarapu 已提交
217
		return this.fileMatch.parent().replace(this.fileMatch).then(() => {
S
Sandeep Somavarapu 已提交
218 219 220 221 222
			if (nextFocusElement) {
				this.viewer.setFocus(nextFocusElement);
			}
			this.viewer.DOMFocus();
			this.viewlet.open(this.fileMatch, true);
S
Sandeep Somavarapu 已提交
223 224 225 226
		});
	}
}

S
Sandeep Somavarapu 已提交
227
export class ReplaceAction extends AbstractSearchAndReplaceAction {
S
Sandeep Somavarapu 已提交
228

229 230 231 232
	public static get KEY_BINDING(): number {
		return KeyMod.Shift | KeyMod.CtrlCmd | KeyCode.KEY_1;
	}

233
	constructor(private viewer: ITree, private element: Match, private viewlet: SearchViewlet,
S
Sandeep Somavarapu 已提交
234 235 236 237
		@IReplaceService private replaceService: IReplaceService,
		@IKeybindingService keyBindingService2: IKeybindingService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@ITelemetryService private telemetryService: ITelemetryService) {
A
Alex Dima 已提交
238
		super('action-replace', appendKeyBindingLabel(nls.localize('match.replace.label', "Replace"), ReplaceAction.KEY_BINDING, keyBindingService2), 'action-replace');
S
Sandeep Somavarapu 已提交
239 240 241
	}

	public run(): TPromise<any> {
242
		this.telemetryService.publicLog('replace.action.selected');
S
Sandeep Somavarapu 已提交
243 244
		let elementToFocus = this.getElementToFocusAfterRemoved(this.viewer, this.element);
		let elementToShowReplacePreview = this.getElementToShowReplacePreview(elementToFocus);
S
Sandeep Somavarapu 已提交
245

S
Sandeep Somavarapu 已提交
246
		return this.element.parent().replace(this.element).then(() => {
S
Sandeep Somavarapu 已提交
247 248
			if (elementToFocus) {
				this.viewer.setFocus(elementToFocus);
S
Sandeep Somavarapu 已提交
249 250
			}
			this.viewer.DOMFocus();
S
Sandeep Somavarapu 已提交
251 252
			if (!elementToShowReplacePreview || this.hasToOpenFile()) {
				this.viewlet.open(this.element, true);
S
Sandeep Somavarapu 已提交
253
			} else {
S
Sandeep Somavarapu 已提交
254
				this.replaceService.openReplacePreviewEditor(elementToShowReplacePreview, true);
S
Sandeep Somavarapu 已提交
255
			}
256
		});
S
Sandeep Somavarapu 已提交
257
	}
S
Sandeep Somavarapu 已提交
258

S
Sandeep Somavarapu 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	private getElementToShowReplacePreview(elementToFocus: FileMatchOrMatch): Match {
		if (this.hasSameParent(elementToFocus)) {
			return <Match>elementToFocus;
		}
		let previousElement = this.getPreviousElementAfterRemoved(this.viewer, this.element);
		if (this.hasSameParent(previousElement)) {
			return <Match>previousElement;
		}
		return null;
	}

	private hasSameParent(element: FileMatchOrMatch): boolean {
		return element && element instanceof Match && element.parent().resource() === this.element.parent().resource();
	}

	private hasToOpenFile(): boolean {
S
Sandeep Somavarapu 已提交
275 276
		let activeInput = this.editorService.getActiveEditorInput();
		if (activeInput instanceof FileEditorInput) {
S
Sandeep Somavarapu 已提交
277
			return activeInput.getResource().fsPath === this.element.parent().resource().fsPath;
S
Sandeep Somavarapu 已提交
278 279 280
		}
		return false;
	}
S
Sandeep Somavarapu 已提交
281 282 283 284
}

export class ConfigureGlobalExclusionsAction extends Action {

S
Sandeep Somavarapu 已提交
285
	constructor( @IInstantiationService private instantiationService: IInstantiationService) {
S
Sandeep Somavarapu 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299
		super('configureGlobalExclusionsAction');

		this.label = nls.localize('ConfigureGlobalExclusionsAction.label', "Open Settings");
		this.enabled = true;
		this.class = 'search-configure-exclusions';
	}

	public run(): TPromise<void> {
		let action = this.instantiationService.createInstance(OpenGlobalSettingsAction, OpenGlobalSettingsAction.ID, OpenGlobalSettingsAction.LABEL);
		action.run().done(() => action.dispose(), errors.onUnexpectedError);

		return TPromise.as(null);
	}
}