searchEditor.ts 22.6 KB
Newer Older
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 7
import * as DOM from 'vs/base/browser/dom';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
8
import { Delayer } from 'vs/base/common/async';
9 10
import { CancellationToken } from 'vs/base/common/cancellation';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
11
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
12 13
import { URI } from 'vs/base/common/uri';
import 'vs/css!./media/searchEditor';
J
Jackson Kearl 已提交
14
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
15
import { Range } from 'vs/editor/common/core/range';
J
Jackson Kearl 已提交
16
import { TrackedRangeStickiness } from 'vs/editor/common/model';
17
import { IModelService } from 'vs/editor/common/services/modelService';
18
import { ReferencesController } from 'vs/editor/contrib/gotoSymbol/peek/referencesController';
19 20
import { localize } from 'vs/nls';
import { ICommandService } from 'vs/platform/commands/common/commands';
21
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
22 23 24
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ILabelService } from 'vs/platform/label/common/label';
25
import { IEditorProgressService, LongRunningOperation } from 'vs/platform/progress/common/progress';
26 27
import { IStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
28 29
import { inputBorder, registerColor, searchEditorFindMatch, searchEditorFindMatchBorder } from 'vs/platform/theme/common/colorRegistry';
import { attachInputBoxStyler } from 'vs/platform/theme/common/styler';
30
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
31
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
J
Jackson Kearl 已提交
32
import { EditorOptions } from 'vs/workbench/common/editor';
33 34
import { ExcludePatternInputWidget, PatternInputWidget } from 'vs/workbench/contrib/search/browser/patternInputWidget';
import { SearchWidget } from 'vs/workbench/contrib/search/browser/searchWidget';
35
import { InputBoxFocusedKey } from 'vs/workbench/contrib/search/common/constants';
36 37
import { ITextQueryBuilderOptions, QueryBuilder } from 'vs/workbench/contrib/search/common/queryBuilder';
import { getOutOfWorkspaceEditorResources } from 'vs/workbench/contrib/search/common/search';
J
Jackson Kearl 已提交
38
import { SearchModel } from 'vs/workbench/contrib/search/common/searchModel';
39
import { InSearchEditor, SearchEditorFindMatchClass } from 'vs/workbench/contrib/searchEditor/browser/constants';
40 41 42
import type { SearchConfiguration, SearchEditorInput } from 'vs/workbench/contrib/searchEditor/browser/searchEditorInput';
import { extractSearchQuery, serializeSearchConfiguration, serializeSearchResultForEditor } from 'vs/workbench/contrib/searchEditor/browser/searchEditorSerialization';
import { IPatternInfo, ISearchConfigurationProperties, ITextQuery } from 'vs/workbench/services/search/common/search';
43
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
44 45
import { ICodeEditorViewState } from 'vs/editor/common/editorCommon';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
J
Jackson Kearl 已提交
46 47 48 49 50
import { BaseTextEditor } from 'vs/workbench/browser/parts/editor/textEditor';
import { assertIsDefined } from 'vs/base/common/types';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfigurationService';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
51 52

const RESULT_LINE_REGEX = /^(\s+)(\d+)(:| )(\s+)(.*)$/;
53
const FILE_LINE_REGEX = /^(\S.*):$/;
54

J
Jackson Kearl 已提交
55
type SearchEditorViewState = ICodeEditorViewState & { focused: 'input' | 'editor' };
56

J
Jackson Kearl 已提交
57
export class SearchEditor extends BaseTextEditor {
58 59
	static readonly ID: string = 'workbench.editor.searchEditor';

J
Jackson Kearl 已提交
60
	static readonly SEARCH_EDITOR_VIEW_STATE_PREFERENCE_KEY = 'searchEditorViewState';
61

62 63 64 65 66 67 68 69
	private queryEditorWidget!: SearchWidget;
	private searchResultEditor!: CodeEditorWidget;
	private queryEditorContainer!: HTMLElement;
	private dimension?: DOM.Dimension;
	private inputPatternIncludes!: PatternInputWidget;
	private inputPatternExcludes!: ExcludePatternInputWidget;
	private includesExcludesContainer!: HTMLElement;
	private toggleQueryDetailsButton!: HTMLElement;
70
	private messageBox!: HTMLElement;
71 72 73

	private runSearchDelayer = new Delayer(300);
	private pauseSearching: boolean = false;
74
	private showingIncludesExcludes: boolean = false;
75 76
	private inSearchEditorContextKey: IContextKey<boolean>;
	private inputFocusContextKey: IContextKey<boolean>;
77
	private searchOperation: LongRunningOperation;
J
Jackson Kearl 已提交
78
	private searchHistoryDelayer: Delayer<void>;
79
	private messageDisposables: IDisposable[] = [];
80
	private container: HTMLElement;
81 82 83 84 85 86 87 88

	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
		@IThemeService themeService: IThemeService,
		@IStorageService storageService: IStorageService,
		@IModelService private readonly modelService: IModelService,
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
		@ILabelService private readonly labelService: ILabelService,
89
		@IInstantiationService readonly instantiationService: IInstantiationService,
90 91
		@IContextViewService private readonly contextViewService: IContextViewService,
		@ICommandService private readonly commandService: ICommandService,
92
		@IContextKeyService readonly contextKeyService: IContextKeyService,
93
		@IEditorProgressService readonly progressService: IEditorProgressService,
J
Jackson Kearl 已提交
94 95 96 97
		@ITextResourceConfigurationService textResourceService: ITextResourceConfigurationService,
		@IEditorGroupsService protected editorGroupService: IEditorGroupsService,
		@IEditorService protected editorService: IEditorService,
		@IConfigurationService protected configurationService: IConfigurationService,
98
	) {
J
Jackson Kearl 已提交
99
		super(SearchEditor.ID, telemetryService, instantiationService, storageService, textResourceService, themeService, editorService, editorGroupService);
100 101
		this.container = DOM.$('.search-editor');

102

103 104 105 106 107 108
		const scopedContextKeyService = contextKeyService.createScoped(this.container);
		this.instantiationService = instantiationService.createChild(new ServiceCollection([IContextKeyService, scopedContextKeyService]));

		this.inSearchEditorContextKey = InSearchEditor.bindTo(scopedContextKeyService);
		this.inSearchEditorContextKey.set(true);
		this.inputFocusContextKey = InputBoxFocusedKey.bindTo(scopedContextKeyService);
109
		this.searchOperation = this._register(new LongRunningOperation(progressService));
J
Jackson Kearl 已提交
110
		this.searchHistoryDelayer = new Delayer<void>(2000);
111 112 113
	}

	createEditor(parent: HTMLElement) {
114
		DOM.append(parent, this.container);
115

116 117
		this.createQueryEditor(this.container);
		this.createResultsEditor(this.container);
118
	}
119

120 121
	private createQueryEditor(parent: HTMLElement) {
		this.queryEditorContainer = DOM.append(parent, DOM.$('.query-container'));
122 123 124
		this.queryEditorWidget = this._register(this.instantiationService.createInstance(SearchWidget, this.queryEditorContainer, { _hideReplaceToggle: true, showContextToggle: true }));
		this._register(this.queryEditorWidget.onReplaceToggled(() => this.reLayout()));
		this._register(this.queryEditorWidget.onDidHeightChange(() => this.reLayout()));
J
Jackson Kearl 已提交
125 126 127
		this.queryEditorWidget.onSearchSubmit(() => this.runSearch(true, true)); // onSearchSubmit has an internal delayer, so skip over ours.
		this.queryEditorWidget.searchInput.onDidOptionChange(() => this.runSearch(false));
		this.queryEditorWidget.onDidToggleContext(() => this.runSearch(false));
128 129 130

		// Includes/Excludes Dropdown
		this.includesExcludesContainer = DOM.append(this.queryEditorContainer, DOM.$('.includes-excludes'));
131

132 133 134 135
		// // Toggle query details button
		this.toggleQueryDetailsButton = DOM.append(this.includesExcludesContainer, DOM.$('.expand.codicon.codicon-ellipsis', { tabindex: 0, role: 'button', title: localize('moreSearch', "Toggle Search Details") }));
		this._register(DOM.addDisposableListener(this.toggleQueryDetailsButton, DOM.EventType.CLICK, e => {
			DOM.EventHelper.stop(e);
136
			this.toggleIncludesExcludes();
137 138 139 140 141
		}));
		this._register(DOM.addDisposableListener(this.toggleQueryDetailsButton, DOM.EventType.KEY_UP, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
			if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
				DOM.EventHelper.stop(e);
142
				this.toggleIncludesExcludes();
143 144 145 146 147 148 149
			}
		}));
		this._register(DOM.addDisposableListener(this.toggleQueryDetailsButton, DOM.EventType.KEY_DOWN, (e: KeyboardEvent) => {
			const event = new StandardKeyboardEvent(e);
			if (event.equals(KeyMod.Shift | KeyCode.Tab)) {
				if (this.queryEditorWidget.isReplaceActive()) {
					this.queryEditorWidget.focusReplaceAllAction();
150 151
				}
				else {
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
					this.queryEditorWidget.isReplaceShown() ? this.queryEditorWidget.replaceInput.focusOnPreserve() : this.queryEditorWidget.focusRegexAction();
				}
				DOM.EventHelper.stop(e);
			}
		}));

		// // Includes
		const folderIncludesList = DOM.append(this.includesExcludesContainer, DOM.$('.file-types.includes'));
		const filesToIncludeTitle = localize('searchScope.includes', "files to include");
		DOM.append(folderIncludesList, DOM.$('h4', undefined, filesToIncludeTitle));
		this.inputPatternIncludes = this._register(this.instantiationService.createInstance(PatternInputWidget, folderIncludesList, this.contextViewService, {
			ariaLabel: localize('label.includes', 'Search Include Patterns'),
		}));
		this.inputPatternIncludes.onSubmit(_triggeredOnType => this.runSearch());

		// // Excludes
		const excludesList = DOM.append(this.includesExcludesContainer, DOM.$('.file-types.excludes'));
		const excludesTitle = localize('searchScope.excludes', "files to exclude");
		DOM.append(excludesList, DOM.$('h4', undefined, excludesTitle));
		this.inputPatternExcludes = this._register(this.instantiationService.createInstance(ExcludePatternInputWidget, excludesList, this.contextViewService, {
			ariaLabel: localize('label.excludes', 'Search Exclude Patterns'),
		}));
		this.inputPatternExcludes.onSubmit(_triggeredOnType => this.runSearch());
		this.inputPatternExcludes.onChangeIgnoreBox(() => this.runSearch());
176 177 178

		[this.queryEditorWidget.searchInput, this.inputPatternIncludes, this.inputPatternExcludes].map(input =>
			this._register(attachInputBoxStyler(input, this.themeService, { inputBorder: searchEditorTextInputBorder })));
179 180 181 182 183 184 185 186 187 188 189 190

		// Messages
		this.messageBox = DOM.append(this.queryEditorContainer, DOM.$('.messages'));
	}


	private toggleRunAgainMessage(show: boolean) {
		DOM.clearNode(this.messageBox);
		dispose(this.messageDisposables);
		this.messageDisposables = [];

		if (show) {
J
Naming  
Jackson Kearl 已提交
191
			const runAgainLink = DOM.append(this.messageBox, DOM.$('a.pointer.prominent.message', {}, localize('runSearch', "Run Search")));
192 193 194 195 196
			this.messageDisposables.push(DOM.addDisposableListener(runAgainLink, DOM.EventType.CLICK, async () => {
				await this.runSearch(true, true);
				this.toggleRunAgainMessage(false);
			}));
		}
197
	}
198

199
	private createResultsEditor(parent: HTMLElement) {
200
		const searchResultContainer = DOM.append(parent, DOM.$('.search-results'));
J
Jackson Kearl 已提交
201 202
		super.createEditor(searchResultContainer);
		this.searchResultEditor = super.getControl() as CodeEditorWidget;
203 204 205 206 207 208 209 210 211
		this.searchResultEditor.onMouseUp(e => {
			if (e.event.detail === 2) {
				const behaviour = this.configurationService.getValue<ISearchConfigurationProperties>('search').searchEditorPreview.doubleClickBehaviour;
				const position = e.target.position;
				if (position && behaviour !== 'selectWord') {
					const line = this.searchResultEditor.getModel()?.getLineContent(position.lineNumber) ?? '';
					if (line.match(RESULT_LINE_REGEX)) {
						this.searchResultEditor.setSelection(Range.fromPositions(position));
						this.commandService.executeCommand(behaviour === 'goToLocation' ? 'editor.action.goToDeclaration' : 'editor.action.openDeclarationToTheSide');
212 213 214
					} else if (line.match(FILE_LINE_REGEX)) {
						this.searchResultEditor.setSelection(Range.fromPositions(position));
						this.commandService.executeCommand('editor.action.peekDefinition');
215 216 217 218
					}
				}
			}
		});
219

J
Jackson Kearl 已提交
220
		this._register(this.onDidBlur(() => this.saveViewState()));
J
Jackson Kearl 已提交
221

222 223
		this._register(this.searchResultEditor.onKeyDown(e => e.keyCode === KeyCode.Escape && this.queryEditorWidget.searchInput.focus()));

224
		this._register(this.searchResultEditor.onDidChangeModelContent(() => this.getInput()?.setDirty(true)));
225 226 227 228 229 230 231 232

		[this.queryEditorWidget.searchInputFocusTracker, this.queryEditorWidget.replaceInputFocusTracker, this.inputPatternExcludes.inputFocusTracker, this.inputPatternIncludes.inputFocusTracker]
			.map(tracker => {
				this._register(tracker.onDidFocus(() => setTimeout(() => this.inputFocusContextKey.set(true), 0)));
				this._register(tracker.onDidBlur(() => this.inputFocusContextKey.set(false)));
			});
	}

J
Jackson Kearl 已提交
233 234 235 236
	getControl() {
		return this.searchResultEditor;
	}

J
Jackson Kearl 已提交
237
	focus() {
238 239
		const viewState = this.loadViewState();
		if (viewState && viewState.focused === 'editor') {
J
Jackson Kearl 已提交
240 241 242 243
			this.searchResultEditor.focus();
		} else {
			this.queryEditorWidget.focus();
		}
J
Jackson Kearl 已提交
244 245
	}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	focusNextInput() {
		if (this.queryEditorWidget.searchInputHasFocus()) {
			if (this.showingIncludesExcludes) {
				this.inputPatternIncludes.focus();
			} else {
				this.searchResultEditor.focus();
			}
		} else if (this.inputPatternIncludes.inputHasFocus()) {
			this.inputPatternExcludes.focus();
		} else if (this.inputPatternExcludes.inputHasFocus()) {
			this.searchResultEditor.focus();
		} else if (this.searchResultEditor.hasWidgetFocus()) {
			// pass
		}
	}

	focusPrevInput() {
		if (this.queryEditorWidget.searchInputHasFocus()) {
			this.searchResultEditor.focus(); // wrap
		} else if (this.inputPatternIncludes.inputHasFocus()) {
			this.queryEditorWidget.searchInput.focus();
		} else if (this.inputPatternExcludes.inputHasFocus()) {
			this.inputPatternIncludes.focus();
		} else if (this.searchResultEditor.hasWidgetFocus()) {
J
Jackson Kearl 已提交
270
			// unreachable.
271
		}
272 273
	}

274 275
	toggleWholeWords() {
		this.queryEditorWidget.searchInput.setWholeWords(!this.queryEditorWidget.searchInput.getWholeWords());
J
Jackson Kearl 已提交
276
		this.runSearch(false);
277 278 279 280
	}

	toggleRegex() {
		this.queryEditorWidget.searchInput.setRegex(!this.queryEditorWidget.searchInput.getRegex());
J
Jackson Kearl 已提交
281
		this.runSearch(false);
282 283 284 285
	}

	toggleCaseSensitive() {
		this.queryEditorWidget.searchInput.setCaseSensitive(!this.queryEditorWidget.searchInput.getCaseSensitive());
J
Jackson Kearl 已提交
286
		this.runSearch(false);
287 288
	}

289 290 291 292
	toggleContextLines() {
		this.queryEditorWidget.toggleContextLines();
	}

293 294 295 296
	toggleQueryDetails() {
		this.toggleIncludesExcludes();
	}

J
Jackson Kearl 已提交
297
	async runSearch(resetCursor = true, instant = false) {
298
		if (!this.pauseSearching) {
299
			await this.runSearchDelayer.trigger(async () => {
J
Jackson Kearl 已提交
300
				await this.doRunSearch();
301
				this.toggleRunAgainMessage(false);
J
Jackson Kearl 已提交
302 303
				if (resetCursor) {
					this.searchResultEditor.setSelection(new Range(1, 1, 1, 1));
J
Jackson Kearl 已提交
304
					this.searchResultEditor.setScrollPosition({ scrollTop: 0, scrollLeft: 0 });
J
Jackson Kearl 已提交
305 306
				}
			}, instant ? 0 : undefined);
307 308 309
		}
	}

J
Jackson Kearl 已提交
310 311
	private readConfigFromWidget() {
		return {
312 313 314 315 316 317 318
			caseSensitive: this.queryEditorWidget.searchInput.getCaseSensitive(),
			contextLines: this.queryEditorWidget.contextLines(),
			excludes: this.inputPatternExcludes.getValue(),
			includes: this.inputPatternIncludes.getValue(),
			query: this.queryEditorWidget.searchInput.getValue(),
			regexp: this.queryEditorWidget.searchInput.getRegex(),
			wholeWord: this.queryEditorWidget.searchInput.getWholeWords(),
319 320
			useIgnores: this.inputPatternExcludes.useExcludesAndIgnoreFiles(),
			showIncludesExcludes: this.showingIncludesExcludes
321
		};
J
Jackson Kearl 已提交
322 323 324 325 326 327 328 329 330 331 332 333
	}

	private async doRunSearch() {
		const startInput = this.getInput();

		this.searchHistoryDelayer.trigger(() => {
			this.queryEditorWidget.searchInput.onSearchSubmit();
			this.inputPatternExcludes.onSearchSubmit();
			this.inputPatternIncludes.onSearchSubmit();
		});

		const config: SearchConfiguration = this.readConfigFromWidget();
334

335 336
		if (!config.query) { return; }

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
		const content: IPatternInfo = {
			pattern: config.query,
			isRegExp: config.regexp,
			isCaseSensitive: config.caseSensitive,
			isWordMatch: config.wholeWord,
		};

		const options: ITextQueryBuilderOptions = {
			_reason: 'searchEditor',
			extraFileResources: this.instantiationService.invokeFunction(getOutOfWorkspaceEditorResources),
			maxResults: 10000,
			disregardIgnoreFiles: !config.useIgnores,
			disregardExcludeSettings: !config.useIgnores,
			excludePattern: config.excludes,
			includePattern: config.includes,
			previewOptions: {
				matchLines: 1,
				charsPerLine: 1000
			},
			afterContext: config.contextLines,
			beforeContext: config.contextLines,
			isSmartCase: this.configurationService.getValue<ISearchConfigurationProperties>('search').smartCase,
			expandPatterns: true
		};

		const folderResources = this.contextService.getWorkspace().folders;
		let query: ITextQuery;
		try {
			const queryBuilder = this.instantiationService.createInstance(QueryBuilder);
			query = queryBuilder.text(content, folderResources.map(folder => folder.uri), options);
		}
		catch (err) {
			return;
		}
		const searchModel = this.instantiationService.createInstance(SearchModel);
372 373
		this.searchOperation.start(500);
		await searchModel.search(query).finally(() => this.searchOperation.stop());
J
Jackson Kearl 已提交
374
		const input = this.getInput();
J
Jackson Kearl 已提交
375 376 377 378
		if (!input ||
			input !== startInput ||
			JSON.stringify(config) !== JSON.stringify(this.readConfigFromWidget())) {

379 380 381 382
			searchModel.dispose();
			return;
		}

383 384
		const controller = ReferencesController.get(this.searchResultEditor);
		controller.closeWidget(false);
385
		const labelFormatter = (uri: URI): string => this.labelService.getUriLabel(uri, { relative: true });
386
		const results = serializeSearchResultForEditor(searchModel.searchResult, config.includes, config.excludes, config.contextLines, labelFormatter, false);
J
Jackson Kearl 已提交
387
		const { header, body } = await input.getModels();
388 389
		this.modelService.updateModel(body, results.text);
		header.setValue(serializeSearchConfiguration(config));
390

J
Jackson Kearl 已提交
391
		input.setDirty(input.resource.scheme !== 'search-editor');
392
		input.setMatchRanges(results.matchRanges);
393

394 395 396 397 398 399 400 401
		searchModel.dispose();
	}

	layout(dimension: DOM.Dimension) {
		this.dimension = dimension;
		this.reLayout();
	}

402 403 404 405 406 407 408 409
	getSelected() {
		const selection = this.searchResultEditor.getSelection();
		if (selection) {
			return this.searchResultEditor.getModel()?.getValueInRange(selection) ?? '';
		}
		return '';
	}

410 411 412 413 414 415 416 417 418
	private reLayout() {
		if (this.dimension) {
			this.queryEditorWidget.setWidth(this.dimension.width - 28 /* container margin */);
			this.searchResultEditor.layout({ height: this.dimension.height - DOM.getTotalHeight(this.queryEditorContainer), width: this.dimension.width });
			this.inputPatternExcludes.setWidth(this.dimension.width - 28 /* container margin */);
			this.inputPatternIncludes.setWidth(this.dimension.width - 28 /* container margin */);
		}
	}

J
Jackson Kearl 已提交
419 420 421 422
	private getInput(): SearchEditorInput | undefined {
		return this._input as SearchEditorInput;
	}

423
	async setInput(newInput: SearchEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
J
Jackson Kearl 已提交
424 425
		this.saveViewState();

426 427
		await super.setInput(newInput, options, token);

428
		const { body, header } = await newInput.getModels();
429

430
		this.searchResultEditor.setModel(body);
431
		this.pauseSearching = true;
432

433
		const config = extractSearchQuery(header);
434
		this.toggleRunAgainMessage(body.getLineCount() === 1 && body.getValue() === '' && config.query !== '');
435 436 437 438 439 440 441 442 443 444

		this.queryEditorWidget.setValue(config.query, true);
		this.queryEditorWidget.searchInput.setCaseSensitive(config.caseSensitive);
		this.queryEditorWidget.searchInput.setRegex(config.regexp);
		this.queryEditorWidget.searchInput.setWholeWords(config.wholeWord);
		this.queryEditorWidget.setContextLines(config.contextLines);
		this.inputPatternExcludes.setValue(config.excludes);
		this.inputPatternIncludes.setValue(config.includes);
		this.inputPatternExcludes.setUseExcludesAndIgnoreFiles(config.useIgnores);
		this.toggleIncludesExcludes(config.showIncludesExcludes);
445

J
Jackson Kearl 已提交
446
		this.restoreViewState();
447 448 449
		this.pauseSearching = false;
	}

450
	private toggleIncludesExcludes(_shouldShow?: boolean): void {
451
		const cls = 'expanded';
452
		const shouldShow = _shouldShow ?? !DOM.hasClass(this.includesExcludesContainer, cls);
453 454 455 456 457 458 459 460 461

		if (shouldShow) {
			this.toggleQueryDetailsButton.setAttribute('aria-expanded', 'true');
			DOM.addClass(this.includesExcludesContainer, cls);
		} else {
			this.toggleQueryDetailsButton.setAttribute('aria-expanded', 'false');
			DOM.removeClass(this.includesExcludesContainer, cls);
		}

462 463
		this.showingIncludesExcludes = DOM.hasClass(this.includesExcludesContainer, cls);

464 465 466
		this.reLayout();
	}

467 468 469
	saveState() {
		this.saveViewState();
		super.saveState();
470
	}
471

J
Jackson Kearl 已提交
472
	private saveViewState() {
J
Jackson Kearl 已提交
473 474 475
		const resource = this.getInput()?.resource;
		if (resource) { this.saveTextEditorViewState(resource); }
	}
J
Jackson Kearl 已提交
476

J
Jackson Kearl 已提交
477 478 479 480 481 482 483
	protected retrieveTextEditorViewState(resource: URI): SearchEditorViewState | null {
		const control = this.getControl();
		const editorViewState = control.saveViewState();
		if (!editorViewState) { return null; }
		if (resource.toString() !== this.getInput()?.resource.toString()) { return null; }

		return { ...editorViewState, focused: this.searchResultEditor.hasWidgetFocus() ? 'editor' : 'input' };
J
Jackson Kearl 已提交
484 485
	}

486
	private loadViewState() {
J
Jackson Kearl 已提交
487 488
		const resource = assertIsDefined(this.input?.getResource());
		return this.loadTextEditorViewState(resource) as SearchEditorViewState;
489 490 491 492
	}

	private restoreViewState() {
		const viewState = this.loadViewState();
J
Jackson Kearl 已提交
493
		if (viewState) { this.searchResultEditor.restoreViewState(viewState); }
494
		if (viewState && viewState.focused === 'editor') {
J
Jackson Kearl 已提交
495 496 497 498 499 500
			this.searchResultEditor.focus();
		} else {
			this.queryEditorWidget.focus();
		}
	}

501
	clearInput() {
J
Jackson Kearl 已提交
502
		this.saveViewState();
503
		super.clearInput();
504
	}
J
Jackson Kearl 已提交
505 506 507 508

	getAriaLabel() {
		return this.getInput()?.getName() ?? localize('searchEditor', "Search Editor");
	}
509
}
510 511

registerThemingParticipant((theme, collector) => {
512
	collector.addRule(`.monaco-editor .${SearchEditorFindMatchClass} { background-color: ${theme.getColor(searchEditorFindMatch)}; }`);
513 514 515

	const findMatchHighlightBorder = theme.getColor(searchEditorFindMatchBorder);
	if (findMatchHighlightBorder) {
516
		collector.addRule(`.monaco-editor .${SearchEditorFindMatchClass} { border: 1px ${theme.type === 'hc' ? 'dotted' : 'solid'} ${findMatchHighlightBorder}; box-sizing: border-box; }`);
517 518
	}
});
519 520

export const searchEditorTextInputBorder = registerColor('searchEditor.textInputBorder', { dark: inputBorder, light: inputBorder, hc: inputBorder }, localize('textInputBoxBorder', "Search editor text input box border."));