-
Notifications
You must be signed in to change notification settings - Fork 29.8k
/
searchEditorInput.ts
390 lines (331 loc) · 15.5 KB
/
searchEditorInput.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'vs/css!./media/searchEditor';
import { Emitter, Event } from 'vs/base/common/event';
import { basename } from 'vs/base/common/path';
import { extname, isEqual, joinPath } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { Range } from 'vs/editor/common/core/range';
import { ITextModel, TrackedRangeStickiness } from 'vs/editor/common/model';
import { IModelService } from 'vs/editor/common/services/model';
import { localize } from 'vs/nls';
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { GroupIdentifier, IRevertOptions, ISaveOptions, EditorResourceAccessor, IMoveResult, EditorInputCapabilities, IUntypedEditorInput } from 'vs/workbench/common/editor';
import { Memento } from 'vs/workbench/common/memento';
import { SearchEditorFindMatchClass, SearchEditorInputTypeId, SearchEditorScheme, SearchEditorWorkingCopyTypeId } from 'vs/workbench/contrib/searchEditor/browser/constants';
import { SearchConfigurationModel, SearchEditorModel, searchEditorModelFactory } from 'vs/workbench/contrib/searchEditor/browser/searchEditorModel';
import { defaultSearchConfig, parseSavedSearchEditor, serializeSearchConfiguration } from 'vs/workbench/contrib/searchEditor/browser/searchEditorSerialization';
import { IPathService } from 'vs/workbench/services/path/common/pathService';
import { ITextFileSaveOptions, ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { IWorkingCopy, IWorkingCopyBackup, IWorkingCopySaveEvent, WorkingCopyCapabilities } from 'vs/workbench/services/workingCopy/common/workingCopy';
import { CancellationToken } from 'vs/base/common/cancellation';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ISearchComplete, ISearchConfigurationProperties } from 'vs/workbench/services/search/common/search';
import { bufferToReadable, VSBuffer } from 'vs/base/common/buffer';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { IResourceEditorInput } from 'vs/platform/editor/common/editor';
import { IDisposable } from 'vs/base/common/lifecycle';
import { Codicon } from 'vs/base/common/codicons';
import { ThemeIcon } from 'vs/base/common/themables';
import { registerIcon } from 'vs/platform/theme/common/iconRegistry';
export type SearchConfiguration = {
query: string;
filesToInclude: string;
filesToExclude: string;
contextLines: number;
matchWholeWord: boolean;
isCaseSensitive: boolean;
isRegexp: boolean;
useExcludeSettingsAndIgnoreFiles: boolean;
showIncludesExcludes: boolean;
onlyOpenEditors: boolean;
notebookSearchConfig: {
includeMarkupInput: boolean;
includeMarkupPreview: boolean;
includeCodeInput: boolean;
includeOutput: boolean;
};
};
export const SEARCH_EDITOR_EXT = '.code-search';
const SearchEditorIcon = registerIcon('search-editor-label-icon', Codicon.search, localize('searchEditorLabelIcon', 'Icon of the search editor label.'));
export class SearchEditorInput extends EditorInput {
static readonly ID: string = SearchEditorInputTypeId;
override get typeId(): string {
return SearchEditorInput.ID;
}
override get editorId(): string | undefined {
return this.typeId;
}
override getIcon(): ThemeIcon {
return SearchEditorIcon;
}
override get capabilities(): EditorInputCapabilities {
let capabilities = EditorInputCapabilities.Singleton;
if (!this.backingUri) {
capabilities |= EditorInputCapabilities.Untitled;
}
return capabilities;
}
private memento: Memento;
private dirty: boolean = false;
private lastLabel: string | undefined;
private readonly _onDidChangeContent = this._register(new Emitter<void>());
readonly onDidChangeContent: Event<void> = this._onDidChangeContent.event;
private readonly _onDidSave = this._register(new Emitter<IWorkingCopySaveEvent>());
readonly onDidSave: Event<IWorkingCopySaveEvent> = this._onDidSave.event;
private oldDecorationsIDs: string[] = [];
get resource() {
return this.backingUri || this.modelUri;
}
public ongoingSearchOperation: Promise<ISearchComplete> | undefined;
public model: SearchEditorModel;
private _cachedResultsModel: ITextModel | undefined;
private _cachedConfigurationModel: SearchConfigurationModel | undefined;
constructor(
public readonly modelUri: URI,
public readonly backingUri: URI | undefined,
@IModelService private readonly modelService: IModelService,
@ITextFileService protected readonly textFileService: ITextFileService,
@IFileDialogService private readonly fileDialogService: IFileDialogService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IWorkingCopyService private readonly workingCopyService: IWorkingCopyService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@IPathService private readonly pathService: IPathService,
@IStorageService storageService: IStorageService,
) {
super();
this.model = instantiationService.createInstance(SearchEditorModel, modelUri);
if (this.modelUri.scheme !== SearchEditorScheme) {
throw Error('SearchEditorInput must be invoked with a SearchEditorScheme uri');
}
this.memento = new Memento(SearchEditorInput.ID, storageService);
this._register(storageService.onWillSaveState(() => this.memento.saveMemento()));
const input = this;
const workingCopyAdapter = new class implements IWorkingCopy {
readonly typeId = SearchEditorWorkingCopyTypeId;
readonly resource = input.modelUri;
get name() { return input.getName(); }
readonly capabilities = input.hasCapability(EditorInputCapabilities.Untitled) ? WorkingCopyCapabilities.Untitled : WorkingCopyCapabilities.None;
readonly onDidChangeDirty = input.onDidChangeDirty;
readonly onDidChangeContent = input.onDidChangeContent;
readonly onDidSave = input.onDidSave;
isDirty(): boolean { return input.isDirty(); }
isModified(): boolean { return input.isDirty(); }
backup(token: CancellationToken): Promise<IWorkingCopyBackup> { return input.backup(token); }
save(options?: ISaveOptions): Promise<boolean> { return input.save(0, options).then(editor => !!editor); }
revert(options?: IRevertOptions): Promise<void> { return input.revert(0, options); }
};
this._register(this.workingCopyService.registerWorkingCopy(workingCopyAdapter));
}
override async save(group: GroupIdentifier, options?: ITextFileSaveOptions): Promise<EditorInput | undefined> {
if (((await this.resolveModels()).resultsModel).isDisposed()) { return; }
if (this.backingUri) {
await this.textFileService.write(this.backingUri, await this.serializeForDisk(), options);
this.setDirty(false);
this._onDidSave.fire({ reason: options?.reason, source: options?.source });
return this;
} else {
return this.saveAs(group, options);
}
}
public tryReadConfigSync(): SearchConfiguration | undefined {
return this._cachedConfigurationModel?.config;
}
private async serializeForDisk() {
const { configurationModel, resultsModel } = await this.resolveModels();
return serializeSearchConfiguration(configurationModel.config) + '\n' + resultsModel.getValue();
}
private configChangeListenerDisposable: IDisposable | undefined;
private registerConfigChangeListeners(model: SearchConfigurationModel) {
this.configChangeListenerDisposable?.dispose();
if (!this.isDisposed()) {
this.configChangeListenerDisposable = model.onConfigDidUpdate(() => {
if (this.lastLabel !== this.getName()) {
this._onDidChangeLabel.fire();
this.lastLabel = this.getName();
}
this.memento.getMemento(StorageScope.WORKSPACE, StorageTarget.MACHINE).searchConfig = model.config;
});
this._register(this.configChangeListenerDisposable);
}
}
async resolveModels() {
return this.model.resolve().then(data => {
this._cachedResultsModel = data.resultsModel;
this._cachedConfigurationModel = data.configurationModel;
if (this.lastLabel !== this.getName()) {
this._onDidChangeLabel.fire();
this.lastLabel = this.getName();
}
this.registerConfigChangeListeners(data.configurationModel);
return data;
});
}
override async saveAs(group: GroupIdentifier, options?: ITextFileSaveOptions): Promise<EditorInput | undefined> {
const path = await this.fileDialogService.pickFileToSave(await this.suggestFileName(), options?.availableFileSystems);
if (path) {
this.telemetryService.publicLog2<
{},
{
owner: 'roblourens';
comment: 'Fired when a search editor is saved';
}>
('searchEditor/saveSearchResults');
const toWrite = await this.serializeForDisk();
if (await this.textFileService.create([{ resource: path, value: toWrite, options: { overwrite: true } }])) {
this.setDirty(false);
if (!isEqual(path, this.modelUri)) {
const input = this.instantiationService.invokeFunction(getOrMakeSearchEditorInput, { fileUri: path, from: 'existingFile' });
input.setMatchRanges(this.getMatchRanges());
return input;
}
return this;
}
}
return undefined;
}
override getName(maxLength = 12): string {
const trimToMax = (label: string) => (label.length < maxLength ? label : `${label.slice(0, maxLength - 3)}...`);
if (this.backingUri) {
const originalURI = EditorResourceAccessor.getOriginalUri(this);
return localize('searchTitle.withQuery', "Search: {0}", basename((originalURI ?? this.backingUri).path, SEARCH_EDITOR_EXT));
}
const query = this._cachedConfigurationModel?.config?.query?.trim();
if (query) {
return localize('searchTitle.withQuery', "Search: {0}", trimToMax(query));
}
return localize('searchTitle', "Search");
}
setDirty(dirty: boolean) {
const wasDirty = this.dirty;
this.dirty = dirty;
if (wasDirty !== dirty) {
this._onDidChangeDirty.fire();
}
}
override isDirty() {
return this.dirty;
}
override async rename(group: GroupIdentifier, target: URI): Promise<IMoveResult | undefined> {
if (extname(target) === SEARCH_EDITOR_EXT) {
return {
editor: this.instantiationService.invokeFunction(getOrMakeSearchEditorInput, { from: 'existingFile', fileUri: target })
};
}
// Ignore move if editor was renamed to a different file extension
return undefined;
}
override dispose() {
this.modelService.destroyModel(this.modelUri);
super.dispose();
}
override matches(other: EditorInput | IUntypedEditorInput): boolean {
if (super.matches(other)) {
return true;
}
if (other instanceof SearchEditorInput) {
return !!(other.modelUri.fragment && other.modelUri.fragment === this.modelUri.fragment) || !!(other.backingUri && isEqual(other.backingUri, this.backingUri));
}
return false;
}
getMatchRanges(): Range[] {
return (this._cachedResultsModel?.getAllDecorations() ?? [])
.filter(decoration => decoration.options.className === SearchEditorFindMatchClass)
.filter(({ range }) => !(range.startColumn === 1 && range.endColumn === 1))
.map(({ range }) => range);
}
async setMatchRanges(ranges: Range[]) {
this.oldDecorationsIDs = (await this.resolveModels()).resultsModel.deltaDecorations(this.oldDecorationsIDs, ranges.map(range =>
({ range, options: { description: 'search-editor-find-match', className: SearchEditorFindMatchClass, stickiness: TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges } })));
}
override async revert(group: GroupIdentifier, options?: IRevertOptions) {
if (options?.soft) {
this.setDirty(false);
return;
}
if (this.backingUri) {
const { config, text } = await this.instantiationService.invokeFunction(parseSavedSearchEditor, this.backingUri);
const { resultsModel, configurationModel } = await this.resolveModels();
resultsModel.setValue(text);
configurationModel.updateConfig(config);
} else {
(await this.resolveModels()).resultsModel.setValue('');
}
super.revert(group, options);
this.setDirty(false);
}
private async backup(token: CancellationToken): Promise<IWorkingCopyBackup> {
const contents = await this.serializeForDisk();
if (token.isCancellationRequested) {
return {};
}
return {
content: bufferToReadable(VSBuffer.fromString(contents))
};
}
private async suggestFileName(): Promise<URI> {
const query = (await this.resolveModels()).configurationModel.config.query;
const searchFileName = (query.replace(/[^\w \-_]+/g, '_') || 'Search') + SEARCH_EDITOR_EXT;
return joinPath(await this.fileDialogService.defaultFilePath(this.pathService.defaultUriScheme), searchFileName);
}
override toUntyped(): IResourceEditorInput | undefined {
if (this.hasCapability(EditorInputCapabilities.Untitled)) {
return undefined;
}
return {
resource: this.resource,
options: {
override: SearchEditorInput.ID
}
};
}
}
export const getOrMakeSearchEditorInput = (
accessor: ServicesAccessor,
existingData: (
| { from: 'model'; config?: Partial<SearchConfiguration>; modelUri: URI; backupOf?: URI }
| { from: 'rawData'; resultsContents: string | undefined; config: Partial<SearchConfiguration> }
| { from: 'existingFile'; fileUri: URI })
): SearchEditorInput => {
const storageService = accessor.get(IStorageService);
const configurationService = accessor.get(IConfigurationService);
const instantiationService = accessor.get(IInstantiationService);
const modelUri = existingData.from === 'model' ? existingData.modelUri : URI.from({ scheme: SearchEditorScheme, fragment: `${Math.random()}` });
if (!searchEditorModelFactory.models.has(modelUri)) {
if (existingData.from === 'existingFile') {
instantiationService.invokeFunction(accessor => searchEditorModelFactory.initializeModelFromExistingFile(accessor, modelUri, existingData.fileUri));
} else {
const searchEditorSettings = configurationService.getValue<ISearchConfigurationProperties>('search').searchEditor;
const reuseOldSettings = searchEditorSettings.reusePriorSearchConfiguration;
const defaultNumberOfContextLines = searchEditorSettings.defaultNumberOfContextLines;
const priorConfig: SearchConfiguration = reuseOldSettings ? new Memento(SearchEditorInput.ID, storageService).getMemento(StorageScope.WORKSPACE, StorageTarget.MACHINE).searchConfig : {};
const defaultConfig = defaultSearchConfig();
const config = { ...defaultConfig, ...priorConfig, ...existingData.config };
if (defaultNumberOfContextLines !== null && defaultNumberOfContextLines !== undefined) {
config.contextLines = existingData?.config?.contextLines ?? defaultNumberOfContextLines;
}
if (existingData.from === 'rawData') {
if (existingData.resultsContents) {
config.contextLines = 0;
}
instantiationService.invokeFunction(accessor => searchEditorModelFactory.initializeModelFromRawData(accessor, modelUri, config, existingData.resultsContents));
} else {
instantiationService.invokeFunction(accessor => searchEditorModelFactory.initializeModelFromExistingModel(accessor, modelUri, config));
}
}
}
return instantiationService.createInstance(
SearchEditorInput,
modelUri,
existingData.from === 'existingFile'
? existingData.fileUri
: existingData.from === 'model'
? existingData.backupOf
: undefined);
};