searchActions.ts 23.7 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
import errors = require('vs/base/common/errors');
9
import paths = require('vs/base/common/paths');
S
Sandeep Somavarapu 已提交
10 11 12 13
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';
B
Benjamin Pasero 已提交
14
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
S
Sandeep Somavarapu 已提交
15
import { ITree } from 'vs/base/parts/tree/browser/tree';
S
Sandeep Somavarapu 已提交
16
import { INavigator } from 'vs/base/common/iterator';
S
Sandeep Somavarapu 已提交
17
import { SearchViewlet } from 'vs/workbench/parts/search/browser/searchViewlet';
18
import { Match, FileMatch, FileMatchOrMatch, FolderMatch, RenderableMatch } from 'vs/workbench/parts/search/common/searchModel';
19
import { IReplaceService } from 'vs/workbench/parts/search/common/replace';
S
Sandeep Somavarapu 已提交
20 21
import * as Constants from 'vs/workbench/parts/search/common/constants';
import { CollapseAllAction as TreeCollapseAction } from 'vs/base/parts/tree/browser/treeDefaults';
22
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
23
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
S
Sandeep Somavarapu 已提交
24
import { ResolvedKeybinding, createKeybinding } from 'vs/base/common/keyCodes';
25
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
26
import { toResource } from 'vs/workbench/common/editor';
27 28 29
import { ServicesAccessor, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IListService } from 'vs/platform/list/browser/listService';
import { explorerItemToFileResource } from 'vs/workbench/parts/files/common/files';
A
Alex Dima 已提交
30
import { OS } from 'vs/base/common/platform';
B
Benjamin Pasero 已提交
31
import { IContextKeyService, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
S
Sandeep Somavarapu 已提交
32

33
export function isSearchViewletFocused(viewletService: IViewletService): boolean {
S
Sandeep Somavarapu 已提交
34 35
	let activeViewlet = viewletService.getActiveViewlet();
	let activeElement = document.activeElement;
36 37 38
	return activeViewlet && activeViewlet.getId() === Constants.VIEWLET_ID && activeElement && DOM.isAncestor(activeElement, (<SearchViewlet>activeViewlet).getContainer().getHTMLElement());
}

39
export function appendKeyBindingLabel(label: string, keyBinding: number | ResolvedKeybinding, keyBindingService2: IKeybindingService): string {
A
Alex Dima 已提交
40
	if (typeof keyBinding === 'number') {
41 42
		const resolvedKeybindings = keyBindingService2.resolveKeybinding(createKeybinding(keyBinding, OS));
		return doAppendKeyBindingLabel(label, resolvedKeybindings.length > 0 ? resolvedKeybindings[0] : null);
A
Alex Dima 已提交
43
	} else {
44
		return doAppendKeyBindingLabel(label, keyBinding);
A
Alex Dima 已提交
45
	}
46 47 48 49
}

function doAppendKeyBindingLabel(label: string, keyBinding: ResolvedKeybinding): string {
	return keyBinding ? label + ' (' + keyBinding.getLabel() + ')' : label;
50 51
}

S
Sandeep Somavarapu 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
export class ToggleCaseSensitiveAction extends Action {

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

	public run(): TPromise<any> {
		let searchViewlet = <SearchViewlet>this.viewletService.getActiveViewlet();
		searchViewlet.toggleCaseSensitive();
		return TPromise.as(null);
	}
}

export class ToggleWholeWordAction extends Action {

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

	public run(): TPromise<any> {
		let searchViewlet = <SearchViewlet>this.viewletService.getActiveViewlet();
		searchViewlet.toggleWholeWords();
		return TPromise.as(null);
	}
}

export class ToggleRegexAction extends Action {

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

	public run(): TPromise<any> {
		let searchViewlet = <SearchViewlet>this.viewletService.getActiveViewlet();
		searchViewlet.toggleRegex();
		return TPromise.as(null);
	}
}

91 92 93 94
export class ShowNextSearchIncludeAction extends Action {

	public static ID = 'search.history.showNextIncludePattern';
	public static LABEL = nls.localize('nextSearchIncludePattern', "Show Next Search Include Pattern");
95
	public static CONTEXT_KEY_EXPRESSION: ContextKeyExpr = ContextKeyExpr.and(Constants.SearchViewletVisibleKey, Constants.PatternIncludesFocusedKey);
96

S
Sandeep Somavarapu 已提交
97 98 99 100
	constructor(id: string, label: string,
		@IViewletService private viewletService: IViewletService,
		@IContextKeyService private contextKeyService: IContextKeyService
	) {
101
		super(id, label);
S
Sandeep Somavarapu 已提交
102
		this.enabled = this.contextKeyService.contextMatchesRules(ShowNextSearchIncludeAction.CONTEXT_KEY_EXPRESSION);
103 104 105 106 107 108 109 110 111 112 113 114 115
	}

	public run(): TPromise<any> {
		let searchAndReplaceWidget = (<SearchViewlet>this.viewletService.getActiveViewlet()).searchIncludePattern;
		searchAndReplaceWidget.showNextTerm();
		return TPromise.as(null);
	}
}

export class ShowPreviousSearchIncludeAction extends Action {

	public static ID = 'search.history.showPreviousIncludePattern';
	public static LABEL = nls.localize('previousSearchIncludePattern', "Show Previous Search Include Pattern");
116
	public static CONTEXT_KEY_EXPRESSION: ContextKeyExpr = ContextKeyExpr.and(Constants.SearchViewletVisibleKey, Constants.PatternIncludesFocusedKey);
117

S
Sandeep Somavarapu 已提交
118 119 120 121
	constructor(id: string, label: string,
		@IViewletService private viewletService: IViewletService,
		@IContextKeyService private contextKeyService: IContextKeyService
	) {
122
		super(id, label);
S
Sandeep Somavarapu 已提交
123
		this.enabled = this.contextKeyService.contextMatchesRules(ShowPreviousSearchIncludeAction.CONTEXT_KEY_EXPRESSION);
124 125 126 127 128 129 130 131 132 133 134 135 136
	}

	public run(): TPromise<any> {
		let searchAndReplaceWidget = (<SearchViewlet>this.viewletService.getActiveViewlet()).searchIncludePattern;
		searchAndReplaceWidget.showPreviousTerm();
		return TPromise.as(null);
	}
}

export class ShowNextSearchExcludeAction extends Action {

	public static ID = 'search.history.showNextExcludePattern';
	public static LABEL = nls.localize('nextSearchExcludePattern', "Show Next Search Exclude Pattern");
137
	public static CONTEXT_KEY_EXPRESSION: ContextKeyExpr = ContextKeyExpr.and(Constants.SearchViewletVisibleKey, Constants.PatternExcludesFocusedKey);
138

S
Sandeep Somavarapu 已提交
139 140 141 142
	constructor(id: string, label: string,
		@IViewletService private viewletService: IViewletService,
		@IContextKeyService private contextKeyService: IContextKeyService
	) {
143
		super(id, label);
S
Sandeep Somavarapu 已提交
144
		this.enabled = this.contextKeyService.contextMatchesRules(ShowNextSearchExcludeAction.CONTEXT_KEY_EXPRESSION);
145 146 147 148 149 150 151 152 153 154 155 156
	}
	public run(): TPromise<any> {
		let searchAndReplaceWidget = (<SearchViewlet>this.viewletService.getActiveViewlet()).searchExcludePattern;
		searchAndReplaceWidget.showNextTerm();
		return TPromise.as(null);
	}
}

export class ShowPreviousSearchExcludeAction extends Action {

	public static ID = 'search.history.showPreviousExcludePattern';
	public static LABEL = nls.localize('previousSearchExcludePattern', "Show Previous Search Exclude Pattern");
157
	public static CONTEXT_KEY_EXPRESSION: ContextKeyExpr = ContextKeyExpr.and(Constants.SearchViewletVisibleKey, Constants.PatternExcludesFocusedKey);
158

S
Sandeep Somavarapu 已提交
159 160 161 162
	constructor(id: string, label: string,
		@IViewletService private viewletService: IViewletService,
		@IContextKeyService private contextKeyService: IContextKeyService
	) {
163
		super(id, label);
S
Sandeep Somavarapu 已提交
164
		this.enabled = this.contextKeyService.contextMatchesRules(ShowPreviousSearchExcludeAction.CONTEXT_KEY_EXPRESSION);
165 166 167 168 169 170 171 172 173
	}

	public run(): TPromise<any> {
		let searchAndReplaceWidget = (<SearchViewlet>this.viewletService.getActiveViewlet()).searchExcludePattern;
		searchAndReplaceWidget.showPreviousTerm();
		return TPromise.as(null);
	}
}

174 175
export class ShowNextSearchTermAction extends Action {

176
	public static ID = 'search.history.showNext';
177
	public static LABEL = nls.localize('nextSearchTerm', "Show Next Search Term");
178
	public static CONTEXT_KEY_EXPRESSION: ContextKeyExpr = ContextKeyExpr.and(Constants.SearchViewletVisibleKey, Constants.SearchInputBoxFocusedKey);
179

S
Sandeep Somavarapu 已提交
180 181 182 183
	constructor(id: string, label: string,
		@IViewletService private viewletService: IViewletService,
		@IContextKeyService private contextKeyService: IContextKeyService
	) {
184
		super(id, label);
S
Sandeep Somavarapu 已提交
185 186
		this.enabled = this.contextKeyService.contextMatchesRules(ShowNextSearchTermAction.CONTEXT_KEY_EXPRESSION);

187 188 189 190 191 192 193 194 195 196 197
	}

	public run(): TPromise<any> {
		let searchAndReplaceWidget = (<SearchViewlet>this.viewletService.getActiveViewlet()).searchAndReplaceWidget;
		searchAndReplaceWidget.showNextSearchTerm();
		return TPromise.as(null);
	}
}

export class ShowPreviousSearchTermAction extends Action {

198
	public static ID = 'search.history.showPrevious';
199
	public static LABEL = nls.localize('previousSearchTerm', "Show Previous Search Term");
200
	public static CONTEXT_KEY_EXPRESSION: ContextKeyExpr = ContextKeyExpr.and(Constants.SearchViewletVisibleKey, Constants.SearchInputBoxFocusedKey);
201

S
Sandeep Somavarapu 已提交
202 203 204 205
	constructor(id: string, label: string,
		@IViewletService private viewletService: IViewletService,
		@IContextKeyService private contextKeyService: IContextKeyService
	) {
206
		super(id, label);
S
Sandeep Somavarapu 已提交
207
		this.enabled = this.contextKeyService.contextMatchesRules(ShowPreviousSearchTermAction.CONTEXT_KEY_EXPRESSION);
208 209 210 211 212 213 214 215 216
	}

	public run(): TPromise<any> {
		let searchAndReplaceWidget = (<SearchViewlet>this.viewletService.getActiveViewlet()).searchAndReplaceWidget;
		searchAndReplaceWidget.showPreviousSearchTerm();
		return TPromise.as(null);
	}
}

217 218 219
export class FocusNextInputAction extends Action {

	public static ID = 'search.focus.nextInputBox';
220
	public static LABEL = nls.localize('focusNextInputBox', "Focus Next Input Box");
221 222 223 224 225 226 227 228 229 230 231 232 233

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

	public run(): TPromise<any> {
		(<SearchViewlet>this.viewletService.getActiveViewlet()).focusNextInputBox();
		return TPromise.as(null);
	}
}

export class FocusPreviousInputAction extends Action {

S
Sandeep Somavarapu 已提交
234
	public static ID = 'search.focus.previousInputBox';
235
	public static LABEL = nls.localize('focusPreviousInputBox', "Focus Previous Input Box");
236 237 238 239 240 241 242 243 244 245 246

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

	public run(): TPromise<any> {
		(<SearchViewlet>this.viewletService.getActiveViewlet()).focusPreviousInputBox();
		return TPromise.as(null);
	}
}

S
Sandeep Somavarapu 已提交
247 248 249 250 251
export class OpenSearchViewletAction extends ToggleViewletAction {

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

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	public run(): TPromise<any> {
		const activeViewlet = this.viewletService.getActiveViewlet();
		const searchViewletWasOpen = activeViewlet && activeViewlet.getId() === Constants.VIEWLET_ID;

		return super.run().then(() => {
			if (!searchViewletWasOpen) {
				// Get the search viewlet and ensure that 'replace' is collapsed
				const searchViewlet = this.viewletService.getActiveViewlet();
				if (searchViewlet && searchViewlet.getId() === Constants.VIEWLET_ID) {
					const searchAndReplaceWidget = (<SearchViewlet>searchViewlet).searchAndReplaceWidget;
					searchAndReplaceWidget.toggleReplace(false);
				}
			}
		});
	}

S
Sandeep Somavarapu 已提交
269 270
}

S
Sandeep Somavarapu 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
export class FocusActiveEditorAction extends Action {

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

	public run(): TPromise<any> {
		let editor = this.editorService.getActiveEditor();
		if (editor) {
			editor.focus();
		}
		return TPromise.as(true);
	}

}

287
export abstract class FindOrReplaceInFilesAction extends Action {
S
Sandeep Somavarapu 已提交
288

289 290
	constructor(id: string, label: string, private viewletService: IViewletService,
		private expandSearchReplaceWidget: boolean, private selectWidgetText, private focusReplace) {
S
Sandeep Somavarapu 已提交
291 292 293 294
		super(id, label);
	}

	public run(): TPromise<any> {
295 296 297 298 299 300 301 302 303
		const viewlet = this.viewletService.getActiveViewlet();
		const searchViewletWasOpen = viewlet && viewlet.getId() === Constants.VIEWLET_ID;
		return this.viewletService.openViewlet(Constants.VIEWLET_ID, true).then((viewlet) => {
			if (!searchViewletWasOpen || this.expandSearchReplaceWidget) {
				const searchAndReplaceWidget = (<SearchViewlet>viewlet).searchAndReplaceWidget;
				searchAndReplaceWidget.toggleReplace(this.expandSearchReplaceWidget);
				searchAndReplaceWidget.focus(this.selectWidgetText, this.focusReplace);
			}
		});
S
Sandeep Somavarapu 已提交
304
	}
305 306 307
}

export class FindInFilesAction extends FindOrReplaceInFilesAction {
S
Sandeep Somavarapu 已提交
308

309 310 311
	constructor(id: string, label: string, @IViewletService viewletService: IViewletService) {
		super(id, label, viewletService, /*expandSearchReplaceWidget=*/false, /*selectWidgetText=*/true, /*focusReplace=*/false);
	}
S
Sandeep Somavarapu 已提交
312 313
}

314
export class ReplaceInFilesAction extends FindOrReplaceInFilesAction {
S
Sandeep Somavarapu 已提交
315 316 317 318

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

319 320
	constructor(id: string, label: string, @IViewletService viewletService: IViewletService) {
		super(id, label, viewletService, /*expandSearchReplaceWidget=*/true, /*selectWidgetText=*/false, /*focusReplace=*/true);
S
Sandeep Somavarapu 已提交
321 322 323
	}
}

S
Sandeep Somavarapu 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337
export class CloseReplaceAction extends Action {

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

	public run(): TPromise<any> {
		let searchAndReplaceWidget = (<SearchViewlet>this.viewletService.getActiveViewlet()).searchAndReplaceWidget;
		searchAndReplaceWidget.toggleReplace(false);
		searchAndReplaceWidget.focus();
		return TPromise.as(null);
	}
}

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
export class FindInWorkspaceAction extends Action {

	public static ID = 'filesExplorer.findInWorkspace';

	constructor( @IViewletService private viewletService: IViewletService) {
		super(FindInWorkspaceAction.ID, nls.localize('findInWorkspace', "Find in Workspace..."));
	}

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

S
Sandeep Somavarapu 已提交
353 354
export class FindInFolderAction extends Action {

355 356
	public static ID = 'filesExplorer.findInFolder';

S
Sandeep Somavarapu 已提交
357 358
	private resource: URI;

359
	constructor(resource: URI, @IInstantiationService private instantiationService: IInstantiationService) {
360
		super(FindInFolderAction.ID, nls.localize('findInFolder', "Find in Folder..."));
S
Sandeep Somavarapu 已提交
361 362 363 364 365

		this.resource = resource;
	}

	public run(event?: any): TPromise<any> {
366
		return this.instantiationService.invokeFunction.apply(this.instantiationService, [findInFolderCommand, this.resource]);
S
Sandeep Somavarapu 已提交
367 368 369
	}
}

370 371 372 373 374 375 376 377
export const findInFolderCommand = (accessor: ServicesAccessor, resource?: URI) => {
	const listService = accessor.get(IListService);
	const viewletService = accessor.get(IViewletService);

	if (!URI.isUri(resource)) {
		const focused = listService.getFocused() ? listService.getFocused().getFocus() : void 0;
		if (focused) {
			const file = explorerItemToFileResource(focused);
378 379
			if (file) {
				resource = file.isDirectory ? file.resource : URI.file(paths.dirname(file.resource.fsPath));
380 381 382 383
			}
		}
	}

B
Benjamin Pasero 已提交
384
	viewletService.openViewlet(Constants.VIEWLET_ID, true).then((viewlet: SearchViewlet) => {
385 386 387
		if (resource) {
			viewlet.searchInFolder(resource);
		}
B
Benjamin Pasero 已提交
388
	}).done(null, errors.onUnexpectedError);
389 390
};

S
Sandeep Somavarapu 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
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);
	}
}

433
export class FocusNextSearchResultAction extends Action {
R
roblou 已提交
434
	public static ID = 'search.action.focusNextSearchResult';
R
Rob Lourens 已提交
435
	public static LABEL = nls.localize('FocusNextSearchResult.label', "Focus Next Search Result");
R
roblou 已提交
436

437
	constructor(id: string, label: string, @IViewletService private viewletService: IViewletService) {
R
roblou 已提交
438
		super(id, label);
439 440 441 442
	}

	public run(): TPromise<any> {
		return this.viewletService.openViewlet(Constants.VIEWLET_ID).then((searchViewlet: SearchViewlet) => {
R
Rob Lourens 已提交
443
			searchViewlet.selectNextMatch();
444 445 446 447 448
		});
	}
}

export class FocusPreviousSearchResultAction extends Action {
R
roblou 已提交
449
	public static ID = 'search.action.focusPreviousSearchResult';
R
Rob Lourens 已提交
450
	public static LABEL = nls.localize('FocusPreviousSearchResult.label', "Focus Previous Search Result");
R
roblou 已提交
451

452
	constructor(id: string, label: string, @IViewletService private viewletService: IViewletService) {
R
roblou 已提交
453
		super(id, label);
454 455 456 457
	}

	public run(): TPromise<any> {
		return this.viewletService.openViewlet(Constants.VIEWLET_ID).then((searchViewlet: SearchViewlet) => {
R
Rob Lourens 已提交
458
			searchViewlet.selectPreviousMatch();
459 460 461 462
		});
	}
}

S
Sandeep Somavarapu 已提交
463 464
export abstract class AbstractSearchAndReplaceAction extends Action {

S
Sandeep Somavarapu 已提交
465 466 467
	/**
	 * Returns element to focus after removing the given element
	 */
468
	public getElementToFocusAfterRemoved(viewer: ITree, elementToBeRemoved: RenderableMatch): RenderableMatch {
S
Sandeep Somavarapu 已提交
469 470 471 472 473 474
		let elementToFocus = this.getNextElementAfterRemoved(viewer, elementToBeRemoved);
		if (!elementToFocus) {
			elementToFocus = this.getPreviousElementAfterRemoved(viewer, elementToBeRemoved);
		}
		return elementToFocus;
	}
S
Sandeep Somavarapu 已提交
475

476
	public getNextElementAfterRemoved(viewer: ITree, element: RenderableMatch): RenderableMatch {
S
Sandeep Somavarapu 已提交
477
		let navigator: INavigator<any> = this.getNavigatorAt(element, viewer);
478 479 480 481
		if (element instanceof FolderMatch) {
			// If file match is removed then next element is the next file match
			while (!!navigator.next() && !(navigator.current() instanceof FolderMatch)) { };
		} else if (element instanceof FileMatch) {
S
Sandeep Somavarapu 已提交
482 483 484 485 486 487 488
			// 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 已提交
489

490
	public getPreviousElementAfterRemoved(viewer: ITree, element: RenderableMatch): RenderableMatch {
S
Sandeep Somavarapu 已提交
491 492 493 494 495 496
		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 已提交
497
		}
S
Sandeep Somavarapu 已提交
498 499 500
		return previousElement;
	}

501
	private getNavigatorAt(element: RenderableMatch, viewer: ITree): INavigator<any> {
J
Johannes Rieken 已提交
502
		let navigator: INavigator<any> = viewer.getNavigator();
S
Sandeep Somavarapu 已提交
503
		while (navigator.current() !== element && !!navigator.next()) { }
S
Sandeep Somavarapu 已提交
504
		return navigator;
S
Sandeep Somavarapu 已提交
505 506 507 508
	}
}

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

510
	constructor(private viewer: ITree, private element: RenderableMatch) {
511
		super('remove', nls.localize('RemoveAction.label', "Remove"), 'action-remove');
S
Sandeep Somavarapu 已提交
512 513
	}

514
	public run(): TPromise<any> {
S
Sandeep Somavarapu 已提交
515
		let nextFocusElement = this.getElementToFocusAfterRemoved(this.viewer, this.element);
S
Sandeep Somavarapu 已提交
516 517
		if (nextFocusElement) {
			this.viewer.setFocus(nextFocusElement);
518 519
		}

520
		let elementToRefresh: any;
521 522 523 524
		const element = this.element;
		if (element instanceof FolderMatch) {
			let parent = element.parent();
			parent.remove(element);
S
Sandeep Somavarapu 已提交
525
			elementToRefresh = parent;
526 527 528 529 530 531 532
		} else if (element instanceof FileMatch) {
			let parent = element.parent();
			parent.remove(element);
			elementToRefresh = parent;
		} else if (element instanceof Match) {
			let parent = element.parent();
			parent.remove(element);
S
Sandeep Somavarapu 已提交
533
			elementToRefresh = parent.count() === 0 ? parent.parent() : parent;
S
Sandeep Somavarapu 已提交
534
		}
535

S
Sandeep Somavarapu 已提交
536
		this.viewer.DOMFocus();
537 538 539
		return this.viewer.refresh(elementToRefresh);
	}

540
}
S
Sandeep Somavarapu 已提交
541

S
Sandeep Somavarapu 已提交
542
export class ReplaceAllAction extends AbstractSearchAndReplaceAction {
S
Sandeep Somavarapu 已提交
543

544
	constructor(private viewer: ITree, private fileMatch: FileMatch, private viewlet: SearchViewlet,
S
Sandeep Somavarapu 已提交
545
		@IReplaceService private replaceService: IReplaceService,
S
Sandeep Somavarapu 已提交
546
		@IKeybindingService keyBindingService: IKeybindingService,
S
Sandeep Somavarapu 已提交
547
		@ITelemetryService private telemetryService: ITelemetryService) {
S
Sandeep Somavarapu 已提交
548
		super(Constants.ReplaceAllInFileActionId, appendKeyBindingLabel(nls.localize('file.replaceAll.label', "Replace All"), keyBindingService.lookupKeybinding(Constants.ReplaceAllInFileActionId), keyBindingService), 'action-replace-all');
S
Sandeep Somavarapu 已提交
549 550
	}

551
	public run(): TPromise<any> {
552
		this.telemetryService.publicLog('replaceAll.action.selected');
S
Sandeep Somavarapu 已提交
553
		let nextFocusElement = this.getElementToFocusAfterRemoved(this.viewer, this.fileMatch);
S
Sandeep Somavarapu 已提交
554
		return this.fileMatch.parent().replace(this.fileMatch).then(() => {
S
Sandeep Somavarapu 已提交
555 556 557 558 559
			if (nextFocusElement) {
				this.viewer.setFocus(nextFocusElement);
			}
			this.viewer.DOMFocus();
			this.viewlet.open(this.fileMatch, true);
S
Sandeep Somavarapu 已提交
560 561 562 563
		});
	}
}

S
Sandeep Somavarapu 已提交
564
export class ReplaceAction extends AbstractSearchAndReplaceAction {
S
Sandeep Somavarapu 已提交
565

566
	constructor(private viewer: ITree, private element: Match, private viewlet: SearchViewlet,
S
Sandeep Somavarapu 已提交
567
		@IReplaceService private replaceService: IReplaceService,
S
Sandeep Somavarapu 已提交
568
		@IKeybindingService keyBindingService: IKeybindingService,
S
Sandeep Somavarapu 已提交
569 570
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@ITelemetryService private telemetryService: ITelemetryService) {
S
Sandeep Somavarapu 已提交
571
		super(Constants.ReplaceActionId, appendKeyBindingLabel(nls.localize('match.replace.label', "Replace"), keyBindingService.lookupKeybinding(Constants.ReplaceActionId), keyBindingService), 'action-replace');
S
Sandeep Somavarapu 已提交
572 573 574
	}

	public run(): TPromise<any> {
S
Sandeep Somavarapu 已提交
575
		this.enabled = false;
576
		this.telemetryService.publicLog('replace.action.selected');
S
Sandeep Somavarapu 已提交
577

S
Sandeep Somavarapu 已提交
578
		return this.element.parent().replace(this.element).then(() => {
S
Sandeep Somavarapu 已提交
579
			let elementToFocus = this.getElementToFocusAfterReplace();
S
Sandeep Somavarapu 已提交
580 581
			if (elementToFocus) {
				this.viewer.setFocus(elementToFocus);
S
Sandeep Somavarapu 已提交
582
			}
S
Sandeep Somavarapu 已提交
583
			let elementToShowReplacePreview = this.getElementToShowReplacePreview(elementToFocus);
S
Sandeep Somavarapu 已提交
584
			this.viewer.DOMFocus();
S
Sandeep Somavarapu 已提交
585 586
			if (!elementToShowReplacePreview || this.hasToOpenFile()) {
				this.viewlet.open(this.element, true);
S
Sandeep Somavarapu 已提交
587
			} else {
S
Sandeep Somavarapu 已提交
588
				this.replaceService.openReplacePreview(elementToShowReplacePreview, true);
S
Sandeep Somavarapu 已提交
589
			}
590
		});
S
Sandeep Somavarapu 已提交
591
	}
S
Sandeep Somavarapu 已提交
592

S
Sandeep Somavarapu 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	private getElementToFocusAfterReplace(): Match {
		let navigator: INavigator<any> = this.viewer.getNavigator();
		let fileMatched = false;
		let elementToFocus = null;
		do {
			elementToFocus = navigator.current();
			if (elementToFocus instanceof Match) {
				if (elementToFocus.parent().id() === this.element.parent().id()) {
					fileMatched = true;
					if (this.element.range().getStartPosition().isBeforeOrEqual((<Match>elementToFocus).range().getStartPosition())) {
						// Closest next match in the same file
						break;
					}
				} else if (fileMatched) {
					// First match in the next file (if expanded)
					break;
				}
			} else if (fileMatched) {
				if (!this.viewer.isExpanded(elementToFocus)) {
					// Next file match (if collapsed)
					break;
				}
			}
		} while (!!navigator.next());
		return elementToFocus;
	}

S
Sandeep Somavarapu 已提交
620 621 622 623 624 625 626 627 628 629 630
	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;
	}

631
	private hasSameParent(element: RenderableMatch): boolean {
S
Sandeep Somavarapu 已提交
632 633 634 635
		return element && element instanceof Match && element.parent().resource() === this.element.parent().resource();
	}

	private hasToOpenFile(): boolean {
636 637
		const file = toResource(this.editorService.getActiveEditorInput(), { filter: 'file' });
		if (file) {
638
			return paths.isEqual(file.fsPath, this.element.parent().resource().fsPath);
S
Sandeep Somavarapu 已提交
639 640 641
		}
		return false;
	}
S
Sandeep Somavarapu 已提交
642
}