searchView.ts 73.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
7
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
8
import * as aria from 'vs/base/browser/ui/aria/aria';
9
import { MessageType } from 'vs/base/browser/ui/inputbox/inputBox';
R
Rob Lourens 已提交
10 11
import { IIdentityProvider } from 'vs/base/browser/ui/list/list';
import { ITreeContextMenuEvent, ITreeElement } from 'vs/base/browser/ui/tree/tree';
J
Joao Moreno 已提交
12
import { IAction, ActionRunner } from 'vs/base/common/actions';
13 14
import { Delayer } from 'vs/base/common/async';
import * as errors from 'vs/base/common/errors';
R
Rob Lourens 已提交
15
import { Event } from 'vs/base/common/event';
R
Rob Lourens 已提交
16
import { Iterator } from 'vs/base/common/iterator';
S
Sandeep Somavarapu 已提交
17
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
18
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
19 20
import * as env from 'vs/base/common/platform';
import * as strings from 'vs/base/common/strings';
21
import { URI } from 'vs/base/common/uri';
22
import 'vs/css!./media/searchview';
23
import { ICodeEditor, isCodeEditor, isDiffEditor, getCodeEditor } from 'vs/editor/browser/editorBrowser';
24 25
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import * as nls from 'vs/nls';
26
import { createAndFillInContextMenuActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
R
Rob Lourens 已提交
27
import { IMenu, IMenuService, MenuId } from 'vs/platform/actions/common/actions';
J
Johannes Rieken 已提交
28
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
29
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
R
Rob Lourens 已提交
30
import { IContextMenuService, IContextViewService } from 'vs/platform/contextview/browser/contextView';
31 32
import { IConfirmation, IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { FileChangesEvent, FileChangeType, IFileService } from 'vs/platform/files/common/files';
J
Johannes Rieken 已提交
33
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
34
import { TreeResourceNavigator, WorkbenchObjectTree, getSelectionKeyboardEvent } from 'vs/platform/list/browser/listService';
35
import { INotificationService } from 'vs/platform/notification/common/notification';
B
Benjamin Pasero 已提交
36
import { IProgressService, IProgressStep, IProgress } from 'vs/platform/progress/common/progress';
37
import { IPatternInfo, ISearchComplete, ISearchConfiguration, ISearchConfigurationProperties, ITextQuery, VIEW_ID, SearchSortOrder } from 'vs/workbench/services/search/common/search';
38
import { ISearchHistoryService, ISearchHistoryValues } from 'vs/workbench/contrib/search/common/searchHistoryService';
39
import { diffInserted, diffInsertedOutline, diffRemoved, diffRemovedOutline, editorFindMatchHighlight, editorFindMatchHighlightBorder, listActiveSelectionForeground, foreground } from 'vs/platform/theme/common/colorRegistry';
40 41 42
import { ICssStyleCollector, ITheme, IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { OpenFileFolderAction, OpenFolderAction } from 'vs/workbench/browser/actions/workspaceActions';
43
import { ResourceLabels } from 'vs/workbench/browser/labels';
44
import { IEditor } from 'vs/workbench/common/editor';
45
import { ExcludePatternInputWidget, PatternInputWidget } from 'vs/workbench/contrib/search/browser/patternInputWidget';
46
import { CancelSearchAction, ClearSearchResultsAction, CollapseDeepestExpandedLevelAction, RefreshAction, IFindInFilesArgs, appendKeyBindingLabel, ExpandAllAction, ToggleCollapseAndExpandAction } from 'vs/workbench/contrib/search/browser/searchActions';
47 48 49 50 51 52
import { FileMatchRenderer, FolderMatchRenderer, MatchRenderer, SearchAccessibilityProvider, SearchDelegate, SearchDND } from 'vs/workbench/contrib/search/browser/searchResultsView';
import { ISearchWidgetOptions, SearchWidget } from 'vs/workbench/contrib/search/browser/searchWidget';
import * as Constants from 'vs/workbench/contrib/search/common/constants';
import { ITextQueryBuilderOptions, QueryBuilder } from 'vs/workbench/contrib/search/common/queryBuilder';
import { IReplaceService } from 'vs/workbench/contrib/search/common/replace';
import { getOutOfWorkspaceEditorResources } from 'vs/workbench/contrib/search/common/search';
53
import { FileMatch, FileMatchOrMatch, IChangeEvent, ISearchWorkbenchService, Match, RenderableMatch, searchMatchComparer, SearchModel, SearchResult, FolderMatch, FolderMatchWithResource } from 'vs/workbench/contrib/search/common/searchModel';
54
import { ACTIVE_GROUP, IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
55
import { IPreferencesService, ISettingsEditorOptions } from 'vs/workbench/services/preferences/common/preferences';
56
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
57
import { relativePath } from 'vs/base/common/resources';
I
isidor 已提交
58
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
S
SteVen Batten 已提交
59
import { ViewPane, IViewPaneOptions } from 'vs/workbench/browser/parts/views/viewPaneContainer';
60
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
61
import { Memento, MementoObject } from 'vs/workbench/common/memento';
62
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
63
import { IOpenerService } from 'vs/platform/opener/common/opener';
64
import { MultiCursorSelectionController } from 'vs/editor/contrib/multicursor/multicursor';
65
import { Selection } from 'vs/editor/common/core/selection';
66
import { Color, RGBA } from 'vs/base/common/color';
67
import { IViewDescriptorService } from 'vs/workbench/common/views';
68
import { OpenSearchEditorAction, createEditorFromSearchResult } from 'vs/workbench/contrib/searchEditor/browser/searchEditorActions';
69
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
E
Erich Gamma 已提交
70

71 72
const $ = dom.$;

73 74 75 76 77 78
enum SearchUIState {
	Idle,
	Searching,
	SlowSearch
}

79 80 81 82 83
export enum SearchViewPosition {
	SideBar,
	Panel
}

J
Jackson Kearl 已提交
84
const SEARCH_CANCELLED_MESSAGE = nls.localize('searchCanceled', "Search was canceled before any results could be found - ");
S
SteVen Batten 已提交
85
export class SearchView extends ViewPane {
E
Erich Gamma 已提交
86

R
Rob Lourens 已提交
87
	private static readonly MAX_TEXT_RESULTS = 10000;
S
Sandeep Somavarapu 已提交
88

89
	private static readonly WIDE_CLASS_NAME = 'wide';
R
Rob Lourens 已提交
90
	private static readonly WIDE_VIEW_SIZE = 1000;
91
	private static readonly ACTIONS_RIGHT_CLASS_NAME = 'actions-right';
92

93
	private isDisposed = false;
94

95
	private container!: HTMLElement;
E
Erich Gamma 已提交
96
	private queryBuilder: QueryBuilder;
97
	private viewModel: SearchModel;
98
	private memento: Memento;
E
Erich Gamma 已提交
99

A
Alex Dima 已提交
100
	private viewletVisible: IContextKey<boolean>;
101 102
	private inputBoxFocused: IContextKey<boolean>;
	private inputPatternIncludesFocused: IContextKey<boolean>;
103
	private inputPatternExclusionsFocused: IContextKey<boolean>;
104 105
	private firstMatchFocused: IContextKey<boolean>;
	private fileMatchOrMatchFocused: IContextKey<boolean>;
106
	private fileMatchOrFolderMatchFocus: IContextKey<boolean>;
107
	private fileMatchOrFolderMatchWithResourceFocus: IContextKey<boolean>;
108
	private fileMatchFocused: IContextKey<boolean>;
109
	private folderMatchFocused: IContextKey<boolean>;
110
	private matchFocused: IContextKey<boolean>;
111
	private hasSearchResultsKey: IContextKey<boolean>;
R
Rob Lourens 已提交
112

113
	private state: SearchUIState = SearchUIState.Idle;
S
Sandeep Somavarapu 已提交
114

115
	private actions: Array<CollapseDeepestExpandedLevelAction | ClearSearchResultsAction | OpenSearchEditorAction> = [];
116
	private toggleCollapseAction: ToggleCollapseAndExpandAction;
117 118
	private cancelAction: CancelSearchAction;
	private refreshAction: RefreshAction;
119
	private contextMenu: IMenu | null = null;
120

121 122
	private tree!: WorkbenchObjectTree<RenderableMatch>;
	private treeLabels!: ResourceLabels;
123
	private viewletState: MementoObject;
124
	private messagesElement!: HTMLElement;
125
	private messageDisposables: IDisposable[] = [];
126 127 128 129 130 131 132 133
	private searchWidgetsContainerElement!: HTMLElement;
	private searchWidget!: SearchWidget;
	private size!: dom.Dimension;
	private queryDetails!: HTMLElement;
	private toggleQueryDetailsButton!: HTMLElement;
	private inputPatternExcludes!: ExcludePatternInputWidget;
	private inputPatternIncludes!: PatternInputWidget;
	private resultsElement!: HTMLElement;
E
Erich Gamma 已提交
134

U
Ubuntu 已提交
135
	private currentSelectedFileMatch: FileMatch | undefined;
S
Sandeep Somavarapu 已提交
136

S
Sandeep Somavarapu 已提交
137
	private delayedRefresh: Delayer<void>;
138
	private changedWhileHidden: boolean = false;
R
Rob Lourens 已提交
139
	private updatedActionsWhileHidden = false;
R
Rob Lourens 已提交
140

U
Ubuntu 已提交
141
	private searchWithoutFolderMessageElement: HTMLElement | undefined;
142

J
Johannes Rieken 已提交
143
	private currentSearchQ = Promise.resolve();
144
	private addToSearchHistoryDelayer: Delayer<void>;
145

146 147
	private toggleCollapseStateDelayer: Delayer<void>;

148 149 150
	private triggerQueryDelayer: Delayer<void>;
	private pauseSearching = false;

151
	constructor(
S
SteVen Batten 已提交
152
		options: IViewPaneOptions,
153 154
		@IFileService private readonly fileService: IFileService,
		@IEditorService private readonly editorService: IEditorService,
155
		@IProgressService private readonly progressService: IProgressService,
156 157 158
		@INotificationService private readonly notificationService: INotificationService,
		@IDialogService private readonly dialogService: IDialogService,
		@IContextViewService private readonly contextViewService: IContextViewService,
159
		@IInstantiationService instantiationService: IInstantiationService,
160
		@IViewDescriptorService viewDescriptorService: IViewDescriptorService,
161
		@IConfigurationService configurationService: IConfigurationService,
162 163
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
		@ISearchWorkbenchService private readonly searchWorkbenchService: ISearchWorkbenchService,
164
		@IContextKeyService readonly contextKeyService: IContextKeyService,
165
		@IReplaceService private readonly replaceService: IReplaceService,
166
		@ITextFileService private readonly textFileService: ITextFileService,
167
		@IPreferencesService private readonly preferencesService: IPreferencesService,
168
		@IThemeService themeService: IThemeService,
169
		@ISearchHistoryService private readonly searchHistoryService: ISearchHistoryService,
170
		@IContextMenuService contextMenuService: IContextMenuService,
171
		@IMenuService private readonly menuService: IMenuService,
172 173 174
		@IAccessibilityService private readonly accessibilityService: IAccessibilityService,
		@IKeybindingService keybindingService: IKeybindingService,
		@IStorageService storageService: IStorageService,
175 176
		@IOpenerService openerService: IOpenerService,
	) {
177

178
		super({ ...options, id: VIEW_ID, ariaHeaderLabel: nls.localize('searchView', "Search") }, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService);
E
Erich Gamma 已提交
179

180 181 182 183 184 185 186 187 188
		this.container = dom.$('.search-view');

		this.contextKeyService = this._register(this.contextKeyService.createScoped(this.container));
		const viewletFocused = Constants.SearchViewFocusedKey.bindTo(this.contextKeyService);
		viewletFocused.set(true);

		this.instantiationService = this.instantiationService.createChild(
			new ServiceCollection([IContextKeyService, this.contextKeyService]));

I
isidor 已提交
189
		this.viewletVisible = Constants.SearchViewVisibleKey.bindTo(contextKeyService);
190 191
		this.inputBoxFocused = Constants.InputBoxFocusedKey.bindTo(this.contextKeyService);
		this.inputPatternIncludesFocused = Constants.PatternIncludesFocusedKey.bindTo(this.contextKeyService);
192
		this.inputPatternExclusionsFocused = Constants.PatternExcludesFocusedKey.bindTo(this.contextKeyService);
193 194
		this.firstMatchFocused = Constants.FirstMatchFocusKey.bindTo(contextKeyService);
		this.fileMatchOrMatchFocused = Constants.FileMatchOrMatchFocusKey.bindTo(contextKeyService);
195
		this.fileMatchOrFolderMatchFocus = Constants.FileMatchOrFolderMatchFocusKey.bindTo(contextKeyService);
196
		this.fileMatchOrFolderMatchWithResourceFocus = Constants.FileMatchOrFolderMatchWithResourceFocusKey.bindTo(contextKeyService);
197
		this.fileMatchFocused = Constants.FileFocusKey.bindTo(contextKeyService);
198
		this.folderMatchFocused = Constants.FolderFocusKey.bindTo(contextKeyService);
199
		this.matchFocused = Constants.MatchFocusKey.bindTo(this.contextKeyService);
200
		this.hasSearchResultsKey = Constants.HasSearchResults.bindTo(this.contextKeyService);
201

202 203
		this.configurationService.onDidChangeConfiguration(e => {
			if (e.affectsConfiguration('search.sortOrder')) {
204
				if (this.searchConfig.sortOrder === SearchSortOrder.Modified) {
205 206 207 208 209 210 211
					// If changing away from modified, remove all fileStats
					// so that updated files are re-retrieved next time.
					this.removeFileStats();
				}
				this.refreshTree();
			}
		});
E
Erich Gamma 已提交
212

R
Rob Lourens 已提交
213
		this.viewModel = this._register(this.searchWorkbenchService.searchModel);
E
Erich Gamma 已提交
214
		this.queryBuilder = this.instantiationService.createInstance(QueryBuilder);
215 216
		this.memento = new Memento(this.id, storageService);
		this.viewletState = this.memento.getMemento(StorageScope.WORKSPACE);
E
Erich Gamma 已提交
217

218
		this._register(this.fileService.onDidFilesChange(e => this.onFilesChanged(e)));
219
		this._register(this.textFileService.untitled.onDidDispose(model => this.onUntitledDidDispose(model.resource)));
B
Benjamin Pasero 已提交
220
		this._register(this.contextService.onDidChangeWorkbenchState(() => this.onDidChangeWorkbenchState()));
221
		this._register(this.searchHistoryService.onDidClearHistory(() => this.clearHistory()));
222

223
		this.delayedRefresh = this._register(new Delayer<void>(250));
J
Jackson Kearl 已提交
224

225
		this.addToSearchHistoryDelayer = this._register(new Delayer<void>(2000));
226
		this.toggleCollapseStateDelayer = this._register(new Delayer<void>(100));
227
		this.triggerQueryDelayer = this._register(new Delayer<void>(0));
228 229 230

		const collapseDeepestExpandedLevelAction = this.instantiationService.createInstance(CollapseDeepestExpandedLevelAction, CollapseDeepestExpandedLevelAction.ID, CollapseDeepestExpandedLevelAction.LABEL);
		const expandAllAction = this.instantiationService.createInstance(ExpandAllAction, ExpandAllAction.ID, ExpandAllAction.LABEL);
231

232 233 234
		this.actions = [
			this._register(this.instantiationService.createInstance(ClearSearchResultsAction, ClearSearchResultsAction.ID, ClearSearchResultsAction.LABEL)),
		];
235 236 237

		if (this.searchConfig.enableSearchEditorPreview) {
			this.actions.push(
238
				this._register(this.instantiationService.createInstance(OpenSearchEditorAction, OpenSearchEditorAction.ID, OpenSearchEditorAction.LABEL))
239 240 241
			);
		}

242 243
		this.refreshAction = this._register(this.instantiationService.createInstance(RefreshAction, RefreshAction.ID, RefreshAction.LABEL));
		this.cancelAction = this._register(this.instantiationService.createInstance(CancelSearchAction, CancelSearchAction.ID, CancelSearchAction.LABEL));
244
		this.toggleCollapseAction = this._register(this.instantiationService.createInstance(ToggleCollapseAndExpandAction, ToggleCollapseAndExpandAction.ID, ToggleCollapseAndExpandAction.LABEL, collapseDeepestExpandedLevelAction, expandAllAction));
245 246 247 248
	}

	getContainer(): HTMLElement {
		return this.container;
E
Erich Gamma 已提交
249 250
	}

R
Rob Lourens 已提交
251
	get searchResult(): SearchResult {
R
Rob Lourens 已提交
252 253 254
		return this.viewModel && this.viewModel.searchResult;
	}

255
	private onDidChangeWorkbenchState(): void {
256 257
		if (this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY && this.searchWithoutFolderMessageElement) {
			dom.hide(this.searchWithoutFolderMessageElement);
258 259 260
		}
	}

261
	renderBody(parent: HTMLElement): void {
J
Joao Moreno 已提交
262
		super.renderBody(parent);
263
		this.container = dom.append(parent, dom.$('.search-view'));
E
Erich Gamma 已提交
264

265
		this.searchWidgetsContainerElement = dom.append(this.container, $('.search-widgets-container'));
266
		this.createSearchWidget(this.searchWidgetsContainerElement);
267

268
		const history = this.searchHistoryService.load();
B
Benjamin Pasero 已提交
269 270
		const filePatterns = this.viewletState['query.filePatterns'] || '';
		const patternExclusions = this.viewletState['query.folderExclusions'] || '';
R
Rob Lourens 已提交
271
		const patternExclusionsHistory: string[] = history.exclude || [];
B
Benjamin Pasero 已提交
272
		const patternIncludes = this.viewletState['query.folderIncludes'] || '';
R
Rob Lourens 已提交
273
		const patternIncludesHistory: string[] = history.include || [];
B
Benjamin Pasero 已提交
274 275 276
		const queryDetailsExpanded = this.viewletState['query.queryDetailsExpanded'] || '';
		const useExcludesAndIgnoreFiles = typeof this.viewletState['query.useExcludesAndIgnoreFiles'] === 'boolean' ?
			this.viewletState['query.useExcludesAndIgnoreFiles'] : true;
277

278
		this.queryDetails = dom.append(this.searchWidgetsContainerElement, $('.query-details'));
S
Sandeep Somavarapu 已提交
279

280 281
		// Toggle query details button
		this.toggleQueryDetailsButton = dom.append(this.queryDetails,
282
			$('.more.codicon.codicon-ellipsis', { tabindex: 0, role: 'button', title: nls.localize('moreSearch', "Toggle Search Details") }));
E
Erich Gamma 已提交
283

284 285
		this._register(dom.addDisposableListener(this.toggleQueryDetailsButton, dom.EventType.CLICK, e => {
			dom.EventHelper.stop(e);
I
isidor 已提交
286
			this.toggleQueryDetails(!this.accessibilityService.isScreenReaderOptimized());
287 288 289
		}));
		this._register(dom.addDisposableListener(this.toggleQueryDetailsButton, dom.EventType.KEY_UP, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
E
Erich Gamma 已提交
290

291 292 293 294 295 296 297
			if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
				dom.EventHelper.stop(e);
				this.toggleQueryDetails(false);
			}
		}));
		this._register(dom.addDisposableListener(this.toggleQueryDetailsButton, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
E
Erich Gamma 已提交
298

299 300 301 302
			if (event.equals(KeyMod.Shift | KeyCode.Tab)) {
				if (this.searchWidget.isReplaceActive()) {
					this.searchWidget.focusReplaceAllAction();
				} else {
S
skprabhanjan 已提交
303
					this.searchWidget.isReplaceShown() ? this.searchWidget.replaceInput.focusOnPreserve() : this.searchWidget.focusRegexAction();
304 305 306 307
				}
				dom.EventHelper.stop(e);
			}
		}));
E
Erich Gamma 已提交
308

309 310 311 312 313
		// folder includes list
		const folderIncludesList = dom.append(this.queryDetails,
			$('.file-types.includes'));
		const filesToIncludeTitle = nls.localize('searchScope.includes', "files to include");
		dom.append(folderIncludesList, $('h4', undefined, filesToIncludeTitle));
314

315 316 317 318
		this.inputPatternIncludes = this._register(this.instantiationService.createInstance(PatternInputWidget, folderIncludesList, this.contextViewService, {
			ariaLabel: nls.localize('label.includes', 'Search Include Patterns'),
			history: patternIncludesHistory,
		}));
319

320
		this.inputPatternIncludes.setValue(patternIncludes);
321

322
		this.inputPatternIncludes.onSubmit(triggeredOnType => this.triggerQueryChange({ triggeredOnType, delay: this.searchConfig.searchOnTypeDebouncePeriod }));
323
		this.inputPatternIncludes.onCancel(() => this.cancelSearch(false));
324 325 326 327 328 329 330 331 332 333
		this.trackInputBox(this.inputPatternIncludes.inputFocusTracker, this.inputPatternIncludesFocused);

		// excludes list
		const excludesList = dom.append(this.queryDetails, $('.file-types.excludes'));
		const excludesTitle = nls.localize('searchScope.excludes', "files to exclude");
		dom.append(excludesList, $('h4', undefined, excludesTitle));
		this.inputPatternExcludes = this._register(this.instantiationService.createInstance(ExcludePatternInputWidget, excludesList, this.contextViewService, {
			ariaLabel: nls.localize('label.excludes', 'Search Exclude Patterns'),
			history: patternExclusionsHistory,
		}));
334

335 336
		this.inputPatternExcludes.setValue(patternExclusions);
		this.inputPatternExcludes.setUseExcludesAndIgnoreFiles(useExcludesAndIgnoreFiles);
337

338
		this.inputPatternExcludes.onSubmit(triggeredOnType => this.triggerQueryChange({ triggeredOnType, delay: this.searchConfig.searchOnTypeDebouncePeriod }));
339
		this.inputPatternExcludes.onCancel(() => this.cancelSearch(false));
340
		this.inputPatternExcludes.onChangeIgnoreBox(() => this.triggerQueryChange());
341 342
		this.trackInputBox(this.inputPatternExcludes.inputFocusTracker, this.inputPatternExclusionsFocused);

343
		this.messagesElement = dom.append(this.container, $('.messages'));
344
		if (this.contextService.getWorkbenchState() === WorkbenchState.EMPTY) {
345
			this.showSearchWithoutFolderMessage();
346
		}
347

348
		this.createSearchResultsView(this.container);
349

350
		if (filePatterns !== '' || patternExclusions !== '' || patternIncludes !== '' || queryDetailsExpanded !== '' || !useExcludesAndIgnoreFiles) {
351
			this.toggleQueryDetails(true, true, true);
352 353
		}

B
Benjamin Pasero 已提交
354
		this._register(this.viewModel.searchResult.onChange((event) => this.onSearchResultsChanged(event)));
S
Sandeep Somavarapu 已提交
355

356
		this._register(this.searchWidget.searchInput.onInput(() => this.updateActions()));
J
jeanp413 已提交
357
		this._register(this.searchWidget.replaceInput.onInput(() => this.updateActions()));
358

359
		this._register(this.onDidChangeBodyVisibility(visible => this.onVisibilityChanged(visible)));
360 361 362 363 364 365 366 367 368 369
	}

	private onVisibilityChanged(visible: boolean): void {
		this.viewletVisible.set(visible);
		if (visible) {
			if (this.changedWhileHidden) {
				// Render if results changed while viewlet was hidden - #37818
				this.refreshAndUpdateCount();
				this.changedWhileHidden = false;
			}
R
Rob Lourens 已提交
370 371 372 373 374 375 376

			if (this.updatedActionsWhileHidden) {
				// The actions can only run or update their enablement when the view is visible,
				// because they can only access the view when it's visible
				this.updateActions();
				this.updatedActionsWhileHidden = false;
			}
377 378 379 380 381 382
		}

		// Enable highlights if there are searchresults
		if (this.viewModel) {
			this.viewModel.searchResult.toggleHighlights(visible);
		}
383 384
	}

R
Rob Lourens 已提交
385
	get searchAndReplaceWidget(): SearchWidget {
386 387 388
		return this.searchWidget;
	}

R
Rob Lourens 已提交
389
	get searchIncludePattern(): PatternInputWidget {
390 391 392
		return this.inputPatternIncludes;
	}

R
Rob Lourens 已提交
393
	get searchExcludePattern(): PatternInputWidget {
394 395 396
		return this.inputPatternExcludes;
	}

397 398 399
	/**
	 * Warning: a bit expensive due to updating the view title
	 */
400
	protected updateActions(): void {
R
Rob Lourens 已提交
401 402 403 404
		if (!this.isVisible()) {
			this.updatedActionsWhileHidden = true;
		}

S
Sandeep Somavarapu 已提交
405 406 407
		for (const action of this.actions) {
			action.update();
		}
408 409 410

		this.refreshAction.update();
		this.cancelAction.update();
411
		this.toggleCollapseAction.update();
412

413
		super.updateActions();
S
Sandeep Somavarapu 已提交
414 415
	}

416
	private createSearchWidget(container: HTMLElement): void {
417
		const contentPattern = this.viewletState['query.contentPattern'] || '';
418
		const replaceText = this.viewletState['query.replaceText'] || '';
419 420 421
		const isRegex = this.viewletState['query.regex'] === true;
		const isWholeWords = this.viewletState['query.wholeWords'] === true;
		const isCaseSensitive = this.viewletState['query.caseSensitive'] === true;
422
		const history = this.searchHistoryService.load();
423 424 425
		const searchHistory = history.search || this.viewletState['query.searchHistory'] || [];
		const replaceHistory = history.replace || this.viewletState['query.replaceHistory'] || [];
		const showReplace = typeof this.viewletState['view.showReplace'] === 'boolean' ? this.viewletState['view.showReplace'] : true;
426
		const preserveCase = this.viewletState['query.preserveCase'] === true;
E
Erich Gamma 已提交
427

428
		this.searchWidget = this._register(this.instantiationService.createInstance(SearchWidget, container, <ISearchWidgetOptions>{
429
			value: contentPattern,
430
			replaceValue: replaceText,
431 432
			isRegex: isRegex,
			isCaseSensitive: isCaseSensitive,
433
			isWholeWords: isWholeWords,
434
			searchHistory: searchHistory,
435 436
			replaceHistory: replaceHistory,
			preserveCase: preserveCase
B
Benjamin Pasero 已提交
437
		}));
S
Sandeep Somavarapu 已提交
438

B
Benjamin Pasero 已提交
439
		if (showReplace) {
S
Sandeep Somavarapu 已提交
440 441 442
			this.searchWidget.toggleReplace(true);
		}

443
		this._register(this.searchWidget.onSearchSubmit(options => this.triggerQueryChange(options)));
444
		this._register(this.searchWidget.onSearchCancel(({ focus }) => this.cancelSearch(focus)));
445
		this._register(this.searchWidget.searchInput.onDidOptionChange(() => this.triggerQueryChange()));
446

447 448 449
		this._register(this.searchWidget.onDidHeightChange(() => this.reLayout()));

		this._register(this.searchWidget.onReplaceToggled(() => this.reLayout()));
B
Benjamin Pasero 已提交
450
		this._register(this.searchWidget.onReplaceStateChange((state) => {
J
Johannes Rieken 已提交
451
			this.viewModel.replaceActive = state;
R
Rob Lourens 已提交
452
			this.refreshTree();
S
Sandeep Somavarapu 已提交
453
		}));
454 455 456 457 458 459

		this._register(this.searchWidget.onPreserveCaseChange((state) => {
			this.viewModel.preserveCase = state;
			this.refreshTree();
		}));

460
		this._register(this.searchWidget.onReplaceValueChanged(() => {
J
Johannes Rieken 已提交
461
			this.viewModel.replaceString = this.searchWidget.getReplaceValue();
R
Rob Lourens 已提交
462
			this.delayedRefresh.trigger(() => this.refreshTree());
S
Sandeep Somavarapu 已提交
463
		}));
464

B
Benjamin Pasero 已提交
465
		this._register(this.searchWidget.onBlur(() => {
S
Sandeep Somavarapu 已提交
466 467 468
			this.toggleQueryDetailsButton.focus();
		}));

B
Benjamin Pasero 已提交
469
		this._register(this.searchWidget.onReplaceAll(() => this.replaceAll()));
470

471 472 473 474
		this.trackInputBox(this.searchWidget.searchInputFocusTracker);
		this.trackInputBox(this.searchWidget.replaceInputFocusTracker);
	}

475
	private trackInputBox(inputFocusTracker: dom.IFocusTracker, contextKey?: IContextKey<boolean>): void {
B
Benjamin Pasero 已提交
476
		this._register(inputFocusTracker.onDidFocus(() => {
477
			this.inputBoxFocused.set(true);
478 479 480
			if (contextKey) {
				contextKey.set(true);
			}
481
		}));
B
Benjamin Pasero 已提交
482
		this._register(inputFocusTracker.onDidBlur(() => {
483
			this.inputBoxFocused.set(this.searchWidget.searchInputHasFocus()
484
				|| this.searchWidget.replaceInputHasFocus()
485 486
				|| this.inputPatternIncludes.inputHasFocus()
				|| this.inputPatternExcludes.inputHasFocus());
487 488 489
			if (contextKey) {
				contextKey.set(false);
			}
490
		}));
S
Sandeep Somavarapu 已提交
491 492
	}

R
Rob Lourens 已提交
493
	private onSearchResultsChanged(event?: IChangeEvent): void {
494 495 496 497 498 499 500
		if (this.isVisible()) {
			return this.refreshAndUpdateCount(event);
		} else {
			this.changedWhileHidden = true;
		}
	}

R
Rob Lourens 已提交
501
	private refreshAndUpdateCount(event?: IChangeEvent): void {
R
Rob Lourens 已提交
502
		this.searchWidget.setReplaceAllActionState(!this.viewModel.searchResult.isEmpty());
503
		this.updateSearchResultCount(this.viewModel.searchResult.query!.userDisabledExcludesAndIgnoreFiles);
R
Rob Lourens 已提交
504
		return this.refreshTree(event);
505 506
	}

R
Rob Lourens 已提交
507
	refreshTree(event?: IChangeEvent): void {
508
		const collapseResults = this.searchConfig.collapseResults;
509
		if (!event || event.added || event.removed) {
510
			// Refresh whole tree
511
			if (this.searchConfig.sortOrder === SearchSortOrder.Modified) {
512 513 514 515 516 517
				// Ensure all matches have retrieved their file stat
				this.retrieveFileStats()
					.then(() => this.tree.setChildren(null, this.createResultIterator(collapseResults)));
			} else {
				this.tree.setChildren(null, this.createResultIterator(collapseResults));
			}
518
		} else {
519
			// If updated counts affect our search order, re-sort the view.
520 521
			if (this.searchConfig.sortOrder === SearchSortOrder.CountAscending ||
				this.searchConfig.sortOrder === SearchSortOrder.CountDescending) {
522 523 524 525 526 527 528 529
				this.tree.setChildren(null, this.createResultIterator(collapseResults));
			} else {
				// FileMatch modified, refresh those elements
				event.elements.forEach(element => {
					this.tree.setChildren(element, this.createIterator(element, collapseResults));
					this.tree.rerender(element);
				});
			}
530 531 532
		}
	}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
	private createResultIterator(collapseResults: ISearchConfigurationProperties['collapseResults']): Iterator<ITreeElement<RenderableMatch>> {
		const folderMatches = this.searchResult.folderMatches()
			.filter(fm => !fm.isEmpty())
			.sort(searchMatchComparer);

		if (folderMatches.length === 1) {
			return this.createFolderIterator(folderMatches[0], collapseResults);
		}

		const foldersIt = Iterator.fromArray(folderMatches);
		return Iterator.map(foldersIt, folderMatch => {
			const children = this.createFolderIterator(folderMatch, collapseResults);
			return <ITreeElement<RenderableMatch>>{ element: folderMatch, children };
		});
	}

549
	private createFolderIterator(folderMatch: FolderMatch, collapseResults: ISearchConfigurationProperties['collapseResults']): Iterator<ITreeElement<RenderableMatch>> {
550
		const sortOrder = this.searchConfig.sortOrder;
551 552
		const filesIt = Iterator.fromArray(
			folderMatch.matches()
553
				.sort((a, b) => searchMatchComparer(a, b, sortOrder)));
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

		return Iterator.map(filesIt, fileMatch => {
			const children = this.createFileIterator(fileMatch);

			let nodeExists = true;
			try { this.tree.getNode(fileMatch); } catch (e) { nodeExists = false; }

			const collapsed = nodeExists ? undefined :
				(collapseResults === 'alwaysCollapse' || (fileMatch.matches().length > 10 && collapseResults !== 'alwaysExpand'));

			return <ITreeElement<RenderableMatch>>{ element: fileMatch, children, collapsed };
		});
	}

	private createFileIterator(fileMatch: FileMatch): Iterator<ITreeElement<RenderableMatch>> {
		const matchesIt = Iterator.from(
			fileMatch.matches()
				.sort(searchMatchComparer));
		return Iterator.map(matchesIt, r => (<ITreeElement<RenderableMatch>>{ element: r }));
	}

575
	private createIterator(match: FolderMatch | FileMatch | SearchResult, collapseResults: ISearchConfigurationProperties['collapseResults']): Iterator<ITreeElement<RenderableMatch>> {
576
		return match instanceof SearchResult ? this.createResultIterator(collapseResults) :
577
			match instanceof FolderMatch ? this.createFolderIterator(match, collapseResults) :
578 579 580
				this.createFileIterator(match);
	}

581
	private replaceAll(): void {
582
		if (this.viewModel.searchResult.count() === 0) {
S
Sandeep Somavarapu 已提交
583 584 585
			return;
		}

586 587 588 589
		const occurrences = this.viewModel.searchResult.count();
		const fileCount = this.viewModel.searchResult.fileCount();
		const replaceValue = this.searchWidget.getReplaceValue() || '';
		const afterReplaceAllMessage = this.buildAfterReplaceAllMessage(occurrences, fileCount, replaceValue);
590

B
Benjamin Pasero 已提交
591 592
		let progressComplete: () => void;
		let progressReporter: IProgress<IProgressStep>;
593 594

		this.progressService.withProgress({ location: this.getProgressLocation(), delay: 100, total: occurrences }, p => {
B
Benjamin Pasero 已提交
595 596 597 598 599
			progressReporter = p;

			return new Promise(resolve => progressComplete = resolve);
		});

600
		const confirmation: IConfirmation = {
601
			title: nls.localize('replaceAll.confirmation.title', "Replace All"),
602
			message: this.buildReplaceAllConfirmationMessage(occurrences, fileCount, replaceValue),
603
			primaryButton: nls.localize('replaceAll.confirm.button', "&&Replace"),
B
Benjamin Pasero 已提交
604
			type: 'question'
605 606
		};

607
		this.dialogService.confirm(confirmation).then(res => {
608
			if (res.confirmed) {
609
				this.searchWidget.setReplaceAllActionState(false);
B
Benjamin Pasero 已提交
610 611
				this.viewModel.searchResult.replaceAll(progressReporter).then(() => {
					progressComplete();
612
					const messageEl = this.clearMessage();
613
					dom.append(messageEl, $('p', undefined, afterReplaceAllMessage));
R
Rob Lourens 已提交
614
					this.reLayout();
615
				}, (error) => {
B
Benjamin Pasero 已提交
616
					progressComplete();
617
					errors.isPromiseCanceledError(error);
618
					this.notificationService.error(error);
619 620 621
				});
			}
		});
622 623
	}

624 625 626 627 628 629 630
	private buildAfterReplaceAllMessage(occurrences: number, fileCount: number, replaceValue?: string) {
		if (occurrences === 1) {
			if (fileCount === 1) {
				if (replaceValue) {
					return nls.localize('replaceAll.occurrence.file.message', "Replaced {0} occurrence across {1} file with '{2}'.", occurrences, fileCount, replaceValue);
				}

631
				return nls.localize('removeAll.occurrence.file.message', "Replaced {0} occurrence across {1} file.", occurrences, fileCount);
632 633 634 635 636 637 638 639 640 641 642 643 644 645
			}

			if (replaceValue) {
				return nls.localize('replaceAll.occurrence.files.message', "Replaced {0} occurrence across {1} files with '{2}'.", occurrences, fileCount, replaceValue);
			}

			return nls.localize('removeAll.occurrence.files.message', "Replaced {0} occurrence across {1} files.", occurrences, fileCount);
		}

		if (fileCount === 1) {
			if (replaceValue) {
				return nls.localize('replaceAll.occurrences.file.message', "Replaced {0} occurrences across {1} file with '{2}'.", occurrences, fileCount, replaceValue);
			}

646
			return nls.localize('removeAll.occurrences.file.message', "Replaced {0} occurrences across {1} file.", occurrences, fileCount);
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
		}

		if (replaceValue) {
			return nls.localize('replaceAll.occurrences.files.message', "Replaced {0} occurrences across {1} files with '{2}'.", occurrences, fileCount, replaceValue);
		}

		return nls.localize('removeAll.occurrences.files.message', "Replaced {0} occurrences across {1} files.", occurrences, fileCount);
	}

	private buildReplaceAllConfirmationMessage(occurrences: number, fileCount: number, replaceValue?: string) {
		if (occurrences === 1) {
			if (fileCount === 1) {
				if (replaceValue) {
					return nls.localize('removeAll.occurrence.file.confirmation.message', "Replace {0} occurrence across {1} file with '{2}'?", occurrences, fileCount, replaceValue);
				}

663
				return nls.localize('replaceAll.occurrence.file.confirmation.message', "Replace {0} occurrence across {1} file?", occurrences, fileCount);
664 665 666 667 668 669 670 671 672 673 674 675 676 677
			}

			if (replaceValue) {
				return nls.localize('removeAll.occurrence.files.confirmation.message', "Replace {0} occurrence across {1} files with '{2}'?", occurrences, fileCount, replaceValue);
			}

			return nls.localize('replaceAll.occurrence.files.confirmation.message', "Replace {0} occurrence across {1} files?", occurrences, fileCount);
		}

		if (fileCount === 1) {
			if (replaceValue) {
				return nls.localize('removeAll.occurrences.file.confirmation.message', "Replace {0} occurrences across {1} file with '{2}'?", occurrences, fileCount, replaceValue);
			}

678
			return nls.localize('replaceAll.occurrences.file.confirmation.message', "Replace {0} occurrences across {1} file?", occurrences, fileCount);
679 680 681 682 683 684 685 686 687
		}

		if (replaceValue) {
			return nls.localize('removeAll.occurrences.files.confirmation.message', "Replace {0} occurrences across {1} files with '{2}'?", occurrences, fileCount, replaceValue);
		}

		return nls.localize('replaceAll.occurrences.files.confirmation.message', "Replace {0} occurrences across {1} files?", occurrences, fileCount);
	}

688
	private clearMessage(): HTMLElement {
R
Rob Lourens 已提交
689
		this.searchWithoutFolderMessageElement = undefined;
690

691 692 693 694 695
		dom.clearNode(this.messagesElement);
		dom.show(this.messagesElement);
		dispose(this.messageDisposables);
		this.messageDisposables = [];

696
		return dom.append(this.messagesElement, $('.message'));
697 698
	}

699 700
	private createSearchResultsView(container: HTMLElement): void {
		this.resultsElement = dom.append(container, $('.results.show-file-icons'));
R
Rob Lourens 已提交
701 702
		const delegate = this.instantiationService.createInstance(SearchDelegate);

703 704 705 706 707 708
		const identityProvider: IIdentityProvider<RenderableMatch> = {
			getId(element: RenderableMatch) {
				return element.id();
			}
		};

709
		this.treeLabels = this._register(this.instantiationService.createInstance(ResourceLabels, { onDidChangeVisibility: this.onDidChangeBodyVisibility }));
710
		this.tree = this._register(<WorkbenchObjectTree<RenderableMatch>>this.instantiationService.createInstance(WorkbenchObjectTree,
J
Joao Moreno 已提交
711
			'SearchView',
R
Rob Lourens 已提交
712 713 714
			this.resultsElement,
			delegate,
			[
B
Benjamin Pasero 已提交
715 716
				this._register(this.instantiationService.createInstance(FolderMatchRenderer, this.viewModel, this, this.treeLabels)),
				this._register(this.instantiationService.createInstance(FileMatchRenderer, this.viewModel, this, this.treeLabels)),
717
				this._register(this.instantiationService.createInstance(MatchRenderer, this.viewModel, this)),
R
Rob Lourens 已提交
718
			],
719
			{
720
				identityProvider,
721
				accessibilityProvider: this.instantiationService.createInstance(SearchAccessibilityProvider, this.viewModel),
R
Rob Lourens 已提交
722
				dnd: this.instantiationService.createInstance(SearchDND),
J
Joao Moreno 已提交
723 724
				multipleSelectionSupport: false,
				overrideStyles: {
725
					listBackground: this.getBackgroundColor()
J
Joao Moreno 已提交
726
				}
B
Benjamin Pasero 已提交
727
			}));
R
Rob Lourens 已提交
728
		this._register(this.tree.onContextMenu(e => this.onContextMenu(e)));
729 730 731
		this._register(this.tree.onDidChangeCollapseState(() =>
			this.toggleCollapseStateDelayer.trigger(() => this.toggleCollapseAction.onTreeCollapseStateChange())
		));
R
Rob Lourens 已提交
732

733
		const resourceNavigator = this._register(new TreeResourceNavigator(this.tree, { openOnFocus: true, openOnSelection: false }));
J
Joao Moreno 已提交
734
		this._register(Event.debounce(resourceNavigator.onDidOpenResource, (last, event) => event, 75, true)(options => {
735
			if (options.element instanceof Match) {
736
				const selectedMatch: Match = options.element;
737 738
				if (this.currentSelectedFileMatch) {
					this.currentSelectedFileMatch.setSelectedMatch(null);
739
				}
740 741
				this.currentSelectedFileMatch = selectedMatch.parent();
				this.currentSelectedFileMatch.setSelectedMatch(selectedMatch);
R
Rob Lourens 已提交
742

R
Rob Lourens 已提交
743
				this.onFocus(selectedMatch, options.editorOptions.preserveFocus, options.sideBySide, options.editorOptions.pinned);
744 745
			}
		}));
S
Sandeep Somavarapu 已提交
746

J
Joao Moreno 已提交
747
		this._register(Event.any<any>(this.tree.onDidFocus, this.tree.onDidChangeFocus)(() => {
748
			if (this.tree.isDOMFocused()) {
R
Rob Lourens 已提交
749 750
				const focus = this.tree.getFocus()[0];
				this.firstMatchFocused.set(this.tree.navigate().first() === focus);
751 752 753 754 755
				this.fileMatchOrMatchFocused.set(!!focus);
				this.fileMatchFocused.set(focus instanceof FileMatch);
				this.folderMatchFocused.set(focus instanceof FolderMatch);
				this.matchFocused.set(focus instanceof Match);
				this.fileMatchOrFolderMatchFocus.set(focus instanceof FileMatch || focus instanceof FolderMatch);
756
				this.fileMatchOrFolderMatchWithResourceFocus.set(focus instanceof FileMatch || focus instanceof FolderMatchWithResource);
757 758 759
			}
		}));

760
		this._register(this.tree.onDidBlur(() => {
761 762 763 764 765 766
			this.firstMatchFocused.reset();
			this.fileMatchOrMatchFocused.reset();
			this.fileMatchFocused.reset();
			this.folderMatchFocused.reset();
			this.matchFocused.reset();
			this.fileMatchOrFolderMatchFocus.reset();
767
			this.fileMatchOrFolderMatchWithResourceFocus.reset();
768
		}));
E
Erich Gamma 已提交
769 770
	}

M
Matt Bierner 已提交
771
	private onContextMenu(e: ITreeContextMenuEvent<RenderableMatch | null>): void {
R
Rob Lourens 已提交
772 773 774 775 776 777 778
		if (!this.contextMenu) {
			this.contextMenu = this._register(this.menuService.createMenu(MenuId.SearchContext, this.contextKeyService));
		}

		e.browserEvent.preventDefault();
		e.browserEvent.stopPropagation();

779 780 781
		const actions: IAction[] = [];
		const actionsDisposable = createAndFillInContextMenuActions(this.contextMenu, { shouldForwardArgs: true }, actions, this.contextMenuService);

R
Rob Lourens 已提交
782 783
		this.contextMenuService.showContextMenu({
			getAnchor: () => e.anchor,
784 785 786
			getActions: () => actions,
			getActionsContext: () => e.element,
			onHide: () => dispose(actionsDisposable)
R
Rob Lourens 已提交
787 788 789
		});
	}

R
Rob Lourens 已提交
790
	selectNextMatch(): void {
M
Matt Bierner 已提交
791
		const [selected] = this.tree.getSelection();
792

R
Rob Lourens 已提交
793
		// Expand the initial selected node, if needed
J
jeanp413 已提交
794
		if (selected && !(selected instanceof Match)) {
R
Rob Lourens 已提交
795 796
			if (this.tree.isCollapsed(selected)) {
				this.tree.expand(selected);
R
Rob Lourens 已提交
797 798 799
			}
		}

R
Rob Lourens 已提交
800
		let navigator = this.tree.navigate(selected);
R
Rob Lourens 已提交
801 802

		let next = navigator.next();
803
		if (!next) {
R
Rob Lourens 已提交
804
			next = navigator.first();
805 806
		}

J
jeanp413 已提交
807
		// Expand until first child is a Match
R
Rob Lourens 已提交
808
		while (next && !(next instanceof Match)) {
R
Rob Lourens 已提交
809 810
			if (this.tree.isCollapsed(next)) {
				this.tree.expand(next);
811 812
			}

J
jeanp413 已提交
813
			// Select the first child
814
			next = navigator.next();
815 816 817
		}

		// Reveal the newly selected element
818
		if (next) {
819 820 821
			if (next === selected) {
				this.tree.setFocus([]);
			}
822
			this.tree.setFocus([next], getSelectionKeyboardEvent(undefined, false));
823 824
			this.tree.reveal(next);
		}
825 826
	}

R
Rob Lourens 已提交
827
	selectPreviousMatch(): void {
M
Matt Bierner 已提交
828
		const [selected] = this.tree.getSelection();
R
Rob Lourens 已提交
829
		let navigator = this.tree.navigate(selected);
830

831
		let prev = navigator.previous();
832

J
jeanp413 已提交
833 834 835 836
		// Select previous until find a Match or a collapsed item
		while (!prev || (!(prev instanceof Match) && !this.tree.isCollapsed(prev))) {
			prev = prev ? navigator.previous() : navigator.last();
		}
R
Rob Lourens 已提交
837

J
jeanp413 已提交
838 839 840 841 842 843
		// Expand until last child is a Match
		while (!(prev instanceof Match)) {
			const nextItem = navigator.next();
			this.tree.expand(prev);
			navigator = this.tree.navigate(nextItem); // recreate navigator because modifying the tree can invalidate it
			prev = nextItem ? navigator.previous() : navigator.last(); // select last child
844 845 846
		}

		// Reveal the newly selected element
847
		if (prev) {
848 849 850
			if (prev === selected) {
				this.tree.setFocus([]);
			}
851
			this.tree.setFocus([prev], getSelectionKeyboardEvent(undefined, false));
R
Rob Lourens 已提交
852
			this.tree.reveal(prev);
853
		}
854 855
	}

R
Rob Lourens 已提交
856
	moveFocusToResults(): void {
Y
Yogesh 已提交
857 858 859
		this.tree.domFocus();
	}

R
Rob Lourens 已提交
860
	focus(): void {
E
Erich Gamma 已提交
861 862
		super.focus();

R
Rob Lourens 已提交
863 864 865 866
		const updatedText = this.updateTextFromSelection();
		this.searchWidget.focus(undefined, undefined, updatedText);
	}

R
Rob Lourens 已提交
867
	updateTextFromSelection(allowUnselectedWord = true): boolean {
868
		let updatedText = false;
U
Ubuntu 已提交
869
		const seedSearchStringFromSelection = this.configurationService.getValue<IEditorOptions>('editor').find!.seedSearchStringFromSelection;
870
		if (seedSearchStringFromSelection) {
R
Rob Lourens 已提交
871
			let selectedText = this.getSearchTextFromEditor(allowUnselectedWord);
872
			if (selectedText) {
873 874 875
				if (this.searchWidget.searchInput.getRegex()) {
					selectedText = strings.escapeRegExpCharacters(selectedText);
				}
876 877 878
				this.pauseSearching = true;
				this.searchWidget.setValue(selectedText);
				this.pauseSearching = false;
879
				updatedText = true;
880
				if (this.searchConfig.searchOnType) { this.triggerQueryChange(); }
881
			}
882
		}
R
Rob Lourens 已提交
883

R
Rob Lourens 已提交
884
		return updatedText;
885 886
	}

R
Rob Lourens 已提交
887
	focusNextInputBox(): void {
888
		if (this.searchWidget.searchInputHasFocus()) {
889 890 891 892 893
			if (this.searchWidget.isReplaceShown()) {
				this.searchWidget.focus(true, true);
			} else {
				this.moveFocusFromSearchOrReplace();
			}
894 895 896 897
			return;
		}

		if (this.searchWidget.replaceInputHasFocus()) {
898
			this.moveFocusFromSearchOrReplace();
899 900 901 902
			return;
		}

		if (this.inputPatternIncludes.inputHasFocus()) {
903 904 905 906 907 908
			this.inputPatternExcludes.focus();
			this.inputPatternExcludes.select();
			return;
		}

		if (this.inputPatternExcludes.inputHasFocus()) {
909 910 911 912 913
			this.selectTreeIfNotSelected();
			return;
		}
	}

914 915
	private moveFocusFromSearchOrReplace() {
		if (this.showsFileTypes()) {
916
			this.toggleQueryDetails(true, this.showsFileTypes());
917 918 919 920 921
		} else {
			this.selectTreeIfNotSelected();
		}
	}

R
Rob Lourens 已提交
922
	focusPreviousInputBox(): void {
923 924 925 926 927 928 929 930 931 932 933 934 935 936
		if (this.searchWidget.searchInputHasFocus()) {
			return;
		}

		if (this.searchWidget.replaceInputHasFocus()) {
			this.searchWidget.focus(true);
			return;
		}

		if (this.inputPatternIncludes.inputHasFocus()) {
			this.searchWidget.focus(true, true);
			return;
		}

937 938 939 940 941 942
		if (this.inputPatternExcludes.inputHasFocus()) {
			this.inputPatternIncludes.focus();
			this.inputPatternIncludes.select();
			return;
		}

S
Sandeep Somavarapu 已提交
943 944 945 946
		if (this.tree.isDOMFocused()) {
			this.moveFocusFromResults();
			return;
		}
947 948
	}

S
Sandeep Somavarapu 已提交
949
	private moveFocusFromResults(): void {
950
		if (this.showsFileTypes()) {
951
			this.toggleQueryDetails(true, true, false, true);
952 953 954
		} else {
			this.searchWidget.focus(true, true);
		}
E
Erich Gamma 已提交
955 956 957 958 959 960 961
	}

	private reLayout(): void {
		if (this.isDisposed) {
			return;
		}

962
		const actionsPosition = this.searchConfig.actionsPosition;
963 964
		dom.toggleClass(this.getContainer(), SearchView.ACTIONS_RIGHT_CLASS_NAME, actionsPosition === 'right');
		dom.toggleClass(this.getContainer(), SearchView.WIDE_CLASS_NAME, this.size.width >= SearchView.WIDE_VIEW_SIZE);
965

966
		this.searchWidget.setWidth(this.size.width - 28 /* container margin */);
E
Erich Gamma 已提交
967

968
		this.inputPatternExcludes.setWidth(this.size.width - 28 /* container margin */);
S
Sandeep Somavarapu 已提交
969
		this.inputPatternIncludes.setWidth(this.size.width - 28 /* container margin */);
E
Erich Gamma 已提交
970

971 972 973 974
		const messagesSize = this.messagesElement.style.display === 'none' ?
			0 :
			dom.getTotalHeight(this.messagesElement);

975
		const searchResultContainerHeight = this.size.height -
976
			messagesSize -
977
			dom.getTotalHeight(this.searchWidgetsContainerElement);
978

979
		this.resultsElement.style.height = searchResultContainerHeight + 'px';
E
Erich Gamma 已提交
980

981
		this.tree.layout(searchResultContainerHeight, this.size.width);
E
Erich Gamma 已提交
982 983
	}

984 985
	protected layoutBody(height: number, width: number): void {
		this.size = new dom.Dimension(width, height);
B
Benjamin Pasero 已提交
986
		this.reLayout();
E
Erich Gamma 已提交
987 988
	}

R
Rob Lourens 已提交
989
	getControl() {
E
Erich Gamma 已提交
990 991 992
		return this.tree;
	}

993 994
	isSlowSearch(): boolean {
		return this.state === SearchUIState.SlowSearch;
995 996
	}

R
Rob Lourens 已提交
997
	allSearchFieldsClear(): boolean {
998
		return this.searchWidget.getReplaceValue() === '' &&
999
			this.searchWidget.searchInput.getValue() === '';
1000 1001
	}

R
Rob Lourens 已提交
1002
	hasSearchResults(): boolean {
S
Sandeep Somavarapu 已提交
1003 1004 1005
		return !this.viewModel.searchResult.isEmpty();
	}

1006
	hasSearchPattern(): boolean {
1007
		return this.searchWidget && this.searchWidget.searchInput.getValue().length > 0;
1008 1009
	}

1010
	clearSearchResults(clearInput = true): void {
1011
		this.viewModel.searchResult.clear();
J
Jackson Kearl 已提交
1012
		this.showEmptyStage(true);
1013
		if (this.contextService.getWorkbenchState() === WorkbenchState.EMPTY) {
1014
			this.showSearchWithoutFolderMessage();
1015
		}
1016 1017 1018
		if (clearInput) {
			this.searchWidget.clear();
		}
S
Sandeep Somavarapu 已提交
1019
		this.viewModel.cancelSearch();
1020
		this.updateActions();
R
Rob Lourens 已提交
1021 1022

		aria.status(nls.localize('ariaSearchResultsClearStatus', "The search results have been cleared"));
E
Erich Gamma 已提交
1023 1024
	}

1025
	cancelSearch(focus: boolean = true): boolean {
S
Sandeep Somavarapu 已提交
1026
		if (this.viewModel.cancelSearch()) {
1027
			if (focus) { this.searchWidget.focus(); }
1028 1029 1030 1031 1032
			return true;
		}
		return false;
	}

1033
	private selectTreeIfNotSelected(): void {
R
Rob Lourens 已提交
1034
		if (this.tree.getNode(null)) {
1035
			this.tree.domFocus();
1036
			const selection = this.tree.getSelection();
E
Erich Gamma 已提交
1037 1038 1039 1040 1041 1042
			if (selection.length === 0) {
				this.tree.focusNext();
			}
		}
	}

M
Matt Bierner 已提交
1043
	private getSearchTextFromEditor(allowUnselectedWord: boolean): string | null {
B
Benjamin Pasero 已提交
1044
		if (!this.editorService.activeEditor) {
E
Erich Gamma 已提交
1045 1046 1047
			return null;
		}

R
Rob Lourens 已提交
1048 1049 1050 1051
		if (dom.isAncestor(document.activeElement, this.getContainer())) {
			return null;
		}

1052 1053 1054 1055
		let activeTextEditorWidget = this.editorService.activeTextEditorWidget;
		if (isDiffEditor(activeTextEditorWidget)) {
			if (activeTextEditorWidget.getOriginalEditor().hasTextFocus()) {
				activeTextEditorWidget = activeTextEditorWidget.getOriginalEditor();
1056
			} else {
1057
				activeTextEditorWidget = activeTextEditorWidget.getModifiedEditor();
1058 1059 1060
			}
		}

M
Matt Bierner 已提交
1061
		if (!isCodeEditor(activeTextEditorWidget) || !activeTextEditorWidget.hasModel()) {
E
Erich Gamma 已提交
1062 1063 1064
			return null;
		}

1065
		const range = activeTextEditorWidget.getSelection();
R
Rob Lourens 已提交
1066 1067 1068 1069
		if (!range) {
			return null;
		}

R
Rob Lourens 已提交
1070
		if (range.isEmpty() && !this.searchWidget.searchInput.getValue() && allowUnselectedWord) {
1071
			const wordAtPosition = activeTextEditorWidget.getModel().getWordAtPosition(range.getStartPosition());
R
Rob Lourens 已提交
1072 1073 1074 1075 1076
			if (wordAtPosition) {
				return wordAtPosition.word;
			}
		}

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
		if (!range.isEmpty()) {
			let searchText = '';
			for (let i = range.startLineNumber; i <= range.endLineNumber; i++) {
				let lineText = activeTextEditorWidget.getModel().getLineContent(i);
				if (i === range.endLineNumber) {
					lineText = lineText.substring(0, range.endColumn - 1);
				}

				if (i === range.startLineNumber) {
					lineText = lineText.substring(range.startColumn - 1);
				}

				if (i !== range.startLineNumber) {
					lineText = '\n' + lineText;
				}

				searchText += lineText;
			}

1096
			return searchText;
E
Erich Gamma 已提交
1097
		}
1098

E
Erich Gamma 已提交
1099 1100 1101 1102 1103 1104 1105
		return null;
	}

	private showsFileTypes(): boolean {
		return dom.hasClass(this.queryDetails, 'more');
	}

R
Rob Lourens 已提交
1106
	toggleCaseSensitive(): void {
S
Sandeep Somavarapu 已提交
1107
		this.searchWidget.searchInput.setCaseSensitive(!this.searchWidget.searchInput.getCaseSensitive());
1108
		this.triggerQueryChange();
S
Sandeep Somavarapu 已提交
1109 1110
	}

R
Rob Lourens 已提交
1111
	toggleWholeWords(): void {
S
Sandeep Somavarapu 已提交
1112
		this.searchWidget.searchInput.setWholeWords(!this.searchWidget.searchInput.getWholeWords());
1113
		this.triggerQueryChange();
S
Sandeep Somavarapu 已提交
1114 1115
	}

R
Rob Lourens 已提交
1116
	toggleRegex(): void {
S
Sandeep Somavarapu 已提交
1117
		this.searchWidget.searchInput.setRegex(!this.searchWidget.searchInput.getRegex());
1118
		this.triggerQueryChange();
S
Sandeep Somavarapu 已提交
1119 1120
	}

1121 1122 1123
	setSearchParameters(args: IFindInFilesArgs = {}): void {
		if (typeof args.isCaseSensitive === 'boolean') {
			this.searchWidget.searchInput.setCaseSensitive(args.isCaseSensitive);
1124
		}
1125 1126
		if (typeof args.matchWholeWord === 'boolean') {
			this.searchWidget.searchInput.setWholeWords(args.matchWholeWord);
1127
		}
1128 1129
		if (typeof args.isRegex === 'boolean') {
			this.searchWidget.searchInput.setRegex(args.isRegex);
1130
		}
1131 1132
		if (typeof args.filesToInclude === 'string') {
			this.searchIncludePattern.setValue(String(args.filesToInclude));
1133
		}
1134 1135
		if (typeof args.filesToExclude === 'string') {
			this.searchExcludePattern.setValue(String(args.filesToExclude));
1136
		}
1137 1138
		if (typeof args.query === 'string') {
			this.searchWidget.searchInput.setValue(args.query);
1139
		}
1140
		if (typeof args.replace === 'string') {
J
jeanp413 已提交
1141
			this.searchWidget.replaceInput.setValue(args.replace);
J
jwikman 已提交
1142
		} else {
J
jeanp413 已提交
1143 1144
			if (this.searchWidget.replaceInput.getValue() !== '') {
				this.searchWidget.replaceInput.setValue('');
J
jwikman 已提交
1145
			}
1146
		}
1147
		if (typeof args.triggerSearch === 'boolean' && args.triggerSearch) {
1148
			this.triggerQueryChange();
1149 1150 1151
		}
	}

R
Rob Lourens 已提交
1152
	toggleQueryDetails(moveFocus = true, show?: boolean, skipLayout?: boolean, reverse?: boolean): void {
1153
		const cls = 'more';
E
Erich Gamma 已提交
1154
		show = typeof show === 'undefined' ? !dom.hasClass(this.queryDetails, cls) : Boolean(show);
B
Benjamin Pasero 已提交
1155
		this.viewletState['query.queryDetailsExpanded'] = show;
E
Erich Gamma 已提交
1156 1157 1158
		skipLayout = Boolean(skipLayout);

		if (show) {
1159
			this.toggleQueryDetailsButton.setAttribute('aria-expanded', 'true');
E
Erich Gamma 已提交
1160
			dom.addClass(this.queryDetails, cls);
1161
			if (moveFocus) {
1162 1163 1164 1165 1166 1167 1168
				if (reverse) {
					this.inputPatternExcludes.focus();
					this.inputPatternExcludes.select();
				} else {
					this.inputPatternIncludes.focus();
					this.inputPatternIncludes.select();
				}
1169
			}
E
Erich Gamma 已提交
1170
		} else {
1171
			this.toggleQueryDetailsButton.setAttribute('aria-expanded', 'false');
E
Erich Gamma 已提交
1172
			dom.removeClass(this.queryDetails, cls);
1173
			if (moveFocus) {
1174
				this.searchWidget.focus();
1175
			}
E
Erich Gamma 已提交
1176
		}
B
Benjamin Pasero 已提交
1177

E
Erich Gamma 已提交
1178
		if (!skipLayout && this.size) {
1179
			this.layout(this.size.height);
E
Erich Gamma 已提交
1180 1181 1182
		}
	}

U
Ubuntu 已提交
1183
	searchInFolders(resources?: URI[]): void {
1184
		const folderPaths: string[] = [];
1185
		const workspace = this.contextService.getWorkspace();
1186 1187 1188

		if (resources) {
			resources.forEach(resource => {
1189
				let folderPath: string | undefined;
1190 1191
				if (this.contextService.getWorkbenchState() === WorkbenchState.FOLDER) {
					// Show relative path from the root for single-root mode
1192
					folderPath = relativePath(workspace.folders[0].uri, resource); // always uses forward slashes
1193 1194 1195 1196 1197 1198
					if (folderPath && folderPath !== '.') {
						folderPath = './' + folderPath;
					}
				} else {
					const owningFolder = this.contextService.getWorkspaceFolder(resource);
					if (owningFolder) {
1199
						const owningRootName = owningFolder.name;
1200 1201

						// If this root is the only one with its basename, use a relative ./ path. If there is another, use an absolute path
1202
						const isUniqueFolder = workspace.folders.filter(folder => folder.name === owningRootName).length === 1;
1203
						if (isUniqueFolder) {
1204 1205
							const relPath = relativePath(owningFolder.uri, resource); // always uses forward slashes
							if (relPath === '') {
1206 1207
								folderPath = `./${owningFolder.name}`;
							} else {
R
Rob Lourens 已提交
1208
								folderPath = `./${owningFolder.name}/${relPath}`;
1209
							}
1210
						} else {
1211
							folderPath = resource.fsPath; // TODO rob: handle on-file URIs
1212
						}
1213 1214
					}
				}
1215 1216 1217 1218 1219

				if (folderPath) {
					folderPaths.push(folderPath);
				}
			});
S
Sandeep Somavarapu 已提交
1220 1221
		}

1222
		if (!folderPaths.length || folderPaths.some(folderPath => folderPath === '.')) {
S
Sandeep Somavarapu 已提交
1223 1224 1225 1226 1227
			this.inputPatternIncludes.setValue('');
			this.searchWidget.focus();
			return;
		}

1228
		// Show 'files to include' box
E
Erich Gamma 已提交
1229
		if (!this.showsFileTypes()) {
1230
			this.toggleQueryDetails(true, true);
E
Erich Gamma 已提交
1231
		}
B
Benjamin Pasero 已提交
1232

1233
		this.inputPatternIncludes.setValue(folderPaths.join(', '));
B
Benjamin Pasero 已提交
1234
		this.searchWidget.focus(false);
E
Erich Gamma 已提交
1235 1236
	}

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
	triggerQueryChange(_options?: { preserveFocus?: boolean, triggeredOnType?: boolean, delay?: number }) {
		const options = { preserveFocus: true, triggeredOnType: false, delay: 0, ..._options };

		if (!this.pauseSearching) {
			this.triggerQueryDelayer.trigger(() => {
				this._onQueryChanged(options.preserveFocus, options.triggeredOnType);
			}, options.delay);
		}
	}

	private _onQueryChanged(preserveFocus: boolean, triggeredOnType = false): void {
1248 1249 1250 1251
		if (!this.searchWidget.searchInput.inputBox.isInputValid()) {
			return;
		}

1252 1253 1254 1255
		const isRegex = this.searchWidget.searchInput.getRegex();
		const isWholeWords = this.searchWidget.searchInput.getWholeWords();
		const isCaseSensitive = this.searchWidget.searchInput.getCaseSensitive();
		const contentPattern = this.searchWidget.searchInput.getValue();
1256 1257 1258
		const excludePatternText = this.inputPatternExcludes.getValue().trim();
		const includePatternText = this.inputPatternIncludes.getValue().trim();
		const useExcludesAndIgnoreFiles = this.inputPatternExcludes.useExcludesAndIgnoreFiles();
E
Erich Gamma 已提交
1259 1260

		if (contentPattern.length === 0) {
1261
			this.clearSearchResults(false);
J
Jackson Kearl 已提交
1262
			this.clearMessage();
E
Erich Gamma 已提交
1263 1264 1265
			return;
		}

1266
		const content: IPatternInfo = {
E
Erich Gamma 已提交
1267 1268 1269
			pattern: contentPattern,
			isRegExp: isRegex,
			isCaseSensitive: isCaseSensitive,
R
Rob Lourens 已提交
1270
			isWordMatch: isWholeWords
E
Erich Gamma 已提交
1271 1272
		};

1273 1274
		const excludePattern = this.inputPatternExcludes.getValue();
		const includePattern = this.inputPatternIncludes.getValue();
E
Erich Gamma 已提交
1275

R
Rob Lourens 已提交
1276 1277 1278
		// Need the full match line to correctly calculate replace text, if this is a search/replace with regex group references ($1, $2, ...).
		// 10000 chars is enough to avoid sending huge amounts of text around, if you do a replace with a longer match, it may or may not resolve the group refs correctly.
		// https://github.com/Microsoft/vscode/issues/58374
1279
		const charsPerLine = content.isRegExp ? 10000 : 1000;
R
Rob Lourens 已提交
1280

1281
		const options: ITextQueryBuilderOptions = {
R
Rob Lourens 已提交
1282
			_reason: 'searchView',
R
Rob Lourens 已提交
1283
			extraFileResources: this.instantiationService.invokeFunction(getOutOfWorkspaceEditorResources),
1284
			maxResults: SearchView.MAX_TEXT_RESULTS,
R
Rob Lourens 已提交
1285 1286
			disregardIgnoreFiles: !useExcludesAndIgnoreFiles || undefined,
			disregardExcludeSettings: !useExcludesAndIgnoreFiles || undefined,
R
Rob Lourens 已提交
1287
			excludePattern,
1288 1289
			includePattern,
			previewOptions: {
R
Rob Lourens 已提交
1290 1291
				matchLines: 1,
				charsPerLine
R
Rob Lourens 已提交
1292
			},
1293
			isSmartCase: this.searchConfig.smartCase,
1294
			expandPatterns: true
E
Erich Gamma 已提交
1295
		};
S
Sandeep Somavarapu 已提交
1296
		const folderResources = this.contextService.getWorkspace().folders;
1297 1298 1299 1300 1301 1302

		const onQueryValidationError = (err: Error) => {
			this.searchWidget.searchInput.showMessage({ content: err.message, type: MessageType.ERROR });
			this.viewModel.searchResult.clear();
		};

1303
		let query: ITextQuery;
1304
		try {
1305
			query = this.queryBuilder.text(content, folderResources.map(folder => folder.uri), options);
1306 1307
		} catch (err) {
			onQueryValidationError(err);
1308
			return;
1309
		}
1310

1311
		this.validateQuery(query).then(() => {
1312
			this.onQueryTriggered(query, options, excludePatternText, includePatternText, triggeredOnType);
1313

1314
			if (!preserveFocus) {
1315
				this.searchWidget.focus(false, undefined, true); // focus back to input field
1316
			}
1317
		}, onQueryValidationError);
1318 1319
	}

J
Johannes Rieken 已提交
1320
	private validateQuery(query: ITextQuery): Promise<void> {
1321 1322 1323
		// Validate folderQueries
		const folderQueriesExistP =
			query.folderQueries.map(fq => {
B
Benjamin Pasero 已提交
1324
				return this.fileService.exists(fq.folder);
1325 1326
			});

1327
		return Promise.all(folderQueriesExistP).then(existResults => {
R
Rob Lourens 已提交
1328 1329
			// If no folders exist, show an error message about the first one
			const existingFolderQueries = query.folderQueries.filter((folderQuery, i) => existResults[i]);
1330
			if (!query.folderQueries.length || existingFolderQueries.length) {
R
Rob Lourens 已提交
1331 1332 1333
				query.folderQueries = existingFolderQueries;
			} else {
				const nonExistantPath = query.folderQueries[0].folder.fsPath;
1334
				const searchPathNotFoundError = nls.localize('searchPathNotFoundError', "Search path not found: {0}", nonExistantPath);
1335
				return Promise.reject(new Error(searchPathNotFoundError));
1336 1337 1338 1339
			}

			return undefined;
		});
E
Erich Gamma 已提交
1340 1341
	}

1342
	private onQueryTriggered(query: ITextQuery, options: ITextQueryBuilderOptions, excludePatternText: string, includePatternText: string, triggeredOnType: boolean): void {
1343 1344 1345 1346 1347
		this.addToSearchHistoryDelayer.trigger(() => {
			this.searchWidget.searchInput.onSearchSubmit();
			this.inputPatternExcludes.onSearchSubmit();
			this.inputPatternIncludes.onSearchSubmit();
		});
1348

S
Sandeep Somavarapu 已提交
1349 1350
		this.viewModel.cancelSearch();

1351
		this.currentSearchQ = this.currentSearchQ
1352
			.then(() => this.doSearch(query, excludePatternText, includePatternText, triggeredOnType))
J
Johannes Rieken 已提交
1353
			.then(() => undefined, () => undefined);
1354 1355
	}

1356
	private doSearch(query: ITextQuery, excludePatternText: string, includePatternText: string, triggeredOnType: boolean): Thenable<void> {
1357
		let progressComplete: () => void;
1358
		this.progressService.withProgress({ location: this.getProgressLocation(), delay: triggeredOnType ? 300 : 0 }, _progress => {
1359 1360
			return new Promise(resolve => progressComplete = resolve);
		});
E
Erich Gamma 已提交
1361

1362
		this.searchWidget.searchInput.clearMessage();
1363
		this.state = SearchUIState.Searching;
E
Erich Gamma 已提交
1364 1365
		this.showEmptyStage();

1366 1367 1368 1369 1370
		const slowTimer = setTimeout(() => {
			this.state = SearchUIState.SlowSearch;
			this.updateActions();
		}, 2000);

1371
		const onComplete = (completed?: ISearchComplete) => {
1372 1373
			clearTimeout(slowTimer);
			this.state = SearchUIState.Idle;
1374 1375

			// Complete up to 100% as needed
1376
			progressComplete();
E
Erich Gamma 已提交
1377

1378
			// Do final render, then expand if just 1 file with less than 50 matches
R
Rob Lourens 已提交
1379
			this.onSearchResultsChanged();
1380

1381
			const collapseResults = this.searchConfig.collapseResults;
1382
			if (collapseResults !== 'alwaysCollapse' && this.viewModel.searchResult.matches().length === 1) {
R
Rob Lourens 已提交
1383 1384 1385
				const onlyMatch = this.viewModel.searchResult.matches()[0];
				if (onlyMatch.count() < 50) {
					this.tree.expand(onlyMatch);
1386
				}
R
Rob Lourens 已提交
1387
			}
1388

R
Rob Lourens 已提交
1389
			this.viewModel.replaceString = this.searchWidget.getReplaceValue();
E
Erich Gamma 已提交
1390

R
Rob Lourens 已提交
1391
			this.updateActions();
1392
			const hasResults = !this.viewModel.searchResult.isEmpty();
E
Erich Gamma 已提交
1393

R
Rob Lourens 已提交
1394 1395 1396 1397 1398 1399
			if (completed && completed.limitHit) {
				this.searchWidget.searchInput.showMessage({
					content: nls.localize('searchMaxResultsWarning', "The result set only contains a subset of all matches. Please be more specific in your search to narrow down the results."),
					type: MessageType.WARNING
				});
			}
E
Erich Gamma 已提交
1400

R
Rob Lourens 已提交
1401
			if (!hasResults) {
1402 1403
				const hasExcludes = !!excludePatternText;
				const hasIncludes = !!includePatternText;
R
Rob Lourens 已提交
1404 1405 1406
				let message: string;

				if (!completed) {
J
Jackson Kearl 已提交
1407
					message = SEARCH_CANCELLED_MESSAGE;
R
Rob Lourens 已提交
1408 1409 1410 1411 1412 1413 1414
				} else if (hasIncludes && hasExcludes) {
					message = nls.localize('noResultsIncludesExcludes', "No results found in '{0}' excluding '{1}' - ", includePatternText, excludePatternText);
				} else if (hasIncludes) {
					message = nls.localize('noResultsIncludes', "No results found in '{0}' - ", includePatternText);
				} else if (hasExcludes) {
					message = nls.localize('noResultsExcludes', "No results found excluding '{0}' - ", excludePatternText);
				} else {
R
Rob Lourens 已提交
1415
					message = nls.localize('noResultsFound', "No results found. Review your settings for configured exclusions and check your gitignore files - ");
R
Rob Lourens 已提交
1416
				}
1417

R
Rob Lourens 已提交
1418 1419
				// Indicate as status to ARIA
				aria.status(message);
E
Erich Gamma 已提交
1420

R
Rob Lourens 已提交
1421 1422
				const messageEl = this.clearMessage();
				const p = dom.append(messageEl, $('p', undefined, message));
E
Erich Gamma 已提交
1423

R
Rob Lourens 已提交
1424 1425 1426 1427
				if (!completed) {
					const searchAgainLink = dom.append(p, $('a.pointer.prominent', undefined, nls.localize('rerunSearch.message', "Search again")));
					this.messageDisposables.push(dom.addDisposableListener(searchAgainLink, dom.EventType.CLICK, (e: MouseEvent) => {
						dom.EventHelper.stop(e, false);
1428
						this.triggerQueryChange({ preserveFocus: false });
R
Rob Lourens 已提交
1429 1430 1431 1432 1433
					}));
				} else if (hasIncludes || hasExcludes) {
					const searchAgainLink = dom.append(p, $('a.pointer.prominent', { tabindex: 0 }, nls.localize('rerunSearchInAll.message', "Search again in all files")));
					this.messageDisposables.push(dom.addDisposableListener(searchAgainLink, dom.EventType.CLICK, (e: MouseEvent) => {
						dom.EventHelper.stop(e, false);
1434

R
Rob Lourens 已提交
1435 1436
						this.inputPatternExcludes.setValue('');
						this.inputPatternIncludes.setValue('');
1437

1438
						this.triggerQueryChange({ preserveFocus: false });
R
Rob Lourens 已提交
1439
					}));
1440
				} else {
R
Rob Lourens 已提交
1441 1442 1443 1444 1445 1446
					const openSettingsLink = dom.append(p, $('a.pointer.prominent', { tabindex: 0 }, nls.localize('openSettings.message', "Open Settings")));
					this.addClickEvents(openSettingsLink, this.onOpenSettings);
				}

				if (completed) {
					dom.append(p, $('span', undefined, ' - '));
1447

R
Rob Lourens 已提交
1448 1449
					const learnMoreLink = dom.append(p, $('a.pointer.prominent', { tabindex: 0 }, nls.localize('openSettings.learnMore', "Learn More")));
					this.addClickEvents(learnMoreLink, this.onLearnMore);
1450
				}
R
Rob Lourens 已提交
1451 1452 1453 1454

				if (this.contextService.getWorkbenchState() === WorkbenchState.EMPTY) {
					this.showSearchWithoutFolderMessage();
				}
1455
				this.reLayout();
R
Rob Lourens 已提交
1456 1457 1458 1459 1460 1461
			} else {
				this.viewModel.searchResult.toggleHighlights(this.isVisible()); // show highlights

				// Indicate final search result count for ARIA
				aria.status(nls.localize('ariaSearchResultsStatus', "Search returned {0} results in {1} files", this.viewModel.searchResult.count(), this.viewModel.searchResult.fileCount()));
			}
E
Erich Gamma 已提交
1462 1463
		};

1464
		const onError = (e: any) => {
1465 1466
			clearTimeout(slowTimer);
			this.state = SearchUIState.Idle;
1467
			if (errors.isPromiseCanceledError(e)) {
U
Ubuntu 已提交
1468
				return onComplete(undefined);
1469
			} else {
1470
				this.updateActions();
1471
				progressComplete();
1472
				this.searchWidget.searchInput.showMessage({ content: e.message, type: MessageType.ERROR });
1473
				this.viewModel.searchResult.clear();
1474

1475
				return Promise.resolve();
E
Erich Gamma 已提交
1476 1477 1478 1479 1480
			}
		};

		let visibleMatches = 0;

1481 1482
		let updatedActionsForFileCount = false;

E
Erich Gamma 已提交
1483
		// Handle UI updates in an interval to show frequent progress and results
1484
		const uiRefreshHandle: any = setInterval(() => {
1485
			if (this.state === SearchUIState.Idle) {
E
Erich Gamma 已提交
1486 1487 1488 1489 1490
				window.clearInterval(uiRefreshHandle);
				return;
			}

			// Search result tree update
1491 1492 1493
			const fileCount = this.viewModel.searchResult.fileCount();
			if (visibleMatches !== fileCount) {
				visibleMatches = fileCount;
R
Rob Lourens 已提交
1494
				this.refreshAndUpdateCount();
1495
			}
1496 1497 1498

			if (fileCount > 0 && !updatedActionsForFileCount) {
				updatedActionsForFileCount = true;
S
Sandeep Somavarapu 已提交
1499
				this.updateActions();
E
Erich Gamma 已提交
1500
			}
1501
		}, 100);
E
Erich Gamma 已提交
1502

S
Sandeep Somavarapu 已提交
1503
		this.searchWidget.setReplaceAllActionState(false);
1504

1505
		return this.viewModel.search(query)
1506
			.then(onComplete, onError);
E
Erich Gamma 已提交
1507 1508
	}

1509 1510
	private addClickEvents = (element: HTMLElement, handler: (event: any) => void): void => {
		this.messageDisposables.push(dom.addDisposableListener(element, dom.EventType.CLICK, handler));
1511
		this.messageDisposables.push(dom.addDisposableListener(element, dom.EventType.KEY_DOWN, e => {
1512
			const event = new StandardKeyboardEvent(e);
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
			let eventHandled = true;

			if (event.equals(KeyCode.Space) || event.equals(KeyCode.Enter)) {
				handler(e);
			} else {
				eventHandled = false;
			}

			if (eventHandled) {
				event.preventDefault();
				event.stopPropagation();
			}
		}));
1526
	};
1527 1528 1529 1530

	private onOpenSettings = (e: dom.EventLike): void => {
		dom.EventHelper.stop(e, false);

1531
		this.openSettings('.exclude');
1532
	};
1533

1534
	private openSettings(query: string): Promise<IEditor | undefined> {
1535 1536
		const options: ISettingsEditorOptions = { query };
		return this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY ?
1537 1538 1539 1540 1541 1542 1543
			this.preferencesService.openWorkspaceSettings(undefined, options) :
			this.preferencesService.openGlobalSettings(undefined, options);
	}

	private onLearnMore = (e: MouseEvent): void => {
		dom.EventHelper.stop(e, false);

1544
		this.openerService.open(URI.parse('https://go.microsoft.com/fwlink/?linkid=853977'));
1545
	};
1546

1547
	private updateSearchResultCount(disregardExcludesAndIgnores?: boolean): void {
1548
		const fileCount = this.viewModel.searchResult.fileCount();
1549 1550
		this.hasSearchResultsKey.set(fileCount > 0);

1551
		const msgWasHidden = this.messagesElement.style.display === 'none';
1552
		if (fileCount > 0) {
1553
			const messageEl = this.clearMessage();
1554 1555 1556 1557 1558
			let resultMsg = this.buildResultCountMessage(this.viewModel.searchResult.count(), fileCount);
			if (disregardExcludesAndIgnores) {
				resultMsg += nls.localize('useIgnoresAndExcludesDisabled', " - exclude settings and ignore files are disabled");
			}

1559 1560
			if (this.searchConfig.enableSearchEditorPreview) {
				dom.append(messageEl, $('span', undefined, resultMsg + ' - '));
1561
				const span = dom.append(messageEl, $('span'));
1562 1563 1564 1565 1566 1567 1568 1569
				const openInEditorLink = dom.append(span, $('a.pointer.prominent', undefined, nls.localize('openInEditor.message', "Open in editor")));

				openInEditorLink.title = appendKeyBindingLabel(
					nls.localize('openInEditor.tooltip', "Copy current search results to an editor"),
					this.keybindingService.lookupKeybinding(Constants.OpenInEditorCommandId), this.keybindingService);

				this.messageDisposables.push(dom.addDisposableListener(openInEditorLink, dom.EventType.CLICK, (e: MouseEvent) => {
					dom.EventHelper.stop(e, false);
1570
					this.instantiationService.invokeFunction(createEditorFromSearchResult, this.searchResult, this.searchIncludePattern.getValue(), this.searchExcludePattern.getValue());
1571 1572 1573
				}));

			} else {
1574
				dom.append(messageEl, $('span', undefined, resultMsg));
1575
			}
R
Rob Lourens 已提交
1576
			this.reLayout();
1577
		} else if (!msgWasHidden) {
1578
			dom.hide(this.messagesElement);
1579 1580 1581
		}
	}

1582 1583 1584 1585 1586 1587 1588
	private buildResultCountMessage(resultCount: number, fileCount: number): string {
		if (resultCount === 1 && fileCount === 1) {
			return nls.localize('search.file.result', "{0} result in {1} file", resultCount, fileCount);
		} else if (resultCount === 1) {
			return nls.localize('search.files.result', "{0} result in {1} files", resultCount, fileCount);
		} else if (fileCount === 1) {
			return nls.localize('search.file.results', "{0} results in {1} file", resultCount, fileCount);
1589
		} else {
1590
			return nls.localize('search.files.results', "{0} results in {1} files", resultCount, fileCount);
1591 1592 1593
		}
	}

1594 1595 1596 1597
	private showSearchWithoutFolderMessage(): void {
		this.searchWithoutFolderMessageElement = this.clearMessage();

		const textEl = dom.append(this.searchWithoutFolderMessageElement,
R
Rob Lourens 已提交
1598
			$('p', undefined, nls.localize('searchWithoutFolder', "You have not opened or specified a folder. Only open files are currently searched - ")));
1599 1600

		const openFolderLink = dom.append(textEl,
1601
			$('a.pointer.prominent', { tabindex: 0 }, nls.localize('openFolder', "Open Folder")));
1602

J
Joao Moreno 已提交
1603
		const actionRunner = new ActionRunner();
1604 1605 1606
		this.messageDisposables.push(dom.addDisposableListener(openFolderLink, dom.EventType.CLICK, (e: MouseEvent) => {
			dom.EventHelper.stop(e, false);

R
Rob Lourens 已提交
1607 1608 1609 1610
			const action = env.isMacintosh ?
				this.instantiationService.createInstance(OpenFileFolderAction, OpenFileFolderAction.ID, OpenFileFolderAction.LABEL) :
				this.instantiationService.createInstance(OpenFolderAction, OpenFolderAction.ID, OpenFolderAction.LABEL);

J
Joao Moreno 已提交
1611
			actionRunner.run(action).then(() => {
1612 1613 1614 1615
				action.dispose();
			}, err => {
				action.dispose();
				errors.onUnexpectedError(err);
1616
			});
1617
		}));
1618 1619
	}

J
Jackson Kearl 已提交
1620
	private showEmptyStage(forceHideMessages = false): void {
E
Erich Gamma 已提交
1621
		// disable 'result'-actions
S
Sandeep Somavarapu 已提交
1622
		this.updateActions();
E
Erich Gamma 已提交
1623

J
Jackson Kearl 已提交
1624 1625
		const showingCancelled = (this.messagesElement.firstChild?.textContent?.indexOf(SEARCH_CANCELLED_MESSAGE) ?? -1) > -1;

E
Erich Gamma 已提交
1626
		// clean up ui
S
Sandeep Somavarapu 已提交
1627
		// this.replaceService.disposeAllReplacePreviews();
J
Jackson Kearl 已提交
1628 1629 1630 1631 1632
		if (showingCancelled || forceHideMessages || !this.configurationService.getValue<ISearchConfiguration>().search.searchOnType) {
			// when in search to type, don't preemptively hide, as it causes flickering and shifting of the live results
			dom.hide(this.messagesElement);
		}

1633
		dom.show(this.resultsElement);
U
Ubuntu 已提交
1634
		this.currentSelectedFileMatch = undefined;
E
Erich Gamma 已提交
1635 1636
	}

1637
	private onFocus(lineMatch: Match, preserveFocus?: boolean, sideBySide?: boolean, pinned?: boolean): Promise<any> {
1638 1639
		const useReplacePreview = this.configurationService.getValue<ISearchConfiguration>().search.useReplacePreview;
		return (useReplacePreview && this.viewModel.isReplaceActive() && !!this.viewModel.replaceString) ?
R
Rob Lourens 已提交
1640 1641
			this.replaceService.openReplacePreview(lineMatch, preserveFocus, sideBySide, pinned) :
			this.open(lineMatch, preserveFocus, sideBySide, pinned);
1642 1643
	}

U
Ubuntu 已提交
1644
	open(element: FileMatchOrMatch, preserveFocus?: boolean, sideBySide?: boolean, pinned?: boolean): Promise<void> {
R
Rob Lourens 已提交
1645
		const selection = this.getSelectionFrom(element);
1646
		const resource = element instanceof Match ? element.parent().resource : (<FileMatch>element).resource;
E
Erich Gamma 已提交
1647
		return this.editorService.openEditor({
1648
			resource: resource,
E
Erich Gamma 已提交
1649
			options: {
1650 1651
				preserveFocus,
				pinned,
S
Sandeep Somavarapu 已提交
1652
				selection,
1653
				revealIfVisible: true
E
Erich Gamma 已提交
1654
			}
B
Benjamin Pasero 已提交
1655
		}, sideBySide ? SIDE_GROUP : ACTIVE_GROUP).then(editor => {
S
Sandeep Somavarapu 已提交
1656
			if (editor && element instanceof Match && preserveFocus) {
1657
				this.viewModel.searchResult.rangeHighlightDecorations.highlightRange(
U
Ubuntu 已提交
1658
					(<ICodeEditor>editor.getControl()).getModel()!,
1659 1660
					element.range()
				);
S
Sandeep Somavarapu 已提交
1661
			} else {
1662
				this.viewModel.searchResult.rangeHighlightDecorations.removeHighlightRange();
S
Sandeep Somavarapu 已提交
1663 1664
			}
		}, errors.onUnexpectedError);
E
Erich Gamma 已提交
1665 1666
	}

1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
	openEditorWithMultiCursor(element: FileMatchOrMatch): Promise<void> {
		const resource = element instanceof Match ? element.parent().resource : (<FileMatch>element).resource;
		return this.editorService.openEditor({
			resource: resource,
			options: {
				preserveFocus: false,
				pinned: true,
				revealIfVisible: true
			}
		}).then(editor => {
			if (editor) {
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
				let fileMatch = null;
				if (element instanceof FileMatch) {
					fileMatch = element;
				}
				else if (element instanceof Match) {
					fileMatch = element.parent();
				}

				if (fileMatch) {
					const selections = fileMatch.matches().map(m => new Selection(m.range().startLineNumber, m.range().startColumn, m.range().endLineNumber, m.range().endColumn));
					const codeEditor = getCodeEditor(editor.getControl());
					if (codeEditor) {
						let multiCursorController = MultiCursorSelectionController.get(codeEditor);
						multiCursorController.selectAllUsingSelections(selections);
					}
1693 1694 1695 1696 1697 1698
				}
			}
			this.viewModel.searchResult.rangeHighlightDecorations.removeHighlightRange();
		}, errors.onUnexpectedError);
	}

1699
	private getSelectionFrom(element: FileMatchOrMatch): any {
1700
		let match: Match | null = null;
1701
		if (element instanceof Match) {
J
Johannes Rieken 已提交
1702
			match = element;
1703 1704
		}
		if (element instanceof FileMatch && element.count() > 0) {
J
Johannes Rieken 已提交
1705
			match = element.matches()[element.matches().length - 1];
1706 1707
		}
		if (match) {
1708
			const range = match.range();
S
Sandeep Somavarapu 已提交
1709
			if (this.viewModel.isReplaceActive() && !!this.viewModel.replaceString) {
1710
				const replaceString = match.replaceString;
1711 1712
				return {
					startLineNumber: range.startLineNumber,
S
Sandeep Somavarapu 已提交
1713
					startColumn: range.startColumn,
1714
					endLineNumber: range.startLineNumber,
S
Sandeep Somavarapu 已提交
1715
					endColumn: range.startColumn + replaceString.length
1716 1717 1718 1719
				};
			}
			return range;
		}
R
Rob Lourens 已提交
1720
		return undefined;
1721 1722
	}

1723
	private onUntitledDidDispose(resource: URI): void {
E
Erich Gamma 已提交
1724 1725 1726 1727
		if (!this.viewModel) {
			return;
		}

1728
		// remove search results from this resource as it got disposed
1729 1730 1731 1732
		const matches = this.viewModel.searchResult.matches();
		for (let i = 0, len = matches.length; i < len; i++) {
			if (resource.toString() === matches[i].resource.toString()) {
				this.viewModel.searchResult.remove(matches[i]);
E
Erich Gamma 已提交
1733 1734 1735 1736 1737
			}
		}
	}

	private onFilesChanged(e: FileChangesEvent): void {
1738
		if (!this.viewModel || (this.searchConfig.sortOrder !== SearchSortOrder.Modified && !e.gotDeleted())) {
E
Erich Gamma 已提交
1739 1740 1741
			return;
		}

1742
		const matches = this.viewModel.searchResult.matches();
1743 1744
		if (e.gotDeleted()) {
			const deletedMatches = matches.filter(m => e.contains(m.resource, FileChangeType.DELETED));
E
Erich Gamma 已提交
1745

1746 1747 1748 1749
			this.viewModel.searchResult.remove(deletedMatches);
		} else {
			// Check if the changed file contained matches
			const changedMatches = matches.filter(m => e.contains(m.resource));
1750
			if (changedMatches.length && this.searchConfig.sortOrder === SearchSortOrder.Modified) {
1751 1752 1753 1754
				// No matches need to be removed, but modified files need to have their file stat updated.
				this.updateFileStats(changedMatches).then(() => this.refreshTree());
			}
		}
E
Erich Gamma 已提交
1755 1756
	}

R
Rob Lourens 已提交
1757
	getActions(): IAction[] {
1758
		return [
1759
			this.state === SearchUIState.SlowSearch ?
1760 1761
				this.cancelAction :
				this.refreshAction,
1762 1763
			...this.actions,
			this.toggleCollapseAction
1764
		];
1765 1766
	}

1767 1768 1769 1770
	private get searchConfig(): ISearchConfigurationProperties {
		return this.configurationService.getValue<ISearchConfigurationProperties>('search');
	}

1771
	private clearHistory(): void {
1772 1773 1774 1775 1776
		this.searchWidget.clearHistory();
		this.inputPatternExcludes.clearHistory();
		this.inputPatternIncludes.clearHistory();
	}

1777
	public saveState(): void {
A
Amy Qiu 已提交
1778 1779 1780 1781
		const isRegex = this.searchWidget.searchInput.getRegex();
		const isWholeWords = this.searchWidget.searchInput.getWholeWords();
		const isCaseSensitive = this.searchWidget.searchInput.getCaseSensitive();
		const contentPattern = this.searchWidget.searchInput.getValue();
1782
		const patternExcludes = this.inputPatternExcludes.getValue().trim();
A
Amy Qiu 已提交
1783
		const patternIncludes = this.inputPatternIncludes.getValue().trim();
1784
		const useExcludesAndIgnoreFiles = this.inputPatternExcludes.useExcludesAndIgnoreFiles();
1785
		const preserveCase = this.viewModel.preserveCase;
A
Amy Qiu 已提交
1786

B
Benjamin Pasero 已提交
1787 1788 1789 1790 1791 1792 1793
		this.viewletState['query.contentPattern'] = contentPattern;
		this.viewletState['query.regex'] = isRegex;
		this.viewletState['query.wholeWords'] = isWholeWords;
		this.viewletState['query.caseSensitive'] = isCaseSensitive;
		this.viewletState['query.folderExclusions'] = patternExcludes;
		this.viewletState['query.folderIncludes'] = patternIncludes;
		this.viewletState['query.useExcludesAndIgnoreFiles'] = useExcludesAndIgnoreFiles;
1794
		this.viewletState['query.preserveCase'] = preserveCase;
A
Amy Qiu 已提交
1795

B
Benjamin Pasero 已提交
1796 1797
		const isReplaceShown = this.searchAndReplaceWidget.isReplaceShown();
		this.viewletState['view.showReplace'] = isReplaceShown;
1798
		this.viewletState['query.replaceText'] = isReplaceShown && this.searchWidget.getReplaceValue();
B
Benjamin Pasero 已提交
1799

1800 1801
		const history: ISearchHistoryValues = Object.create(null);

1802
		const searchHistory = this.searchWidget.getSearchHistory();
1803 1804 1805 1806
		if (searchHistory && searchHistory.length) {
			history.search = searchHistory;
		}

1807
		const replaceHistory = this.searchWidget.getReplaceHistory();
1808 1809 1810 1811
		if (replaceHistory && replaceHistory.length) {
			history.replace = replaceHistory;
		}

1812
		const patternExcludesHistory = this.inputPatternExcludes.getHistory();
1813 1814 1815 1816
		if (patternExcludesHistory && patternExcludesHistory.length) {
			history.exclude = patternExcludesHistory;
		}

1817
		const patternIncludesHistory = this.inputPatternIncludes.getHistory();
1818 1819 1820
		if (patternIncludesHistory && patternIncludesHistory.length) {
			history.include = patternIncludesHistory;
		}
1821

1822
		this.searchHistoryService.save(history);
B
Benjamin Pasero 已提交
1823

1824 1825
		this.memento.saveMemento();

B
Benjamin Pasero 已提交
1826
		super.saveState();
A
Amy Qiu 已提交
1827 1828
	}

1829
	private async retrieveFileStats(): Promise<void> {
R
rzj17 已提交
1830 1831
		const files = this.searchResult.matches().filter(f => !f.fileStat).map(f => f.resolveFileStat(this.fileService));
		await Promise.all(files);
1832 1833 1834
	}

	private async updateFileStats(elements: FileMatch[]): Promise<void> {
1835
		const files = elements.map(f => f.resolveFileStat(this.fileService));
R
rzj17 已提交
1836
		await Promise.all(files);
1837 1838 1839 1840 1841 1842 1843 1844
	}

	private removeFileStats(): void {
		for (const fileMatch of this.searchResult.matches()) {
			fileMatch.fileStat = undefined;
		}
	}

R
Rob Lourens 已提交
1845
	dispose(): void {
E
Erich Gamma 已提交
1846
		this.isDisposed = true;
1847
		this.saveState();
E
Erich Gamma 已提交
1848 1849
		super.dispose();
	}
1850 1851 1852
}

registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {
B
Benjamin Pasero 已提交
1853
	const matchHighlightColor = theme.getColor(editorFindMatchHighlight);
1854
	if (matchHighlightColor) {
I
isidor 已提交
1855
		collector.addRule(`.monaco-workbench .search-view .findInFileMatch { background-color: ${matchHighlightColor}; }`);
B
Benjamin Pasero 已提交
1856 1857 1858 1859
	}

	const diffInsertedColor = theme.getColor(diffInserted);
	if (diffInsertedColor) {
I
isidor 已提交
1860
		collector.addRule(`.monaco-workbench .search-view .replaceMatch { background-color: ${diffInsertedColor}; }`);
B
Benjamin Pasero 已提交
1861 1862 1863 1864
	}

	const diffRemovedColor = theme.getColor(diffRemoved);
	if (diffRemovedColor) {
I
isidor 已提交
1865
		collector.addRule(`.monaco-workbench .search-view .replace.findInFileMatch { background-color: ${diffRemovedColor}; }`);
B
Benjamin Pasero 已提交
1866 1867 1868 1869
	}

	const diffInsertedOutlineColor = theme.getColor(diffInsertedOutline);
	if (diffInsertedOutlineColor) {
1870
		collector.addRule(`.monaco-workbench .search-view .replaceMatch:not(:empty) { border: 1px ${theme.type === 'hc' ? 'dashed' : 'solid'} ${diffInsertedOutlineColor}; }`);
B
Benjamin Pasero 已提交
1871 1872 1873 1874
	}

	const diffRemovedOutlineColor = theme.getColor(diffRemovedOutline);
	if (diffRemovedOutlineColor) {
1875
		collector.addRule(`.monaco-workbench .search-view .replace.findInFileMatch { border: 1px ${theme.type === 'hc' ? 'dashed' : 'solid'} ${diffRemovedOutlineColor}; }`);
B
Benjamin Pasero 已提交
1876 1877
	}

1878 1879
	const findMatchHighlightBorder = theme.getColor(editorFindMatchHighlightBorder);
	if (findMatchHighlightBorder) {
1880
		collector.addRule(`.monaco-workbench .search-view .findInFileMatch { border: 1px ${theme.type === 'hc' ? 'dashed' : 'solid'} ${findMatchHighlightBorder}; }`);
1881
	}
1882 1883 1884

	const outlineSelectionColor = theme.getColor(listActiveSelectionForeground);
	if (outlineSelectionColor) {
1885
		collector.addRule(`.monaco-workbench .search-view .monaco-list.element-focused .monaco-list-row.focused.selected:not(.highlighted) .action-label:focus { outline-color: ${outlineSelectionColor} }`);
1886
	}
1887 1888 1889 1890 1891 1892

	const foregroundColor = theme.getColor(foreground);
	if (foregroundColor) {
		const fgWithOpacity = new Color(new RGBA(foregroundColor.rgba.r, foregroundColor.rgba.g, foregroundColor.rgba.b, 0.5));
		collector.addRule(`.vs-dark .search-view .message { color: ${fgWithOpacity}; }`);
	}
1893
});